mirror of
https://github.com/CarmJos/EasySQL.git
synced 2026-06-04 15:28:20 +08:00
[v0.2.9] [A] 添加自定义默认异常处理器的方法。
This commit is contained in:
@@ -84,24 +84,6 @@ public interface SQLAction<T> {
|
|||||||
return execute(t -> t, exceptionHandler);
|
return execute(t -> t, exceptionHandler);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 执行语句并处理返回值
|
|
||||||
*
|
|
||||||
* @param function 处理方法
|
|
||||||
* @param <R> 需要返回的内容
|
|
||||||
* @return 指定类型数据
|
|
||||||
* @throws SQLException 当SQL操作出现问题时抛出
|
|
||||||
*/
|
|
||||||
@Nullable
|
|
||||||
default <R> R executeFunction(@NotNull SQLFunction<T, R> function) throws SQLException {
|
|
||||||
try {
|
|
||||||
T value = execute();
|
|
||||||
return function.apply(value);
|
|
||||||
} catch (SQLException exception) {
|
|
||||||
throw new SQLException(exception);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 执行语句并处理返回值
|
* 执行语句并处理返回值
|
||||||
*
|
*
|
||||||
@@ -121,6 +103,24 @@ public interface SQLAction<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 执行语句并处理返回值
|
||||||
|
*
|
||||||
|
* @param function 处理方法
|
||||||
|
* @param <R> 需要返回的内容
|
||||||
|
* @return 指定类型数据
|
||||||
|
* @throws SQLException 当SQL操作出现问题时抛出
|
||||||
|
*/
|
||||||
|
@Nullable
|
||||||
|
default <R> R executeFunction(@NotNull SQLFunction<T, R> function) throws SQLException {
|
||||||
|
try {
|
||||||
|
T value = execute();
|
||||||
|
return function.apply(value);
|
||||||
|
} catch (SQLException exception) {
|
||||||
|
throw new SQLException(exception);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 异步执行SQL语句,采用默认异常处理,无需返回值。
|
* 异步执行SQL语句,采用默认异常处理,无需返回值。
|
||||||
*/
|
*/
|
||||||
@@ -147,7 +147,7 @@ public interface SQLAction<T> {
|
|||||||
@Nullable SQLExceptionHandler failure);
|
@Nullable SQLExceptionHandler failure);
|
||||||
|
|
||||||
default void handleException(@Nullable SQLExceptionHandler handler, SQLException exception) {
|
default void handleException(@Nullable SQLExceptionHandler handler, SQLException exception) {
|
||||||
if (handler == null) handler = defaultExceptionHandler();
|
if (handler == null) handler = getExceptionHandler();
|
||||||
handler.accept(exception, this);
|
handler.accept(exception, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -162,5 +162,22 @@ public interface SQLAction<T> {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 得到通用的异常处理器。
|
||||||
|
* <br> 若未使用 {@link #setExceptionHandler(SQLExceptionHandler)} 方法
|
||||||
|
* <br> 则会返回 {@link #defaultExceptionHandler()} 。
|
||||||
|
*
|
||||||
|
* @return 通用异常处理器。
|
||||||
|
*/
|
||||||
|
@NotNull SQLExceptionHandler getExceptionHandler();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设定通用的异常处理器。
|
||||||
|
* <br> 在使用 {@link #execute(SQLExceptionHandler)} 等相关方法时,若传入的处理器为null,则会采用此处理器。
|
||||||
|
* <br> 若该方法传入参数为 null,则会使用 {@link #defaultExceptionHandler()} 。
|
||||||
|
*
|
||||||
|
* @param handler 异常处理器
|
||||||
|
*/
|
||||||
|
void setExceptionHandler(@Nullable SQLExceptionHandler handler);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -70,7 +70,7 @@ public class SQLQueryImpl implements SQLQuery {
|
|||||||
);
|
);
|
||||||
getManager().getActiveQuery().remove(getAction().getActionUUID());
|
getManager().getActiveQuery().remove(getAction().getActionUUID());
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
getAction().handleException(getAction().defaultExceptionHandler(), e);
|
getAction().handleException(getAction().getExceptionHandler(), e);
|
||||||
}
|
}
|
||||||
this.queryAction = null;
|
this.queryAction = null;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user