feat: add sqlServer 2005 LimitOffsetProcessor

This commit is contained in:
开源海哥 2023-07-10 16:17:08 +08:00
parent a2d356050a
commit 722808b6ed
2 changed files with 6 additions and 22 deletions

View File

@ -111,26 +111,7 @@ public interface LimitOffsetProcessor {
}
List<QueryTable> queryTables = CPI.getQueryTables(queryWrapper);
// String selectColumnString;
// List<QueryColumn> selectColumns = CPI.getSelectColumns(queryWrapper);
// if (selectColumns == null || selectColumns.isEmpty()){
// selectColumnString = "*";
// }else {
// StringBuilder columnsSql = new StringBuilder();
// int index = 0;
// for (QueryColumn selectColumn : selectColumns) {
// String selectColumnSql = CPI.toSelectSql(selectColumn, queryTables, dialect);
// columnsSql.append(selectColumnSql);
// if (index != selectColumns.size() - 1) {
// columnsSql.append(DELIMITER);
// }
// index++;
// }
// selectColumnString = columnsSql.toString();
// }
String originalSQL = sql.toString();
String orderByString;
List<QueryOrderBy> orderBys = CPI.getOrderBys(queryWrapper);
if (orderBys == null || orderBys.isEmpty()) {
@ -145,11 +126,12 @@ public interface LimitOffsetProcessor {
}
index++;
}
originalSQL = originalSQL.substring(0, sql.lastIndexOf(ORDER_BY));
orderByString = orderBySql.toString();
}
StringBuilder newSql = new StringBuilder("WITH temp_datas AS(");
newSql.append("SELECT ROW_NUMBER() OVER (").append(orderByString).append(") as __rn,").append(sql.substring(6));
newSql.append("SELECT ROW_NUMBER() OVER (").append(orderByString).append(") as __rn,").append(originalSQL.substring(6));
newSql.append(")");
newSql.append(" SELECT * FROM temp_datas WHERE __rn BETWEEN ").append(limitOffset + 1).append(" AND ").append(limitOffset + limitRows);
newSql.append(" ORDER BY __rn");

View File

@ -1,6 +1,7 @@
package com.mybatisflex.coretest;
import com.mybatisflex.core.dialect.IDialect;
import com.mybatisflex.core.dialect.KeywordWrap;
import com.mybatisflex.core.dialect.LimitOffsetProcessor;
import com.mybatisflex.core.dialect.impl.CommonsDialectImpl;
import com.mybatisflex.core.query.QueryWrapper;
@ -18,9 +19,10 @@ public class SqlServer2005DialectTester {
.from(ACCOUNT)
.where(ACCOUNT.ID.in("100","200"))
.and(ACCOUNT.SEX.eq(1))
.orderBy(ACCOUNT.ID.desc())
.limit(10,10);
IDialect dialect = new CommonsDialectImpl(LimitOffsetProcessor.SQLSERVER_2005);
IDialect dialect = new CommonsDialectImpl(KeywordWrap.SQUARE_BRACKETS,LimitOffsetProcessor.SQLSERVER_2005);
String sql = dialect.forSelectByQuery(query);
System.out.println(sql);
}