Search in sources :

Example 1 with DMLSelectStatement

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

the class MySQLOutputASTVisitor method visit.

@Override
public void visit(DMLSelectStatement node) {
    appendable.append("SELECT ");
    final DMLSelectStatement.SelectOption option = node.getOption();
    switch(option.resultDup) {
        case ALL:
            break;
        case DISTINCT:
            appendable.append("DISTINCT ");
            break;
        case DISTINCTROW:
            appendable.append("DISTINCTROW ");
            break;
        default:
            throw new IllegalArgumentException("unknown option for SELECT: " + option);
    }
    if (option.highPriority) {
        appendable.append("HIGH_PRIORITY ");
    }
    if (option.straightJoin) {
        appendable.append("STRAIGHT_JOIN ");
    }
    switch(option.resultSize) {
        case SQL_BIG_RESULT:
            appendable.append("SQL_BIG_RESULT ");
            break;
        case SQL_SMALL_RESULT:
            appendable.append("SQL_SMALL_RESULT ");
            break;
        case UNDEF:
            break;
        default:
            throw new IllegalArgumentException("unknown option for SELECT: " + option);
    }
    if (option.sqlBufferResult) {
        appendable.append("SQL_BUFFER_RESULT ");
    }
    switch(option.queryCache) {
        case SQL_CACHE:
            appendable.append("SQL_CACHE ");
            break;
        case SQL_NO_CACHE:
            appendable.append("SQL_NO_CACHE ");
            break;
        case UNDEF:
            break;
        default:
            throw new IllegalArgumentException("unknown option for SELECT: " + option);
    }
    if (option.sqlCalcFoundRows) {
        appendable.append("SQL_CALC_FOUND_ROWS ");
    }
    boolean isFst = true;
    List<Pair<Expression, String>> exprList = node.getSelectExprList();
    for (Pair<Expression, String> p : exprList) {
        if (isFst)
            isFst = false;
        else
            appendable.append(", ");
        p.getKey().accept(this);
        String alias = p.getValue();
        if (alias != null) {
            appendable.append(" AS ").append(alias);
        }
    }
    TableReferences from = node.getTables();
    if (from != null) {
        appendable.append(" FROM ");
        from.accept(this);
    }
    Expression where = node.getWhere();
    if (where != null) {
        appendable.append(" WHERE ");
        where.accept(this);
    }
    GroupBy group = node.getGroup();
    if (group != null) {
        appendable.append(' ');
        group.accept(this);
    }
    Expression having = node.getHaving();
    if (having != null) {
        appendable.append(" HAVING ");
        having.accept(this);
    }
    OrderBy order = node.getOrder();
    if (order != null) {
        appendable.append(' ');
        order.accept(this);
    }
    Limit limit = node.getLimit();
    if (limit != null) {
        appendable.append(' ');
        limit.accept(this);
    }
    switch(option.lockMode) {
        case FOR_UPDATE:
            appendable.append(" FOR UPDATE");
            break;
        case LOCK_IN_SHARE_MODE:
            appendable.append(" LOCK IN SHARE MODE");
            break;
        case UNDEF:
            break;
        default:
            throw new IllegalArgumentException("unknown option for SELECT: " + option);
    }
}
Also used : OrderBy(com.alibaba.cobar.parser.ast.fragment.OrderBy) GroupBy(com.alibaba.cobar.parser.ast.fragment.GroupBy) LiteralString(com.alibaba.cobar.parser.ast.expression.primary.literal.LiteralString) TableReferences(com.alibaba.cobar.parser.ast.fragment.tableref.TableReferences) BetweenAndExpression(com.alibaba.cobar.parser.ast.expression.comparison.BetweenAndExpression) ComparisionEqualsExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionEqualsExpression) QueryExpression(com.alibaba.cobar.parser.ast.expression.misc.QueryExpression) InExpression(com.alibaba.cobar.parser.ast.expression.comparison.InExpression) FunctionExpression(com.alibaba.cobar.parser.ast.expression.primary.function.FunctionExpression) MatchExpression(com.alibaba.cobar.parser.ast.expression.primary.MatchExpression) VariableExpression(com.alibaba.cobar.parser.ast.expression.primary.VariableExpression) ComparisionIsExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionIsExpression) LogicalOrExpression(com.alibaba.cobar.parser.ast.expression.logical.LogicalOrExpression) LogicalAndExpression(com.alibaba.cobar.parser.ast.expression.logical.LogicalAndExpression) UnaryOperatorExpression(com.alibaba.cobar.parser.ast.expression.UnaryOperatorExpression) ComparisionNullSafeEqualsExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionNullSafeEqualsExpression) LikeExpression(com.alibaba.cobar.parser.ast.expression.string.LikeExpression) TernaryOperatorExpression(com.alibaba.cobar.parser.ast.expression.TernaryOperatorExpression) CollateExpression(com.alibaba.cobar.parser.ast.expression.type.CollateExpression) Expression(com.alibaba.cobar.parser.ast.expression.Expression) CaseWhenOperatorExpression(com.alibaba.cobar.parser.ast.expression.primary.CaseWhenOperatorExpression) UserExpression(com.alibaba.cobar.parser.ast.expression.misc.UserExpression) PolyadicOperatorExpression(com.alibaba.cobar.parser.ast.expression.PolyadicOperatorExpression) BinaryOperatorExpression(com.alibaba.cobar.parser.ast.expression.BinaryOperatorExpression) RowExpression(com.alibaba.cobar.parser.ast.expression.primary.RowExpression) DMLSelectStatement(com.alibaba.cobar.parser.ast.stmt.dml.DMLSelectStatement) Limit(com.alibaba.cobar.parser.ast.fragment.Limit) Pair(com.alibaba.cobar.parser.util.Pair)

Example 2 with DMLSelectStatement

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

the class MySQLDMLParser method selectPrimary.

protected DMLSelectStatement selectPrimary() throws SQLSyntaxErrorException {
    switch(lexer.token()) {
        case KW_SELECT:
            return select();
        case PUNC_LEFT_PAREN:
            lexer.nextToken();
            DMLSelectStatement select = selectPrimary();
            match(PUNC_RIGHT_PAREN);
            return select;
        default:
            throw err("unexpected token: " + lexer.token());
    }
}
Also used : DMLSelectStatement(com.alibaba.cobar.parser.ast.stmt.dml.DMLSelectStatement)

Example 3 with DMLSelectStatement

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

the class MySQLDMLSelectParserTest method testSelect.

public void testSelect() throws SQLSyntaxErrorException {
    String sql = "SELect t1.id , t2.* from t1, test.t2 where test.t1.id=1 and t1.id=test.t2.id";
    MySQLLexer lexer = new MySQLLexer(sql);
    MySQLDMLSelectParser parser = new MySQLDMLSelectParser(lexer, new MySQLExprParser(lexer));
    DMLSelectStatement select = parser.select();
    Assert.assertNotNull(select);
    String output = output2MySQL(select, sql);
    Assert.assertEquals("SELECT t1.id, t2.* FROM t1, test.t2 WHERE test.t1.id = 1 AND t1.id = test.t2.id", output);
    sql = "select * from  offer  a  straight_join wp_image b use key for join(t1,t2) on a.member_id=b.member_id inner join product_visit c where a.member_id=c.member_id and c.member_id='abc' ";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDMLSelectParser(lexer, new MySQLExprParser(lexer));
    select = parser.select();
    Assert.assertNotNull(select);
    output = output2MySQL(select, sql);
    Assert.assertEquals("SELECT * FROM offer AS A STRAIGHT_JOIN wp_image AS B USE KEY FOR JOIN (t1, t2) ON a.member_id = b.member_id INNER JOIN product_visit AS C WHERE a.member_id = c.member_id AND c.member_id = 'abc'", output);
    sql = "SELect all tb1.id,tb2.id from tb1,tb2 where tb1.id2=tb2.id2";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDMLSelectParser(lexer, new MySQLExprParser(lexer));
    select = parser.select();
    Assert.assertNotNull(select);
    output = output2MySQL(select, sql);
    Assert.assertEquals("SELECT tb1.id, tb2.id FROM tb1, tb2 WHERE tb1.id2 = tb2.id2", output);
    sql = "SELect distinct high_priority tb1.id,tb2.id from tb1,tb2 where tb1.id2=tb2.id2";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDMLSelectParser(lexer, new MySQLExprParser(lexer));
    select = parser.select();
    Assert.assertNotNull(select);
    output = output2MySQL(select, sql);
    Assert.assertEquals("SELECT DISTINCT HIGH_PRIORITY tb1.id, tb2.id FROM tb1, tb2 WHERE tb1.id2 = tb2.id2", output);
    sql = "SELect distinctrow high_priority sql_small_result tb1.id,tb2.id " + "from tb1,tb2 where tb1.id2=tb2.id2";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDMLSelectParser(lexer, new MySQLExprParser(lexer));
    select = parser.select();
    Assert.assertNotNull(select);
    output = output2MySQL(select, sql);
    Assert.assertEquals("SELECT DISTINCTROW HIGH_PRIORITY SQL_SMALL_RESULT tb1.id, tb2.id FROM tb1, tb2 WHERE tb1.id2 = tb2.id2", output);
    sql = "SELect  sql_cache id1,id2 from tb1,tb2 where tb1.id1=tb2.id1 ";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDMLSelectParser(lexer, new MySQLExprParser(lexer));
    select = parser.select();
    Assert.assertNotNull(select);
    output = output2MySQL(select, sql);
    Assert.assertEquals("SELECT SQL_CACHE id1, id2 FROM tb1, tb2 WHERE tb1.id1 = tb2.id1", output);
    sql = "SELect  sql_cache id1,max(id2) from tb1 group by id1 having id1>10 order by id3 desc";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDMLSelectParser(lexer, new MySQLExprParser(lexer));
    select = parser.select();
    Assert.assertNotNull(select);
    output = output2MySQL(select, sql);
    Assert.assertEquals("SELECT SQL_CACHE id1, MAX(id2) FROM tb1 GROUP BY id1 HAVING id1 > 10 ORDER BY id3 DESC", output);
    sql = "SELect  SQL_BUFFER_RESULT tb1.id1,id2 from tb1";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDMLSelectParser(lexer, new MySQLExprParser(lexer));
    select = parser.select();
    Assert.assertNotNull(select);
    output = output2MySQL(select, sql);
    Assert.assertEquals("SELECT SQL_BUFFER_RESULT tb1.id1, id2 FROM tb1", output);
    sql = "SELect  SQL_no_cache tb1.id1,id2 from tb1";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDMLSelectParser(lexer, new MySQLExprParser(lexer));
    select = parser.select();
    Assert.assertNotNull(select);
    output = output2MySQL(select, sql);
    Assert.assertEquals("SELECT SQL_NO_CACHE tb1.id1, id2 FROM tb1", output);
    sql = "SELect  SQL_CALC_FOUND_ROWS tb1.id1,id2 from tb1";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDMLSelectParser(lexer, new MySQLExprParser(lexer));
    select = parser.select();
    Assert.assertNotNull(select);
    output = output2MySQL(select, sql);
    Assert.assertEquals("SELECT SQL_CALC_FOUND_ROWS tb1.id1, id2 FROM tb1", output);
    sql = "SELect 1+1 ";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDMLSelectParser(lexer, new MySQLExprParser(lexer));
    select = parser.select();
    Assert.assertNotNull(select);
    output = output2MySQL(select, sql);
    Assert.assertEquals("SELECT 1 + 1", output);
    sql = "SELect t1.* from tb ";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDMLSelectParser(lexer, new MySQLExprParser(lexer));
    select = parser.select();
    Assert.assertNotNull(select);
    output = output2MySQL(select, sql);
    Assert.assertEquals("SELECT t1.* FROM tb", output);
    sql = "SELect distinct high_priority straight_join sql_big_result sql_cache tb1.id,tb2.id " + "from tb1,tb2 where tb1.id2=tb2.id2";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDMLSelectParser(lexer, new MySQLExprParser(lexer));
    select = parser.select();
    Assert.assertNotNull(select);
    output = output2MySQL(select, sql);
    Assert.assertEquals("SELECT DISTINCT HIGH_PRIORITY STRAIGHT_JOIN SQL_BIG_RESULT" + " SQL_CACHE tb1.id, tb2.id FROM tb1, tb2 WHERE tb1.id2 = tb2.id2", output);
    sql = "SELect distinct id1,id2 from tb1,tb2 where tb1.id1=tb2.id2 for update";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDMLSelectParser(lexer, new MySQLExprParser(lexer));
    select = parser.select();
    Assert.assertNotNull(select);
    output = output2MySQL(select, sql);
    Assert.assertEquals("SELECT DISTINCT id1, id2 FROM tb1, tb2 WHERE tb1.id1 = tb2.id2 FOR UPDATE", output);
    sql = "SELect distinct id1,id2 from tb1,tb2 where tb1.id1=tb2.id2 lock in share mode";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDMLSelectParser(lexer, new MySQLExprParser(lexer));
    select = parser.select();
    Assert.assertNotNull(select);
    output = output2MySQL(select, sql);
    Assert.assertEquals("SELECT DISTINCT id1, id2 FROM tb1, tb2 WHERE tb1.id1 = tb2.id2 LOCK IN SHARE MODE", output);
}
Also used : MySQLLexer(com.alibaba.cobar.parser.recognizer.mysql.lexer.MySQLLexer) DMLSelectStatement(com.alibaba.cobar.parser.ast.stmt.dml.DMLSelectStatement)

Example 4 with DMLSelectStatement

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

the class MySQLDMLParser method trsOrQuery.

/**
     * @return type of {@link QueryExpression} or {@link TableReferences}
     */
private Object trsOrQuery() throws SQLSyntaxErrorException {
    Object ref;
    switch(lexer.token()) {
        case KW_SELECT:
            DMLSelectStatement select = selectPrimary();
            return buildUnionSelect(select);
        case PUNC_LEFT_PAREN:
            lexer.nextToken();
            ref = trsOrQuery();
            match(PUNC_RIGHT_PAREN);
            if (ref instanceof QueryExpression) {
                if (ref instanceof DMLSelectStatement) {
                    QueryExpression rst = buildUnionSelect((DMLSelectStatement) ref);
                    if (rst != ref) {
                        return rst;
                    }
                }
                String alias = as();
                if (alias != null) {
                    ref = new SubqueryFactor((QueryExpression) ref, alias);
                } else {
                    return ref;
                }
            }
            // ---- build factor complete---------------
            ref = buildTableReference((TableReference) ref);
            // ---- build ref complete---------------
            break;
        default:
            ref = tableReference();
    }
    List<TableReference> list;
    if (lexer.token() == PUNC_COMMA) {
        list = new LinkedList<TableReference>();
        list.add((TableReference) ref);
        for (; lexer.token() == PUNC_COMMA; ) {
            lexer.nextToken();
            ref = tableReference();
            list.add((TableReference) ref);
        }
        return new TableReferences(list);
    }
    list = new ArrayList<TableReference>(1);
    list.add((TableReference) ref);
    return new TableReferences(list);
}
Also used : TableReference(com.alibaba.cobar.parser.ast.fragment.tableref.TableReference) DMLSelectStatement(com.alibaba.cobar.parser.ast.stmt.dml.DMLSelectStatement) SubqueryFactor(com.alibaba.cobar.parser.ast.fragment.tableref.SubqueryFactor) QueryExpression(com.alibaba.cobar.parser.ast.expression.misc.QueryExpression) TableReferences(com.alibaba.cobar.parser.ast.fragment.tableref.TableReferences)

Example 5 with DMLSelectStatement

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

the class MySQLDMLSelectParser method selectUnion.

/**
     * first token is either {@link MySQLToken#KW_SELECT} or
     * {@link MySQLToken#PUNC_LEFT_PAREN} which has been scanned but not yet
     * consumed
     * 
     * @return {@link DMLSelectStatement} or {@link DMLSelectUnionStatement}
     */
public DMLQueryStatement selectUnion() throws SQLSyntaxErrorException {
    DMLSelectStatement select = selectPrimary();
    DMLQueryStatement query = buildUnionSelect(select);
    return query;
}
Also used : DMLQueryStatement(com.alibaba.cobar.parser.ast.stmt.dml.DMLQueryStatement) DMLSelectStatement(com.alibaba.cobar.parser.ast.stmt.dml.DMLSelectStatement)

Aggregations

DMLSelectStatement (com.alibaba.cobar.parser.ast.stmt.dml.DMLSelectStatement)9 Limit (com.alibaba.cobar.parser.ast.fragment.Limit)3 OrderBy (com.alibaba.cobar.parser.ast.fragment.OrderBy)3 TableReferences (com.alibaba.cobar.parser.ast.fragment.tableref.TableReferences)3 Expression (com.alibaba.cobar.parser.ast.expression.Expression)2 QueryExpression (com.alibaba.cobar.parser.ast.expression.misc.QueryExpression)2 GroupBy (com.alibaba.cobar.parser.ast.fragment.GroupBy)2 TableReference (com.alibaba.cobar.parser.ast.fragment.tableref.TableReference)2 MySQLLexer (com.alibaba.cobar.parser.recognizer.mysql.lexer.MySQLLexer)2 Pair (com.alibaba.cobar.parser.util.Pair)2 BinaryOperatorExpression (com.alibaba.cobar.parser.ast.expression.BinaryOperatorExpression)1 PolyadicOperatorExpression (com.alibaba.cobar.parser.ast.expression.PolyadicOperatorExpression)1 TernaryOperatorExpression (com.alibaba.cobar.parser.ast.expression.TernaryOperatorExpression)1 UnaryOperatorExpression (com.alibaba.cobar.parser.ast.expression.UnaryOperatorExpression)1 BetweenAndExpression (com.alibaba.cobar.parser.ast.expression.comparison.BetweenAndExpression)1 ComparisionEqualsExpression (com.alibaba.cobar.parser.ast.expression.comparison.ComparisionEqualsExpression)1 ComparisionIsExpression (com.alibaba.cobar.parser.ast.expression.comparison.ComparisionIsExpression)1 ComparisionNullSafeEqualsExpression (com.alibaba.cobar.parser.ast.expression.comparison.ComparisionNullSafeEqualsExpression)1 InExpression (com.alibaba.cobar.parser.ast.expression.comparison.InExpression)1 LogicalAndExpression (com.alibaba.cobar.parser.ast.expression.logical.LogicalAndExpression)1