use of com.alibaba.cobar.parser.ast.expression.primary.Identifier in project cobar by alibaba.
the class MySQLDMLDeleteParser method delete.
/**
* first token is {@link MySQLToken#KW_DELETE} <code><pre>
* 'DELETE' 'LOW_PRIORITY'? 'QUICK'? 'IGNORE'? (
* 'FROM' tid ( (',' tid)* 'USING' table_refs ('WHERE' cond)?
* | ('WHERE' cond)? ('ORDER' 'BY' ids)? ('LIMIT' count)? ) // single table
* | tid (',' tid)* 'FROM' table_refs ('WHERE' cond)? )
* </pre></code>
*/
public DMLDeleteStatement delete() throws SQLSyntaxErrorException {
match(KW_DELETE);
boolean lowPriority = false;
boolean quick = false;
boolean ignore = false;
loopOpt: for (; ; lexer.nextToken()) {
switch(lexer.token()) {
case KW_LOW_PRIORITY:
lowPriority = true;
break;
case KW_IGNORE:
ignore = true;
break;
case IDENTIFIER:
SpecialIdentifier si = specialIdentifiers.get(lexer.stringValueUppercase());
if (SpecialIdentifier.QUICK == si) {
quick = true;
break;
}
default:
break loopOpt;
}
}
List<Identifier> tempList;
TableReferences tempRefs;
Expression tempWhere;
if (lexer.token() == KW_FROM) {
lexer.nextToken();
Identifier id = identifier();
tempList = new ArrayList<Identifier>(1);
tempList.add(id);
switch(lexer.token()) {
case PUNC_COMMA:
tempList = buildIdList(id);
case KW_USING:
lexer.nextToken();
tempRefs = tableRefs();
if (lexer.token() == KW_WHERE) {
lexer.nextToken();
tempWhere = exprParser.expression();
return new DMLDeleteStatement(lowPriority, quick, ignore, tempList, tempRefs, tempWhere);
}
return new DMLDeleteStatement(lowPriority, quick, ignore, tempList, tempRefs);
case KW_WHERE:
case KW_ORDER:
case KW_LIMIT:
break;
default:
return new DMLDeleteStatement(lowPriority, quick, ignore, id);
}
tempWhere = null;
OrderBy orderBy = null;
Limit limit = null;
if (lexer.token() == KW_WHERE) {
lexer.nextToken();
tempWhere = exprParser.expression();
}
if (lexer.token() == KW_ORDER) {
orderBy = orderBy();
}
if (lexer.token() == KW_LIMIT) {
limit = limit();
}
return new DMLDeleteStatement(lowPriority, quick, ignore, id, tempWhere, orderBy, limit);
}
tempList = idList();
match(KW_FROM);
tempRefs = tableRefs();
if (lexer.token() == KW_WHERE) {
lexer.nextToken();
tempWhere = exprParser.expression();
return new DMLDeleteStatement(lowPriority, quick, ignore, tempList, tempRefs, tempWhere);
}
return new DMLDeleteStatement(lowPriority, quick, ignore, tempList, tempRefs);
}
use of com.alibaba.cobar.parser.ast.expression.primary.Identifier in project cobar by alibaba.
the class MySQLOutputASTVisitor method visit.
@Override
public void visit(DMLReplaceStatement node) {
appendable.append("REPLACE ");
switch(node.getMode()) {
case DELAY:
appendable.append("DELAYED ");
break;
case LOW:
appendable.append("LOW_PRIORITY ");
break;
case UNDEF:
break;
default:
throw new IllegalArgumentException("unknown mode for INSERT: " + node.getMode());
}
appendable.append("INTO ");
node.getTable().accept(this);
appendable.append(' ');
List<Identifier> cols = node.getColumnNameList();
if (cols != null && !cols.isEmpty()) {
appendable.append('(');
printList(cols);
appendable.append(") ");
}
QueryExpression select = node.getSelect();
if (select == null) {
appendable.append("VALUES ");
List<RowExpression> rows = node.getRowList();
if (rows != null && !rows.isEmpty()) {
boolean isFst = true;
for (RowExpression row : rows) {
if (row == null || row.getRowExprList().isEmpty())
continue;
if (isFst)
isFst = false;
else
appendable.append(", ");
appendable.append('(');
printList(row.getRowExprList());
appendable.append(')');
}
} else {
throw new IllegalArgumentException("at least one row for REPLACE");
}
} else {
select.accept(this);
}
}
use of com.alibaba.cobar.parser.ast.expression.primary.Identifier in project cobar by alibaba.
the class PartitionKeyVisitor method visit.
@Override
public void visit(DMLInsertStatement node) {
insertReplace(node);
List<Pair<Identifier, Expression>> dup = node.getDuplicateUpdate();
if (dup != null) {
ASTNode[] duplist = new ASTNode[dup.size() * 2];
int i = 0;
for (Pair<Identifier, Expression> p : dup) {
Identifier key = null;
Expression value = null;
if (p != null) {
key = p.getKey();
value = p.getValue();
}
duplist[i++] = key;
duplist[i++] = value;
}
visitChild(2, false, false, duplist);
}
}
use of com.alibaba.cobar.parser.ast.expression.primary.Identifier in project cobar by alibaba.
the class PartitionKeyVisitor method visit.
@Override
public void visit(TableRefFactor node) {
Identifier table = node.getTable();
visitChild(1, false, false, table);
String tableName = table.getIdTextUpUnescape();
addTable(tableName);
String alias = node.getAliasUnescapeUppercase();
if (alias == null) {
tableAlias.put(null, tableName);
tableAlias.put(tableName, tableName);
} else {
if (!tableAlias.containsKey(null)) {
tableAlias.put(null, tableName);
}
tableAlias.put(alias, tableName);
}
}
use of com.alibaba.cobar.parser.ast.expression.primary.Identifier in project cobar by alibaba.
the class PartitionKeyVisitor method visit.
@Override
public void visit(InExpression node) {
Expression left = node.getLeftOprand();
Expression right = node.getRightOprand();
visitChild(2, false, false, left, right);
if (verdictColumn && !node.isNot() && left instanceof Identifier && right instanceof InExpressionList) {
Identifier col = (Identifier) left;
String colName = col.getIdTextUpUnescape();
String table = tableAlias.get(col.getLevelUnescapeUpName(2));
if (isRuledColumn(table, colName)) {
List<Object> valList = ensureColumnValueList(ensureColumnValueByTable(table), colName);
Map<Object, Set<Pair<Expression, ASTNode>>> valMap = ensureColumnValueIndexObjMap(ensureColumnValueIndexByTable(table), colName);
InExpressionList inlist = (InExpressionList) right;
for (Expression expr : inlist.getList()) {
Object value = expr.evaluation(evaluationParameter);
if (value != Expression.UNEVALUATABLE) {
valList.add(value);
addIntoColumnValueIndex(valMap, value, expr, node);
}
}
}
}
}
Aggregations