Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/main/java/net/sf/jsqlparser/statement/delete/Delete.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import net.sf.jsqlparser.statement.select.FromItem;
import net.sf.jsqlparser.statement.select.Join;
import net.sf.jsqlparser.statement.select.Limit;
import net.sf.jsqlparser.statement.select.OptionClause;
import net.sf.jsqlparser.statement.select.OrderByElement;
import net.sf.jsqlparser.statement.select.PlainSelect;
import net.sf.jsqlparser.statement.select.WithItem;
Expand All @@ -45,6 +46,7 @@ public class Delete implements Statement {
private Expression where;
private PreferringClause preferringClause;
private Limit limit;
private OptionClause option;
private List<OrderByElement> orderByElements;
private boolean hasFrom = true;
private DeleteModifierPriority modifierPriority;
Expand Down Expand Up @@ -147,6 +149,19 @@ public Limit getLimit() {
return limit;
}

public OptionClause getOption() {
return option;
}

public Delete setOption(OptionClause option) {
this.option = option;
return this;
}

public Delete withOption(OptionClause option) {
return setOption(option);
}

public void setLimit(Limit limit) {
this.limit = limit;
}
Expand Down Expand Up @@ -280,6 +295,10 @@ public String toString() {
b.append(limit);
}

if (option != null) {
b.append(option);
}

if (returningClause != null) {
returningClause.appendTo(b);
}
Expand Down
59 changes: 59 additions & 0 deletions src/main/java/net/sf/jsqlparser/statement/select/OptionClause.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*-
* #%L
* JSQLParser library
* %%
* Copyright (C) 2004 - 2019 JSQLParser
* %%
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
* #L%
*/
package net.sf.jsqlparser.statement.select;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
* Models the SQL Server (T-SQL) {@code OPTION (...)} query hint clause, which attaches a list of
* {@link OptionHint}s to the end of a {@code SELECT}, {@code UPDATE} or {@code DELETE} statement,
* see <a href="https://learn.microsoft.com/en-us/sql/t-sql/queries/hints-transact-sql-query">Hints
* (Transact-SQL) - Query Hints</a>.
*/
public class OptionClause implements Serializable {

private List<OptionHint> optionHints;

public OptionClause() {
super();
}

public OptionClause(List<OptionHint> optionHints) {
this.optionHints = optionHints;
}

public List<OptionHint> getOptionHints() {
return optionHints;
}

public void setOptionHints(List<OptionHint> optionHints) {
this.optionHints = optionHints;
}

public OptionClause withOptionHints(List<OptionHint> optionHints) {
setOptionHints(optionHints);
return this;
}

public OptionClause addOptionHint(OptionHint optionHint) {
if (optionHints == null) {
optionHints = new ArrayList<>();
}
optionHints.add(optionHint);
return this;
}

@Override
public String toString() {
return " OPTION (" + Select.getStringList(optionHints, true, false) + ")";
}
}
113 changes: 113 additions & 0 deletions src/main/java/net/sf/jsqlparser/statement/select/OptionHint.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*-
* #%L
* JSQLParser library
* %%
* Copyright (C) 2004 - 2019 JSQLParser
* %%
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
* #L%
*/
package net.sf.jsqlparser.statement.select;

import java.io.Serializable;

import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.operators.relational.ExpressionList;

/**
* Models one hint of the SQL Server (T-SQL) {@code OPTION (...)} query hint clause, see
* <a href="https://learn.microsoft.com/en-us/sql/t-sql/queries/hints-transact-sql-query">Hints
* (Transact-SQL) - Query Hints</a>.
* <p>
* A hint is a (possibly multi-word) keyword such as {@code RECOMPILE}, {@code HASH JOIN} or
* {@code OPTIMIZE FOR UNKNOWN}, optionally followed by a single value argument ({@code FAST 100},
* {@code MAXDOP 4}, {@code MAX_GRANT_PERCENT = 25}) or by a parenthesized argument list
* ({@code USE HINT ('...')}, {@code OPTIMIZE FOR (@p = 1)}, {@code TABLE HINT (t, INDEX (i))}).
*/
public class OptionHint implements Serializable {

private String name;
private Expression value;
private ExpressionList<Expression> parameters;
private boolean useEquals = false;

public OptionHint() {
super();
}

public OptionHint(String name) {
this.name = name;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public OptionHint withName(String name) {
setName(name);
return this;
}

public Expression getValue() {
return value;
}

public void setValue(Expression value) {
this.value = value;
}

public OptionHint withValue(Expression value) {
setValue(value);
return this;
}

public ExpressionList<Expression> getParameters() {
return parameters;
}

public void setParameters(ExpressionList<Expression> parameters) {
this.parameters = parameters;
}

public OptionHint withParameters(ExpressionList<Expression> parameters) {
setParameters(parameters);
return this;
}

public OptionHint addParameter(Expression parameter) {
if (parameters == null) {
parameters = new ExpressionList<>();
}
parameters.add(parameter);
return this;
}

public boolean isUseEquals() {
return useEquals;
}

public void setUseEquals(boolean useEquals) {
this.useEquals = useEquals;
}

public OptionHint withUseEquals(boolean useEquals) {
setUseEquals(useEquals);
return this;
}

@Override
public String toString() {
StringBuilder b = new StringBuilder(name);
if (value != null) {
b.append(useEquals ? " = " : " ").append(value);
}
if (parameters != null) {
b.append(" (").append(Select.getStringList(parameters, true, false)).append(')');
}
return b.toString();
}
}
18 changes: 18 additions & 0 deletions src/main/java/net/sf/jsqlparser/statement/select/Select.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public abstract class Select extends ASTNodeAccessImpl implements Statement, Exp
boolean oracleSiblings = false;

ForClause forClause = null;
OptionClause option = null;

List<OrderByElement> orderByElements;
List<InterpolateElement> interpolate;
Expand Down Expand Up @@ -195,6 +196,19 @@ public Select setForClause(ForClause forClause) {
return this;
}

public OptionClause getOption() {
return option;
}

public Select setOption(OptionClause option) {
this.option = option;
return this;
}

public Select withOption(OptionClause option) {
return setOption(option);
}

public List<OrderByElement> getOrderByElements() {
return orderByElements;
}
Expand Down Expand Up @@ -494,6 +508,10 @@ public StringBuilder appendTo(StringBuilder builder) {
forClause.appendTo(builder);
}

if (option != null) {
builder.append(option);
}

if (limitBy != null) {
builder.append(limitBy);
}
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/net/sf/jsqlparser/statement/update/Update.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import net.sf.jsqlparser.statement.select.FromItem;
import net.sf.jsqlparser.statement.select.Join;
import net.sf.jsqlparser.statement.select.Limit;
import net.sf.jsqlparser.statement.select.OptionClause;
import net.sf.jsqlparser.statement.select.OrderByElement;
import net.sf.jsqlparser.statement.select.PlainSelect;
import net.sf.jsqlparser.statement.select.Select;
Expand Down Expand Up @@ -48,6 +49,7 @@ public class Update implements Statement {
private OracleHint oracleHint = null;
private List<OrderByElement> orderByElements;
private Limit limit;
private OptionClause option;
private ReturningClause returningClause;
private UpdateModifierPriority modifierPriority;
private boolean modifierIgnore;
Expand Down Expand Up @@ -255,6 +257,19 @@ public Limit getLimit() {
return limit;
}

public OptionClause getOption() {
return option;
}

public Update setOption(OptionClause option) {
this.option = option;
return this;
}

public Update withOption(OptionClause option) {
return setOption(option);
}

public void setLimit(Limit limit) {
this.limit = limit;
}
Expand Down Expand Up @@ -356,6 +371,10 @@ public String toString() {
b.append(limit);
}

if (option != null) {
b.append(option);
}

if (returningClause != null) {
returningClause.appendTo(b);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ public void deParse(Delete delete) {
new LimitDeparser(expressionVisitor, builder).deParse(delete.getLimit());
}

if (delete.getOption() != null) {
builder.append(delete.getOption());
}

if (delete.getReturningClause() != null) {
delete.getReturningClause().appendTo(builder);
}
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/net/sf/jsqlparser/util/deparser/SelectDeParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ public <S> StringBuilder visit(ParenthesedSelect select, S context) {
deParseInterpolate(select.getInterpolate());
}

if (select.getOption() != null) {
builder.append(select.getOption());
}

Alias alias = select.getAlias();
if (alias != null) {
builder.append(alias);
Expand Down Expand Up @@ -345,6 +349,10 @@ public <S> StringBuilder visit(PlainSelect plainSelect, S context) {
plainSelect.getForClause().appendTo(builder);
}

if (plainSelect.getOption() != null) {
builder.append(plainSelect.getOption());
}

if (plainSelect.isEmitChanges()) {
builder.append(" EMIT CHANGES");
}
Expand Down Expand Up @@ -758,6 +766,10 @@ public <S> StringBuilder visit(SetOperationList list, S context) {
deParseInterpolate(list.getInterpolate());
}

if (list.getOption() != null) {
builder.append(list.getOption());
}

if (list.getLimit() != null) {
new LimitDeparser(expressionVisitor, builder).deParse(list.getLimit());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ public void deParse(Update update) {
new LimitDeparser(expressionVisitor, builder).deParse(update.getLimit());
}

if (update.getOption() != null) {
builder.append(update.getOption());
}

if (update.getReturningClause() != null) {
update.getReturningClause().appendTo(builder);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import net.sf.jsqlparser.parser.feature.Feature;
import net.sf.jsqlparser.statement.delete.Delete;
import net.sf.jsqlparser.statement.select.OptionHint;
import net.sf.jsqlparser.util.validation.ValidationCapability;

/**
Expand Down Expand Up @@ -48,6 +49,15 @@ public void validate(Delete delete) {
getValidator(LimitValidator.class).validate(delete.getLimit());
}

if (delete.getOption() != null) {
for (OptionHint optionHint : delete.getOption().getOptionHints()) {
validateOptionalExpression(optionHint.getValue());
if (optionHint.getParameters() != null) {
optionHint.getParameters().forEach(this::validateOptionalExpression);
}
}
}

if (delete.getReturningClause() != null) {
delete.getReturningClause().forEach(c -> c.accept(v, null));
}
Expand Down
Loading
Loading