1414import java .util .Collections ;
1515import java .util .List ;
1616import java .util .Optional ;
17+ import net .sf .jsqlparser .expression .operators .relational .ExpressionList ;
18+ import net .sf .jsqlparser .expression .operators .relational .ItemsList ;
1719
1820public class WithItem implements SelectBody {
1921
2022 private String name ;
2123 private List <SelectItem > withItemList ;
22- private SelectBody selectBody ;
24+ private ItemsList itemsList ;
25+ private boolean useValues = true ;
26+ private boolean useBracketsForValues = false ;
27+
28+ private SubSelect subSelect ;
2329 private boolean recursive ;
2430
31+ /**
32+ * Get the values (as VALUES (...) or SELECT)
33+ *
34+ * @return the values of the insert
35+ */
36+ public ItemsList getItemsList () {
37+ return itemsList ;
38+ }
39+
40+ public void setItemsList (ItemsList list ) {
41+ itemsList = list ;
42+ }
43+
44+ public boolean isUseValues () {
45+ return useValues ;
46+ }
47+
48+ public void setUseValues (boolean useValues ) {
49+ this .useValues = useValues ;
50+ }
51+
52+ public WithItem withItemsList (ItemsList itemsList ) {
53+ this .setItemsList (itemsList );
54+ return this ;
55+ }
56+
57+ public WithItem withUseValues (boolean useValues ) {
58+ this .setUseValues (useValues );
59+ return this ;
60+ }
61+
62+ public boolean isUsingBracketsForValues () {
63+ return useBracketsForValues ;
64+ }
65+
66+ public void setUseBracketsForValues (boolean useBracketsForValues ) {
67+ this .useBracketsForValues = useBracketsForValues ;
68+ }
69+
70+ public WithItem withUseBracketsForValues (boolean useBracketsForValues ) {
71+ this .setUseBracketsForValues (useBracketsForValues );
72+ return this ;
73+ }
74+
2575 public String getName () {
2676 return name ;
2777 }
@@ -38,12 +88,12 @@ public void setRecursive(boolean recursive) {
3888 this .recursive = recursive ;
3989 }
4090
41- public SelectBody getSelectBody () {
42- return selectBody ;
91+ public SubSelect getSubSelect () {
92+ return subSelect . withUseBrackets ( false ) ;
4393 }
4494
45- public void setSelectBody ( SelectBody selectBody ) {
46- this .selectBody = selectBody ;
95+ public void setSubSelect ( SubSelect subSelect ) {
96+ this .subSelect = subSelect . withUseBrackets ( false ) ;
4797 }
4898
4999 /**
@@ -62,9 +112,26 @@ public void setWithItemList(List<SelectItem> withItemList) {
62112 @ Override
63113 @ SuppressWarnings ({"PMD.CyclomaticComplexity" })
64114 public String toString () {
65- return (recursive ? "RECURSIVE " : "" ) + name + ((withItemList != null ) ? " " + PlainSelect .
66- getStringList (withItemList , true , true ) : "" )
67- + " AS (" + selectBody + ")" ;
115+ StringBuilder builder = new StringBuilder ();
116+ builder .append (recursive ? "RECURSIVE " : "" );
117+ builder .append (name );
118+ builder .append (
119+ (withItemList != null ) ? " " + PlainSelect .getStringList (withItemList , true , true ) : "" );
120+ builder .append (" AS " );
121+
122+ if (useValues ) {
123+ builder .append ("(VALUES " );
124+ ExpressionList expressionList = (ExpressionList ) itemsList ;
125+ builder .append (
126+ PlainSelect .getStringList (expressionList .getExpressions (), true , useBracketsForValues ));
127+ builder .append (")" );
128+ } else {
129+ builder .append (subSelect .isUseBrackets () ? "" : "(" );
130+ builder .append (subSelect );
131+
132+ builder .append (subSelect .isUseBrackets () ? "" : ")" );
133+ }
134+ return builder .toString ();
68135 }
69136
70137 @ Override
@@ -82,8 +149,8 @@ public WithItem withWithItemList(List<SelectItem> withItemList) {
82149 return this ;
83150 }
84151
85- public WithItem withSelectBody ( SelectBody selectBody ) {
86- this .setSelectBody ( selectBody );
152+ public WithItem withSubSelect ( SubSelect subSelect ) {
153+ this .setSubSelect ( subSelect );
87154 return this ;
88155 }
89156
@@ -103,8 +170,4 @@ public WithItem addWithItemList(Collection<? extends SelectItem> withItemList) {
103170 collection .addAll (withItemList );
104171 return this .withWithItemList (collection );
105172 }
106-
107- public <E extends SelectBody > E getSelectBody (Class <E > type ) {
108- return type .cast (getSelectBody ());
109- }
110173}
0 commit comments