diff --git a/core/core-backend/src/main/java/io/dataease/chart/charts/impl/table/TableInfoHandler.java b/core/core-backend/src/main/java/io/dataease/chart/charts/impl/table/TableInfoHandler.java index e6e2b7da6d..cf465f3aed 100644 --- a/core/core-backend/src/main/java/io/dataease/chart/charts/impl/table/TableInfoHandler.java +++ b/core/core-backend/src/main/java/io/dataease/chart/charts/impl/table/TableInfoHandler.java @@ -90,7 +90,7 @@ public class TableInfoHandler extends DefaultChartHandler { var querySql = originSql + limit; var tablePageMode = (String) filterResult.getContext().get("tablePageMode"); - var totalPageSql = "SELECT COUNT(*) FROM (" + SQLProvider.createQuerySQL(sqlMeta, false, false, view) + ") COUNT_TEMP"; + var totalPageSql = "SELECT COUNT(*) FROM (" + SQLProvider.createQuerySQLNoSort(sqlMeta, false, view) + ") COUNT_TEMP"; if (StringUtils.isNotEmpty(totalPageSql) && StringUtils.equalsIgnoreCase(tablePageMode, "page")) { totalPageSql = provider.rebuildSQL(totalPageSql, sqlMeta, crossDs, dsMap); datasourceRequest.setQuery(totalPageSql); diff --git a/core/core-backend/src/main/java/io/dataease/engine/sql/SQLProvider.java b/core/core-backend/src/main/java/io/dataease/engine/sql/SQLProvider.java index 3ac52442fb..1b05e4424d 100644 --- a/core/core-backend/src/main/java/io/dataease/engine/sql/SQLProvider.java +++ b/core/core-backend/src/main/java/io/dataease/engine/sql/SQLProvider.java @@ -1,9 +1,9 @@ package io.dataease.engine.sql; +import io.dataease.engine.constant.SQLConstants; import io.dataease.extensions.datasource.model.SQLMeta; import io.dataease.extensions.datasource.model.SQLObj; import io.dataease.extensions.view.dto.ChartViewDTO; -import io.dataease.engine.constant.SQLConstants; import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.StringUtils; import org.stringtemplate.v4.ST; @@ -132,6 +132,48 @@ public class SQLProvider { return sqlLimit(st.render(), view); } + public static String createQuerySQLNoSort(SQLMeta sqlMeta, boolean isGroup, ChartViewDTO view) { + STGroup stg = new STGroupString(SqlTemplate.PREVIEW_SQL); + ST st_sql = stg.getInstanceOf("previewSql"); + + st_sql.add("isGroup", isGroup); + + SQLObj tableObj = sqlMeta.getTable(); + if (ObjectUtils.isNotEmpty(tableObj)) st_sql.add("table", tableObj); + + List xFields = sqlMeta.getXFields(); + if (ObjectUtils.isNotEmpty(xFields)) st_sql.add("groups", xFields); + + List yFields = sqlMeta.getYFields(); + List yWheres = sqlMeta.getYWheres(); + if (ObjectUtils.isNotEmpty(yFields)) st_sql.add("aggregators", yFields); + + String customWheres = sqlMeta.getCustomWheres(); + String extWheres = sqlMeta.getExtWheres(); + String whereTrees = sqlMeta.getWhereTrees(); + List wheres = new ArrayList<>(); + if (customWheres != null) wheres.add(customWheres); + if (extWheres != null) wheres.add(extWheres); + if (whereTrees != null) wheres.add(whereTrees); + if (ObjectUtils.isNotEmpty(wheres)) st_sql.add("filters", wheres); + String sql = st_sql.render(); + + ST st = stg.getInstanceOf("previewSql"); + st_sql.add("isGroup", isGroup); + + SQLObj tableSQL = SQLObj.builder() + .tableName(String.format(SQLConstants.BRACKETS, sql)) + .tableAlias(String.format(SQLConstants.TABLE_ALIAS_PREFIX, 1)) + .build(); + if (ObjectUtils.isNotEmpty(tableSQL)) st.add("table", tableSQL); + + List aggWheres = new ArrayList<>(); + if (ObjectUtils.isNotEmpty(yWheres)) aggWheres.addAll(yWheres); + if (ObjectUtils.isNotEmpty(aggWheres)) st.add("filters", aggWheres); + + return sqlLimit(st.render(), view); + } + public static String sqlLimit(String sql, ChartViewDTO view) { if (StringUtils.equalsIgnoreCase(view.getType(), "table-info")) { return sql;