Search in sources :

Example 86 with PGSchemaStatVisitor

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

the class PGCreateTableTest_11 method test_0.

public void test_0() throws Exception {
    String sql = "CREATE TABLE KTV.KTV_FUNCTION_ROLE_20151211 (\n" + "\tURL VARCHAR(150),\n" + "\tFUNCTION VARCHAR(1002),\n" + "\tROLE VARCHAR(100),\n" + "\t\"COUNT\" VARCHAR(102)\n" + ")";
    PGSQLStatementParser parser = new PGSQLStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement stmt = statementList.get(0);
    assertEquals("CREATE TABLE KTV.KTV_FUNCTION_ROLE_20151211 (\n" + "\tURL VARCHAR(150),\n" + "\tFUNCTION VARCHAR(1002),\n" + "\tROLE VARCHAR(100),\n" + "\t\"COUNT\" VARCHAR(102)\n" + ")", SQLUtils.toPGString(stmt));
    assertEquals("create table KTV.KTV_FUNCTION_ROLE_20151211 (\n" + "\tURL VARCHAR(150),\n" + "\tFUNCTION VARCHAR(1002),\n" + "\tROLE VARCHAR(100),\n" + "\t\"COUNT\" VARCHAR(102)\n" + ")", 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("KTV.KTV_FUNCTION_ROLE_20151211")));
    assertTrue(visitor.getTables().get(new TableStat.Name("KTV.KTV_FUNCTION_ROLE_20151211")).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)

Example 87 with PGSchemaStatVisitor

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

the class PGCreateTableTest_3 method test_0.

public void test_0() throws Exception {
    String sql = // 
    "CREATE TABLE products (" + // 
    "    product_no integer," + // 
    "    name text," + // 
    "    price numeric DEFAULT nextval('products_product_no_seq')" + ");";
    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("products")));
    Assert.assertTrue(visitor.getTables().get(new TableStat.Name("products")).getCreateCount() == 1);
    Assert.assertTrue(visitor.getColumns().size() == 3);
}
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 88 with PGSchemaStatVisitor

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

the class PGCreateTableTest_5 method test_0.

public void test_0() throws Exception {
    String sql = // 
    "CREATE TABLE products (" + // 
    "  state           char(2)" + ") INHERITS (cities);";
    PGSQLStatementParser parser = new PGSQLStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement stmt = statementList.get(0);
    Assert.assertEquals("CREATE TABLE products (" + "\n\tstate char(2)" + "\n) INHERITS (cities);", SQLUtils.toPGString(stmt));
    Assert.assertEquals("create table products (" + "\n\tstate char(2)" + "\n) inherits (cities);", SQLUtils.toPGString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
    Assert.assertEquals(1, statementList.size());
    PGSchemaStatVisitor visitor = new PGSchemaStatVisitor();
    stmt.accept(visitor);
    // System.out.println("Tables : " + visitor.getTables());
    // System.out.println("fields : " + visitor.getColumns());
    Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("products")));
    Assert.assertTrue(visitor.getTables().get(new TableStat.Name("products")).getCreateCount() == 1);
    Assert.assertTrue(visitor.getColumns().size() == 1);
}
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 89 with PGSchemaStatVisitor

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

the class PGDeleteTest method test_0.

public void test_0() throws Exception {
    String sql = "DELETE FROM films USING producers WHERE producer_id = producers.id AND producers.name = 'foo';";
    PGSQLStatementParser parser = new PGSQLStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement stmt = statementList.get(0);
    // print(statementList);
    Assert.assertEquals(1, statementList.size());
    {
        SchemaStatVisitor v = new SchemaStatVisitor();
        stmt.accept(v);
    }
    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());
    Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("films")));
    Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("producers")));
// Assert.assertTrue(visitor.getFields().size() == 0);
}
Also used : PGSchemaStatVisitor(com.alibaba.druid.sql.dialect.postgresql.visitor.PGSchemaStatVisitor) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SchemaStatVisitor(com.alibaba.druid.sql.visitor.SchemaStatVisitor) PGSchemaStatVisitor(com.alibaba.druid.sql.dialect.postgresql.visitor.PGSchemaStatVisitor) PGSQLStatementParser(com.alibaba.druid.sql.dialect.postgresql.parser.PGSQLStatementParser)

Example 90 with PGSchemaStatVisitor

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

the class PGSelectIntoTest method test_0.

public void test_0() throws Exception {
    String sql = "SELECT * INTO films_recent FROM films WHERE date_prod >= '2002-01-01';";
    PGSQLStatementParser parser = new PGSQLStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement stmt = statementList.get(0);
    // print(statementList);
    Assert.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());
    Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("films_recent")));
    Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("films")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("films", "*")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("films", "date_prod")));
    String result = SQLUtils.toPGString(stmt);
    Assert.assertEquals(result, // 
    "SELECT *" + // 
    "\nINTO films_recent" + // 
    "\nFROM films" + "\nWHERE date_prod >= '2002-01-01';");
    String result_lcase = SQLUtils.toPGString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION);
    Assert.assertEquals(result_lcase, // 
    "select *" + // 
    "\ninto films_recent" + // 
    "\nfrom films" + "\nwhere date_prod >= '2002-01-01';");
}
Also used : Column(com.alibaba.druid.stat.TableStat.Column) 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