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());
}
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);
}
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;
}
Aggregations