!364 SQLSERVER_2005 多表分页bug修复

Merge pull request !364 from 简单风/fix-bug-#I87AOA
This commit is contained in:
Michael Yang 2023-10-12 08:26:52 +00:00 committed by Gitee
commit d5cf8f53ea
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
2 changed files with 42 additions and 1 deletions

View File

@ -113,7 +113,10 @@ public interface LimitOffsetProcessor {
limitOffset = 0L;
}
// fix-bug:#I87AOA QueryWrapper 构建的SQL 执行的SQL不一致
List<QueryTable> queryTables = CPI.getQueryTables(queryWrapper);
List<QueryTable> joinTables = CPI.getJoinTables(queryWrapper);
List<QueryTable> allTables = CollectionUtil.merge(queryTables, joinTables);
String originalSQL = sql.toString();
String orderByString;
List<QueryOrderBy> orderBys = CPI.getOrderBys(queryWrapper);
@ -123,7 +126,7 @@ public interface LimitOffsetProcessor {
StringBuilder orderBySql = new StringBuilder(ORDER_BY);
int index = 0;
for (QueryOrderBy orderBy : orderBys) {
orderBySql.append(orderBy.toSql(queryTables, dialect));
orderBySql.append(orderBy.toSql(allTables, dialect));
if (index != orderBys.size() - 1) {
orderBySql.append(DELIMITER);
}

View File

@ -0,0 +1,38 @@
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;
import org.junit.Assert;
import org.junit.Test;
import static com.mybatisflex.coretest.table.AccountTableDef.ACCOUNT;
import static com.mybatisflex.coretest.table.ArticleTableDef.ARTICLE;
public class LimitOffsetProcessorTester {
@Test
public void testSqlServer2005() {
IDialect dialect = new CommonsDialectImpl(KeywordWrap.SQUARE_BRACKETS, LimitOffsetProcessor.SQLSERVER_2005);
QueryWrapper oneTableQueryWrapper = QueryWrapper.create()
.select()
.from(ACCOUNT)
.orderBy(ACCOUNT.ID.desc()).limit(10, 10);
String sql = dialect.forSelectByQuery(oneTableQueryWrapper);
System.out.println(sql);
Assert.assertEquals("WITH temp_datas AS(SELECT ROW_NUMBER() OVER ( ORDER BY [id] DESC) as __rn, * FROM [tb_account]) SELECT * FROM temp_datas WHERE __rn BETWEEN 11 AND 20 ORDER BY __rn", sql);
QueryWrapper twoTablequeryWrapper = QueryWrapper.create()
.select()
.from(ACCOUNT)
.leftJoin(ARTICLE).on(ARTICLE.ACCOUNT_ID.eq(ACCOUNT.ID))
.orderBy(ACCOUNT.ID.desc()).limit(10, 10);
sql = dialect.forSelectByQuery(twoTablequeryWrapper);
System.out.println(sql);
Assert.assertEquals("WITH temp_datas AS(" +
"SELECT ROW_NUMBER() OVER ( ORDER BY [tb_account].[id] DESC) as __rn, * FROM [tb_account] LEFT JOIN [tb_article] ON [tb_article].[account_id] = [tb_account].[id]" +
") SELECT * FROM temp_datas WHERE __rn BETWEEN 11 AND 20 ORDER BY __rn", sql);
}
}