Search in sources :

Example 1 with OrderBy

use of com.alibaba.cobar.parser.ast.fragment.OrderBy in project cobar by alibaba.

the class MySQLDMLUpdateParser method update.

/**
     * nothing has been pre-consumed <code><pre>
     * 'UPDATE' 'LOW_PRIORITY'? 'IGNORE'? table_reference
     *   'SET' colName ('='|':=') (expr|'DEFAULT') (',' colName ('='|':=') (expr|'DEFAULT'))*
     *     ('WHERE' cond)?
     *     {singleTable}? => ('ORDER' 'BY' orderBy)?  ('LIMIT' count)?
     * </pre></code>
     */
public DMLUpdateStatement update() throws SQLSyntaxErrorException {
    match(KW_UPDATE);
    boolean lowPriority = false;
    boolean ignore = false;
    if (lexer.token() == KW_LOW_PRIORITY) {
        lexer.nextToken();
        lowPriority = true;
    }
    if (lexer.token() == KW_IGNORE) {
        lexer.nextToken();
        ignore = true;
    }
    TableReferences tableRefs = tableRefs();
    match(KW_SET);
    List<Pair<Identifier, Expression>> values;
    Identifier col = identifier();
    match(OP_EQUALS, OP_ASSIGN);
    Expression expr = exprParser.expression();
    if (lexer.token() == PUNC_COMMA) {
        values = new LinkedList<Pair<Identifier, Expression>>();
        values.add(new Pair<Identifier, Expression>(col, expr));
        for (; lexer.token() == PUNC_COMMA; ) {
            lexer.nextToken();
            col = identifier();
            match(OP_EQUALS, OP_ASSIGN);
            expr = exprParser.expression();
            values.add(new Pair<Identifier, Expression>(col, expr));
        }
    } else {
        values = new ArrayList<Pair<Identifier, Expression>>(1);
        values.add(new Pair<Identifier, Expression>(col, expr));
    }
    Expression where = null;
    if (lexer.token() == KW_WHERE) {
        lexer.nextToken();
        where = exprParser.expression();
    }
    OrderBy orderBy = null;
    Limit limit = null;
    if (tableRefs.isSingleTable()) {
        orderBy = orderBy();
        limit = limit();
    }
    return new DMLUpdateStatement(lowPriority, ignore, tableRefs, values, where, orderBy, limit);
}
Also used : OrderBy(com.alibaba.cobar.parser.ast.fragment.OrderBy) Identifier(com.alibaba.cobar.parser.ast.expression.primary.Identifier) Expression(com.alibaba.cobar.parser.ast.expression.Expression) DMLUpdateStatement(com.alibaba.cobar.parser.ast.stmt.dml.DMLUpdateStatement) Limit(com.alibaba.cobar.parser.ast.fragment.Limit) TableReferences(com.alibaba.cobar.parser.ast.fragment.tableref.TableReferences) Pair(com.alibaba.cobar.parser.util.Pair)

Example 2 with OrderBy

use of com.alibaba.cobar.parser.ast.fragment.OrderBy in project cobar by alibaba.

the class MySQLOutputASTVisitor method visit.

@Override
public void visit(DMLDeleteStatement node) {
    appendable.append("DELETE ");
    if (node.isLowPriority())
        appendable.append("LOW_PRIORITY ");
    if (node.isQuick())
        appendable.append("QUICK ");
    if (node.isIgnore())
        appendable.append("IGNORE ");
    TableReferences tableRefs = node.getTableRefs();
    if (tableRefs == null) {
        appendable.append("FROM ");
        node.getTableNames().get(0).accept(this);
    } else {
        printList(node.getTableNames());
        appendable.append(" FROM ");
        node.getTableRefs().accept(this);
    }
    Expression where = node.getWhereCondition();
    if (where != null) {
        appendable.append(" WHERE ");
        where.accept(this);
    }
    OrderBy orderBy = node.getOrderBy();
    if (orderBy != null) {
        appendable.append(' ');
        orderBy.accept(this);
    }
    Limit limit = node.getLimit();
    if (limit != null) {
        appendable.append(' ');
        limit.accept(this);
    }
}
Also used : OrderBy(com.alibaba.cobar.parser.ast.fragment.OrderBy) 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) Limit(com.alibaba.cobar.parser.ast.fragment.Limit) TableReferences(com.alibaba.cobar.parser.ast.fragment.tableref.TableReferences)

Example 3 with OrderBy

use of com.alibaba.cobar.parser.ast.fragment.OrderBy 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 4 with OrderBy

use of com.alibaba.cobar.parser.ast.fragment.OrderBy in project cobar by alibaba.

the class PartitionKeyVisitor method visit.

@Override
public void visit(DMLDeleteStatement node) {
    TableReference tr = node.getTableRefs();
    List<Identifier> tbs = node.getTableNames();
    if (tr == null) {
        Identifier table = tbs.get(0);
        tableAsTableFactor(table);
    } else {
        visitChild(1, verdictColumn, false, tr);
        for (Identifier tb : tbs) {
            if (tb instanceof Wildcard) {
                int trim = tb.trimParent(2, trimSchema);
                schemaTrimmed = schemaTrimmed || trim == Identifier.PARENT_TRIMED;
                customedSchema = customedSchema || trim == Identifier.PARENT_IGNORED;
            } else {
                int trim = tb.trimParent(1, trimSchema);
                schemaTrimmed = schemaTrimmed || trim == Identifier.PARENT_TRIMED;
                customedSchema = customedSchema || trim == Identifier.PARENT_IGNORED;
            }
        }
    }
    Expression where = node.getWhereCondition();
    visitChild(2, verdictColumn, false, where);
    if (tr == null) {
        OrderBy order = node.getOrderBy();
        visitChild(2, false, false, order);
    }
}
Also used : OrderBy(com.alibaba.cobar.parser.ast.fragment.OrderBy) TableReference(com.alibaba.cobar.parser.ast.fragment.tableref.TableReference) Identifier(com.alibaba.cobar.parser.ast.expression.primary.Identifier) Wildcard(com.alibaba.cobar.parser.ast.expression.primary.Wildcard) 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) ComparisionIsExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionIsExpression) LogicalOrExpression(com.alibaba.cobar.parser.ast.expression.logical.LogicalOrExpression) ReplacableExpression(com.alibaba.cobar.parser.ast.expression.ReplacableExpression) 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) 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) IndexHint(com.alibaba.cobar.parser.ast.fragment.tableref.IndexHint)

Example 5 with OrderBy

use of com.alibaba.cobar.parser.ast.fragment.OrderBy in project cobar by alibaba.

the class MySQLDMLParserTest method testOrderBy.

public void testOrderBy() throws SQLSyntaxErrorException {
    String sql = "order by c1 asc, c2 desc  , c3 ";
    MySQLLexer lexer = new MySQLLexer(sql);
    MySQLDMLParser parser = getDMLParser(lexer);
    OrderBy orderBy = parser.orderBy();
    String output = output2MySQL(orderBy, sql);
    ListUtil.isEquals(ListUtil.createList(new Pair<Expression, SortOrder>(new Identifier(null, "c1"), SortOrder.ASC), new Pair<Expression, SortOrder>(new Identifier(null, "c2"), SortOrder.DESC), new Pair<Expression, SortOrder>(new Identifier(null, "c3"), SortOrder.ASC)), orderBy.getOrderByList());
    Assert.assertEquals("ORDER BY c1, c2 DESC, c3", output);
    sql = "order by c1   ";
    lexer = new MySQLLexer(sql);
    parser = getDMLParser(lexer);
    orderBy = parser.orderBy();
    output = output2MySQL(orderBy, sql);
    ListUtil.isEquals(ListUtil.createList(new Pair<Expression, SortOrder>(new Identifier(null, "c1"), SortOrder.ASC)), orderBy.getOrderByList());
    Assert.assertEquals("ORDER BY c1", output);
    sql = "order by c1 asc  ";
    lexer = new MySQLLexer(sql);
    parser = getDMLParser(lexer);
    orderBy = parser.orderBy();
    output = output2MySQL(orderBy, sql);
    ListUtil.isEquals(ListUtil.createList(new Pair<Expression, SortOrder>(new Identifier(null, "c1"), SortOrder.ASC)), orderBy.getOrderByList());
    Assert.assertEquals("ORDER BY c1", output);
    sql = "order by c1 desc  ";
    lexer = new MySQLLexer(sql);
    parser = getDMLParser(lexer);
    orderBy = parser.orderBy();
    output = output2MySQL(orderBy, sql);
    ListUtil.isEquals(ListUtil.createList(new Pair<Expression, SortOrder>(new Identifier(null, "c1"), SortOrder.DESC)), orderBy.getOrderByList());
    Assert.assertEquals("ORDER BY c1 DESC", output);
}
Also used : OrderBy(com.alibaba.cobar.parser.ast.fragment.OrderBy) MySQLLexer(com.alibaba.cobar.parser.recognizer.mysql.lexer.MySQLLexer) Identifier(com.alibaba.cobar.parser.ast.expression.primary.Identifier) Pair(com.alibaba.cobar.parser.util.Pair)

Aggregations

OrderBy (com.alibaba.cobar.parser.ast.fragment.OrderBy)12 Expression (com.alibaba.cobar.parser.ast.expression.Expression)10 QueryExpression (com.alibaba.cobar.parser.ast.expression.misc.QueryExpression)7 Limit (com.alibaba.cobar.parser.ast.fragment.Limit)7 BinaryOperatorExpression (com.alibaba.cobar.parser.ast.expression.BinaryOperatorExpression)6 PolyadicOperatorExpression (com.alibaba.cobar.parser.ast.expression.PolyadicOperatorExpression)6 UnaryOperatorExpression (com.alibaba.cobar.parser.ast.expression.UnaryOperatorExpression)6 BetweenAndExpression (com.alibaba.cobar.parser.ast.expression.comparison.BetweenAndExpression)6 ComparisionEqualsExpression (com.alibaba.cobar.parser.ast.expression.comparison.ComparisionEqualsExpression)6 ComparisionIsExpression (com.alibaba.cobar.parser.ast.expression.comparison.ComparisionIsExpression)6 ComparisionNullSafeEqualsExpression (com.alibaba.cobar.parser.ast.expression.comparison.ComparisionNullSafeEqualsExpression)6 InExpression (com.alibaba.cobar.parser.ast.expression.comparison.InExpression)6 LogicalAndExpression (com.alibaba.cobar.parser.ast.expression.logical.LogicalAndExpression)6 LogicalOrExpression (com.alibaba.cobar.parser.ast.expression.logical.LogicalOrExpression)6 UserExpression (com.alibaba.cobar.parser.ast.expression.misc.UserExpression)6 CaseWhenOperatorExpression (com.alibaba.cobar.parser.ast.expression.primary.CaseWhenOperatorExpression)6 Identifier (com.alibaba.cobar.parser.ast.expression.primary.Identifier)6 MatchExpression (com.alibaba.cobar.parser.ast.expression.primary.MatchExpression)6 RowExpression (com.alibaba.cobar.parser.ast.expression.primary.RowExpression)6 FunctionExpression (com.alibaba.cobar.parser.ast.expression.primary.function.FunctionExpression)6