use of com.alibaba.cobar.parser.ast.expression.Expression in project cobar by alibaba.
the class MySQLDMLUpdateParser method update.
/**
* nothing has been pre-consumed <code><pre>
* 'UPDATE' 'LOW_PRIORITY'? 'IGNORE'? table_reference
* 'SET' colName ('='|':=') (expr|'DEFAULT') (',' colName ('='|':=') (expr|'DEFAULT'))*
* ('WHERE' cond)?
* {singleTable}? => ('ORDER' 'BY' orderBy)? ('LIMIT' count)?
* </pre></code>
*/
public DMLUpdateStatement update() throws SQLSyntaxErrorException {
match(KW_UPDATE);
boolean lowPriority = false;
boolean ignore = false;
if (lexer.token() == KW_LOW_PRIORITY) {
lexer.nextToken();
lowPriority = true;
}
if (lexer.token() == KW_IGNORE) {
lexer.nextToken();
ignore = true;
}
TableReferences tableRefs = tableRefs();
match(KW_SET);
List<Pair<Identifier, Expression>> values;
Identifier col = identifier();
match(OP_EQUALS, OP_ASSIGN);
Expression expr = exprParser.expression();
if (lexer.token() == PUNC_COMMA) {
values = new LinkedList<Pair<Identifier, Expression>>();
values.add(new Pair<Identifier, Expression>(col, expr));
for (; lexer.token() == PUNC_COMMA; ) {
lexer.nextToken();
col = identifier();
match(OP_EQUALS, OP_ASSIGN);
expr = exprParser.expression();
values.add(new Pair<Identifier, Expression>(col, expr));
}
} else {
values = new ArrayList<Pair<Identifier, Expression>>(1);
values.add(new Pair<Identifier, Expression>(col, expr));
}
Expression where = null;
if (lexer.token() == KW_WHERE) {
lexer.nextToken();
where = exprParser.expression();
}
OrderBy orderBy = null;
Limit limit = null;
if (tableRefs.isSingleTable()) {
orderBy = orderBy();
limit = limit();
}
return new DMLUpdateStatement(lowPriority, ignore, tableRefs, values, where, orderBy, limit);
}
use of com.alibaba.cobar.parser.ast.expression.Expression in project cobar by alibaba.
the class ServerRouter method replacePartitionKeyOperand.
private static void replacePartitionKeyOperand(Map<String, Map<Object, Set<Pair<Expression, ASTNode>>>> index, List<String> cols) {
if (cols == null) {
return;
}
for (String col : cols) {
Map<Object, Set<Pair<Expression, ASTNode>>> map = index.get(col);
if (map == null) {
continue;
}
for (Set<Pair<Expression, ASTNode>> set : map.values()) {
if (set == null) {
continue;
}
for (Pair<Expression, ASTNode> p : set) {
Expression expr = p.getKey();
ASTNode parent = p.getValue();
if (PartitionKeyVisitor.isPartitionKeyOperandSingle(expr, parent)) {
((ReplacableExpression) expr).setReplaceExpr(ReplacableExpression.BOOL_FALSE);
} else if (PartitionKeyVisitor.isPartitionKeyOperandIn(expr, parent)) {
((ReplacableExpression) parent).setReplaceExpr(ReplacableExpression.BOOL_FALSE);
}
}
}
}
}
use of com.alibaba.cobar.parser.ast.expression.Expression in project cobar by alibaba.
the class ServerRouter method validateAST.
private static void validateAST(SQLStatement ast, TableConfig tc, RuleConfig rule, PartitionKeyVisitor visitor) throws SQLNonTransientException {
if (ast instanceof DMLUpdateStatement) {
List<Identifier> columns = null;
List<String> ruleCols = rule.getColumns();
DMLUpdateStatement update = (DMLUpdateStatement) ast;
for (Pair<Identifier, Expression> pair : update.getValues()) {
for (String ruleCol : ruleCols) {
if (equals(pair.getKey().getIdTextUpUnescape(), ruleCol)) {
if (columns == null) {
columns = new ArrayList<Identifier>(ruleCols.size());
}
columns.add(pair.getKey());
}
}
}
if (columns == null) {
return;
}
Map<String, String> alias = visitor.getTableAlias();
for (Identifier column : columns) {
String table = column.getLevelUnescapeUpName(2);
table = alias.get(table);
if (table != null && table.equals(tc.getName())) {
throw new SQLFeatureNotSupportedException("partition key cannot be changed");
}
}
}
}
use of com.alibaba.cobar.parser.ast.expression.Expression in project cobar by alibaba.
the class MySQLExprParser method intervalExpression.
/**
* first <code>INTERVAL</code> has been consumed
*/
private Expression intervalExpression() throws SQLSyntaxErrorException {
Expression fstExpr;
List<Expression> argList = null;
if (lexer.token() == PUNC_LEFT_PAREN) {
if (lexer.nextToken() == KW_SELECT) {
fstExpr = subQuery();
match(PUNC_RIGHT_PAREN);
} else {
fstExpr = expression();
if (lexer.token() == PUNC_COMMA) {
lexer.nextToken();
argList = new LinkedList<Expression>();
argList.add(fstExpr);
argList = expressionList(argList);
} else {
match(PUNC_RIGHT_PAREN);
}
}
} else {
fstExpr = expression();
}
if (argList != null) {
return new Interval(argList).setCacheEvalRst(cacheEvalRst);
}
return new IntervalPrimary(fstExpr, intervalPrimaryUnit()).setCacheEvalRst(cacheEvalRst);
}
use of com.alibaba.cobar.parser.ast.expression.Expression in project cobar by alibaba.
the class MySQLExprParser method functionChar.
/**
* first '(' has been consumed
*/
private Char functionChar() throws SQLSyntaxErrorException {
Char chr;
for (List<Expression> tempExprList = new LinkedList<Expression>(); ; ) {
Expression tempExpr = expression();
tempExprList.add(tempExpr);
switch(lexer.token()) {
case PUNC_COMMA:
lexer.nextToken();
continue;
case PUNC_RIGHT_PAREN:
lexer.nextToken();
chr = new Char(tempExprList, null);
chr.setCacheEvalRst(cacheEvalRst);
return chr;
case KW_USING:
lexer.nextToken();
String tempStr = lexer.stringValue();
match(IDENTIFIER);
match(PUNC_RIGHT_PAREN);
chr = new Char(tempExprList, tempStr);
chr.setCacheEvalRst(cacheEvalRst);
return chr;
default:
throw err("expect ',' or 'USING' or ')' but is " + lexer.token());
}
}
}
Aggregations