Search in sources :

Example 1 with SysVarPrimary

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

the class MySQLExprParserTest method testPrimarySystemVar.

public void testPrimarySystemVar() throws Exception {
    String sql = "@@gloBal . /*dd*/ `all`";
    MySQLLexer lexer = new MySQLLexer(sql);
    MySQLExprParser parser = new MySQLExprParser(lexer);
    Expression expr = parser.expression();
    String output = output2MySQL(expr, sql);
    Assert.assertEquals("@@global.`all`", output);
    Assert.assertEquals(SysVarPrimary.class, expr.getClass());
    SysVarPrimary sysvar = (SysVarPrimary) expr;
    Assert.assertEquals(VariableScope.GLOBAL, sysvar.getScope());
    Assert.assertEquals("`all`", sysvar.getVarText());
    sql = "@@Session . /*dd*/ any";
    lexer = new MySQLLexer(sql);
    parser = new MySQLExprParser(lexer);
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("@@any", output);
    Assert.assertEquals(SysVarPrimary.class, expr.getClass());
    sysvar = (SysVarPrimary) expr;
    Assert.assertEquals(VariableScope.SESSION, sysvar.getScope());
    Assert.assertEquals("any", sysvar.getVarText());
    sql = "@@LOCAl . /*dd*/ `usage`";
    lexer = new MySQLLexer(sql);
    parser = new MySQLExprParser(lexer);
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("@@`usage`", output);
    Assert.assertEquals(SysVarPrimary.class, expr.getClass());
    sysvar = (SysVarPrimary) expr;
    Assert.assertEquals(VariableScope.SESSION, sysvar.getScope());
    Assert.assertEquals("`usage`", sysvar.getVarText());
    sql = "@@LOCAl . /*dd*/ `var1`";
    lexer = new MySQLLexer(sql);
    parser = new MySQLExprParser(lexer);
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("@@`var1`", output);
    Assert.assertEquals(SysVarPrimary.class, expr.getClass());
    sysvar = (SysVarPrimary) expr;
    Assert.assertEquals(VariableScope.SESSION, sysvar.getScope());
    Assert.assertEquals("`var1`", sysvar.getVarText());
    sql = "@@var1   ,";
    lexer = new MySQLLexer(sql);
    parser = new MySQLExprParser(lexer);
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("@@var1", output);
    Assert.assertEquals(SysVarPrimary.class, expr.getClass());
    sysvar = (SysVarPrimary) expr;
    Assert.assertEquals(VariableScope.SESSION, sysvar.getScope());
    Assert.assertEquals("var1", sysvar.getVarText());
    sql = "@@`case``1`   ,@@_";
    lexer = new MySQLLexer(sql);
    parser = new MySQLExprParser(lexer);
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("@@`case``1`", output);
    Assert.assertEquals(SysVarPrimary.class, expr.getClass());
    sysvar = (SysVarPrimary) expr;
    Assert.assertEquals(VariableScope.SESSION, sysvar.getScope());
    Assert.assertEquals("`case``1`", sysvar.getVarText());
    lexer.nextToken();
    parser = new MySQLExprParser(lexer);
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("@@_", output);
    Assert.assertEquals(SysVarPrimary.class, expr.getClass());
    sysvar = (SysVarPrimary) expr;
    Assert.assertEquals(VariableScope.SESSION, sysvar.getScope());
    Assert.assertEquals("_", sysvar.getVarText());
}
Also used : MySQLLexer(com.alibaba.cobar.parser.recognizer.mysql.lexer.MySQLLexer) RegexpExpression(com.alibaba.cobar.parser.ast.expression.string.RegexpExpression) LogicalAndExpression(com.alibaba.cobar.parser.ast.expression.logical.LogicalAndExpression) UnaryOperatorExpression(com.alibaba.cobar.parser.ast.expression.UnaryOperatorExpression) MinusExpression(com.alibaba.cobar.parser.ast.expression.arithmeic.MinusExpression) 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) BetweenAndExpression(com.alibaba.cobar.parser.ast.expression.comparison.BetweenAndExpression) LikeExpression(com.alibaba.cobar.parser.ast.expression.string.LikeExpression) TernaryOperatorExpression(com.alibaba.cobar.parser.ast.expression.TernaryOperatorExpression) ArithmeticSubtractExpression(com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticSubtractExpression) 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) SoundsLikeExpression(com.alibaba.cobar.parser.ast.expression.string.SoundsLikeExpression) 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) ArithmeticIntegerDivideExpression(com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticIntegerDivideExpression) CollateExpression(com.alibaba.cobar.parser.ast.expression.type.CollateExpression) BitInvertExpression(com.alibaba.cobar.parser.ast.expression.bit.BitInvertExpression) BitXORExpression(com.alibaba.cobar.parser.ast.expression.bit.BitXORExpression) LogicalNotExpression(com.alibaba.cobar.parser.ast.expression.logical.LogicalNotExpression) Expression(com.alibaba.cobar.parser.ast.expression.Expression) BitShiftExpression(com.alibaba.cobar.parser.ast.expression.bit.BitShiftExpression) AssignmentExpression(com.alibaba.cobar.parser.ast.expression.misc.AssignmentExpression) CaseWhenOperatorExpression(com.alibaba.cobar.parser.ast.expression.primary.CaseWhenOperatorExpression) BitOrExpression(com.alibaba.cobar.parser.ast.expression.bit.BitOrExpression) ComparisionGreaterThanOrEqualsExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionGreaterThanOrEqualsExpression) 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) PolyadicOperatorExpression(com.alibaba.cobar.parser.ast.expression.PolyadicOperatorExpression) ComparisionNotEqualsExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionNotEqualsExpression) BinaryOperatorExpression(com.alibaba.cobar.parser.ast.expression.BinaryOperatorExpression) RowExpression(com.alibaba.cobar.parser.ast.expression.primary.RowExpression) ComparisionIsExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionIsExpression) LogicalOrExpression(com.alibaba.cobar.parser.ast.expression.logical.LogicalOrExpression) ComparisionLessThanExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionLessThanExpression) ArithmeticModExpression(com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticModExpression) ComparisionGreaterThanExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionGreaterThanExpression) ArithmeticDivideExpression(com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticDivideExpression) ComparisionLessOrGreaterThanExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionLessOrGreaterThanExpression) LiteralString(com.alibaba.cobar.parser.ast.expression.primary.literal.LiteralString) SysVarPrimary(com.alibaba.cobar.parser.ast.expression.primary.SysVarPrimary)

Example 2 with SysVarPrimary

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

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

the class MySQLParser method systemVariale.

/**
 * first token must be {@link MySQLToken#SYS_VAR}
 */
public SysVarPrimary systemVariale() throws SQLSyntaxErrorException {
    SysVarPrimary sys;
    VariableScope scope = VariableScope.SESSION;
    String str = lexer.stringValue();
    String strUp = lexer.stringValueUppercase();
    match(SYS_VAR);
    SpecialIdentifier si = specialIdentifiers.get(strUp);
    if (si != null) {
        switch(si) {
            case GLOBAL:
                scope = VariableScope.GLOBAL;
            case SESSION:
            case LOCAL:
                match(PUNC_DOT);
                str = lexer.stringValue();
                strUp = lexer.stringValueUppercase();
                match(IDENTIFIER);
                sys = new SysVarPrimary(scope, str, strUp);
                sys.setCacheEvalRst(cacheEvalRst);
                return sys;
        }
    }
    sys = new SysVarPrimary(scope, str, strUp);
    sys.setCacheEvalRst(cacheEvalRst);
    return sys;
}
Also used : SysVarPrimary(com.alibaba.cobar.parser.ast.expression.primary.SysVarPrimary) VariableScope(com.alibaba.cobar.parser.ast.fragment.VariableScope)

Aggregations

SysVarPrimary (com.alibaba.cobar.parser.ast.expression.primary.SysVarPrimary)3 Expression (com.alibaba.cobar.parser.ast.expression.Expression)2 VariableScope (com.alibaba.cobar.parser.ast.fragment.VariableScope)2 BinaryOperatorExpression (com.alibaba.cobar.parser.ast.expression.BinaryOperatorExpression)1 PolyadicOperatorExpression (com.alibaba.cobar.parser.ast.expression.PolyadicOperatorExpression)1 TernaryOperatorExpression (com.alibaba.cobar.parser.ast.expression.TernaryOperatorExpression)1 UnaryOperatorExpression (com.alibaba.cobar.parser.ast.expression.UnaryOperatorExpression)1 ArithmeticAddExpression (com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticAddExpression)1 ArithmeticDivideExpression (com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticDivideExpression)1 ArithmeticIntegerDivideExpression (com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticIntegerDivideExpression)1 ArithmeticModExpression (com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticModExpression)1 ArithmeticMultiplyExpression (com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticMultiplyExpression)1 ArithmeticSubtractExpression (com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticSubtractExpression)1 MinusExpression (com.alibaba.cobar.parser.ast.expression.arithmeic.MinusExpression)1 BitAndExpression (com.alibaba.cobar.parser.ast.expression.bit.BitAndExpression)1 BitInvertExpression (com.alibaba.cobar.parser.ast.expression.bit.BitInvertExpression)1 BitOrExpression (com.alibaba.cobar.parser.ast.expression.bit.BitOrExpression)1 BitShiftExpression (com.alibaba.cobar.parser.ast.expression.bit.BitShiftExpression)1 BitXORExpression (com.alibaba.cobar.parser.ast.expression.bit.BitXORExpression)1 BetweenAndExpression (com.alibaba.cobar.parser.ast.expression.comparison.BetweenAndExpression)1