use of com.alibaba.cobar.parser.ast.expression.misc.AssignmentExpression in project cobar by alibaba.
the class MySQLExprParser method primaryExpression.
private Expression primaryExpression(final String consumed, String consumedUp) throws SQLSyntaxErrorException {
if (consumed != null) {
return startedFromIdentifier(consumed, consumedUp);
}
String tempStr;
String tempStrUp;
StringBuilder tempSb;
Number tempNum;
Expression tempExpr;
Expression tempExpr2;
List<Expression> tempExprList;
switch(lexer.token()) {
case PLACE_HOLDER:
tempStr = lexer.stringValue();
tempStrUp = lexer.stringValueUppercase();
lexer.nextToken();
return createPlaceHolder(tempStr, tempStrUp);
case LITERAL_BIT:
tempStr = lexer.stringValue();
lexer.nextToken();
return new LiteralBitField(null, tempStr).setCacheEvalRst(cacheEvalRst);
case LITERAL_HEX:
LiteralHexadecimal hex = new LiteralHexadecimal(null, lexer.getSQL(), lexer.getOffsetCache(), lexer.getSizeCache(), charset);
lexer.nextToken();
return hex.setCacheEvalRst(cacheEvalRst);
case LITERAL_BOOL_FALSE:
lexer.nextToken();
return new LiteralBoolean(false).setCacheEvalRst(cacheEvalRst);
case LITERAL_BOOL_TRUE:
lexer.nextToken();
return new LiteralBoolean(true).setCacheEvalRst(cacheEvalRst);
case LITERAL_NULL:
lexer.nextToken();
return new LiteralNull().setCacheEvalRst(cacheEvalRst);
case LITERAL_NCHARS:
tempSb = new StringBuilder();
do {
lexer.appendStringContent(tempSb);
} while (lexer.nextToken() == LITERAL_CHARS);
return new LiteralString(null, tempSb.toString(), true).setCacheEvalRst(cacheEvalRst);
case LITERAL_CHARS:
tempSb = new StringBuilder();
do {
lexer.appendStringContent(tempSb);
} while (lexer.nextToken() == LITERAL_CHARS);
return new LiteralString(null, tempSb.toString(), false).setCacheEvalRst(cacheEvalRst);
case LITERAL_NUM_PURE_DIGIT:
tempNum = lexer.integerValue();
lexer.nextToken();
return new LiteralNumber(tempNum).setCacheEvalRst(cacheEvalRst);
case LITERAL_NUM_MIX_DIGIT:
tempNum = lexer.decimalValue();
lexer.nextToken();
return new LiteralNumber(tempNum).setCacheEvalRst(cacheEvalRst);
case QUESTION_MARK:
int index = lexer.paramIndex();
lexer.nextToken();
return createParam(index);
case KW_CASE:
lexer.nextToken();
return caseWhenExpression();
case KW_INTERVAL:
lexer.nextToken();
return intervalExpression();
case KW_EXISTS:
lexer.nextToken();
match(PUNC_LEFT_PAREN);
tempExpr = subQuery();
match(PUNC_RIGHT_PAREN);
return new ExistsPrimary((QueryExpression) tempExpr).setCacheEvalRst(cacheEvalRst);
case USR_VAR:
tempStr = lexer.stringValue();
tempExpr = new UsrDefVarPrimary(tempStr).setCacheEvalRst(cacheEvalRst);
if (lexer.nextToken() == OP_ASSIGN) {
lexer.nextToken();
tempExpr2 = expression();
return new AssignmentExpression(tempExpr, tempExpr2);
}
return tempExpr;
case SYS_VAR:
return systemVariale();
case KW_MATCH:
lexer.nextToken();
return matchExpression();
case PUNC_LEFT_PAREN:
lexer.nextToken();
if (lexer.token() == KW_SELECT) {
tempExpr = subQuery();
match(PUNC_RIGHT_PAREN);
return tempExpr;
}
tempExpr = expression();
switch(lexer.token()) {
case PUNC_RIGHT_PAREN:
lexer.nextToken();
return tempExpr;
case PUNC_COMMA:
lexer.nextToken();
tempExprList = new LinkedList<Expression>();
tempExprList.add(tempExpr);
tempExprList = expressionList(tempExprList);
return new RowExpression(tempExprList).setCacheEvalRst(cacheEvalRst);
default:
throw err("unexpected token: " + lexer.token());
}
case KW_UTC_DATE:
lexer.nextToken();
if (lexer.token() == PUNC_LEFT_PAREN) {
lexer.nextToken();
match(PUNC_RIGHT_PAREN);
}
return new UtcDate(null).setCacheEvalRst(cacheEvalRst);
case KW_UTC_TIME:
lexer.nextToken();
if (lexer.token() == PUNC_LEFT_PAREN) {
lexer.nextToken();
match(PUNC_RIGHT_PAREN);
}
return new UtcTime(null).setCacheEvalRst(cacheEvalRst);
case KW_UTC_TIMESTAMP:
lexer.nextToken();
if (lexer.token() == PUNC_LEFT_PAREN) {
lexer.nextToken();
match(PUNC_RIGHT_PAREN);
}
return new UtcTimestamp(null).setCacheEvalRst(cacheEvalRst);
case KW_CURRENT_DATE:
lexer.nextToken();
if (lexer.token() == PUNC_LEFT_PAREN) {
lexer.nextToken();
match(PUNC_RIGHT_PAREN);
}
return new Curdate().setCacheEvalRst(cacheEvalRst);
case KW_CURRENT_TIME:
lexer.nextToken();
if (lexer.token() == PUNC_LEFT_PAREN) {
lexer.nextToken();
match(PUNC_RIGHT_PAREN);
}
return new Curtime().setCacheEvalRst(cacheEvalRst);
case KW_CURRENT_TIMESTAMP:
case KW_LOCALTIME:
case KW_LOCALTIMESTAMP:
lexer.nextToken();
if (lexer.token() == PUNC_LEFT_PAREN) {
lexer.nextToken();
match(PUNC_RIGHT_PAREN);
}
return new Now().setCacheEvalRst(cacheEvalRst);
case KW_CURRENT_USER:
lexer.nextToken();
if (lexer.token() == PUNC_LEFT_PAREN) {
lexer.nextToken();
match(PUNC_RIGHT_PAREN);
}
return new CurrentUser().setCacheEvalRst(cacheEvalRst);
case KW_DEFAULT:
if (lexer.nextToken() == PUNC_LEFT_PAREN) {
return ordinaryFunction(lexer.stringValue(), lexer.stringValueUppercase());
}
return new DefaultValue().setCacheEvalRst(cacheEvalRst);
case KW_DATABASE:
case KW_IF:
case KW_INSERT:
case KW_LEFT:
case KW_REPEAT:
case KW_REPLACE:
case KW_RIGHT:
case KW_SCHEMA:
case KW_VALUES:
tempStr = lexer.stringValue();
tempStrUp = lexer.stringValueUppercase();
String tempStrUp2 = MySQLToken.keyWordToString(lexer.token());
if (!tempStrUp2.equals(tempStrUp)) {
tempStrUp = tempStr = tempStrUp2;
}
if (lexer.nextToken() == PUNC_LEFT_PAREN) {
return ordinaryFunction(tempStr, tempStrUp);
}
throw err("keyword not followed by '(' is not expression: " + tempStr);
case KW_MOD:
lexer.nextToken();
match(PUNC_LEFT_PAREN);
tempExpr = expression();
match(PUNC_COMMA);
tempExpr2 = expression();
match(PUNC_RIGHT_PAREN);
return new ArithmeticModExpression(tempExpr, tempExpr2).setCacheEvalRst(cacheEvalRst);
case KW_CHAR:
lexer.nextToken();
match(PUNC_LEFT_PAREN);
return functionChar();
case KW_CONVERT:
lexer.nextToken();
match(PUNC_LEFT_PAREN);
return functionConvert();
case IDENTIFIER:
tempStr = lexer.stringValue();
tempStrUp = lexer.stringValueUppercase();
lexer.nextToken();
return startedFromIdentifier(tempStr, tempStrUp);
case OP_ASTERISK:
lexer.nextToken();
return new Wildcard(null).setCacheEvalRst(cacheEvalRst);
default:
throw err("unrecognized token as first token of primary: " + lexer.token());
}
}
use of com.alibaba.cobar.parser.ast.expression.misc.AssignmentExpression in project cobar by alibaba.
the class MySQLExprParserTest method testAssignment.
public void testAssignment() throws Exception {
String sql = "a /*dd*/:=b:=c";
MySQLExprParser parser = new MySQLExprParser(new MySQLLexer(sql));
Expression expr = parser.expression();
String output = output2MySQL(expr, sql);
Assert.assertEquals("a := b := c", output);
Assert.assertEquals(AssignmentExpression.class, expr.getClass());
AssignmentExpression ass = (AssignmentExpression) expr;
Assert.assertEquals(AssignmentExpression.class, ass.getRightOprand().getClass());
ass = (AssignmentExpression) ass.getRightOprand();
Assert.assertEquals("b", ((Identifier) ass.getLeftOprand()).getIdText());
sql = "c=@var:=1";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("c = (@var := 1)", output);
Assert.assertEquals(ComparisionEqualsExpression.class, expr.getClass());
ass = (AssignmentExpression) ((BinaryOperatorExpression) expr).getRightOprand();
UsrDefVarPrimary usr = (UsrDefVarPrimary) ass.getLeftOprand();
Assert.assertEquals("@var", usr.getVarText());
sql = "a:=b or c &&d :=0b1101 or b'01'&0xabc";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("a := b OR c AND d := b'1101' OR b'01' & x'abc'", output);
Assert.assertEquals(AssignmentExpression.class, expr.getClass());
ass = (AssignmentExpression) expr;
Assert.assertEquals(AssignmentExpression.class, ass.getRightOprand().getClass());
ass = (AssignmentExpression) ass.getRightOprand();
Assert.assertEquals(LogicalOrExpression.class, ass.getLeftOprand().getClass());
Assert.assertEquals(LogicalOrExpression.class, ass.getRightOprand().getClass());
LogicalOrExpression lor = (LogicalOrExpression) ass.getLeftOprand();
Assert.assertEquals(LogicalAndExpression.class, lor.getOperand(1).getClass());
lor = (LogicalOrExpression) ass.getRightOprand();
Assert.assertEquals(LiteralBitField.class, lor.getOperand(0).getClass());
Assert.assertEquals(BitAndExpression.class, lor.getOperand(1).getClass());
sql = "a:=((b or (c &&d)) :=((0b1101 or (b'01'&0xabc))))";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("a := b OR c AND d := b'1101' OR b'01' & x'abc'", output);
Assert.assertEquals(AssignmentExpression.class, expr.getClass());
ass = (AssignmentExpression) expr;
Assert.assertEquals(AssignmentExpression.class, ass.getRightOprand().getClass());
ass = (AssignmentExpression) ass.getRightOprand();
Assert.assertEquals(LogicalOrExpression.class, ass.getLeftOprand().getClass());
Assert.assertEquals(LogicalOrExpression.class, ass.getRightOprand().getClass());
lor = (LogicalOrExpression) ass.getLeftOprand();
Assert.assertEquals(LogicalAndExpression.class, lor.getOperand(1).getClass());
lor = (LogicalOrExpression) ass.getRightOprand();
Assert.assertEquals(LiteralBitField.class, lor.getOperand(0).getClass());
Assert.assertEquals(BitAndExpression.class, lor.getOperand(1).getClass());
sql = "(a:=b) or c &&(d :=0b1101 or b'01')&0xabc ^null";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("(a := b) OR c AND (d := b'1101' OR b'01') & x'abc' ^ NULL", output);
Assert.assertEquals(LogicalOrExpression.class, expr.getClass());
lor = (LogicalOrExpression) expr;
Assert.assertEquals(AssignmentExpression.class, lor.getOperand(0).getClass());
Assert.assertEquals(LogicalAndExpression.class, lor.getOperand(1).getClass());
LogicalAndExpression land = (LogicalAndExpression) lor.getOperand(1);
Assert.assertEquals(Identifier.class, land.getOperand(0).getClass());
Assert.assertEquals(BitAndExpression.class, land.getOperand(1).getClass());
BitAndExpression band = (BitAndExpression) land.getOperand(1);
Assert.assertEquals(AssignmentExpression.class, band.getLeftOprand().getClass());
Assert.assertEquals(BitXORExpression.class, band.getRightOprand().getClass());
ass = (AssignmentExpression) band.getLeftOprand();
Assert.assertEquals(LogicalOrExpression.class, ass.getRightOprand().getClass());
BitXORExpression bxor = (BitXORExpression) band.getRightOprand();
Assert.assertEquals(LiteralHexadecimal.class, bxor.getLeftOprand().getClass());
Assert.assertEquals(LiteralNull.class, bxor.getRightOprand().getClass());
}
use of com.alibaba.cobar.parser.ast.expression.misc.AssignmentExpression in project cobar by alibaba.
the class MySQLExprParser method expression.
/**
* first token of this expression has been scanned, not yet consumed
*/
public Expression expression() throws SQLSyntaxErrorException {
MySQLToken token = lexer.token();
if (token == null) {
token = lexer.nextToken();
}
if (token == EOF) {
err("unexpected EOF");
}
Expression left = logicalOrExpression();
if (lexer.token() == OP_ASSIGN) {
lexer.nextToken();
Expression right = expression();
return new AssignmentExpression(left, right).setCacheEvalRst(cacheEvalRst);
}
return left;
}
Aggregations