From 360b416525cb5deb9c578037fadbfe3ec9f69e89 Mon Sep 17 00:00:00 2001 From: carm Date: Mon, 24 Jan 2022 15:40:12 +0800 Subject: [PATCH] =?UTF-8?q?[v0.2.9]=20[A]=20=E6=B7=BB=E5=8A=A0=E8=87=AA?= =?UTF-8?q?=E5=AE=9A=E4=B9=89=E9=BB=98=E8=AE=A4=E5=BC=82=E5=B8=B8=E5=A4=84?= =?UTF-8?q?=E7=90=86=E5=99=A8=E7=9A=84=E6=96=B9=E6=B3=95=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cc/carm/lib/easysql/api/SQLAction.java | 55 ++++++++++++------- .../carm/lib/easysql/query/SQLQueryImpl.java | 2 +- 2 files changed, 37 insertions(+), 20 deletions(-) diff --git a/easysql-api/src/main/java/cc/carm/lib/easysql/api/SQLAction.java b/easysql-api/src/main/java/cc/carm/lib/easysql/api/SQLAction.java index 5cfc2e1..c8a9d87 100644 --- a/easysql-api/src/main/java/cc/carm/lib/easysql/api/SQLAction.java +++ b/easysql-api/src/main/java/cc/carm/lib/easysql/api/SQLAction.java @@ -84,24 +84,6 @@ public interface SQLAction { return execute(t -> t, exceptionHandler); } - /** - * 执行语句并处理返回值 - * - * @param function 处理方法 - * @param 需要返回的内容 - * @return 指定类型数据 - * @throws SQLException 当SQL操作出现问题时抛出 - */ - @Nullable - default R executeFunction(@NotNull SQLFunction 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 { } } + /** + * 执行语句并处理返回值 + * + * @param function 处理方法 + * @param 需要返回的内容 + * @return 指定类型数据 + * @throws SQLException 当SQL操作出现问题时抛出 + */ + @Nullable + default R executeFunction(@NotNull SQLFunction function) throws SQLException { + try { + T value = execute(); + return function.apply(value); + } catch (SQLException exception) { + throw new SQLException(exception); + } + } + /** * 异步执行SQL语句,采用默认异常处理,无需返回值。 */ @@ -147,7 +147,7 @@ public interface SQLAction { @Nullable SQLExceptionHandler failure); default void handleException(@Nullable SQLExceptionHandler handler, SQLException exception) { - if (handler == null) handler = defaultExceptionHandler(); + if (handler == null) handler = getExceptionHandler(); handler.accept(exception, this); } @@ -162,5 +162,22 @@ public interface SQLAction { }; } + /** + * 得到通用的异常处理器。 + *
若未使用 {@link #setExceptionHandler(SQLExceptionHandler)} 方法 + *
则会返回 {@link #defaultExceptionHandler()} 。 + * + * @return 通用异常处理器。 + */ + @NotNull SQLExceptionHandler getExceptionHandler(); + + /** + * 设定通用的异常处理器。 + *
在使用 {@link #execute(SQLExceptionHandler)} 等相关方法时,若传入的处理器为null,则会采用此处理器。 + *
若该方法传入参数为 null,则会使用 {@link #defaultExceptionHandler()} 。 + * + * @param handler 异常处理器 + */ + void setExceptionHandler(@Nullable SQLExceptionHandler handler); } diff --git a/easysql-impl/src/main/java/cc/carm/lib/easysql/query/SQLQueryImpl.java b/easysql-impl/src/main/java/cc/carm/lib/easysql/query/SQLQueryImpl.java index 9d24d6a..978181a 100644 --- a/easysql-impl/src/main/java/cc/carm/lib/easysql/query/SQLQueryImpl.java +++ b/easysql-impl/src/main/java/cc/carm/lib/easysql/query/SQLQueryImpl.java @@ -70,7 +70,7 @@ public class SQLQueryImpl implements SQLQuery { ); getManager().getActiveQuery().remove(getAction().getActionUUID()); } catch (SQLException e) { - getAction().handleException(getAction().defaultExceptionHandler(), e); + getAction().handleException(getAction().getExceptionHandler(), e); } this.queryAction = null; }