use of com.alibaba.cobar.parser.ast.expression.primary.RowExpression in project cobar by alibaba.
the class MySQLExprParser method primaryExpression.
private Expression primaryExpression(final String consumed, String consumedUp) throws SQLSyntaxErrorException {
if (consumed != null) {
return startedFromIdentifier(consumed, consumedUp);
}
String tempStr;
String tempStrUp;
StringBuilder tempSb;
Number tempNum;
Expression tempExpr;
Expression tempExpr2;
List<Expression> tempExprList;
switch(lexer.token()) {
case PLACE_HOLDER:
tempStr = lexer.stringValue();
tempStrUp = lexer.stringValueUppercase();
lexer.nextToken();
return createPlaceHolder(tempStr, tempStrUp);
case LITERAL_BIT:
tempStr = lexer.stringValue();
lexer.nextToken();
return new LiteralBitField(null, tempStr).setCacheEvalRst(cacheEvalRst);
case LITERAL_HEX:
LiteralHexadecimal hex = new LiteralHexadecimal(null, lexer.getSQL(), lexer.getOffsetCache(), lexer.getSizeCache(), charset);
lexer.nextToken();
return hex.setCacheEvalRst(cacheEvalRst);
case LITERAL_BOOL_FALSE:
lexer.nextToken();
return new LiteralBoolean(false).setCacheEvalRst(cacheEvalRst);
case LITERAL_BOOL_TRUE:
lexer.nextToken();
return new LiteralBoolean(true).setCacheEvalRst(cacheEvalRst);
case LITERAL_NULL:
lexer.nextToken();
return new LiteralNull().setCacheEvalRst(cacheEvalRst);
case LITERAL_NCHARS:
tempSb = new StringBuilder();
do {
lexer.appendStringContent(tempSb);
} while (lexer.nextToken() == LITERAL_CHARS);
return new LiteralString(null, tempSb.toString(), true).setCacheEvalRst(cacheEvalRst);
case LITERAL_CHARS:
tempSb = new StringBuilder();
do {
lexer.appendStringContent(tempSb);
} while (lexer.nextToken() == LITERAL_CHARS);
return new LiteralString(null, tempSb.toString(), false).setCacheEvalRst(cacheEvalRst);
case LITERAL_NUM_PURE_DIGIT:
tempNum = lexer.integerValue();
lexer.nextToken();
return new LiteralNumber(tempNum).setCacheEvalRst(cacheEvalRst);
case LITERAL_NUM_MIX_DIGIT:
tempNum = lexer.decimalValue();
lexer.nextToken();
return new LiteralNumber(tempNum).setCacheEvalRst(cacheEvalRst);
case QUESTION_MARK:
int index = lexer.paramIndex();
lexer.nextToken();
return createParam(index);
case KW_CASE:
lexer.nextToken();
return caseWhenExpression();
case KW_INTERVAL:
lexer.nextToken();
return intervalExpression();
case KW_EXISTS:
lexer.nextToken();
match(PUNC_LEFT_PAREN);
tempExpr = subQuery();
match(PUNC_RIGHT_PAREN);
return new ExistsPrimary((QueryExpression) tempExpr).setCacheEvalRst(cacheEvalRst);
case USR_VAR:
tempStr = lexer.stringValue();
tempExpr = new UsrDefVarPrimary(tempStr).setCacheEvalRst(cacheEvalRst);
if (lexer.nextToken() == OP_ASSIGN) {
lexer.nextToken();
tempExpr2 = expression();
return new AssignmentExpression(tempExpr, tempExpr2);
}
return tempExpr;
case SYS_VAR:
return systemVariale();
case KW_MATCH:
lexer.nextToken();
return matchExpression();
case PUNC_LEFT_PAREN:
lexer.nextToken();
if (lexer.token() == KW_SELECT) {
tempExpr = subQuery();
match(PUNC_RIGHT_PAREN);
return tempExpr;
}
tempExpr = expression();
switch(lexer.token()) {
case PUNC_RIGHT_PAREN:
lexer.nextToken();
return tempExpr;
case PUNC_COMMA:
lexer.nextToken();
tempExprList = new LinkedList<Expression>();
tempExprList.add(tempExpr);
tempExprList = expressionList(tempExprList);
return new RowExpression(tempExprList).setCacheEvalRst(cacheEvalRst);
default:
throw err("unexpected token: " + lexer.token());
}
case KW_UTC_DATE:
lexer.nextToken();
if (lexer.token() == PUNC_LEFT_PAREN) {
lexer.nextToken();
match(PUNC_RIGHT_PAREN);
}
return new UtcDate(null).setCacheEvalRst(cacheEvalRst);
case KW_UTC_TIME:
lexer.nextToken();
if (lexer.token() == PUNC_LEFT_PAREN) {
lexer.nextToken();
match(PUNC_RIGHT_PAREN);
}
return new UtcTime(null).setCacheEvalRst(cacheEvalRst);
case KW_UTC_TIMESTAMP:
lexer.nextToken();
if (lexer.token() == PUNC_LEFT_PAREN) {
lexer.nextToken();
match(PUNC_RIGHT_PAREN);
}
return new UtcTimestamp(null).setCacheEvalRst(cacheEvalRst);
case KW_CURRENT_DATE:
lexer.nextToken();
if (lexer.token() == PUNC_LEFT_PAREN) {
lexer.nextToken();
match(PUNC_RIGHT_PAREN);
}
return new Curdate().setCacheEvalRst(cacheEvalRst);
case KW_CURRENT_TIME:
lexer.nextToken();
if (lexer.token() == PUNC_LEFT_PAREN) {
lexer.nextToken();
match(PUNC_RIGHT_PAREN);
}
return new Curtime().setCacheEvalRst(cacheEvalRst);
case KW_CURRENT_TIMESTAMP:
case KW_LOCALTIME:
case KW_LOCALTIMESTAMP:
lexer.nextToken();
if (lexer.token() == PUNC_LEFT_PAREN) {
lexer.nextToken();
match(PUNC_RIGHT_PAREN);
}
return new Now().setCacheEvalRst(cacheEvalRst);
case KW_CURRENT_USER:
lexer.nextToken();
if (lexer.token() == PUNC_LEFT_PAREN) {
lexer.nextToken();
match(PUNC_RIGHT_PAREN);
}
return new CurrentUser().setCacheEvalRst(cacheEvalRst);
case KW_DEFAULT:
if (lexer.nextToken() == PUNC_LEFT_PAREN) {
return ordinaryFunction(lexer.stringValue(), lexer.stringValueUppercase());
}
return new DefaultValue().setCacheEvalRst(cacheEvalRst);
case KW_DATABASE:
case KW_IF:
case KW_INSERT:
case KW_LEFT:
case KW_REPEAT:
case KW_REPLACE:
case KW_RIGHT:
case KW_SCHEMA:
case KW_VALUES:
tempStr = lexer.stringValue();
tempStrUp = lexer.stringValueUppercase();
String tempStrUp2 = MySQLToken.keyWordToString(lexer.token());
if (!tempStrUp2.equals(tempStrUp)) {
tempStrUp = tempStr = tempStrUp2;
}
if (lexer.nextToken() == PUNC_LEFT_PAREN) {
return ordinaryFunction(tempStr, tempStrUp);
}
throw err("keyword not followed by '(' is not expression: " + tempStr);
case KW_MOD:
lexer.nextToken();
match(PUNC_LEFT_PAREN);
tempExpr = expression();
match(PUNC_COMMA);
tempExpr2 = expression();
match(PUNC_RIGHT_PAREN);
return new ArithmeticModExpression(tempExpr, tempExpr2).setCacheEvalRst(cacheEvalRst);
case KW_CHAR:
lexer.nextToken();
match(PUNC_LEFT_PAREN);
return functionChar();
case KW_CONVERT:
lexer.nextToken();
match(PUNC_LEFT_PAREN);
return functionConvert();
case IDENTIFIER:
tempStr = lexer.stringValue();
tempStrUp = lexer.stringValueUppercase();
lexer.nextToken();
return startedFromIdentifier(tempStr, tempStrUp);
case OP_ASTERISK:
lexer.nextToken();
return new Wildcard(null).setCacheEvalRst(cacheEvalRst);
default:
throw err("unrecognized token as first token of primary: " + lexer.token());
}
}
use of com.alibaba.cobar.parser.ast.expression.primary.RowExpression in project cobar by alibaba.
the class MySQLOutputASTVisitor method visit.
@Override
public void visit(DMLInsertStatement node) {
appendable.append("INSERT ");
switch(node.getMode()) {
case DELAY:
appendable.append("DELAYED ");
break;
case HIGH:
appendable.append("HIGH_PRIORITY ");
break;
case LOW:
appendable.append("LOW_PRIORITY ");
break;
case UNDEF:
break;
default:
throw new IllegalArgumentException("unknown mode for INSERT: " + node.getMode());
}
if (node.isIgnore())
appendable.append("IGNORE ");
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 INSERT");
}
} else {
select.accept(this);
}
List<Pair<Identifier, Expression>> dup = node.getDuplicateUpdate();
if (dup != null && !dup.isEmpty()) {
appendable.append(" ON DUPLICATE KEY UPDATE ");
boolean isFst = true;
for (Pair<Identifier, Expression> p : dup) {
if (isFst)
isFst = false;
else
appendable.append(", ");
p.getKey().accept(this);
appendable.append(" = ");
p.getValue().accept(this);
}
}
}
use of com.alibaba.cobar.parser.ast.expression.primary.RowExpression in project cobar by alibaba.
the class MySQLDMLReplaceParser method replace.
/**
* nothing has been pre-consumed <code><pre>
* 'REPLACE' ('LOW_PRIORITY' | 'DELAYED')? ('INTO')? tableName
* ( 'SET' colName ('='|':=') (expr|'DEFAULT') (',' colName ('='|':=') (expr|'DEFAULT'))*
* | '(' ( colName (','colName)* ')' ( '(' 'SELECT' ... ')'
* | 'SELECT' ...
* |('VALUES'|'VALUE') value ( ',' value )*
* )
* | 'SELECT' ... ')'
* )
* | 'SELECT' ...
* |('VALUES'|'VALUE') value ( ',' value )*
* )
* value := '(' (expr|'DEFAULT') ( ',' (expr|'DEFAULT'))* ')'
* </pre></code>
*/
public DMLReplaceStatement replace() throws SQLSyntaxErrorException {
match(KW_REPLACE);
DMLReplaceStatement.ReplaceMode mode = DMLReplaceStatement.ReplaceMode.UNDEF;
switch(lexer.token()) {
case KW_LOW_PRIORITY:
lexer.nextToken();
mode = DMLReplaceStatement.ReplaceMode.LOW;
break;
case KW_DELAYED:
lexer.nextToken();
mode = DMLReplaceStatement.ReplaceMode.DELAY;
break;
}
if (lexer.token() == KW_INTO) {
lexer.nextToken();
}
Identifier table = identifier();
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));
return new DMLReplaceStatement(mode, table, columnNameList, rowList);
case IDENTIFIER:
if (!"VALUE".equals(lexer.stringValueUppercase())) {
break;
}
case KW_VALUES:
lexer.nextToken();
columnNameList = null;
rowList = rowList();
return new DMLReplaceStatement(mode, table, columnNameList, rowList);
case KW_SELECT:
columnNameList = null;
select = select();
return new DMLReplaceStatement(mode, table, columnNameList, select);
case PUNC_LEFT_PAREN:
switch(lexer.nextToken()) {
case PUNC_LEFT_PAREN:
case KW_SELECT:
columnNameList = null;
select = selectPrimary();
match(PUNC_RIGHT_PAREN);
return new DMLReplaceStatement(mode, table, columnNameList, select);
}
columnNameList = idList();
match(PUNC_RIGHT_PAREN);
switch(lexer.token()) {
case PUNC_LEFT_PAREN:
case KW_SELECT:
select = selectPrimary();
return new DMLReplaceStatement(mode, table, columnNameList, select);
case KW_VALUES:
lexer.nextToken();
break;
default:
matchIdentifier("VALUE");
}
rowList = rowList();
return new DMLReplaceStatement(mode, table, columnNameList, rowList);
}
throw err("unexpected token for replace: " + lexer.token());
}
use of com.alibaba.cobar.parser.ast.expression.primary.RowExpression in project cobar by alibaba.
the class MySQLExprParser method startedFromIdentifier.
/**
* last token consumed is {@link MySQLToken#IDENTIFIER}, MUST NOT be
* <code>null</code>
*/
private Expression startedFromIdentifier(final String consumed, String consumedUp) throws SQLSyntaxErrorException {
Expression tempExpr;
Expression tempExpr2;
List<Expression> tempExprList;
String tempStr;
StringBuilder tempSb;
boolean tempGroupDistinct;
switch(lexer.token()) {
case PUNC_DOT:
for (tempExpr = new Identifier(null, consumed, consumedUp).setCacheEvalRst(cacheEvalRst); lexer.token() == PUNC_DOT; ) {
switch(lexer.nextToken()) {
case IDENTIFIER:
tempExpr = new Identifier((Identifier) tempExpr, lexer.stringValue(), lexer.stringValueUppercase()).setCacheEvalRst(cacheEvalRst);
lexer.nextToken();
break;
case OP_ASTERISK:
lexer.nextToken();
return new Wildcard((Identifier) tempExpr).setCacheEvalRst(cacheEvalRst);
default:
throw err("expect IDENTIFIER or '*' after '.', but is " + lexer.token());
}
}
return tempExpr;
case LITERAL_BIT:
if (consumed.charAt(0) != '_') {
return new Identifier(null, consumed, consumedUp).setCacheEvalRst(cacheEvalRst);
}
tempStr = lexer.stringValue();
lexer.nextToken();
return new LiteralBitField(consumed, tempStr).setCacheEvalRst(cacheEvalRst);
case LITERAL_HEX:
if (consumed.charAt(0) != '_') {
return new Identifier(null, consumed, consumedUp).setCacheEvalRst(cacheEvalRst);
}
LiteralHexadecimal hex = new LiteralHexadecimal(consumed, lexer.getSQL(), lexer.getOffsetCache(), lexer.getSizeCache(), charset);
lexer.nextToken();
return hex.setCacheEvalRst(cacheEvalRst);
case LITERAL_CHARS:
if (consumed.charAt(0) != '_') {
return new Identifier(null, consumed, consumedUp).setCacheEvalRst(cacheEvalRst);
}
tempSb = new StringBuilder();
do {
lexer.appendStringContent(tempSb);
} while (lexer.nextToken() == LITERAL_CHARS);
return new LiteralString(consumed, tempSb.toString(), false).setCacheEvalRst(cacheEvalRst);
case PUNC_LEFT_PAREN:
consumedUp = Identifier.unescapeName(consumedUp);
switch(functionManager.getParsingStrategy(consumedUp)) {
case GET_FORMAT:
// GET_FORMAT({DATE|TIME|DATETIME},
// {'EUR'|'USA'|'JIS'|'ISO'|'INTERNAL'})
lexer.nextToken();
int gfi = matchIdentifier("DATE", "TIME", "DATETIME", "TIMESTAMP");
match(PUNC_COMMA);
Expression getFormatArg = expression();
match(PUNC_RIGHT_PAREN);
switch(gfi) {
case 0:
return new GetFormat(GetFormat.FormatType.DATE, getFormatArg);
case 1:
return new GetFormat(GetFormat.FormatType.TIME, getFormatArg);
case 2:
case 3:
return new GetFormat(GetFormat.FormatType.DATETIME, getFormatArg);
}
throw err("unexpected format type for GET_FORMAT()");
case CAST:
lexer.nextToken();
tempExpr = expression();
match(KW_AS);
Pair<String, Pair<Expression, Expression>> type = type4specialFunc();
match(PUNC_RIGHT_PAREN);
Pair<Expression, Expression> info = type.getValue();
if (info != null) {
return new Cast(tempExpr, type.getKey(), info.getKey(), info.getValue()).setCacheEvalRst(cacheEvalRst);
} else {
return new Cast(tempExpr, type.getKey(), null, null).setCacheEvalRst(cacheEvalRst);
}
case POSITION:
lexer.nextToken();
tempExprList = new ArrayList<Expression>(2);
tempExprList.add(expression());
match(KW_IN);
tempExprList.add(expression());
match(PUNC_RIGHT_PAREN);
return new Locate(tempExprList).setCacheEvalRst(cacheEvalRst);
case SUBSTRING:
lexer.nextToken();
tempExprList = new ArrayList<Expression>(3);
tempExprList.add(expression());
match(PUNC_COMMA, KW_FROM);
tempExprList.add(expression());
switch(lexer.token()) {
case PUNC_COMMA:
case KW_FOR:
lexer.nextToken();
tempExprList.add(expression());
default:
match(PUNC_RIGHT_PAREN);
}
return new Substring(tempExprList).setCacheEvalRst(cacheEvalRst);
case ROW:
lexer.nextToken();
tempExprList = expressionList(new LinkedList<Expression>());
return new RowExpression(tempExprList).setCacheEvalRst(cacheEvalRst);
case TRIM:
Direction direction;
switch(lexer.nextToken()) {
case KW_BOTH:
lexer.nextToken();
direction = Direction.BOTH;
break;
case KW_LEADING:
lexer.nextToken();
direction = Direction.LEADING;
break;
case KW_TRAILING:
lexer.nextToken();
direction = Direction.TRAILING;
break;
default:
direction = Direction.DEFAULT;
}
if (direction == Direction.DEFAULT) {
tempExpr = expression();
if (lexer.token() == KW_FROM) {
lexer.nextToken();
tempExpr2 = expression();
match(PUNC_RIGHT_PAREN);
return new Trim(direction, tempExpr, tempExpr2).setCacheEvalRst(cacheEvalRst);
}
match(PUNC_RIGHT_PAREN);
return new Trim(direction, null, tempExpr).setCacheEvalRst(cacheEvalRst);
}
if (lexer.token() == KW_FROM) {
lexer.nextToken();
tempExpr = expression();
match(PUNC_RIGHT_PAREN);
return new Trim(direction, null, tempExpr).setCacheEvalRst(cacheEvalRst);
}
tempExpr = expression();
match(KW_FROM);
tempExpr2 = expression();
match(PUNC_RIGHT_PAREN);
return new Trim(direction, tempExpr, tempExpr2).setCacheEvalRst(cacheEvalRst);
case AVG:
if (lexer.nextToken() == KW_DISTINCT) {
tempGroupDistinct = true;
lexer.nextToken();
} else {
tempGroupDistinct = false;
}
tempExpr = expression();
match(PUNC_RIGHT_PAREN);
return new Avg(tempExpr, tempGroupDistinct).setCacheEvalRst(cacheEvalRst);
case MAX:
if (lexer.nextToken() == KW_DISTINCT) {
tempGroupDistinct = true;
lexer.nextToken();
} else {
tempGroupDistinct = false;
}
tempExpr = expression();
match(PUNC_RIGHT_PAREN);
return new Max(tempExpr, tempGroupDistinct).setCacheEvalRst(cacheEvalRst);
case MIN:
if (lexer.nextToken() == KW_DISTINCT) {
tempGroupDistinct = true;
lexer.nextToken();
} else {
tempGroupDistinct = false;
}
tempExpr = expression();
match(PUNC_RIGHT_PAREN);
return new Min(tempExpr, tempGroupDistinct).setCacheEvalRst(cacheEvalRst);
case SUM:
if (lexer.nextToken() == KW_DISTINCT) {
tempGroupDistinct = true;
lexer.nextToken();
} else {
tempGroupDistinct = false;
}
tempExpr = expression();
match(PUNC_RIGHT_PAREN);
return new Sum(tempExpr, tempGroupDistinct).setCacheEvalRst(cacheEvalRst);
case COUNT:
if (lexer.nextToken() == KW_DISTINCT) {
lexer.nextToken();
tempExprList = expressionList(new LinkedList<Expression>());
return new Count(tempExprList).setCacheEvalRst(cacheEvalRst);
}
tempExpr = expression();
match(PUNC_RIGHT_PAREN);
return new Count(tempExpr).setCacheEvalRst(cacheEvalRst);
case GROUP_CONCAT:
if (lexer.nextToken() == KW_DISTINCT) {
lexer.nextToken();
tempGroupDistinct = true;
} else {
tempGroupDistinct = false;
}
for (tempExprList = new LinkedList<Expression>(); ; ) {
tempExpr = expression();
tempExprList.add(tempExpr);
if (lexer.token() == PUNC_COMMA) {
lexer.nextToken();
} else {
break;
}
}
boolean isDesc = false;
List<Expression> appendedColumnNames = null;
// order by
tempExpr = null;
// literalChars
tempStr = null;
switch(lexer.token()) {
case KW_ORDER:
lexer.nextToken();
match(KW_BY);
tempExpr = expression();
if (lexer.token() == KW_ASC) {
lexer.nextToken();
} else if (lexer.token() == KW_DESC) {
isDesc = true;
lexer.nextToken();
}
for (appendedColumnNames = new LinkedList<Expression>(); lexer.token() == PUNC_COMMA; ) {
lexer.nextToken();
appendedColumnNames.add(expression());
}
if (lexer.token() != KW_SEPARATOR) {
break;
}
case KW_SEPARATOR:
lexer.nextToken();
tempSb = new StringBuilder();
lexer.appendStringContent(tempSb);
tempStr = LiteralString.getUnescapedString(tempSb.toString());
match(LITERAL_CHARS);
break;
}
match(PUNC_RIGHT_PAREN);
return new GroupConcat(tempGroupDistinct, tempExprList, tempExpr, isDesc, appendedColumnNames, tempStr).setCacheEvalRst(cacheEvalRst);
case CHAR:
lexer.nextToken();
return functionChar();
case CONVERT:
lexer.nextToken();
return functionConvert();
case EXTRACT:
lexer.nextToken();
return extract();
case TIMESTAMPDIFF:
lexer.nextToken();
return timestampdiff();
case TIMESTAMPADD:
lexer.nextToken();
return timestampadd();
case _ORDINARY:
return ordinaryFunction(consumed, consumedUp);
case _DEFAULT:
return new Identifier(null, consumed, consumedUp).setCacheEvalRst(cacheEvalRst);
default:
throw err("unexpected function parsing strategy for id of " + consumed);
}
default:
return new Identifier(null, consumed, consumedUp).setCacheEvalRst(cacheEvalRst);
}
}
use of com.alibaba.cobar.parser.ast.expression.primary.RowExpression 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());
}
Aggregations