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()); }
public SQLSelectQuery queryRest(SQLSelectQuery selectQuery) { if (lexer.token() == (Token.UNION)) { SQLUnionQuery union = new SQLUnionQuery(); union.setLeft(selectQuery); lexer.nextToken(); if (lexer.token() == (Token.ALL)) { union.setOperator(SQLUnionOperator.UNION_ALL); lexer.nextToken(); } SQLSelectQuery right = query(); union.setRight(right); return queryRest(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; }