mirror of
https://gitee.com/dromara/hutool.git
synced 2025-07-15 23:13:33 +08:00
修复嵌套SQL中order by子句错误截断问题
This commit is contained in:
parent
79b38c19c9
commit
53d755c817
@ -16,6 +16,7 @@ import org.dromara.hutool.core.array.ArrayUtil;
|
|||||||
import org.dromara.hutool.core.io.IoUtil;
|
import org.dromara.hutool.core.io.IoUtil;
|
||||||
import org.dromara.hutool.core.lang.Assert;
|
import org.dromara.hutool.core.lang.Assert;
|
||||||
import org.dromara.hutool.core.map.MapUtil;
|
import org.dromara.hutool.core.map.MapUtil;
|
||||||
|
import org.dromara.hutool.core.regex.PatternPool;
|
||||||
import org.dromara.hutool.core.text.StrUtil;
|
import org.dromara.hutool.core.text.StrUtil;
|
||||||
import org.dromara.hutool.db.dialect.Dialect;
|
import org.dromara.hutool.db.dialect.Dialect;
|
||||||
import org.dromara.hutool.db.dialect.DialectFactory;
|
import org.dromara.hutool.db.dialect.DialectFactory;
|
||||||
@ -28,6 +29,8 @@ import java.io.Serializable;
|
|||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.sql.PreparedStatement;
|
import java.sql.PreparedStatement;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 提供基于方言的原始增删改查执行封装
|
* 提供基于方言的原始增删改查执行封装
|
||||||
@ -297,13 +300,15 @@ public class DialectRunner implements Serializable {
|
|||||||
*/
|
*/
|
||||||
public long count(final Connection conn, final SqlBuilder sqlBuilder) throws DbRuntimeException {
|
public long count(final Connection conn, final SqlBuilder sqlBuilder) throws DbRuntimeException {
|
||||||
checkConn(conn);
|
checkConn(conn);
|
||||||
|
|
||||||
String selectSql = sqlBuilder.build();
|
String selectSql = sqlBuilder.build();
|
||||||
// 去除最后的order by 子句
|
|
||||||
final int orderByIndex = StrUtil.lastIndexOfIgnoreCase(selectSql, " order by");
|
// 去除order by 子句
|
||||||
if (orderByIndex > 0) {
|
final Pattern pattern = PatternPool.get("(.*?)[\\s]order[\\s]by[\\s][^\\s]+\\s(asc|desc)?", Pattern.CASE_INSENSITIVE);
|
||||||
selectSql = StrUtil.subPre(selectSql, orderByIndex);
|
final Matcher matcher = pattern.matcher(selectSql);
|
||||||
|
if (matcher.matches()) {
|
||||||
|
selectSql = matcher.group(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return SqlExecutor.queryAndClosePs(dialect.psForCount(conn,
|
return SqlExecutor.queryAndClosePs(dialect.psForCount(conn,
|
||||||
SqlBuilder.of(selectSql).addParams(sqlBuilder.getParamValueArray())),
|
SqlBuilder.of(selectSql).addParams(sqlBuilder.getParamValueArray())),
|
||||||
|
Loading…
Reference in New Issue
Block a user