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 c8a9d87..4cfec06 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 @@ -3,10 +3,12 @@ package cc.carm.lib.easysql.api; import cc.carm.lib.easysql.api.function.SQLExceptionHandler; import cc.carm.lib.easysql.api.function.SQLFunction; import cc.carm.lib.easysql.api.function.SQLHandler; +import cc.carm.lib.easysql.api.function.impl.DefaultSQLExceptionHandler; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.sql.SQLException; +import java.util.Optional; import java.util.UUID; /** @@ -147,7 +149,7 @@ public interface SQLAction { @Nullable SQLExceptionHandler failure); default void handleException(@Nullable SQLExceptionHandler handler, SQLException exception) { - if (handler == null) handler = getExceptionHandler(); + if (handler == null) handler = defaultExceptionHandler(); handler.accept(exception, this); } @@ -155,22 +157,10 @@ public interface SQLAction { * @return 默认的异常处理器 */ default SQLExceptionHandler defaultExceptionHandler() { - return (exception, action) -> { - getManager().getLogger().severe("Error when execute [" + action.getSQLContent() + "]"); - getManager().getLogger().severe(exception.getLocalizedMessage()); - exception.printStackTrace(); - }; + return Optional.ofNullable(DefaultSQLExceptionHandler.getCustomHandler()) + .orElse(new DefaultSQLExceptionHandler(getManager())); } - /** - * 得到通用的异常处理器。 - *
若未使用 {@link #setExceptionHandler(SQLExceptionHandler)} 方法 - *
则会返回 {@link #defaultExceptionHandler()} 。 - * - * @return 通用异常处理器。 - */ - @NotNull SQLExceptionHandler getExceptionHandler(); - /** * 设定通用的异常处理器。 *
在使用 {@link #execute(SQLExceptionHandler)} 等相关方法时,若传入的处理器为null,则会采用此处理器。 @@ -178,6 +168,8 @@ public interface SQLAction { * * @param handler 异常处理器 */ - void setExceptionHandler(@Nullable SQLExceptionHandler handler); + default void setExceptionHandler(@Nullable SQLExceptionHandler handler) { + DefaultSQLExceptionHandler.setCustomHandler(handler); + } } diff --git a/easysql-api/src/main/java/cc/carm/lib/easysql/api/function/impl/DefaultSQLExceptionHandler.java b/easysql-api/src/main/java/cc/carm/lib/easysql/api/function/impl/DefaultSQLExceptionHandler.java new file mode 100644 index 0000000..2fad658 --- /dev/null +++ b/easysql-api/src/main/java/cc/carm/lib/easysql/api/function/impl/DefaultSQLExceptionHandler.java @@ -0,0 +1,40 @@ +package cc.carm.lib.easysql.api.function.impl; + +import cc.carm.lib.easysql.api.SQLAction; +import cc.carm.lib.easysql.api.SQLManager; +import cc.carm.lib.easysql.api.function.SQLExceptionHandler; +import org.jetbrains.annotations.Nullable; + +import java.sql.SQLException; + +public class DefaultSQLExceptionHandler implements SQLExceptionHandler { + + private static @Nullable SQLExceptionHandler customDefaultHandler = null; + + public static void setCustomHandler(@Nullable SQLExceptionHandler handler) { + DefaultSQLExceptionHandler.customDefaultHandler = handler; + } + + public static @Nullable SQLExceptionHandler getCustomHandler() { + return customDefaultHandler; + } + + private final SQLManager sqlManager; + + public DefaultSQLExceptionHandler(SQLManager manager) { + this.sqlManager = manager; + } + + protected SQLManager getManager() { + return sqlManager; + } + + @Override + public void accept(SQLException exception, SQLAction sqlAction) { + getManager().getLogger().severe("Error when execute [" + sqlAction.getSQLContent() + "]"); + getManager().getLogger().severe(exception.getLocalizedMessage()); + exception.printStackTrace(); + } + + +} diff --git a/easysql-impl/src/main/java/cc/carm/lib/easysql/action/AbstractSQLAction.java b/easysql-impl/src/main/java/cc/carm/lib/easysql/action/AbstractSQLAction.java index 45d7e40..2fe7d7d 100644 --- a/easysql-impl/src/main/java/cc/carm/lib/easysql/action/AbstractSQLAction.java +++ b/easysql-impl/src/main/java/cc/carm/lib/easysql/action/AbstractSQLAction.java @@ -8,8 +8,8 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.sql.SQLException; +import java.util.Optional; import java.util.UUID; -import java.util.function.BiConsumer; public abstract class AbstractSQLAction implements SQLAction { @@ -20,7 +20,7 @@ public abstract class AbstractSQLAction implements SQLAction { protected @NotNull String sqlContent; - protected @Nullable BiConsumer> exceptionHandler = null; + protected static @Nullable SQLExceptionHandler exceptionHandler = null; public AbstractSQLAction(@NotNull SQLManagerImpl manager, @NotNull String sql) { this(manager, sql, System.currentTimeMillis()); 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 978181a..9d24d6a 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().getExceptionHandler(), e); + getAction().handleException(getAction().defaultExceptionHandler(), e); } this.queryAction = null; }