PageUtils support SQL Server

This commit is contained in:
wenshao
2013-07-27 20:54:03 +08:00
parent 58174442a4
commit c65c28e9b2
4 changed files with 76 additions and 10 deletions
@@ -192,10 +192,8 @@ public class PagerUtils {
SQLAggregateExpr aggregateExpr = new SQLAggregateExpr("ROW_NUMBER");
SQLOrderBy orderBy = select.getOrderBy();
if (orderBy != null) {
aggregateExpr.setOver(new SQLOver(orderBy));
select.setOrderBy(null);
}
aggregateExpr.setOver(new SQLOver(orderBy));
select.setOrderBy(null);
queryBlock.getSelectList().add(new SQLSelectItem(aggregateExpr, "ROWNUM"));
@@ -208,21 +206,24 @@ public class PagerUtils {
return SQLUtils.toSQLString(countQueryBlock, dbType);
}
SQLServerSelectQueryBlock countQueryBlock = new SQLServerSelectQueryBlock();
countQueryBlock.getSelectList().add(new SQLSelectItem(new SQLPropertyExpr(new SQLIdentifierExpr("XX"), "*")));
countQueryBlock.setFrom(new SQLSubqueryTableSource(select, "XX"));
if (offset <= 0) {
countQueryBlock.setTop(new SQLServerTop(new SQLNumberExpr(count)));
return SQLUtils.toSQLString(countQueryBlock, dbType);
}
SQLAggregateExpr aggregateExpr = new SQLAggregateExpr("ROW_NUMBER");
SQLOrderBy orderBy = select.getOrderBy();
aggregateExpr.setOver(new SQLOver(orderBy));
select.setOrderBy(null);
countQueryBlock.getSelectList().add(new SQLSelectItem(aggregateExpr, "ROWNUM"));
countQueryBlock.setFrom(new SQLSubqueryTableSource(select, "XX"));
if (offset <= 0) {
return SQLUtils.toSQLString(countQueryBlock, dbType);
}
SQLServerSelectQueryBlock offsetQueryBlock = new SQLServerSelectQueryBlock();
offsetQueryBlock.getSelectList().add(new SQLSelectItem(new SQLAllColumnExpr()));
offsetQueryBlock.setFrom(new SQLSubqueryTableSource(new SQLSelect(countQueryBlock), "XXX"));
@@ -0,0 +1,23 @@
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_SQLServer_2 extends TestCase {
public void test_db2_union() throws Exception {
String sql = "select * from t1 union select * from t2";
String result = PagerUtils.limit(sql, JdbcConstants.SQL_SERVER, 0, 10);
Assert.assertEquals("SELECT TOP 10 XX.*" //
+ "\nFROM (SELECT *" //
+ "\n\tFROM t1" //
+ "\n\tUNION" //
+ "\n\tSELECT *" //
+ "\n\tFROM t2" //
+ "\n\t) XX", result);
}
}
@@ -0,0 +1,19 @@
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_SQLServer_3 extends TestCase {
public void test_db2_union() throws Exception {
String sql = "select * from t1 where id > 1";
String result = PagerUtils.limit(sql, JdbcConstants.SQL_SERVER, 0, 10);
Assert.assertEquals("SELECT TOP 10 *" //
+ "\nFROM t1" //
+ "\nWHERE id > 1", result);
}
}
@@ -0,0 +1,23 @@
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_SQLServer_4 extends TestCase {
public void test_db2_union() throws Exception {
String sql = "select * from t1 where id > 1";
String result = PagerUtils.limit(sql, JdbcConstants.SQL_SERVER, 100, 10);
Assert.assertEquals("SELECT *" //
+ "\nFROM (SELECT *, ROW_NUMBER() OVER () AS ROWNUM" //
+ "\n\tFROM t1" //
+ "\n\tWHERE id > 1" //
+ "\n\t) XX" //
+ "\nWHERE ROWNUM > 100" //
+ "\n\tAND ROWNUM <= 110", result);
}
}