Search in sources :

Example 1 with PGSelectStatement

use of com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGSelectStatement in project druid by alibaba.

the class PGSQLStatementParser method parseWith.

public SQLStatement parseWith() {
    PGWithClause with = this.parseWithClause();
    if (lexer.token() == Token.INSERT) {
        PGInsertStatement stmt = this.parseInsert();
        stmt.setWith(with);
        return stmt;
    }
    if (lexer.token() == Token.SELECT) {
        PGSelectStatement stmt = this.parseSelect();
        stmt.setWith(with);
        return stmt;
    }
    if (lexer.token() == Token.DELETE) {
        PGDeleteStatement stmt = this.parseDeleteStatement();
        stmt.setWith(with);
        return stmt;
    }
    throw new ParserException("TODO");
}
Also used : PGSelectStatement(com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGSelectStatement) PGWithClause(com.alibaba.druid.sql.dialect.postgresql.ast.PGWithClause) PGInsertStatement(com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGInsertStatement) PGDeleteStatement(com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGDeleteStatement)

Example 2 with PGSelectStatement

use of com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGSelectStatement in project druid by alibaba.

the class PGSelectTest21 method test_1.

public void test_1() throws Exception {
    String sql = "with a(a1,b1) as (select * from b) select * from a";
    PGSQLStatementParser parser = new PGSQLStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement statemen = statementList.get(0);
    //        print(statementList);
    assertTrue(statemen instanceof PGSelectStatement);
    assertTrue(((PGSelectStatement) statemen).getWith().getWithQuery().size() == 1);
    StringBuffer sb = new StringBuffer();
    ((PGSelectStatement) statemen).getWith().getWithQuery().get(0).getName().output(sb);
    assertTrue("a".equals(sb.toString()));
    assertTrue(((PGSelectStatement) statemen).getWith().getWithQuery().get(0).getColumns().size() == 2);
}
Also used : PGSelectStatement(com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGSelectStatement) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) PGSQLStatementParser(com.alibaba.druid.sql.dialect.postgresql.parser.PGSQLStatementParser)

Example 3 with PGSelectStatement

use of com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGSelectStatement in project druid by alibaba.

the class PGSelectTest1 method test_1.

public void test_1() throws Exception {
    String sql = "(select * from a) union select * from b";
    PGSQLStatementParser parser = new PGSQLStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement statemen = statementList.get(0);
    //         print(statementList);
    Assert.assertEquals(1, statementList.size());
    assertTrue(statemen instanceof PGSelectStatement);
    PGSelectStatement select = (PGSelectStatement) statemen;
    assertTrue(select.getSelect().getQuery() instanceof SQLUnionQuery);
    SQLUnionQuery unionQuery = (SQLUnionQuery) select.getSelect().getQuery();
    assertTrue(unionQuery.getLeft() instanceof SQLSelectQueryBlock);
    assertTrue(unionQuery.getRight() instanceof SQLSelectQueryBlock);
    SQLSelectQueryBlock leftQueryBlock = (SQLSelectQueryBlock) unionQuery.getLeft();
    assertTrue(leftQueryBlock.isParenthesized());
}
Also used : PGSelectStatement(com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGSelectStatement) SQLUnionQuery(com.alibaba.druid.sql.ast.statement.SQLUnionQuery) SQLSelectQueryBlock(com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) PGSQLStatementParser(com.alibaba.druid.sql.dialect.postgresql.parser.PGSQLStatementParser)

Example 4 with PGSelectStatement

use of com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGSelectStatement in project druid by alibaba.

the class PGSQLStatementParser method parseSelect.

public PGSelectStatement parseSelect() {
    PGSelectParser selectParser = createSQLSelectParser();
    SQLSelect select = selectParser.select();
    return new PGSelectStatement(select);
}
Also used : PGSelectStatement(com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGSelectStatement) SQLSelect(com.alibaba.druid.sql.ast.statement.SQLSelect)

Example 5 with PGSelectStatement

use of com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGSelectStatement in project druid by alibaba.

the class PGIntervalSQLTest method equal.

private void equal(String targetSql, String resultSql) {
    PGSQLStatementParser parser = new PGSQLStatementParser(targetSql);
    PGSelectStatement statement = parser.parseSelect();
    Assert.assertTrue(statement.toString().equals(resultSql));
}
Also used : PGSelectStatement(com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGSelectStatement) PGSQLStatementParser(com.alibaba.druid.sql.dialect.postgresql.parser.PGSQLStatementParser)

Aggregations

PGSelectStatement (com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGSelectStatement)5 PGSQLStatementParser (com.alibaba.druid.sql.dialect.postgresql.parser.PGSQLStatementParser)3 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)2 SQLSelect (com.alibaba.druid.sql.ast.statement.SQLSelect)1 SQLSelectQueryBlock (com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock)1 SQLUnionQuery (com.alibaba.druid.sql.ast.statement.SQLUnionQuery)1 PGWithClause (com.alibaba.druid.sql.dialect.postgresql.ast.PGWithClause)1 PGDeleteStatement (com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGDeleteStatement)1 PGInsertStatement (com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGInsertStatement)1