fix db bug

This commit is contained in:
Looly 2021-04-13 15:04:19 +08:00
parent 56e32846cd
commit 97e15f8152
4 changed files with 101 additions and 68 deletions

View File

@ -3,12 +3,13 @@
------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------
# 5.6.3 (2021-04-11) # 5.6.4 (2021-04-13)
### 新特性 ### 新特性
* 【core 】 DatePattern补充DateTimeFormatterpr#308@Gitee * 【core 】 DatePattern补充DateTimeFormatterpr#308@Gitee
### Bug修复 ### Bug修复
* 【db 】 修复SQL分页时未使用别名导致的错误同时count时取消order by子句issue#I3IJ8X@Gitee
------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------

View File

@ -1,7 +1,9 @@
package cn.hutool.db; package cn.hutool.db;
import cn.hutool.core.collection.CollUtil; import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.map.MapUtil; import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.db.dialect.Dialect; import cn.hutool.db.dialect.Dialect;
import cn.hutool.db.dialect.DialectFactory; import cn.hutool.db.dialect.DialectFactory;
import cn.hutool.db.handler.EntityListHandler; import cn.hutool.db.handler.EntityListHandler;
@ -279,7 +281,7 @@ public class SqlConnRunner extends DialectRunner {
} }
/** /**
* 获取查询结果总数生成类似于 SELECT count(1) from (sql) * 获取查询结果总数生成类似于 SELECT count(1) from (sql) as _count
* *
* @param conn 数据库连接对象 * @param conn 数据库连接对象
* @param selectSql 查询语句 * @param selectSql 查询语句
@ -287,7 +289,16 @@ public class SqlConnRunner extends DialectRunner {
* @throws SQLException SQL异常 * @throws SQLException SQL异常
*/ */
public long count(Connection conn, CharSequence selectSql) throws SQLException { public long count(Connection conn, CharSequence selectSql) throws SQLException {
SqlBuilder sqlBuilder = SqlBuilder.of(selectSql).insertPreFragment("SELECT count(1) from(").append(")"); Assert.notBlank(selectSql, "Select SQL must be not blank!");
final int orderByIndex = StrUtil.indexOfIgnoreCase(selectSql, " order by");
if(orderByIndex > 0){
selectSql = StrUtil.subPre(selectSql, orderByIndex);
}
SqlBuilder sqlBuilder = SqlBuilder.of(selectSql)
.insertPreFragment("SELECT count(1) from(")
// issue#I3IJ8X@Gitee在子查询时需设置单独别名此处为了防止和用户的表名冲突使用自定义的较长别名
.append(") as _hutool_alias_count_");
return page(conn, sqlBuilder, null, new NumberHandler()).intValue(); return page(conn, sqlBuilder, null, new NumberHandler()).intValue();
} }

View File

@ -21,12 +21,12 @@ import java.util.Map.Entry;
* 调用getParamValues()方法获得占位符对应的值 * 调用getParamValues()方法获得占位符对应的值
* *
* @author Looly * @author Looly
*
*/ */
public class SqlBuilder implements Builder<String> { public class SqlBuilder implements Builder<String> {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
// --------------------------------------------------------------- Static methods start // --------------------------------------------------------------- Static methods start
/** /**
* 创建SQL构建器 * 创建SQL构建器
* *
@ -48,6 +48,7 @@ public class SqlBuilder implements Builder<String>{
/** /**
* 从已有的SQL中构建一个SqlBuilder * 从已有的SQL中构建一个SqlBuilder
*
* @param sql SQL语句 * @param sql SQL语句
* @return SqlBuilder * @return SqlBuilder
* @since 5.5.3 * @since 5.5.3
@ -59,30 +60,44 @@ public class SqlBuilder implements Builder<String>{
// --------------------------------------------------------------- Static methods end // --------------------------------------------------------------- Static methods end
// --------------------------------------------------------------- Enums start // --------------------------------------------------------------- Enums start
/** /**
* SQL中多表关联用的关键字 * SQL中多表关联用的关键字
* *
* @author Looly * @author Looly
*
*/ */
public enum Join { public enum Join {
/** 如果表中有至少一个匹配,则返回行 */ /**
* 如果表中有至少一个匹配则返回行
*/
INNER, INNER,
/** 即使右表中没有匹配,也从左表返回所有的行 */ /**
* 即使右表中没有匹配也从左表返回所有的行
*/
LEFT, LEFT,
/** 即使左表中没有匹配,也从右表返回所有的行 */ /**
* 即使左表中没有匹配也从右表返回所有的行
*/
RIGHT, RIGHT,
/** 只要其中一个表中存在匹配,就返回行 */ /**
* 只要其中一个表中存在匹配就返回行
*/
FULL FULL
} }
// --------------------------------------------------------------- Enums end // --------------------------------------------------------------- Enums end
private final StringBuilder sql = new StringBuilder(); private final StringBuilder sql = new StringBuilder();
/** 字段列表(仅用于插入和更新) */ /**
* 字段列表仅用于插入和更新
*/
private final List<String> fields = new ArrayList<>(); private final List<String> fields = new ArrayList<>();
/** 占位符对应的值列表 */ /**
* 占位符对应的值列表
*/
private final List<Object> paramValues = new ArrayList<>(); private final List<Object> paramValues = new ArrayList<>();
/** 包装器 */ /**
* 包装器
*/
private Wrapper wrapper; private Wrapper wrapper;
// --------------------------------------------------------------- Constructor start // --------------------------------------------------------------- Constructor start
@ -132,8 +147,7 @@ public class SqlBuilder implements Builder<String>{
validateEntity(entity); validateEntity(entity);
if (null != wrapper) { if (null != wrapper) {
// 包装表名 // 包装表名 entity = wrapper.wrap(entity);
// entity = wrapper.wrap(entity);
entity.setTableName(wrapper.wrap(entity.getTableName())); entity.setTableName(wrapper.wrap(entity.getTableName()));
} }
@ -169,7 +183,7 @@ public class SqlBuilder implements Builder<String>{
} }
sql.append("INSERT INTO ")// sql.append("INSERT INTO ")//
.append(entity.getTableName()).append(" (").append(fieldsPart).append(") VALUES (")// .append(entity.getTableName()).append(" (").append(fieldsPart).append(") VALUES (")//
.append(placeHolder.toString()).append(")"); .append(placeHolder).append(")");
return this; return this;
} }
@ -548,7 +562,7 @@ public class SqlBuilder implements Builder<String>{
* SqlBuilder builder = SqlBuilder.of("select *"); * SqlBuilder builder = SqlBuilder.of("select *");
* builder.append(" from ").append("user"); * builder.append(" from ").append("user");
* </pre> * </pre>
* * <p>
* 如果需要追加带占位符的片段需调用{@link #addParams(Object...)} 方法加入对应参数值 * 如果需要追加带占位符的片段需调用{@link #addParams(Object...)} 方法加入对应参数值
* *
* @param sqlFragment SQL其它部分片段 * @param sqlFragment SQL其它部分片段
@ -644,6 +658,7 @@ public class SqlBuilder implements Builder<String>{
} }
// --------------------------------------------------------------- private method start // --------------------------------------------------------------- private method start
/** /**
* 构建组合条件<br> * 构建组合条件<br>
* 例如name = ? AND type IN (?, ?) AND other LIKE ? * 例如name = ? AND type IN (?, ?) AND other LIKE ?

View File

@ -39,7 +39,7 @@ public class DbTest {
@Test @Test
public void pageTest2() throws SQLException { public void pageTest2() throws SQLException {
String sql = "select * from user"; String sql = "select * from user order by name";
// 测试数据库中一共4条数据第0页有3条第1页有1条 // 测试数据库中一共4条数据第0页有3条第1页有1条
List<Entity> page0 = Db.use().page( List<Entity> page0 = Db.use().page(
sql, Page.of(0, 3)); sql, Page.of(0, 3));
@ -56,6 +56,12 @@ public class DbTest {
Assert.assertEquals(4, count); Assert.assertEquals(4, count);
} }
@Test
public void countTest2() throws SQLException {
final long count = Db.use().count("select * from user order by name DESC");
Assert.assertEquals(4, count);
}
@Test @Test
public void findLikeTest() throws SQLException { public void findLikeTest() throws SQLException {
// 方式1 // 方式1