Search in sources :

Example 41 with PGSchemaStatVisitor

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

the class PGSelectTest23 method test_0.

public void test_0() throws Exception {
    String sql = "select id, login_name, name, password, salt, roles, register_date from user WHERE ( name like ? ) limit ? offset ?";
    PGSQLStatementParser parser = new PGSQLStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement stmt = statementList.get(0);
    //        print(statementList);
    Assert.assertEquals("SELECT id, login_name, name, password, salt" + "\n\t, roles, register_date" + "\nFROM user" + "\nWHERE name LIKE ?" + "\nLIMIT ? OFFSET ?", SQLUtils.toPGString(stmt));
    Assert.assertEquals("select id, login_name, name, password, salt" + "\n\t, roles, register_date" + "\nfrom user" + "\nwhere name like ?" + "\nlimit ? offset ?", 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());
    //        System.out.println("coditions : " + visitor.getConditions());
    Assert.assertEquals(7, visitor.getColumns().size());
    Assert.assertEquals(1, visitor.getTables().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 42 with PGSchemaStatVisitor

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

the class PGSelectTest24 method test_0.

public void test_0() throws Exception {
    String sql = "select wm_concat(article_id) over() from t_nds_web_article";
    PGSQLStatementParser parser = new PGSQLStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement stmt = statementList.get(0);
    Assert.assertEquals("SELECT wm_concat(article_id) OVER ()" + "\nFROM t_nds_web_article", SQLUtils.toPGString(stmt));
    Assert.assertEquals("select wm_concat(article_id) over ()" + "\nfrom t_nds_web_article", 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());
    //        System.out.println("coditions : " + visitor.getConditions());
    Assert.assertEquals(1, visitor.getColumns().size());
    Assert.assertEquals(1, visitor.getTables().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 43 with PGSchemaStatVisitor

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

the class PGSelectTest25 method test_0.

public void test_0() throws Exception {
    String sql = "select COALESCE((SELECT project_deduct_mandays from mytable_01 limit 1)) from t";
    PGSQLStatementParser parser = new PGSQLStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement stmt = statementList.get(0);
    Assert.assertEquals("SELECT COALESCE((" + "\n\t\tSELECT project_deduct_mandays" + "\n\t\tFROM mytable_01" + "\n\t\tLIMIT 1" + "\n\t\t))" + "\nFROM t", SQLUtils.toPGString(stmt));
    Assert.assertEquals("select COALESCE((" + "\n\t\tselect project_deduct_mandays" + "\n\t\tfrom mytable_01" + "\n\t\tlimit 1" + "\n\t\t))" + "\nfrom t", 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());
    //        System.out.println("coditions : " + visitor.getConditions());
    Assert.assertEquals(1, visitor.getColumns().size());
    Assert.assertEquals(2, visitor.getTables().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 44 with PGSchemaStatVisitor

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

the class PGSelectTest26 method test_0.

public void test_0() throws Exception {
    String sql = "select '[{\"a\":\"foo\"},{\"b\":\"bar\"},{\"c\":\"baz\"}]'::json->2 from dual";
    PGSQLStatementParser parser = new PGSQLStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement stmt = statementList.get(0);
    Assert.assertEquals("SELECT '[{\"a\":\"foo\"},{\"b\":\"bar\"},{\"c\":\"baz\"}]'::json -> 2" + "\nFROM dual", SQLUtils.toPGString(stmt));
    Assert.assertEquals("select '[{\"a\":\"foo\"},{\"b\":\"bar\"},{\"c\":\"baz\"}]'::json -> 2" + "\nfrom dual", 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());
    //        System.out.println("coditions : " + visitor.getConditions());
    Assert.assertEquals(0, visitor.getColumns().size());
    Assert.assertEquals(1, visitor.getTables().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 45 with PGSchemaStatVisitor

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

the class PGSelectTest27 method test_0.

public void test_0() throws Exception {
    String sql = "select '[{\"a\":\"foo\"},{\"b\":\"bar\"},{\"c\":\"baz\"}]'::json->>2 from dual";
    PGSQLStatementParser parser = new PGSQLStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement stmt = statementList.get(0);
    Assert.assertEquals("SELECT '[{\"a\":\"foo\"},{\"b\":\"bar\"},{\"c\":\"baz\"}]'::json ->> 2" + "\nFROM dual", SQLUtils.toPGString(stmt));
    Assert.assertEquals("select '[{\"a\":\"foo\"},{\"b\":\"bar\"},{\"c\":\"baz\"}]'::json ->> 2" + "\nfrom dual", 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());
    //        System.out.println("coditions : " + visitor.getConditions());
    Assert.assertEquals(0, visitor.getColumns().size());
    Assert.assertEquals(1, visitor.getTables().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)107 PGSchemaStatVisitor (com.alibaba.druid.sql.dialect.postgresql.visitor.PGSchemaStatVisitor)107 PGSQLStatementParser (com.alibaba.druid.sql.dialect.postgresql.parser.PGSQLStatementParser)102 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