Search in sources :

Example 6 with QueryExpression

use of com.alibaba.cobar.parser.ast.expression.misc.QueryExpression in project cobar by alibaba.

the class MySQLExprParser method anyAllExpression.

private Expression anyAllExpression() throws SQLSyntaxErrorException {
    QueryExpression subquery = null;
    switch(lexer.token()) {
        case KW_ALL:
            lexer.nextToken();
            match(PUNC_LEFT_PAREN);
            subquery = subQuery();
            match(PUNC_RIGHT_PAREN);
            return new SubqueryAllExpression(subquery).setCacheEvalRst(cacheEvalRst);
        default:
            int matchIndex = equalsIdentifier("SOME", "ANY");
            if (matchIndex < 0) {
                return bitOrExpression(null, null);
            }
            String consumed = lexer.stringValue();
            String consumedUp = lexer.stringValueUppercase();
            if (lexer.nextToken() == PUNC_LEFT_PAREN) {
                lexer.nextToken();
                subquery = subQuery();
                match(PUNC_RIGHT_PAREN);
                return new SubqueryAnyExpression(subquery).setCacheEvalRst(cacheEvalRst);
            }
            return bitOrExpression(consumed, consumedUp);
    }
}
Also used : SubqueryAllExpression(com.alibaba.cobar.parser.ast.expression.misc.SubqueryAllExpression) SubqueryAnyExpression(com.alibaba.cobar.parser.ast.expression.misc.SubqueryAnyExpression) LiteralString(com.alibaba.cobar.parser.ast.expression.primary.literal.LiteralString) QueryExpression(com.alibaba.cobar.parser.ast.expression.misc.QueryExpression)

Example 7 with QueryExpression

use of com.alibaba.cobar.parser.ast.expression.misc.QueryExpression in project cobar by alibaba.

the class MySQLDMLInsertParser method insert.

/**
     * nothing has been pre-consumed <code><pre>
     * 'INSERT' ('LOW_PRIORITY'|'DELAYED'|'HIGH_PRIORITY')? 'IGNORE'? 'INTO'? tbname 
     *  (  'SET' colName ('='|':=') (expr|'DEFAULT') (',' colName ('='|':=') (expr|'DEFAULT'))*
     *   | '(' (  colName (',' colName)* ')' ( ('VALUES'|'VALUE') value (',' value)*
     *                                        | '(' 'SELECT' ... ')'
     *                                        | 'SELECT' ...  
     *                                       )
     *          | 'SELECT' ... ')' 
     *         )
     *   |('VALUES'|'VALUE') value  ( ',' value )*
     *   | 'SELECT' ...
     *  )
     * ( 'ON' 'DUPLICATE' 'KEY' 'UPDATE' colName ('='|':=') expr ( ',' colName ('='|':=') expr)* )?
     * 
     * value := '(' (expr|'DEFAULT') ( ',' (expr|'DEFAULT'))* ')'
     * </pre></code>
     */
public DMLInsertStatement insert() throws SQLSyntaxErrorException {
    match(KW_INSERT);
    DMLInsertStatement.InsertMode mode = DMLInsertStatement.InsertMode.UNDEF;
    boolean ignore = false;
    switch(lexer.token()) {
        case KW_LOW_PRIORITY:
            lexer.nextToken();
            mode = DMLInsertStatement.InsertMode.LOW;
            break;
        case KW_DELAYED:
            lexer.nextToken();
            mode = DMLInsertStatement.InsertMode.DELAY;
            break;
        case KW_HIGH_PRIORITY:
            lexer.nextToken();
            mode = DMLInsertStatement.InsertMode.HIGH;
            break;
    }
    if (lexer.token() == KW_IGNORE) {
        ignore = true;
        lexer.nextToken();
    }
    if (lexer.token() == KW_INTO) {
        lexer.nextToken();
    }
    Identifier table = identifier();
    List<Pair<Identifier, Expression>> dupUpdate;
    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));
            dupUpdate = onDuplicateUpdate();
            return new DMLInsertStatement(mode, ignore, table, columnNameList, rowList, dupUpdate);
        case IDENTIFIER:
            if (!"VALUE".equals(lexer.stringValueUppercase())) {
                break;
            }
        case KW_VALUES:
            lexer.nextToken();
            columnNameList = null;
            rowList = rowList();
            dupUpdate = onDuplicateUpdate();
            return new DMLInsertStatement(mode, ignore, table, columnNameList, rowList, dupUpdate);
        case KW_SELECT:
            columnNameList = null;
            select = select();
            dupUpdate = onDuplicateUpdate();
            return new DMLInsertStatement(mode, ignore, table, columnNameList, select, dupUpdate);
        case PUNC_LEFT_PAREN:
            switch(lexer.nextToken()) {
                case PUNC_LEFT_PAREN:
                case KW_SELECT:
                    columnNameList = null;
                    select = selectPrimary();
                    match(PUNC_RIGHT_PAREN);
                    dupUpdate = onDuplicateUpdate();
                    return new DMLInsertStatement(mode, ignore, table, columnNameList, select, dupUpdate);
            }
            columnNameList = idList();
            match(PUNC_RIGHT_PAREN);
            switch(lexer.token()) {
                case PUNC_LEFT_PAREN:
                case KW_SELECT:
                    select = selectPrimary();
                    dupUpdate = onDuplicateUpdate();
                    return new DMLInsertStatement(mode, ignore, table, columnNameList, select, dupUpdate);
                case KW_VALUES:
                    lexer.nextToken();
                    break;
                default:
                    matchIdentifier("VALUE");
            }
            rowList = rowList();
            dupUpdate = onDuplicateUpdate();
            return new DMLInsertStatement(mode, ignore, table, columnNameList, rowList, dupUpdate);
    }
    throw err("unexpected token for insert: " + lexer.token());
}
Also used : RowExpression(com.alibaba.cobar.parser.ast.expression.primary.RowExpression) DMLInsertStatement(com.alibaba.cobar.parser.ast.stmt.dml.DMLInsertStatement) 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) QueryExpression(com.alibaba.cobar.parser.ast.expression.misc.QueryExpression) Pair(com.alibaba.cobar.parser.util.Pair)

Example 8 with QueryExpression

use of com.alibaba.cobar.parser.ast.expression.misc.QueryExpression 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 9 with QueryExpression

use of com.alibaba.cobar.parser.ast.expression.misc.QueryExpression in project cobar by alibaba.

the class MySQLExprParser method rightOprandOfIn.

/**
     * @return {@link QueryExpression} or {@link InExpressionList}
     */
private Expression rightOprandOfIn() throws SQLSyntaxErrorException {
    match(PUNC_LEFT_PAREN);
    if (KW_SELECT == lexer.token()) {
        QueryExpression subq = subQuery();
        match(PUNC_RIGHT_PAREN);
        return subq;
    }
    return new InExpressionList(expressionList(new LinkedList<Expression>())).setCacheEvalRst(cacheEvalRst);
}
Also used : InExpressionList(com.alibaba.cobar.parser.ast.expression.misc.InExpressionList) SubqueryAllExpression(com.alibaba.cobar.parser.ast.expression.misc.SubqueryAllExpression) MinusExpression(com.alibaba.cobar.parser.ast.expression.arithmeic.MinusExpression) BetweenAndExpression(com.alibaba.cobar.parser.ast.expression.comparison.BetweenAndExpression) BitAndExpression(com.alibaba.cobar.parser.ast.expression.bit.BitAndExpression) ComparisionEqualsExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionEqualsExpression) NegativeValueExpression(com.alibaba.cobar.parser.ast.expression.logical.NegativeValueExpression) QueryExpression(com.alibaba.cobar.parser.ast.expression.misc.QueryExpression) CastBinaryExpression(com.alibaba.cobar.parser.ast.expression.type.CastBinaryExpression) InExpression(com.alibaba.cobar.parser.ast.expression.comparison.InExpression) BitInvertExpression(com.alibaba.cobar.parser.ast.expression.bit.BitInvertExpression) BitXORExpression(com.alibaba.cobar.parser.ast.expression.bit.BitXORExpression) FunctionExpression(com.alibaba.cobar.parser.ast.expression.primary.function.FunctionExpression) BitShiftExpression(com.alibaba.cobar.parser.ast.expression.bit.BitShiftExpression) BitOrExpression(com.alibaba.cobar.parser.ast.expression.bit.BitOrExpression) ComparisionGreaterThanOrEqualsExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionGreaterThanOrEqualsExpression) 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) ComparisionGreaterThanExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionGreaterThanExpression) ComparisionLessOrGreaterThanExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionLessOrGreaterThanExpression) SubqueryAnyExpression(com.alibaba.cobar.parser.ast.expression.misc.SubqueryAnyExpression) RegexpExpression(com.alibaba.cobar.parser.ast.expression.string.RegexpExpression) LogicalAndExpression(com.alibaba.cobar.parser.ast.expression.logical.LogicalAndExpression) ArithmeticMultiplyExpression(com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticMultiplyExpression) ComparisionLessThanOrEqualsExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionLessThanOrEqualsExpression) ComparisionNullSafeEqualsExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionNullSafeEqualsExpression) LikeExpression(com.alibaba.cobar.parser.ast.expression.string.LikeExpression) ArithmeticSubtractExpression(com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticSubtractExpression) SoundsLikeExpression(com.alibaba.cobar.parser.ast.expression.string.SoundsLikeExpression) ArithmeticIntegerDivideExpression(com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticIntegerDivideExpression) CollateExpression(com.alibaba.cobar.parser.ast.expression.type.CollateExpression) LogicalNotExpression(com.alibaba.cobar.parser.ast.expression.logical.LogicalNotExpression) Expression(com.alibaba.cobar.parser.ast.expression.Expression) AssignmentExpression(com.alibaba.cobar.parser.ast.expression.misc.AssignmentExpression) CaseWhenOperatorExpression(com.alibaba.cobar.parser.ast.expression.primary.CaseWhenOperatorExpression) LogicalXORExpression(com.alibaba.cobar.parser.ast.expression.logical.LogicalXORExpression) UserExpression(com.alibaba.cobar.parser.ast.expression.misc.UserExpression) ArithmeticAddExpression(com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticAddExpression) ComparisionNotEqualsExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionNotEqualsExpression) RowExpression(com.alibaba.cobar.parser.ast.expression.primary.RowExpression) ComparisionLessThanExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionLessThanExpression) ArithmeticModExpression(com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticModExpression) ArithmeticDivideExpression(com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticDivideExpression) QueryExpression(com.alibaba.cobar.parser.ast.expression.misc.QueryExpression)

Example 10 with QueryExpression

use of com.alibaba.cobar.parser.ast.expression.misc.QueryExpression in project cobar by alibaba.

the class MySQLOutputASTVisitor method visit.

@Override
public void visit(DMLReplaceStatement node) {
    appendable.append("REPLACE ");
    switch(node.getMode()) {
        case DELAY:
            appendable.append("DELAYED ");
            break;
        case LOW:
            appendable.append("LOW_PRIORITY ");
            break;
        case UNDEF:
            break;
        default:
            throw new IllegalArgumentException("unknown mode for INSERT: " + node.getMode());
    }
    appendable.append("INTO ");
    node.getTable().accept(this);
    appendable.append(' ');
    List<Identifier> cols = node.getColumnNameList();
    if (cols != null && !cols.isEmpty()) {
        appendable.append('(');
        printList(cols);
        appendable.append(") ");
    }
    QueryExpression select = node.getSelect();
    if (select == null) {
        appendable.append("VALUES ");
        List<RowExpression> rows = node.getRowList();
        if (rows != null && !rows.isEmpty()) {
            boolean isFst = true;
            for (RowExpression row : rows) {
                if (row == null || row.getRowExprList().isEmpty())
                    continue;
                if (isFst)
                    isFst = false;
                else
                    appendable.append(", ");
                appendable.append('(');
                printList(row.getRowExprList());
                appendable.append(')');
            }
        } else {
            throw new IllegalArgumentException("at least one row for REPLACE");
        }
    } else {
        select.accept(this);
    }
}
Also used : Identifier(com.alibaba.cobar.parser.ast.expression.primary.Identifier) RowExpression(com.alibaba.cobar.parser.ast.expression.primary.RowExpression) QueryExpression(com.alibaba.cobar.parser.ast.expression.misc.QueryExpression)

Aggregations

QueryExpression (com.alibaba.cobar.parser.ast.expression.misc.QueryExpression)13 RowExpression (com.alibaba.cobar.parser.ast.expression.primary.RowExpression)7 Expression (com.alibaba.cobar.parser.ast.expression.Expression)6 Identifier (com.alibaba.cobar.parser.ast.expression.primary.Identifier)6 BetweenAndExpression (com.alibaba.cobar.parser.ast.expression.comparison.BetweenAndExpression)4 ComparisionEqualsExpression (com.alibaba.cobar.parser.ast.expression.comparison.ComparisionEqualsExpression)4 ComparisionIsExpression (com.alibaba.cobar.parser.ast.expression.comparison.ComparisionIsExpression)4 ComparisionNullSafeEqualsExpression (com.alibaba.cobar.parser.ast.expression.comparison.ComparisionNullSafeEqualsExpression)4 InExpression (com.alibaba.cobar.parser.ast.expression.comparison.InExpression)4 LogicalAndExpression (com.alibaba.cobar.parser.ast.expression.logical.LogicalAndExpression)4 LogicalOrExpression (com.alibaba.cobar.parser.ast.expression.logical.LogicalOrExpression)4 UserExpression (com.alibaba.cobar.parser.ast.expression.misc.UserExpression)4 CaseWhenOperatorExpression (com.alibaba.cobar.parser.ast.expression.primary.CaseWhenOperatorExpression)4 MatchExpression (com.alibaba.cobar.parser.ast.expression.primary.MatchExpression)4 FunctionExpression (com.alibaba.cobar.parser.ast.expression.primary.function.FunctionExpression)4 LikeExpression (com.alibaba.cobar.parser.ast.expression.string.LikeExpression)4 CollateExpression (com.alibaba.cobar.parser.ast.expression.type.CollateExpression)4 BinaryOperatorExpression (com.alibaba.cobar.parser.ast.expression.BinaryOperatorExpression)2 PolyadicOperatorExpression (com.alibaba.cobar.parser.ast.expression.PolyadicOperatorExpression)2 UnaryOperatorExpression (com.alibaba.cobar.parser.ast.expression.UnaryOperatorExpression)2