use of com.alibaba.cobar.parser.ast.stmt.dml.DMLSelectStatement 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.stmt.dml.DMLSelectStatement in project cobar by alibaba.
the class MySQLDDLParser method createTable.
/**
* <code>TABLE</code> has been consumed
*/
private DDLCreateTableStatement createTable(boolean temp) throws SQLSyntaxErrorException {
boolean ifNotExists = false;
if (lexer.token() == KW_IF) {
lexer.nextToken();
match(KW_NOT);
match(KW_EXISTS);
ifNotExists = true;
}
Identifier table = identifier();
DDLCreateTableStatement stmt = new DDLCreateTableStatement(temp, ifNotExists, table);
createTableDefs(stmt);
TableOptions options = new TableOptions();
stmt.setTableOptions(options);
tableOptions(options);
DDLCreateTableStatement.SelectOption selectOpt = null;
switch(lexer.token()) {
case KW_IGNORE:
selectOpt = DDLCreateTableStatement.SelectOption.IGNORED;
if (lexer.nextToken() == KW_AS) {
lexer.nextToken();
}
break;
case KW_REPLACE:
selectOpt = DDLCreateTableStatement.SelectOption.REPLACE;
if (lexer.nextToken() == KW_AS) {
lexer.nextToken();
}
break;
case KW_AS:
lexer.nextToken();
case KW_SELECT:
break;
case EOF:
return stmt;
default:
throw new SQLSyntaxErrorException("DDL CREATE TABLE statement not end properly");
}
DMLSelectStatement select = new MySQLDMLSelectParser(lexer, exprParser).select();
stmt.setSelect(selectOpt, select);
match(EOF);
return stmt;
}
use of com.alibaba.cobar.parser.ast.stmt.dml.DMLSelectStatement in project cobar by alibaba.
the class MySQLOutputASTVisitor method visit.
@Override
public void visit(DMLSelectUnionStatement node) {
List<DMLSelectStatement> list = node.getSelectStmtList();
if (list == null || list.isEmpty()) {
throw new IllegalArgumentException("SELECT UNION must have at least one SELECT");
}
final int fstDist = node.getFirstDistinctIndex();
int i = 0;
for (DMLSelectStatement select : list) {
if (i > 0) {
appendable.append(" UNION ");
if (i > fstDist) {
appendable.append("ALL ");
}
}
appendable.append('(');
select.accept(this);
appendable.append(')');
++i;
}
OrderBy order = node.getOrderBy();
if (order != null) {
appendable.append(' ');
order.accept(this);
}
Limit limit = node.getLimit();
if (limit != null) {
appendable.append(' ');
limit.accept(this);
}
}
use of com.alibaba.cobar.parser.ast.stmt.dml.DMLSelectStatement in project cobar by alibaba.
the class MySQLDMLSelectParserTest method testSelectChinese.
public void testSelectChinese() throws SQLSyntaxErrorException {
String sql = "SELect t1.id , t2.* from t1, test.t2 where test.t1.id='中''‘文' and t1.id=test.t2.id";
MySQLLexer lexer = new MySQLLexer(sql);
MySQLDMLSelectParser parser = new MySQLDMLSelectParser(lexer, new MySQLExprParser(lexer));
DMLSelectStatement select = parser.select();
Assert.assertNotNull(select);
String output = output2MySQL(select, sql);
Assert.assertEquals("SELECT t1.id, t2.* FROM t1, test.t2 WHERE test.t1.id = '中\\'‘文' AND t1.id = test.t2.id", output);
}
Aggregations