use of com.alibaba.cobar.parser.ast.expression.misc.QueryExpression in project cobar by alibaba.
the class MySQLExprParser method anyAllExpression.
private Expression anyAllExpression() throws SQLSyntaxErrorException {
QueryExpression subquery = null;
switch(lexer.token()) {
case KW_ALL:
lexer.nextToken();
match(PUNC_LEFT_PAREN);
subquery = subQuery();
match(PUNC_RIGHT_PAREN);
return new SubqueryAllExpression(subquery).setCacheEvalRst(cacheEvalRst);
default:
int matchIndex = equalsIdentifier("SOME", "ANY");
if (matchIndex < 0) {
return bitOrExpression(null, null);
}
String consumed = lexer.stringValue();
String consumedUp = lexer.stringValueUppercase();
if (lexer.nextToken() == PUNC_LEFT_PAREN) {
lexer.nextToken();
subquery = subQuery();
match(PUNC_RIGHT_PAREN);
return new SubqueryAnyExpression(subquery).setCacheEvalRst(cacheEvalRst);
}
return bitOrExpression(consumed, consumedUp);
}
}
use of com.alibaba.cobar.parser.ast.expression.misc.QueryExpression in project cobar by alibaba.
the class MySQLDMLInsertParser method insert.
/**
* nothing has been pre-consumed <code><pre>
* 'INSERT' ('LOW_PRIORITY'|'DELAYED'|'HIGH_PRIORITY')? 'IGNORE'? 'INTO'? tbname
* ( 'SET' colName ('='|':=') (expr|'DEFAULT') (',' colName ('='|':=') (expr|'DEFAULT'))*
* | '(' ( colName (',' colName)* ')' ( ('VALUES'|'VALUE') value (',' value)*
* | '(' 'SELECT' ... ')'
* | 'SELECT' ...
* )
* | 'SELECT' ... ')'
* )
* |('VALUES'|'VALUE') value ( ',' value )*
* | 'SELECT' ...
* )
* ( 'ON' 'DUPLICATE' 'KEY' 'UPDATE' colName ('='|':=') expr ( ',' colName ('='|':=') expr)* )?
*
* value := '(' (expr|'DEFAULT') ( ',' (expr|'DEFAULT'))* ')'
* </pre></code>
*/
public DMLInsertStatement insert() throws SQLSyntaxErrorException {
match(KW_INSERT);
DMLInsertStatement.InsertMode mode = DMLInsertStatement.InsertMode.UNDEF;
boolean ignore = false;
switch(lexer.token()) {
case KW_LOW_PRIORITY:
lexer.nextToken();
mode = DMLInsertStatement.InsertMode.LOW;
break;
case KW_DELAYED:
lexer.nextToken();
mode = DMLInsertStatement.InsertMode.DELAY;
break;
case KW_HIGH_PRIORITY:
lexer.nextToken();
mode = DMLInsertStatement.InsertMode.HIGH;
break;
}
if (lexer.token() == KW_IGNORE) {
ignore = true;
lexer.nextToken();
}
if (lexer.token() == KW_INTO) {
lexer.nextToken();
}
Identifier table = identifier();
List<Pair<Identifier, Expression>> dupUpdate;
List<Identifier> columnNameList;
List<RowExpression> rowList;
QueryExpression select;
List<Expression> tempRowValue;
switch(lexer.token()) {
case KW_SET:
lexer.nextToken();
columnNameList = new LinkedList<Identifier>();
tempRowValue = new LinkedList<Expression>();
for (; ; lexer.nextToken()) {
Identifier id = identifier();
match(OP_EQUALS, OP_ASSIGN);
Expression expr = exprParser.expression();
columnNameList.add(id);
tempRowValue.add(expr);
if (lexer.token() != PUNC_COMMA) {
break;
}
}
rowList = new ArrayList<RowExpression>(1);
rowList.add(new RowExpression(tempRowValue));
dupUpdate = onDuplicateUpdate();
return new DMLInsertStatement(mode, ignore, table, columnNameList, rowList, dupUpdate);
case IDENTIFIER:
if (!"VALUE".equals(lexer.stringValueUppercase())) {
break;
}
case KW_VALUES:
lexer.nextToken();
columnNameList = null;
rowList = rowList();
dupUpdate = onDuplicateUpdate();
return new DMLInsertStatement(mode, ignore, table, columnNameList, rowList, dupUpdate);
case KW_SELECT:
columnNameList = null;
select = select();
dupUpdate = onDuplicateUpdate();
return new DMLInsertStatement(mode, ignore, table, columnNameList, select, dupUpdate);
case PUNC_LEFT_PAREN:
switch(lexer.nextToken()) {
case PUNC_LEFT_PAREN:
case KW_SELECT:
columnNameList = null;
select = selectPrimary();
match(PUNC_RIGHT_PAREN);
dupUpdate = onDuplicateUpdate();
return new DMLInsertStatement(mode, ignore, table, columnNameList, select, dupUpdate);
}
columnNameList = idList();
match(PUNC_RIGHT_PAREN);
switch(lexer.token()) {
case PUNC_LEFT_PAREN:
case KW_SELECT:
select = selectPrimary();
dupUpdate = onDuplicateUpdate();
return new DMLInsertStatement(mode, ignore, table, columnNameList, select, dupUpdate);
case KW_VALUES:
lexer.nextToken();
break;
default:
matchIdentifier("VALUE");
}
rowList = rowList();
dupUpdate = onDuplicateUpdate();
return new DMLInsertStatement(mode, ignore, table, columnNameList, rowList, dupUpdate);
}
throw err("unexpected token for insert: " + lexer.token());
}
use of com.alibaba.cobar.parser.ast.expression.misc.QueryExpression 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.expression.misc.QueryExpression in project cobar by alibaba.
the class MySQLExprParser method rightOprandOfIn.
/**
* @return {@link QueryExpression} or {@link InExpressionList}
*/
private Expression rightOprandOfIn() throws SQLSyntaxErrorException {
match(PUNC_LEFT_PAREN);
if (KW_SELECT == lexer.token()) {
QueryExpression subq = subQuery();
match(PUNC_RIGHT_PAREN);
return subq;
}
return new InExpressionList(expressionList(new LinkedList<Expression>())).setCacheEvalRst(cacheEvalRst);
}
use of com.alibaba.cobar.parser.ast.expression.misc.QueryExpression in project cobar by alibaba.
the class MySQLOutputASTVisitor method visit.
@Override
public void visit(DMLReplaceStatement node) {
appendable.append("REPLACE ");
switch(node.getMode()) {
case DELAY:
appendable.append("DELAYED ");
break;
case LOW:
appendable.append("LOW_PRIORITY ");
break;
case UNDEF:
break;
default:
throw new IllegalArgumentException("unknown mode for INSERT: " + node.getMode());
}
appendable.append("INTO ");
node.getTable().accept(this);
appendable.append(' ');
List<Identifier> cols = node.getColumnNameList();
if (cols != null && !cols.isEmpty()) {
appendable.append('(');
printList(cols);
appendable.append(") ");
}
QueryExpression select = node.getSelect();
if (select == null) {
appendable.append("VALUES ");
List<RowExpression> rows = node.getRowList();
if (rows != null && !rows.isEmpty()) {
boolean isFst = true;
for (RowExpression row : rows) {
if (row == null || row.getRowExprList().isEmpty())
continue;
if (isFst)
isFst = false;
else
appendable.append(", ");
appendable.append('(');
printList(row.getRowExprList());
appendable.append(')');
}
} else {
throw new IllegalArgumentException("at least one row for REPLACE");
}
} else {
select.accept(this);
}
}
Aggregations