mirror of
https://github.com/CarmJos/EasySQL.git
synced 2024-09-20 05:45:47 +00:00
[v0.2.9] [A] 添加自定义默认异常处理器的方法。
This commit is contained in:
parent
360b416525
commit
a8a475fc7a
@ -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.SQLExceptionHandler;
|
||||||
import cc.carm.lib.easysql.api.function.SQLFunction;
|
import cc.carm.lib.easysql.api.function.SQLFunction;
|
||||||
import cc.carm.lib.easysql.api.function.SQLHandler;
|
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.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
|
import java.util.Optional;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -147,7 +149,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 = getExceptionHandler();
|
if (handler == null) handler = defaultExceptionHandler();
|
||||||
handler.accept(exception, this);
|
handler.accept(exception, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -155,22 +157,10 @@ public interface SQLAction<T> {
|
|||||||
* @return 默认的异常处理器
|
* @return 默认的异常处理器
|
||||||
*/
|
*/
|
||||||
default SQLExceptionHandler defaultExceptionHandler() {
|
default SQLExceptionHandler defaultExceptionHandler() {
|
||||||
return (exception, action) -> {
|
return Optional.ofNullable(DefaultSQLExceptionHandler.getCustomHandler())
|
||||||
getManager().getLogger().severe("Error when execute [" + action.getSQLContent() + "]");
|
.orElse(new DefaultSQLExceptionHandler(getManager()));
|
||||||
getManager().getLogger().severe(exception.getLocalizedMessage());
|
|
||||||
exception.printStackTrace();
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 得到通用的异常处理器。
|
|
||||||
* <br> 若未使用 {@link #setExceptionHandler(SQLExceptionHandler)} 方法
|
|
||||||
* <br> 则会返回 {@link #defaultExceptionHandler()} 。
|
|
||||||
*
|
|
||||||
* @return 通用异常处理器。
|
|
||||||
*/
|
|
||||||
@NotNull SQLExceptionHandler getExceptionHandler();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 设定通用的异常处理器。
|
* 设定通用的异常处理器。
|
||||||
* <br> 在使用 {@link #execute(SQLExceptionHandler)} 等相关方法时,若传入的处理器为null,则会采用此处理器。
|
* <br> 在使用 {@link #execute(SQLExceptionHandler)} 等相关方法时,若传入的处理器为null,则会采用此处理器。
|
||||||
@ -178,6 +168,8 @@ public interface SQLAction<T> {
|
|||||||
*
|
*
|
||||||
* @param handler 异常处理器
|
* @param handler 异常处理器
|
||||||
*/
|
*/
|
||||||
void setExceptionHandler(@Nullable SQLExceptionHandler handler);
|
default void setExceptionHandler(@Nullable SQLExceptionHandler handler) {
|
||||||
|
DefaultSQLExceptionHandler.setCustomHandler(handler);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -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();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -8,8 +8,8 @@ import org.jetbrains.annotations.NotNull;
|
|||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
|
import java.util.Optional;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import java.util.function.BiConsumer;
|
|
||||||
|
|
||||||
public abstract class AbstractSQLAction<T> implements SQLAction<T> {
|
public abstract class AbstractSQLAction<T> implements SQLAction<T> {
|
||||||
|
|
||||||
@ -20,7 +20,7 @@ public abstract class AbstractSQLAction<T> implements SQLAction<T> {
|
|||||||
|
|
||||||
protected @NotNull String sqlContent;
|
protected @NotNull String sqlContent;
|
||||||
|
|
||||||
protected @Nullable BiConsumer<SQLException, SQLAction<T>> exceptionHandler = null;
|
protected static @Nullable SQLExceptionHandler exceptionHandler = null;
|
||||||
|
|
||||||
public AbstractSQLAction(@NotNull SQLManagerImpl manager, @NotNull String sql) {
|
public AbstractSQLAction(@NotNull SQLManagerImpl manager, @NotNull String sql) {
|
||||||
this(manager, sql, System.currentTimeMillis());
|
this(manager, sql, System.currentTimeMillis());
|
||||||
|
@ -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().getExceptionHandler(), e);
|
getAction().handleException(getAction().defaultExceptionHandler(), e);
|
||||||
}
|
}
|
||||||
this.queryAction = null;
|
this.queryAction = null;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user