Skip to content

Commit 6b35e37

Browse files
doc: show case how to manipulate select items
Signed-off-by: Andreas Reichel <[email protected]>
1 parent 8f4cdb3 commit 6b35e37

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

src/test/java/net/sf/jsqlparser/test/AssortedFeatureTests.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,14 @@
1010
package net.sf.jsqlparser.test;
1111

1212
import net.sf.jsqlparser.JSQLParserException;
13+
import net.sf.jsqlparser.expression.Alias;
14+
import net.sf.jsqlparser.expression.Function;
1315
import net.sf.jsqlparser.expression.LongValue;
1416
import net.sf.jsqlparser.expression.StringValue;
1517
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
1618
import net.sf.jsqlparser.statement.Statement;
19+
import net.sf.jsqlparser.statement.select.PlainSelect;
20+
import net.sf.jsqlparser.statement.select.SelectItem;
1721
import net.sf.jsqlparser.util.deparser.ExpressionDeParser;
1822
import net.sf.jsqlparser.util.deparser.SelectDeParser;
1923
import net.sf.jsqlparser.util.deparser.StatementDeParser;
@@ -61,4 +65,54 @@ public void testIssue1608() throws JSQLParserException {
6165
"INSERT INTO example (num, name, address, tel) VALUES (1, 'name', 'test ', '1234-1234')"));
6266
System.out.println(cleanStatement("DELETE FROM table1 where col=5 and col2=4"));
6367
}
68+
69+
@Test
70+
void addSelectItemTest() throws JSQLParserException {
71+
String provided = "SELECT col1 FROM WHATEVER";
72+
String expected = "SELECT col1, Sum(1, 2) AS col2 FROM WHATEVER";
73+
74+
PlainSelect select = (PlainSelect) CCJSqlParserUtil.parse(provided);
75+
76+
Function f = new Function("Sum", new LongValue(1), new LongValue(2));
77+
SelectItem<?> i = new SelectItem<>(f, new Alias("col2", true));
78+
79+
select
80+
.getSelectItems()
81+
.add(i);
82+
83+
TestUtils.assertStatementCanBeDeparsedAs(select, expected);
84+
}
85+
86+
@Test
87+
void removeSelectItemTest() throws JSQLParserException {
88+
String provided = "SELECT col1, Sum(1, 2) AS col2 FROM WHATEVER";
89+
String expected = "SELECT col1 FROM WHATEVER";
90+
91+
PlainSelect select = (PlainSelect) CCJSqlParserUtil.parse(provided);
92+
93+
select
94+
.getSelectItems()
95+
.remove(1);
96+
97+
TestUtils.assertStatementCanBeDeparsedAs(select, expected);
98+
}
99+
100+
@Test
101+
void sweapSelectItemTest() throws JSQLParserException {
102+
String provided = "SELECT col1 FROM WHATEVER";
103+
String expected = "SELECT Sum(1, 2) AS col1 FROM WHATEVER";
104+
105+
PlainSelect select = (PlainSelect) CCJSqlParserUtil.parse(provided);
106+
107+
Function f = new Function("Sum", new LongValue(1), new LongValue(2));
108+
SelectItem<?> i = new SelectItem<>(f, new Alias("col1", true));
109+
select
110+
.getSelectItems()
111+
.remove(0);
112+
select
113+
.getSelectItems()
114+
.add(0, i);
115+
116+
TestUtils.assertStatementCanBeDeparsedAs(select, expected);
117+
}
64118
}

0 commit comments

Comments
 (0)