nativePageSql

This commit is contained in:
redkale
2024-06-06 17:00:35 +08:00
parent 2e8c183c3a
commit 43012a5399

View File

@@ -80,7 +80,7 @@ public abstract class AbstractDataSqlSource extends AbstractDataSource
((CompletableFuture<Sheet>) querySheetDBAsync(i, false, false, false, null, null, (FilterNode) null)) ((CompletableFuture<Sheet>) querySheetDBAsync(i, false, false, false, null, null, (FilterNode) null))
.thenApply(e -> e == null ? new ArrayList() : e.list(true)); .thenApply(e -> e == null ? new ArrayList() : e.list(true));
protected final IntFunction<String> signFunc = index -> prepareParamSign(index); protected final IntFunction<String> signFunc = this::prepareParamSign;
// Flipper.sort转换成以ORDER BY开头SQL的缓存 // Flipper.sort转换成以ORDER BY开头SQL的缓存
protected final Map<String, String> sortOrderbySqls = new ConcurrentHashMap<>(); protected final Map<String, String> sortOrderbySqls = new ConcurrentHashMap<>();
@@ -161,23 +161,21 @@ public abstract class AbstractDataSqlSource extends AbstractDataSource
protected <T> PageCountSql createPageCountSql( protected <T> PageCountSql createPageCountSql(
EntityInfo<T> info, EntityInfo<T> info,
final boolean readCache, boolean readCache,
boolean needTotal, boolean needTotal,
final boolean distinct, boolean distinct,
SelectColumn selects, SelectColumn selects,
String[] tables, String[] tables,
Flipper flipper, Flipper flipper,
FilterNode node) { FilterNode node) {
final Map<Class, String> joinTabalis = node == null ? null : node.getJoinTabalis(); final Map<Class, String> joinTabalis = node == null ? null : node.getJoinTabalis();
final CharSequence join =
node == null ? null : node.createSQLJoin(this, false, joinTabalis, new HashSet<>(), info);
final CharSequence where = node == null ? null : node.createSQLExpress(this, info, joinTabalis); final CharSequence where = node == null ? null : node.createSQLExpress(this, info, joinTabalis);
final String joinAndWhere = CharSequence join = node == null ? null : node.createSQLJoin(this, false, joinTabalis, new HashSet<>(), info);
(join == null ? "" : join) + ((where == null || where.length() == 0) ? "" : (" WHERE " + where)); final String joinAndWhere = (join == null ? "" : join) + (Utility.isEmpty(where) ? "" : (" WHERE " + where));
String pageSql = null; String pageSql = null;
String countSql = null; String countSql = null;
boolean containsLimit = false; boolean containsLimit = false;
{ // 组装pageSql、countSql // 组装pageSql、countSql
String listSubSql; String listSubSql;
StringBuilder union = new StringBuilder(); StringBuilder union = new StringBuilder();
if (tables.length == 1) { if (tables.length == 1) {
@@ -204,8 +202,8 @@ public abstract class AbstractDataSqlSource extends AbstractDataSource
if ("oracle".equals(dbtype)) { if ("oracle".equals(dbtype)) {
int start = flipper.getOffset(); int start = flipper.getOffset();
int end = flipper.getOffset() + flipper.getLimit(); int end = flipper.getOffset() + flipper.getLimit();
pageSql = "SELECT * FROM (SELECT T_.*, ROWNUM RN_ FROM (" + pageSql + ") T_) WHERE RN_ BETWEEN " pageSql = "SELECT * FROM (SELECT T_.*, ROWNUM RN_ FROM (" + pageSql + ") T_) WHERE RN_ BETWEEN " + start
+ start + " AND " + end; + " AND " + end;
containsLimit = true; containsLimit = true;
} else if ("sqlserver".equals(dbtype)) { } else if ("sqlserver".equals(dbtype)) {
int offset = flipper.getOffset(); int offset = flipper.getOffset();
@@ -231,7 +229,6 @@ public abstract class AbstractDataSqlSource extends AbstractDataSource
} }
countSql = countSubSql; countSql = countSubSql;
} }
}
if (readCache && info.isLoggable(logger, Level.FINEST, pageSql)) { if (readCache && info.isLoggable(logger, Level.FINEST, pageSql)) {
String prefix = needTotal ? " querySheet" : " queryList"; String prefix = needTotal ? " querySheet" : " queryList";
if (countSql != null) { if (countSql != null) {
@@ -240,16 +237,13 @@ public abstract class AbstractDataSqlSource extends AbstractDataSource
logger.finest( logger.finest(
info.getType().getSimpleName() + prefix + (needTotal ? " page-sql=" : " list-sql=") + pageSql); info.getType().getSimpleName() + prefix + (needTotal ? " page-sql=" : " list-sql=") + pageSql);
} }
PageCountSql rs = new PageCountSql(); return new PageCountSql(pageSql, countSql, containsLimit);
rs.pageSql = pageSql;
rs.countSql = countSql;
rs.pageContainsLimit = containsLimit;
return rs;
} }
/** /**
* 根据Flipper获取ORDER BY的SQL语句不存在Flipper或sort字段返回空字符串 * 根据Flipper获取ORDER BY的SQL语句不存在Flipper或sort字段返回空字符串
* *
* @param info EntityInfo
* @param flipper 翻页对象 * @param flipper 翻页对象
* @return String * @return String
*/ */
@@ -3990,12 +3984,18 @@ public abstract class AbstractDataSqlSource extends AbstractDataSource
protected static class PageCountSql { protected static class PageCountSql {
public String pageSql; public final String pageSql;
@Nullable @Nullable
public String countSql; public final String countSql;
public boolean pageContainsLimit; public final boolean pageContainsLimit;
public PageCountSql(String pageSql, String countSql, boolean pageContainsLimit) {
this.pageSql = pageSql;
this.countSql = countSql;
this.pageContainsLimit = pageContainsLimit;
}
@Override @Override
public String toString() { public String toString() {