diff --git a/src/main/java/com/thinkgem/jeesite/common/persistence/dialect/db/SQLServer2005Dialect.java b/src/main/java/com/thinkgem/jeesite/common/persistence/dialect/db/SQLServer2005Dialect.java index 7da688e7e1722576fabbf1b4fbde43033545c2ca..29a363ab1ab0f5210ade4a831f9320617fce2112 100644 --- a/src/main/java/com/thinkgem/jeesite/common/persistence/dialect/db/SQLServer2005Dialect.java +++ b/src/main/java/com/thinkgem/jeesite/common/persistence/dialect/db/SQLServer2005Dialect.java @@ -83,7 +83,7 @@ public class SQLServer2005Dialect implements Dialect { static String getOrderByPart(String sql) { String loweredString = sql.toLowerCase(); - int orderByIndex = loweredString.indexOf("order by"); + int orderByIndex = loweredString.lastIndexOf("order by"); if (orderByIndex != -1) { // if we find a new "order by" then we need to ignore // the previous one since it was probably used for a subquery diff --git a/src/main/java/com/thinkgem/jeesite/common/persistence/interceptor/SQLHelper.java b/src/main/java/com/thinkgem/jeesite/common/persistence/interceptor/SQLHelper.java index a82d4e12c92f82df55ec8f34bb11bc6a3264f90d..d3c2054031ba40a8a149f7b90d9652bd0616924b 100644 --- a/src/main/java/com/thinkgem/jeesite/common/persistence/interceptor/SQLHelper.java +++ b/src/main/java/com/thinkgem/jeesite/common/persistence/interceptor/SQLHelper.java @@ -181,15 +181,38 @@ public class SQLHelper { * @return */ @SuppressWarnings("unused") - private static String removeOrders(String qlString) { - Pattern p = Pattern.compile("order\\s*by[\\w|\\W|\\s|\\S]*", Pattern.CASE_INSENSITIVE); - Matcher m = p.matcher(qlString); - StringBuffer sb = new StringBuffer(); - while (m.find()) { - m.appendReplacement(sb, ""); + private static String removeOrders(String sqlString) { + String dbName = Global.getConfig("jdbc.type"); + //存储每个partition + List partitionGroups = new ArrayList<>(); + if ("mssql".equals(dbName)) { + //替换mssql中的partition操作 + Pattern partitionPattern = + Pattern.compile("(?<=ROW_NUMBER[\\s|\\S]?\\([\\s|\\S]?\\)[\\s|\\S]?OVER[\\s|\\S]?\\()[^\\)]+", + Pattern.CASE_INSENSITIVE); + Matcher partitionMatcher = partitionPattern.matcher(sqlString); + while (partitionMatcher.find()) { + partitionGroups.add(partitionMatcher.group()); + } + sqlString = partitionMatcher.replaceAll("{{partition}}"); + } + Pattern p = Pattern.compile("order\\s*by[\\w|\\W|\\s|\\S]*", Pattern.CASE_INSENSITIVE); + Matcher m = p.matcher(sqlString); + sqlString = m.replaceAll(""); + if ("mssql".equals(dbName)) { + //还原每个partition + Pattern p1 = Pattern.compile("\\{\\{partition\\}\\}", Pattern.CASE_INSENSITIVE); + Matcher m1 = p1.matcher(sqlString); + StringBuffer stringBuffer = new StringBuffer(); + int j = 0; + while (m1.find()) { + m1.appendReplacement(stringBuffer, partitionGroups.get(j)); + j++; + } + m1.appendTail(stringBuffer); + sqlString = stringBuffer.toString(); } - m.appendTail(sb); - return sb.toString(); + return sqlString; } }