This commit is contained in:
Looly 2024-01-18 17:04:53 +08:00
parent c84db9070a
commit 207b7b1687
28 changed files with 409 additions and 372 deletions

View File

@ -78,10 +78,10 @@ public abstract class AbstractDb<R extends AbstractDb<R>> extends DefaultConnect
* @param sql 查询语句
* @param params 参数
* @return 结果对象
* @throws DbRuntimeException SQL执行异常
* @throws DbException SQL执行异常
* @since 3.1.1
*/
public List<Entity> query(final String sql, final Map<String, Object> params) throws DbRuntimeException {
public List<Entity> query(final String sql, final Map<String, Object> params) throws DbException {
return query(sql, new EntityListHandler(this.caseInsensitive), params);
}
@ -91,10 +91,10 @@ public abstract class AbstractDb<R extends AbstractDb<R>> extends DefaultConnect
* @param sql 查询语句
* @param params 参数
* @return 结果对象
* @throws DbRuntimeException SQL执行异常
* @throws DbException SQL执行异常
* @since 3.1.1
*/
public List<Entity> query(final String sql, final Object... params) throws DbRuntimeException {
public List<Entity> 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<R extends AbstractDb<R>> extends DefaultConnect
* @param beanClass 元素Bean类型
* @param params 参数
* @return 结果对象
* @throws DbRuntimeException SQL执行异常
* @throws DbException SQL执行异常
* @since 3.2.2
*/
public <T> List<T> query(final String sql, final Class<T> beanClass, final Object... params) throws DbRuntimeException {
public <T> List<T> query(final String sql, final Class<T> beanClass, final Object... params) throws DbException {
return query(sql, new BeanListHandler<>(beanClass), params);
}
@ -119,9 +119,9 @@ public abstract class AbstractDb<R extends AbstractDb<R>> 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<R extends AbstractDb<R>> 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<R extends AbstractDb<R>> 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<R extends AbstractDb<R>> extends DefaultConnect
* @param rsh 结果集处理对象
* @param params 参数
* @return 结果对象
* @throws DbRuntimeException SQL执行异常
* @throws DbException SQL执行异常
*/
public <T> T query(final String sql, final RsHandler<T> rsh, final Object... params) throws DbRuntimeException {
public <T> T query(final String sql, final RsHandler<T> rsh, final Object... params) throws DbException {
Connection conn = null;
try {
conn = this.getConnection();
@ -177,10 +177,10 @@ public abstract class AbstractDb<R extends AbstractDb<R>> extends DefaultConnect
* @param rsh 结果集处理对象
* @param paramMap 参数
* @return 结果对象
* @throws DbRuntimeException SQL执行异常
* @throws DbException SQL执行异常
* @since 5.2.2
*/
public <T> T query(final String sql, final RsHandler<T> rsh, final Map<String, Object> paramMap) throws DbRuntimeException {
public <T> T query(final String sql, final RsHandler<T> rsh, final Map<String, Object> paramMap) throws DbException {
Connection conn = null;
try {
conn = this.getConnection();
@ -198,10 +198,10 @@ public abstract class AbstractDb<R extends AbstractDb<R>> extends DefaultConnect
* @param statementFunc 自定义{@link PreparedStatement}创建函数
* @param rsh 结果集处理对象
* @return 结果对象
* @throws DbRuntimeException SQL执行异常
* @throws DbException SQL执行异常
* @since 5.7.17
*/
public <T> T query(final SerFunction<Connection, PreparedStatement> statementFunc, final RsHandler<T> rsh) throws DbRuntimeException {
public <T> T query(final SerFunction<Connection, PreparedStatement> statementFunc, final RsHandler<T> rsh) throws DbException {
Connection conn = null;
try {
conn = this.getConnection();
@ -221,9 +221,9 @@ public abstract class AbstractDb<R extends AbstractDb<R>> 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<R extends AbstractDb<R>> 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<R extends AbstractDb<R>> 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<Object[]> paramsBatch) throws DbRuntimeException {
public int[] executeBatch(final String sql, final Iterable<Object[]> paramsBatch) throws DbException {
Connection conn = null;
try {
conn = this.getConnection();
@ -276,10 +276,10 @@ public abstract class AbstractDb<R extends AbstractDb<R>> 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<R extends AbstractDb<R>> extends DefaultConnect
*
* @param sqls SQL列表
* @return 每个SQL执行影响的行数
* @throws DbRuntimeException SQL执行异常
* @throws DbException SQL执行异常
* @since 5.4.2
*/
public int[] executeBatch(final Iterable<String> sqls) throws DbRuntimeException {
public int[] executeBatch(final Iterable<String> sqls) throws DbException {
Connection conn = null;
try {
conn = this.getConnection();
@ -317,9 +317,9 @@ public abstract class AbstractDb<R extends AbstractDb<R>> 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<R extends AbstractDb<R>> 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<R extends AbstractDb<R>> 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<R extends AbstractDb<R>> extends DefaultConnect
*
* @param records 记录列表
* @return 插入行数
* @throws DbRuntimeException SQL执行异常
* @throws DbException SQL执行异常
*/
public int[] insert(final Collection<Entity> records) throws DbRuntimeException {
public int[] insert(final Collection<Entity> records) throws DbException {
Connection conn = null;
try {
conn = this.getConnection();
@ -394,9 +394,9 @@ public abstract class AbstractDb<R extends AbstractDb<R>> extends DefaultConnect
*
* @param record 记录
* @return 主键列表
* @throws DbRuntimeException SQL执行异常
* @throws DbException SQL执行异常
*/
public List<Object> insertForGeneratedKeys(final Entity record) throws DbRuntimeException {
public List<Object> insertForGeneratedKeys(final Entity record) throws DbException {
Connection conn = null;
try {
conn = this.getConnection();
@ -411,9 +411,9 @@ public abstract class AbstractDb<R extends AbstractDb<R>> 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<R extends AbstractDb<R>> 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<R extends AbstractDb<R>> 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<R extends AbstractDb<R>> 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<R extends AbstractDb<R>> extends DefaultConnect
* @param field 字段名
* @param value 字段值
* @return 记录
* @throws DbRuntimeException SQL执行异常
* @throws DbException SQL执行异常
*/
public <T> Entity get(final String tableName, final String field, final T value) throws DbRuntimeException {
public <T> 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<R extends AbstractDb<R>> 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<R extends AbstractDb<R>> extends DefaultConnect
* @param where 条件实体类包含表名
* @param rsh 结果集处理对象
* @return 结果对象
* @throws DbRuntimeException SQL执行异常
* @throws DbException SQL执行异常
*/
public <T> T find(final Collection<String> fields, final Entity where, final RsHandler<T> rsh) throws DbRuntimeException {
public <T> T find(final Collection<String> fields, final Entity where, final RsHandler<T> rsh) throws DbException {
return find(Query.of(where).setFields(fields), rsh);
}
@ -533,10 +533,10 @@ public abstract class AbstractDb<R extends AbstractDb<R>> extends DefaultConnect
* @param fields 返回的字段列表null则返回所有字段
* @param where 条件实体类包含表名
* @return 结果Entity列表
* @throws DbRuntimeException SQL执行异常
* @throws DbException SQL执行异常
* @since 4.5.16
*/
public List<Entity> find(final Collection<String> fields, final Entity where) throws DbRuntimeException {
public List<Entity> find(final Collection<String> fields, final Entity where) throws DbException {
return find(fields, where, new EntityListHandler(this.caseInsensitive));
}
@ -548,10 +548,10 @@ public abstract class AbstractDb<R extends AbstractDb<R>> extends DefaultConnect
* @param query {@link Query}对象此对象中可以定义返回字段查询条件查询的表分页等信息
* @param rsh 结果集处理对象
* @return 结果对象
* @throws DbRuntimeException SQL执行异常
* @throws DbException SQL执行异常
* @since 4.0.0
*/
public <T> T find(final Query query, final RsHandler<T> rsh) throws DbRuntimeException {
public <T> T find(final Query query, final RsHandler<T> rsh) throws DbException {
Connection conn = null;
try {
conn = this.getConnection();
@ -570,9 +570,9 @@ public abstract class AbstractDb<R extends AbstractDb<R>> extends DefaultConnect
* @param rsh 结果集处理对象
* @param fields 字段列表可变长参数如果无值表示查询全部字段
* @return 结果对象
* @throws DbRuntimeException SQL执行异常
* @throws DbException SQL执行异常
*/
public <T> T find(final Entity where, final RsHandler<T> rsh, final String... fields) throws DbRuntimeException {
public <T> T find(final Entity where, final RsHandler<T> rsh, final String... fields) throws DbException {
return find(Arrays.asList(fields), where, rsh);
}
@ -582,10 +582,10 @@ public abstract class AbstractDb<R extends AbstractDb<R>> extends DefaultConnect
*
* @param where 条件实体类包含表名
* @return 数据对象列表
* @throws DbRuntimeException SQL执行异常
* @throws DbException SQL执行异常
* @since 3.2.1
*/
public List<Entity> find(final Entity where) throws DbRuntimeException {
public List<Entity> find(final Entity where) throws DbException {
return find(where.getFieldNames(), where, new EntityListHandler(this.caseInsensitive));
}
@ -597,10 +597,10 @@ public abstract class AbstractDb<R extends AbstractDb<R>> extends DefaultConnect
* @param where 条件实体类包含表名
* @param beanClass Bean类
* @return 数据对象列表
* @throws DbRuntimeException SQL执行异常
* @throws DbException SQL执行异常
* @since 3.2.2
*/
public <T> List<T> find(final Entity where, final Class<T> beanClass) throws DbRuntimeException {
public <T> List<T> find(final Entity where, final Class<T> beanClass) throws DbException {
return find(where.getFieldNames(), where, BeanListHandler.of(beanClass));
}
@ -610,9 +610,9 @@ public abstract class AbstractDb<R extends AbstractDb<R>> extends DefaultConnect
*
* @param where 条件实体类包含表名
* @return 数据对象列表
* @throws DbRuntimeException SQL执行异常
* @throws DbException SQL执行异常
*/
public List<Entity> findAll(final Entity where) throws DbRuntimeException {
public List<Entity> findAll(final Entity where) throws DbException {
return find(where, new EntityListHandler(this.caseInsensitive));
}
@ -624,10 +624,10 @@ public abstract class AbstractDb<R extends AbstractDb<R>> extends DefaultConnect
* @param where 条件实体类包含表名
* @param beanClass 返回的对象类型
* @return 数据对象列表
* @throws DbRuntimeException SQL执行异常
* @throws DbException SQL执行异常
* @since 3.2.2
*/
public <T> List<T> findAll(final Entity where, final Class<T> beanClass) throws DbRuntimeException {
public <T> List<T> findAll(final Entity where, final Class<T> beanClass) throws DbException {
return find(where, BeanListHandler.of(beanClass));
}
@ -636,9 +636,9 @@ public abstract class AbstractDb<R extends AbstractDb<R>> extends DefaultConnect
*
* @param tableName 表名
* @return 数据对象列表
* @throws DbRuntimeException SQL执行异常
* @throws DbException SQL执行异常
*/
public List<Entity> findAll(final String tableName) throws DbRuntimeException {
public List<Entity> findAll(final String tableName) throws DbException {
return findAll(Entity.of(tableName));
}
@ -649,9 +649,9 @@ public abstract class AbstractDb<R extends AbstractDb<R>> extends DefaultConnect
* @param field 字段名
* @param value 字段值
* @return 数据对象列表
* @throws DbRuntimeException SQL执行异常
* @throws DbException SQL执行异常
*/
public List<Entity> findBy(final String tableName, final String field, final Object value) throws DbRuntimeException {
public List<Entity> 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<R extends AbstractDb<R>> extends DefaultConnect
* @param tableName 表名
* @param wheres 条件多个条件的连接逻辑使用{@link Condition#setLinkOperator(LogicalOperator)} 定义
* @return 数据对象列表
* @throws DbRuntimeException SQL执行异常
* @throws DbException SQL执行异常
* @since 4.0.0
*/
public List<Entity> findBy(final String tableName, final Condition... wheres) throws DbRuntimeException {
public List<Entity> 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<R extends AbstractDb<R>> extends DefaultConnect
* @param value 字段值
* @param likeType {@link LikeType}
* @return 数据对象列表
* @throws DbRuntimeException SQL执行异常
* @throws DbException SQL执行异常
*/
public List<Entity> findLike(final String tableName, final String field, final String value, final LikeType likeType) throws DbRuntimeException {
public List<Entity> 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<R extends AbstractDb<R>> 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<R extends AbstractDb<R>> 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<R extends AbstractDb<R>> 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<R extends AbstractDb<R>> extends DefaultConnect
* @param where 条件实体类包含表名
* @param page 分页对象
* @return 结果对象
* @throws DbRuntimeException SQL执行异常
* @throws DbException SQL执行异常
* @since 3.2.2
*/
public List<Entity> pageForEntityList(final Entity where, final Page page) throws DbRuntimeException {
public List<Entity> 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<R extends AbstractDb<R>> extends DefaultConnect
* @param page 分页对象
* @param rsh 结果集处理对象
* @return 结果对象
* @throws DbRuntimeException SQL执行异常
* @throws DbException SQL执行异常
* @since 3.2.2
*/
public <T> T page(final Entity where, final Page page, final RsHandler<T> rsh) throws DbRuntimeException {
public <T> T page(final Entity where, final Page page, final RsHandler<T> rsh) throws DbException {
return page(where.getFieldNames(), where, page, rsh);
}
@ -813,9 +813,9 @@ public abstract class AbstractDb<R extends AbstractDb<R>> extends DefaultConnect
* @param page 分页对象
* @param rsh 结果集处理对象
* @return 结果对象
* @throws DbRuntimeException SQL执行异常
* @throws DbException SQL执行异常
*/
public <T> T page(final Collection<String> fields, final Entity where, final Page page, final RsHandler<T> rsh) throws DbRuntimeException {
public <T> T page(final Collection<String> fields, final Entity where, final Page page, final RsHandler<T> rsh) throws DbException {
Connection conn = null;
try {
conn = this.getConnection();
@ -834,10 +834,10 @@ public abstract class AbstractDb<R extends AbstractDb<R>> extends DefaultConnect
* @param rsh 结果集处理对象
* @param params 参数
* @return 结果对象
* @throws DbRuntimeException SQL执行异常
* @throws DbException SQL执行异常
* @since 5.6.6
*/
public <T> T page(final CharSequence sql, final Page page, final RsHandler<T> rsh, final Object... params) throws DbRuntimeException {
public <T> T page(final CharSequence sql, final Page page, final RsHandler<T> rsh, final Object... params) throws DbException {
Connection conn = null;
try {
conn = this.getConnection();
@ -855,9 +855,9 @@ public abstract class AbstractDb<R extends AbstractDb<R>> extends DefaultConnect
* @param page 分页对象
* @param rsh 结果集处理对象
* @return 结果对象
* @throws DbRuntimeException SQL执行异常
* @throws DbException SQL执行异常
*/
public <T> T page(final SqlBuilder sql, final Page page, final RsHandler<T> rsh) throws DbRuntimeException {
public <T> T page(final SqlBuilder sql, final Page page, final RsHandler<T> rsh) throws DbException {
Connection conn = null;
try {
conn = this.getConnection();
@ -874,10 +874,10 @@ public abstract class AbstractDb<R extends AbstractDb<R>> extends DefaultConnect
* @param page 分页对象
* @param params 参数列表
* @return 结果对象
* @throws DbRuntimeException SQL执行异常
* @throws DbException SQL执行异常
* @since 5.5.3
*/
public PageResult<Entity> page(final CharSequence sql, final Page page, final Object... params) throws DbRuntimeException {
public PageResult<Entity> 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<R extends AbstractDb<R>> extends DefaultConnect
* @param pageNumber 页码
* @param pageSize 每页结果数
* @return 结果对象
* @throws DbRuntimeException SQL执行异常
* @throws DbException SQL执行异常
*/
public PageResult<Entity> page(final Collection<String> fields, final Entity where, final int pageNumber, final int pageSize) throws DbRuntimeException {
public PageResult<Entity> page(final Collection<String> 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<R extends AbstractDb<R>> extends DefaultConnect
* @param where 条件实体类包含表名
* @param page 分页对象
* @return 结果对象
* @throws DbRuntimeException SQL执行异常
* @throws DbException SQL执行异常
*/
public PageResult<Entity> page(final Collection<String> fields, final Entity where, final Page page) throws DbRuntimeException {
public PageResult<Entity> page(final Collection<String> fields, final Entity where, final Page page) throws DbException {
Connection conn = null;
try {
conn = this.getConnection();
@ -929,9 +929,9 @@ public abstract class AbstractDb<R extends AbstractDb<R>> extends DefaultConnect
* @param where 条件实体类包含表名
* @param page 分页对象
* @return 分页结果集
* @throws DbRuntimeException SQL执行异常
* @throws DbException SQL执行异常
*/
public PageResult<Entity> page(final Entity where, final Page page) throws DbRuntimeException {
public PageResult<Entity> 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<R extends AbstractDb<R>> 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

View File

@ -25,9 +25,9 @@ public interface ConnectionHolder {
* 获得链接根据实现不同可以自定义获取连接的方式
*
* @return {@link Connection}
* @throws DbRuntimeException 连接获取异常
* @throws DbException 连接获取异常
*/
Connection getConnection() throws DbRuntimeException;
Connection getConnection() throws DbException;
/**
* 关闭连接<br>

View File

@ -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<Object> addForGeneratedKeys(final Entity entity) throws DbRuntimeException {
public List<Object> 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 <T> 主键类型
* @param pk 主键
* @return 删除行数
* @throws DbRuntimeException SQL执行异常
* @throws DbException SQL执行异常
*/
public <T> int del(final T pk) throws DbRuntimeException {
public <T> 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 <T> int del(final String field, final T value) throws DbRuntimeException {
public <T> 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 <T> 主键类型
* @param pk 主键值
* @return 记录
* @throws DbRuntimeException SQL执行异常
* @throws DbException SQL执行异常
*/
public <T> Entity get(final T pk) throws DbRuntimeException {
public <T> 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 <T> Entity get(final String field, final T value) throws DbRuntimeException {
public <T> 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 <T> List<Entity> find(final String field, final T value) throws DbRuntimeException {
public <T> List<Entity> 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<Entity> findAll() throws DbRuntimeException {
public List<Entity> 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<Entity> find(final Entity where) throws DbRuntimeException {
public List<Entity> 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<Entity> findBySql(String sql, final Object... params) throws DbRuntimeException {
public List<Entity> 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<Entity> page(final Entity where, final Page page, final String... selectFields) throws DbRuntimeException {
public PageResult<Entity> 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<Entity> page(final Entity where, final Page page) throws DbRuntimeException {
public PageResult<Entity> 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

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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> T insert(final Connection conn, final Entity record, final RsHandler<T> generatedKeysHandler) throws DbRuntimeException {
public <T> T insert(final Connection conn, final Entity record, final RsHandler<T> 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> T find(final Connection conn, final Query query, final RsHandler<T> rsh) throws DbRuntimeException {
public <T> T find(final Connection conn, final Query query, final RsHandler<T> 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<Entity> page(final Connection conn, final Query query) throws DbRuntimeException {
public PageResult<Entity> page(final Connection conn, final Query query) throws DbException {
final Page page = query.getPage();
final PageResultHandler<Entity> 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> T page(final Connection conn, final Query query, final RsHandler<T> rsh) throws DbRuntimeException {
public <T> T page(final Connection conn, final Query query, final RsHandler<T> 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<Entity> page(final Connection conn, final SqlBuilder sqlBuilder, final Page page) throws DbRuntimeException {
public PageResult<Entity> page(final Connection conn, final SqlBuilder sqlBuilder, final Page page) throws DbException {
final PageResultHandler<Entity> 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> T page(final Connection conn, final SqlBuilder sqlBuilder, final Page page, final RsHandler<T> rsh) throws DbRuntimeException {
public <T> T page(final Connection conn, final SqlBuilder sqlBuilder, final Page page, final RsHandler<T> 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);
}

View File

@ -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

View File

@ -104,28 +104,28 @@ public class Session extends AbstractDb<Session> 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<Session> 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<Session> 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<Session> 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<Session> implements Closeable {
* Connection.TRANSACTION_SERIALIZABLE 禁止脏读不可重复读和幻读<br>
*
* @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<Session> implements Closeable {
* 在事务中执行操作通过实现{@link SerConsumer}接口的call方法执行多条SQL语句从而完成事务
*
* @param func 函数抽象在函数中执行多个SQL操作多个操作会被合并为同一事务
* @throws DbRuntimeException SQL异常
* @throws DbException SQL异常
* @since 3.2.3
*/
public void tx(final SerConsumer<Session> func) throws DbRuntimeException {
public void tx(final SerConsumer<Session> func) throws DbException {
try {
beginTransaction();
func.accept(this);
commit();
} catch (final Throwable e) {
quietRollback();
throw new DbRuntimeException(e);
throw new DbException(e);
}
}

View File

@ -24,6 +24,7 @@ import java.util.Properties;
* <li>基本配置项如driverurluserpassword等</li>
* <li>连接配置如remarksuseInformationSchema等</li>
* <li>连接池配置如初始容量最大容量等取决于连接池库具体要求</li>
* <li>其它配置如是否大小写敏感SQL过滤器等</li>
* </ul>
*
* @author Looly

View File

@ -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配置项

View File

@ -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);

View File

@ -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;

View File

@ -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);
}
}

View File

@ -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());

View File

@ -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);
}

View File

@ -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<Conn
try {
this.raw = DriverManager.getConnection(config.getUrl(), info);
} catch (final SQLException e) {
throw new DbRuntimeException(e);
throw new DbException(e);
}
this.dataSource = dataSource;

View File

@ -17,7 +17,7 @@ import org.dromara.hutool.core.pool.ObjectFactory;
import org.dromara.hutool.core.pool.ObjectPool;
import org.dromara.hutool.core.pool.partition.PartitionObjectPool;
import org.dromara.hutool.core.pool.partition.PartitionPoolConfig;
import org.dromara.hutool.db.DbRuntimeException;
import org.dromara.hutool.db.DbException;
import org.dromara.hutool.db.config.DbConfig;
import org.dromara.hutool.db.ds.simple.AbstractDataSource;
import org.dromara.hutool.setting.props.Props;
@ -102,7 +102,7 @@ public class PooledDataSource extends AbstractDataSource {
return null != connection
&& connection.isValid(maxWait);
} catch (final SQLException e) {
throw new DbRuntimeException(e);
throw new DbException(e);
}
}

View File

@ -15,7 +15,7 @@ package org.dromara.hutool.db.handler;
import org.dromara.hutool.core.convert.Convert;
import org.dromara.hutool.core.lang.Assert;
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.handler.row.*;
@ -147,7 +147,7 @@ public class ResultSetUtil {
if (rs != null && rs.next()) {
try {
generatedKey = rs.getLong(1);
} catch (final DbRuntimeException e) {
} catch (final DbException e) {
// 自增主键不为数字或者为Oracle的rowid跳过
}
}

View File

@ -14,7 +14,7 @@ package org.dromara.hutool.db.meta;
import org.dromara.hutool.core.util.BooleanUtil;
import org.dromara.hutool.core.regex.ReUtil;
import org.dromara.hutool.db.DbRuntimeException;
import org.dromara.hutool.db.DbException;
import java.io.Serializable;
import java.sql.ResultSet;
@ -108,7 +108,7 @@ public class Column implements Serializable, Cloneable {
try {
init(table, columnMetaRs);
} catch (final SQLException e) {
throw new DbRuntimeException(e, "Get table [{}] meta info error!", tableName);
throw new DbException(e, "Get table [{}] meta info error!", tableName);
}
}
// ----------------------------------------------------- Constructor end

View File

@ -12,7 +12,7 @@
package org.dromara.hutool.db.meta;
import org.dromara.hutool.db.DbRuntimeException;
import org.dromara.hutool.db.DbException;
import java.io.Serializable;
import java.sql.ResultSet;
@ -39,7 +39,7 @@ public class ColumnIndexInfo implements Serializable, Cloneable {
rs.getString("COLUMN_NAME"),
rs.getString("ASC_OR_DESC"));
} catch (final SQLException e) {
throw new DbRuntimeException(e);
throw new DbException(e);
}
}

View File

@ -16,7 +16,7 @@ import org.dromara.hutool.core.collection.ListUtil;
import org.dromara.hutool.core.convert.Convert;
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.Entity;
import javax.sql.DataSource;
@ -108,7 +108,7 @@ public class MetaUtil {
}
}
} catch (final Exception e) {
throw new DbRuntimeException("Get tables error!", e);
throw new DbException("Get tables error!", e);
} finally {
IoUtil.closeQuietly(conn);
}
@ -120,9 +120,9 @@ public class MetaUtil {
*
* @param rs 结果集
* @return 列名数组
* @throws DbRuntimeException SQL执行异常
* @throws DbException SQL执行异常
*/
public static String[] getColumnNames(final ResultSet rs) throws DbRuntimeException {
public static String[] getColumnNames(final ResultSet rs) throws DbException {
try {
final ResultSetMetaData rsmd = rs.getMetaData();
final int columnCount = rsmd.getColumnCount();
@ -132,7 +132,7 @@ public class MetaUtil {
}
return labelNames;
} catch (final Exception e) {
throw new DbRuntimeException("Get colunms error!", e);
throw new DbException("Get colunms error!", e);
}
}
@ -142,7 +142,7 @@ public class MetaUtil {
* @param ds 数据源
* @param tableName 表名
* @return 列数组
* @throws DbRuntimeException SQL执行异常
* @throws DbException SQL执行异常
*/
public static String[] getColumnNames(final DataSource ds, final String tableName) {
final List<String> 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);
}

View File

@ -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);
}
}

View File

@ -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];
}

View File

@ -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<String> {
* 验证实体类对象的有效性
*
* @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<String> {
*/
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<String> {
*/
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<String> {
*/
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) {

View File

@ -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<String, Object> paramMap) throws DbRuntimeException {
public static int execute(final Connection conn, final String sql, final Map<String, Object> 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<String, Object> paramMap) throws DbRuntimeException {
public static Long executeForGeneratedKey(final Connection conn, final String sql, final Map<String, Object> 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<Object[]> paramsBatch) throws DbRuntimeException {
public static int[] executeBatch(final Connection conn, final String sql, final Iterable<Object[]> 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<String> sqls) throws DbRuntimeException {
public static int[] executeBatch(final Connection conn, final Iterable<String> 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> T query(final Connection conn, final String sql, final RsHandler<T> rsh, final Map<String, Object> paramMap) throws DbRuntimeException {
public static <T> T query(final Connection conn, final String sql, final RsHandler<T> rsh, final Map<String, Object> 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> T query(final Connection conn, final SqlBuilder sqlBuilder, final RsHandler<T> rsh) throws DbRuntimeException {
public static <T> T query(final Connection conn, final SqlBuilder sqlBuilder, final RsHandler<T> 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> T query(final Connection conn, final String sql, final RsHandler<T> rsh, final Object... params) throws DbRuntimeException {
public static <T> T query(final Connection conn, final String sql, final RsHandler<T> 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> T query(final Connection conn, final SerFunction<Connection, PreparedStatement> statementFunc, final RsHandler<T> rsh) throws DbRuntimeException {
public static <T> T query(final Connection conn, final SerFunction<Connection, PreparedStatement> statementFunc, final RsHandler<T> 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> T query(final PreparedStatement ps, final RsHandler<T> rsh, final Object... params) throws DbRuntimeException {
public static <T> T query(final PreparedStatement ps, final RsHandler<T> 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> T queryAndClosePs(final PreparedStatement ps, final RsHandler<T> rsh, final Object... params) throws DbRuntimeException {
public static <T> T queryAndClosePs(final PreparedStatement ps, final RsHandler<T> 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> T executeQuery(final PreparedStatement ps, final RsHandler<T> rsh) throws DbRuntimeException {
private static <T> T executeQuery(final PreparedStatement ps, final RsHandler<T> 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);
}

View File

@ -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;
}

View File

@ -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<StatementWrapper> {
try {
return _build();
} catch (final SQLException e) {
throw new DbRuntimeException(e);
throw new DbException(e);
}
}
@ -126,9 +126,9 @@ public class StatementBuilder implements Builder<StatementWrapper> {
* 创建批量操作的{@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<Object> paramsBatch = this.boundSql.getParams();
@ -159,7 +159,7 @@ public class StatementBuilder implements Builder<StatementWrapper> {
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<StatementWrapper> {
.fillArrayParam(params)
.getRaw();
} catch (final SQLException e) {
throw new DbRuntimeException(e);
throw new DbException(e);
}
}