Skip to content

Commit

Permalink
feature: add sqlserver's adaptation to the console paging interface (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
GoodBoyCoder authored Oct 22, 2023
1 parent 5aca41d commit 2626f24
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 2 deletions.
3 changes: 2 additions & 1 deletion changes/en-us/2.0.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ The version is updated as follows:
- [[#5902](https://github.com/seata/seata/pull/5902)] support IPv6
- [[#5907](https://github.com/seata/seata/pull/5907)] support polardb-x 2.0 in AT mode
- [[#5932](https://github.com/seata/seata/pull/5932)] support Dameng database
- [[#5946](https://github.com/seata/seata/pull/5946)] add sqlserver's adaptation to the console paging interface

### bugfix:
- [[#5677](https://github.com/seata/seata/pull/5677)] fix saga mode serviceTask inputParams json autoType convert exception
Expand Down Expand Up @@ -175,7 +176,7 @@ Thanks to these contributors for their code commits. Please report an unintended
- [pengten](https://github.com/pengten)
- [wangliang181230](https://github.com/wangliang181230)
- [GoodBoyCoder](https://github.com/GoodBoyCoder)
- [a364176773](https://github.com/a364176773)
- [funky-eyes](https://github.com/funky-eyes)
- [isharpever](https://github.com/isharpever)
- [mxsm](https://github.com/mxsm)
- [liuqiufeng](https://github.com/liuqiufeng)
Expand Down
3 changes: 2 additions & 1 deletion changes/zh-cn/2.0.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Seata 是一款开源的分布式事务解决方案,提供高性能和简单
- [[#5902](https://github.com/seata/seata/pull/5902)] 支持IPv6网络环境
- [[#5907](https://github.com/seata/seata/pull/5907)] 增加AT模式的PolarDB-X 2.0数据库支持
- [[#5932](https://github.com/seata/seata/pull/5932)] AT模式支持达梦数据库
- [[#5946](https://github.com/seata/seata/pull/5946)] 增加sqlserver对控制台分页接口的适配

### bugfix:
- [[#5677](https://github.com/seata/seata/pull/5677)] 修复saga模式下serviceTask入参autoType转化失败问题
Expand Down Expand Up @@ -174,7 +175,7 @@ Seata 是一款开源的分布式事务解决方案,提供高性能和简单
- [pengten](https://github.com/pengten)
- [wangliang181230](https://github.com/wangliang181230)
- [GoodBoyCoder](https://github.com/GoodBoyCoder)
- [a364176773](https://github.com/a364176773)
- [funky-eyes](https://github.com/funky-eyes)
- [isharpever](https://github.com/isharpever)
- [mxsm](https://github.com/mxsm)
- [liuqiufeng](https://github.com/liuqiufeng)
Expand Down
10 changes: 10 additions & 0 deletions common/src/main/java/io/seata/common/util/PageUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ public class PageUtil {
private static final String ORACLE_PAGE_TEMPLATE = "select * from ( select ROWNUM rn, temp.* from ("
+ SOURCE_SQL_PLACE_HOLD + ") temp ) where rn between " + START_PLACE_HOLD + " and " + END_PLACE_HOLD;

/**
* The constant SQLSERVER_PAGE_TEMPLATE
*/
private static final String SQLSERVER_PAGE_TEMPLATE = "select * from (select temp.*, ROW_NUMBER() OVER(ORDER BY (select NULL)) AS rowId from ("
+ SOURCE_SQL_PLACE_HOLD + ") temp ) t where t.rowId between " + START_PLACE_HOLD + " and " + END_PLACE_HOLD;
/**
* check page parm
*
Expand Down Expand Up @@ -113,6 +118,10 @@ public static String pageSql(String sourceSql, String dbType, int pageNum, int p
return ORACLE_PAGE_TEMPLATE.replace(SOURCE_SQL_PLACE_HOLD, sourceSql)
.replace(START_PLACE_HOLD, String.valueOf(pageSize * (pageNum - 1) + 1))
.replace(END_PLACE_HOLD, String.valueOf(pageSize * pageNum));
case "sqlserver":
return SQLSERVER_PAGE_TEMPLATE.replace(SOURCE_SQL_PLACE_HOLD, sourceSql)
.replace(START_PLACE_HOLD, String.valueOf(pageSize * (pageNum - 1) + 1))
.replace(END_PLACE_HOLD, String.valueOf(pageSize * pageNum));
default:
throw new NotSupportYetException("PageUtil not support this dbType:" + dbType);
}
Expand All @@ -134,6 +143,7 @@ public static String countSql(String sourceSql, String dbType) {
case "dm":
return sourceSql.replaceAll("(?i)(?<=select)(.*)(?=from)", " count(1) ");
case "postgresql":
case "sqlserver":
int lastIndexOfOrderBy = sourceSql.toLowerCase().lastIndexOf("order by");
if (lastIndexOfOrderBy != -1) {
return sourceSql.substring(0, lastIndexOfOrderBy).replaceAll("(?i)(?<=select)(.*)(?=from)", " count(1) ");
Expand Down
3 changes: 3 additions & 0 deletions common/src/test/java/io/seata/common/util/PageUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,15 @@ public void testPageSql() {
String oracleTargetSql = "select * from " +
"( select ROWNUM rn, temp.* from (select * from test where a = 1) temp )" +
" where rn between 1 and 5";
String sqlserverTargetSql = "select * from (select temp.*, ROW_NUMBER() OVER(ORDER BY (select NULL)) AS rowId from (select * from test where a = 1) temp ) t where t.rowId between 1 and 5";

assertEquals(PageUtil.pageSql(sourceSql, "mysql", 1, 5), mysqlTargetSql);
assertEquals(PageUtil.pageSql(sourceSql, "h2", 1, 5), mysqlTargetSql);
assertEquals(PageUtil.pageSql(sourceSql, "postgresql", 1, 5), mysqlTargetSql);
assertEquals(PageUtil.pageSql(sourceSql, "oceanbase", 1, 5), mysqlTargetSql);
assertEquals(PageUtil.pageSql(sourceSql, "dm", 1, 5), mysqlTargetSql);
assertEquals(PageUtil.pageSql(sourceSql, "oracle", 1, 5), oracleTargetSql);
assertEquals(PageUtil.pageSql(sourceSql, "sqlserver", 1, 5), sqlserverTargetSql);

assertThrows(NotSupportYetException.class, () -> PageUtil.pageSql(sourceSql, "xxx", 1, 5));
}
Expand All @@ -60,6 +62,7 @@ void testCountSql() {
assertEquals(PageUtil.countSql(sourceSql, "oceanbase"), targetSql);
assertEquals(PageUtil.countSql(sourceSql, "dm"), targetSql);
assertEquals(PageUtil.countSql(sourceSql, "oracle"), targetSql);
assertEquals(PageUtil.countSql(sourceSql, "sqlserver"), targetSql);

assertThrows(NotSupportYetException.class, () -> PageUtil.countSql(sourceSql, "xxx"));
}
Expand Down

0 comments on commit 2626f24

Please sign in to comment.