use of com.alibaba.cobar.parser.ast.expression.Expression in project cobar by alibaba.
the class MySQLDMLParser method buildTableReference.
@SuppressWarnings("unchecked")
private TableReference buildTableReference(TableReference ref) throws SQLSyntaxErrorException {
for (; ; ) {
Expression on;
List<String> using;
TableReference temp;
boolean isOut = false;
boolean isLeft = true;
switch(lexer.token()) {
case KW_INNER:
case KW_CROSS:
lexer.nextToken();
case KW_JOIN:
lexer.nextToken();
temp = tableFactor();
switch(lexer.token()) {
case KW_ON:
lexer.nextToken();
on = exprParser.expression();
ref = new InnerJoin(ref, temp, on);
break;
case KW_USING:
lexer.nextToken();
match(PUNC_LEFT_PAREN);
using = idNameList();
ref = new InnerJoin(ref, temp, using);
break;
default:
ref = new InnerJoin(ref, temp);
break;
}
break;
case KW_STRAIGHT_JOIN:
lexer.nextToken();
temp = tableFactor();
switch(lexer.token()) {
case KW_ON:
lexer.nextToken();
on = exprParser.expression();
ref = new StraightJoin(ref, temp, on);
break;
default:
ref = new StraightJoin(ref, temp);
break;
}
break;
case KW_RIGHT:
isLeft = false;
case KW_LEFT:
lexer.nextToken();
if (lexer.token() == KW_OUTER) {
lexer.nextToken();
}
match(KW_JOIN);
temp = tableReference();
switch(lexer.token()) {
case KW_ON:
lexer.nextToken();
on = exprParser.expression();
ref = new OuterJoin(isLeft, ref, temp, on);
break;
case KW_USING:
lexer.nextToken();
match(PUNC_LEFT_PAREN);
using = idNameList();
ref = new OuterJoin(isLeft, ref, temp, using);
break;
default:
Object condition = temp.removeLastConditionElement();
if (condition instanceof Expression) {
ref = new OuterJoin(isLeft, ref, temp, (Expression) condition);
} else if (condition instanceof List) {
ref = new OuterJoin(isLeft, ref, temp, (List<String>) condition);
} else {
throw err("conditionExpr cannot be null for outer join");
}
break;
}
break;
case KW_NATURAL:
lexer.nextToken();
switch(lexer.token()) {
case KW_RIGHT:
isLeft = false;
case KW_LEFT:
lexer.nextToken();
if (lexer.token() == KW_OUTER) {
lexer.nextToken();
}
isOut = true;
case KW_JOIN:
lexer.nextToken();
temp = tableFactor();
ref = new NaturalJoin(isOut, isLeft, ref, temp);
break;
default:
throw err("unexpected token after NATURAL for natural join:" + lexer.token());
}
break;
default:
return ref;
}
}
}
use of com.alibaba.cobar.parser.ast.expression.Expression in project cobar by alibaba.
the class MySQLDMLParser method groupBy.
/**
* nothing has been pre-consumed
*
* @return null if there is no order by
*/
protected GroupBy groupBy() throws SQLSyntaxErrorException {
if (lexer.token() != KW_GROUP) {
return null;
}
lexer.nextToken();
match(KW_BY);
Expression expr = exprParser.expression();
SortOrder order = SortOrder.ASC;
GroupBy groupBy;
switch(lexer.token()) {
case KW_DESC:
order = SortOrder.DESC;
case KW_ASC:
lexer.nextToken();
default:
break;
}
switch(lexer.token()) {
case KW_WITH:
lexer.nextToken();
matchIdentifier("ROLLUP");
return new GroupBy(expr, order, true);
case PUNC_COMMA:
break;
default:
return new GroupBy(expr, order, false);
}
for (groupBy = new GroupBy().addOrderByItem(expr, order); 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();
default:
break;
}
groupBy.addOrderByItem(expr, order);
if (lexer.token() == KW_WITH) {
lexer.nextToken();
matchIdentifier("ROLLUP");
return groupBy.setWithRollup();
}
}
return groupBy;
}
use of com.alibaba.cobar.parser.ast.expression.Expression 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.expression.Expression in project cobar by alibaba.
the class MySQLDMLSelectParser method selectExprList.
private List<Pair<Expression, String>> selectExprList() throws SQLSyntaxErrorException {
Expression expr = exprParser.expression();
String alias = as();
List<Pair<Expression, String>> list;
if (lexer.token() == PUNC_COMMA) {
list = new LinkedList<Pair<Expression, String>>();
list.add(new Pair<Expression, String>(expr, alias));
} else {
list = new ArrayList<Pair<Expression, String>>(1);
list.add(new Pair<Expression, String>(expr, alias));
return list;
}
for (; lexer.token() == PUNC_COMMA; list.add(new Pair<Expression, String>(expr, alias))) {
lexer.nextToken();
expr = exprParser.expression();
alias = as();
}
return list;
}
use of com.alibaba.cobar.parser.ast.expression.Expression 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);
}
Aggregations