Search in sources :

Example 1 with Replace

use of net.sf.jsqlparser.statement.replace.Replace in project JSqlParser by JSQLParser.

the class TablesNamesFinderTest method testGetTableListFromReplace.

@Test
public void testGetTableListFromReplace() throws Exception {
    String sql = "REPLACE INTO MY_TABLE1 (a) VALUES ((SELECT a from MY_TABLE2 WHERE a = 1))";
    net.sf.jsqlparser.statement.Statement statement = pm.parse(new StringReader(sql));
    Replace replaceStatement = (Replace) statement;
    TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
    List<String> tableList = tablesNamesFinder.getTableList(replaceStatement);
    assertEquals(2, tableList.size());
    assertTrue(tableList.contains("MY_TABLE1"));
    assertTrue(tableList.contains("MY_TABLE2"));
}
Also used : Replace(net.sf.jsqlparser.statement.replace.Replace) Statement(net.sf.jsqlparser.statement.Statement) StringReader(java.io.StringReader) Test(org.junit.Test) CCJSqlParserManagerTest(net.sf.jsqlparser.test.simpleparsing.CCJSqlParserManagerTest)

Example 2 with Replace

use of net.sf.jsqlparser.statement.replace.Replace in project JSqlParser by JSQLParser.

the class StatementDeParserTest method shouldUseProvidedDeParsersWhenDeParsingReplaceWithItemsList.

@Test
@SuppressWarnings("PMD.JUnitTestsShouldIncludeAssert")
public void shouldUseProvidedDeParsersWhenDeParsingReplaceWithItemsList() {
    Replace replace = new Replace();
    Table table = new Table();
    ItemsList itemsList = mock(ItemsList.class);
    replace.setTable(table);
    replace.setItemsList(itemsList);
    statementDeParser.visit(replace);
    then(itemsList).should().accept(argThat(is(replaceDeParserWithDeParsers(equalTo(expressionDeParser), equalTo(selectDeParser)))));
}
Also used : Replace(net.sf.jsqlparser.statement.replace.Replace) Table(net.sf.jsqlparser.schema.Table) ItemsList(net.sf.jsqlparser.expression.operators.relational.ItemsList) Test(org.junit.Test)

Example 3 with Replace

use of net.sf.jsqlparser.statement.replace.Replace in project JSqlParser by JSQLParser.

the class ReplaceTest method testReplaceSyntax1.

@Test
public void testReplaceSyntax1() throws JSQLParserException {
    String statement = "REPLACE mytable SET col1='as', col2=?, col3=565";
    Replace replace = (Replace) PARSER_MANAGER.parse(new StringReader(statement));
    assertEquals("mytable", replace.getTable().getName());
    assertEquals(3, replace.getColumns().size());
    assertEquals("col1", ((Column) replace.getColumns().get(0)).getColumnName());
    assertEquals("col2", ((Column) replace.getColumns().get(1)).getColumnName());
    assertEquals("col3", ((Column) replace.getColumns().get(2)).getColumnName());
    assertEquals("as", ((StringValue) replace.getExpressions().get(0)).getValue());
    assertTrue(replace.getExpressions().get(1) instanceof JdbcParameter);
    assertEquals(565, ((LongValue) replace.getExpressions().get(2)).getValue());
    assertEquals(statement, "" + replace);
}
Also used : Replace(net.sf.jsqlparser.statement.replace.Replace) JdbcParameter(net.sf.jsqlparser.expression.JdbcParameter) StringReader(java.io.StringReader) Test(org.junit.Test)

Example 4 with Replace

use of net.sf.jsqlparser.statement.replace.Replace in project JSqlParser by JSQLParser.

the class ReplaceTest method testReplaceSyntax3.

@Test
public void testReplaceSyntax3() throws JSQLParserException {
    String statement = "REPLACE mytable (col1, col2, col3) SELECT * FROM mytable3";
    Replace replace = (Replace) PARSER_MANAGER.parse(new StringReader(statement));
    assertEquals("mytable", replace.getTable().getName());
    assertEquals(3, replace.getColumns().size());
    assertEquals("col1", ((Column) replace.getColumns().get(0)).getColumnName());
    assertEquals("col2", ((Column) replace.getColumns().get(1)).getColumnName());
    assertEquals("col3", ((Column) replace.getColumns().get(2)).getColumnName());
    assertTrue(replace.getItemsList() instanceof SubSelect);
}
Also used : Replace(net.sf.jsqlparser.statement.replace.Replace) StringReader(java.io.StringReader) SubSelect(net.sf.jsqlparser.statement.select.SubSelect) Test(org.junit.Test)

Example 5 with Replace

use of net.sf.jsqlparser.statement.replace.Replace in project JSqlParser by JSQLParser.

the class StatementDeParserTest method shouldUseProvidedDeParsersWhenDeParsingReplaceWithoutItemsList.

@Test
@SuppressWarnings("PMD.JUnitTestsShouldIncludeAssert")
public void shouldUseProvidedDeParsersWhenDeParsingReplaceWithoutItemsList() {
    Replace replace = new Replace();
    Table table = new Table();
    List<Column> columns = new ArrayList<Column>();
    List<Expression> expressions = new ArrayList<Expression>();
    Column column1 = new Column();
    Column column2 = new Column();
    Expression expression1 = mock(Expression.class);
    Expression expression2 = mock(Expression.class);
    replace.setTable(table);
    replace.setColumns(columns);
    replace.setExpressions(expressions);
    columns.add(column1);
    columns.add(column2);
    expressions.add(expression1);
    expressions.add(expression2);
    statementDeParser.visit(replace);
    then(expression1).should().accept(expressionDeParser);
    then(expression2).should().accept(expressionDeParser);
}
Also used : Replace(net.sf.jsqlparser.statement.replace.Replace) Table(net.sf.jsqlparser.schema.Table) Column(net.sf.jsqlparser.schema.Column) Expression(net.sf.jsqlparser.expression.Expression) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Aggregations

Replace (net.sf.jsqlparser.statement.replace.Replace)6 Test (org.junit.Test)6 StringReader (java.io.StringReader)4 JdbcParameter (net.sf.jsqlparser.expression.JdbcParameter)2 Table (net.sf.jsqlparser.schema.Table)2 ArrayList (java.util.ArrayList)1 Expression (net.sf.jsqlparser.expression.Expression)1 ItemsList (net.sf.jsqlparser.expression.operators.relational.ItemsList)1 Column (net.sf.jsqlparser.schema.Column)1 Statement (net.sf.jsqlparser.statement.Statement)1 SubSelect (net.sf.jsqlparser.statement.select.SubSelect)1 CCJSqlParserManagerTest (net.sf.jsqlparser.test.simpleparsing.CCJSqlParserManagerTest)1