use of com.alibaba.cobar.parser.ast.fragment.tableref.TableReference 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.fragment.tableref.TableReference in project cobar by alibaba.
the class MySQLDMLParser method trsOrQuery.
/**
* @return type of {@link QueryExpression} or {@link TableReferences}
*/
private Object trsOrQuery() throws SQLSyntaxErrorException {
Object ref;
switch(lexer.token()) {
case KW_SELECT:
DMLSelectStatement select = selectPrimary();
return buildUnionSelect(select);
case PUNC_LEFT_PAREN:
lexer.nextToken();
ref = trsOrQuery();
match(PUNC_RIGHT_PAREN);
if (ref instanceof QueryExpression) {
if (ref instanceof DMLSelectStatement) {
QueryExpression rst = buildUnionSelect((DMLSelectStatement) ref);
if (rst != ref) {
return rst;
}
}
String alias = as();
if (alias != null) {
ref = new SubqueryFactor((QueryExpression) ref, alias);
} else {
return ref;
}
}
// ---- build factor complete---------------
ref = buildTableReference((TableReference) ref);
// ---- build ref complete---------------
break;
default:
ref = tableReference();
}
List<TableReference> list;
if (lexer.token() == PUNC_COMMA) {
list = new LinkedList<TableReference>();
list.add((TableReference) ref);
for (; lexer.token() == PUNC_COMMA; ) {
lexer.nextToken();
ref = tableReference();
list.add((TableReference) ref);
}
return new TableReferences(list);
}
list = new ArrayList<TableReference>(1);
list.add((TableReference) ref);
return new TableReferences(list);
}
use of com.alibaba.cobar.parser.ast.fragment.tableref.TableReference 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.tableref.TableReference 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.tableref.TableReference in project cobar by alibaba.
the class PartitionKeyVisitor method visit.
@Override
public void visit(StraightJoin node) {
TableReference tr1 = node.getLeftTableRef();
TableReference tr2 = node.getRightTableRef();
Expression on = node.getOnCond();
visitChild(1, verdictColumn, verdictGroupFunc, tr1, tr2);
visitChild(2, verdictColumn && isVerdictPassthroughWhere(on), false, on);
}
Aggregations