From bd091bff5cf020bcd2f0037d2619390e0a1f672a Mon Sep 17 00:00:00 2001 From: MaxKey Date: Sun, 16 May 2021 15:25:14 +0800 Subject: [PATCH] fix query count fix query count --- .../provider/SqlProviderQuery.java | 30 ++++++++++++++----- .../jpa/persistence/provider/SqlSyntax.java | 27 +++++++++++++++++ .../config/applicationConfig.properties | 16 +++++----- .../persistence/xml/mysql/StudentsMapper.xml | 2 ++ .../resources/spring/applicationContext.xml | 4 +-- 5 files changed, 62 insertions(+), 17 deletions(-) create mode 100644 mybatis-jpa-extra-core/src/main/java/org/apache/mybatis/jpa/persistence/provider/SqlSyntax.java diff --git a/mybatis-jpa-extra-core/src/main/java/org/apache/mybatis/jpa/persistence/provider/SqlProviderQuery.java b/mybatis-jpa-extra-core/src/main/java/org/apache/mybatis/jpa/persistence/provider/SqlProviderQuery.java index 7ff298e..c68a3f9 100644 --- a/mybatis-jpa-extra-core/src/main/java/org/apache/mybatis/jpa/persistence/provider/SqlProviderQuery.java +++ b/mybatis-jpa-extra-core/src/main/java/org/apache/mybatis/jpa/persistence/provider/SqlProviderQuery.java @@ -106,10 +106,12 @@ public class SqlProviderQuery { JpaPagination pagination=(JpaPagination)entity; //获取缓存数据 PageResultsSqlCache pageResultsSqlCache=JpaBaseService.pageResultsBoundSqlCache.get(pagination.getPageResultSelectUUID()); - String selectSql=pageResultsSqlCache.getSql(); + //多个空格 tab 替换成1个空格 + String selectSql=pageResultsSqlCache.getSql().replaceAll("\t", " ").replaceAll(" +"," "); BoundSql boundSql=(BoundSql)pageResultsSqlCache.getBoundSql(); + _logger.trace("Count original SQL :\n" + selectSql); - StringBuffer sql=new StringBuffer(); + StringBuffer sql=new StringBuffer(SqlSyntax.SELECT +" "+ SqlSyntax.Functions.COUNT_ONE +" countrows_ "); StringBuffer countSql=new StringBuffer(); if(boundSql.getParameterMappings()==null ||boundSql.getParameterMappings().isEmpty()) { @@ -122,13 +124,27 @@ public class SqlProviderQuery { } countSql.append(selectSql); } + String countSqlLowerCase = countSql.toString().toLowerCase(); + _logger.trace("Count SQL LowerCase :\n" + countSqlLowerCase); - if(countSql.toString().toLowerCase().indexOf("distinct")>0) { - sql.append("select count(1) countrows_ from (").append(countSql).append(" ) count_table_"); + if(countSqlLowerCase.indexOf(SqlSyntax.DISTINCT + " ")>0 //去重 + ||countSqlLowerCase.indexOf(" " + SqlSyntax.GROUPBY + " ")>0 //分组 + ||countSqlLowerCase.indexOf(" " + SqlSyntax.HAVING + " ")>0 //聚合函数 + ||(countSqlLowerCase.indexOf(" " + SqlSyntax.FROM + " ") + != countSqlLowerCase.lastIndexOf(" " + SqlSyntax.FROM + " ") + ) //嵌套 + ) { + _logger.trace("Count SQL Complex "); + sql.append(SqlSyntax.FROM).append(" (").append(countSql).append(" ) count_table_"); }else { - sql.append("select count(1) countrows_ ").append( - countSql.substring(countSql.toString().toLowerCase().indexOf("from")) - ); + int fromIndex = countSqlLowerCase.indexOf(" " + SqlSyntax.FROM + " "); + int orderByIndex = countSqlLowerCase.indexOf(" " + SqlSyntax.ORDERBY + " "); + _logger.trace("Count SQL from Index "+ fromIndex +" , order by " +orderByIndex); + if(orderByIndex > -1) { + sql.append(countSql.substring(fromIndex,orderByIndex)); + }else { + sql.append(countSql.substring(fromIndex)); + } } //删除缓存 JpaBaseService.pageResultsBoundSqlCache.remove(pagination.getPageResultSelectUUID()); diff --git a/mybatis-jpa-extra-core/src/main/java/org/apache/mybatis/jpa/persistence/provider/SqlSyntax.java b/mybatis-jpa-extra-core/src/main/java/org/apache/mybatis/jpa/persistence/provider/SqlSyntax.java new file mode 100644 index 0000000..57ce781 --- /dev/null +++ b/mybatis-jpa-extra-core/src/main/java/org/apache/mybatis/jpa/persistence/provider/SqlSyntax.java @@ -0,0 +1,27 @@ +package org.apache.mybatis.jpa.persistence.provider; + +public class SqlSyntax { + + public final static String SELECT = "select"; + + public final static String DISTINCT = "distinct"; + + public final static String FROM = "from"; + + public final static String GROUPBY = "group by"; + + public final static String ORDERBY = "order by"; + + public final static String HAVING = "having"; + + public class Functions{ + + public static final String COUNT_ALL = "count(*)"; + + public static final String COUNT_ONE = "count(1)"; + } + + + + +} diff --git a/mybatis-jpa-extra-core/src/test/resources/config/applicationConfig.properties b/mybatis-jpa-extra-core/src/test/resources/config/applicationConfig.properties index 7e033c9..2364c63 100644 --- a/mybatis-jpa-extra-core/src/test/resources/config/applicationConfig.properties +++ b/mybatis-jpa-extra-core/src/test/resources/config/applicationConfig.properties @@ -21,15 +21,15 @@ config.datasource.database=postgresql # for SyBase jdbc:sybase:Tds:hostname:port/secdb # for Derby jdbc:derby://localhost:1527/secdb # -config.datasource.driverclass=org.postgresql.Driver -config.datasource.url=jdbc:postgresql://localhost:5432/postgres -config.datasource.username=postgres -config.datasource.password=postgresql +#config.datasource.driverclass=org.postgresql.Driver +#config.datasource.url=jdbc:postgresql://localhost:5432/postgres +#config.datasource.username=postgres +#config.datasource.password=postgresql -#config.datasource.driverclass=com.mysql.jdbc.Driver -#config.datasource.url=jdbc:mysql://localhost/test?autoReconnect=false&characterEncoding=UTF-8&serverTimezone=UTC -#config.datasource.username=root -#config.datasource.password=maxkey +config.datasource.driverclass=com.mysql.jdbc.Driver +config.datasource.url=jdbc:mysql://localhost/test?autoReconnect=false&characterEncoding=UTF-8&serverTimezone=UTC +config.datasource.username=root +config.datasource.password=maxkey ############################################################################ \ No newline at end of file diff --git a/mybatis-jpa-extra-core/src/test/resources/org/apache/mybatis/jpa/test/dao/persistence/xml/mysql/StudentsMapper.xml b/mybatis-jpa-extra-core/src/test/resources/org/apache/mybatis/jpa/test/dao/persistence/xml/mysql/StudentsMapper.xml index ca6ad1d..bdc107c 100644 --- a/mybatis-jpa-extra-core/src/test/resources/org/apache/mybatis/jpa/test/dao/persistence/xml/mysql/StudentsMapper.xml +++ b/mybatis-jpa-extra-core/src/test/resources/org/apache/mybatis/jpa/test/dao/persistence/xml/mysql/StudentsMapper.xml @@ -30,6 +30,8 @@ FROM STUDENTS + + order by STDAGE diff --git a/mybatis-jpa-extra-core/src/test/resources/spring/applicationContext.xml b/mybatis-jpa-extra-core/src/test/resources/spring/applicationContext.xml index 448427d..e80b87e 100644 --- a/mybatis-jpa-extra-core/src/test/resources/spring/applicationContext.xml +++ b/mybatis-jpa-extra-core/src/test/resources/spring/applicationContext.xml @@ -72,9 +72,9 @@ - + - +