Search in sources :

Example 81 with PGSQLStatementParser

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

the class PGExportParameterVisitorTest1 method test_pg.

public void test_pg() throws Exception {
    String sql = "select fname, count(*) from t where fid = 1 group by fname order by 1";
    PGSQLStatementParser parser = new PGSQLStatementParser(sql);
    SQLStatement stmt = parser.parseStatementList().get(0);
    PGExportParameterVisitor visitor = new PGExportParameterVisitor();
    stmt.accept(visitor);
    List<Object> parameters = visitor.getParameters();
    Assert.assertEquals(1, parameters.size());
    Assert.assertEquals(1, parameters.get(0));
}
Also used : PGExportParameterVisitor(com.alibaba.druid.sql.dialect.postgresql.visitor.PGExportParameterVisitor) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) PGSQLStatementParser(com.alibaba.druid.sql.dialect.postgresql.parser.PGSQLStatementParser)

Example 82 with PGSQLStatementParser

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

the class PGInsertTest0 method test_0.

public void test_0() throws Exception {
    String sql = "INSERT INTO films VALUES ('UA502', 'Bananas', 105, '1971-07-13', 'Comedy', '82 minutes');";
    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("films")));
    Assert.assertEquals(0, visitor.getColumns().size());
//        Assert.assertTrue(visitor.getFields().contains(new TableStat.Column("films", "producer_id")));
}
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 83 with PGSQLStatementParser

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

the class PGInsertTest1 method test_0.

public void test_0() throws Exception {
    String sql = "INSERT INTO films (code, title, did, date_prod, kind) VALUES ('T_601', 'Yojimbo', 106, '1961-06-16', 'Drama');";
    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("films")));
    Assert.assertEquals(5, visitor.getColumns().size());
    Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("films", "kind")));
    Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("films", "code")));
    Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("films", "date_prod")));
    Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("films", "title")));
    Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("films", "did")));
}
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 84 with PGSQLStatementParser

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

the class PGInsertTest2 method test_0.

public void test_0() throws Exception {
    String sql = "INSERT INTO films (code, title, did, date_prod, kind) VALUES" + "    ('B6717', 'Tampopo', 110, '1985-02-10', 'Comedy'), " + "('HG120', 'The Dinner Game', 140, DEFAULT, 'Comedy');";
    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.assertEquals(5, visitor.getColumns().size());
    Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("films", "kind")));
    Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("films", "code")));
    Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("films", "date_prod")));
    Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("films", "title")));
    Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("films", "did")));
}
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 85 with PGSQLStatementParser

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

the class PGSelectTest15 method test_0.

public void test_0() throws Exception {
    String sql = "WITH RECURSIVE search_graph(id, link, data, depth) AS (" + "        SELECT g.id, g.link, g.data, 1" + "        FROM graph g" + "      UNION ALL" + "        SELECT g.id, g.link, g.data, sg.depth + 1" + "        FROM graph g, search_graph sg" + "        WHERE g.id = sg.link" + ")" + "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(7, 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)

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