Search in sources :

Example 11 with SQLUnionQuery

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

the class MySqlSelectTest_102_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 order by a desc;";
    MySqlStatementParser parser = new MySqlStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    assertEquals(1, statementList.size());
    SQLSelectStatement stmt = (SQLSelectStatement) statementList.get(0);
    SQLUnionQuery union = (SQLUnionQuery) stmt.getSelect().getQuery();
    assertNotNull(union.getOrderBy());
    assertTrue(union.getLeft() 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" + "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)

Example 12 with SQLUnionQuery

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

the class MySqlSelectTest_104_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 union select a from a5 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" + "UNION\n" + "SELECT a\n" + "FROM a5\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)

Example 13 with SQLUnionQuery

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

the class MySqlSelectTest_233 method test_0.

public void test_0() throws Exception {
    String sql = "select f1 \n" + "from t1\n" + "union all\n" + "select f2\n" + "from t2";
    SQLSelectStatement stmt = (SQLSelectStatement) SQLUtils.parseSingleMysqlStatement(sql);
    SQLUnionQuery query = (SQLUnionQuery) stmt.getSelect().getQuery();
    SQLSelectQueryBlock left = (SQLSelectQueryBlock) query.getLeft();
    left.addOrderBy(new SQLIdentifierExpr("f1"));
    SQLSelectQueryBlock right = (SQLSelectQueryBlock) query.getRight();
    right.addOrderBy(new SQLIdentifierExpr("f1"));
    assertEquals("SELECT f1\n" + "FROM t1\n" + "ORDER BY f1\n" + "UNION ALL\n" + "(SELECT f2\n" + "FROM t2\n" + "ORDER BY f1)", stmt.toString());
}
Also used : SQLUnionQuery(com.alibaba.druid.sql.ast.statement.SQLUnionQuery) SQLSelectStatement(com.alibaba.druid.sql.ast.statement.SQLSelectStatement) SQLSelectQueryBlock(com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)

Example 14 with SQLUnionQuery

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

the class Issue2038 method test_for_demo.

public void test_for_demo() throws Exception {
    String sql = "select * from (select * from t union all select * from t1 union all select * from t3) xx";
    List<SQLStatement> stmtList = SQLUtils.parseStatements(sql, DbType.db2);
    SQLASTVisitor visitor = new DB2ASTVisitorAdapter() {

        public boolean visit(SQLUnionQuery x) {
            System.out.println("union");
            return true;
        }

        public boolean visit(DB2SelectQueryBlock x) {
            System.out.println("select");
            return true;
        }
    };
    for (SQLStatement stmt : stmtList) {
        stmt.accept(visitor);
    }
}
Also used : SQLASTVisitor(com.alibaba.druid.sql.visitor.SQLASTVisitor) DB2ASTVisitorAdapter(com.alibaba.druid.sql.dialect.db2.visitor.DB2ASTVisitorAdapter) SQLUnionQuery(com.alibaba.druid.sql.ast.statement.SQLUnionQuery) DB2SelectQueryBlock(com.alibaba.druid.sql.dialect.db2.ast.stmt.DB2SelectQueryBlock) 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