use of com.alibaba.cobar.parser.ast.fragment.tableref.TableReferences 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.TableReferences 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.TableReferences 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);
}
Aggregations