From 207b7b1687e79d3baec6f969431ee8f784f9470f Mon Sep 17 00:00:00 2001 From: Looly Date: Thu, 18 Jan 2024 17:04:53 +0800 Subject: [PATCH] fix code --- .../org/dromara/hutool/db/AbstractDb.java | 206 +++++++++--------- .../dromara/hutool/db/ConnectionHolder.java | 4 +- .../org/dromara/hutool/db/DaoTemplate.java | 82 +++---- .../org/dromara/hutool/db/DbException.java | 85 ++++++++ .../dromara/hutool/db/DbRuntimeException.java | 49 ----- .../hutool/db/DefaultConnectionHolder.java | 4 +- .../org/dromara/hutool/db/DialectRunner.java | 80 +++---- .../java/org/dromara/hutool/db/Entity.java | 2 +- .../java/org/dromara/hutool/db/Session.java | 44 ++-- .../dromara/hutool/db/config/DbConfig.java | 1 + .../hutool/db/config/SettingConfigParser.java | 6 +- .../db/dialect/impl/AnsiSqlDialect.java | 6 +- .../dromara/hutool/db/driver/DriverUtil.java | 12 +- .../java/org/dromara/hutool/db/ds/DSUtil.java | 6 +- .../hutool/db/ds/c3p0/C3p0DSFactory.java | 4 +- .../hutool/db/ds/jndi/JndiDSFactory.java | 4 +- .../hutool/db/ds/pooled/PooledConnection.java | 4 +- .../hutool/db/ds/pooled/PooledDataSource.java | 4 +- .../hutool/db/handler/ResultSetUtil.java | 4 +- .../org/dromara/hutool/db/meta/Column.java | 4 +- .../hutool/db/meta/ColumnIndexInfo.java | 4 +- .../org/dromara/hutool/db/meta/MetaUtil.java | 16 +- .../dromara/hutool/db/meta/ResultColumn.java | 8 +- .../java/org/dromara/hutool/db/sql/Query.java | 8 +- .../org/dromara/hutool/db/sql/SqlBuilder.java | 18 +- .../dromara/hutool/db/sql/SqlExecutor.java | 94 ++++---- .../org/dromara/hutool/db/sql/SqlUtil.java | 10 +- .../hutool/db/sql/StatementBuilder.java | 12 +- 28 files changed, 409 insertions(+), 372 deletions(-) create mode 100644 hutool-db/src/main/java/org/dromara/hutool/db/DbException.java delete mode 100644 hutool-db/src/main/java/org/dromara/hutool/db/DbRuntimeException.java diff --git a/hutool-db/src/main/java/org/dromara/hutool/db/AbstractDb.java b/hutool-db/src/main/java/org/dromara/hutool/db/AbstractDb.java index 99a55a831..73c352232 100644 --- a/hutool-db/src/main/java/org/dromara/hutool/db/AbstractDb.java +++ b/hutool-db/src/main/java/org/dromara/hutool/db/AbstractDb.java @@ -78,10 +78,10 @@ public abstract class AbstractDb> extends DefaultConnect * @param sql 查询语句 * @param params 参数 * @return 结果对象 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 * @since 3.1.1 */ - public List query(final String sql, final Map params) throws DbRuntimeException { + public List query(final String sql, final Map params) throws DbException { return query(sql, new EntityListHandler(this.caseInsensitive), params); } @@ -91,10 +91,10 @@ public abstract class AbstractDb> extends DefaultConnect * @param sql 查询语句 * @param params 参数 * @return 结果对象 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 * @since 3.1.1 */ - public List query(final String sql, final Object... params) throws DbRuntimeException { + public List query(final String sql, final Object... params) throws DbException { return query(sql, new EntityListHandler(this.caseInsensitive), params); } @@ -106,10 +106,10 @@ public abstract class AbstractDb> extends DefaultConnect * @param beanClass 元素Bean类型 * @param params 参数 * @return 结果对象 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 * @since 3.2.2 */ - public List query(final String sql, final Class beanClass, final Object... params) throws DbRuntimeException { + public List query(final String sql, final Class beanClass, final Object... params) throws DbException { return query(sql, new BeanListHandler<>(beanClass), params); } @@ -119,9 +119,9 @@ public abstract class AbstractDb> extends DefaultConnect * @param sql 查询语句 * @param params 参数 * @return 结果对象 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 */ - public Entity queryOne(final String sql, final Object... params) throws DbRuntimeException { + public Entity queryOne(final String sql, final Object... params) throws DbException { return query(sql, new EntityHandler(this.caseInsensitive), params); } @@ -131,9 +131,9 @@ public abstract class AbstractDb> extends DefaultConnect * @param sql 查询语句 * @param params 参数 * @return 结果对象 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 */ - public Number queryNumber(final String sql, final Object... params) throws DbRuntimeException { + public Number queryNumber(final String sql, final Object... params) throws DbException { return query(sql, new NumberHandler(), params); } @@ -143,9 +143,9 @@ public abstract class AbstractDb> extends DefaultConnect * @param sql 查询语句 * @param params 参数 * @return 结果对象 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 */ - public String queryString(final String sql, final Object... params) throws DbRuntimeException { + public String queryString(final String sql, final Object... params) throws DbException { return query(sql, new StringHandler(), params); } @@ -157,9 +157,9 @@ public abstract class AbstractDb> extends DefaultConnect * @param rsh 结果集处理对象 * @param params 参数 * @return 结果对象 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 */ - public T query(final String sql, final RsHandler rsh, final Object... params) throws DbRuntimeException { + public T query(final String sql, final RsHandler rsh, final Object... params) throws DbException { Connection conn = null; try { conn = this.getConnection(); @@ -177,10 +177,10 @@ public abstract class AbstractDb> extends DefaultConnect * @param rsh 结果集处理对象 * @param paramMap 参数 * @return 结果对象 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 * @since 5.2.2 */ - public T query(final String sql, final RsHandler rsh, final Map paramMap) throws DbRuntimeException { + public T query(final String sql, final RsHandler rsh, final Map paramMap) throws DbException { Connection conn = null; try { conn = this.getConnection(); @@ -198,10 +198,10 @@ public abstract class AbstractDb> extends DefaultConnect * @param statementFunc 自定义{@link PreparedStatement}创建函数 * @param rsh 结果集处理对象 * @return 结果对象 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 * @since 5.7.17 */ - public T query(final SerFunction statementFunc, final RsHandler rsh) throws DbRuntimeException { + public T query(final SerFunction statementFunc, final RsHandler rsh) throws DbException { Connection conn = null; try { conn = this.getConnection(); @@ -221,9 +221,9 @@ public abstract class AbstractDb> extends DefaultConnect * @param sql SQL * @param params 参数 * @return 影响行数 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 */ - public int execute(final String sql, final Object... params) throws DbRuntimeException { + public int execute(final String sql, final Object... params) throws DbException { Connection conn = null; try { conn = this.getConnection(); @@ -240,9 +240,9 @@ public abstract class AbstractDb> extends DefaultConnect * @param sql SQL * @param params 参数 * @return 主键 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 */ - public Long executeForGeneratedKey(final String sql, final Object... params) throws DbRuntimeException { + public Long executeForGeneratedKey(final String sql, final Object... params) throws DbException { Connection conn = null; try { conn = this.getConnection(); @@ -258,10 +258,10 @@ public abstract class AbstractDb> extends DefaultConnect * @param sql SQL * @param paramsBatch 批量的参数 * @return 每个SQL执行影响的行数 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 * @since 5.4.2 */ - public int[] executeBatch(final String sql, final Iterable paramsBatch) throws DbRuntimeException { + public int[] executeBatch(final String sql, final Iterable paramsBatch) throws DbException { Connection conn = null; try { conn = this.getConnection(); @@ -276,10 +276,10 @@ public abstract class AbstractDb> extends DefaultConnect * * @param sqls SQL列表 * @return 每个SQL执行影响的行数 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 * @since 4.5.6 */ - public int[] executeBatch(final String... sqls) throws DbRuntimeException { + public int[] executeBatch(final String... sqls) throws DbException { Connection conn = null; try { conn = this.getConnection(); @@ -294,10 +294,10 @@ public abstract class AbstractDb> extends DefaultConnect * * @param sqls SQL列表 * @return 每个SQL执行影响的行数 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 * @since 5.4.2 */ - public int[] executeBatch(final Iterable sqls) throws DbRuntimeException { + public int[] executeBatch(final Iterable sqls) throws DbException { Connection conn = null; try { conn = this.getConnection(); @@ -317,9 +317,9 @@ public abstract class AbstractDb> extends DefaultConnect * * @param record 记录 * @return 插入行数 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 */ - public int insert(final Entity record) throws DbRuntimeException { + public int insert(final Entity record) throws DbException { Connection conn = null; try { conn = this.getConnection(); @@ -336,10 +336,10 @@ public abstract class AbstractDb> extends DefaultConnect * @param record 记录 * @param keys 需要检查唯一性的字段 * @return 插入行数 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 * @since 4.0.10 */ - public int insertOrUpdate(final Entity record, final String... keys) throws DbRuntimeException { + public int insertOrUpdate(final Entity record, final String... keys) throws DbException { Connection conn = null; try { conn = this.getConnection(); @@ -357,10 +357,10 @@ public abstract class AbstractDb> extends DefaultConnect * @param record 记录 * @param keys 需要检查唯一性的字段 * @return 插入行数 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 * @since 5.7.21 */ - public int upsert(final Entity record, final String... keys) throws DbRuntimeException { + public int upsert(final Entity record, final String... keys) throws DbException { Connection conn = null; try { conn = this.getConnection(); @@ -377,9 +377,9 @@ public abstract class AbstractDb> extends DefaultConnect * * @param records 记录列表 * @return 插入行数 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 */ - public int[] insert(final Collection records) throws DbRuntimeException { + public int[] insert(final Collection records) throws DbException { Connection conn = null; try { conn = this.getConnection(); @@ -394,9 +394,9 @@ public abstract class AbstractDb> extends DefaultConnect * * @param record 记录 * @return 主键列表 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 */ - public List insertForGeneratedKeys(final Entity record) throws DbRuntimeException { + public List insertForGeneratedKeys(final Entity record) throws DbException { Connection conn = null; try { conn = this.getConnection(); @@ -411,9 +411,9 @@ public abstract class AbstractDb> extends DefaultConnect * * @param record 记录 * @return 主键 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 */ - public Long insertForGeneratedKey(final Entity record) throws DbRuntimeException { + public Long insertForGeneratedKey(final Entity record) throws DbException { Connection conn = null; try { conn = this.getConnection(); @@ -433,9 +433,9 @@ public abstract class AbstractDb> extends DefaultConnect * @param field 字段名,最好是主键 * @param value 值,值可以是列表或数组,被当作IN查询处理 * @return 删除行数 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 */ - public int del(final String tableName, final String field, final Object value) throws DbRuntimeException { + public int del(final String tableName, final String field, final Object value) throws DbException { return del(Entity.of(tableName).set(field, value)); } @@ -444,9 +444,9 @@ public abstract class AbstractDb> extends DefaultConnect * * @param where 条件 * @return 影响行数 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 */ - public int del(final Entity where) throws DbRuntimeException { + public int del(final Entity where) throws DbException { Connection conn = null; try { conn = this.getConnection(); @@ -466,9 +466,9 @@ public abstract class AbstractDb> extends DefaultConnect * @param record 记录 * @param where 条件 * @return 影响行数 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 */ - public int update(final Entity record, final Entity where) throws DbRuntimeException { + public int update(final Entity record, final Entity where) throws DbException { Connection conn = null; try { conn = this.getConnection(); @@ -490,9 +490,9 @@ public abstract class AbstractDb> extends DefaultConnect * @param field 字段名 * @param value 字段值 * @return 记录 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 */ - public Entity get(final String tableName, final String field, final T value) throws DbRuntimeException { + public Entity get(final String tableName, final String field, final T value) throws DbException { return this.get(Entity.of(tableName).set(field, value)); } @@ -501,9 +501,9 @@ public abstract class AbstractDb> extends DefaultConnect * * @param where 条件 * @return 记录 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 */ - public Entity get(final Entity where) throws DbRuntimeException { + public Entity get(final Entity where) throws DbException { return find(where.getFieldNames(), where, new EntityHandler(this.caseInsensitive)); } @@ -520,9 +520,9 @@ public abstract class AbstractDb> extends DefaultConnect * @param where 条件实体类(包含表名) * @param rsh 结果集处理对象 * @return 结果对象 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 */ - public T find(final Collection fields, final Entity where, final RsHandler rsh) throws DbRuntimeException { + public T find(final Collection fields, final Entity where, final RsHandler rsh) throws DbException { return find(Query.of(where).setFields(fields), rsh); } @@ -533,10 +533,10 @@ public abstract class AbstractDb> extends DefaultConnect * @param fields 返回的字段列表,null则返回所有字段 * @param where 条件实体类(包含表名) * @return 结果Entity列表 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 * @since 4.5.16 */ - public List find(final Collection fields, final Entity where) throws DbRuntimeException { + public List find(final Collection fields, final Entity where) throws DbException { return find(fields, where, new EntityListHandler(this.caseInsensitive)); } @@ -548,10 +548,10 @@ public abstract class AbstractDb> extends DefaultConnect * @param query {@link Query}对象,此对象中可以定义返回字段、查询条件,查询的表、分页等信息 * @param rsh 结果集处理对象 * @return 结果对象 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 * @since 4.0.0 */ - public T find(final Query query, final RsHandler rsh) throws DbRuntimeException { + public T find(final Query query, final RsHandler rsh) throws DbException { Connection conn = null; try { conn = this.getConnection(); @@ -570,9 +570,9 @@ public abstract class AbstractDb> extends DefaultConnect * @param rsh 结果集处理对象 * @param fields 字段列表,可变长参数如果无值表示查询全部字段 * @return 结果对象 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 */ - public T find(final Entity where, final RsHandler rsh, final String... fields) throws DbRuntimeException { + public T find(final Entity where, final RsHandler rsh, final String... fields) throws DbException { return find(Arrays.asList(fields), where, rsh); } @@ -582,10 +582,10 @@ public abstract class AbstractDb> extends DefaultConnect * * @param where 条件实体类(包含表名) * @return 数据对象列表 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 * @since 3.2.1 */ - public List find(final Entity where) throws DbRuntimeException { + public List find(final Entity where) throws DbException { return find(where.getFieldNames(), where, new EntityListHandler(this.caseInsensitive)); } @@ -597,10 +597,10 @@ public abstract class AbstractDb> extends DefaultConnect * @param where 条件实体类(包含表名) * @param beanClass Bean类 * @return 数据对象列表 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 * @since 3.2.2 */ - public List find(final Entity where, final Class beanClass) throws DbRuntimeException { + public List find(final Entity where, final Class beanClass) throws DbException { return find(where.getFieldNames(), where, BeanListHandler.of(beanClass)); } @@ -610,9 +610,9 @@ public abstract class AbstractDb> extends DefaultConnect * * @param where 条件实体类(包含表名) * @return 数据对象列表 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 */ - public List findAll(final Entity where) throws DbRuntimeException { + public List findAll(final Entity where) throws DbException { return find(where, new EntityListHandler(this.caseInsensitive)); } @@ -624,10 +624,10 @@ public abstract class AbstractDb> extends DefaultConnect * @param where 条件实体类(包含表名) * @param beanClass 返回的对象类型 * @return 数据对象列表 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 * @since 3.2.2 */ - public List findAll(final Entity where, final Class beanClass) throws DbRuntimeException { + public List findAll(final Entity where, final Class beanClass) throws DbException { return find(where, BeanListHandler.of(beanClass)); } @@ -636,9 +636,9 @@ public abstract class AbstractDb> extends DefaultConnect * * @param tableName 表名 * @return 数据对象列表 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 */ - public List findAll(final String tableName) throws DbRuntimeException { + public List findAll(final String tableName) throws DbException { return findAll(Entity.of(tableName)); } @@ -649,9 +649,9 @@ public abstract class AbstractDb> extends DefaultConnect * @param field 字段名 * @param value 字段值 * @return 数据对象列表 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 */ - public List findBy(final String tableName, final String field, final Object value) throws DbRuntimeException { + public List findBy(final String tableName, final String field, final Object value) throws DbException { return findAll(Entity.of(tableName).set(field, value)); } @@ -661,10 +661,10 @@ public abstract class AbstractDb> extends DefaultConnect * @param tableName 表名 * @param wheres 条件,多个条件的连接逻辑使用{@link Condition#setLinkOperator(LogicalOperator)} 定义 * @return 数据对象列表 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 * @since 4.0.0 */ - public List findBy(final String tableName, final Condition... wheres) throws DbRuntimeException { + public List findBy(final String tableName, final Condition... wheres) throws DbException { final Query query = new Query(wheres, tableName); return find(query, new EntityListHandler(this.caseInsensitive)); } @@ -677,9 +677,9 @@ public abstract class AbstractDb> extends DefaultConnect * @param value 字段值 * @param likeType {@link LikeType} * @return 数据对象列表 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 */ - public List findLike(final String tableName, final String field, final String value, final LikeType likeType) throws DbRuntimeException { + public List findLike(final String tableName, final String field, final String value, final LikeType likeType) throws DbException { return findAll(Entity.of(tableName).set(field, SqlUtil.buildLikeValue(value, likeType, true))); } // endregion @@ -691,9 +691,9 @@ public abstract class AbstractDb> extends DefaultConnect * * @param where 查询条件 * @return 复合条件的结果数 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 */ - public long count(final Entity where) throws DbRuntimeException { + public long count(final Entity where) throws DbException { Connection conn = null; try { conn = this.getConnection(); @@ -708,9 +708,9 @@ public abstract class AbstractDb> extends DefaultConnect * * @param sql sql构造器 * @return 复合条件的结果数 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 */ - public long count(final SqlBuilder sql) throws DbRuntimeException { + public long count(final SqlBuilder sql) throws DbException { Connection conn = null; try { conn = this.getConnection(); @@ -726,10 +726,10 @@ public abstract class AbstractDb> extends DefaultConnect * @param selectSql 查询SQL语句 * @param params 查询参数 * @return 复合条件的结果数 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 * @since 5.6.6 */ - public long count(final CharSequence selectSql, final Object... params) throws DbRuntimeException { + public long count(final CharSequence selectSql, final Object... params) throws DbException { Connection conn = null; try { conn = this.getConnection(); @@ -780,10 +780,10 @@ public abstract class AbstractDb> extends DefaultConnect * @param where 条件实体类(包含表名) * @param page 分页对象 * @return 结果对象 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 * @since 3.2.2 */ - public List pageForEntityList(final Entity where, final Page page) throws DbRuntimeException { + public List pageForEntityList(final Entity where, final Page page) throws DbException { return page(where, page, new EntityListHandler(this.caseInsensitive)); } @@ -796,10 +796,10 @@ public abstract class AbstractDb> extends DefaultConnect * @param page 分页对象 * @param rsh 结果集处理对象 * @return 结果对象 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 * @since 3.2.2 */ - public T page(final Entity where, final Page page, final RsHandler rsh) throws DbRuntimeException { + public T page(final Entity where, final Page page, final RsHandler rsh) throws DbException { return page(where.getFieldNames(), where, page, rsh); } @@ -813,9 +813,9 @@ public abstract class AbstractDb> extends DefaultConnect * @param page 分页对象 * @param rsh 结果集处理对象 * @return 结果对象 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 */ - public T page(final Collection fields, final Entity where, final Page page, final RsHandler rsh) throws DbRuntimeException { + public T page(final Collection fields, final Entity where, final Page page, final RsHandler rsh) throws DbException { Connection conn = null; try { conn = this.getConnection(); @@ -834,10 +834,10 @@ public abstract class AbstractDb> extends DefaultConnect * @param rsh 结果集处理对象 * @param params 参数 * @return 结果对象 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 * @since 5.6.6 */ - public T page(final CharSequence sql, final Page page, final RsHandler rsh, final Object... params) throws DbRuntimeException { + public T page(final CharSequence sql, final Page page, final RsHandler rsh, final Object... params) throws DbException { Connection conn = null; try { conn = this.getConnection(); @@ -855,9 +855,9 @@ public abstract class AbstractDb> extends DefaultConnect * @param page 分页对象 * @param rsh 结果集处理对象 * @return 结果对象 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 */ - public T page(final SqlBuilder sql, final Page page, final RsHandler rsh) throws DbRuntimeException { + public T page(final SqlBuilder sql, final Page page, final RsHandler rsh) throws DbException { Connection conn = null; try { conn = this.getConnection(); @@ -874,10 +874,10 @@ public abstract class AbstractDb> extends DefaultConnect * @param page 分页对象 * @param params 参数列表 * @return 结果对象 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 * @since 5.5.3 */ - public PageResult page(final CharSequence sql, final Page page, final Object... params) throws DbRuntimeException { + public PageResult page(final CharSequence sql, final Page page, final Object... params) throws DbException { Connection conn = null; try { conn = this.getConnection(); @@ -896,9 +896,9 @@ public abstract class AbstractDb> extends DefaultConnect * @param pageNumber 页码 * @param pageSize 每页结果数 * @return 结果对象 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 */ - public PageResult page(final Collection fields, final Entity where, final int pageNumber, final int pageSize) throws DbRuntimeException { + public PageResult page(final Collection fields, final Entity where, final int pageNumber, final int pageSize) throws DbException { return page(fields, where, new Page(pageNumber, pageSize)); } @@ -910,9 +910,9 @@ public abstract class AbstractDb> extends DefaultConnect * @param where 条件实体类(包含表名) * @param page 分页对象 * @return 结果对象 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 */ - public PageResult page(final Collection fields, final Entity where, final Page page) throws DbRuntimeException { + public PageResult page(final Collection fields, final Entity where, final Page page) throws DbException { Connection conn = null; try { conn = this.getConnection(); @@ -929,9 +929,9 @@ public abstract class AbstractDb> extends DefaultConnect * @param where 条件实体类(包含表名) * @param page 分页对象 * @return 分页结果集 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 */ - public PageResult page(final Entity where, final Page page) throws DbRuntimeException { + public PageResult page(final Entity where, final Page page) throws DbException { return this.page(where.getFieldNames(), where, page); } // endregion @@ -1015,19 +1015,19 @@ public abstract class AbstractDb> extends DefaultConnect * 检查数据库是否支持事务,此项检查同一个数据源只检查一次,如果不支持抛出DbRuntimeException异常 * * @param conn Connection - * @throws DbRuntimeException 获取元数据信息失败 - * @throws DbRuntimeException 不支持事务 + * @throws DbException 获取元数据信息失败 + * @throws DbException 不支持事务 */ - protected void checkTransactionSupported(final Connection conn) throws DbRuntimeException { + protected void checkTransactionSupported(final Connection conn) throws DbException { if (null == isSupportTransaction) { try { isSupportTransaction = conn.getMetaData().supportsTransactions(); } catch (final SQLException e) { - throw new DbRuntimeException(e); + throw new DbException(e); } } if (!isSupportTransaction) { - throw new DbRuntimeException("Transaction not supported for current database!"); + throw new DbException("Transaction not supported for current database!"); } } // ---------------------------------------------------------------------------- protected method end diff --git a/hutool-db/src/main/java/org/dromara/hutool/db/ConnectionHolder.java b/hutool-db/src/main/java/org/dromara/hutool/db/ConnectionHolder.java index 0c4117f9c..a524bae48 100644 --- a/hutool-db/src/main/java/org/dromara/hutool/db/ConnectionHolder.java +++ b/hutool-db/src/main/java/org/dromara/hutool/db/ConnectionHolder.java @@ -25,9 +25,9 @@ public interface ConnectionHolder { * 获得链接。根据实现不同,可以自定义获取连接的方式 * * @return {@link Connection} - * @throws DbRuntimeException 连接获取异常 + * @throws DbException 连接获取异常 */ - Connection getConnection() throws DbRuntimeException; + Connection getConnection() throws DbException; /** * 关闭连接
diff --git a/hutool-db/src/main/java/org/dromara/hutool/db/DaoTemplate.java b/hutool-db/src/main/java/org/dromara/hutool/db/DaoTemplate.java index 132e4eeee..77e607c56 100644 --- a/hutool-db/src/main/java/org/dromara/hutool/db/DaoTemplate.java +++ b/hutool-db/src/main/java/org/dromara/hutool/db/DaoTemplate.java @@ -110,9 +110,9 @@ public class DaoTemplate { * * @param entity 实体对象 * @return 插入行数 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 */ - public int add(final Entity entity) throws DbRuntimeException { + public int add(final Entity entity) throws DbException { return db.insert(fixEntity(entity)); } @@ -121,9 +121,9 @@ public class DaoTemplate { * * @param entity 实体对象 * @return 主键列表 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 */ - public List addForGeneratedKeys(final Entity entity) throws DbRuntimeException { + public List addForGeneratedKeys(final Entity entity) throws DbException { return db.insertForGeneratedKeys(fixEntity(entity)); } @@ -132,9 +132,9 @@ public class DaoTemplate { * * @param entity 实体对象 * @return 自增主键 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 */ - public Long addForGeneratedKey(final Entity entity) throws DbRuntimeException { + public Long addForGeneratedKey(final Entity entity) throws DbException { return db.insertForGeneratedKey(fixEntity(entity)); } // endregion @@ -147,9 +147,9 @@ public class DaoTemplate { * @param 主键类型 * @param pk 主键 * @return 删除行数 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 */ - public int del(final T pk) throws DbRuntimeException { + public int del(final T pk) throws DbException { if (pk == null) { return 0; } @@ -163,9 +163,9 @@ public class DaoTemplate { * @param field 字段名 * @param value 字段值 * @return 删除行数 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 */ - public int del(final String field, final T value) throws DbRuntimeException { + public int del(final String field, final T value) throws DbException { if (StrUtil.isBlank(field)) { return 0; } @@ -178,9 +178,9 @@ public class DaoTemplate { * * @param where 删除条件,当条件为空时,返回0(防止误删全表) * @return 删除行数 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 */ - public int del(final Entity where) throws DbRuntimeException { + public int del(final Entity where) throws DbException { if (MapUtil.isEmpty(where)) { return 0; } @@ -196,9 +196,9 @@ public class DaoTemplate { * @param record 更新的内容 * @param where 条件 * @return 更新条目数 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 */ - public int update(final Entity record, final Entity where) throws DbRuntimeException { + public int update(final Entity record, final Entity where) throws DbException { if (MapUtil.isEmpty(record)) { return 0; } @@ -210,16 +210,16 @@ public class DaoTemplate { * * @param entity 实体对象,必须包含主键 * @return 更新行数 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 */ - public int update(Entity entity) throws DbRuntimeException { + public int update(Entity entity) throws DbException { if (MapUtil.isEmpty(entity)) { return 0; } entity = fixEntity(entity); final Object pk = entity.get(primaryKeyField); if (null == pk) { - throw new DbRuntimeException(StrUtil.format("Please determine `{}` for update", primaryKeyField)); + throw new DbException(StrUtil.format("Please determine `{}` for update", primaryKeyField)); } final Entity where = Entity.of(tableName).set(primaryKeyField, pk); @@ -234,9 +234,9 @@ public class DaoTemplate { * * @param entity 实体,当包含主键时更新,否则新增 * @return 新增或更新条数 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 */ - public int addOrUpdate(final Entity entity) throws DbRuntimeException { + public int addOrUpdate(final Entity entity) throws DbException { return null == entity.get(primaryKeyField) ? add(entity) : update(entity); } // endregion @@ -248,9 +248,9 @@ public class DaoTemplate { * @param 主键类型 * @param pk 主键值 * @return 记录 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 */ - public Entity get(final T pk) throws DbRuntimeException { + public Entity get(final T pk) throws DbException { return this.get(primaryKeyField, pk); } @@ -262,9 +262,9 @@ public class DaoTemplate { * @param field 字段名 * @param value 字段值 * @return 记录 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 */ - public Entity get(final String field, final T value) throws DbRuntimeException { + public Entity get(final String field, final T value) throws DbException { return this.get(Entity.of(tableName).set(field, value)); } @@ -273,9 +273,9 @@ public class DaoTemplate { * * @param where 条件 * @return 记录 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 */ - public Entity get(final Entity where) throws DbRuntimeException { + public Entity get(final Entity where) throws DbException { return db.get(fixEntity(where)); } // endregion @@ -289,9 +289,9 @@ public class DaoTemplate { * @param field 字段名 * @param value 字段值 * @return 记录 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 */ - public List find(final String field, final T value) throws DbRuntimeException { + public List find(final String field, final T value) throws DbException { return this.find(Entity.of(tableName).set(field, value)); } @@ -299,9 +299,9 @@ public class DaoTemplate { * 查询当前表的所有记录 * * @return 记录 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 */ - public List findAll() throws DbRuntimeException { + public List findAll() throws DbException { return this.find(Entity.of(tableName)); } @@ -310,9 +310,9 @@ public class DaoTemplate { * * @param where 查询条件 * @return 记录 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 */ - public List find(final Entity where) throws DbRuntimeException { + public List find(final Entity where) throws DbException { return db.find(null, fixEntity(where)); } @@ -324,9 +324,9 @@ public class DaoTemplate { * @param sql SQL语句 * @param params SQL占位符中对应的参数 * @return 记录 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 */ - public List findBySql(String sql, final Object... params) throws DbRuntimeException { + public List findBySql(String sql, final Object... params) throws DbException { final String selectKeyword = StrUtil.subPre(sql.trim(), 6).toLowerCase(); if (!"select".equals(selectKeyword)) { sql = "SELECT * FROM " + this.tableName + " " + sql; @@ -341,9 +341,9 @@ public class DaoTemplate { * @param page 分页对象 * @param selectFields 查询的字段列表 * @return 分页结果集 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 */ - public PageResult page(final Entity where, final Page page, final String... selectFields) throws DbRuntimeException { + public PageResult page(final Entity where, final Page page, final String... selectFields) throws DbException { return db.page(Arrays.asList(selectFields), fixEntity(where), page); } @@ -353,9 +353,9 @@ public class DaoTemplate { * @param where 条件 * @param page 分页对象 * @return 分页结果集 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 */ - public PageResult page(final Entity where, final Page page) throws DbRuntimeException { + public PageResult page(final Entity where, final Page page) throws DbException { return db.page(fixEntity(where), page); } @@ -364,9 +364,9 @@ public class DaoTemplate { * * @param where 条件 * @return 数量 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 */ - public long count(final Entity where) throws DbRuntimeException { + public long count(final Entity where) throws DbException { return db.count(fixEntity(where)); } @@ -375,9 +375,9 @@ public class DaoTemplate { * * @param where 条件 * @return 是否存在 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 */ - public boolean exist(final Entity where) throws DbRuntimeException { + public boolean exist(final Entity where) throws DbException { return this.count(where) > 0; } // endregion diff --git a/hutool-db/src/main/java/org/dromara/hutool/db/DbException.java b/hutool-db/src/main/java/org/dromara/hutool/db/DbException.java new file mode 100644 index 000000000..28ee20f51 --- /dev/null +++ b/hutool-db/src/main/java/org/dromara/hutool/db/DbException.java @@ -0,0 +1,85 @@ +/* + * Copyright (c) 2023 looly(loolly@aliyun.com) + * Hutool is licensed under Mulan PSL v2. + * You can use this software according to the terms and conditions of the Mulan PSL v2. + * You may obtain a copy of Mulan PSL v2 at: + * https://license.coscl.org.cn/MulanPSL2 + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, + * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, + * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. + * See the Mulan PSL v2 for more details. + */ + +package org.dromara.hutool.db; + +import org.dromara.hutool.core.exception.HutoolException; + +/** + * 数据库异常 + * + * @author Looly + */ +public class DbException extends HutoolException { + private static final long serialVersionUID = 3624487785708765623L; + + /** + * 构造 + * + * @param e 异常 + */ + public DbException(final Throwable e) { + super(e); + } + + /** + * 构造 + * + * @param message 消息 + */ + public DbException(final String message) { + super(message); + } + + /** + * 构造 + * + * @param messageTemplate 消息模板 + * @param params 参数 + */ + public DbException(final String messageTemplate, final Object... params) { + super(messageTemplate, params); + } + + /** + * 构造 + * + * @param message 消息 + * @param cause 被包装的子异常 + */ + public DbException(final String message, final Throwable cause) { + super(message, cause); + } + + /** + * 构造 + * + * @param message 消息 + * @param cause 被包装的子异常 + * @param enableSuppression 是否启用抑制 + * @param writableStackTrace 堆栈跟踪是否应该是可写的 + */ + public DbException(final String message, final Throwable cause, final boolean enableSuppression, final boolean writableStackTrace) { + super(message, cause, enableSuppression, writableStackTrace); + } + + /** + * 构造 + * + * @param cause 被包装的子异常 + * @param messageTemplate 消息模板 + * @param params 参数 + */ + public DbException(final Throwable cause, final String messageTemplate, final Object... params) { + super(cause, messageTemplate, params); + } +} diff --git a/hutool-db/src/main/java/org/dromara/hutool/db/DbRuntimeException.java b/hutool-db/src/main/java/org/dromara/hutool/db/DbRuntimeException.java deleted file mode 100644 index 9e9eda54a..000000000 --- a/hutool-db/src/main/java/org/dromara/hutool/db/DbRuntimeException.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright (c) 2023 looly(loolly@aliyun.com) - * Hutool is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * https://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, - * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, - * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -package org.dromara.hutool.db; - -import org.dromara.hutool.core.exception.ExceptionUtil; -import org.dromara.hutool.core.text.StrUtil; - -/** - * 数据库异常 - * - * @author Looly - */ -public class DbRuntimeException extends RuntimeException { - private static final long serialVersionUID = 3624487785708765623L; - - public DbRuntimeException(final Throwable e) { - super(ExceptionUtil.getMessage(e), e); - } - - public DbRuntimeException(final String message) { - super(message); - } - - public DbRuntimeException(final String messageTemplate, final Object... params) { - super(StrUtil.format(messageTemplate, params)); - } - - public DbRuntimeException(final String message, final Throwable throwable) { - super(message, throwable); - } - - public DbRuntimeException(final String message, final Throwable throwable, final boolean enableSuppression, final boolean writableStackTrace) { - super(message, throwable, enableSuppression, writableStackTrace); - } - - public DbRuntimeException(final Throwable throwable, final String messageTemplate, final Object... params) { - super(StrUtil.format(messageTemplate, params), throwable); - } -} diff --git a/hutool-db/src/main/java/org/dromara/hutool/db/DefaultConnectionHolder.java b/hutool-db/src/main/java/org/dromara/hutool/db/DefaultConnectionHolder.java index 7a684676b..bb28cc02d 100644 --- a/hutool-db/src/main/java/org/dromara/hutool/db/DefaultConnectionHolder.java +++ b/hutool-db/src/main/java/org/dromara/hutool/db/DefaultConnectionHolder.java @@ -35,11 +35,11 @@ public class DefaultConnectionHolder implements ConnectionHolder { } @Override - public Connection getConnection() throws DbRuntimeException { + public Connection getConnection() throws DbException { try { return ThreadLocalConnection.INSTANCE.get(this.ds); } catch (final SQLException e) { - throw new DbRuntimeException(e); + throw new DbException(e); } } diff --git a/hutool-db/src/main/java/org/dromara/hutool/db/DialectRunner.java b/hutool-db/src/main/java/org/dromara/hutool/db/DialectRunner.java index 97feb7cae..3062d2e1d 100644 --- a/hutool-db/src/main/java/org/dromara/hutool/db/DialectRunner.java +++ b/hutool-db/src/main/java/org/dromara/hutool/db/DialectRunner.java @@ -75,9 +75,9 @@ public class DialectRunner implements Serializable { * @param conn 数据库连接 * @param records 记录列表,记录KV必须严格一致 * @return 插入行数 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 */ - public int[] insert(final Connection conn, final Entity... records) throws DbRuntimeException { + public int[] insert(final Connection conn, final Entity... records) throws DbException { checkConn(conn); if (ArrayUtil.isEmpty(records)) { return new int[]{0}; @@ -95,7 +95,7 @@ public class DialectRunner implements Serializable { ps = dialect.psForInsertBatch(conn, records); return ps.executeBatch(); } catch (final SQLException e) { - throw new DbRuntimeException(e); + throw new DbException(e); } finally { IoUtil.closeQuietly(ps); } @@ -110,10 +110,10 @@ public class DialectRunner implements Serializable { * @param record 记录 * @param keys 需要检查唯一性的字段 * @return 插入行数 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 * @since 5.7.20 */ - public int upsert(final Connection conn, final Entity record, final String... keys) throws DbRuntimeException { + public int upsert(final Connection conn, final Entity record, final String... keys) throws DbException { PreparedStatement ps = null; try { ps = getDialect().psForUpsert(conn, record, keys); @@ -124,7 +124,7 @@ public class DialectRunner implements Serializable { try { return ps.executeUpdate(); } catch (final SQLException e) { - throw new DbRuntimeException(e); + throw new DbException(e); } finally { IoUtil.closeQuietly(ps); } @@ -141,9 +141,9 @@ public class DialectRunner implements Serializable { * @param record 记录 * @param keys 需要检查唯一性的字段 * @return 插入行数 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 */ - public int insertOrUpdate(final Connection conn, final Entity record, final String... keys) throws DbRuntimeException { + public int insertOrUpdate(final Connection conn, final Entity record, final String... keys) throws DbException { final Entity where = record.filterNew(keys); if (MapUtil.isNotEmpty(where) && count(conn, Query.of(where)) > 0) { return update(conn, record.removeNew(keys), where); @@ -161,12 +161,12 @@ public class DialectRunner implements Serializable { * @param record 记录 * @param generatedKeysHandler 自增主键处理器,用于定义返回自增主键的范围和类型 * @return 主键列表 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 */ - public T insert(final Connection conn, final Entity record, final RsHandler generatedKeysHandler) throws DbRuntimeException { + public T insert(final Connection conn, final Entity record, final RsHandler generatedKeysHandler) throws DbException { checkConn(conn); if (MapUtil.isEmpty(record)) { - throw new DbRuntimeException("Empty entity provided!"); + throw new DbException("Empty entity provided!"); } PreparedStatement ps = null; @@ -178,7 +178,7 @@ public class DialectRunner implements Serializable { } return StatementUtil.getGeneratedKeys(ps, generatedKeysHandler); } catch (final SQLException e) { - throw new DbRuntimeException(e); + throw new DbException(e); } finally { IoUtil.closeQuietly(ps); } @@ -191,13 +191,13 @@ public class DialectRunner implements Serializable { * @param conn 数据库连接 * @param where 条件 * @return 影响行数 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 */ - public int del(final Connection conn, final Entity where) throws DbRuntimeException { + public int del(final Connection conn, final Entity where) throws DbException { checkConn(conn); if (MapUtil.isEmpty(where)) { //不允许做全表删除 - throw new DbRuntimeException("Empty entity provided!"); + throw new DbException("Empty entity provided!"); } PreparedStatement ps = null; @@ -205,7 +205,7 @@ public class DialectRunner implements Serializable { ps = dialect.psForDelete(conn, Query.of(where)); return ps.executeUpdate(); } catch (final SQLException e) { - throw new DbRuntimeException(e); + throw new DbException(e); } finally { IoUtil.closeQuietly(ps); } @@ -219,16 +219,16 @@ public class DialectRunner implements Serializable { * @param record 记录 * @param where 条件 * @return 影响行数 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 */ - public int update(final Connection conn, final Entity record, final Entity where) throws DbRuntimeException { + public int update(final Connection conn, final Entity record, final Entity where) throws DbException { checkConn(conn); if (MapUtil.isEmpty(record)) { - throw new DbRuntimeException("Empty entity provided!"); + throw new DbException("Empty entity provided!"); } if (MapUtil.isEmpty(where)) { //不允许做全表更新 - throw new DbRuntimeException("Empty where provided!"); + throw new DbException("Empty where provided!"); } //表名可以从被更新记录的Entity中获得,也可以从Where中获得 @@ -244,7 +244,7 @@ public class DialectRunner implements Serializable { ps = dialect.psForUpdate(conn, record, query); return ps.executeUpdate(); } catch (final SQLException e) { - throw new DbRuntimeException(e); + throw new DbException(e); } finally { IoUtil.closeQuietly(ps); } @@ -259,15 +259,15 @@ public class DialectRunner implements Serializable { * @param query {@link Query} * @param rsh 结果集处理对象 * @return 结果对象 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 */ - public T find(final Connection conn, final Query query, final RsHandler rsh) throws DbRuntimeException { + public T find(final Connection conn, final Query query, final RsHandler rsh) throws DbException { checkConn(conn); Assert.notNull(query, "[query] is null !"); try { return SqlExecutor.queryAndClosePs(dialect.psForFind(conn, query), rsh); } catch (final SQLException e) { - throw new DbRuntimeException(e); + throw new DbException(e); } } @@ -277,14 +277,14 @@ public class DialectRunner implements Serializable { * @param conn 数据库连接对象 * @param query 查询 * @return 复合条件的结果数 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 */ - public long count(final Connection conn, final Query query) throws DbRuntimeException { + public long count(final Connection conn, final Query query) throws DbException { checkConn(conn); try { return SqlExecutor.queryAndClosePs(dialect.psForCount(conn, query), new NumberHandler()).longValue(); } catch (final SQLException e) { - throw new DbRuntimeException(e); + throw new DbException(e); } } @@ -295,10 +295,10 @@ public class DialectRunner implements Serializable { * @param conn 数据库连接对象 * @param sqlBuilder 查询语句 * @return 复合条件的结果数 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 * @since 5.7.2 */ - public long count(final Connection conn, final SqlBuilder sqlBuilder) throws DbRuntimeException { + public long count(final Connection conn, final SqlBuilder sqlBuilder) throws DbException { checkConn(conn); String selectSql = sqlBuilder.build(); @@ -314,7 +314,7 @@ public class DialectRunner implements Serializable { SqlBuilder.of(selectSql).addParams(sqlBuilder.getParamValueArray())), new NumberHandler()).longValue(); } catch (final SQLException e) { - throw new DbRuntimeException(e); + throw new DbException(e); } } @@ -325,9 +325,9 @@ public class DialectRunner implements Serializable { * @param conn 数据库连接对象 * @param query 查询 * @return 结果对象 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 */ - public PageResult page(final Connection conn, final Query query) throws DbRuntimeException { + public PageResult page(final Connection conn, final Query query) throws DbException { final Page page = query.getPage(); final PageResultHandler entityResultHandler = PageResultHandler.of( // 分页查询中总数的查询要去掉分页信息 @@ -345,9 +345,9 @@ public class DialectRunner implements Serializable { * @param query 查询条件(包含表名) * @param rsh 结果集处理对象 * @return 结果对象 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 */ - public T page(final Connection conn, final Query query, final RsHandler rsh) throws DbRuntimeException { + public T page(final Connection conn, final Query query, final RsHandler rsh) throws DbException { checkConn(conn); if (null == query.getPage()) { return this.find(conn, query, rsh); @@ -356,7 +356,7 @@ public class DialectRunner implements Serializable { try { return SqlExecutor.queryAndClosePs(dialect.psForPage(conn, query), rsh); } catch (final SQLException e) { - throw new DbRuntimeException(e); + throw new DbException(e); } } @@ -368,9 +368,9 @@ public class DialectRunner implements Serializable { * @param sqlBuilder SQL构建器,可以使用{@link SqlBuilder#of(CharSequence)} 包装普通SQL * @param page 分页对象 * @return 结果对象 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 */ - public PageResult page(final Connection conn, final SqlBuilder sqlBuilder, final Page page) throws DbRuntimeException { + public PageResult page(final Connection conn, final SqlBuilder sqlBuilder, final Page page) throws DbException { final PageResultHandler entityResultHandler = PageResultHandler.of( new PageResult<>(page, (int) count(conn, sqlBuilder))); @@ -387,10 +387,10 @@ public class DialectRunner implements Serializable { * @param page 分页对象 * @param rsh 结果集处理对象 * @return 结果对象 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 * @since 5.5.3 */ - public T page(final Connection conn, final SqlBuilder sqlBuilder, final Page page, final RsHandler rsh) throws DbRuntimeException { + public T page(final Connection conn, final SqlBuilder sqlBuilder, final Page page, final RsHandler rsh) throws DbException { checkConn(conn); if (null == page) { return SqlExecutor.query(conn, sqlBuilder, rsh); @@ -400,7 +400,7 @@ public class DialectRunner implements Serializable { try { ps = dialect.psForPage(conn, sqlBuilder, page); } catch (final SQLException e) { - throw new DbRuntimeException(e); + throw new DbException(e); } return SqlExecutor.queryAndClosePs(ps, rsh); } diff --git a/hutool-db/src/main/java/org/dromara/hutool/db/Entity.java b/hutool-db/src/main/java/org/dromara/hutool/db/Entity.java index a53679697..eea15ce3a 100644 --- a/hutool-db/src/main/java/org/dromara/hutool/db/Entity.java +++ b/hutool-db/src/main/java/org/dromara/hutool/db/Entity.java @@ -409,7 +409,7 @@ public class Entity extends Dict { if (obj instanceof RowId) { return (RowId) obj; } - throw new DbRuntimeException("Value of field [{}] is not a rowid!", field); + throw new DbException("Value of field [{}] is not a rowid!", field); } // -------------------------------------------------------------------- Get end diff --git a/hutool-db/src/main/java/org/dromara/hutool/db/Session.java b/hutool-db/src/main/java/org/dromara/hutool/db/Session.java index 233456ac4..592d3577a 100644 --- a/hutool-db/src/main/java/org/dromara/hutool/db/Session.java +++ b/hutool-db/src/main/java/org/dromara/hutool/db/Session.java @@ -104,28 +104,28 @@ public class Session extends AbstractDb implements Closeable { /** * 开始事务 * - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 */ - public void beginTransaction() throws DbRuntimeException { + public void beginTransaction() throws DbException { final Connection conn = getConnection(); checkTransactionSupported(conn); try { conn.setAutoCommit(false); } catch (final SQLException e) { - throw new DbRuntimeException(e); + throw new DbException(e); } } /** * 提交事务 * - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 */ - public void commit() throws DbRuntimeException { + public void commit() throws DbException { try { getConnection().commit(); } catch (final SQLException e) { - throw new DbRuntimeException(e); + throw new DbException(e); } finally { try { getConnection().setAutoCommit(true); // 事务结束,恢复自动提交 @@ -138,13 +138,13 @@ public class Session extends AbstractDb implements Closeable { /** * 回滚事务 * - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 */ - public void rollback() throws DbRuntimeException { + public void rollback() throws DbException { try { getConnection().rollback(); } catch (final SQLException e) { - throw new DbRuntimeException(e); + throw new DbException(e); } finally { try { getConnection().setAutoCommit(true); // 事务结束,恢复自动提交 @@ -176,13 +176,13 @@ public class Session extends AbstractDb implements Closeable { * 回滚到某个保存点,保存点的设置请使用setSavepoint方法 * * @param savepoint 保存点 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 */ - public void rollback(final Savepoint savepoint) throws DbRuntimeException { + public void rollback(final Savepoint savepoint) throws DbException { try { getConnection().rollback(savepoint); } catch (final SQLException e) { - throw new DbRuntimeException(e); + throw new DbException(e); } finally { try { getConnection().setAutoCommit(true); // 事务结束,恢复自动提交 @@ -215,13 +215,13 @@ public class Session extends AbstractDb implements Closeable { * 设置保存点 * * @return 保存点对象 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 */ - public Savepoint setSavepoint() throws DbRuntimeException { + public Savepoint setSavepoint() throws DbException { try { return getConnection().setSavepoint(); } catch (final SQLException e) { - throw new DbRuntimeException(e); + throw new DbException(e); } } @@ -246,16 +246,16 @@ public class Session extends AbstractDb implements Closeable { * Connection.TRANSACTION_SERIALIZABLE 禁止脏读、不可重复读和幻读
* * @param level 隔离级别 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 */ - public void setTransactionIsolation(final int level) throws DbRuntimeException { + public void setTransactionIsolation(final int level) throws DbException { try { if (getConnection().getMetaData().supportsTransactionIsolationLevel(level) == false) { - throw new DbRuntimeException(StrUtil.format("Transaction isolation [{}] not support!", level)); + throw new DbException(StrUtil.format("Transaction isolation [{}] not support!", level)); } getConnection().setTransactionIsolation(level); } catch (final SQLException e) { - throw new DbRuntimeException(e); + throw new DbException(e); } } @@ -263,17 +263,17 @@ public class Session extends AbstractDb implements Closeable { * 在事务中执行操作,通过实现{@link SerConsumer}接口的call方法执行多条SQL语句从而完成事务 * * @param func 函数抽象,在函数中执行多个SQL操作,多个操作会被合并为同一事务 - * @throws DbRuntimeException SQL异常 + * @throws DbException SQL异常 * @since 3.2.3 */ - public void tx(final SerConsumer func) throws DbRuntimeException { + public void tx(final SerConsumer func) throws DbException { try { beginTransaction(); func.accept(this); commit(); } catch (final Throwable e) { quietRollback(); - throw new DbRuntimeException(e); + throw new DbException(e); } } diff --git a/hutool-db/src/main/java/org/dromara/hutool/db/config/DbConfig.java b/hutool-db/src/main/java/org/dromara/hutool/db/config/DbConfig.java index 5d31ad9ac..970d058bd 100644 --- a/hutool-db/src/main/java/org/dromara/hutool/db/config/DbConfig.java +++ b/hutool-db/src/main/java/org/dromara/hutool/db/config/DbConfig.java @@ -24,6 +24,7 @@ import java.util.Properties; *
  • 基本配置项,如driver、url、user、password等
  • *
  • 连接配置,如remarks、useInformationSchema等
  • *
  • 连接池配置,如初始容量、最大容量等,取决于连接池库具体要求
  • + *
  • 其它配置,如是否大小写敏感、SQL过滤器等
  • * * * @author Looly diff --git a/hutool-db/src/main/java/org/dromara/hutool/db/config/SettingConfigParser.java b/hutool-db/src/main/java/org/dromara/hutool/db/config/SettingConfigParser.java index 9323f7226..842c02d15 100644 --- a/hutool-db/src/main/java/org/dromara/hutool/db/config/SettingConfigParser.java +++ b/hutool-db/src/main/java/org/dromara/hutool/db/config/SettingConfigParser.java @@ -16,7 +16,7 @@ import org.dromara.hutool.core.convert.Convert; import org.dromara.hutool.core.io.resource.NoResourceException; import org.dromara.hutool.core.map.MapUtil; import org.dromara.hutool.core.text.StrUtil; -import org.dromara.hutool.db.DbRuntimeException; +import org.dromara.hutool.db.DbException; import org.dromara.hutool.db.driver.DriverUtil; import org.dromara.hutool.setting.Setting; import org.dromara.hutool.setting.props.Props; @@ -68,7 +68,7 @@ public class SettingConfigParser implements ConfigParser { final Setting subSetting = setting.getSetting(group); if (MapUtil.isEmpty(subSetting)) { - throw new DbRuntimeException("No config for group: [{}]", group); + throw new DbException("No config for group: [{}]", group); } return toDbConfig(subSetting); @@ -105,7 +105,7 @@ public class SettingConfigParser implements ConfigParser { // 基本信息 final String url = setting.getAndRemove(DSKeys.KEY_ALIAS_URL); if (StrUtil.isBlank(url)) { - throw new DbRuntimeException("No JDBC URL!"); + throw new DbException("No JDBC URL!"); } // 移除用户可能误加入的show sql配置项 diff --git a/hutool-db/src/main/java/org/dromara/hutool/db/dialect/impl/AnsiSqlDialect.java b/hutool-db/src/main/java/org/dromara/hutool/db/dialect/impl/AnsiSqlDialect.java index 247edf894..54b9c3727 100644 --- a/hutool-db/src/main/java/org/dromara/hutool/db/dialect/impl/AnsiSqlDialect.java +++ b/hutool-db/src/main/java/org/dromara/hutool/db/dialect/impl/AnsiSqlDialect.java @@ -14,7 +14,7 @@ package org.dromara.hutool.db.dialect.impl; import org.dromara.hutool.core.array.ArrayUtil; import org.dromara.hutool.core.lang.Assert; -import org.dromara.hutool.db.DbRuntimeException; +import org.dromara.hutool.db.DbException; import org.dromara.hutool.db.Entity; import org.dromara.hutool.db.Page; import org.dromara.hutool.db.StatementUtil; @@ -59,7 +59,7 @@ public class AnsiSqlDialect implements Dialect { @Override public PreparedStatement psForInsertBatch(final Connection conn, final Entity... entities) { if (ArrayUtil.isEmpty(entities)) { - throw new DbRuntimeException("Entities for batch insert is empty !"); + throw new DbException("Entities for batch insert is empty !"); } // 批量,根据第一行数据结构生成SQL占位符 final SqlBuilder insert = SqlBuilder.of(quoteWrapper).insert(entities[0], this.dialectName()); @@ -104,7 +104,7 @@ public class AnsiSqlDialect implements Dialect { public PreparedStatement psForPage(final Connection conn, final Query query) { Assert.notNull(query, "query must be not null !"); if (ArrayUtil.hasBlank(query.getTableNames())) { - throw new DbRuntimeException("Table name must be not empty !"); + throw new DbException("Table name must be not empty !"); } final SqlBuilder find = SqlBuilder.of(quoteWrapper).query(query); diff --git a/hutool-db/src/main/java/org/dromara/hutool/db/driver/DriverUtil.java b/hutool-db/src/main/java/org/dromara/hutool/db/driver/DriverUtil.java index a06334ddd..2ee39ebc8 100644 --- a/hutool-db/src/main/java/org/dromara/hutool/db/driver/DriverUtil.java +++ b/hutool-db/src/main/java/org/dromara/hutool/db/driver/DriverUtil.java @@ -14,7 +14,7 @@ package org.dromara.hutool.db.driver; import org.dromara.hutool.core.io.IoUtil; import org.dromara.hutool.core.text.StrUtil; -import org.dromara.hutool.db.DbRuntimeException; +import org.dromara.hutool.db.DbException; import org.dromara.hutool.db.ds.DSWrapper; import javax.sql.DataSource; @@ -60,9 +60,9 @@ public class DriverUtil { try { conn = ds.getConnection(); } catch (final SQLException e) { - throw new DbRuntimeException("Get Connection error !", e); + throw new DbException("Get Connection error !", e); } catch (final NullPointerException e) { - throw new DbRuntimeException("Unexpected NullPointException, maybe [jdbcUrl] or [url] is empty!", e); + throw new DbException("Unexpected NullPointException, maybe [jdbcUrl] or [url] is empty!", e); } driver = identifyDriver(conn); } finally { @@ -77,9 +77,9 @@ public class DriverUtil { * * @param conn 数据库连接对象 * @return 驱动 - * @throws DbRuntimeException SQL异常包装,获取元数据信息失败 + * @throws DbException SQL异常包装,获取元数据信息失败 */ - public static String identifyDriver(final Connection conn) throws DbRuntimeException { + public static String identifyDriver(final Connection conn) throws DbException { String driver; final DatabaseMetaData meta; try { @@ -89,7 +89,7 @@ public class DriverUtil { driver = identifyDriver(meta.getDriverName()); } } catch (final SQLException e) { - throw new DbRuntimeException("Identify driver error!", e); + throw new DbException("Identify driver error!", e); } return driver; diff --git a/hutool-db/src/main/java/org/dromara/hutool/db/ds/DSUtil.java b/hutool-db/src/main/java/org/dromara/hutool/db/ds/DSUtil.java index 6ecedbe18..ebdf87683 100644 --- a/hutool-db/src/main/java/org/dromara/hutool/db/ds/DSUtil.java +++ b/hutool-db/src/main/java/org/dromara/hutool/db/ds/DSUtil.java @@ -12,7 +12,7 @@ package org.dromara.hutool.db.ds; -import org.dromara.hutool.db.DbRuntimeException; +import org.dromara.hutool.db.DbException; import org.dromara.hutool.log.LogUtil; import javax.naming.InitialContext; @@ -37,7 +37,7 @@ public class DSUtil { public static DataSource getJndiDSWithLog(final String jndiName) { try { return getJndiDS(jndiName); - } catch (final DbRuntimeException e) { + } catch (final DbException e) { LogUtil.error(e.getCause(), "Find JNDI datasource error!"); } return null; @@ -53,7 +53,7 @@ public class DSUtil { try { return (DataSource) new InitialContext().lookup(jndiName); } catch (final NamingException e) { - throw new DbRuntimeException(e); + throw new DbException(e); } } diff --git a/hutool-db/src/main/java/org/dromara/hutool/db/ds/c3p0/C3p0DSFactory.java b/hutool-db/src/main/java/org/dromara/hutool/db/ds/c3p0/C3p0DSFactory.java index 936175828..a81aa4262 100644 --- a/hutool-db/src/main/java/org/dromara/hutool/db/ds/c3p0/C3p0DSFactory.java +++ b/hutool-db/src/main/java/org/dromara/hutool/db/ds/c3p0/C3p0DSFactory.java @@ -14,7 +14,7 @@ package org.dromara.hutool.db.ds.c3p0; import com.mchange.v2.c3p0.ComboPooledDataSource; import org.dromara.hutool.core.map.MapUtil; -import org.dromara.hutool.db.DbRuntimeException; +import org.dromara.hutool.db.DbException; import org.dromara.hutool.db.ds.DSFactory; import org.dromara.hutool.db.config.DbConfig; import org.dromara.hutool.setting.props.Props; @@ -45,7 +45,7 @@ public class C3p0DSFactory implements DSFactory { try { ds.setDriverClass(config.getDriver()); } catch (final PropertyVetoException e) { - throw new DbRuntimeException(e); + throw new DbException(e); } ds.setUser(config.getUser()); ds.setPassword(config.getPass()); diff --git a/hutool-db/src/main/java/org/dromara/hutool/db/ds/jndi/JndiDSFactory.java b/hutool-db/src/main/java/org/dromara/hutool/db/ds/jndi/JndiDSFactory.java index bfc8fe3a4..80221536f 100644 --- a/hutool-db/src/main/java/org/dromara/hutool/db/ds/jndi/JndiDSFactory.java +++ b/hutool-db/src/main/java/org/dromara/hutool/db/ds/jndi/JndiDSFactory.java @@ -13,7 +13,7 @@ package org.dromara.hutool.db.ds.jndi; import org.dromara.hutool.core.text.StrUtil; -import org.dromara.hutool.db.DbRuntimeException; +import org.dromara.hutool.db.DbException; import org.dromara.hutool.db.ds.DSFactory; import org.dromara.hutool.db.ds.DSUtil; import org.dromara.hutool.db.config.DbConfig; @@ -43,7 +43,7 @@ public class JndiDSFactory implements DSFactory { public DataSource createDataSource(final DbConfig config) { final String jndiName = config.getPoolProps().getProperty("jndi"); if (StrUtil.isEmpty(jndiName)) { - throw new DbRuntimeException("No setting name [jndi] for this group."); + throw new DbException("No setting name [jndi] for this group."); } return DSUtil.getJndiDS(jndiName); } diff --git a/hutool-db/src/main/java/org/dromara/hutool/db/ds/pooled/PooledConnection.java b/hutool-db/src/main/java/org/dromara/hutool/db/ds/pooled/PooledConnection.java index 47d834b65..9e43c4b60 100644 --- a/hutool-db/src/main/java/org/dromara/hutool/db/ds/pooled/PooledConnection.java +++ b/hutool-db/src/main/java/org/dromara/hutool/db/ds/pooled/PooledConnection.java @@ -14,7 +14,7 @@ package org.dromara.hutool.db.ds.pooled; import org.dromara.hutool.core.map.MapUtil; import org.dromara.hutool.core.pool.Poolable; -import org.dromara.hutool.db.DbRuntimeException; +import org.dromara.hutool.db.DbException; import org.dromara.hutool.db.config.DbConfig; import org.dromara.hutool.setting.props.Props; @@ -61,7 +61,7 @@ public class PooledConnection extends ConnectionWrapper implements Poolable columnNames = new ArrayList<>(); @@ -164,7 +164,7 @@ public class MetaUtil { } return columnNames.toArray(new String[0]); } catch (final Exception e) { - throw new DbRuntimeException("Get columns error!", e); + throw new DbException("Get columns error!", e); } finally { IoUtil.closeQuietly(conn); } @@ -283,7 +283,7 @@ public class MetaUtil { table.setIndexInfoList(ListUtil.of(indexInfoMap.values())); } } catch (final SQLException e) { - throw new DbRuntimeException("Get columns error!", e); + throw new DbException("Get columns error!", e); } finally { IoUtil.closeQuietly(conn); } diff --git a/hutool-db/src/main/java/org/dromara/hutool/db/meta/ResultColumn.java b/hutool-db/src/main/java/org/dromara/hutool/db/meta/ResultColumn.java index 221c81f06..d79778df0 100644 --- a/hutool-db/src/main/java/org/dromara/hutool/db/meta/ResultColumn.java +++ b/hutool-db/src/main/java/org/dromara/hutool/db/meta/ResultColumn.java @@ -12,7 +12,7 @@ package org.dromara.hutool.db.meta; -import org.dromara.hutool.db.DbRuntimeException; +import org.dromara.hutool.db.DbException; import java.sql.ResultSetMetaData; import java.sql.SQLException; @@ -50,9 +50,9 @@ public class ResultColumn { * * @param metaData {@link ResultSetMetaData} * @param columnIndexBase1 列序号,从1开始。即第一列为1,第二列为2。。。 - * @throws DbRuntimeException SQLException包装 + * @throws DbException SQLException包装 */ - public ResultColumn(final ResultSetMetaData metaData, final int columnIndexBase1) throws DbRuntimeException { + public ResultColumn(final ResultSetMetaData metaData, final int columnIndexBase1) throws DbException { try { this.autoIncrement = metaData.isAutoIncrement(columnIndexBase1); this.caseSensitive = metaData.isCaseSensitive(columnIndexBase1); @@ -75,7 +75,7 @@ public class ResultColumn { this.definitelyWritable = metaData.isDefinitelyWritable(columnIndexBase1); this.className = metaData.getColumnClassName(columnIndexBase1); } catch (final SQLException e) { - throw new DbRuntimeException(e); + throw new DbException(e); } } diff --git a/hutool-db/src/main/java/org/dromara/hutool/db/sql/Query.java b/hutool-db/src/main/java/org/dromara/hutool/db/sql/Query.java index 0fea1685f..d754e9a48 100644 --- a/hutool-db/src/main/java/org/dromara/hutool/db/sql/Query.java +++ b/hutool-db/src/main/java/org/dromara/hutool/db/sql/Query.java @@ -15,7 +15,7 @@ package org.dromara.hutool.db.sql; import org.dromara.hutool.core.collection.CollUtil; import org.dromara.hutool.core.collection.ListUtil; import org.dromara.hutool.core.array.ArrayUtil; -import org.dromara.hutool.db.DbRuntimeException; +import org.dromara.hutool.db.DbException; import org.dromara.hutool.db.Entity; import org.dromara.hutool.db.Page; @@ -212,11 +212,11 @@ public class Query implements Cloneable { * 获得第一个表名 * * @return 表名 - * @throws DbRuntimeException 没有表 + * @throws DbException 没有表 */ - public String getFirstTableName() throws DbRuntimeException { + public String getFirstTableName() throws DbException { if (ArrayUtil.isEmpty(this.tableNames)) { - throw new DbRuntimeException("No tableName!"); + throw new DbException("No tableName!"); } return this.tableNames[0]; } diff --git a/hutool-db/src/main/java/org/dromara/hutool/db/sql/SqlBuilder.java b/hutool-db/src/main/java/org/dromara/hutool/db/sql/SqlBuilder.java index 78acfc679..953915354 100644 --- a/hutool-db/src/main/java/org/dromara/hutool/db/sql/SqlBuilder.java +++ b/hutool-db/src/main/java/org/dromara/hutool/db/sql/SqlBuilder.java @@ -16,7 +16,7 @@ import org.dromara.hutool.core.lang.builder.Builder; import org.dromara.hutool.core.collection.CollUtil; import org.dromara.hutool.core.text.StrUtil; import org.dromara.hutool.core.array.ArrayUtil; -import org.dromara.hutool.db.DbRuntimeException; +import org.dromara.hutool.db.DbException; import org.dromara.hutool.db.Entity; import org.dromara.hutool.db.dialect.DialectName; import org.dromara.hutool.db.dialect.impl.OracleDialect; @@ -73,17 +73,17 @@ public class SqlBuilder implements Builder { * 验证实体类对象的有效性 * * @param entity 实体类对象 - * @throws DbRuntimeException SQL异常包装,获取元数据信息失败 + * @throws DbException SQL异常包装,获取元数据信息失败 */ - public static void validateEntity(final Entity entity) throws DbRuntimeException { + public static void validateEntity(final Entity entity) throws DbException { if (null == entity) { - throw new DbRuntimeException("Entity is null !"); + throw new DbException("Entity is null !"); } if (StrUtil.isBlank(entity.getTableName())) { - throw new DbRuntimeException("Entity`s table name is null !"); + throw new DbException("Entity`s table name is null !"); } if (entity.isEmpty()) { - throw new DbRuntimeException("No filed and value in this entity !"); + throw new DbException("No filed and value in this entity !"); } } @@ -232,7 +232,7 @@ public class SqlBuilder implements Builder { */ public SqlBuilder delete(String tableName) { if (StrUtil.isBlank(tableName)) { - throw new DbRuntimeException("Table name is blank !"); + throw new DbException("Table name is blank !"); } if (null != quoteWrapper) { @@ -339,7 +339,7 @@ public class SqlBuilder implements Builder { */ public SqlBuilder from(String... tableNames) { if (ArrayUtil.isEmpty(tableNames) || ArrayUtil.hasBlank(tableNames)) { - throw new DbRuntimeException("Table name is blank in table names !"); + throw new DbException("Table name is blank in table names !"); } if (null != quoteWrapper) { @@ -489,7 +489,7 @@ public class SqlBuilder implements Builder { */ public SqlBuilder join(String tableName, final Join join) { if (StrUtil.isBlank(tableName)) { - throw new DbRuntimeException("Table name is blank !"); + throw new DbException("Table name is blank !"); } if (null != join) { diff --git a/hutool-db/src/main/java/org/dromara/hutool/db/sql/SqlExecutor.java b/hutool-db/src/main/java/org/dromara/hutool/db/sql/SqlExecutor.java index 90f6361f8..a72a83e14 100644 --- a/hutool-db/src/main/java/org/dromara/hutool/db/sql/SqlExecutor.java +++ b/hutool-db/src/main/java/org/dromara/hutool/db/sql/SqlExecutor.java @@ -15,7 +15,7 @@ package org.dromara.hutool.db.sql; import org.dromara.hutool.core.collection.iter.ArrayIter; import org.dromara.hutool.core.io.IoUtil; import org.dromara.hutool.core.func.SerFunction; -import org.dromara.hutool.db.DbRuntimeException; +import org.dromara.hutool.db.DbException; import org.dromara.hutool.db.StatementUtil; import org.dromara.hutool.db.handler.RsHandler; @@ -39,10 +39,10 @@ public class SqlExecutor { * @param sql SQL,使用name做为占位符,例如:name * @param paramMap 参数Map * @return 影响的行数 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 * @since 4.0.10 */ - public static int execute(final Connection conn, final String sql, final Map paramMap) throws DbRuntimeException { + public static int execute(final Connection conn, final String sql, final Map paramMap) throws DbException { final NamedSql namedSql = new NamedSql(sql, paramMap); return execute(conn, namedSql.getSql(), namedSql.getParamArray()); } @@ -56,15 +56,15 @@ public class SqlExecutor { * @param sql SQL * @param params 参数 * @return 影响的行数 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 */ - public static int execute(final Connection conn, final String sql, final Object... params) throws DbRuntimeException { + public static int execute(final Connection conn, final String sql, final Object... params) throws DbException { PreparedStatement ps = null; try { ps = StatementUtil.prepareStatement(false, conn, sql, params); return ps.executeUpdate(); } catch (final SQLException e) { - throw new DbRuntimeException(e); + throw new DbException(e); } finally { IoUtil.closeQuietly(ps); } @@ -78,15 +78,15 @@ public class SqlExecutor { * @param sql SQL * @param params 参数 * @return 如果执行后第一个结果是ResultSet,则返回true,否则返回false。 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 */ - public static boolean call(final Connection conn, final String sql, final Object... params) throws DbRuntimeException { + public static boolean call(final Connection conn, final String sql, final Object... params) throws DbException { CallableStatement call = null; try { call = StatementUtil.prepareCall(conn, sql, params); return call.execute(); } catch (final SQLException e) { - throw new DbRuntimeException(e); + throw new DbException(e); } finally { IoUtil.closeQuietly(call); } @@ -100,14 +100,14 @@ public class SqlExecutor { * @param sql SQL * @param params 参数 * @return ResultSet - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 * @since 4.1.4 */ - public static ResultSet callQuery(final Connection conn, final String sql, final Object... params) throws DbRuntimeException { + public static ResultSet callQuery(final Connection conn, final String sql, final Object... params) throws DbException { try { return StatementUtil.prepareCall(conn, sql, params).executeQuery(); } catch (final SQLException e) { - throw new DbRuntimeException(e); + throw new DbException(e); } } @@ -120,10 +120,10 @@ public class SqlExecutor { * @param sql SQL * @param paramMap 参数Map * @return 主键 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 * @since 4.0.10 */ - public static Long executeForGeneratedKey(final Connection conn, final String sql, final Map paramMap) throws DbRuntimeException { + public static Long executeForGeneratedKey(final Connection conn, final String sql, final Map paramMap) throws DbException { final NamedSql namedSql = new NamedSql(sql, paramMap); return executeForGeneratedKey(conn, namedSql.getSql(), namedSql.getParamArray()); } @@ -137,9 +137,9 @@ public class SqlExecutor { * @param sql SQL * @param params 参数 * @return 主键 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 */ - public static Long executeForGeneratedKey(final Connection conn, final String sql, final Object... params) throws DbRuntimeException { + public static Long executeForGeneratedKey(final Connection conn, final String sql, final Object... params) throws DbException { PreparedStatement ps = null; ResultSet rs = null; try { @@ -155,7 +155,7 @@ public class SqlExecutor { } return null; } catch (final SQLException e) { - throw new DbRuntimeException(e); + throw new DbException(e); } finally { IoUtil.closeQuietly(ps); IoUtil.closeQuietly(rs); @@ -171,15 +171,15 @@ public class SqlExecutor { * @param sql SQL * @param paramsBatch 批量的参数 * @return 每个SQL执行影响的行数 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 */ - public static int[] executeBatch(final Connection conn, final String sql, final Iterable paramsBatch) throws DbRuntimeException { + public static int[] executeBatch(final Connection conn, final String sql, final Iterable paramsBatch) throws DbException { PreparedStatement ps = null; try { ps = StatementUtil.prepareStatementForBatch(conn, sql, paramsBatch); return ps.executeBatch(); } catch (final SQLException e) { - throw new DbRuntimeException(e); + throw new DbException(e); } finally { IoUtil.closeQuietly(ps); } @@ -193,10 +193,10 @@ public class SqlExecutor { * @param conn 数据库连接对象 * @param sqls SQL列表 * @return 每个SQL执行影响的行数 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 * @since 4.5.6 */ - public static int[] executeBatch(final Connection conn, final String... sqls) throws DbRuntimeException { + public static int[] executeBatch(final Connection conn, final String... sqls) throws DbException { return executeBatch(conn, new ArrayIter<>(sqls)); } @@ -208,10 +208,10 @@ public class SqlExecutor { * @param conn 数据库连接对象 * @param sqls SQL列表 * @return 每个SQL执行影响的行数 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 * @since 4.5.6 */ - public static int[] executeBatch(final Connection conn, final Iterable sqls) throws DbRuntimeException { + public static int[] executeBatch(final Connection conn, final Iterable sqls) throws DbException { Statement statement = null; try { statement = conn.createStatement(); @@ -220,7 +220,7 @@ public class SqlExecutor { } return statement.executeBatch(); } catch (final SQLException e) { - throw new DbRuntimeException(e); + throw new DbException(e); } finally { IoUtil.closeQuietly(statement); } @@ -236,10 +236,10 @@ public class SqlExecutor { * @param rsh 结果集处理对象 * @param paramMap 参数对 * @return 结果对象 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 * @since 4.0.10 */ - public static T query(final Connection conn, final String sql, final RsHandler rsh, final Map paramMap) throws DbRuntimeException { + public static T query(final Connection conn, final String sql, final RsHandler rsh, final Map paramMap) throws DbException { final NamedSql namedSql = new NamedSql(sql, paramMap); return query(conn, namedSql.getSql(), rsh, namedSql.getParamArray()); } @@ -253,10 +253,10 @@ public class SqlExecutor { * @param sqlBuilder SQL构建器,包含参数 * @param rsh 结果集处理对象 * @return 结果对象 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 * @since 5.5.3 */ - public static T query(final Connection conn, final SqlBuilder sqlBuilder, final RsHandler rsh) throws DbRuntimeException { + public static T query(final Connection conn, final SqlBuilder sqlBuilder, final RsHandler rsh) throws DbException { return query(conn, sqlBuilder.build(), rsh, sqlBuilder.getParamValueArray()); } @@ -270,9 +270,9 @@ public class SqlExecutor { * @param rsh 结果集处理对象 * @param params 参数 * @return 结果对象 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 */ - public static T query(final Connection conn, final String sql, final RsHandler rsh, final Object... params) throws DbRuntimeException { + public static T query(final Connection conn, final String sql, final RsHandler rsh, final Object... params) throws DbException { PreparedStatement ps = null; try { ps = StatementUtil.prepareStatement(false, conn, sql, params); @@ -291,10 +291,10 @@ public class SqlExecutor { * @param statementFunc 自定义{@link PreparedStatement}创建函数 * @param rsh 自定义结果集处理 * @return 结果 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 * @since 5.7.17 */ - public static T query(final Connection conn, final SerFunction statementFunc, final RsHandler rsh) throws DbRuntimeException { + public static T query(final Connection conn, final SerFunction statementFunc, final RsHandler rsh) throws DbException { PreparedStatement ps = null; try { ps = statementFunc.apply(conn); @@ -316,14 +316,14 @@ public class SqlExecutor { * @param ps PreparedStatement对象 * @param params 参数 * @return 影响的行数 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 */ - public static int executeUpdate(final PreparedStatement ps, final Object... params) throws DbRuntimeException { + public static int executeUpdate(final PreparedStatement ps, final Object... params) throws DbException { try { StatementUtil.fillArrayParam(ps, params); return ps.executeUpdate(); } catch (final SQLException e) { - throw new DbRuntimeException(e); + throw new DbException(e); } } @@ -335,14 +335,14 @@ public class SqlExecutor { * @param ps PreparedStatement对象 * @param params 参数 * @return 如果执行后第一个结果是ResultSet,则返回true,否则返回false。 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 */ - public static boolean execute(final PreparedStatement ps, final Object... params) throws DbRuntimeException { + public static boolean execute(final PreparedStatement ps, final Object... params) throws DbException { try { StatementUtil.fillArrayParam(ps, params); return ps.execute(); } catch (final SQLException e) { - throw new DbRuntimeException(e); + throw new DbException(e); } } @@ -355,14 +355,14 @@ public class SqlExecutor { * @param rsh 结果集处理对象 * @param params 参数 * @return 结果对象 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 */ - public static T query(final PreparedStatement ps, final RsHandler rsh, final Object... params) throws DbRuntimeException { + public static T query(final PreparedStatement ps, final RsHandler rsh, final Object... params) throws DbException { try { StatementUtil.fillArrayParam(ps, params); return executeQuery(ps, rsh); } catch (final SQLException e) { - throw new DbRuntimeException(e); + throw new DbException(e); } } @@ -374,9 +374,9 @@ public class SqlExecutor { * @param rsh 结果集处理对象 * @param params 参数 * @return 结果对象 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 */ - public static T queryAndClosePs(final PreparedStatement ps, final RsHandler rsh, final Object... params) throws DbRuntimeException { + public static T queryAndClosePs(final PreparedStatement ps, final RsHandler rsh, final Object... params) throws DbException { try { return query(ps, rsh, params); } finally { @@ -392,16 +392,16 @@ public class SqlExecutor { * @param ps {@link PreparedStatement} * @param rsh 结果集处理对象 * @return 结果对象 - * @throws DbRuntimeException SQL执行异常 + * @throws DbException SQL执行异常 * @since 4.1.13 */ - private static T executeQuery(final PreparedStatement ps, final RsHandler rsh) throws DbRuntimeException { + private static T executeQuery(final PreparedStatement ps, final RsHandler rsh) throws DbException { ResultSet rs = null; try { rs = ps.executeQuery(); return rsh.handle(rs); } catch (final SQLException e) { - throw new DbRuntimeException(e); + throw new DbException(e); } finally { IoUtil.closeQuietly(rs); } diff --git a/hutool-db/src/main/java/org/dromara/hutool/db/sql/SqlUtil.java b/hutool-db/src/main/java/org/dromara/hutool/db/sql/SqlUtil.java index 0ffed98c6..366f21713 100644 --- a/hutool-db/src/main/java/org/dromara/hutool/db/sql/SqlUtil.java +++ b/hutool-db/src/main/java/org/dromara/hutool/db/sql/SqlUtil.java @@ -15,7 +15,7 @@ package org.dromara.hutool.db.sql; import org.dromara.hutool.core.io.IoUtil; import org.dromara.hutool.core.util.CharsetUtil; import org.dromara.hutool.core.text.StrUtil; -import org.dromara.hutool.db.DbRuntimeException; +import org.dromara.hutool.db.DbException; import org.dromara.hutool.db.Entity; import org.dromara.hutool.db.sql.Condition.LikeType; @@ -171,7 +171,7 @@ public class SqlUtil { reader = clob.getCharacterStream(); return IoUtil.read(reader); } catch (final SQLException e) { - throw new DbRuntimeException(e); + throw new DbException(e); } finally { IoUtil.closeQuietly(reader); } @@ -191,7 +191,7 @@ public class SqlUtil { in = blob.getBinaryStream(); return IoUtil.read(in, charset); } catch (final SQLException e) { - throw new DbRuntimeException(e); + throw new DbException(e); } finally { IoUtil.closeQuietly(in); } @@ -214,7 +214,7 @@ public class SqlUtil { out = blob.setBinaryStream(1); IoUtil.copy(dataStream, out); } catch (final SQLException e) { - throw new DbRuntimeException(e); + throw new DbException(e); } finally { IoUtil.closeQuietly(out); if (closeAfterUse) { @@ -238,7 +238,7 @@ public class SqlUtil { blob = conn.createBlob(); blob.setBytes(0, data); } catch (final SQLException e) { - throw new DbRuntimeException(e); + throw new DbException(e); } return blob; } diff --git a/hutool-db/src/main/java/org/dromara/hutool/db/sql/StatementBuilder.java b/hutool-db/src/main/java/org/dromara/hutool/db/sql/StatementBuilder.java index 91d6d0845..e43b9cfcd 100644 --- a/hutool-db/src/main/java/org/dromara/hutool/db/sql/StatementBuilder.java +++ b/hutool-db/src/main/java/org/dromara/hutool/db/sql/StatementBuilder.java @@ -20,7 +20,7 @@ import org.dromara.hutool.core.lang.Assert; import org.dromara.hutool.core.lang.builder.Builder; import org.dromara.hutool.core.map.MapUtil; import org.dromara.hutool.core.text.StrUtil; -import org.dromara.hutool.db.DbRuntimeException; +import org.dromara.hutool.db.DbException; import org.dromara.hutool.db.Entity; import org.dromara.hutool.db.sql.filter.SqlFilter; @@ -118,7 +118,7 @@ public class StatementBuilder implements Builder { try { return _build(); } catch (final SQLException e) { - throw new DbRuntimeException(e); + throw new DbException(e); } } @@ -126,9 +126,9 @@ public class StatementBuilder implements Builder { * 创建批量操作的{@link StatementWrapper} * * @return {@link StatementWrapper},{@code null}表示不执行 - * @throws DbRuntimeException SQL异常 + * @throws DbException SQL异常 */ - public StatementWrapper buildForBatch() throws DbRuntimeException { + public StatementWrapper buildForBatch() throws DbException { final String sql = this.boundSql.getSql(); Assert.notBlank(sql, "Sql String must be not blank!"); final List paramsBatch = this.boundSql.getParams(); @@ -159,7 +159,7 @@ public class StatementBuilder implements Builder { ps.addBatch(); } } catch (final SQLException e) { - throw new DbRuntimeException(e); + throw new DbException(e); } return ps; } @@ -183,7 +183,7 @@ public class StatementBuilder implements Builder { .fillArrayParam(params) .getRaw(); } catch (final SQLException e) { - throw new DbRuntimeException(e); + throw new DbException(e); } }