Search in sources :

Example 1 with VariableExpression

use of com.alibaba.cobar.parser.ast.expression.primary.VariableExpression 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 2 with VariableExpression

use of com.alibaba.cobar.parser.ast.expression.primary.VariableExpression 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)

Aggregations

Expression (com.alibaba.cobar.parser.ast.expression.Expression)2 VariableExpression (com.alibaba.cobar.parser.ast.expression.primary.VariableExpression)2 Pair (com.alibaba.cobar.parser.util.Pair)2 SysVarPrimary (com.alibaba.cobar.parser.ast.expression.primary.SysVarPrimary)1 UsrDefVarPrimary (com.alibaba.cobar.parser.ast.expression.primary.UsrDefVarPrimary)1 LiteralString (com.alibaba.cobar.parser.ast.expression.primary.literal.LiteralString)1 VariableScope (com.alibaba.cobar.parser.ast.fragment.VariableScope)1 DALSetCharacterSetStatement (com.alibaba.cobar.parser.ast.stmt.dal.DALSetCharacterSetStatement)1 DALSetNamesStatement (com.alibaba.cobar.parser.ast.stmt.dal.DALSetNamesStatement)1 DALSetStatement (com.alibaba.cobar.parser.ast.stmt.dal.DALSetStatement)1 MTSSetTransactionStatement (com.alibaba.cobar.parser.ast.stmt.mts.MTSSetTransactionStatement)1