add db2 PageUtils support

This commit is contained in:
wenshao
2013-07-26 01:01:58 +08:00
parent 89f1106e34
commit 9722e3aeea
19 changed files with 538 additions and 14 deletions
@@ -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);
}
}