Search in sources :

Example 6 with SQLUnionQuery

use of com.alibaba.druid.sql.ast.statement.SQLUnionQuery 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 7 with SQLUnionQuery

use of com.alibaba.druid.sql.ast.statement.SQLUnionQuery in project druid by alibaba.

the class PagerUtils method count.

private static String count(SQLSelect select, String dbType) {
    if (select.getOrderBy() != null) {
        select.setOrderBy(null);
    }
    SQLSelectQuery query = select.getQuery();
    clearOrderBy(query);
    if (query instanceof SQLSelectQueryBlock) {
        SQLSelectItem countItem = createCountItem(dbType);
        SQLSelectQueryBlock queryBlock = (SQLSelectQueryBlock) query;
        if (queryBlock.getGroupBy() != null && queryBlock.getGroupBy().getItems().size() > 0) {
            return createCountUseSubQuery(select, dbType);
        }
        int option = queryBlock.getDistionOption();
        if (option == SQLSetQuantifier.DISTINCT && queryBlock.getSelectList().size() == 1) {
            SQLSelectItem firstItem = queryBlock.getSelectList().get(0);
            SQLAggregateExpr exp = new SQLAggregateExpr("COUNT", SQLAggregateOption.DISTINCT);
            exp.addArgument(firstItem.getExpr());
            firstItem.setExpr(exp);
            queryBlock.setDistionOption(0);
        } else {
            queryBlock.getSelectList().clear();
            queryBlock.getSelectList().add(countItem);
        }
        return SQLUtils.toSQLString(select, dbType);
    } else if (query instanceof SQLUnionQuery) {
        return createCountUseSubQuery(select, dbType);
    }
    throw new IllegalStateException();
}
Also used : SQLSelectItem(com.alibaba.druid.sql.ast.statement.SQLSelectItem) SQLUnionQuery(com.alibaba.druid.sql.ast.statement.SQLUnionQuery) SQLSelectQuery(com.alibaba.druid.sql.ast.statement.SQLSelectQuery) SQLSelectQueryBlock(com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock) SQLAggregateExpr(com.alibaba.druid.sql.ast.expr.SQLAggregateExpr)

Example 8 with SQLUnionQuery

use of com.alibaba.druid.sql.ast.statement.SQLUnionQuery in project druid by alibaba.

the class SQLSelectParser method parseTableSource.

public SQLTableSource parseTableSource() {
    if (lexer.token() == Token.LPAREN) {
        lexer.nextToken();
        SQLTableSource tableSource;
        if (lexer.token() == Token.SELECT || lexer.token() == Token.WITH || lexer.token == Token.SEL) {
            SQLSelect select = select();
            accept(Token.RPAREN);
            SQLSelectQuery query = queryRest(select.getQuery());
            if (query instanceof SQLUnionQuery) {
                tableSource = new SQLUnionQueryTableSource((SQLUnionQuery) query);
            } else {
                tableSource = new SQLSubqueryTableSource(select);
            }
        } else if (lexer.token() == Token.LPAREN) {
            tableSource = parseTableSource();
            accept(Token.RPAREN);
        } else {
            tableSource = parseTableSource();
            accept(Token.RPAREN);
        }
        return parseTableSourceRest(tableSource);
    }
    if (lexer.token() == Token.SELECT) {
        throw new ParserException("TODO");
    }
    SQLExprTableSource tableReference = new SQLExprTableSource();
    parseTableSourceQueryTableExpr(tableReference);
    SQLTableSource tableSrc = parseTableSourceRest(tableReference);
    if (lexer.hasComment() && lexer.isKeepComments()) {
        tableSrc.addAfterComment(lexer.readAndResetComments());
    }
    return tableSrc;
}
Also used : SQLSubqueryTableSource(com.alibaba.druid.sql.ast.statement.SQLSubqueryTableSource) SQLSelect(com.alibaba.druid.sql.ast.statement.SQLSelect) SQLUnionQueryTableSource(com.alibaba.druid.sql.ast.statement.SQLUnionQueryTableSource) SQLUnionQuery(com.alibaba.druid.sql.ast.statement.SQLUnionQuery) SQLSelectQuery(com.alibaba.druid.sql.ast.statement.SQLSelectQuery) SQLExprTableSource(com.alibaba.druid.sql.ast.statement.SQLExprTableSource) SQLTableSource(com.alibaba.druid.sql.ast.statement.SQLTableSource)

Example 9 with SQLUnionQuery

use of com.alibaba.druid.sql.ast.statement.SQLUnionQuery in project druid by alibaba.

the class SQLSelectParser method queryRest.

public SQLSelectQuery queryRest(SQLSelectQuery selectQuery) {
    if (lexer.token() == Token.UNION) {
        lexer.nextToken();
        SQLUnionQuery union = createSQLUnionQuery();
        union.setLeft(selectQuery);
        if (lexer.token() == Token.ALL) {
            union.setOperator(SQLUnionOperator.UNION_ALL);
            lexer.nextToken();
        } else if (lexer.token() == Token.DISTINCT) {
            union.setOperator(SQLUnionOperator.DISTINCT);
            lexer.nextToken();
        }
        SQLSelectQuery right = this.query();
        union.setRight(right);
        return unionRest(union);
    }
    if (lexer.token() == Token.EXCEPT) {
        lexer.nextToken();
        SQLUnionQuery union = new SQLUnionQuery();
        union.setLeft(selectQuery);
        union.setOperator(SQLUnionOperator.EXCEPT);
        SQLSelectQuery right = this.query();
        union.setRight(right);
        return union;
    }
    if (lexer.token() == Token.INTERSECT) {
        lexer.nextToken();
        SQLUnionQuery union = new SQLUnionQuery();
        union.setLeft(selectQuery);
        union.setOperator(SQLUnionOperator.INTERSECT);
        SQLSelectQuery right = this.query();
        union.setRight(right);
        return union;
    }
    if (lexer.token() == Token.MINUS) {
        lexer.nextToken();
        SQLUnionQuery union = new SQLUnionQuery();
        union.setLeft(selectQuery);
        union.setOperator(SQLUnionOperator.MINUS);
        SQLSelectQuery right = this.query();
        union.setRight(right);
        return union;
    }
    return selectQuery;
}
Also used : SQLUnionQuery(com.alibaba.druid.sql.ast.statement.SQLUnionQuery) SQLSelectQuery(com.alibaba.druid.sql.ast.statement.SQLSelectQuery)

Example 10 with SQLUnionQuery

use of com.alibaba.druid.sql.ast.statement.SQLUnionQuery in project druid by alibaba.

the class MySqlSelectTest_103_union_orderBy method test_0.

public void test_0() throws Exception {
    String sql = "\n" + "select a from a1 union select a from a2 union select a from a3 union select a from a4 order by a desc";
    MySqlStatementParser parser = new MySqlStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    assertEquals(1, statementList.size());
    SQLSelectStatement stmt = (SQLSelectStatement) statementList.get(0);
    System.out.println(stmt);
    SQLUnionQuery union = (SQLUnionQuery) stmt.getSelect().getQuery();
    assertNotNull(union.getOrderBy());
    assertTrue(union.getLeft() instanceof SQLUnionQuery);
    assertFalse(union.getRight() instanceof SQLUnionQuery);
    assertTrue(((SQLUnionQuery) union.getLeft()).getLeft() instanceof SQLUnionQuery);
    assertFalse(((SQLUnionQuery) union.getLeft()).getRight() instanceof SQLUnionQuery);
    assertEquals("SELECT a\n" + "FROM a1\n" + "UNION\n" + "SELECT a\n" + "FROM a2\n" + "UNION\n" + "SELECT a\n" + "FROM a3\n" + "UNION\n" + "SELECT a\n" + "FROM a4\n" + "ORDER BY a DESC", stmt.toString());
}
Also used : SQLUnionQuery(com.alibaba.druid.sql.ast.statement.SQLUnionQuery) SQLSelectStatement(com.alibaba.druid.sql.ast.statement.SQLSelectStatement) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Aggregations

SQLUnionQuery (com.alibaba.druid.sql.ast.statement.SQLUnionQuery)14 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)9 SQLSelectStatement (com.alibaba.druid.sql.ast.statement.SQLSelectStatement)8 SQLSelectQueryBlock (com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock)5 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)4 SQLSelectQuery (com.alibaba.druid.sql.ast.statement.SQLSelectQuery)3 SQLLimit (com.alibaba.druid.sql.ast.SQLLimit)1 SQLAggregateExpr (com.alibaba.druid.sql.ast.expr.SQLAggregateExpr)1 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)1 SQLExprTableSource (com.alibaba.druid.sql.ast.statement.SQLExprTableSource)1 SQLSelect (com.alibaba.druid.sql.ast.statement.SQLSelect)1 SQLSelectItem (com.alibaba.druid.sql.ast.statement.SQLSelectItem)1 SQLSubqueryTableSource (com.alibaba.druid.sql.ast.statement.SQLSubqueryTableSource)1 SQLTableSource (com.alibaba.druid.sql.ast.statement.SQLTableSource)1 SQLUnionQueryTableSource (com.alibaba.druid.sql.ast.statement.SQLUnionQueryTableSource)1 DB2SelectQueryBlock (com.alibaba.druid.sql.dialect.db2.ast.stmt.DB2SelectQueryBlock)1 DB2ASTVisitorAdapter (com.alibaba.druid.sql.dialect.db2.visitor.DB2ASTVisitorAdapter)1 MySqlSelectQueryBlock (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock)1 PGSelectQueryBlock (com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGSelectQueryBlock)1 PGSelectStatement (com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGSelectStatement)1