use of com.alibaba.cobar.parser.ast.fragment.Limit in project cobar by alibaba.
the class MySQLOutputASTVisitor method visit.
@Override
public void visit(DMLDeleteStatement node) {
appendable.append("DELETE ");
if (node.isLowPriority())
appendable.append("LOW_PRIORITY ");
if (node.isQuick())
appendable.append("QUICK ");
if (node.isIgnore())
appendable.append("IGNORE ");
TableReferences tableRefs = node.getTableRefs();
if (tableRefs == null) {
appendable.append("FROM ");
node.getTableNames().get(0).accept(this);
} else {
printList(node.getTableNames());
appendable.append(" FROM ");
node.getTableRefs().accept(this);
}
Expression where = node.getWhereCondition();
if (where != null) {
appendable.append(" WHERE ");
where.accept(this);
}
OrderBy orderBy = node.getOrderBy();
if (orderBy != null) {
appendable.append(' ');
orderBy.accept(this);
}
Limit limit = node.getLimit();
if (limit != null) {
appendable.append(' ');
limit.accept(this);
}
}
use of com.alibaba.cobar.parser.ast.fragment.Limit in project cobar by alibaba.
the class MySQLDMLParserTest method testLimit.
public void testLimit() throws SQLSyntaxErrorException {
String sql = "limit 1,2";
MySQLLexer lexer = new MySQLLexer(sql);
MySQLDMLParser parser = getDMLParser(lexer);
Limit limit = parser.limit();
String output = output2MySQL(limit, sql);
Assert.assertEquals(1, limit.getOffset());
Assert.assertEquals(2, limit.getSize());
Assert.assertEquals("LIMIT 1, 2", output);
sql = "limit 1,?";
lexer = new MySQLLexer(sql);
parser = getDMLParser(lexer);
limit = parser.limit();
output = output2MySQL(limit, sql);
Assert.assertEquals(1, limit.getOffset());
Assert.assertEquals(new ParamMarker(1), limit.getSize());
Assert.assertEquals("LIMIT 1, ?", output);
sql = "limit ?,9";
lexer = new MySQLLexer(sql);
parser = getDMLParser(lexer);
limit = parser.limit();
output = output2MySQL(limit, sql);
Assert.assertEquals(new ParamMarker(1), limit.getOffset());
Assert.assertEquals(9, limit.getSize());
Assert.assertEquals("LIMIT ?, 9", output);
sql = "limit ?,?";
lexer = new MySQLLexer(sql);
parser = getDMLParser(lexer);
limit = parser.limit();
output = output2MySQL(limit, sql);
Assert.assertEquals(new ParamMarker(1), limit.getOffset());
Assert.assertEquals(new ParamMarker(2), limit.getSize());
Assert.assertEquals("LIMIT ?, ?", output);
sql = "limit ? d";
lexer = new MySQLLexer(sql);
parser = getDMLParser(lexer);
limit = parser.limit();
output = output2MySQL(limit, sql);
Assert.assertEquals(0, limit.getOffset());
Assert.assertEquals(new ParamMarker(1), limit.getSize());
Assert.assertEquals("LIMIT 0, ?", output);
sql = "limit 9 f";
lexer = new MySQLLexer(sql);
parser = getDMLParser(lexer);
limit = parser.limit();
output = output2MySQL(limit, sql);
Assert.assertEquals(0, limit.getOffset());
Assert.assertEquals(9, limit.getSize());
Assert.assertEquals("LIMIT 0, 9", output);
sql = "limit 9 ofFset 0";
lexer = new MySQLLexer(sql);
parser = getDMLParser(lexer);
limit = parser.limit();
output = output2MySQL(limit, sql);
Assert.assertEquals(0, limit.getOffset());
Assert.assertEquals(9, limit.getSize());
Assert.assertEquals("LIMIT 0, 9", output);
sql = "limit ? offset 0";
lexer = new MySQLLexer(sql);
parser = getDMLParser(lexer);
limit = parser.limit();
output = output2MySQL(limit, sql);
Assert.assertEquals(0, limit.getOffset());
Assert.assertEquals(new ParamMarker(1), limit.getSize());
Assert.assertEquals("LIMIT 0, ?", output);
sql = "limit ? offset ?";
lexer = new MySQLLexer(sql);
parser = getDMLParser(lexer);
limit = parser.limit();
output = output2MySQL(limit, sql);
Assert.assertEquals(new ParamMarker(2), limit.getOffset());
Assert.assertEquals(new ParamMarker(1), limit.getSize());
Assert.assertEquals("LIMIT ?, ?", output);
sql = "limit 9 offset ?";
lexer = new MySQLLexer(sql);
parser = getDMLParser(lexer);
limit = parser.limit();
output = output2MySQL(limit, sql);
Assert.assertEquals(new ParamMarker(1), limit.getOffset());
Assert.assertEquals(9, limit.getSize());
Assert.assertEquals("LIMIT ?, 9", output);
}
use of com.alibaba.cobar.parser.ast.fragment.Limit 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.fragment.Limit in project cobar by alibaba.
the class MySQLDMLDeleteParser method delete.
/**
* first token is {@link MySQLToken#KW_DELETE} <code><pre>
* 'DELETE' 'LOW_PRIORITY'? 'QUICK'? 'IGNORE'? (
* 'FROM' tid ( (',' tid)* 'USING' table_refs ('WHERE' cond)?
* | ('WHERE' cond)? ('ORDER' 'BY' ids)? ('LIMIT' count)? ) // single table
* | tid (',' tid)* 'FROM' table_refs ('WHERE' cond)? )
* </pre></code>
*/
public DMLDeleteStatement delete() throws SQLSyntaxErrorException {
match(KW_DELETE);
boolean lowPriority = false;
boolean quick = false;
boolean ignore = false;
loopOpt: for (; ; lexer.nextToken()) {
switch(lexer.token()) {
case KW_LOW_PRIORITY:
lowPriority = true;
break;
case KW_IGNORE:
ignore = true;
break;
case IDENTIFIER:
SpecialIdentifier si = specialIdentifiers.get(lexer.stringValueUppercase());
if (SpecialIdentifier.QUICK == si) {
quick = true;
break;
}
default:
break loopOpt;
}
}
List<Identifier> tempList;
TableReferences tempRefs;
Expression tempWhere;
if (lexer.token() == KW_FROM) {
lexer.nextToken();
Identifier id = identifier();
tempList = new ArrayList<Identifier>(1);
tempList.add(id);
switch(lexer.token()) {
case PUNC_COMMA:
tempList = buildIdList(id);
case KW_USING:
lexer.nextToken();
tempRefs = tableRefs();
if (lexer.token() == KW_WHERE) {
lexer.nextToken();
tempWhere = exprParser.expression();
return new DMLDeleteStatement(lowPriority, quick, ignore, tempList, tempRefs, tempWhere);
}
return new DMLDeleteStatement(lowPriority, quick, ignore, tempList, tempRefs);
case KW_WHERE:
case KW_ORDER:
case KW_LIMIT:
break;
default:
return new DMLDeleteStatement(lowPriority, quick, ignore, id);
}
tempWhere = null;
OrderBy orderBy = null;
Limit limit = null;
if (lexer.token() == KW_WHERE) {
lexer.nextToken();
tempWhere = exprParser.expression();
}
if (lexer.token() == KW_ORDER) {
orderBy = orderBy();
}
if (lexer.token() == KW_LIMIT) {
limit = limit();
}
return new DMLDeleteStatement(lowPriority, quick, ignore, id, tempWhere, orderBy, limit);
}
tempList = idList();
match(KW_FROM);
tempRefs = tableRefs();
if (lexer.token() == KW_WHERE) {
lexer.nextToken();
tempWhere = exprParser.expression();
return new DMLDeleteStatement(lowPriority, quick, ignore, tempList, tempRefs, tempWhere);
}
return new DMLDeleteStatement(lowPriority, quick, ignore, tempList, tempRefs);
}
use of com.alibaba.cobar.parser.ast.fragment.Limit 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);
}
}
Aggregations