Search in sources :

Example 1 with SQLUnionQuery

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

the class PagerUtils method clearOrderBy.

private static void clearOrderBy(SQLSelectQuery query) {
    if (query instanceof SQLSelectQueryBlock) {
        SQLSelectQueryBlock queryBlock = (SQLSelectQueryBlock) query;
        if (queryBlock instanceof MySqlSelectQueryBlock) {
            MySqlSelectQueryBlock mysqlQueryBlock = (MySqlSelectQueryBlock) queryBlock;
            if (mysqlQueryBlock.getOrderBy() != null) {
                mysqlQueryBlock.setOrderBy(null);
            }
        } else if (queryBlock instanceof PGSelectQueryBlock) {
            PGSelectQueryBlock pgQueryBlock = (PGSelectQueryBlock) queryBlock;
            if (pgQueryBlock.getOrderBy() != null) {
                pgQueryBlock.setOrderBy(null);
            }
        }
        return;
    }
    if (query instanceof SQLUnionQuery) {
        SQLUnionQuery union = (SQLUnionQuery) query;
        if (union.getOrderBy() != null) {
            union.setOrderBy(null);
        }
        clearOrderBy(union.getLeft());
        clearOrderBy(union.getRight());
    }
}
Also used : SQLUnionQuery(com.alibaba.druid.sql.ast.statement.SQLUnionQuery) SQLSelectQueryBlock(com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock) MySqlSelectQueryBlock(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock) PGSelectQueryBlock(com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGSelectQueryBlock)

Example 2 with SQLUnionQuery

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

the class MySqlSelectTest_138_ads_minus method test_0.

public void test_0() throws Exception {
    String sql = "SELECT DISTINCT o_orderkey\n" + "FROM simple_query2.orders__0\n" + "WHERE o_orderdate > '1995-09-31'\n" + "UNION\n" + "SELECT DISTINCT o_orderkey\n" + "FROM simple_query2.orders__0\n" + "WHERE o_orderdate > '1994-09-31'\n" + "INTERSECT\n" + "SELECT DISTINCT o_orderkey\n" + "FROM simple_query2.orders__0\n" + "WHERE o_orderdate > '1996-09-31'\n" + "MINUS\n" + "SELECT DISTINCT o_orderkey\n" + "FROM simple_query2.orders__0\n" + "WHERE o_orderdate > '1994-09-31'\n";
    List<SQLStatement> statementList = SQLUtils.parseStatements(sql, JdbcConstants.MYSQL);
    SQLSelectStatement stmt = (SQLSelectStatement) statementList.get(0);
    assertEquals(1, statementList.size());
    assertEquals("SELECT DISTINCT o_orderkey\n" + "FROM simple_query2.orders__0\n" + "WHERE o_orderdate > '1995-09-31'\n" + "UNION\n" + "SELECT DISTINCT o_orderkey\n" + "FROM simple_query2.orders__0\n" + "WHERE o_orderdate > '1994-09-31'\n" + "INTERSECT\n" + "SELECT DISTINCT o_orderkey\n" + "FROM simple_query2.orders__0\n" + "WHERE o_orderdate > '1996-09-31'\n" + "MINUS\n" + "SELECT DISTINCT o_orderkey\n" + "FROM simple_query2.orders__0\n" + "WHERE o_orderdate > '1994-09-31'", stmt.toString());
    SQLUnionQuery q1 = (SQLUnionQuery) stmt.getSelect().getQuery();
    assertTrue(q1.getRight() instanceof SQLSelectQueryBlock);
    SQLUnionQuery q2 = (SQLUnionQuery) q1.getLeft();
    assertTrue(q2.getRight() instanceof SQLSelectQueryBlock);
    SQLUnionQuery q3 = (SQLUnionQuery) q2.getLeft();
    assertTrue(q3.getRight() instanceof SQLSelectQueryBlock);
    assertTrue(q3.getLeft() instanceof SQLSelectQueryBlock);
}
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) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Example 3 with SQLUnionQuery

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

the class MySqlSelectTest_174_union method test_0.

public void test_0() throws Exception {
    String sql = "select id from a where a.id < 10 union select id from b where a.id < 10 limit 10";
    // 
    List<SQLStatement> statementList = SQLUtils.parseStatements(sql, JdbcConstants.MYSQL);
    SQLSelectStatement stmt = (SQLSelectStatement) statementList.get(0);
    assertEquals(1, statementList.size());
    assertEquals("SELECT id\n" + "FROM a\n" + "WHERE a.id < 10\n" + "UNION\n" + "SELECT id\n" + "FROM b\n" + "WHERE a.id < 10\n" + "LIMIT 10", stmt.toString());
    SQLUnionQuery union = (SQLUnionQuery) stmt.getSelect().getQuery();
    SQLLimit limit = union.getLimit();
    assertNotNull(limit);
}
Also used : SQLUnionQuery(com.alibaba.druid.sql.ast.statement.SQLUnionQuery) SQLLimit(com.alibaba.druid.sql.ast.SQLLimit) SQLSelectStatement(com.alibaba.druid.sql.ast.statement.SQLSelectStatement) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Example 4 with SQLUnionQuery

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

the class MySqlSelectTest_99_union_order method test_0.

public void test_0() throws Exception {
    String sql = "select id as id1 from ll union select id as id1 from rr order by id desc;";
    MySqlStatementParser parser = new MySqlStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    assertEquals(1, statementList.size());
    SQLSelectStatement stmt = (SQLSelectStatement) statementList.get(0);
    assertEquals("SELECT id AS id1\n" + "FROM ll\n" + "UNION\n" + "SELECT id AS id1\n" + "FROM rr\n" + "ORDER BY id DESC;", stmt.toString());
    SQLUnionQuery union = (SQLUnionQuery) stmt.getSelect().getQuery();
    assertNotNull(union.getOrderBy());
// SQLCommentHint hint = (SQLCommentHint) stmt.getSelect().getHints().get(0);
// assertEquals("+ xxx ", hint.getText());
}
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 5 with SQLUnionQuery

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

the class OracleSelectTest116 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";
    List<SQLStatement> statementList = SQLUtils.parseStatements(sql, JdbcConstants.ORACLE);
    assertEquals(1, statementList.size());
    SQLSelectStatement stmt = (SQLSelectStatement) statementList.get(0);
    System.out.println(stmt);
    SQLUnionQuery union = (SQLUnionQuery) stmt.getSelect().getQuery();
    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", stmt.toString());
}
Also used : SQLUnionQuery(com.alibaba.druid.sql.ast.statement.SQLUnionQuery) SQLSelectStatement(com.alibaba.druid.sql.ast.statement.SQLSelectStatement) 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