use of com.alibaba.cobar.parser.util.Pair 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.util.Pair 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.util.Pair in project cobar by alibaba.
the class MySQLDMLInsertParser method onDuplicateUpdate.
/**
* @return null for not exist
*/
private List<Pair<Identifier, Expression>> onDuplicateUpdate() throws SQLSyntaxErrorException {
if (lexer.token() != KW_ON) {
return null;
}
lexer.nextToken();
matchIdentifier("DUPLICATE");
match(KW_KEY);
match(KW_UPDATE);
List<Pair<Identifier, Expression>> list;
Identifier col = identifier();
match(OP_EQUALS, OP_ASSIGN);
Expression expr = exprParser.expression();
if (lexer.token() == PUNC_COMMA) {
list = new LinkedList<Pair<Identifier, Expression>>();
list.add(new Pair<Identifier, Expression>(col, expr));
for (; lexer.token() == PUNC_COMMA; ) {
lexer.nextToken();
col = identifier();
match(OP_EQUALS, OP_ASSIGN);
expr = exprParser.expression();
list.add(new Pair<Identifier, Expression>(col, expr));
}
return list;
}
list = new ArrayList<Pair<Identifier, Expression>>(1);
list.add(new Pair<Identifier, Expression>(col, expr));
return list;
}
use of com.alibaba.cobar.parser.util.Pair 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.util.Pair 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