use of com.alibaba.cobar.parser.ast.fragment.OrderBy in project cobar by alibaba.
the class MySQLDMLParser method orderBy.
/**
* nothing has been pre-consumed
*
* @return null if there is no order by
*/
protected OrderBy orderBy() throws SQLSyntaxErrorException {
if (lexer.token() != KW_ORDER) {
return null;
}
lexer.nextToken();
match(KW_BY);
Expression expr = exprParser.expression();
SortOrder order = SortOrder.ASC;
OrderBy orderBy;
switch(lexer.token()) {
case KW_DESC:
order = SortOrder.DESC;
case KW_ASC:
if (lexer.nextToken() != PUNC_COMMA) {
return new OrderBy(expr, order);
}
case PUNC_COMMA:
orderBy = new OrderBy();
orderBy.addOrderByItem(expr, order);
break;
default:
return new OrderBy(expr, order);
}
for (; lexer.token() == PUNC_COMMA; ) {
lexer.nextToken();
order = SortOrder.ASC;
expr = exprParser.expression();
switch(lexer.token()) {
case KW_DESC:
order = SortOrder.DESC;
case KW_ASC:
lexer.nextToken();
}
orderBy.addOrderByItem(expr, order);
}
return orderBy;
}
use of com.alibaba.cobar.parser.ast.fragment.OrderBy in project cobar by alibaba.
the class MySQLDMLSelectParser method select.
@Override
public DMLSelectStatement select() throws SQLSyntaxErrorException {
match(KW_SELECT);
DMLSelectStatement.SelectOption option = selectOption();
List<Pair<Expression, String>> exprList = selectExprList();
TableReferences tables = null;
Expression where = null;
GroupBy group = null;
Expression having = null;
OrderBy order = null;
Limit limit = null;
boolean dual = false;
if (lexer.token() == KW_FROM) {
if (lexer.nextToken() == KW_DUAL) {
lexer.nextToken();
dual = true;
List<TableReference> trs = new ArrayList<TableReference>(1);
trs.add(new Dual());
tables = new TableReferences(trs);
} else {
tables = tableRefs();
}
}
if (lexer.token() == KW_WHERE) {
lexer.nextToken();
where = exprParser.expression();
}
if (!dual) {
group = groupBy();
if (lexer.token() == KW_HAVING) {
lexer.nextToken();
having = exprParser.expression();
}
order = orderBy();
}
limit = limit();
if (!dual) {
switch(lexer.token()) {
case KW_FOR:
lexer.nextToken();
match(KW_UPDATE);
option.lockMode = DMLSelectStatement.LockMode.FOR_UPDATE;
break;
case KW_LOCK:
lexer.nextToken();
match(KW_IN);
matchIdentifier("SHARE");
matchIdentifier("MODE");
option.lockMode = DMLSelectStatement.LockMode.LOCK_IN_SHARE_MODE;
break;
}
}
return new DMLSelectStatement(option, exprList, tables, where, group, having, order, limit);
}
use of com.alibaba.cobar.parser.ast.fragment.OrderBy 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.fragment.OrderBy in project cobar by alibaba.
the class PartitionKeyVisitor method visit.
@Override
public void visit(DMLSelectStatement node) {
boolean verdictGroup = true;
List<Expression> exprList = node.getSelectExprListWithoutAlias();
if (verdictGroupFunc) {
for (Expression expr : exprList) {
if (!isGroupFuncPassthroughSelect(expr)) {
groupFuncType = GROUP_CANCEL;
verdictGroup = false;
break;
}
}
limit(node.getLimit());
}
visitChild(2, false, verdictGroupFunc && verdictGroup, exprList);
TableReference tr = node.getTables();
visitChild(1, verdictColumn, verdictGroupFunc && verdictGroup, tr);
Expression where = node.getWhere();
visitChild(2, verdictColumn, false, where);
GroupBy group = node.getGroup();
visitChild(2, false, false, group);
Expression having = node.getHaving();
visitChild(2, verdictColumn, false, having);
OrderBy order = node.getOrder();
visitChild(2, false, false, order);
}
use of com.alibaba.cobar.parser.ast.fragment.OrderBy in project cobar by alibaba.
the class PartitionKeyVisitor method visit.
@Override
public void visit(DMLUpdateStatement node) {
TableReference tr = node.getTableRefs();
visitChild(1, false, false, tr);
List<Pair<Identifier, Expression>> assignmentList = node.getValues();
if (assignmentList != null && !assignmentList.isEmpty()) {
List<ASTNode> list = new ArrayList<ASTNode>(assignmentList.size() * 2);
for (Pair<Identifier, Expression> p : assignmentList) {
if (p == null)
continue;
list.add(p.getKey());
list.add(p.getValue());
}
visitChild(2, false, false, list);
}
Expression where = node.getWhere();
visitChild(2, verdictColumn, false, where);
OrderBy order = node.getOrderBy();
visitChild(2, false, false, order);
}
Aggregations