Search in sources :

Example 1 with DMLSelectUnionStatement

use of com.alibaba.cobar.parser.ast.stmt.dml.DMLSelectUnionStatement in project cobar by alibaba.

the class MySQLDMLSelectParserTest method testSelectUnion.

public void testSelectUnion() throws SQLSyntaxErrorException {
    String sql = "(select id from t1) union all (select id from t2) union all (select id from t3) ordeR By d desC limit 1 offset ?";
    MySQLLexer lexer = new MySQLLexer(sql);
    MySQLDMLSelectParser parser = new MySQLDMLSelectParser(lexer, new MySQLExprParser(lexer));
    DMLSelectUnionStatement select = (DMLSelectUnionStatement) parser.selectUnion();
    Assert.assertEquals(0, select.getFirstDistinctIndex());
    Assert.assertEquals(3, select.getSelectStmtList().size());
    String output = output2MySQL(select, sql);
    Assert.assertEquals("(SELECT id FROM t1) UNION ALL (SELECT id FROM t2) UNION ALL (SELECT id FROM t3) ORDER BY d DESC LIMIT ?, 1", output);
    sql = "(select id from t1) union  select id from t2 order by id union aLl (select id from t3) ordeR By d asC";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDMLSelectParser(lexer, new MySQLExprParser(lexer));
    select = (DMLSelectUnionStatement) parser.selectUnion();
    Assert.assertEquals(1, select.getFirstDistinctIndex());
    Assert.assertEquals(3, select.getSelectStmtList().size());
    output = output2MySQL(select, sql);
    Assert.assertEquals("(SELECT id FROM t1) UNION (SELECT id FROM t2 ORDER BY id) UNION ALL (SELECT id FROM t3) ORDER BY d", output);
    sql = "(select id from t1) union distInct (select id from t2) union  select id from t3";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDMLSelectParser(lexer, new MySQLExprParser(lexer));
    select = (DMLSelectUnionStatement) parser.selectUnion();
    Assert.assertEquals(2, select.getFirstDistinctIndex());
    Assert.assertEquals(3, select.getSelectStmtList().size());
    output = output2MySQL(select, sql);
    Assert.assertEquals("(SELECT id FROM t1) UNION (SELECT id FROM t2) UNION (SELECT id FROM t3)", output);
}
Also used : MySQLLexer(com.alibaba.cobar.parser.recognizer.mysql.lexer.MySQLLexer) DMLSelectUnionStatement(com.alibaba.cobar.parser.ast.stmt.dml.DMLSelectUnionStatement)

Example 2 with DMLSelectUnionStatement

use of com.alibaba.cobar.parser.ast.stmt.dml.DMLSelectUnionStatement in project cobar by alibaba.

the class MySQLDMLParser method buildUnionSelect.

/**
     * @return argument itself if there is no union
     */
protected DMLQueryStatement buildUnionSelect(DMLSelectStatement select) throws SQLSyntaxErrorException {
    if (lexer.token() != KW_UNION) {
        return select;
    }
    DMLSelectUnionStatement union = new DMLSelectUnionStatement(select);
    for (; lexer.token() == KW_UNION; ) {
        lexer.nextToken();
        boolean isAll = false;
        switch(lexer.token()) {
            case KW_ALL:
                isAll = true;
            case KW_DISTINCT:
                lexer.nextToken();
                break;
        }
        select = selectPrimary();
        union.addSelect(select, isAll);
    }
    union.setOrderBy(orderBy()).setLimit(limit());
    return union;
}
Also used : DMLSelectUnionStatement(com.alibaba.cobar.parser.ast.stmt.dml.DMLSelectUnionStatement)

Aggregations

DMLSelectUnionStatement (com.alibaba.cobar.parser.ast.stmt.dml.DMLSelectUnionStatement)2 MySQLLexer (com.alibaba.cobar.parser.recognizer.mysql.lexer.MySQLLexer)1