Skip to content

Commit a97160a

Browse files
author
Jonathan Burnhams
committed
Started to add pivot support
1 parent 517839a commit a97160a

File tree

12 files changed

+338
-38
lines changed

12 files changed

+338
-38
lines changed

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

100644100755
Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import net.sf.jsqlparser.statement.select.FromItem;
2525
import net.sf.jsqlparser.statement.select.FromItemVisitor;
2626
import net.sf.jsqlparser.statement.select.IntoTableVisitor;
27+
import net.sf.jsqlparser.statement.select.Pivot;
2728

2829
/**
2930
* A table. It can have an alias and the schema name it belongs to.
@@ -33,6 +34,7 @@ public class Table implements FromItem {
3334
private String schemaName;
3435
private String name;
3536
private String alias;
37+
private Pivot pivot;
3638

3739
public Table() {
3840
}
@@ -68,7 +70,7 @@ public void setAlias(String string) {
6870
alias = string;
6971
}
7072

71-
public String getWholeTableName() {
73+
public String getWholeTableName() {
7274

7375
String tableWholeName = null;
7476
if (name == null) {
@@ -93,8 +95,18 @@ public void accept(IntoTableVisitor intoTableVisitor) {
9395
intoTableVisitor.visit(this);
9496
}
9597

96-
@Override
98+
public Pivot getPivot() {
99+
return pivot;
100+
}
101+
102+
public void setPivot(Pivot pivot) {
103+
this.pivot = pivot;
104+
}
105+
106+
@Override
97107
public String toString() {
98-
return getWholeTableName() + ((alias != null) ? " AS " + alias : "");
108+
return getWholeTableName() +
109+
((pivot != null) ? " "+pivot : "") +
110+
((alias != null) ? " AS " + alias : "");
99111
}
100112
}

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

100644100755
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,9 @@ public interface FromItem {
3232
String getAlias();
3333

3434
void setAlias(String alias);
35+
36+
Pivot getPivot();
37+
38+
void setPivot(Pivot pivot);
39+
3540
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* #%L
3+
* JSQLParser library
4+
* %%
5+
* Copyright (C) 2004 - 2013 JSQLParser
6+
* %%
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU Lesser General Public License as
9+
* published by the Free Software Foundation, either version 2.1 of the
10+
* License, or (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Lesser Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Lesser Public
18+
* License along with this program. If not, see
19+
* <http://www.gnu.org/licenses/lgpl-2.1.html>.
20+
* #L%
21+
*/
22+
package net.sf.jsqlparser.statement.select;
23+
24+
import net.sf.jsqlparser.expression.Function;
25+
26+
public class FunctionItem {
27+
28+
private Function function;
29+
private String alias;
30+
31+
public String getAlias() {
32+
return alias;
33+
}
34+
35+
public void setAlias(String string) {
36+
alias = string;
37+
}
38+
39+
public Function getFunction() {
40+
return function;
41+
}
42+
43+
public void setFunction(Function function) {
44+
this.function = function;
45+
}
46+
47+
@Override
48+
public String toString() {
49+
return function + ((alias != null) ? " AS " + alias : "");
50+
}
51+
}

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

100644100755
Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public class LateralSubSelect implements FromItem {
3030

3131
private SubSelect subSelect;
3232
private String alias;
33+
private Pivot pivot;
3334

3435
public void setSubSelect(SubSelect subSelect) {
3536
this.subSelect = subSelect;
@@ -54,8 +55,18 @@ public void setAlias(String alias) {
5455
this.alias = alias;
5556
}
5657

57-
@Override
58+
public Pivot getPivot() {
59+
return pivot;
60+
}
61+
62+
public void setPivot(Pivot pivot) {
63+
this.pivot = pivot;
64+
}
65+
66+
@Override
5867
public String toString() {
59-
return "LATERAL" + subSelect.toString() + ((alias != null) ? " AS " + alias : "");
68+
return "LATERAL" + subSelect.toString() +
69+
((pivot != null) ? " "+pivot : "") +
70+
((alias != null) ? " AS " + alias : "");
6071
}
6172
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* #%L
3+
* JSQLParser library
4+
* %%
5+
* Copyright (C) 2004 - 2013 JSQLParser
6+
* %%
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU Lesser General Public License as
9+
* published by the Free Software Foundation, either version 2.1 of the
10+
* License, or (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Lesser Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Lesser Public
18+
* License along with this program. If not, see
19+
* <http://www.gnu.org/licenses/lgpl-2.1.html>.
20+
* #L%
21+
*/
22+
package net.sf.jsqlparser.statement.select;
23+
24+
import net.sf.jsqlparser.schema.Column;
25+
26+
import java.util.List;
27+
28+
public class Pivot {
29+
30+
private List<FunctionItem> functionItems;
31+
32+
private List<Column> forColumns;
33+
34+
private List<SelectExpressionItem> inItems;
35+
36+
public List<FunctionItem> getFunctionItems() {
37+
return functionItems;
38+
}
39+
40+
public void setFunctionItems(List<FunctionItem> functionItems) {
41+
this.functionItems = functionItems;
42+
}
43+
44+
public List<Column> getForColumns() {
45+
return forColumns;
46+
}
47+
48+
public void setForColumns(List<Column> forColumns) {
49+
this.forColumns = forColumns;
50+
}
51+
52+
public List<SelectExpressionItem> getInItems() {
53+
return inItems;
54+
}
55+
56+
public void setInItems(List<SelectExpressionItem> inItems) {
57+
this.inItems = inItems;
58+
}
59+
60+
@Override
61+
public String toString() {
62+
return "PIVOT (" +
63+
PlainSelect.getStringList(functionItems) +
64+
" FOR " + PlainSelect.getStringList(forColumns, true, forColumns.size() > 1) +
65+
" IN " + PlainSelect.getStringList(inItems, true, true) + ")";
66+
}
67+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* #%L
3+
* JSQLParser library
4+
* %%
5+
* Copyright (C) 2004 - 2013 JSQLParser
6+
* %%
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU Lesser General Public License as
9+
* published by the Free Software Foundation, either version 2.1 of the
10+
* License, or (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Lesser Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Lesser Public
18+
* License along with this program. If not, see
19+
* <http://www.gnu.org/licenses/lgpl-2.1.html>.
20+
* #L%
21+
*/
22+
package net.sf.jsqlparser.statement.select;
23+
24+
public class PivotXml extends Pivot {
25+
26+
}

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

100644100755
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@
2121
*/
2222
package net.sf.jsqlparser.statement.select;
2323

24-
import java.util.Iterator;
25-
import java.util.List;
26-
2724
import net.sf.jsqlparser.expression.Expression;
2825
import net.sf.jsqlparser.schema.Table;
2926

27+
import java.util.Iterator;
28+
import java.util.List;
29+
3030
/**
3131
* The core of a "SELECT" statement (no UNION, no ORDER BY)
3232
*/
@@ -159,7 +159,7 @@ public void setGroupByColumnReferences(List<Expression> list) {
159159
groupByColumnReferences = list;
160160
}
161161

162-
@Override
162+
@Override
163163
public String toString() {
164164
StringBuilder sql = new StringBuilder("SELECT ");
165165
if (distinct != null) {

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

100644100755
Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public class SubJoin implements FromItem {
2929
private FromItem left;
3030
private Join join;
3131
private String alias;
32+
private Pivot pivot;
3233

3334
@Override
3435
public void accept(FromItemVisitor fromItemVisitor) {
@@ -51,7 +52,15 @@ public void setJoin(Join j) {
5152
join = j;
5253
}
5354

54-
@Override
55+
public Pivot getPivot() {
56+
return pivot;
57+
}
58+
59+
public void setPivot(Pivot pivot) {
60+
this.pivot = pivot;
61+
}
62+
63+
@Override
5564
public String getAlias() {
5665
return alias;
5766
}
@@ -63,6 +72,8 @@ public void setAlias(String string) {
6372

6473
@Override
6574
public String toString() {
66-
return "(" + left + " " + join + ")" + ((alias != null) ? " AS " + alias : "");
75+
return "(" + left + " " + join + ")" +
76+
((pivot != null) ? " "+pivot : "") +
77+
((alias != null) ? " AS " + alias : "");
6778
}
6879
}

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

100644100755
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ public class SubSelect implements FromItem, Expression, ItemsList {
3434
private SelectBody selectBody;
3535
private String alias;
3636

37+
private Pivot pivot;
38+
3739
@Override
3840
public void accept(FromItemVisitor fromItemVisitor) {
3941
fromItemVisitor.visit(this);
@@ -62,6 +64,14 @@ public void setAlias(String string) {
6264
alias = string;
6365
}
6466

67+
public Pivot getPivot() {
68+
return pivot;
69+
}
70+
71+
public void setPivot(Pivot pivot) {
72+
this.pivot = pivot;
73+
}
74+
6575
@Override
6676
public void accept(ItemsListVisitor itemsListVisitor) {
6777
itemsListVisitor.visit(this);

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

100644100755
Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@
2121
*/
2222
package net.sf.jsqlparser.statement.select;
2323

24-
import java.util.Iterator;
25-
import java.util.List;
2624
import net.sf.jsqlparser.expression.operators.relational.ExpressionList;
2725
import net.sf.jsqlparser.expression.operators.relational.MultiExpressionList;
2826

27+
import java.util.Iterator;
28+
import java.util.List;
29+
2930
/**
3031
* This is a container for a values item within a select statement. It holds
3132
* some syntactical stuff that differs from values within an insert statement.
@@ -61,7 +62,16 @@ public void setAlias(String alias) {
6162
this.alias = alias;
6263
}
6364

64-
public MultiExpressionList getMultiExpressionList() {
65+
@Override
66+
public Pivot getPivot() {
67+
return null;
68+
}
69+
70+
@Override
71+
public void setPivot(Pivot pivot) {
72+
}
73+
74+
public MultiExpressionList getMultiExpressionList() {
6575
return multiExpressionList;
6676
}
6777

0 commit comments

Comments
 (0)