mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
add db2 PageUtils support
This commit is contained in:
@@ -527,12 +527,5 @@
|
||||
<version>2.0.2</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.yammer.metrics</groupId>
|
||||
<artifactId>metrics-core</artifactId>
|
||||
<version>2.2.0</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
||||
@@ -18,6 +18,8 @@ package com.alibaba.druid.sql;
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.SQLOrderBy;
|
||||
import com.alibaba.druid.sql.ast.SQLOver;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLAggregateExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr;
|
||||
@@ -86,6 +88,10 @@ public class PagerUtils {
|
||||
return limitOracle(select, dbType, offset, count);
|
||||
}
|
||||
|
||||
if (JdbcConstants.DB2.equals(dbType)) {
|
||||
return limitDB2(select, dbType, offset, count);
|
||||
}
|
||||
|
||||
if (query instanceof SQLSelectQueryBlock) {
|
||||
return limitQueryBlock(select, dbType, offset, count);
|
||||
}
|
||||
@@ -104,6 +110,53 @@ public class PagerUtils {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
private static String limitDB2(SQLSelect select, String dbType, int offset, int count) {
|
||||
SQLSelectQuery query = select.getQuery();
|
||||
|
||||
if (query instanceof SQLSelectQueryBlock) {
|
||||
DB2SelectQueryBlock queryBlock = (DB2SelectQueryBlock) query;
|
||||
if (offset <= 0) {
|
||||
queryBlock.setFirst(new SQLNumberExpr(count));
|
||||
return SQLUtils.toSQLString(select, dbType);
|
||||
}
|
||||
|
||||
SQLAggregateExpr aggregateExpr = new SQLAggregateExpr("ROW_NUMBER");
|
||||
SQLOrderBy orderBy = select.getOrderBy();
|
||||
if (orderBy != null) {
|
||||
aggregateExpr.setOver(new SQLOver(orderBy));
|
||||
select.setOrderBy(null);
|
||||
}
|
||||
|
||||
queryBlock.getSelectList().add(new SQLSelectItem(aggregateExpr, "ROWNUM"));
|
||||
|
||||
DB2SelectQueryBlock countQueryBlock = new DB2SelectQueryBlock();
|
||||
countQueryBlock.getSelectList().add(new SQLSelectItem(new SQLAllColumnExpr()));
|
||||
|
||||
countQueryBlock.setFrom(new SQLSubqueryTableSource(select, "XX"));
|
||||
|
||||
SQLBinaryOpExpr gt = new SQLBinaryOpExpr(new SQLIdentifierExpr("ROWNUM"), //
|
||||
SQLBinaryOperator.GreaterThan, //
|
||||
new SQLNumberExpr(offset));
|
||||
SQLBinaryOpExpr lteq = new SQLBinaryOpExpr(new SQLIdentifierExpr("ROWNUM"), //
|
||||
SQLBinaryOperator.LessThanOrEqual, //
|
||||
new SQLNumberExpr(count + offset));
|
||||
SQLBinaryOpExpr pageCondition = new SQLBinaryOpExpr(gt, SQLBinaryOperator.BooleanAnd, lteq);
|
||||
|
||||
countQueryBlock.setWhere(pageCondition);
|
||||
|
||||
// DB2SelectQueryBlock offsetQueryBlock = new DB2SelectQueryBlock();
|
||||
// offsetQueryBlock.getSelectList().add(new SQLSelectItem(new SQLAllColumnExpr()));
|
||||
// offsetQueryBlock.setFrom(new SQLSubqueryTableSource(new SQLSelect(countQueryBlock), "XXX"));
|
||||
// offsetQueryBlock.setWhere(new SQLBinaryOpExpr(new SQLIdentifierExpr("RN"), //
|
||||
// SQLBinaryOperator.GreaterThan, //
|
||||
// new SQLNumberExpr(offset)));
|
||||
|
||||
return SQLUtils.toSQLString(countQueryBlock, dbType);
|
||||
}
|
||||
|
||||
return SQLUtils.toSQLString(select, dbType);
|
||||
}
|
||||
|
||||
private static String limitOracle(SQLSelect select, String dbType, int offset, int count) {
|
||||
SQLSelectQuery query = select.getQuery();
|
||||
|
||||
@@ -227,7 +280,7 @@ public class PagerUtils {
|
||||
if (JdbcConstants.SQL_SERVER.equals(dbType)) {
|
||||
return new SQLServerSelectQueryBlock();
|
||||
}
|
||||
|
||||
|
||||
if (JdbcConstants.DB2.equals(dbType)) {
|
||||
return new DB2SelectQueryBlock();
|
||||
}
|
||||
|
||||
@@ -63,6 +63,10 @@ public class SQLUtils {
|
||||
if (JdbcUtils.POSTGRESQL.equals(dbType)) {
|
||||
return toPGString(sqlObject);
|
||||
}
|
||||
|
||||
if (JdbcUtils.DB2.equals(dbType)) {
|
||||
return toDB2String(sqlObject);
|
||||
}
|
||||
|
||||
return toSQLServerString(sqlObject);
|
||||
}
|
||||
@@ -110,6 +114,14 @@ public class SQLUtils {
|
||||
String sql = out.toString();
|
||||
return sql;
|
||||
}
|
||||
|
||||
public static String toDB2String(SQLObject sqlObject) {
|
||||
StringBuilder out = new StringBuilder();
|
||||
sqlObject.accept(new DB2OutputVisitor(out));
|
||||
|
||||
String sql = out.toString();
|
||||
return sql;
|
||||
}
|
||||
|
||||
public static String toSQLServerString(SQLObject sqlObject) {
|
||||
StringBuilder out = new StringBuilder();
|
||||
|
||||
@@ -26,6 +26,14 @@ public class SQLOver extends SQLObjectImpl {
|
||||
|
||||
protected final List<SQLExpr> partitionBy = new ArrayList<SQLExpr>();
|
||||
protected SQLOrderBy orderBy;
|
||||
|
||||
public SQLOver() {
|
||||
|
||||
}
|
||||
|
||||
public SQLOver(SQLOrderBy orderBy) {
|
||||
this.setOrderBy(orderBy);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void accept0(SQLASTVisitor visitor) {
|
||||
@@ -41,6 +49,9 @@ public class SQLOver extends SQLObjectImpl {
|
||||
}
|
||||
|
||||
public void setOrderBy(SQLOrderBy orderBy) {
|
||||
if (orderBy != null) {
|
||||
orderBy.setParent(this);
|
||||
}
|
||||
this.orderBy = orderBy;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* Copyright 1999-2011 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.sql.dialect.db2.ast;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLObject;
|
||||
import com.alibaba.druid.sql.dialect.db2.visitor.DB2ASTVisitor;
|
||||
|
||||
|
||||
public interface DB2Object extends SQLObject {
|
||||
void accept0(DB2ASTVisitor visitor);
|
||||
}
|
||||
@@ -15,11 +15,46 @@
|
||||
*/
|
||||
package com.alibaba.druid.sql.dialect.db2.ast.stmt;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock;
|
||||
import com.alibaba.druid.sql.dialect.db2.ast.DB2Object;
|
||||
import com.alibaba.druid.sql.dialect.db2.visitor.DB2ASTVisitor;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
|
||||
|
||||
public class DB2SelectQueryBlock extends SQLSelectQueryBlock {
|
||||
public class DB2SelectQueryBlock extends SQLSelectQueryBlock implements DB2Object {
|
||||
|
||||
private SQLExpr first;
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
|
||||
public SQLExpr getFirst() {
|
||||
return first;
|
||||
}
|
||||
|
||||
public void setFirst(SQLExpr first) {
|
||||
this.first = first;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void accept0(SQLASTVisitor visitor) {
|
||||
if (visitor instanceof DB2ASTVisitor) {
|
||||
accept0((DB2ASTVisitor) visitor);
|
||||
return;
|
||||
}
|
||||
|
||||
super.accept0(visitor);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept0(DB2ASTVisitor visitor) {
|
||||
if (visitor.visit(this)) {
|
||||
acceptChild(visitor, this.selectList);
|
||||
acceptChild(visitor, this.from);
|
||||
acceptChild(visitor, this.where);
|
||||
acceptChild(visitor, this.groupBy);
|
||||
acceptChild(visitor, this.first);
|
||||
}
|
||||
visitor.endVisit(this);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,18 @@
|
||||
/*
|
||||
* Copyright 1999-2011 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.sql.dialect.db2.parser;
|
||||
|
||||
import com.alibaba.druid.sql.parser.Lexer;
|
||||
|
||||
@@ -1,3 +1,18 @@
|
||||
/*
|
||||
* Copyright 1999-2011 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.sql.dialect.db2.parser;
|
||||
|
||||
import java.util.HashMap;
|
||||
@@ -17,6 +32,10 @@ public class DB2Lexer extends Lexer {
|
||||
|
||||
map.putAll(Keywords.DEFAULT_KEYWORDS.getKeywords());
|
||||
|
||||
map.put("FETCH", Token.FETCH);
|
||||
map.put("FIRST", Token.FIRST);
|
||||
map.put("ONLY", Token.ONLY);
|
||||
|
||||
DEFAULT_DB2_KEYWORDS = new Keywords(map);
|
||||
}
|
||||
|
||||
|
||||
@@ -15,10 +15,11 @@
|
||||
*/
|
||||
package com.alibaba.druid.sql.dialect.db2.parser;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.SQLSetQuantifier;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectQuery;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock;
|
||||
import com.alibaba.druid.sql.dialect.db2.ast.stmt.DB2SelectQueryBlock;
|
||||
import com.alibaba.druid.sql.parser.ParserException;
|
||||
import com.alibaba.druid.sql.parser.SQLExprParser;
|
||||
import com.alibaba.druid.sql.parser.SQLSelectParser;
|
||||
import com.alibaba.druid.sql.parser.Token;
|
||||
@@ -49,7 +50,7 @@ public class DB2SelectParser extends SQLSelectParser {
|
||||
}
|
||||
|
||||
accept(Token.SELECT);
|
||||
|
||||
|
||||
if (lexer.token() == Token.COMMENT) {
|
||||
lexer.nextToken();
|
||||
}
|
||||
@@ -74,6 +75,17 @@ public class DB2SelectParser extends SQLSelectParser {
|
||||
parseWhere(queryBlock);
|
||||
|
||||
parseGroupBy(queryBlock);
|
||||
|
||||
if (lexer.token() == Token.FETCH) {
|
||||
lexer.nextToken();
|
||||
accept(Token.FIRST);
|
||||
SQLExpr first = this.exprParser.primary();
|
||||
queryBlock.setFirst(first);
|
||||
if (identifierEquals("ROW") || identifierEquals("ROWS")) {
|
||||
lexer.nextToken();
|
||||
}
|
||||
accept(Token.ONLY);
|
||||
}
|
||||
|
||||
return queryRest(queryBlock);
|
||||
}
|
||||
|
||||
@@ -1,3 +1,18 @@
|
||||
/*
|
||||
* Copyright 1999-2011 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.sql.dialect.db2.parser;
|
||||
|
||||
import com.alibaba.druid.sql.parser.Lexer;
|
||||
|
||||
@@ -1,8 +1,26 @@
|
||||
/*
|
||||
* Copyright 1999-2011 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.sql.dialect.db2.visitor;
|
||||
|
||||
import com.alibaba.druid.sql.dialect.db2.ast.stmt.DB2SelectQueryBlock;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
|
||||
|
||||
|
||||
public interface DB2ASTVisitor extends SQLASTVisitor {
|
||||
|
||||
boolean visit(DB2SelectQueryBlock x);
|
||||
|
||||
void endVisit(DB2SelectQueryBlock x);
|
||||
}
|
||||
|
||||
@@ -1,7 +1,33 @@
|
||||
/*
|
||||
* Copyright 1999-2011 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.sql.dialect.db2.visitor;
|
||||
|
||||
import com.alibaba.druid.sql.dialect.db2.ast.stmt.DB2SelectQueryBlock;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTVisitorAdapter;
|
||||
|
||||
public class DB2ASTVisitorAdapter extends SQLASTVisitorAdapter implements DB2ASTVisitor {
|
||||
|
||||
@Override
|
||||
public boolean visit(DB2SelectQueryBlock x) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(DB2SelectQueryBlock x) {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,10 +1,46 @@
|
||||
/*
|
||||
* Copyright 1999-2011 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.sql.dialect.db2.visitor;
|
||||
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock;
|
||||
import com.alibaba.druid.sql.dialect.db2.ast.stmt.DB2SelectQueryBlock;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTOutputVisitor;
|
||||
|
||||
|
||||
public class DB2OutputVisitor extends SQLASTOutputVisitor implements DB2ASTVisitor {
|
||||
|
||||
public DB2OutputVisitor(Appendable appender){
|
||||
super(appender);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(DB2SelectQueryBlock x) {
|
||||
this.visit((SQLSelectQueryBlock) x);
|
||||
|
||||
if (x.getFirst() != null) {
|
||||
println();
|
||||
print("FETCH FIRST ");
|
||||
x.getFirst().accept(this);
|
||||
print(" ROWS ONLY");
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(DB2SelectQueryBlock x) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,6 +15,8 @@
|
||||
*/
|
||||
package com.alibaba.druid.sql.dialect.db2.visitor;
|
||||
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock;
|
||||
import com.alibaba.druid.sql.dialect.db2.ast.stmt.DB2SelectQueryBlock;
|
||||
import com.alibaba.druid.sql.visitor.SchemaStatVisitor;
|
||||
import com.alibaba.druid.util.JdbcUtils;
|
||||
|
||||
@@ -25,4 +27,14 @@ public class DB2SchemaStatVisitor extends SchemaStatVisitor implements DB2ASTVis
|
||||
return JdbcUtils.DB2;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(DB2SelectQueryBlock x) {
|
||||
return this.visit((SQLSelectQueryBlock) x);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(DB2SelectQueryBlock x) {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,3 +1,18 @@
|
||||
/*
|
||||
* Copyright 1999-2011 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.wall.spi;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
package com.alibaba.druid.bvt.sql;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.alibaba.druid.sql.PagerUtils;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
|
||||
public class PagerUtilsTest_Limit_db2_0 extends TestCase {
|
||||
|
||||
public void test_mysql_0() throws Exception {
|
||||
String sql = "select * from t";
|
||||
String result = PagerUtils.limit(sql, JdbcConstants.DB2, 0, 10);
|
||||
Assert.assertEquals("SELECT *" + //
|
||||
"\nFROM t" + //
|
||||
"\nFETCH FIRST 10 ROWS ONLY", result);
|
||||
}
|
||||
|
||||
public void test_mysql_1() throws Exception {
|
||||
String sql = "select * from t";
|
||||
String result = PagerUtils.limit(sql, JdbcConstants.DB2, 10, 10);
|
||||
Assert.assertEquals("SELECT *"//
|
||||
+ "\nFROM (SELECT *, ROW_NUMBER() AS ROWNUM"//
|
||||
+ "\n\tFROM t"//
|
||||
+ "\n\t) XX"//
|
||||
+ "\nWHERE ROWNUM > 10"//
|
||||
+ "\n\tAND ROWNUM <= 20", result);
|
||||
}
|
||||
|
||||
public void test_mysql_2() throws Exception {
|
||||
String sql = "select * from t where age > 100";
|
||||
String result = PagerUtils.limit(sql, JdbcConstants.DB2, 20, 10);
|
||||
Assert.assertEquals("SELECT *"//
|
||||
+ "\nFROM (SELECT *, ROW_NUMBER() AS ROWNUM"//
|
||||
+ "\n\tFROM t"//
|
||||
+ "\n\tWHERE age > 100"//
|
||||
+ "\n\t) XX"//
|
||||
+ "\nWHERE ROWNUM > 20"//
|
||||
+ "\n\tAND ROWNUM <= 30", result);
|
||||
}
|
||||
|
||||
public void test_mysql_3() throws Exception {
|
||||
String sql = "select id, name, salary from t order by id, name";
|
||||
String result = PagerUtils.limit(sql, JdbcConstants.DB2, 20, 10);
|
||||
Assert.assertEquals("SELECT *"//
|
||||
+ "\nFROM (SELECT id, name, salary, ROW_NUMBER() OVER (ORDER BY id, name) AS ROWNUM"//
|
||||
+ "\n\tFROM t"//
|
||||
+ "\n\t) XX"//
|
||||
+ "\nWHERE ROWNUM > 20"//
|
||||
+ "\n\tAND ROWNUM <= 30", result);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
* Copyright 1999-2011 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.db2;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.alibaba.druid.sql.DB2Test;
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.dialect.db2.parser.DB2StatementParser;
|
||||
import com.alibaba.druid.sql.dialect.db2.visitor.DB2SchemaStatVisitor;
|
||||
import com.alibaba.druid.stat.TableStat;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
|
||||
public class DB2SelectTest_0 extends DB2Test {
|
||||
|
||||
public void test_0() throws Exception {
|
||||
String sql = "SELECT * FROM DSN81010.EMP;";
|
||||
|
||||
DB2StatementParser parser = new DB2StatementParser(sql);
|
||||
List<SQLStatement> statementList = parser.parseStatementList();
|
||||
SQLStatement stmt = statementList.get(0);
|
||||
print(statementList);
|
||||
|
||||
Assert.assertEquals(1, statementList.size());
|
||||
|
||||
DB2SchemaStatVisitor visitor = new DB2SchemaStatVisitor();
|
||||
stmt.accept(visitor);
|
||||
|
||||
System.out.println("Tables : " + visitor.getTables());
|
||||
System.out.println("fields : " + visitor.getColumns());
|
||||
System.out.println("coditions : " + visitor.getConditions());
|
||||
System.out.println("orderBy : " + visitor.getOrderByColumns());
|
||||
|
||||
Assert.assertEquals(1, visitor.getTables().size());
|
||||
Assert.assertEquals(1, visitor.getColumns().size());
|
||||
Assert.assertEquals(0, visitor.getConditions().size());
|
||||
|
||||
Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("DSN81010.EMP")));
|
||||
|
||||
// Assert.assertTrue(visitor.getColumns().contains(new Column("mytable", "last_name")));
|
||||
// Assert.assertTrue(visitor.getColumns().contains(new Column("mytable", "first_name")));
|
||||
// Assert.assertTrue(visitor.getColumns().contains(new Column("mytable", "full_name")));
|
||||
|
||||
String output = SQLUtils.toSQLString(stmt, JdbcConstants.DB2);
|
||||
Assert.assertEquals("SELECT *" //
|
||||
+ "\nFROM DSN81010.EMP", //
|
||||
output);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
* Copyright 1999-2011 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.db2;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.alibaba.druid.sql.DB2Test;
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.dialect.db2.parser.DB2StatementParser;
|
||||
import com.alibaba.druid.sql.dialect.db2.visitor.DB2SchemaStatVisitor;
|
||||
import com.alibaba.druid.stat.TableStat;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
|
||||
public class DB2SelectTest_1 extends DB2Test {
|
||||
|
||||
public void test_0() throws Exception {
|
||||
String sql = "SELECT * FROM DSN81010.EMP FETCH FIRST 5 ROWS ONLY;";
|
||||
|
||||
DB2StatementParser parser = new DB2StatementParser(sql);
|
||||
List<SQLStatement> statementList = parser.parseStatementList();
|
||||
SQLStatement stmt = statementList.get(0);
|
||||
print(statementList);
|
||||
|
||||
Assert.assertEquals(1, statementList.size());
|
||||
|
||||
DB2SchemaStatVisitor visitor = new DB2SchemaStatVisitor();
|
||||
stmt.accept(visitor);
|
||||
|
||||
System.out.println("Tables : " + visitor.getTables());
|
||||
System.out.println("fields : " + visitor.getColumns());
|
||||
System.out.println("coditions : " + visitor.getConditions());
|
||||
System.out.println("orderBy : " + visitor.getOrderByColumns());
|
||||
|
||||
Assert.assertEquals(1, visitor.getTables().size());
|
||||
Assert.assertEquals(1, visitor.getColumns().size());
|
||||
Assert.assertEquals(0, visitor.getConditions().size());
|
||||
|
||||
Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("DSN81010.EMP")));
|
||||
|
||||
// Assert.assertTrue(visitor.getColumns().contains(new Column("mytable", "last_name")));
|
||||
// Assert.assertTrue(visitor.getColumns().contains(new Column("mytable", "first_name")));
|
||||
// Assert.assertTrue(visitor.getColumns().contains(new Column("mytable", "full_name")));
|
||||
|
||||
String output = SQLUtils.toSQLString(stmt, JdbcConstants.DB2);
|
||||
Assert.assertEquals("SELECT *" //
|
||||
+ "\nFROM DSN81010.EMP", //
|
||||
output);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* Copyright 1999-2011 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.sql;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.dialect.db2.visitor.DB2OutputVisitor;
|
||||
|
||||
public class DB2Test extends TestCase {
|
||||
protected String output(List<SQLStatement> stmtList) {
|
||||
StringBuilder out = new StringBuilder();
|
||||
DB2OutputVisitor visitor = new DB2OutputVisitor(out);
|
||||
|
||||
for (SQLStatement stmt : stmtList) {
|
||||
stmt.accept(visitor);
|
||||
}
|
||||
|
||||
return out.toString();
|
||||
}
|
||||
|
||||
protected void print(List<SQLStatement> stmtList) {
|
||||
String text = output(stmtList);
|
||||
String outputProperty = System.getProperty("druid.output");
|
||||
if ("false".equals(outputProperty)) {
|
||||
return;
|
||||
}
|
||||
System.out.println(text);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user