Skip to content

Commit e460cda

Browse files
feat: ParenthesedSelectBody and ParenthesedFromItem
- First properly working version - Work in progress, 13 tests failing
1 parent 500046f commit e460cda

23 files changed

Lines changed: 499 additions & 420 deletions

src/main/java/net/sf/jsqlparser/parser/CCJSqlParserUtil.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ public static Statement parse(String sql, Consumer<CCJSqlParser> consumer) throw
7979
consumer.accept(parser);
8080
}
8181
statement = parseStatement(parser);
82+
} else {
83+
throw ex;
8284
}
8385
}
8486
return statement;

src/main/java/net/sf/jsqlparser/parser/feature/Feature.java

Lines changed: 19 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ public enum Feature {
113113
limitOffset,
114114
/**
115115
* "OFFSET offset"
116+
*
116117
* @see Offset
117118
*/
118119
offset,
@@ -129,12 +130,12 @@ public enum Feature {
129130
fetch,
130131
/**
131132
* "FETCH FIRST row_count (ROW | ROWS) ONLY"
133+
*
132134
* @see Fetch#isFetchParamFirst()
133135
*/
134136
fetchFirst,
135137
/**
136-
* "FETCH NEXT row_count (ROW | ROWS) ONLY"
137-
* if not {@link #fetchFirst}
138+
* "FETCH NEXT row_count (ROW | ROWS) ONLY" if not {@link #fetchFirst}
138139
*
139140
* @see Fetch#isFetchParamFirst()
140141
*/
@@ -193,8 +194,7 @@ public enum Feature {
193194
*/
194195
joinApply,
195196

196-
joinWindow,
197-
joinUsingColumns,
197+
joinWindow, joinUsingColumns,
198198

199199
/**
200200
* "SKIP variable" | "SKIP ?" | "SKIP rowCount"
@@ -203,9 +203,7 @@ public enum Feature {
203203
*/
204204
skip,
205205
/**
206-
* "FIRST" \?|[0-9]+|variable
207-
* or
208-
* "LIMIT" \?|[0-9]+|variable
206+
* "FIRST" \?|[0-9]+|variable or "LIMIT" \?|[0-9]+|variable
209207
*
210208
* @see First
211209
*/
@@ -328,9 +326,7 @@ public enum Feature {
328326
/**
329327
* UPDATE table SET (col, ...) = (SELECT col, ... )"
330328
*/
331-
updateUseSelect,
332-
updateOrderBy,
333-
updateLimit,
329+
updateUseSelect, updateOrderBy, updateLimit,
334330
/**
335331
* "RETURNING expr(, expr)*"
336332
*
@@ -429,8 +425,7 @@ public enum Feature {
429425
*
430426
* @see Execute
431427
*/
432-
execute,
433-
executeExec, executeCall, executeExecute,
428+
execute, executeExec, executeCall, executeExecute,
434429

435430
/**
436431
* SQL "EXECUTE" statement is allowed
@@ -454,13 +449,7 @@ public enum Feature {
454449
*
455450
* @see Drop
456451
*/
457-
drop,
458-
dropTable,
459-
dropIndex,
460-
dropView,
461-
dropSchema,
462-
dropSequence,
463-
dropTableIfExists, dropIndexIfExists, dropViewIfExists, dropSchemaIfExists, dropSequenceIfExists,
452+
drop, dropTable, dropIndex, dropView, dropSchema, dropSequence, dropTableIfExists, dropIndexIfExists, dropViewIfExists, dropSchemaIfExists, dropSequenceIfExists,
464453

465454
/**
466455
* SQL "CREATE SCHEMA" statement is allowed
@@ -650,17 +639,12 @@ public enum Feature {
650639
*/
651640
pivotXml,
652641

653-
setOperation,
654-
setOperationUnion,
655-
setOperationIntersect,
656-
setOperationExcept,
657-
setOperationMinus,
642+
setOperation, setOperationUnion, setOperationIntersect, setOperationExcept, setOperationMinus,
658643

659644
/**
660645
* "WITH name query"
661646
*/
662-
withItem,
663-
withItemRecursive,
647+
withItem, withItemRecursive,
664648

665649
lateralSubSelect,
666650
/**
@@ -722,14 +706,11 @@ public enum Feature {
722706
*
723707
* @see OracleHierarchicalExpression
724708
*/
725-
oracleHierarchicalExpression,
726-
oracleOrderBySiblings,
709+
oracleHierarchicalExpression, oracleOrderBySiblings,
727710

728711
// MYSQL
729712

730-
mySqlHintStraightJoin,
731-
mysqlSqlCacheFlag,
732-
mysqlCalcFoundRows,
713+
mySqlHintStraightJoin, mysqlSqlCacheFlag, mysqlCalcFoundRows,
733714

734715
// SQLSERVER
735716

@@ -744,32 +725,30 @@ public enum Feature {
744725
allowSquareBracketQuotation(false),
745726

746727
/**
747-
* allow parsing of RDBMS specific syntax by switching off SQL Standard
748-
* Compliant Syntax
728+
* allow parsing of RDBMS specific syntax by switching off SQL Standard Compliant Syntax
749729
*/
750730
allowPostgresSpecificSyntax(false),
751731

752732
// PERFORMANCE
753733

754734
/**
755-
* allows complex expression parameters or named parameters for functions
756-
* will be switched off, when deep nesting of functions is detected
735+
* allows complex expression parameters or named parameters for functions will be switched off,
736+
* when deep nesting of functions is detected
757737
*/
758738
allowComplexParsing(true),
759739

760740
/**
761-
* allows passing through Unsupported Statements as a plain List of Tokens
762-
* needs to be switched off, when VALIDATING statements or parsing blocks
741+
* allows passing through Unsupported Statements as a plain List of Tokens needs to be switched
742+
* off, when VALIDATING statements or parsing blocks
763743
*/
764744
allowUnsupportedStatements(false),
765745

766-
timeOut( 6000),
746+
timeOut(6000),
767747

768748
/**
769749
* allows Backslash '\' as Escape Character
770750
*/
771-
allowBackslashEscapeCharacter(false),
772-
;
751+
allowBackslashEscapeCharacter(false),;
773752

774753
private final Object value;
775754
private final boolean configurable;

src/main/java/net/sf/jsqlparser/statement/insert/Insert.java

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,6 @@
99
*/
1010
package net.sf.jsqlparser.statement.insert;
1111

12-
import java.util.ArrayList;
13-
import java.util.Collection;
14-
import java.util.Collections;
15-
import java.util.Iterator;
16-
import java.util.List;
17-
import java.util.Optional;
18-
1912
import net.sf.jsqlparser.expression.Expression;
2013
import net.sf.jsqlparser.expression.OracleHint;
2114
import net.sf.jsqlparser.expression.RowConstructor;
@@ -30,10 +23,16 @@
3023
import net.sf.jsqlparser.statement.select.Select;
3124
import net.sf.jsqlparser.statement.select.SelectBody;
3225
import net.sf.jsqlparser.statement.select.SelectItem;
33-
import net.sf.jsqlparser.statement.select.SetOperationList;
3426
import net.sf.jsqlparser.statement.select.WithItem;
3527
import net.sf.jsqlparser.statement.values.ValuesStatement;
3628

29+
import java.util.ArrayList;
30+
import java.util.Collection;
31+
import java.util.Collections;
32+
import java.util.Iterator;
33+
import java.util.List;
34+
import java.util.Optional;
35+
3736
@SuppressWarnings({"PMD.CyclomaticComplexity"})
3837
public class Insert implements Statement {
3938

@@ -103,27 +102,19 @@ public void setColumns(List<Column> list) {
103102
public ItemsList getItemsList() {
104103
if (select!=null) {
105104
SelectBody selectBody = select.getSelectBody();
106-
if (selectBody instanceof SetOperationList) {
107-
SetOperationList setOperationList = (SetOperationList) selectBody;
108-
List<SelectBody> selects = setOperationList.getSelects();
109-
110-
if (selects.size() == 1) {
111-
SelectBody selectBody1 = selects.get(0);
112-
if (selectBody1 instanceof ValuesStatement) {
113-
ValuesStatement valuesStatement = (ValuesStatement) selectBody1;
114-
if (valuesStatement.getExpressions() instanceof ExpressionList) {
115-
ExpressionList expressionList = (ExpressionList) valuesStatement.getExpressions();
116-
117-
if (expressionList.getExpressions().size() == 1 && expressionList.getExpressions().get(0) instanceof RowConstructor) {
118-
RowConstructor rowConstructor = (RowConstructor) expressionList.getExpressions().get(0);
119-
return rowConstructor.getExprList();
120-
} else {
121-
return expressionList;
122-
}
123-
} else {
124-
return valuesStatement.getExpressions();
125-
}
105+
if (selectBody instanceof ValuesStatement) {
106+
ValuesStatement valuesStatement = (ValuesStatement) selectBody;
107+
if (valuesStatement.getExpressions() instanceof ExpressionList) {
108+
ExpressionList expressionList = (ExpressionList) valuesStatement.getExpressions();
109+
110+
if (expressionList.getExpressions().size() == 1 && expressionList.getExpressions().get(0) instanceof RowConstructor) {
111+
RowConstructor rowConstructor = (RowConstructor) expressionList.getExpressions().get(0);
112+
return rowConstructor.getExprList();
113+
} else {
114+
return expressionList;
126115
}
116+
} else {
117+
return valuesStatement.getExpressions();
127118
}
128119
}
129120
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package net.sf.jsqlparser.statement.select;
2+
3+
public class ParenthesedJoin extends Join {
4+
private Join join;
5+
6+
public Join getJoin() {
7+
return join;
8+
}
9+
10+
public void setJoin(Join join) {
11+
this.join = join;
12+
}
13+
14+
public ParenthesedJoin withJoin(Join join) {
15+
setJoin(join);
16+
return this;
17+
}
18+
19+
@Override
20+
public String toString() {
21+
return "(" + super.toString() + ")";
22+
}
23+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package net.sf.jsqlparser.statement.select;
2+
3+
public class ParenthesedSelectBody implements SelectBody {
4+
private SelectBody selectBody;
5+
6+
public SelectBody getSelectBody() {
7+
return selectBody;
8+
}
9+
10+
public void setSelectBody(SelectBody selectBody) {
11+
this.selectBody = selectBody;
12+
}
13+
14+
public ParenthesedSelectBody withSelectBody(SelectBody selectBody) {
15+
setSelectBody(selectBody);
16+
return this;
17+
}
18+
19+
@Override
20+
public void accept(SelectVisitor selectVisitor) {
21+
selectVisitor.visit(this);
22+
}
23+
24+
public String toString() {
25+
return "(" + selectBody + ")";
26+
}
27+
}

src/main/java/net/sf/jsqlparser/statement/select/ParenthesisFromItem.java

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,17 @@
1111

1212
import net.sf.jsqlparser.expression.Alias;
1313

14-
public class ParenthesisFromItem implements FromItem {
14+
import java.util.ArrayList;
15+
import java.util.Collections;
16+
import java.util.List;
17+
import java.util.Optional;
1518

19+
public class ParenthesisFromItem implements FromItem {
1620
private FromItem fromItem;
17-
21+
private List<Join> joins;
1822
private Alias alias;
23+
private Pivot pivot;
24+
private UnPivot unPivot;
1925

2026
public ParenthesisFromItem() {
2127
}
@@ -32,14 +38,54 @@ public final void setFromItem(FromItem fromItem) {
3238
this.fromItem = fromItem;
3339
}
3440

41+
public List<Join> getJoins() {
42+
return joins;
43+
}
44+
45+
public FromItem addJoins(Join... joins) {
46+
List<Join> list = Optional.ofNullable(getJoins()).orElseGet(ArrayList::new);
47+
Collections.addAll(list, joins);
48+
return withJoins(list);
49+
}
50+
51+
public FromItem withJoins(List<Join> joins) {
52+
this.setJoins(joins);
53+
return this;
54+
}
55+
56+
public void setJoins(List<Join> list) {
57+
joins = list;
58+
}
59+
3560
@Override
3661
public void accept(FromItemVisitor fromItemVisitor) {
3762
fromItemVisitor.visit(this);
3863
}
3964

65+
public StringBuilder appendTo(StringBuilder builder) {
66+
builder.append("(");
67+
builder.append(fromItem);
68+
if (joins != null) {
69+
for (Join join : joins) {
70+
if (join.isSimple()) {
71+
builder.append(", ").append(join);
72+
} else {
73+
builder.append(" ").append(join);
74+
}
75+
}
76+
}
77+
builder.append(")");
78+
79+
if (alias!=null) {
80+
builder.append(alias);
81+
}
82+
83+
return builder;
84+
}
85+
4086
@Override
4187
public String toString() {
42-
return "(" + fromItem + ")" + (alias != null ? alias.toString() : "");
88+
return appendTo(new StringBuilder()).toString();
4389
}
4490

4591
@Override
@@ -54,22 +100,22 @@ public void setAlias(Alias alias) {
54100

55101
@Override
56102
public Pivot getPivot() {
57-
return null;
103+
return pivot;
58104
}
59105

60106
@Override
61107
public void setPivot(Pivot pivot) {
62-
throw new UnsupportedOperationException("Not supported yet.");
108+
this.pivot = pivot;
63109
}
64110

65111
@Override
66112
public UnPivot getUnPivot() {
67-
return null;
113+
return unPivot;
68114
}
69115

70116
@Override
71117
public void setUnPivot(UnPivot unpivot) {
72-
throw new UnsupportedOperationException("Not supported yet.");
118+
this.unPivot = unpivot;
73119
}
74120

75121
public ParenthesisFromItem withFromItem(FromItem fromItem) {

0 commit comments

Comments
 (0)