improved oracle sql parser support fetch clause. issue #1448

This commit is contained in:
wenshao
2016-10-14 17:48:10 +08:00
parent 27e6b8f9f4
commit 4f612ec2e0
9 changed files with 139 additions and 36 deletions
@@ -37,6 +37,7 @@ public class SQLSelectQueryBlock extends SQLObjectImpl implements SQLSelectQuery
protected boolean forUpdate = false;
protected boolean noWait = false;
protected SQLExpr waitTime;
protected SQLExpr first;
public SQLSelectQueryBlock(){
@@ -142,9 +143,24 @@ public class SQLSelectQueryBlock extends SQLObjectImpl implements SQLSelectQuery
}
public void setWaitTime(SQLExpr waitTime) {
if (waitTime != null) {
waitTime.setParent(this);
}
this.waitTime = waitTime;
}
public SQLExpr getFirst() {
return first;
}
public void setFirst(SQLExpr first) {
if (first != null) {
first.setParent(this);
}
this.first = first;
}
@Override
protected void accept0(SQLASTVisitor visitor) {
if (visitor.visit(this)) {
@@ -23,7 +23,7 @@ import com.alibaba.druid.sql.visitor.SQLASTVisitor;
public class DB2SelectQueryBlock extends SQLSelectQueryBlock implements DB2Object {
private SQLExpr first;
private Isolation isolation;
@@ -31,14 +31,6 @@ public class DB2SelectQueryBlock extends SQLSelectQueryBlock implements DB2Objec
private SQLExpr optimizeFor;
public SQLExpr getFirst() {
return first;
}
public void setFirst(SQLExpr first) {
this.first = first;
}
@Override
protected void accept0(SQLASTVisitor visitor) {
if (visitor instanceof DB2ASTVisitor) {
@@ -38,27 +38,6 @@ public class DB2OutputVisitor extends SQLASTOutputVisitor implements DB2ASTVisit
public boolean visit(DB2SelectQueryBlock x) {
this.visit((SQLSelectQueryBlock) x);
if (x.getFirst() != null) {
//order by 语句必须在FETCH FIRST ROWS ONLY之前
SQLObject parent= x.getParent();
if(parent instanceof SQLSelect)
{
SQLOrderBy orderBy= ((SQLSelect) parent).getOrderBy();
if (orderBy!=null&&orderBy.getItems().size() > 0) {
println();
print0(ucase ? "ORDER BY " : "order by ");
printAndAccept(orderBy.getItems(), ", ");
((SQLSelect) parent).setOrderBy(null);
}
}
println();
print0(ucase ? "FETCH FIRST " : "fetch first ");
x.getFirst().accept(this);
print0(ucase ? " ROWS ONLY" : " rows only");
}
if (x.isForReadOnly()) {
println();
print0(ucase ? "FOR READ ONLY" : "for read only");
@@ -109,6 +109,8 @@ public class OracleLexer extends Lexer {
map.put("PURGE", Token.PURGE);
map.put("INITIALLY", Token.INITIALLY);
map.put("FETCH", Token.FETCH);
DEFAULT_ORACLE_KEYWORDS = new Keywords(map);
}
@@ -276,6 +276,8 @@ public class OracleSelectParser extends SQLSelectParser {
parseModelClause(queryBlock);
parseFetchClause(queryBlock);
return queryRest(queryBlock);
}
@@ -17,12 +17,7 @@ package com.alibaba.druid.sql.dialect.oracle.visitor;
import java.util.List;
import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.SQLHint;
import com.alibaba.druid.sql.ast.SQLObject;
import com.alibaba.druid.sql.ast.SQLParameter;
import com.alibaba.druid.sql.ast.SQLSetQuantifier;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.ast.*;
import com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr;
import com.alibaba.druid.sql.ast.expr.SQLCharExpr;
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
@@ -572,6 +567,8 @@ public class OracleOutputVisitor extends SQLASTOutputVisitor implements OracleAS
x.getModelClause().accept(this);
}
printFetchFirst(x);
return false;
}
@@ -540,4 +540,25 @@ public class SQLSelectParser extends SQLParser {
}
}
public void parseFetchClause(SQLSelectQueryBlock queryBlock) {
if (lexer.token() == Token.FETCH) {
lexer.nextToken();
if (lexer.token() == Token.FIRST) {
lexer.nextToken();
} else {
acceptIdentifier("FIRST");
}
SQLExpr first = this.exprParser.primary();
queryBlock.setFirst(first);
if (identifierEquals("ROW") || identifierEquals("ROWS")) {
lexer.nextToken();
}
if (lexer.token() == Token.ONLY) {
lexer.nextToken();
} else {
acceptIdentifier("ONLY");
}
}
}
}
@@ -787,9 +787,31 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
x.getOrderBy().accept(this);
}
printFetchFirst(x);
return false;
}
protected void printFetchFirst(SQLSelectQueryBlock x) {
if (x.getFirst() != null) {
//order by 语句必须在FETCH FIRST ROWS ONLY之前
SQLObject parent= x.getParent();
if(parent instanceof SQLSelect) {
SQLOrderBy orderBy = ((SQLSelect) parent).getOrderBy();
if (orderBy!=null&&orderBy.getItems().size() > 0) {
println();
print0(ucase ? "ORDER BY " : "order by ");
printAndAccept(orderBy.getItems(), ", ");
((SQLSelect) parent).setOrderBy(null);
}
}
println();
print0(ucase ? "FETCH FIRST " : "fetch first ");
x.getFirst().accept(this);
print0(ucase ? " ROWS ONLY" : " rows only");
}
}
public boolean visit(SQLSelectItem x) {
if (x.isConnectByRoot()) {
print0(ucase ? "CONNECT_BY_ROOT " : "connect_by_root ");