use of com.alibaba.cobar.parser.ast.expression.Expression in project cobar by alibaba.
the class MySQLExprParser method userExpression.
private Expression userExpression(String consumed, String consumedUp) throws SQLSyntaxErrorException {
Expression first = primaryExpression(consumed, consumedUp);
if (lexer.token() == USR_VAR) {
if (first instanceof LiteralString) {
StringBuilder str = new StringBuilder().append('\'').append(((LiteralString) first).getString()).append('\'').append(lexer.stringValue());
lexer.nextToken();
return new UserExpression(str.toString()).setCacheEvalRst(cacheEvalRst);
} else if (first instanceof Identifier) {
StringBuilder str = new StringBuilder().append(((Identifier) first).getIdText()).append(lexer.stringValue());
lexer.nextToken();
return new UserExpression(str.toString()).setCacheEvalRst(cacheEvalRst);
}
}
return first;
}
use of com.alibaba.cobar.parser.ast.expression.Expression 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.Expression in project cobar by alibaba.
the class MySQLExprParser method matchExpression.
/**
* first <code>MATCH</code> has been consumed
*/
private Expression matchExpression() throws SQLSyntaxErrorException {
match(PUNC_LEFT_PAREN);
List<Expression> colList = expressionList(new LinkedList<Expression>());
matchIdentifier("AGAINST");
match(PUNC_LEFT_PAREN);
Expression pattern = expression();
Modifier modifier = Modifier._DEFAULT;
switch(lexer.token()) {
case KW_WITH:
lexer.nextToken();
match(IDENTIFIER);
match(IDENTIFIER);
modifier = Modifier.WITH_QUERY_EXPANSION;
break;
case KW_IN:
switch(lexer.nextToken()) {
case KW_NATURAL:
lexer.nextToken();
matchIdentifier("LANGUAGE");
matchIdentifier("MODE");
if (lexer.token() == KW_WITH) {
lexer.nextToken();
lexer.nextToken();
lexer.nextToken();
modifier = Modifier.IN_NATURAL_LANGUAGE_MODE_WITH_QUERY_EXPANSION;
} else {
modifier = Modifier.IN_NATURAL_LANGUAGE_MODE;
}
break;
default:
matchIdentifier("BOOLEAN");
matchIdentifier("MODE");
modifier = Modifier.IN_BOOLEAN_MODE;
break;
}
}
match(PUNC_RIGHT_PAREN);
return new MatchExpression(colList, pattern, modifier).setCacheEvalRst(cacheEvalRst);
}
use of com.alibaba.cobar.parser.ast.expression.Expression in project cobar by alibaba.
the class MySQLOutputASTVisitor method visit.
@Override
public void visit(CaseWhenOperatorExpression node) {
appendable.append("CASE");
Expression comparee = node.getComparee();
if (comparee != null) {
appendable.append(' ');
comparee.accept(this);
}
List<Pair<Expression, Expression>> whenList = node.getWhenList();
for (Pair<Expression, Expression> whenthen : whenList) {
appendable.append(" WHEN ");
Expression when = whenthen.getKey();
when.accept(this);
appendable.append(" THEN ");
Expression then = whenthen.getValue();
then.accept(this);
}
Expression elseRst = node.getElseResult();
if (elseRst != null) {
appendable.append(" ELSE ");
elseRst.accept(this);
}
appendable.append(" END");
}
use of com.alibaba.cobar.parser.ast.expression.Expression in project cobar by alibaba.
the class MySQLOutputASTVisitor method visit.
@Override
public void visit(DMLSelectStatement node) {
appendable.append("SELECT ");
final DMLSelectStatement.SelectOption option = node.getOption();
switch(option.resultDup) {
case ALL:
break;
case DISTINCT:
appendable.append("DISTINCT ");
break;
case DISTINCTROW:
appendable.append("DISTINCTROW ");
break;
default:
throw new IllegalArgumentException("unknown option for SELECT: " + option);
}
if (option.highPriority) {
appendable.append("HIGH_PRIORITY ");
}
if (option.straightJoin) {
appendable.append("STRAIGHT_JOIN ");
}
switch(option.resultSize) {
case SQL_BIG_RESULT:
appendable.append("SQL_BIG_RESULT ");
break;
case SQL_SMALL_RESULT:
appendable.append("SQL_SMALL_RESULT ");
break;
case UNDEF:
break;
default:
throw new IllegalArgumentException("unknown option for SELECT: " + option);
}
if (option.sqlBufferResult) {
appendable.append("SQL_BUFFER_RESULT ");
}
switch(option.queryCache) {
case SQL_CACHE:
appendable.append("SQL_CACHE ");
break;
case SQL_NO_CACHE:
appendable.append("SQL_NO_CACHE ");
break;
case UNDEF:
break;
default:
throw new IllegalArgumentException("unknown option for SELECT: " + option);
}
if (option.sqlCalcFoundRows) {
appendable.append("SQL_CALC_FOUND_ROWS ");
}
boolean isFst = true;
List<Pair<Expression, String>> exprList = node.getSelectExprList();
for (Pair<Expression, String> p : exprList) {
if (isFst)
isFst = false;
else
appendable.append(", ");
p.getKey().accept(this);
String alias = p.getValue();
if (alias != null) {
appendable.append(" AS ").append(alias);
}
}
TableReferences from = node.getTables();
if (from != null) {
appendable.append(" FROM ");
from.accept(this);
}
Expression where = node.getWhere();
if (where != null) {
appendable.append(" WHERE ");
where.accept(this);
}
GroupBy group = node.getGroup();
if (group != null) {
appendable.append(' ');
group.accept(this);
}
Expression having = node.getHaving();
if (having != null) {
appendable.append(" HAVING ");
having.accept(this);
}
OrderBy order = node.getOrder();
if (order != null) {
appendable.append(' ');
order.accept(this);
}
Limit limit = node.getLimit();
if (limit != null) {
appendable.append(' ');
limit.accept(this);
}
switch(option.lockMode) {
case FOR_UPDATE:
appendable.append(" FOR UPDATE");
break;
case LOCK_IN_SHARE_MODE:
appendable.append(" LOCK IN SHARE MODE");
break;
case UNDEF:
break;
default:
throw new IllegalArgumentException("unknown option for SELECT: " + option);
}
}
Aggregations