1919import org .junit .Test ;
2020
2121public class InsertTest {
22-
23- private CCJSqlParserManager parserManager = new CCJSqlParserManager ();
24-
22+
23+ private CCJSqlParserManager parserManager = new CCJSqlParserManager ();
24+
2525 @ Test
26- public void testRegularInsert () throws JSQLParserException {
27- String statement = "INSERT INTO mytable (col1, col2, col3) VALUES (?, 'sadfsd', 234)" ;
28- Insert insert = (Insert ) parserManager .parse (new StringReader (statement ));
29- assertEquals ("mytable" , insert .getTable ().getName ());
30- assertEquals (3 , insert .getColumns ().size ());
31- assertEquals ("col1" , ((Column ) insert .getColumns ().get (0 )).getColumnName ());
32- assertEquals ("col2" , ((Column ) insert .getColumns ().get (1 )).getColumnName ());
33- assertEquals ("col3" , ((Column ) insert .getColumns ().get (2 )).getColumnName ());
34- assertEquals (3 , ((ExpressionList ) insert .getItemsList ()).getExpressions ().size ());
35- assertTrue (((ExpressionList ) insert .getItemsList ()).getExpressions ().get (0 ) instanceof JdbcParameter );
36- assertEquals ("sadfsd" ,
37- ((StringValue ) ((ExpressionList ) insert .getItemsList ()).getExpressions ().get (1 )).getValue ());
38- assertEquals (234 , ((LongValue ) ((ExpressionList ) insert .getItemsList ()).getExpressions ().get (2 )).getValue ());
39- assertEquals (statement , "" + insert );
40-
41- statement = "INSERT INTO myschema.mytable VALUES (?, ?, 2.3)" ;
42- insert = (Insert ) parserManager .parse (new StringReader (statement ));
43- assertEquals ("myschema.mytable" , insert .getTable ().getFullyQualifiedName ());
44- assertEquals (3 , ((ExpressionList ) insert .getItemsList ()).getExpressions ().size ());
45- assertTrue (((ExpressionList ) insert .getItemsList ()).getExpressions ().get (0 ) instanceof JdbcParameter );
46- assertEquals (2.3 , ((DoubleValue ) ((ExpressionList ) insert .getItemsList ()).getExpressions ().get (2 )).getValue (),
47- 0.0 );
48- assertEquals (statement , "" + insert );
49-
50- }
51-
26+ public void testRegularInsert () throws JSQLParserException {
27+ String statement = "INSERT INTO mytable (col1, col2, col3) VALUES (?, 'sadfsd', 234)" ;
28+ Insert insert = (Insert ) parserManager .parse (new StringReader (statement ));
29+ assertEquals ("mytable" , insert .getTable ().getName ());
30+ assertEquals (3 , insert .getColumns ().size ());
31+ assertEquals ("col1" , ((Column ) insert .getColumns ().get (0 )).getColumnName ());
32+ assertEquals ("col2" , ((Column ) insert .getColumns ().get (1 )).getColumnName ());
33+ assertEquals ("col3" , ((Column ) insert .getColumns ().get (2 )).getColumnName ());
34+ assertEquals (3 , ((ExpressionList ) insert .getItemsList ()).getExpressions ().size ());
35+ assertTrue (((ExpressionList ) insert .getItemsList ()).getExpressions ().get (0 ) instanceof JdbcParameter );
36+ assertEquals ("sadfsd" ,
37+ ((StringValue ) ((ExpressionList ) insert .getItemsList ()).getExpressions ().get (1 )).
38+ getValue ());
39+ assertEquals (234 , ((LongValue ) ((ExpressionList ) insert .getItemsList ()).getExpressions ().
40+ get (2 )).getValue ());
41+ assertEquals (statement , "" + insert );
42+
43+ statement = "INSERT INTO myschema.mytable VALUES (?, ?, 2.3)" ;
44+ insert = (Insert ) parserManager .parse (new StringReader (statement ));
45+ assertEquals ("myschema.mytable" , insert .getTable ().getFullyQualifiedName ());
46+ assertEquals (3 , ((ExpressionList ) insert .getItemsList ()).getExpressions ().size ());
47+ assertTrue (((ExpressionList ) insert .getItemsList ()).getExpressions ().get (0 ) instanceof JdbcParameter );
48+ assertEquals (2.3 , ((DoubleValue ) ((ExpressionList ) insert .getItemsList ()).getExpressions ().
49+ get (2 )).getValue (),
50+ 0.0 );
51+ assertEquals (statement , "" + insert );
52+
53+ }
54+
5255 @ Test
53- public void testInsertWithKeywordValue () throws JSQLParserException {
54- String statement = "INSERT INTO mytable (col1) VALUE ('val1')" ;
55- Insert insert = (Insert ) parserManager .parse (new StringReader (statement ));
56- assertEquals ("mytable" , insert .getTable ().getName ());
57- assertEquals (1 , insert .getColumns ().size ());
58- assertEquals ("col1" , ((Column ) insert .getColumns ().get (0 )).getColumnName ());
59- assertEquals ("val1" ,
60- ((StringValue ) ((ExpressionList ) insert .getItemsList ()).getExpressions ().get (0 )).getValue ());
61- assertEquals ("INSERT INTO mytable (col1) VALUES ('val1')" , insert .toString ());
62- }
63-
56+ public void testInsertWithKeywordValue () throws JSQLParserException {
57+ String statement = "INSERT INTO mytable (col1) VALUE ('val1')" ;
58+ Insert insert = (Insert ) parserManager .parse (new StringReader (statement ));
59+ assertEquals ("mytable" , insert .getTable ().getName ());
60+ assertEquals (1 , insert .getColumns ().size ());
61+ assertEquals ("col1" , ((Column ) insert .getColumns ().get (0 )).getColumnName ());
62+ assertEquals ("val1" ,
63+ ((StringValue ) ((ExpressionList ) insert .getItemsList ()).getExpressions ().get (0 )).
64+ getValue ());
65+ assertEquals ("INSERT INTO mytable (col1) VALUES ('val1')" , insert .toString ());
66+ }
67+
6468 @ Test
65- public void testInsertFromSelect () throws JSQLParserException {
66- String statement = "INSERT INTO mytable (col1, col2, col3) SELECT * FROM mytable2" ;
67- Insert insert = (Insert ) parserManager .parse (new StringReader (statement ));
68- assertEquals ("mytable" , insert .getTable ().getName ());
69- assertEquals (3 , insert .getColumns ().size ());
70- assertEquals ("col1" , ((Column ) insert .getColumns ().get (0 )).getColumnName ());
71- assertEquals ("col2" , ((Column ) insert .getColumns ().get (1 )).getColumnName ());
72- assertEquals ("col3" , ((Column ) insert .getColumns ().get (2 )).getColumnName ());
73- assertNull (insert .getItemsList ());
69+ public void testInsertFromSelect () throws JSQLParserException {
70+ String statement = "INSERT INTO mytable (col1, col2, col3) SELECT * FROM mytable2" ;
71+ Insert insert = (Insert ) parserManager .parse (new StringReader (statement ));
72+ assertEquals ("mytable" , insert .getTable ().getName ());
73+ assertEquals (3 , insert .getColumns ().size ());
74+ assertEquals ("col1" , ((Column ) insert .getColumns ().get (0 )).getColumnName ());
75+ assertEquals ("col2" , ((Column ) insert .getColumns ().get (1 )).getColumnName ());
76+ assertEquals ("col3" , ((Column ) insert .getColumns ().get (2 )).getColumnName ());
77+ assertNull (insert .getItemsList ());
7478 assertNotNull (insert .getSelect ());
75- assertEquals ("mytable2" ,
76- ((Table ) ((PlainSelect )insert .getSelect ().getSelectBody ()).getFromItem ()).getName ());
77-
78- // toString uses brakets
79- String statementToString = "INSERT INTO mytable (col1, col2, col3) SELECT * FROM mytable2" ;
80- assertEquals (statementToString , "" + insert );
81- }
79+ assertEquals ("mytable2" ,
80+ ((Table ) ((PlainSelect ) insert .getSelect ().getSelectBody ()).getFromItem ()).getName ());
8281
82+ // toString uses brakets
83+ String statementToString = "INSERT INTO mytable (col1, col2, col3) SELECT * FROM mytable2" ;
84+ assertEquals (statementToString , "" + insert );
85+ }
86+
8387 @ Test
84- public void testInsertMultiRowValue () throws JSQLParserException {
85- assertSqlCanBeParsedAndDeparsed ("INSERT INTO mytable (col1, col2) VALUES (a, b), (d, e)" );
86- }
87-
88+ public void testInsertMultiRowValue () throws JSQLParserException {
89+ assertSqlCanBeParsedAndDeparsed ("INSERT INTO mytable (col1, col2) VALUES (a, b), (d, e)" );
90+ }
91+
8892 @ Test
89- public void testInsertMultiRowValueDifferent () throws JSQLParserException {
90- try {
91- assertSqlCanBeParsedAndDeparsed ("INSERT INTO mytable (col1, col2) VALUES (a, b), (d, e, c)" );
92- } catch (Exception e ) {
93- return ;
94- }
95-
96- fail ("should not work" );
97- }
98-
93+ public void testInsertMultiRowValueDifferent () throws JSQLParserException {
94+ try {
95+ assertSqlCanBeParsedAndDeparsed ("INSERT INTO mytable (col1, col2) VALUES (a, b), (d, e, c)" );
96+ } catch (Exception e ) {
97+ return ;
98+ }
99+
100+ fail ("should not work" );
101+ }
102+
99103 @ Test
100- public void testSimpleInsert () throws JSQLParserException {
101- assertSqlCanBeParsedAndDeparsed ("INSERT INTO example (num, name, address, tel) VALUES (1, 'name', 'test ', '1234-1234')" );
102- }
104+ public void testSimpleInsert () throws JSQLParserException {
105+ assertSqlCanBeParsedAndDeparsed ("INSERT INTO example (num, name, address, tel) VALUES (1, 'name', 'test ', '1234-1234')" );
106+ }
103107
104108 @ Test
105109 public void testInsertWithReturning () throws JSQLParserException {
@@ -133,31 +137,31 @@ public void testInsertWithKeywords() throws JSQLParserException {
133137 assertSqlCanBeParsedAndDeparsed ("INSERT INTO kvPair (value, key) VALUES (?, ?)" );
134138 }
135139
136- @ Test
137- public void testHexValues () throws JSQLParserException {
138- assertSqlCanBeParsedAndDeparsed ("INSERT INTO TABLE2 VALUES ('1', \" DSDD\" , x'EFBFBDC7AB')" );
139- }
140-
141- @ Test
142- public void testHexValues2 () throws JSQLParserException {
143- assertSqlCanBeParsedAndDeparsed ("INSERT INTO TABLE2 VALUES ('1', \" DSDD\" , 0xEFBFBDC7AB)" );
144- }
145-
146- @ Test
147- public void testHexValues3 () throws JSQLParserException {
148- assertSqlCanBeParsedAndDeparsed ("INSERT INTO TABLE2 VALUES ('1', \" DSDD\" , 0xabcde)" );
149- }
150-
151- @ Test
152- public void testDuplicateKey () throws JSQLParserException {
153- assertSqlCanBeParsedAndDeparsed ("INSERT INTO Users0 (UserId, Key, Value) VALUES (51311, 'T_211', 18) ON DUPLICATE KEY UPDATE Value = 18" );
154- }
155-
140+ @ Test
141+ public void testHexValues () throws JSQLParserException {
142+ assertSqlCanBeParsedAndDeparsed ("INSERT INTO TABLE2 VALUES ('1', \" DSDD\" , x'EFBFBDC7AB')" );
143+ }
144+
145+ @ Test
146+ public void testHexValues2 () throws JSQLParserException {
147+ assertSqlCanBeParsedAndDeparsed ("INSERT INTO TABLE2 VALUES ('1', \" DSDD\" , 0xEFBFBDC7AB)" );
148+ }
149+
150+ @ Test
151+ public void testHexValues3 () throws JSQLParserException {
152+ assertSqlCanBeParsedAndDeparsed ("INSERT INTO TABLE2 VALUES ('1', \" DSDD\" , 0xabcde)" );
153+ }
154+
155+ @ Test
156+ public void testDuplicateKey () throws JSQLParserException {
157+ assertSqlCanBeParsedAndDeparsed ("INSERT INTO Users0 (UserId, Key, Value) VALUES (51311, 'T_211', 18) ON DUPLICATE KEY UPDATE Value = 18" );
158+ }
159+
156160 @ Test
157161 public void testModifierIgnore () throws JSQLParserException {
158162 assertSqlCanBeParsedAndDeparsed ("INSERT IGNORE INTO `AoQiSurvey_FlashVersion_Single` VALUES (302215163, 'WIN 16,0,0,235')" );
159163 }
160-
164+
161165 @ Test
162166 public void testModifierPriority1 () throws JSQLParserException {
163167 assertSqlCanBeParsedAndDeparsed ("INSERT DELAYED INTO kvPair (value, key) VALUES (?, ?)" );
@@ -182,4 +186,9 @@ public void testIssue223() throws JSQLParserException {
182186 public void testKeywordPrecisionIssue363 () throws JSQLParserException {
183187 assertSqlCanBeParsedAndDeparsed ("INSERT INTO test (user_id, precision) VALUES (1, '111')" );
184188 }
189+
190+ @ Test
191+ public void testWithDeparsingIssue406 () throws JSQLParserException {
192+ 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+ }
185194}
0 commit comments