Search in sources :

Example 11 with Pair

use of com.alibaba.cobar.parser.util.Pair 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 12 with Pair

use of com.alibaba.cobar.parser.util.Pair 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 13 with Pair

use of com.alibaba.cobar.parser.util.Pair 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 14 with Pair

use of com.alibaba.cobar.parser.util.Pair in project cobar by alibaba.

the class MySQLDMLSelectParser method selectExprList.

private List<Pair<Expression, String>> selectExprList() throws SQLSyntaxErrorException {
    Expression expr = exprParser.expression();
    String alias = as();
    List<Pair<Expression, String>> list;
    if (lexer.token() == PUNC_COMMA) {
        list = new LinkedList<Pair<Expression, String>>();
        list.add(new Pair<Expression, String>(expr, alias));
    } else {
        list = new ArrayList<Pair<Expression, String>>(1);
        list.add(new Pair<Expression, String>(expr, alias));
        return list;
    }
    for (; lexer.token() == PUNC_COMMA; list.add(new Pair<Expression, String>(expr, alias))) {
        lexer.nextToken();
        expr = exprParser.expression();
        alias = as();
    }
    return list;
}
Also used : Expression(com.alibaba.cobar.parser.ast.expression.Expression) Pair(com.alibaba.cobar.parser.util.Pair)

Example 15 with Pair

use of com.alibaba.cobar.parser.util.Pair in project cobar by alibaba.

the class MySQLDMLSelectParser method select.

@Override
public DMLSelectStatement select() throws SQLSyntaxErrorException {
    match(KW_SELECT);
    DMLSelectStatement.SelectOption option = selectOption();
    List<Pair<Expression, String>> exprList = selectExprList();
    TableReferences tables = null;
    Expression where = null;
    GroupBy group = null;
    Expression having = null;
    OrderBy order = null;
    Limit limit = null;
    boolean dual = false;
    if (lexer.token() == KW_FROM) {
        if (lexer.nextToken() == KW_DUAL) {
            lexer.nextToken();
            dual = true;
            List<TableReference> trs = new ArrayList<TableReference>(1);
            trs.add(new Dual());
            tables = new TableReferences(trs);
        } else {
            tables = tableRefs();
        }
    }
    if (lexer.token() == KW_WHERE) {
        lexer.nextToken();
        where = exprParser.expression();
    }
    if (!dual) {
        group = groupBy();
        if (lexer.token() == KW_HAVING) {
            lexer.nextToken();
            having = exprParser.expression();
        }
        order = orderBy();
    }
    limit = limit();
    if (!dual) {
        switch(lexer.token()) {
            case KW_FOR:
                lexer.nextToken();
                match(KW_UPDATE);
                option.lockMode = DMLSelectStatement.LockMode.FOR_UPDATE;
                break;
            case KW_LOCK:
                lexer.nextToken();
                match(KW_IN);
                matchIdentifier("SHARE");
                matchIdentifier("MODE");
                option.lockMode = DMLSelectStatement.LockMode.LOCK_IN_SHARE_MODE;
                break;
        }
    }
    return new DMLSelectStatement(option, exprList, tables, where, group, having, order, limit);
}
Also used : OrderBy(com.alibaba.cobar.parser.ast.fragment.OrderBy) GroupBy(com.alibaba.cobar.parser.ast.fragment.GroupBy) ArrayList(java.util.ArrayList) TableReferences(com.alibaba.cobar.parser.ast.fragment.tableref.TableReferences) TableReference(com.alibaba.cobar.parser.ast.fragment.tableref.TableReference) Expression(com.alibaba.cobar.parser.ast.expression.Expression) DMLSelectStatement(com.alibaba.cobar.parser.ast.stmt.dml.DMLSelectStatement) Limit(com.alibaba.cobar.parser.ast.fragment.Limit) Dual(com.alibaba.cobar.parser.ast.fragment.tableref.Dual) Pair(com.alibaba.cobar.parser.util.Pair)

Aggregations

Pair (com.alibaba.cobar.parser.util.Pair)22 Expression (com.alibaba.cobar.parser.ast.expression.Expression)20 RowExpression (com.alibaba.cobar.parser.ast.expression.primary.RowExpression)13 Identifier (com.alibaba.cobar.parser.ast.expression.primary.Identifier)12 InExpression (com.alibaba.cobar.parser.ast.expression.comparison.InExpression)11 QueryExpression (com.alibaba.cobar.parser.ast.expression.misc.QueryExpression)10 BetweenAndExpression (com.alibaba.cobar.parser.ast.expression.comparison.BetweenAndExpression)8 ComparisionEqualsExpression (com.alibaba.cobar.parser.ast.expression.comparison.ComparisionEqualsExpression)8 ComparisionIsExpression (com.alibaba.cobar.parser.ast.expression.comparison.ComparisionIsExpression)8 ComparisionNullSafeEqualsExpression (com.alibaba.cobar.parser.ast.expression.comparison.ComparisionNullSafeEqualsExpression)8 LogicalAndExpression (com.alibaba.cobar.parser.ast.expression.logical.LogicalAndExpression)8 LogicalOrExpression (com.alibaba.cobar.parser.ast.expression.logical.LogicalOrExpression)8 UserExpression (com.alibaba.cobar.parser.ast.expression.misc.UserExpression)8 CaseWhenOperatorExpression (com.alibaba.cobar.parser.ast.expression.primary.CaseWhenOperatorExpression)8 MatchExpression (com.alibaba.cobar.parser.ast.expression.primary.MatchExpression)8 FunctionExpression (com.alibaba.cobar.parser.ast.expression.primary.function.FunctionExpression)8 LikeExpression (com.alibaba.cobar.parser.ast.expression.string.LikeExpression)8 CollateExpression (com.alibaba.cobar.parser.ast.expression.type.CollateExpression)8 ASTNode (com.alibaba.cobar.parser.ast.ASTNode)6 BinaryOperatorExpression (com.alibaba.cobar.parser.ast.expression.BinaryOperatorExpression)6