use of net.sf.jsqlparser.statement.insert.Insert in project JSqlParser by JSQLParser.
the class InsertTest method testInsertWithKeywordValue.
@Test
public void testInsertWithKeywordValue() throws JSQLParserException {
String statement = "INSERT INTO mytable (col1) VALUE ('val1')";
Insert insert = (Insert) parserManager.parse(new StringReader(statement));
assertEquals("mytable", insert.getTable().getName());
assertEquals(1, insert.getColumns().size());
assertEquals("col1", ((Column) insert.getColumns().get(0)).getColumnName());
assertEquals("val1", ((StringValue) ((ExpressionList) insert.getItemsList()).getExpressions().get(0)).getValue());
assertEquals("INSERT INTO mytable (col1) VALUES ('val1')", insert.toString());
}
use of net.sf.jsqlparser.statement.insert.Insert in project JSqlParser by JSQLParser.
the class InsertTest method testInsertFromSelect.
@Test
public void testInsertFromSelect() throws JSQLParserException {
String statement = "INSERT INTO mytable (col1, col2, col3) SELECT * FROM mytable2";
Insert insert = (Insert) parserManager.parse(new StringReader(statement));
assertEquals("mytable", insert.getTable().getName());
assertEquals(3, insert.getColumns().size());
assertEquals("col1", ((Column) insert.getColumns().get(0)).getColumnName());
assertEquals("col2", ((Column) insert.getColumns().get(1)).getColumnName());
assertEquals("col3", ((Column) insert.getColumns().get(2)).getColumnName());
assertNull(insert.getItemsList());
assertNotNull(insert.getSelect());
assertEquals("mytable2", ((Table) ((PlainSelect) insert.getSelect().getSelectBody()).getFromItem()).getName());
// toString uses brakets
String statementToString = "INSERT INTO mytable (col1, col2, col3) SELECT * FROM mytable2";
assertEquals(statementToString, "" + insert);
}
use of net.sf.jsqlparser.statement.insert.Insert in project JSqlParser by JSQLParser.
the class InsertTest method testInsertFromSetWithDuplicateElimination.
@Test
public void testInsertFromSetWithDuplicateElimination() throws JSQLParserException {
String statement = "INSERT INTO mytable SET col1 = 122 " + "ON DUPLICATE KEY UPDATE col2 = col2 + 1, col3 = 'saint'";
Insert insert = (Insert) parserManager.parse(new StringReader(statement));
assertEquals("mytable", insert.getTable().getName());
assertEquals(1, insert.getSetColumns().size());
assertEquals("col1", ((Column) insert.getSetColumns().get(0)).getColumnName());
assertEquals(1, insert.getSetExpressionList().size());
assertEquals("122", insert.getSetExpressionList().get(0).toString());
assertEquals(2, insert.getDuplicateUpdateColumns().size());
assertEquals("col2", ((Column) insert.getDuplicateUpdateColumns().get(0)).getColumnName());
assertEquals("col3", ((Column) insert.getDuplicateUpdateColumns().get(1)).getColumnName());
assertEquals(2, insert.getDuplicateUpdateExpressionList().size());
assertEquals("col2 + 1", insert.getDuplicateUpdateExpressionList().get(0).toString());
assertEquals("'saint'", insert.getDuplicateUpdateExpressionList().get(1).toString());
assertEquals(statement, "" + insert);
}
use of net.sf.jsqlparser.statement.insert.Insert in project JSqlParser by JSQLParser.
the class StatementDeParserTest method shouldUseProvidedDeparsersWhenDeParsingInsert.
@Test
@SuppressWarnings("PMD.JUnitTestsShouldIncludeAssert")
public void shouldUseProvidedDeparsersWhenDeParsingInsert() throws JSQLParserException {
Insert insert = new Insert();
Table table = new Table();
List<Column> duplicateUpdateColumns = new ArrayList<Column>();
List<Expression> duplicateUpdateExpressionList = new ArrayList<Expression>();
Column duplicateUpdateColumn1 = new Column();
Column duplicateUpdateColumn2 = new Column();
Expression duplicateUpdateExpression1 = mock(Expression.class);
Expression duplicateUpdateExpression2 = mock(Expression.class);
Select select = new Select();
List<WithItem> withItemsList = new ArrayList<WithItem>();
WithItem withItem1 = spy(new WithItem());
WithItem withItem2 = spy(new WithItem());
SelectBody withItem1SelectBody = mock(SelectBody.class);
SelectBody withItem2SelectBody = mock(SelectBody.class);
SelectBody selectBody = mock(SelectBody.class);
insert.setSelect(select);
insert.setTable(table);
insert.setUseDuplicate(true);
insert.setDuplicateUpdateColumns(duplicateUpdateColumns);
insert.setDuplicateUpdateExpressionList(duplicateUpdateExpressionList);
duplicateUpdateColumns.add(duplicateUpdateColumn1);
duplicateUpdateColumns.add(duplicateUpdateColumn2);
duplicateUpdateExpressionList.add(duplicateUpdateExpression1);
duplicateUpdateExpressionList.add(duplicateUpdateExpression2);
insert.setDuplicateUpdateExpressionList(duplicateUpdateExpressionList);
select.setWithItemsList(withItemsList);
select.setSelectBody(selectBody);
withItemsList.add(withItem1);
withItemsList.add(withItem2);
withItem1.setSelectBody(withItem1SelectBody);
withItem2.setSelectBody(withItem2SelectBody);
statementDeParser.visit(insert);
then(withItem1).should().accept(selectDeParser);
then(withItem2).should().accept(selectDeParser);
then(selectBody).should().accept(selectDeParser);
then(duplicateUpdateExpression1).should().accept(expressionDeParser);
then(duplicateUpdateExpression1).should().accept(expressionDeParser);
}
use of net.sf.jsqlparser.statement.insert.Insert in project JSqlParser by JSQLParser.
the class TablesNamesFinderTest method testGetTableListFromInsertValues.
@Test
public void testGetTableListFromInsertValues() throws Exception {
String sql = "INSERT INTO MY_TABLE1 (a) VALUES (5)";
net.sf.jsqlparser.statement.Statement statement = pm.parse(new StringReader(sql));
Insert insertStatement = (Insert) statement;
TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
List<String> tableList = tablesNamesFinder.getTableList(insertStatement);
assertEquals(1, tableList.size());
assertTrue(tableList.contains("MY_TABLE1"));
}
Aggregations