mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improved oracle sql parser support fetch clause. issue #1448
This commit is contained in:
@@ -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 ");
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
/*
|
||||
* Copyright 1999-2101 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.alibaba.druid.bvt.sql.oracle.select;
|
||||
|
||||
import com.alibaba.druid.sql.OracleTest;
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.dialect.oracle.parser.OracleStatementParser;
|
||||
import com.alibaba.druid.sql.dialect.oracle.visitor.OracleSchemaStatVisitor;
|
||||
import org.junit.Assert;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class OracleSelectTest58 extends OracleTest {
|
||||
|
||||
public void test_0() throws Exception {
|
||||
String sql = //
|
||||
"SELECT * FROM T FETCH FIRST 10 ROWS ONLY"; //
|
||||
|
||||
OracleStatementParser parser = new OracleStatementParser(sql);
|
||||
List<SQLStatement> statementList = parser.parseStatementList();
|
||||
SQLStatement stmt = statementList.get(0);
|
||||
print(statementList);
|
||||
|
||||
Assert.assertEquals(1, statementList.size());
|
||||
|
||||
OracleSchemaStatVisitor visitor = new OracleSchemaStatVisitor();
|
||||
stmt.accept(visitor);
|
||||
|
||||
System.out.println("Tables : " + visitor.getTables());
|
||||
System.out.println("fields : " + visitor.getColumns());
|
||||
System.out.println("coditions : " + visitor.getConditions());
|
||||
System.out.println("relationships : " + visitor.getRelationships());
|
||||
System.out.println("orderBy : " + visitor.getOrderByColumns());
|
||||
|
||||
Assert.assertEquals(1, visitor.getTables().size());
|
||||
|
||||
Assert.assertEquals(1, visitor.getColumns().size());
|
||||
|
||||
{
|
||||
String text = SQLUtils.toOracleString(stmt);
|
||||
|
||||
Assert.assertEquals("SELECT *\n" +
|
||||
"FROM T\n" +
|
||||
"FETCH FIRST 10 ROWS ONLY", text);
|
||||
}
|
||||
|
||||
{
|
||||
String text = SQLUtils.toOracleString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION);
|
||||
|
||||
Assert.assertEquals("select *\n" +
|
||||
"from T\n" +
|
||||
"fetch first 10 rows only", text);
|
||||
}
|
||||
// Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("acduser.vw_acd_info", "xzqh")));
|
||||
|
||||
// Assert.assertTrue(visitor.getOrderByColumns().contains(new TableStat.Column("employees", "last_name")));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user