|
13 | 13 | import net.sf.jsqlparser.schema.Table;
|
14 | 14 | import net.sf.jsqlparser.statement.insert.Insert;
|
15 | 15 | import net.sf.jsqlparser.statement.select.PlainSelect;
|
| 16 | + |
16 | 17 | import static net.sf.jsqlparser.test.TestUtils.assertSqlCanBeParsedAndDeparsed;
|
17 | 18 | import static org.junit.Assert.*;
|
18 | 19 |
|
@@ -83,6 +84,63 @@ public void testInsertFromSelect() throws JSQLParserException {
|
83 | 84 | String statementToString = "INSERT INTO mytable (col1, col2, col3) SELECT * FROM mytable2";
|
84 | 85 | assertEquals(statementToString, "" + insert);
|
85 | 86 | }
|
| 87 | + |
| 88 | + @Test |
| 89 | + public void testInsertFromSet() throws JSQLParserException { |
| 90 | + String statement = "INSERT INTO mytable SET col1 = 12, col2 = name1 * name2"; |
| 91 | + Insert insert = (Insert) parserManager.parse(new StringReader(statement)); |
| 92 | + assertEquals("mytable", insert.getTable().getName()); |
| 93 | + assertEquals(2, insert.getSetColumns().size()); |
| 94 | + assertEquals("col1", ((Column) insert.getSetColumns().get(0)).getColumnName()); |
| 95 | + assertEquals("col2", ((Column) insert.getSetColumns().get(1)).getColumnName()); |
| 96 | + assertEquals(2, insert.getSetExpressionList().size()); |
| 97 | + assertEquals("12", insert.getSetExpressionList().get(0).toString()); |
| 98 | + assertEquals("name1 * name2", insert.getSetExpressionList().get(1).toString()); |
| 99 | + assertEquals(statement, "" + insert); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + public void testInsertValuesWithDuplicateElimination() throws JSQLParserException { |
| 104 | + String statement = "INSERT INTO TEST (ID, COUNTER) VALUES (123, 0) " |
| 105 | + + "ON DUPLICATE KEY UPDATE COUNTER = COUNTER + 1"; |
| 106 | + Insert insert = (Insert) parserManager.parse(new StringReader(statement)); |
| 107 | + assertEquals("TEST", insert.getTable().getName()); |
| 108 | + assertEquals(2, insert.getColumns().size()); |
| 109 | + assertTrue(insert.isUseValues()); |
| 110 | + assertEquals("ID", ((Column) insert.getColumns().get(0)).getColumnName()); |
| 111 | + assertEquals("COUNTER", ((Column) insert.getColumns().get(1)).getColumnName()); |
| 112 | + assertEquals(2, ((ExpressionList) insert.getItemsList()).getExpressions().size()); |
| 113 | + assertEquals(123, ((LongValue) ((ExpressionList) insert.getItemsList()).getExpressions(). |
| 114 | + get(0)).getValue()); |
| 115 | + assertEquals(0, ((LongValue) ((ExpressionList) insert.getItemsList()).getExpressions(). |
| 116 | + get(1)).getValue()); |
| 117 | + assertEquals(1, insert.getDuplicateUpdateColumns().size()); |
| 118 | + assertEquals("COUNTER", ((Column) insert.getDuplicateUpdateColumns().get(0)).getColumnName()); |
| 119 | + assertEquals(1, insert.getDuplicateUpdateExpressionList().size()); |
| 120 | + assertEquals("COUNTER + 1", insert.getDuplicateUpdateExpressionList().get(0).toString()); |
| 121 | + assertFalse(insert.isUseSelectBrackets()); |
| 122 | + assertTrue(insert.isUseDuplicate()); |
| 123 | + assertEquals(statement, "" + insert); |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + public void testInsertFromSetWithDuplicateElimination() throws JSQLParserException { |
| 128 | + String statement = "INSERT INTO mytable SET col1 = 122 " |
| 129 | + + "ON DUPLICATE KEY UPDATE col2 = col2 + 1, col3 = 'saint'"; |
| 130 | + Insert insert = (Insert) parserManager.parse(new StringReader(statement)); |
| 131 | + assertEquals("mytable", insert.getTable().getName()); |
| 132 | + assertEquals(1, insert.getSetColumns().size()); |
| 133 | + assertEquals("col1", ((Column) insert.getSetColumns().get(0)).getColumnName()); |
| 134 | + assertEquals(1, insert.getSetExpressionList().size()); |
| 135 | + assertEquals("122", insert.getSetExpressionList().get(0).toString()); |
| 136 | + assertEquals(2, insert.getDuplicateUpdateColumns().size()); |
| 137 | + assertEquals("col2", ((Column) insert.getDuplicateUpdateColumns().get(0)).getColumnName()); |
| 138 | + assertEquals("col3", ((Column) insert.getDuplicateUpdateColumns().get(1)).getColumnName()); |
| 139 | + assertEquals(2, insert.getDuplicateUpdateExpressionList().size()); |
| 140 | + assertEquals("col2 + 1", insert.getDuplicateUpdateExpressionList().get(0).toString()); |
| 141 | + assertEquals("'saint'", insert.getDuplicateUpdateExpressionList().get(1).toString()); |
| 142 | + assertEquals(statement, "" + insert); |
| 143 | + } |
86 | 144 |
|
87 | 145 | @Test
|
88 | 146 | public void testInsertMultiRowValue() throws JSQLParserException {
|
@@ -191,4 +249,22 @@ public void testKeywordPrecisionIssue363() throws JSQLParserException {
|
191 | 249 | public void testWithDeparsingIssue406() throws JSQLParserException {
|
192 | 250 | assertSqlCanBeParsedAndDeparsed("insert into mytab3 (a,b,c) select a,b,c from mytab where exists(with t as (select * from mytab2) select * from t)", true);
|
193 | 251 | }
|
| 252 | + |
| 253 | + @Test |
| 254 | + public void testInsertSetInDeparsing() throws JSQLParserException { |
| 255 | + assertSqlCanBeParsedAndDeparsed("INSERT INTO mytable SET col1 = 12, col2 = name1 * name2"); |
| 256 | + } |
| 257 | + |
| 258 | + @Test |
| 259 | + public void testInsertValuesWithDuplicateEliminationInDeparsing() throws JSQLParserException { |
| 260 | + assertSqlCanBeParsedAndDeparsed("INSERT INTO TEST (ID, COUNTER) VALUES (123, 0) " |
| 261 | + + "ON DUPLICATE KEY UPDATE COUNTER = COUNTER + 1"); |
| 262 | + } |
| 263 | + |
| 264 | + @Test |
| 265 | + public void testInsertSetWithDuplicateEliminationInDeparsing() throws JSQLParserException { |
| 266 | + assertSqlCanBeParsedAndDeparsed("INSERT INTO mytable SET col1 = 122 " |
| 267 | + + "ON DUPLICATE KEY UPDATE col2 = col2 + 1, col3 = 'saint'"); |
| 268 | + } |
| 269 | + |
194 | 270 | }
|
0 commit comments