Skip to content

Commit

Permalink
bug fixed for PagerUtils, for issue alibaba#3081
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Feb 22, 2019
1 parent c732a35 commit 24a83cd
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/main/java/com/alibaba/druid/sql/PagerUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -272,8 +272,13 @@ private static boolean limitSQLServer(SQLSelect select, String dbType, int offse

SQLAggregateExpr aggregateExpr = new SQLAggregateExpr("ROW_NUMBER");
SQLOrderBy orderBy = select.getOrderBy();
aggregateExpr.setOver(new SQLOver(orderBy));
select.setOrderBy(null);
if (orderBy != null) {
aggregateExpr.setOver(new SQLOver(orderBy));
select.setOrderBy(null);
} else if (queryBlock.getOrderBy() != null){
aggregateExpr.setOver(new SQLOver(queryBlock.getOrderBy()));
queryBlock.setOrderBy(null);
}

queryBlock.getSelectList().add(new SQLSelectItem(aggregateExpr, "ROWNUM"));

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.alibaba.druid.bvt.sql;

import com.alibaba.druid.sql.PagerUtils;
import com.alibaba.druid.util.JdbcConstants;
import junit.framework.TestCase;
import org.junit.Assert;

public class PagerUtilsTest_Limit_SQLServer_6 extends TestCase {

public void test_db2_union() throws Exception {
String sql = "SELECT t.name USER_NAME, t.xxx FROM t_sd_users t ORDER BY t.name ASC";
String result = PagerUtils.limit(sql, JdbcConstants.SQL_SERVER, 10, 10);
Assert.assertEquals("SELECT *\n" +
"FROM (\n" +
"\tSELECT t.name AS USER_NAME, t.xxx, ROW_NUMBER() OVER (ORDER BY t.name ASC) AS ROWNUM\n" +
"\tFROM t_sd_users t\n" +
") XX\n" +
"WHERE ROWNUM > 10\n" +
"\tAND ROWNUM <= 20", result);
}
}

0 comments on commit 24a83cd

Please sign in to comment.