Search in sources :

Example 21 with Identifier

use of com.alibaba.cobar.parser.ast.expression.primary.Identifier in project cobar by alibaba.

the class MySQLDMLInsertParser method onDuplicateUpdate.

/**
     * @return null for not exist
     */
private List<Pair<Identifier, Expression>> onDuplicateUpdate() throws SQLSyntaxErrorException {
    if (lexer.token() != KW_ON) {
        return null;
    }
    lexer.nextToken();
    matchIdentifier("DUPLICATE");
    match(KW_KEY);
    match(KW_UPDATE);
    List<Pair<Identifier, Expression>> list;
    Identifier col = identifier();
    match(OP_EQUALS, OP_ASSIGN);
    Expression expr = exprParser.expression();
    if (lexer.token() == PUNC_COMMA) {
        list = new LinkedList<Pair<Identifier, Expression>>();
        list.add(new Pair<Identifier, Expression>(col, expr));
        for (; lexer.token() == PUNC_COMMA; ) {
            lexer.nextToken();
            col = identifier();
            match(OP_EQUALS, OP_ASSIGN);
            expr = exprParser.expression();
            list.add(new Pair<Identifier, Expression>(col, expr));
        }
        return list;
    }
    list = new ArrayList<Pair<Identifier, Expression>>(1);
    list.add(new Pair<Identifier, Expression>(col, expr));
    return list;
}
Also used : Identifier(com.alibaba.cobar.parser.ast.expression.primary.Identifier) Expression(com.alibaba.cobar.parser.ast.expression.Expression) RowExpression(com.alibaba.cobar.parser.ast.expression.primary.RowExpression) QueryExpression(com.alibaba.cobar.parser.ast.expression.misc.QueryExpression) Pair(com.alibaba.cobar.parser.util.Pair)

Example 22 with Identifier

use of com.alibaba.cobar.parser.ast.expression.primary.Identifier in project cobar by alibaba.

the class MySQLDMLParser method tableFactor.

private TableReference tableFactor() throws SQLSyntaxErrorException {
    String alias = null;
    switch(lexer.token()) {
        case PUNC_LEFT_PAREN:
            lexer.nextToken();
            Object ref = trsOrQuery();
            match(PUNC_RIGHT_PAREN);
            if (ref instanceof QueryExpression) {
                alias = as();
                return new SubqueryFactor((QueryExpression) ref, alias);
            }
            return (TableReferences) ref;
        case IDENTIFIER:
            Identifier table = identifier();
            alias = as();
            List<IndexHint> hintList = hintList();
            return new TableRefFactor(table, alias, hintList);
        default:
            throw err("unexpected token for tableFactor: " + lexer.token());
    }
}
Also used : Identifier(com.alibaba.cobar.parser.ast.expression.primary.Identifier) IndexHint(com.alibaba.cobar.parser.ast.fragment.tableref.IndexHint) SubqueryFactor(com.alibaba.cobar.parser.ast.fragment.tableref.SubqueryFactor) QueryExpression(com.alibaba.cobar.parser.ast.expression.misc.QueryExpression) TableRefFactor(com.alibaba.cobar.parser.ast.fragment.tableref.TableRefFactor) TableReferences(com.alibaba.cobar.parser.ast.fragment.tableref.TableReferences)

Example 23 with Identifier

use of com.alibaba.cobar.parser.ast.expression.primary.Identifier in project cobar by alibaba.

the class MySQLDMLReplaceParser method replace.

/**
     * nothing has been pre-consumed <code><pre>
     * 'REPLACE' ('LOW_PRIORITY' | 'DELAYED')? ('INTO')? tableName
     *  (  'SET' colName ('='|':=') (expr|'DEFAULT') (',' colName ('='|':=') (expr|'DEFAULT'))*
     *   | '(' (  colName (','colName)* ')' (  '(' 'SELECT' ... ')'
     *                                       | 'SELECT' ...
     *                                       |('VALUES'|'VALUE') value ( ',' value )*
     *                                      )
     *          | 'SELECT' ... ')'
     *         )
     *   | 'SELECT' ...
     *   |('VALUES'|'VALUE') value ( ',' value )*
     *  )
     * value := '(' (expr|'DEFAULT') ( ',' (expr|'DEFAULT'))* ')'
     * </pre></code>
     */
public DMLReplaceStatement replace() throws SQLSyntaxErrorException {
    match(KW_REPLACE);
    DMLReplaceStatement.ReplaceMode mode = DMLReplaceStatement.ReplaceMode.UNDEF;
    switch(lexer.token()) {
        case KW_LOW_PRIORITY:
            lexer.nextToken();
            mode = DMLReplaceStatement.ReplaceMode.LOW;
            break;
        case KW_DELAYED:
            lexer.nextToken();
            mode = DMLReplaceStatement.ReplaceMode.DELAY;
            break;
    }
    if (lexer.token() == KW_INTO) {
        lexer.nextToken();
    }
    Identifier table = identifier();
    List<Identifier> columnNameList;
    List<RowExpression> rowList;
    QueryExpression select;
    List<Expression> tempRowValue;
    switch(lexer.token()) {
        case KW_SET:
            lexer.nextToken();
            columnNameList = new LinkedList<Identifier>();
            tempRowValue = new LinkedList<Expression>();
            for (; ; lexer.nextToken()) {
                Identifier id = identifier();
                match(OP_EQUALS, OP_ASSIGN);
                Expression expr = exprParser.expression();
                columnNameList.add(id);
                tempRowValue.add(expr);
                if (lexer.token() != PUNC_COMMA) {
                    break;
                }
            }
            rowList = new ArrayList<RowExpression>(1);
            rowList.add(new RowExpression(tempRowValue));
            return new DMLReplaceStatement(mode, table, columnNameList, rowList);
        case IDENTIFIER:
            if (!"VALUE".equals(lexer.stringValueUppercase())) {
                break;
            }
        case KW_VALUES:
            lexer.nextToken();
            columnNameList = null;
            rowList = rowList();
            return new DMLReplaceStatement(mode, table, columnNameList, rowList);
        case KW_SELECT:
            columnNameList = null;
            select = select();
            return new DMLReplaceStatement(mode, table, columnNameList, select);
        case PUNC_LEFT_PAREN:
            switch(lexer.nextToken()) {
                case PUNC_LEFT_PAREN:
                case KW_SELECT:
                    columnNameList = null;
                    select = selectPrimary();
                    match(PUNC_RIGHT_PAREN);
                    return new DMLReplaceStatement(mode, table, columnNameList, select);
            }
            columnNameList = idList();
            match(PUNC_RIGHT_PAREN);
            switch(lexer.token()) {
                case PUNC_LEFT_PAREN:
                case KW_SELECT:
                    select = selectPrimary();
                    return new DMLReplaceStatement(mode, table, columnNameList, select);
                case KW_VALUES:
                    lexer.nextToken();
                    break;
                default:
                    matchIdentifier("VALUE");
            }
            rowList = rowList();
            return new DMLReplaceStatement(mode, table, columnNameList, rowList);
    }
    throw err("unexpected token for replace: " + lexer.token());
}
Also used : DMLReplaceStatement(com.alibaba.cobar.parser.ast.stmt.dml.DMLReplaceStatement) Identifier(com.alibaba.cobar.parser.ast.expression.primary.Identifier) Expression(com.alibaba.cobar.parser.ast.expression.Expression) RowExpression(com.alibaba.cobar.parser.ast.expression.primary.RowExpression) QueryExpression(com.alibaba.cobar.parser.ast.expression.misc.QueryExpression) RowExpression(com.alibaba.cobar.parser.ast.expression.primary.RowExpression) QueryExpression(com.alibaba.cobar.parser.ast.expression.misc.QueryExpression)

Example 24 with Identifier

use of com.alibaba.cobar.parser.ast.expression.primary.Identifier in project cobar by alibaba.

the class PartitionKeyVisitor method visit.

@Override
public void visit(ComparisionNullSafeEqualsExpression node) {
    Expression left = node.getLeftOprand();
    Expression right = node.getRightOprand();
    visitChild(2, false, false, left, right);
    if (verdictColumn) {
        if (left instanceof Identifier) {
            comparisionEquals((Identifier) left, right.evaluation(evaluationParameter), true, node);
        } else if (right instanceof Identifier) {
            comparisionEquals((Identifier) right, left.evaluation(evaluationParameter), true, node);
        }
    }
}
Also used : Identifier(com.alibaba.cobar.parser.ast.expression.primary.Identifier) 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)

Example 25 with Identifier

use of com.alibaba.cobar.parser.ast.expression.primary.Identifier in project cobar by alibaba.

the class PartitionKeyVisitor method visit.

@Override
public void visit(ComparisionEqualsExpression node) {
    Expression left = node.getLeftOprand();
    Expression right = node.getRightOprand();
    visitChild(2, false, false, left, right);
    if (verdictColumn) {
        if (left instanceof Identifier) {
            comparisionEquals((Identifier) left, right.evaluation(evaluationParameter), false, node);
        } else if (right instanceof Identifier) {
            comparisionEquals((Identifier) right, left.evaluation(evaluationParameter), false, node);
        }
    }
}
Also used : Identifier(com.alibaba.cobar.parser.ast.expression.primary.Identifier) 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)

Aggregations

Identifier (com.alibaba.cobar.parser.ast.expression.primary.Identifier)50 Expression (com.alibaba.cobar.parser.ast.expression.Expression)28 QueryExpression (com.alibaba.cobar.parser.ast.expression.misc.QueryExpression)18 RowExpression (com.alibaba.cobar.parser.ast.expression.primary.RowExpression)18 InExpression (com.alibaba.cobar.parser.ast.expression.comparison.InExpression)14 BetweenAndExpression (com.alibaba.cobar.parser.ast.expression.comparison.BetweenAndExpression)13 ComparisionEqualsExpression (com.alibaba.cobar.parser.ast.expression.comparison.ComparisionEqualsExpression)13 ComparisionIsExpression (com.alibaba.cobar.parser.ast.expression.comparison.ComparisionIsExpression)13 ComparisionNullSafeEqualsExpression (com.alibaba.cobar.parser.ast.expression.comparison.ComparisionNullSafeEqualsExpression)13 LogicalAndExpression (com.alibaba.cobar.parser.ast.expression.logical.LogicalAndExpression)13 LogicalOrExpression (com.alibaba.cobar.parser.ast.expression.logical.LogicalOrExpression)13 UserExpression (com.alibaba.cobar.parser.ast.expression.misc.UserExpression)13 CaseWhenOperatorExpression (com.alibaba.cobar.parser.ast.expression.primary.CaseWhenOperatorExpression)13 MatchExpression (com.alibaba.cobar.parser.ast.expression.primary.MatchExpression)13 FunctionExpression (com.alibaba.cobar.parser.ast.expression.primary.function.FunctionExpression)13 LikeExpression (com.alibaba.cobar.parser.ast.expression.string.LikeExpression)13 CollateExpression (com.alibaba.cobar.parser.ast.expression.type.CollateExpression)13 Pair (com.alibaba.cobar.parser.util.Pair)12 BinaryOperatorExpression (com.alibaba.cobar.parser.ast.expression.BinaryOperatorExpression)11 PolyadicOperatorExpression (com.alibaba.cobar.parser.ast.expression.PolyadicOperatorExpression)11