1
mirror of https://github.com/CarmJos/EasySQL.git synced 2026-06-04 15:28:20 +08:00

[0.3.9] 版本更新

- `[R]` 修改项目结构,移除无用的 `easysql-tester` 模块, 整合相关测试到 `easysql-demo` 的`src/test` 下。
- `[U]` 移除`DefaultSQLExceptionHandler` 类,与 `SQLExceptionHandler` 接口下添加 `detailed()` 与 `silent()` 两种预设错误处理器,并支持通过 `SQLManager#setExceptionHandler()` 方法应用全局生效的默认错误处理器。
> 注意: 十分不建议使用 `silent()` 处理器为默认处理器!
This commit is contained in:
2022-04-09 01:22:23 +08:00
parent f00e741035
commit 0a6c8ae1a9
25 changed files with 216 additions and 363 deletions
+3 -3
View File
@@ -5,13 +5,13 @@
<parent>
<groupId>cc.carm.lib</groupId>
<artifactId>easysql-parent</artifactId>
<version>0.3.8</version>
<version>0.3.9</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
</properties>
@@ -3,7 +3,6 @@ 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.defaults.DefaultSQLExceptionHandler;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -189,13 +188,14 @@ public interface SQLAction<T> {
}
/**
* 默认异常处理器
* 获取管理器提供的默认异常处理器
* 若未使用过 {@link #setExceptionHandler(SQLExceptionHandler)} 方法,
* 则默认返回 {@link SQLExceptionHandler#detailed(Logger)} 。
*
* @return {@link DefaultSQLExceptionHandler#get(Logger)}
* @see DefaultSQLExceptionHandler
* @return {@link SQLExceptionHandler}
*/
default SQLExceptionHandler defaultExceptionHandler() {
return DefaultSQLExceptionHandler.get(getManager().getLogger());
return getManager().getExceptionHandler();
}
/**
@@ -206,7 +206,7 @@ public interface SQLAction<T> {
* @param handler 异常处理器
*/
default void setExceptionHandler(@Nullable SQLExceptionHandler handler) {
DefaultSQLExceptionHandler.setCustomHandler(handler);
getManager().setExceptionHandler(handler);
}
}
@@ -5,6 +5,7 @@ import cc.carm.lib.easysql.api.action.PreparedSQLUpdateBatchAction;
import cc.carm.lib.easysql.api.action.SQLUpdateAction;
import cc.carm.lib.easysql.api.action.SQLUpdateBatchAction;
import cc.carm.lib.easysql.api.builder.*;
import cc.carm.lib.easysql.api.function.SQLExceptionHandler;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -56,6 +57,24 @@ public interface SQLManager {
*/
@NotNull Map<UUID, SQLQuery> getActiveQuery();
/**
* 获取改管理器提供的默认异常处理器。
* 若未使用过 {@link #setExceptionHandler(SQLExceptionHandler)} 方法,
* 则默认返回 {@link SQLExceptionHandler#detailed(Logger)} 。
*
* @return {@link SQLExceptionHandler}
*/
@NotNull SQLExceptionHandler getExceptionHandler();
/**
* 设定通用的异常处理器。
* <br> 在使用 {@link SQLAction#execute(SQLExceptionHandler)} 等相关方法时,若传入的处理器为null,则会采用此处理器。
* <br> 若该方法传入参数为 null,则会使用 {@link SQLExceptionHandler#detailed(Logger)} 。
*
* @param handler 异常处理器
*/
void setExceptionHandler(@Nullable SQLExceptionHandler handler);
/**
* 执行一条不需要返回结果的SQL语句(多用于UPDATE、REPLACE、DELETE方法)
* 该方法使用 Statement 实现,请注意SQL注入风险!
@@ -1,12 +1,51 @@
package cc.carm.lib.easysql.api.function;
import cc.carm.lib.easysql.api.SQLAction;
import cc.carm.lib.easysql.api.action.SQLUpdateBatchAction;
import java.sql.SQLException;
import java.util.function.BiConsumer;
import java.util.logging.Logger;
/**
* 异常处理器。
* <br> 在使用 {@link SQLAction#execute(SQLExceptionHandler)} 等相关方法时,
* 如果发生异常,则会调用错误处理器进行错误内容的输出提示。
*/
@FunctionalInterface
public interface SQLExceptionHandler extends BiConsumer<SQLException, SQLAction<?>> {
/**
* 默认的异常处理器,将详细的输出相关错误与错误来源。
*
* @param logger 用于输出错误信息的Logger。
* @return 输出详细信息的错误处理器。
*/
static SQLExceptionHandler detailed(Logger logger) {
return (exception, sqlAction) -> {
if (sqlAction instanceof SQLUpdateBatchAction) {
logger.severe("Error when execute SQLs : ");
int i = 1;
for (String content : ((SQLUpdateBatchAction) sqlAction).getSQLContents()) {
logger.severe(String.format("#%d {%s}", i, content));
i++;
}
} else {
logger.severe("Error when execute { " + sqlAction.getSQLContent() + " }");
}
exception.printStackTrace();
};
}
/**
* “安静“ 的错误处理器,发生错误什么都不做。
* 强烈不建议把此处理器作为默认处理器使用!
*
* @return 无输出的处理器。
*/
static SQLExceptionHandler silent() {
return (exception, sqlAction) -> {
};
}
}
@@ -1,56 +0,0 @@
package cc.carm.lib.easysql.api.function.defaults;
import cc.carm.lib.easysql.api.SQLAction;
import cc.carm.lib.easysql.api.action.SQLUpdateBatchAction;
import cc.carm.lib.easysql.api.function.SQLExceptionHandler;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.sql.SQLException;
import java.util.logging.Logger;
public class DefaultSQLExceptionHandler implements SQLExceptionHandler {
private static @Nullable SQLExceptionHandler customDefaultHandler = null;
private final Logger logger;
public DefaultSQLExceptionHandler(Logger logger) {
this.logger = logger;
}
public static @Nullable SQLExceptionHandler getCustomHandler() {
return customDefaultHandler;
}
public static void setCustomHandler(@Nullable SQLExceptionHandler handler) {
DefaultSQLExceptionHandler.customDefaultHandler = handler;
}
public static @NotNull SQLExceptionHandler get(Logger logger) {
if (getCustomHandler() != null) return getCustomHandler();
else return new DefaultSQLExceptionHandler(logger);
}
protected Logger getLogger() {
return logger;
}
@Override
public void accept(SQLException exception, SQLAction<?> sqlAction) {
if (sqlAction instanceof SQLUpdateBatchAction) {
getLogger().severe("Error when execute SQLs : ");
int i = 1;
for (String content : ((SQLUpdateBatchAction) sqlAction).getSQLContents()) {
getLogger().severe(String.format("#%d {%s}", i, content));
i++;
}
} else {
getLogger().severe("Error when execute { " + sqlAction.getSQLContent() + " }");
}
exception.printStackTrace();
}
}