mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +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.lang.Assert;
|
||||
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.db.dialect.Dialect;
|
||||
import org.dromara.hutool.db.dialect.DialectFactory;
|
||||
@ -28,6 +29,8 @@ import java.io.Serializable;
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
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 {
|
||||
checkConn(conn);
|
||||
|
||||
String selectSql = sqlBuilder.build();
|
||||
// 去除最后的order by 子句
|
||||
final int orderByIndex = StrUtil.lastIndexOfIgnoreCase(selectSql, " order by");
|
||||
if (orderByIndex > 0) {
|
||||
selectSql = StrUtil.subPre(selectSql, orderByIndex);
|
||||
|
||||
// 去除order by 子句
|
||||
final Pattern pattern = PatternPool.get("(.*?)[\\s]order[\\s]by[\\s][^\\s]+\\s(asc|desc)?", Pattern.CASE_INSENSITIVE);
|
||||
final Matcher matcher = pattern.matcher(selectSql);
|
||||
if (matcher.matches()) {
|
||||
selectSql = matcher.group(1);
|
||||
}
|
||||
|
||||
try {
|
||||
return SqlExecutor.queryAndClosePs(dialect.psForCount(conn,
|
||||
SqlBuilder.of(selectSql).addParams(sqlBuilder.getParamValueArray())),
|
||||
|
Loading…
x
Reference in New Issue
Block a user