use of com.alibaba.cobar.parser.util.Pair in project cobar by alibaba.
the class MySQLDALParser method varAssign.
private Object varAssign() throws SQLSyntaxErrorException {
VariableExpression var;
Expression expr;
VariableScope scope = VariableScope.SESSION;
switch(lexer.token()) {
case IDENTIFIER:
boolean explictScope = false;
SpecialIdentifier si = specialIdentifiers.get(lexer.stringValueUppercase());
if (si != null) {
switch(si) {
case TRANSACTION:
return setMTSSetTransactionStatement(null);
case GLOBAL:
scope = VariableScope.GLOBAL;
case SESSION:
case LOCAL:
explictScope = true;
lexer.nextToken();
default:
break;
}
}
if (explictScope && specialIdentifiers.get(lexer.stringValueUppercase()) == SpecialIdentifier.TRANSACTION) {
return setMTSSetTransactionStatement(scope);
}
var = new SysVarPrimary(scope, lexer.stringValue(), lexer.stringValueUppercase());
match(IDENTIFIER);
break;
case SYS_VAR:
var = systemVariale();
break;
case USR_VAR:
var = new UsrDefVarPrimary(lexer.stringValue());
lexer.nextToken();
break;
default:
throw err("unexpected token for SET statement");
}
match(OP_EQUALS, OP_ASSIGN);
expr = exprParser.expression();
return new Pair<VariableExpression, Expression>(var, expr);
}
use of com.alibaba.cobar.parser.util.Pair in project cobar by alibaba.
the class MySQLDALParser method set.
/**
* @return {@link DALSetStatement} or {@link MTSSetTransactionStatement}
*/
@SuppressWarnings("unchecked")
public SQLStatement set() throws SQLSyntaxErrorException {
match(KW_SET);
if (lexer.token() == KW_OPTION) {
lexer.nextToken();
}
if (lexer.token() == IDENTIFIER && SpecialIdentifier.NAMES == specialIdentifiers.get(lexer.stringValueUppercase())) {
if (lexer.nextToken() == KW_DEFAULT) {
lexer.nextToken();
return new DALSetNamesStatement();
}
String charsetName = getStringValue();
String collationName = null;
if (lexer.token() == KW_COLLATE) {
lexer.nextToken();
collationName = getStringValue();
}
return new DALSetNamesStatement(charsetName, collationName);
} else if (lexer.token() == KW_CHARACTER) {
lexer.nextToken();
match(KW_SET);
if (lexer.token() == KW_DEFAULT) {
lexer.nextToken();
return new DALSetCharacterSetStatement();
}
String charsetName = getStringValue();
return new DALSetCharacterSetStatement(charsetName);
}
List<Pair<VariableExpression, Expression>> assignmentList;
Object obj = varAssign();
if (obj instanceof MTSSetTransactionStatement) {
return (MTSSetTransactionStatement) obj;
}
Pair<VariableExpression, Expression> pair = (Pair<VariableExpression, Expression>) obj;
if (lexer.token() != PUNC_COMMA) {
assignmentList = new ArrayList<Pair<VariableExpression, Expression>>(1);
assignmentList.add(pair);
return new DALSetStatement(assignmentList);
}
assignmentList = new LinkedList<Pair<VariableExpression, Expression>>();
assignmentList.add(pair);
for (; lexer.token() == PUNC_COMMA; ) {
lexer.nextToken();
pair = (Pair<VariableExpression, Expression>) varAssign();
assignmentList.add(pair);
}
return new DALSetStatement(assignmentList);
}
use of com.alibaba.cobar.parser.util.Pair 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.util.Pair 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.util.Pair 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