|
10 | 10 | package net.sf.jsqlparser.test;
|
11 | 11 |
|
12 | 12 | import net.sf.jsqlparser.JSQLParserException;
|
| 13 | +import net.sf.jsqlparser.expression.Alias; |
| 14 | +import net.sf.jsqlparser.expression.Function; |
13 | 15 | import net.sf.jsqlparser.expression.LongValue;
|
14 | 16 | import net.sf.jsqlparser.expression.StringValue;
|
15 | 17 | import net.sf.jsqlparser.parser.CCJSqlParserUtil;
|
16 | 18 | import net.sf.jsqlparser.statement.Statement;
|
| 19 | +import net.sf.jsqlparser.statement.select.PlainSelect; |
| 20 | +import net.sf.jsqlparser.statement.select.SelectItem; |
17 | 21 | import net.sf.jsqlparser.util.deparser.ExpressionDeParser;
|
18 | 22 | import net.sf.jsqlparser.util.deparser.SelectDeParser;
|
19 | 23 | import net.sf.jsqlparser.util.deparser.StatementDeParser;
|
@@ -61,4 +65,54 @@ public void testIssue1608() throws JSQLParserException {
|
61 | 65 | "INSERT INTO example (num, name, address, tel) VALUES (1, 'name', 'test ', '1234-1234')"));
|
62 | 66 | System.out.println(cleanStatement("DELETE FROM table1 where col=5 and col2=4"));
|
63 | 67 | }
|
| 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 | + } |
64 | 118 | }
|
0 commit comments