mirror of
https://gitee.com/dromara/hutool.git
synced 2025-11-24 16:43:24 +08:00
add SqlUtil.removeOuterOrderBy
This commit is contained in:
@@ -20,7 +20,6 @@ import cn.hutool.v7.core.array.ArrayUtil;
|
||||
import cn.hutool.v7.core.io.IoUtil;
|
||||
import cn.hutool.v7.core.lang.Assert;
|
||||
import cn.hutool.v7.core.map.MapUtil;
|
||||
import cn.hutool.v7.core.regex.PatternPool;
|
||||
import cn.hutool.v7.core.text.StrUtil;
|
||||
import cn.hutool.v7.db.config.DbConfig;
|
||||
import cn.hutool.v7.db.dialect.Dialect;
|
||||
@@ -29,12 +28,11 @@ import cn.hutool.v7.db.handler.PageResultHandler;
|
||||
import cn.hutool.v7.db.handler.RsHandler;
|
||||
import cn.hutool.v7.db.sql.*;
|
||||
|
||||
import java.io.Serial;
|
||||
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;
|
||||
|
||||
/**
|
||||
* 提供基于方言的原始增删改查执行封装
|
||||
@@ -43,6 +41,7 @@ import java.util.regex.Pattern;
|
||||
* @since 5.5.3
|
||||
*/
|
||||
public class DialectRunner implements Serializable {
|
||||
@Serial
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private final DbConfig config;
|
||||
@@ -285,14 +284,9 @@ public class DialectRunner implements Serializable {
|
||||
*/
|
||||
public long count(final Connection conn, final SqlBuilder sqlBuilder) throws DbException {
|
||||
checkConn(conn);
|
||||
String selectSql = sqlBuilder.build();
|
||||
|
||||
// 去除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);
|
||||
}
|
||||
final String selectSql = SqlUtil.removeOuterOrderBy(sqlBuilder.build());
|
||||
|
||||
return StatementUtil.executeQuery(dialect.psForCount(conn,
|
||||
SqlBuilder.of(selectSql).addParams(sqlBuilder.getParamValueArray())), NumberHandler.INSTANCE).longValue();
|
||||
|
||||
@@ -17,8 +17,10 @@
|
||||
package cn.hutool.v7.db.sql;
|
||||
|
||||
import cn.hutool.v7.core.io.IoUtil;
|
||||
import cn.hutool.v7.core.util.CharsetUtil;
|
||||
import cn.hutool.v7.core.regex.PatternPool;
|
||||
import cn.hutool.v7.core.regex.ReUtil;
|
||||
import cn.hutool.v7.core.text.StrUtil;
|
||||
import cn.hutool.v7.core.util.CharsetUtil;
|
||||
import cn.hutool.v7.db.DbException;
|
||||
import cn.hutool.v7.db.Entity;
|
||||
import cn.hutool.v7.db.sql.Condition.LikeType;
|
||||
@@ -27,13 +29,10 @@ import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.Reader;
|
||||
import java.nio.charset.Charset;
|
||||
import java.sql.Blob;
|
||||
import java.sql.Clob;
|
||||
import java.sql.Connection;
|
||||
import java.sql.RowId;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.*;
|
||||
import java.util.List;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* SQL相关工具类,包括相关SQL语句拼接等
|
||||
@@ -43,6 +42,11 @@ import java.util.Map.Entry;
|
||||
*/
|
||||
public class SqlUtil {
|
||||
|
||||
/**
|
||||
* 创建SQL中的order by语句的正则
|
||||
*/
|
||||
private static final Pattern PATTERN_ORDER_BY = PatternPool.get("(.*)\\s+order\\s+by\\s+[^\\s]+", Pattern.CASE_INSENSITIVE);
|
||||
|
||||
/**
|
||||
* 构件相等条件的where语句<br>
|
||||
* 如果没有条件语句,泽返回空串,表示没有条件
|
||||
@@ -268,4 +272,15 @@ public class SqlUtil {
|
||||
public static java.sql.Timestamp toSqlTimestamp(final java.util.Date date) {
|
||||
return new java.sql.Timestamp(date.getTime());
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除 SQL中的 ORDER BY 子句
|
||||
*
|
||||
* @param selectSql 原始 SQL
|
||||
* @return 移除 ORDER BY 子句后的 SQL
|
||||
*/
|
||||
public static String removeOuterOrderBy(final String selectSql) {
|
||||
// 去除order by 子句
|
||||
return ReUtil.getGroup1(PATTERN_ORDER_BY, selectSql);
|
||||
}
|
||||
}
|
||||
|
||||
32
hutool-db/src/test/java/cn/hutool/v7/db/Issue4066Test.java
Normal file
32
hutool-db/src/test/java/cn/hutool/v7/db/Issue4066Test.java
Normal file
@@ -0,0 +1,32 @@
|
||||
package cn.hutool.v7.db;
|
||||
|
||||
import cn.hutool.v7.db.sql.SqlUtil;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class Issue4066Test {
|
||||
/**
|
||||
* 基础测试:简单的 ORDER BY 语句
|
||||
*/
|
||||
@Test
|
||||
public void removeOuterOrderByTest1() {
|
||||
// 测试基本的ORDER BY移除
|
||||
final String sql = "SELECT * FROM users ORDER BY name";
|
||||
final String result = SqlUtil.removeOuterOrderBy(sql);
|
||||
|
||||
assertEquals("SELECT * FROM users", result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 多字段 ORDER BY 测试:包含多个排序字段的复杂 ORDER BY语句
|
||||
*/
|
||||
@Test
|
||||
public void removeOuterOrderByTest2() {
|
||||
// 测试多字段ORDER BY移除
|
||||
final String sql = "SELECT id, name, age FROM users WHERE status = 'active' ORDER BY name ASC, age DESC, created_date";
|
||||
final String result = SqlUtil.removeOuterOrderBy(sql);
|
||||
|
||||
assertEquals("SELECT id, name, age FROM users WHERE status = 'active'", result);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user