Search in sources :

Example 66 with PGSchemaStatVisitor

use of com.alibaba.druid.sql.dialect.postgresql.visitor.PGSchemaStatVisitor in project druid by alibaba.

the class PGDeleteTest7 method test_0.

public void test_0() throws Exception {
    String sql = "WITH t AS (" + "    DELETE FROM foo" + ")" + "DELETE FROM bar;";
    PGSQLStatementParser parser = new PGSQLStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement statemen = statementList.get(0);
    // print(statementList);
    Assert.assertEquals(1, statementList.size());
    PGSchemaStatVisitor visitor = new PGSchemaStatVisitor();
    statemen.accept(visitor);
    // System.out.println("Tables : " + visitor.getTables());
    // System.out.println("fields : " + visitor.getColumns());
    // System.out.println("coditions : " + visitor.getConditions());
    Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("foo")));
    Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("bar")));
    Assert.assertTrue(visitor.getColumns().size() == 0);
// Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("products", "date")));
}
Also used : PGSchemaStatVisitor(com.alibaba.druid.sql.dialect.postgresql.visitor.PGSchemaStatVisitor) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) PGSQLStatementParser(com.alibaba.druid.sql.dialect.postgresql.parser.PGSQLStatementParser)

Example 67 with PGSchemaStatVisitor

use of com.alibaba.druid.sql.dialect.postgresql.visitor.PGSchemaStatVisitor in project druid by alibaba.

the class PGInsertTest10 method test_0.

public void test_0() throws Exception {
    String sql = "INSERT INTO test VALUES (B'101', B'00');";
    PGSQLStatementParser parser = new PGSQLStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement stmt = statementList.get(0);
    // print(statementList);
    assertEquals(1, statementList.size());
    PGSchemaStatVisitor visitor = new PGSchemaStatVisitor();
    stmt.accept(visitor);
    System.out.println("Tables : " + visitor.getTables());
    System.out.println("fields : " + visitor.getColumns());
    // System.out.println("coditions : " + visitor.getConditions());
    assertTrue(visitor.getTables().containsKey(new TableStat.Name("test")));
    assertEquals(0, visitor.getColumns().size());
    // 
    // assertTrue(visitor.getColumns().contains(new TableStat.Column("distributors", "did")));
    // assertTrue(visitor.getColumns().contains(new TableStat.Column("distributors", "dname")));
    assertEquals("INSERT INTO test\n" + "VALUES (B'101', B'00');", stmt.toString());
}
Also used : PGSchemaStatVisitor(com.alibaba.druid.sql.dialect.postgresql.visitor.PGSchemaStatVisitor) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) PGSQLStatementParser(com.alibaba.druid.sql.dialect.postgresql.parser.PGSQLStatementParser)

Example 68 with PGSchemaStatVisitor

use of com.alibaba.druid.sql.dialect.postgresql.visitor.PGSchemaStatVisitor in project druid by alibaba.

the class PGDeleteTest4 method test_0.

public void test_0() throws Exception {
    String sql = "DELETE FROM films WHERE producer_id IN (SELECT id FROM producers WHERE name = 'foo');";
    PGSQLStatementParser parser = new PGSQLStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement statemen = statementList.get(0);
    // print(statementList);
    Assert.assertEquals(1, statementList.size());
    PGSchemaStatVisitor visitor = new PGSchemaStatVisitor();
    statemen.accept(visitor);
    // System.out.println("Tables : " + visitor.getTables());
    // System.out.println("fields : " + visitor.getColumns());
    Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("films")));
    Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("producers")));
    // System.out.println("coditions : " + visitor.getConditions());
    Assert.assertTrue(visitor.getColumns().size() == 3);
    Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("films", "producer_id")));
    Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("producers", "id")));
    Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("producers", "name")));
}
Also used : PGSchemaStatVisitor(com.alibaba.druid.sql.dialect.postgresql.visitor.PGSchemaStatVisitor) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) PGSQLStatementParser(com.alibaba.druid.sql.dialect.postgresql.parser.PGSQLStatementParser)

Example 69 with PGSchemaStatVisitor

use of com.alibaba.druid.sql.dialect.postgresql.visitor.PGSchemaStatVisitor in project druid by alibaba.

the class PGDeleteTest8 method test_0.

public void test_0() throws Exception {
    String sql = "WITH RECURSIVE included_parts(sub_part, part) AS (" + "    SELECT sub_part, part FROM parts WHERE part = 'our_product'" + "  UNION ALL" + "    SELECT p.sub_part, p.part" + "    FROM included_parts pr, parts p" + "    WHERE p.part = pr.sub_part" + "  )" + "DELETE FROM parts\n" + "  WHERE part IN (SELECT part FROM included_parts);";
    PGSQLStatementParser parser = new PGSQLStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement stmt = statementList.get(0);
    System.out.println(stmt);
    assertEquals(1, statementList.size());
    PGSchemaStatVisitor visitor = new PGSchemaStatVisitor();
    stmt.accept(visitor);
    System.out.println("Tables : " + visitor.getTables());
    System.out.println("fields : " + visitor.getColumns());
    // System.out.println("coditions : " + visitor.getConditions());
    assertTrue(visitor.getTables().containsKey(new TableStat.Name("parts")));
    assertEquals(2, visitor.getColumns().size());
    assertTrue(visitor.containsColumn("parts", "sub_part"));
    assertTrue(visitor.containsColumn("parts", "sub_part"));
}
Also used : PGSchemaStatVisitor(com.alibaba.druid.sql.dialect.postgresql.visitor.PGSchemaStatVisitor) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) PGSQLStatementParser(com.alibaba.druid.sql.dialect.postgresql.parser.PGSQLStatementParser)

Example 70 with PGSchemaStatVisitor

use of com.alibaba.druid.sql.dialect.postgresql.visitor.PGSchemaStatVisitor in project druid by alibaba.

the class PGCreateSequenceTest0 method test_0.

public void test_0() throws Exception {
    String sql = "\n" + "create sequence SEQ_CONTRACT_OPER_ID    \n" + "INCREMENT BY 1\n" + "START WITH 1\n" + "NOMAXVALUE\n" + "NOCYCLE\n" + "CACHE 10;  ";
    PGSQLStatementParser parser = new PGSQLStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement stmt = statementList.get(0);
    assertEquals("CREATE SEQUENCE SEQ_CONTRACT_OPER_ID START WITH 1 INCREMENT BY 1 NO MAXVALUE NO CYCLE CACHE 10;", SQLUtils.toPGString(stmt));
    assertEquals("create sequence SEQ_CONTRACT_OPER_ID start with 1 increment by 1 no maxvalue no cycle cache 10;", SQLUtils.toPGString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
    assertEquals(1, statementList.size());
    PGSchemaStatVisitor visitor = new PGSchemaStatVisitor();
    stmt.accept(visitor);
// System.out.println("Tables : " + visitor.getTables());
// System.out.println("fields : " + visitor.getColumns());
// assertTrue(visitor.getTables().containsKey(new TableStat.Name("test_site_data_select_111")));
// 
// assertTrue(visitor.getTables().get(new TableStat.Name("test_site_data_select_111")).getCreateCount() == 1);
// 
// assertEquals(4, visitor.getColumns().size() );
}
Also used : PGSchemaStatVisitor(com.alibaba.druid.sql.dialect.postgresql.visitor.PGSchemaStatVisitor) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) PGSQLStatementParser(com.alibaba.druid.sql.dialect.postgresql.parser.PGSQLStatementParser)

Aggregations

SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)132 PGSchemaStatVisitor (com.alibaba.druid.sql.dialect.postgresql.visitor.PGSchemaStatVisitor)132 PGSQLStatementParser (com.alibaba.druid.sql.dialect.postgresql.parser.PGSQLStatementParser)127 SchemaStatVisitor (com.alibaba.druid.sql.visitor.SchemaStatVisitor)1 Column (com.alibaba.druid.stat.TableStat.Column)1 WallProvider (com.alibaba.druid.wall.WallProvider)1 PGWallProvider (com.alibaba.druid.wall.spi.PGWallProvider)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1