use of com.alibaba.cobar.parser.ast.expression.Expression 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.ast.expression.Expression 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.ast.expression.Expression in project cobar by alibaba.
the class MySQLDALParser method showColumns.
/**
* First token is {@link SpecialIdentifier#COLUMNS}
*
* <pre>
* SHOW [FULL] <code>COLUMNS {FROM | IN} tbl_name [{FROM | IN} db_name] [LIKE 'pattern' | WHERE expr] </code>
* </pre>
*/
private ShowColumns showColumns(boolean full) throws SQLSyntaxErrorException {
lexer.nextToken();
match(KW_FROM, KW_IN);
Identifier table = identifier();
Identifier database = null;
switch(lexer.token()) {
case KW_FROM:
case KW_IN:
lexer.nextToken();
database = identifier();
}
switch(lexer.token()) {
case KW_LIKE:
String like = like();
return new ShowColumns(full, table, database, like);
case KW_WHERE:
Expression where = where();
return new ShowColumns(full, table, database, where);
}
return new ShowColumns(full, table, database);
}
use of com.alibaba.cobar.parser.ast.expression.Expression 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.Expression in project cobar by alibaba.
the class MySQLDMLInsertReplaceParser method rowList.
protected List<RowExpression> rowList() throws SQLSyntaxErrorException {
List<RowExpression> valuesList;
List<Expression> tempRowValue = rowValue();
if (lexer.token() == PUNC_COMMA) {
valuesList = new LinkedList<RowExpression>();
valuesList.add(new RowExpression(tempRowValue));
for (; lexer.token() == PUNC_COMMA; ) {
lexer.nextToken();
tempRowValue = rowValue();
valuesList.add(new RowExpression(tempRowValue));
}
} else {
valuesList = new ArrayList<RowExpression>(1);
valuesList.add(new RowExpression(tempRowValue));
}
return valuesList;
}
Aggregations