use of com.alibaba.cobar.parser.ast.fragment.OrderBy 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.fragment.OrderBy in project cobar by alibaba.
the class MySQLOutputASTVisitor method visit.
@Override
public void visit(DMLDeleteStatement node) {
appendable.append("DELETE ");
if (node.isLowPriority())
appendable.append("LOW_PRIORITY ");
if (node.isQuick())
appendable.append("QUICK ");
if (node.isIgnore())
appendable.append("IGNORE ");
TableReferences tableRefs = node.getTableRefs();
if (tableRefs == null) {
appendable.append("FROM ");
node.getTableNames().get(0).accept(this);
} else {
printList(node.getTableNames());
appendable.append(" FROM ");
node.getTableRefs().accept(this);
}
Expression where = node.getWhereCondition();
if (where != null) {
appendable.append(" WHERE ");
where.accept(this);
}
OrderBy orderBy = node.getOrderBy();
if (orderBy != null) {
appendable.append(' ');
orderBy.accept(this);
}
Limit limit = node.getLimit();
if (limit != null) {
appendable.append(' ');
limit.accept(this);
}
}
use of com.alibaba.cobar.parser.ast.fragment.OrderBy 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);
}
}
use of com.alibaba.cobar.parser.ast.fragment.OrderBy in project cobar by alibaba.
the class PartitionKeyVisitor method visit.
@Override
public void visit(DMLDeleteStatement node) {
TableReference tr = node.getTableRefs();
List<Identifier> tbs = node.getTableNames();
if (tr == null) {
Identifier table = tbs.get(0);
tableAsTableFactor(table);
} else {
visitChild(1, verdictColumn, false, tr);
for (Identifier tb : tbs) {
if (tb instanceof Wildcard) {
int trim = tb.trimParent(2, trimSchema);
schemaTrimmed = schemaTrimmed || trim == Identifier.PARENT_TRIMED;
customedSchema = customedSchema || trim == Identifier.PARENT_IGNORED;
} else {
int trim = tb.trimParent(1, trimSchema);
schemaTrimmed = schemaTrimmed || trim == Identifier.PARENT_TRIMED;
customedSchema = customedSchema || trim == Identifier.PARENT_IGNORED;
}
}
}
Expression where = node.getWhereCondition();
visitChild(2, verdictColumn, false, where);
if (tr == null) {
OrderBy order = node.getOrderBy();
visitChild(2, false, false, order);
}
}
use of com.alibaba.cobar.parser.ast.fragment.OrderBy in project cobar by alibaba.
the class MySQLDMLParserTest method testOrderBy.
public void testOrderBy() throws SQLSyntaxErrorException {
String sql = "order by c1 asc, c2 desc , c3 ";
MySQLLexer lexer = new MySQLLexer(sql);
MySQLDMLParser parser = getDMLParser(lexer);
OrderBy orderBy = parser.orderBy();
String output = output2MySQL(orderBy, sql);
ListUtil.isEquals(ListUtil.createList(new Pair<Expression, SortOrder>(new Identifier(null, "c1"), SortOrder.ASC), new Pair<Expression, SortOrder>(new Identifier(null, "c2"), SortOrder.DESC), new Pair<Expression, SortOrder>(new Identifier(null, "c3"), SortOrder.ASC)), orderBy.getOrderByList());
Assert.assertEquals("ORDER BY c1, c2 DESC, c3", output);
sql = "order by c1 ";
lexer = new MySQLLexer(sql);
parser = getDMLParser(lexer);
orderBy = parser.orderBy();
output = output2MySQL(orderBy, sql);
ListUtil.isEquals(ListUtil.createList(new Pair<Expression, SortOrder>(new Identifier(null, "c1"), SortOrder.ASC)), orderBy.getOrderByList());
Assert.assertEquals("ORDER BY c1", output);
sql = "order by c1 asc ";
lexer = new MySQLLexer(sql);
parser = getDMLParser(lexer);
orderBy = parser.orderBy();
output = output2MySQL(orderBy, sql);
ListUtil.isEquals(ListUtil.createList(new Pair<Expression, SortOrder>(new Identifier(null, "c1"), SortOrder.ASC)), orderBy.getOrderByList());
Assert.assertEquals("ORDER BY c1", output);
sql = "order by c1 desc ";
lexer = new MySQLLexer(sql);
parser = getDMLParser(lexer);
orderBy = parser.orderBy();
output = output2MySQL(orderBy, sql);
ListUtil.isEquals(ListUtil.createList(new Pair<Expression, SortOrder>(new Identifier(null, "c1"), SortOrder.DESC)), orderBy.getOrderByList());
Assert.assertEquals("ORDER BY c1 DESC", output);
}
Aggregations