Search in sources :

Example 86 with PGSQLStatementParser

use of com.alibaba.druid.sql.dialect.postgresql.parser.PGSQLStatementParser in project druid by alibaba.

the class PGSelectTest16 method test_0.

public void test_0() throws Exception {
    String sql = "WITH RECURSIVE search_graph(id, link, data, depth, path, cycle) AS (" + "        SELECT g.id, g.link, g.data, 1," + "          ARRAY[g.id]," + "          false" + "        FROM graph g" + "      UNION ALL" + "        SELECT g.id, g.link, g.data, sg.depth + 1," + "          path || g.id," + "          g.id = ANY(path)" + "        FROM graph g, search_graph sg" + "        WHERE g.id = sg.link AND NOT cycle" + ")" + "SELECT * FROM search_graph;";
    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.assertEquals(9, visitor.getColumns().size());
    Assert.assertEquals(2, visitor.getTables().size());
    Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("graph", "id")));
    Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("graph", "link")));
    Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("graph", "data")));
    Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("search_graph", "depth")));
    Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("graph", "path")));
    Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("search_graph", "link")));
    Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("graph", "cycle")));
    Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("sg", "depth")));
    Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("sg", "link")));
}
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 PGSQLStatementParser

use of com.alibaba.druid.sql.dialect.postgresql.parser.PGSQLStatementParser in project druid by alibaba.

the class PGSelectTest17 method test_0.

public void test_0() throws Exception {
    String sql = "WITH RECURSIVE search_graph(id, link, data, depth, path, cycle) AS (" + "        SELECT g.id, g.link, g.data, 1," + "          ARRAY[ROW(g.f1, g.f2)]," + "          false" + "        FROM graph g" + "      UNION ALL" + "        SELECT g.id, g.link, g.data, sg.depth + 1," + "          path || ROW(g.f1, g.f2)," + "          ROW(g.f1, g.f2) = ANY(path)" + "        FROM graph g, search_graph sg" + "        WHERE g.id = sg.link AND NOT cycle" + ")" + "SELECT * FROM search_graph;";
    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.assertEquals(11, 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 88 with PGSQLStatementParser

use of com.alibaba.druid.sql.dialect.postgresql.parser.PGSQLStatementParser in project druid by alibaba.

the class PGSelectTest22 method test_0.

public void test_0() throws Exception {
    String sql = "SELECT i.relname, d.indisunique, a.attname" + "          FROM pg_class t, pg_class i, pg_index d, pg_attribute " + "         WHERE i.relkind = 'i'" + "           AND d.indexrelid = i.oid" + "           AND d.indisprimary = 'f'" + "           AND t.oid = d.indrelid" + "           AND t.relname = 'schema_migrations'" + "           AND a.attrelid = t.oid" + "           AND ( d.indkey[0]=a.attnum OR d.indkey[1]=a.attnum" + "              OR d.indkey[2]=a.attnum OR d.indkey[3]=a.attnum" + "              OR d.indkey[4]=a.attnum OR d.indkey[5]=a.attnum" + "              OR d.indkey[6]=a.attnum OR d.indkey[7]=a.attnum" + "              OR d.indkey[8]=a.attnum OR d.indkey[9]=a.attnum )" + "        ORDER BY i.relname" + "";
    PGSQLStatementParser parser = new PGSQLStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement statemen = statementList.get(0);
    //        print(statementList);
    Assert.assertEquals("SELECT i.relname, d.indisunique, a.attname" + "\nFROM pg_class t, pg_class i, pg_index d, pg_attribute" + "\nWHERE i.relkind = 'i'" + "\n\tAND d.indexrelid = i.oid" + "\n\tAND d.indisprimary = 'f'" + "\n\tAND t.oid = d.indrelid" + "\n\tAND t.relname = 'schema_migrations'" + "\n\tAND a.attrelid = t.oid" + "\n\tAND (d.indkey[0] = a.attnum" + "\n\t\tOR d.indkey[1] = a.attnum" + "\n\t\tOR d.indkey[2] = a.attnum" + "\n\t\tOR d.indkey[3] = a.attnum" + "\n\t\tOR d.indkey[4] = a.attnum" + "\n\t\tOR d.indkey[5] = a.attnum" + "\n\t\tOR d.indkey[6] = a.attnum" + "\n\t\tOR d.indkey[7] = a.attnum" + "\n\t\tOR d.indkey[8] = a.attnum" + "\n\t\tOR d.indkey[9] = a.attnum)" + "\nORDER BY i.relname", output(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.assertEquals(11, visitor.getColumns().size());
    Assert.assertEquals(3, 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 89 with PGSQLStatementParser

use of com.alibaba.druid.sql.dialect.postgresql.parser.PGSQLStatementParser in project druid by alibaba.

the class PGSetTest method test_0.

public void test_0() throws Exception {
    String sql = "SET standard_conforming_strings=on";
    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.assertEquals(0, visitor.getColumns().size());
    String mergedSql = ParameterizedOutputVisitorUtils.parameterize(sql, JdbcUtils.POSTGRESQL);
    System.out.println(mergedSql);
}
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 90 with PGSQLStatementParser

use of com.alibaba.druid.sql.dialect.postgresql.parser.PGSQLStatementParser in project druid by alibaba.

the class PGSetTest2 method test_0.

public void test_0() throws Exception {
    String sql = "SET search_path = \"public\",\"$user\", public";
    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.assertEquals(0, visitor.getColumns().size());
    String mergedSql = ParameterizedOutputVisitorUtils.parameterize(sql, JdbcUtils.POSTGRESQL);
    assertEquals("SET search_path = \"public\",\"$user\", public", mergedSql);
}
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

PGSQLStatementParser (com.alibaba.druid.sql.dialect.postgresql.parser.PGSQLStatementParser)113 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)109 PGSchemaStatVisitor (com.alibaba.druid.sql.dialect.postgresql.visitor.PGSchemaStatVisitor)102 PGSelectStatement (com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGSelectStatement)3 PGExportParameterVisitor (com.alibaba.druid.sql.dialect.postgresql.visitor.PGExportParameterVisitor)2 ParserException (com.alibaba.druid.sql.parser.ParserException)2 SQLOrderBy (com.alibaba.druid.sql.ast.SQLOrderBy)1 SQLSelectQueryBlock (com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock)1 SQLUnionQuery (com.alibaba.druid.sql.ast.statement.SQLUnionQuery)1 DB2StatementParser (com.alibaba.druid.sql.dialect.db2.parser.DB2StatementParser)1 MySqlSelectQueryBlock (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock)1 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)1 OracleStatementParser (com.alibaba.druid.sql.dialect.oracle.parser.OracleStatementParser)1 PGDeleteStatement (com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGDeleteStatement)1 PGInsertStatement (com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGInsertStatement)1 PGSelectQueryBlock (com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGSelectQueryBlock)1 PGOutputVisitor (com.alibaba.druid.sql.dialect.postgresql.visitor.PGOutputVisitor)1 SQLServerSelectQueryBlock (com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerSelectQueryBlock)1 SQLServerStatementParser (com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerStatementParser)1 Column (com.alibaba.druid.stat.TableStat.Column)1