Search in sources :

Example 71 with Expression

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

the class MySQLDALParser method varAssign.

private Object varAssign() throws SQLSyntaxErrorException {
    VariableExpression var;
    Expression expr;
    VariableScope scope = VariableScope.SESSION;
    switch(lexer.token()) {
        case IDENTIFIER:
            boolean explictScope = false;
            SpecialIdentifier si = specialIdentifiers.get(lexer.stringValueUppercase());
            if (si != null) {
                switch(si) {
                    case TRANSACTION:
                        return setMTSSetTransactionStatement(null);
                    case GLOBAL:
                        scope = VariableScope.GLOBAL;
                    case SESSION:
                    case LOCAL:
                        explictScope = true;
                        lexer.nextToken();
                    default:
                        break;
                }
            }
            if (explictScope && specialIdentifiers.get(lexer.stringValueUppercase()) == SpecialIdentifier.TRANSACTION) {
                return setMTSSetTransactionStatement(scope);
            }
            var = new SysVarPrimary(scope, lexer.stringValue(), lexer.stringValueUppercase());
            match(IDENTIFIER);
            break;
        case SYS_VAR:
            var = systemVariale();
            break;
        case USR_VAR:
            var = new UsrDefVarPrimary(lexer.stringValue());
            lexer.nextToken();
            break;
        default:
            throw err("unexpected token for SET statement");
    }
    match(OP_EQUALS, OP_ASSIGN);
    expr = exprParser.expression();
    return new Pair<VariableExpression, Expression>(var, expr);
}
Also used : UsrDefVarPrimary(com.alibaba.cobar.parser.ast.expression.primary.UsrDefVarPrimary) Expression(com.alibaba.cobar.parser.ast.expression.Expression) VariableExpression(com.alibaba.cobar.parser.ast.expression.primary.VariableExpression) VariableExpression(com.alibaba.cobar.parser.ast.expression.primary.VariableExpression) SysVarPrimary(com.alibaba.cobar.parser.ast.expression.primary.SysVarPrimary) VariableScope(com.alibaba.cobar.parser.ast.fragment.VariableScope) Pair(com.alibaba.cobar.parser.util.Pair)

Example 72 with Expression

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

the class MySQLDALParser method set.

/**
     * @return {@link DALSetStatement} or {@link MTSSetTransactionStatement}
     */
@SuppressWarnings("unchecked")
public SQLStatement set() throws SQLSyntaxErrorException {
    match(KW_SET);
    if (lexer.token() == KW_OPTION) {
        lexer.nextToken();
    }
    if (lexer.token() == IDENTIFIER && SpecialIdentifier.NAMES == specialIdentifiers.get(lexer.stringValueUppercase())) {
        if (lexer.nextToken() == KW_DEFAULT) {
            lexer.nextToken();
            return new DALSetNamesStatement();
        }
        String charsetName = getStringValue();
        String collationName = null;
        if (lexer.token() == KW_COLLATE) {
            lexer.nextToken();
            collationName = getStringValue();
        }
        return new DALSetNamesStatement(charsetName, collationName);
    } else if (lexer.token() == KW_CHARACTER) {
        lexer.nextToken();
        match(KW_SET);
        if (lexer.token() == KW_DEFAULT) {
            lexer.nextToken();
            return new DALSetCharacterSetStatement();
        }
        String charsetName = getStringValue();
        return new DALSetCharacterSetStatement(charsetName);
    }
    List<Pair<VariableExpression, Expression>> assignmentList;
    Object obj = varAssign();
    if (obj instanceof MTSSetTransactionStatement) {
        return (MTSSetTransactionStatement) obj;
    }
    Pair<VariableExpression, Expression> pair = (Pair<VariableExpression, Expression>) obj;
    if (lexer.token() != PUNC_COMMA) {
        assignmentList = new ArrayList<Pair<VariableExpression, Expression>>(1);
        assignmentList.add(pair);
        return new DALSetStatement(assignmentList);
    }
    assignmentList = new LinkedList<Pair<VariableExpression, Expression>>();
    assignmentList.add(pair);
    for (; lexer.token() == PUNC_COMMA; ) {
        lexer.nextToken();
        pair = (Pair<VariableExpression, Expression>) varAssign();
        assignmentList.add(pair);
    }
    return new DALSetStatement(assignmentList);
}
Also used : DALSetNamesStatement(com.alibaba.cobar.parser.ast.stmt.dal.DALSetNamesStatement) VariableExpression(com.alibaba.cobar.parser.ast.expression.primary.VariableExpression) LiteralString(com.alibaba.cobar.parser.ast.expression.primary.literal.LiteralString) MTSSetTransactionStatement(com.alibaba.cobar.parser.ast.stmt.mts.MTSSetTransactionStatement) Expression(com.alibaba.cobar.parser.ast.expression.Expression) VariableExpression(com.alibaba.cobar.parser.ast.expression.primary.VariableExpression) DALSetCharacterSetStatement(com.alibaba.cobar.parser.ast.stmt.dal.DALSetCharacterSetStatement) DALSetStatement(com.alibaba.cobar.parser.ast.stmt.dal.DALSetStatement) Pair(com.alibaba.cobar.parser.util.Pair)

Example 73 with Expression

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

the class MySQLDALParser method showColumns.

/**
     * First token is {@link SpecialIdentifier#COLUMNS}
     * 
     * <pre>
     * SHOW [FULL] <code>COLUMNS {FROM | IN} tbl_name [{FROM | IN} db_name] [LIKE 'pattern' | WHERE expr] </code>
     * </pre>
     */
private ShowColumns showColumns(boolean full) throws SQLSyntaxErrorException {
    lexer.nextToken();
    match(KW_FROM, KW_IN);
    Identifier table = identifier();
    Identifier database = null;
    switch(lexer.token()) {
        case KW_FROM:
        case KW_IN:
            lexer.nextToken();
            database = identifier();
    }
    switch(lexer.token()) {
        case KW_LIKE:
            String like = like();
            return new ShowColumns(full, table, database, like);
        case KW_WHERE:
            Expression where = where();
            return new ShowColumns(full, table, database, where);
    }
    return new ShowColumns(full, table, database);
}
Also used : Identifier(com.alibaba.cobar.parser.ast.expression.primary.Identifier) Expression(com.alibaba.cobar.parser.ast.expression.Expression) VariableExpression(com.alibaba.cobar.parser.ast.expression.primary.VariableExpression) ShowColumns(com.alibaba.cobar.parser.ast.stmt.dal.ShowColumns) LiteralString(com.alibaba.cobar.parser.ast.expression.primary.literal.LiteralString)

Example 74 with Expression

use of com.alibaba.cobar.parser.ast.expression.Expression 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 75 with Expression

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

the class MySQLDMLInsertReplaceParser method rowList.

protected List<RowExpression> rowList() throws SQLSyntaxErrorException {
    List<RowExpression> valuesList;
    List<Expression> tempRowValue = rowValue();
    if (lexer.token() == PUNC_COMMA) {
        valuesList = new LinkedList<RowExpression>();
        valuesList.add(new RowExpression(tempRowValue));
        for (; lexer.token() == PUNC_COMMA; ) {
            lexer.nextToken();
            tempRowValue = rowValue();
            valuesList.add(new RowExpression(tempRowValue));
        }
    } else {
        valuesList = new ArrayList<RowExpression>(1);
        valuesList.add(new RowExpression(tempRowValue));
    }
    return valuesList;
}
Also used : RowExpression(com.alibaba.cobar.parser.ast.expression.primary.RowExpression) Expression(com.alibaba.cobar.parser.ast.expression.Expression) RowExpression(com.alibaba.cobar.parser.ast.expression.primary.RowExpression)

Aggregations

Expression (com.alibaba.cobar.parser.ast.expression.Expression)128 RowExpression (com.alibaba.cobar.parser.ast.expression.primary.RowExpression)96 QueryExpression (com.alibaba.cobar.parser.ast.expression.misc.QueryExpression)93 InExpression (com.alibaba.cobar.parser.ast.expression.comparison.InExpression)91 ComparisionEqualsExpression (com.alibaba.cobar.parser.ast.expression.comparison.ComparisionEqualsExpression)88 LogicalAndExpression (com.alibaba.cobar.parser.ast.expression.logical.LogicalAndExpression)88 BetweenAndExpression (com.alibaba.cobar.parser.ast.expression.comparison.BetweenAndExpression)87 ComparisionIsExpression (com.alibaba.cobar.parser.ast.expression.comparison.ComparisionIsExpression)87 ComparisionNullSafeEqualsExpression (com.alibaba.cobar.parser.ast.expression.comparison.ComparisionNullSafeEqualsExpression)87 LogicalOrExpression (com.alibaba.cobar.parser.ast.expression.logical.LogicalOrExpression)87 UserExpression (com.alibaba.cobar.parser.ast.expression.misc.UserExpression)87 CaseWhenOperatorExpression (com.alibaba.cobar.parser.ast.expression.primary.CaseWhenOperatorExpression)87 LikeExpression (com.alibaba.cobar.parser.ast.expression.string.LikeExpression)87 CollateExpression (com.alibaba.cobar.parser.ast.expression.type.CollateExpression)87 FunctionExpression (com.alibaba.cobar.parser.ast.expression.primary.function.FunctionExpression)76 MatchExpression (com.alibaba.cobar.parser.ast.expression.primary.MatchExpression)71 BinaryOperatorExpression (com.alibaba.cobar.parser.ast.expression.BinaryOperatorExpression)58 PolyadicOperatorExpression (com.alibaba.cobar.parser.ast.expression.PolyadicOperatorExpression)58 UnaryOperatorExpression (com.alibaba.cobar.parser.ast.expression.UnaryOperatorExpression)58 ArithmeticAddExpression (com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticAddExpression)45