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());
}
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();
}
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;
}
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;
}
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());
}
Aggregations