Skip to content

Commit 0aa229d

Browse files
committed
fixes #473
1 parent 58c42bc commit 0aa229d

File tree

9 files changed

+58
-23
lines changed

9 files changed

+58
-23
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ Also I would like to know about needed examples or documentation stuff.
4848

4949
## Extensions in the latest SNAPSHOT version 1.2
5050

51+
* introduced alias for subquery in combination with a pivot definition (this **changes** alias handling within the library for pivot sqls)
5152

5253
## Extensions of JSqlParser releases
5354

src/main/java/net/sf/jsqlparser/schema/Table.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,8 @@ public void setHint(MySQLIndexHint hint) {
145145
@Override
146146
public String toString() {
147147
return getFullyQualifiedName()
148-
+ ((pivot != null) ? " " + pivot : "")
149148
+ ((alias != null) ? alias.toString() : "")
149+
+ ((pivot != null) ? " " + pivot : "")
150150
+ ((hint != null) ? hint.toString() : "");
151151
}
152152
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public void setPivot(Pivot pivot) {
7070
@Override
7171
public String toString() {
7272
return "LATERAL" + subSelect.toString()
73-
+ ((pivot != null) ? " " + pivot : "")
74-
+ ((alias != null) ? alias.toString() : "");
73+
+ ((alias != null) ? alias.toString() : "")
74+
+ ((pivot != null) ? " " + pivot : "");
7575
}
7676
}

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,15 @@
2424
import net.sf.jsqlparser.schema.Column;
2525

2626
import java.util.List;
27+
import net.sf.jsqlparser.expression.Alias;
2728

2829
public class Pivot {
2930

3031
private List<FunctionItem> functionItems;
31-
3232
private List<Column> forColumns;
33-
3433
private List<SelectExpressionItem> singleInItems;
35-
3634
private List<ExpressionListItem> multiInItems;
35+
private Alias alias;
3736

3837
public void accept(PivotVisitor pivotVisitor) {
3938
pivotVisitor.visit(this);
@@ -75,12 +74,21 @@ public List<?> getInItems() {
7574
return singleInItems == null ? multiInItems : singleInItems;
7675
}
7776

77+
public Alias getAlias() {
78+
return alias;
79+
}
80+
81+
public void setAlias(Alias alias) {
82+
this.alias = alias;
83+
}
84+
7885
@Override
7986
public String toString() {
8087
return "PIVOT ("
8188
+ PlainSelect.getStringList(functionItems)
8289
+ " FOR " + PlainSelect.
8390
getStringList(forColumns, true, forColumns != null && forColumns.size() > 1)
84-
+ " IN " + PlainSelect.getStringList(getInItems(), true, true) + ")";
91+
+ " IN " + PlainSelect.getStringList(getInItems(), true, true) + ")"
92+
+ (alias!=null?alias.toString():"");
8593
}
8694
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public void setAlias(Alias alias) {
7777
@Override
7878
public String toString() {
7979
return "(" + left + " " + join + ")"
80-
+ ((pivot != null) ? " " + pivot : "")
81-
+ ((alias != null) ? alias.toString() : "");
80+
+ ((alias != null) ? alias.toString() : "")
81+
+ ((pivot != null) ? " " + pivot : "");
8282
}
8383
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,12 +122,12 @@ public String toString() {
122122
retval.append(")");
123123
}
124124

125-
if (pivot != null) {
126-
retval.append(" ").append(pivot);
127-
}
128125
if (alias != null) {
129126
retval.append(alias.toString());
130127
}
128+
if (pivot != null) {
129+
retval.append(" ").append(pivot);
130+
}
131131

132132
return retval.toString();
133133
}

src/main/java/net/sf/jsqlparser/util/deparser/SelectDeParser.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ public SelectDeParser() {
4040
}
4141

4242
/**
43-
* @param expressionVisitor a {@link ExpressionVisitor} to de-parse expressions. It has to share
44-
* the same<br>
43+
* @param expressionVisitor a {@link ExpressionVisitor} to de-parse expressions. It has to share the same<br>
4544
* StringBuilder (buffer parameter) as this object in order to work
4645
* @param buffer the buffer that will be filled with the select
4746
*/
@@ -211,27 +210,27 @@ public void visit(SubSelect subSelect) {
211210
}
212211
subSelect.getSelectBody().accept(this);
213212
buffer.append(")");
214-
Pivot pivot = subSelect.getPivot();
215-
if (pivot != null) {
216-
pivot.accept(this);
217-
}
218213
Alias alias = subSelect.getAlias();
219214
if (alias != null) {
220215
buffer.append(alias.toString());
221216
}
217+
Pivot pivot = subSelect.getPivot();
218+
if (pivot != null) {
219+
pivot.accept(this);
220+
}
222221
}
223222

224223
@Override
225224
public void visit(Table tableName) {
226225
buffer.append(tableName.getFullyQualifiedName());
227-
Pivot pivot = tableName.getPivot();
228-
if (pivot != null) {
229-
pivot.accept(this);
230-
}
231226
Alias alias = tableName.getAlias();
232227
if (alias != null) {
233228
buffer.append(alias);
234229
}
230+
Pivot pivot = tableName.getPivot();
231+
if (pivot != null) {
232+
pivot.accept(this);
233+
}
235234
MySQLIndexHint indexHint = tableName.getIndexHint();
236235
if (indexHint != null) {
237236
buffer.append(indexHint);
@@ -249,6 +248,9 @@ public void visit(Pivot pivot) {
249248
append(" IN ")
250249
.append(PlainSelect.getStringList(pivot.getInItems(), true, true))
251250
.append(")");
251+
if (pivot.getAlias() != null) {
252+
buffer.append(pivot.getAlias().toString());
253+
}
252254
}
253255

254256
@Override

src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1294,6 +1294,7 @@ Pivot Pivot():
12941294
List<Column> forColumns;
12951295
List<SelectExpressionItem> singleInItems = null;
12961296
List<ExpressionListItem> multiInItems = null;
1297+
Alias alias = null;
12971298
}
12981299
{
12991300
<K_PIVOT> "(" functionItems = PivotFunctionItems() <K_FOR>
@@ -1303,11 +1304,13 @@ Pivot Pivot():
13031304
| multiInItems = PivotMultiInItems() )
13041305
")"
13051306
")"
1307+
[ alias = Alias() ]
13061308
{
13071309
retval.setFunctionItems(functionItems);
13081310
retval.setForColumns(forColumns);
13091311
retval.setSingleInItems(singleInItems);
13101312
retval.setMultiInItems(multiInItems);
1313+
retval.setAlias(alias);
13111314
return retval;
13121315
}
13131316
}
@@ -1386,8 +1389,8 @@ FromItem FromItem():
13861389
|
13871390
fromItem=LateralSubSelect()
13881391
)
1389-
[(LOOKAHEAD(2) pivot=PivotXml()|pivot=Pivot()) { fromItem.setPivot(pivot); } ]
13901392
[ alias=Alias() { fromItem.setAlias(alias); } ]
1393+
[(LOOKAHEAD(2) pivot=PivotXml()|pivot=Pivot()) { fromItem.setPivot(pivot); } ]
13911394
[
13921395
LOOKAHEAD(2)
13931396
indexHint=MySQLIndexHint() {

src/test/java/net/sf/jsqlparser/test/select/SelectTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1928,6 +1928,27 @@ public void testPivotXmlSubquery1() throws JSQLParserException {
19281928
public void testPivotFunction() throws JSQLParserException {
19291929
assertSqlCanBeParsedAndDeparsed("SELECT to_char((SELECT col1 FROM (SELECT times_purchased, state_code FROM customers t) PIVOT (count(state_code) FOR state_code IN ('NY', 'CT', 'NJ', 'FL', 'MO')) ORDER BY times_purchased)) FROM DUAL");
19301930
}
1931+
1932+
public void testPivotWithAlias() throws JSQLParserException {
1933+
assertSqlCanBeParsedAndDeparsed("SELECT * FROM (SELECT * FROM mytable LEFT JOIN mytable2 ON Factor_ID = Id) f PIVOT (max(f.value) FOR f.factoryCode IN (ZD, COD, SW, PH))");
1934+
}
1935+
1936+
public void testPivotWithAlias2() throws JSQLParserException {
1937+
assertSqlCanBeParsedAndDeparsed("SELECT * FROM (SELECT * FROM mytable LEFT JOIN mytable2 ON Factor_ID = Id) f PIVOT (max(f.value) FOR f.factoryCode IN (ZD, COD, SW, PH)) d");
1938+
}
1939+
1940+
public void testPivotWithAlias3() throws JSQLParserException {
1941+
assertSqlCanBeParsedAndDeparsed("SELECT * FROM (SELECT * FROM mytable LEFT JOIN mytable2 ON Factor_ID = Id) PIVOT (max(f.value) FOR f.factoryCode IN (ZD, COD, SW, PH)) d");
1942+
}
1943+
1944+
public void testPivotWithAlias4() throws JSQLParserException {
1945+
assertSqlCanBeParsedAndDeparsed("SELECT * FROM (" +
1946+
"SELECT a.Station_ID stationId, b.Factor_Code factoryCode, a.Value value" +
1947+
" FROM T_Data_Real a" +
1948+
" LEFT JOIN T_Bas_Factor b ON a.Factor_ID = b.Id" +
1949+
") f " +
1950+
"PIVOT (max(f.value) FOR f.factoryCode IN (ZD, COD, SW, PH)) d");
1951+
}
19311952

19321953
public void testRegexpLike1() throws JSQLParserException {
19331954
String stmt = "SELECT * FROM mytable WHERE REGEXP_LIKE(first_name, '^Ste(v|ph)en$')";

0 commit comments

Comments
 (0)