提交 bd091bff 编写于 作者: M MaxKey

fix query count

fix query count
上级 d8ed513b
...@@ -106,10 +106,12 @@ public class SqlProviderQuery <T extends JpaBaseEntity>{ ...@@ -106,10 +106,12 @@ public class SqlProviderQuery <T extends JpaBaseEntity>{
JpaPagination pagination=(JpaPagination)entity; JpaPagination pagination=(JpaPagination)entity;
//获取缓存数据 //获取缓存数据
PageResultsSqlCache pageResultsSqlCache=JpaBaseService.pageResultsBoundSqlCache.get(pagination.getPageResultSelectUUID()); 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(); 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(); StringBuffer countSql=new StringBuffer();
if(boundSql.getParameterMappings()==null ||boundSql.getParameterMappings().isEmpty()) { if(boundSql.getParameterMappings()==null ||boundSql.getParameterMappings().isEmpty()) {
...@@ -122,13 +124,27 @@ public class SqlProviderQuery <T extends JpaBaseEntity>{ ...@@ -122,13 +124,27 @@ public class SqlProviderQuery <T extends JpaBaseEntity>{
} }
countSql.append(selectSql); countSql.append(selectSql);
} }
String countSqlLowerCase = countSql.toString().toLowerCase();
_logger.trace("Count SQL LowerCase :\n" + countSqlLowerCase);
if(countSql.toString().toLowerCase().indexOf("distinct")>0) { if(countSqlLowerCase.indexOf(SqlSyntax.DISTINCT + " ")>0 //去重
sql.append("select count(1) countrows_ from (").append(countSql).append(" ) count_table_"); ||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 { }else {
sql.append("select count(1) countrows_ ").append( int fromIndex = countSqlLowerCase.indexOf(" " + SqlSyntax.FROM + " ");
countSql.substring(countSql.toString().toLowerCase().indexOf("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()); JpaBaseService.pageResultsBoundSqlCache.remove(pagination.getPageResultSelectUUID());
......
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)";
}
}
...@@ -21,15 +21,15 @@ config.datasource.database=postgresql ...@@ -21,15 +21,15 @@ config.datasource.database=postgresql
# for SyBase jdbc:sybase:Tds:hostname:port/secdb # for SyBase jdbc:sybase:Tds:hostname:port/secdb
# for Derby jdbc:derby://localhost:1527/secdb # for Derby jdbc:derby://localhost:1527/secdb
# #
config.datasource.driverclass=org.postgresql.Driver #config.datasource.driverclass=org.postgresql.Driver
config.datasource.url=jdbc:postgresql://localhost:5432/postgres #config.datasource.url=jdbc:postgresql://localhost:5432/postgres
config.datasource.username=postgres #config.datasource.username=postgres
config.datasource.password=postgresql #config.datasource.password=postgresql
#config.datasource.driverclass=com.mysql.jdbc.Driver config.datasource.driverclass=com.mysql.jdbc.Driver
#config.datasource.url=jdbc:mysql://localhost/test?autoReconnect=false&characterEncoding=UTF-8&serverTimezone=UTC config.datasource.url=jdbc:mysql://localhost/test?autoReconnect=false&characterEncoding=UTF-8&serverTimezone=UTC
#config.datasource.username=root config.datasource.username=root
#config.datasource.password=maxkey config.datasource.password=maxkey
############################################################################ ############################################################################
\ No newline at end of file
...@@ -30,6 +30,8 @@ ...@@ -30,6 +30,8 @@
FROM STUDENTS FROM STUDENTS
<include refid="sql_condition"/> <include refid="sql_condition"/>
<!-- group by STDMAJOR -->
order by STDAGE
</select> </select>
......
...@@ -72,9 +72,9 @@ ...@@ -72,9 +72,9 @@
<!--<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">--> <!--<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">-->
<bean id="sqlSessionFactory" class="org.apache.mybatis.jpa.MyBatisSessionFactoryBean"> <bean id="sqlSessionFactory" class="org.apache.mybatis.jpa.MyBatisSessionFactoryBean">
<property name="timeout" value="30" /> <property name="timeout" value="30" />
<property name="dialect" value="postgresql" /> <property name="dialect" value="mysql" />
<property name="dataSource" ref="dataSource" /> <property name="dataSource" ref="dataSource" />
<property name="mapperLocations" value="classpath*:/org/apache/mybatis/jpa/test/dao/persistence/xml/postgresql/*.xml" /> <property name="mapperLocations" value="classpath*:/org/apache/mybatis/jpa/test/dao/persistence/xml/mysql/*.xml" />
<property name="typeAliasesPackage" <property name="typeAliasesPackage"
value=" value="
org.apache.mybatis.jpa.test.entity, org.apache.mybatis.jpa.test.entity,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册