Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ public class ParserKeywordsUtils {
{"OVERWRITE ", RESTRICTED_JSQLPARSER},
{"PIVOT", RESTRICTED_JSQLPARSER},
{"PREFERRING", RESTRICTED_JSQLPARSER},
{"PREWHERE", RESTRICTED_JSQLPARSER},
{"PRIOR", RESTRICTED_ALIAS},
{"PROCEDURE", RESTRICTED_ALIAS},
{"PUBLIC", RESTRICTED_ALIAS},
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/net/sf/jsqlparser/statement/select/PlainSelect.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class PlainSelect extends Select {
private FromItem fromItem;
private List<LateralView> lateralViews;
private List<Join> joins;
private Expression preWhere;
private Expression where;
private GroupByElement groupBy;
private Expression having;
Expand Down Expand Up @@ -160,6 +161,14 @@ public void setWhere(Expression where) {
this.where = where;
}

public Expression getPreWhere() {
return preWhere;
}

public void setPreWhere(Expression preWhere) {
this.preWhere = preWhere;
}

public PlainSelect withFromItem(FromItem item) {
this.setFromItem(item);
return this;
Expand Down Expand Up @@ -569,6 +578,9 @@ public StringBuilder appendSelectBodyTo(StringBuilder builder) {
if (ksqlWindow != null) {
builder.append(" WINDOW ").append(ksqlWindow);
}
if (preWhere != null) {
builder.append(" PREWHERE ").append(preWhere);
}
if (where != null) {
builder.append(" WHERE ").append(where);
}
Expand Down Expand Up @@ -597,6 +609,9 @@ public StringBuilder appendSelectBodyTo(StringBuilder builder) {
}
} else {
// without from
if (preWhere != null) {
builder.append(" PREWHERE ").append(preWhere);
}
if (where != null) {
builder.append(" WHERE ").append(where);
}
Expand Down Expand Up @@ -669,6 +684,11 @@ public PlainSelect withWhere(Expression where) {
return this;
}

public PlainSelect withPreWhere(Expression preWhere) {
this.setPreWhere(preWhere);
return this;
}

public PlainSelect withOptimizeFor(OptimizeFor optimizeFor) {
this.setOptimizeFor(optimizeFor);
return this;
Expand Down Expand Up @@ -767,6 +787,10 @@ public <E extends Expression> E getWhere(Class<E> type) {
return type.cast(getWhere());
}

public <E extends Expression> E getPreWhere(Class<E> type) {
return type.cast(getPreWhere());
}

public <E extends Expression> E getHaving(Class<E> type) {
return type.cast(getHaving());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ public <S> T visit(PlainSelect plainSelect, S context) {
// //@todo: implement
// }

expressionVisitor.visitExpression(plainSelect.getPreWhere(), context);
expressionVisitor.visitExpression(plainSelect.getWhere(), context);

// if (plainSelect.getOracleHierarchical() != null) {
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/net/sf/jsqlparser/util/TablesNamesFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,9 @@ public <S> Void visit(PlainSelect plainSelect, S context) {
}

visitJoins(plainSelect.getJoins(), context);
if (plainSelect.getPreWhere() != null) {
plainSelect.getPreWhere().accept(this, context);
}
if (plainSelect.getWhere() != null) {
plainSelect.getWhere().accept(this, context);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ public <S> StringBuilder visit(PlainSelect plainSelect, S context) {
builder.append(plainSelect.getKsqlWindow().toString());
}

deparsePreWhereClause(plainSelect);
deparseWhereClause(plainSelect);

if (plainSelect.getOracleHierarchical() != null) {
Expand Down Expand Up @@ -394,6 +395,13 @@ protected void deparseWhereClause(PlainSelect plainSelect) {
}
}

protected void deparsePreWhereClause(PlainSelect plainSelect) {
if (plainSelect.getPreWhere() != null) {
builder.append(" PREWHERE ");
plainSelect.getPreWhere().accept(expressionVisitor, null);
}
}

protected void deparseDistinctClause(Distinct distinct) {
if (distinct != null) {
if (distinct.isUseUnique()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ public <S> Void visit(PlainSelect plainSelect, S context) {
// validateOptionalList(plainSelect.getSelectItems(), () -> this, SelectItem::accept,
// context);

validateOptionalExpression(plainSelect.getPreWhere());
validateOptionalExpression(plainSelect.getWhere());
validateOptionalExpression(plainSelect.getOracleHierarchical());

Expand Down
12 changes: 12 additions & 0 deletions src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,7 @@ TOKEN: /* SQL Keywords. prefixed with K_ to avoid name clashes */
| <K_PLUS:"PLUS">
| <K_PREFERRING: "PREFERRING">
| <K_PRECEDING: "PRECEDING">
| <K_PREWHERE: "PREWHERE">
| <K_PRIMARY:"PRIMARY">
| <K_PRIOR:"PRIOR">
| <K_PROCEDURE:"PROCEDURE">
Expand Down Expand Up @@ -4118,6 +4119,7 @@ PlainSelect PlainSelect() #PlainSelect:
List<LateralView> lateralViews = null;
List<Join> joins = null;
List<SelectItem<?>> distinctOn = null;
Expression preWhere = null;
Expression where = null;
ForClause forClause = null;
List<OrderByElement> orderByElements;
Expand Down Expand Up @@ -4213,6 +4215,7 @@ PlainSelect PlainSelect() #PlainSelect:
[ LOOKAHEAD(2) <K_FINAL> { plainSelect.setUsingFinal(true); } ]

[ LOOKAHEAD(2) ksqlWindow=KSQLWindowClause() { plainSelect.setKsqlWindow(ksqlWindow); } ]
[ LOOKAHEAD(2) preWhere=PreWhereClause() { plainSelect.setPreWhere(preWhere); }]
[ LOOKAHEAD(2) where=WhereClause() { plainSelect.setWhere(where); }]
[ LOOKAHEAD(2) oracleHierarchicalQueryClause=OracleHierarchicalQueryClause() { plainSelect.setOracleHierarchical(oracleHierarchicalQueryClause); } ]
[ LOOKAHEAD(2) preferringClause=PreferringClause() { plainSelect.setPreferringClause(preferringClause); }
Expand Down Expand Up @@ -5088,6 +5091,15 @@ Expression WhereClause():
{ return retval; }
}

Expression PreWhereClause():
{
Expression retval = null;
}
{
<K_PREWHERE> retval=Expression()
{ return retval; }
}

OracleHierarchicalExpression OracleHierarchicalQueryClause():
{
OracleHierarchicalExpression result = new OracleHierarchicalExpression();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,21 @@ public void execute() throws Throwable {
}
}, "Fail when restricted keyword GLOBAL is used as an Alias.");
}

@Test
public void testPreWhereClause() throws JSQLParserException {
String sqlStr = "SELECT * FROM table1 PREWHERE column_name = 'value'";
PlainSelect select = (PlainSelect) assertSqlCanBeParsedAndDeparsed(sqlStr, true);
Assertions.assertNotNull(select.getPreWhere());
Assertions.assertNull(select.getWhere());
}

@Test
public void testPreWhereWithWhereClause() throws JSQLParserException {
String sqlStr =
"SELECT * FROM table1 PREWHERE column_name = 'value' WHERE id > 10";
PlainSelect select = (PlainSelect) assertSqlCanBeParsedAndDeparsed(sqlStr, true);
Assertions.assertNotNull(select.getPreWhere());
Assertions.assertNotNull(select.getWhere());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ public void testGetTablesWithXor() throws Exception {
assertThat(TablesNamesFinder.findTables(sqlStr)).containsExactlyInAnyOrder("MY_TABLE1");
}

@Test
public void testGetTablesWithPreWhere() throws Exception {
String sqlStr =
"SELECT * FROM MY_TABLE1 PREWHERE ID IN (SELECT ID FROM MY_TABLE2)";
assertThat(TablesNamesFinder.findTables(sqlStr)).containsExactlyInAnyOrder("MY_TABLE1",
"MY_TABLE2");
}

@Test
public void testGetTablesWithStmt() throws Exception {
String sqlStr =
Expand Down Expand Up @@ -734,4 +742,3 @@ void testNestedTablesInJsonObject() throws JSQLParserException {
"table2", "table3");
}
}