From 0a6c8ae1a96424d5f029e2f752dacec1fad77c93 Mon Sep 17 00:00:00 2001 From: CarmJos Date: Sat, 9 Apr 2022 01:22:23 +0800 Subject: [PATCH] =?UTF-8?q?[0.3.9]=20=E7=89=88=E6=9C=AC=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=20-=20`[R]`=20=E4=BF=AE=E6=94=B9=E9=A1=B9=E7=9B=AE=E7=BB=93?= =?UTF-8?q?=E6=9E=84=EF=BC=8C=E7=A7=BB=E9=99=A4=E6=97=A0=E7=94=A8=E7=9A=84?= =?UTF-8?q?=20`easysql-tester`=20=E6=A8=A1=E5=9D=97,=20=E6=95=B4=E5=90=88?= =?UTF-8?q?=E7=9B=B8=E5=85=B3=E6=B5=8B=E8=AF=95=E5=88=B0=20`easysql-demo`?= =?UTF-8?q?=20=E7=9A=84`src/test`=20=E4=B8=8B=E3=80=82=20-=20`[U]`=20?= =?UTF-8?q?=E7=A7=BB=E9=99=A4`DefaultSQLExceptionHandler`=20=E7=B1=BB?= =?UTF-8?q?=EF=BC=8C=E4=B8=8E=20`SQLExceptionHandler`=20=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E4=B8=8B=E6=B7=BB=E5=8A=A0=20`detailed()`=20=E4=B8=8E=20`silen?= =?UTF-8?q?t()`=20=E4=B8=A4=E7=A7=8D=E9=A2=84=E8=AE=BE=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E5=A4=84=E7=90=86=E5=99=A8=EF=BC=8C=E5=B9=B6=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E9=80=9A=E8=BF=87=20`SQLManager#setExceptionHandler()`=20?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E5=BA=94=E7=94=A8=E5=85=A8=E5=B1=80=E7=94=9F?= =?UTF-8?q?=E6=95=88=E7=9A=84=E9=BB=98=E8=AE=A4=E9=94=99=E8=AF=AF=E5=A4=84?= =?UTF-8?q?=E7=90=86=E5=99=A8=E3=80=82=20>=20=E6=B3=A8=E6=84=8F=EF=BC=9A?= =?UTF-8?q?=20=E5=8D=81=E5=88=86=E4=B8=8D=E5=BB=BA=E8=AE=AE=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=20`silent()`=20=E5=A4=84=E7=90=86=E5=99=A8=E4=B8=BA?= =?UTF-8?q?=E9=BB=98=E8=AE=A4=E5=A4=84=E7=90=86=E5=99=A8=EF=BC=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/pom.xml | 6 +- .../cc/carm/lib/easysql/api/SQLAction.java | 12 +- .../cc/carm/lib/easysql/api/SQLManager.java | 19 +++ .../api/function/SQLExceptionHandler.java | 39 +++++ .../defaults/DefaultSQLExceptionHandler.java | 56 -------- example/demo/pom.xml | 24 +++- .../java/cc/carm/lib/easysql/EasySQLTest.java | 75 ++++++++++ .../cc/carm/lib/easysql/TestHandler.java} | 6 +- .../carm/lib/easysql}/tests/DeleteTest.java | 6 +- .../lib/easysql}/tests/QueryCloseTest.java | 6 +- .../lib/easysql}/tests/QueryFunctionTest.java | 6 +- .../lib/easysql}/tests/QueryNotCloseTest.java | 6 +- .../easysql}/tests/SQLUpdateBatchTests.java | 6 +- .../tests/SQLUpdateReturnKeysTest.java | 2 +- .../lib/easysql}/tests/TableAlterTest.java | 6 +- .../lib/easysql}/tests/TableCreateTest.java | 7 +- .../lib/easysql}/tests/TableRenameTest.java | 6 +- example/tester/pom.xml | 135 ------------------ .../java/cc/carm/lib/easysql/tester/Main.java | 93 ------------ example/tester/src/main/resources/log4j2.xml | 26 ---- impl/pom.xml | 6 +- .../lib/easysql/manager/SQLManagerImpl.java | 16 +++ pom.xml | 3 +- with-pool/beecp/pom.xml | 6 +- with-pool/hikaricp/pom.xml | 6 +- 25 files changed, 216 insertions(+), 363 deletions(-) delete mode 100644 api/src/main/java/cc/carm/lib/easysql/api/function/defaults/DefaultSQLExceptionHandler.java create mode 100644 example/demo/src/test/java/cc/carm/lib/easysql/EasySQLTest.java rename example/{tester/src/main/java/cc/carm/lib/easysql/tester/EasySQLTest.java => demo/src/test/java/cc/carm/lib/easysql/TestHandler.java} (90%) rename example/{tester/src/main/java/cc/carm/lib/easysql/tester => demo/src/test/java/cc/carm/lib/easysql}/tests/DeleteTest.java (75%) rename example/{tester/src/main/java/cc/carm/lib/easysql/tester => demo/src/test/java/cc/carm/lib/easysql}/tests/QueryCloseTest.java (84%) rename example/{tester/src/main/java/cc/carm/lib/easysql/tester => demo/src/test/java/cc/carm/lib/easysql}/tests/QueryFunctionTest.java (80%) rename example/{tester/src/main/java/cc/carm/lib/easysql/tester => demo/src/test/java/cc/carm/lib/easysql}/tests/QueryNotCloseTest.java (82%) rename example/{tester/src/main/java/cc/carm/lib/easysql/tester => demo/src/test/java/cc/carm/lib/easysql}/tests/SQLUpdateBatchTests.java (86%) rename example/{tester/src/main/java/cc/carm/lib/easysql/tester => demo/src/test/java/cc/carm/lib/easysql}/tests/SQLUpdateReturnKeysTest.java (93%) rename example/{tester/src/main/java/cc/carm/lib/easysql/tester => demo/src/test/java/cc/carm/lib/easysql}/tests/TableAlterTest.java (86%) rename example/{tester/src/main/java/cc/carm/lib/easysql/tester => demo/src/test/java/cc/carm/lib/easysql}/tests/TableCreateTest.java (84%) rename example/{tester/src/main/java/cc/carm/lib/easysql/tester => demo/src/test/java/cc/carm/lib/easysql}/tests/TableRenameTest.java (70%) delete mode 100644 example/tester/pom.xml delete mode 100644 example/tester/src/main/java/cc/carm/lib/easysql/tester/Main.java delete mode 100644 example/tester/src/main/resources/log4j2.xml diff --git a/api/pom.xml b/api/pom.xml index 7273d8d..2c0e8f5 100644 --- a/api/pom.xml +++ b/api/pom.xml @@ -5,13 +5,13 @@ cc.carm.lib easysql-parent - 0.3.8 + 0.3.9 4.0.0 - 11 - 11 + ${java.version} + ${java.version} UTF-8 UTF-8 diff --git a/api/src/main/java/cc/carm/lib/easysql/api/SQLAction.java b/api/src/main/java/cc/carm/lib/easysql/api/SQLAction.java index 83accbc..5ff1896 100644 --- a/api/src/main/java/cc/carm/lib/easysql/api/SQLAction.java +++ b/api/src/main/java/cc/carm/lib/easysql/api/SQLAction.java @@ -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 { } /** - * 默认的异常处理器 + * 获取管理器提供的默认异常处理器。 + * 若未使用过 {@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 { * @param handler 异常处理器 */ default void setExceptionHandler(@Nullable SQLExceptionHandler handler) { - DefaultSQLExceptionHandler.setCustomHandler(handler); + getManager().setExceptionHandler(handler); } } diff --git a/api/src/main/java/cc/carm/lib/easysql/api/SQLManager.java b/api/src/main/java/cc/carm/lib/easysql/api/SQLManager.java index fc32956..feba02a 100644 --- a/api/src/main/java/cc/carm/lib/easysql/api/SQLManager.java +++ b/api/src/main/java/cc/carm/lib/easysql/api/SQLManager.java @@ -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 getActiveQuery(); + /** + * 获取改管理器提供的默认异常处理器。 + * 若未使用过 {@link #setExceptionHandler(SQLExceptionHandler)} 方法, + * 则默认返回 {@link SQLExceptionHandler#detailed(Logger)} 。 + * + * @return {@link SQLExceptionHandler} + */ + @NotNull SQLExceptionHandler getExceptionHandler(); + + /** + * 设定通用的异常处理器。 + *
在使用 {@link SQLAction#execute(SQLExceptionHandler)} 等相关方法时,若传入的处理器为null,则会采用此处理器。 + *
若该方法传入参数为 null,则会使用 {@link SQLExceptionHandler#detailed(Logger)} 。 + * + * @param handler 异常处理器 + */ + void setExceptionHandler(@Nullable SQLExceptionHandler handler); + /** * 执行一条不需要返回结果的SQL语句(多用于UPDATE、REPLACE、DELETE方法) * 该方法使用 Statement 实现,请注意SQL注入风险! diff --git a/api/src/main/java/cc/carm/lib/easysql/api/function/SQLExceptionHandler.java b/api/src/main/java/cc/carm/lib/easysql/api/function/SQLExceptionHandler.java index 4f15a83..1c6583a 100644 --- a/api/src/main/java/cc/carm/lib/easysql/api/function/SQLExceptionHandler.java +++ b/api/src/main/java/cc/carm/lib/easysql/api/function/SQLExceptionHandler.java @@ -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; +/** + * 异常处理器。 + *
在使用 {@link SQLAction#execute(SQLExceptionHandler)} 等相关方法时, + * 如果发生异常,则会调用错误处理器进行错误内容的输出提示。 + */ @FunctionalInterface public interface SQLExceptionHandler extends BiConsumer> { + /** + * 默认的异常处理器,将详细的输出相关错误与错误来源。 + * + * @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) -> { + }; + } } diff --git a/api/src/main/java/cc/carm/lib/easysql/api/function/defaults/DefaultSQLExceptionHandler.java b/api/src/main/java/cc/carm/lib/easysql/api/function/defaults/DefaultSQLExceptionHandler.java deleted file mode 100644 index eb5fa1d..0000000 --- a/api/src/main/java/cc/carm/lib/easysql/api/function/defaults/DefaultSQLExceptionHandler.java +++ /dev/null @@ -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(); - } - - -} diff --git a/example/demo/pom.xml b/example/demo/pom.xml index 558a2c2..26ea0d0 100644 --- a/example/demo/pom.xml +++ b/example/demo/pom.xml @@ -5,14 +5,14 @@ easysql-parent cc.carm.lib - 0.3.8 + 0.3.9 ../../pom.xml 4.0.0 - + - 11 - 11 + ${java.version} + ${java.version} UTF-8 UTF-8 true @@ -64,6 +64,22 @@ compile + + + com.zaxxer + HikariCP + 4.0.3 + test + + + + + com.h2database + h2 + 2.1.210 + test + + diff --git a/example/demo/src/test/java/cc/carm/lib/easysql/EasySQLTest.java b/example/demo/src/test/java/cc/carm/lib/easysql/EasySQLTest.java new file mode 100644 index 0000000..420fcec --- /dev/null +++ b/example/demo/src/test/java/cc/carm/lib/easysql/EasySQLTest.java @@ -0,0 +1,75 @@ +package cc.carm.lib.easysql; + +import cc.carm.lib.easysql.api.SQLManager; +import cc.carm.lib.easysql.manager.SQLManagerImpl; +import cc.carm.lib.easysql.tests.*; +import com.zaxxer.hikari.HikariConfig; +import com.zaxxer.hikari.HikariDataSource; +import org.jetbrains.annotations.NotNull; +import org.junit.Test; + +import java.util.LinkedHashSet; +import java.util.Set; + +public class EasySQLTest { + + + @Test + public void onTest() { + + HikariConfig config = new HikariConfig(); + config.setDriverClassName("org.h2.Driver"); + config.setJdbcUrl("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;MODE=MYSQL;"); + + SQLManager sqlManager = new SQLManagerImpl(new HikariDataSource(config), "test"); + + print("加载测试类..."); + Set tests = new LinkedHashSet<>(); + tests.add(new TableCreateTest()); +// tests.add(new TableAlterTest()); +// tests.add(new TableRenameTest()); +// tests.add(new QueryNotCloseTest()); + tests.add(new QueryCloseTest()); + tests.add(new SQLUpdateBatchTests()); + tests.add(new SQLUpdateReturnKeysTest()); + tests.add(new QueryFunctionTest()); + tests.add(new DeleteTest()); + + print("准备进行测试..."); + + int index = 1; + int success = 0; + + for (TestHandler currentTest : tests) { + print("-------------------------------------------------"); + if (currentTest.executeTest(index, sqlManager)) { + success++; + } + + index++; + try { + Thread.sleep(1000L); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + } + + print(" "); + print("全部测试执行完毕,成功 %s 个,失败 %s 个。", + success, (tests.size() - success) + ); + + if (sqlManager.getDataSource() instanceof HikariDataSource) { + //Close bee connection pool + ((HikariDataSource) sqlManager.getDataSource()).close(); + } + + } + + + public static void print(@NotNull String format, Object... params) { + System.out.printf((format) + "%n", params); + } + +} diff --git a/example/tester/src/main/java/cc/carm/lib/easysql/tester/EasySQLTest.java b/example/demo/src/test/java/cc/carm/lib/easysql/TestHandler.java similarity index 90% rename from example/tester/src/main/java/cc/carm/lib/easysql/tester/EasySQLTest.java rename to example/demo/src/test/java/cc/carm/lib/easysql/TestHandler.java index 540e645..eaba2ad 100644 --- a/example/tester/src/main/java/cc/carm/lib/easysql/tester/EasySQLTest.java +++ b/example/demo/src/test/java/cc/carm/lib/easysql/TestHandler.java @@ -1,4 +1,4 @@ -package cc.carm.lib.easysql.tester; +package cc.carm.lib.easysql; import cc.carm.lib.easysql.api.SQLManager; import org.jetbrains.annotations.ApiStatus; @@ -6,10 +6,10 @@ import org.jetbrains.annotations.NotNull; import java.sql.SQLException; -public abstract class EasySQLTest { +public abstract class TestHandler { protected static void print(@NotNull String format, Object... params) { - Main.print(format, params); + EasySQLTest.print(format, params); } @ApiStatus.OverrideOnly diff --git a/example/tester/src/main/java/cc/carm/lib/easysql/tester/tests/DeleteTest.java b/example/demo/src/test/java/cc/carm/lib/easysql/tests/DeleteTest.java similarity index 75% rename from example/tester/src/main/java/cc/carm/lib/easysql/tester/tests/DeleteTest.java rename to example/demo/src/test/java/cc/carm/lib/easysql/tests/DeleteTest.java index d1ab74e..36f232d 100644 --- a/example/tester/src/main/java/cc/carm/lib/easysql/tester/tests/DeleteTest.java +++ b/example/demo/src/test/java/cc/carm/lib/easysql/tests/DeleteTest.java @@ -1,11 +1,11 @@ -package cc.carm.lib.easysql.tester.tests; +package cc.carm.lib.easysql.tests; import cc.carm.lib.easysql.api.SQLManager; -import cc.carm.lib.easysql.tester.EasySQLTest; +import cc.carm.lib.easysql.TestHandler; import java.sql.SQLException; -public class DeleteTest extends EasySQLTest { +public class DeleteTest extends TestHandler { @Override diff --git a/example/tester/src/main/java/cc/carm/lib/easysql/tester/tests/QueryCloseTest.java b/example/demo/src/test/java/cc/carm/lib/easysql/tests/QueryCloseTest.java similarity index 84% rename from example/tester/src/main/java/cc/carm/lib/easysql/tester/tests/QueryCloseTest.java rename to example/demo/src/test/java/cc/carm/lib/easysql/tests/QueryCloseTest.java index 9c0a32e..78370fd 100644 --- a/example/tester/src/main/java/cc/carm/lib/easysql/tester/tests/QueryCloseTest.java +++ b/example/demo/src/test/java/cc/carm/lib/easysql/tests/QueryCloseTest.java @@ -1,13 +1,13 @@ -package cc.carm.lib.easysql.tester.tests; +package cc.carm.lib.easysql.tests; import cc.carm.lib.easysql.api.SQLManager; import cc.carm.lib.easysql.api.SQLQuery; -import cc.carm.lib.easysql.tester.EasySQLTest; +import cc.carm.lib.easysql.TestHandler; import java.sql.ResultSet; import java.sql.SQLException; -public class QueryCloseTest extends EasySQLTest { +public class QueryCloseTest extends TestHandler { @Override diff --git a/example/tester/src/main/java/cc/carm/lib/easysql/tester/tests/QueryFunctionTest.java b/example/demo/src/test/java/cc/carm/lib/easysql/tests/QueryFunctionTest.java similarity index 80% rename from example/tester/src/main/java/cc/carm/lib/easysql/tester/tests/QueryFunctionTest.java rename to example/demo/src/test/java/cc/carm/lib/easysql/tests/QueryFunctionTest.java index 5c64f01..6eca2db 100644 --- a/example/tester/src/main/java/cc/carm/lib/easysql/tester/tests/QueryFunctionTest.java +++ b/example/demo/src/test/java/cc/carm/lib/easysql/tests/QueryFunctionTest.java @@ -1,11 +1,11 @@ -package cc.carm.lib.easysql.tester.tests; +package cc.carm.lib.easysql.tests; +import cc.carm.lib.easysql.TestHandler; import cc.carm.lib.easysql.api.SQLManager; -import cc.carm.lib.easysql.tester.EasySQLTest; import java.sql.SQLException; -public class QueryFunctionTest extends EasySQLTest { +public class QueryFunctionTest extends TestHandler { @Override diff --git a/example/tester/src/main/java/cc/carm/lib/easysql/tester/tests/QueryNotCloseTest.java b/example/demo/src/test/java/cc/carm/lib/easysql/tests/QueryNotCloseTest.java similarity index 82% rename from example/tester/src/main/java/cc/carm/lib/easysql/tester/tests/QueryNotCloseTest.java rename to example/demo/src/test/java/cc/carm/lib/easysql/tests/QueryNotCloseTest.java index d057714..86a9a04 100644 --- a/example/tester/src/main/java/cc/carm/lib/easysql/tester/tests/QueryNotCloseTest.java +++ b/example/demo/src/test/java/cc/carm/lib/easysql/tests/QueryNotCloseTest.java @@ -1,13 +1,13 @@ -package cc.carm.lib.easysql.tester.tests; +package cc.carm.lib.easysql.tests; import cc.carm.lib.easysql.api.SQLManager; import cc.carm.lib.easysql.api.SQLQuery; -import cc.carm.lib.easysql.tester.EasySQLTest; +import cc.carm.lib.easysql.TestHandler; import java.sql.ResultSet; import java.sql.SQLException; -public class QueryNotCloseTest extends EasySQLTest { +public class QueryNotCloseTest extends TestHandler { @Override public void onTest(SQLManager sqlManager) throws SQLException { diff --git a/example/tester/src/main/java/cc/carm/lib/easysql/tester/tests/SQLUpdateBatchTests.java b/example/demo/src/test/java/cc/carm/lib/easysql/tests/SQLUpdateBatchTests.java similarity index 86% rename from example/tester/src/main/java/cc/carm/lib/easysql/tester/tests/SQLUpdateBatchTests.java rename to example/demo/src/test/java/cc/carm/lib/easysql/tests/SQLUpdateBatchTests.java index 7294dce..4aa8b89 100644 --- a/example/tester/src/main/java/cc/carm/lib/easysql/tester/tests/SQLUpdateBatchTests.java +++ b/example/demo/src/test/java/cc/carm/lib/easysql/tests/SQLUpdateBatchTests.java @@ -1,7 +1,7 @@ -package cc.carm.lib.easysql.tester.tests; +package cc.carm.lib.easysql.tests; import cc.carm.lib.easysql.api.SQLManager; -import cc.carm.lib.easysql.tester.EasySQLTest; +import cc.carm.lib.easysql.TestHandler; import java.sql.SQLException; import java.util.Arrays; @@ -10,7 +10,7 @@ import java.util.UUID; import java.util.stream.Collectors; import java.util.stream.IntStream; -public class SQLUpdateBatchTests extends EasySQLTest { +public class SQLUpdateBatchTests extends TestHandler { protected static List generateParams() { diff --git a/example/tester/src/main/java/cc/carm/lib/easysql/tester/tests/SQLUpdateReturnKeysTest.java b/example/demo/src/test/java/cc/carm/lib/easysql/tests/SQLUpdateReturnKeysTest.java similarity index 93% rename from example/tester/src/main/java/cc/carm/lib/easysql/tester/tests/SQLUpdateReturnKeysTest.java rename to example/demo/src/test/java/cc/carm/lib/easysql/tests/SQLUpdateReturnKeysTest.java index 67c7159..afea87a 100644 --- a/example/tester/src/main/java/cc/carm/lib/easysql/tester/tests/SQLUpdateReturnKeysTest.java +++ b/example/demo/src/test/java/cc/carm/lib/easysql/tests/SQLUpdateReturnKeysTest.java @@ -1,4 +1,4 @@ -package cc.carm.lib.easysql.tester.tests; +package cc.carm.lib.easysql.tests; import cc.carm.lib.easysql.api.SQLManager; diff --git a/example/tester/src/main/java/cc/carm/lib/easysql/tester/tests/TableAlterTest.java b/example/demo/src/test/java/cc/carm/lib/easysql/tests/TableAlterTest.java similarity index 86% rename from example/tester/src/main/java/cc/carm/lib/easysql/tester/tests/TableAlterTest.java rename to example/demo/src/test/java/cc/carm/lib/easysql/tests/TableAlterTest.java index 89dce3d..2ee9c23 100644 --- a/example/tester/src/main/java/cc/carm/lib/easysql/tester/tests/TableAlterTest.java +++ b/example/demo/src/test/java/cc/carm/lib/easysql/tests/TableAlterTest.java @@ -1,12 +1,12 @@ -package cc.carm.lib.easysql.tester.tests; +package cc.carm.lib.easysql.tests; import cc.carm.lib.easysql.api.SQLManager; import cc.carm.lib.easysql.api.enums.NumberType; -import cc.carm.lib.easysql.tester.EasySQLTest; +import cc.carm.lib.easysql.TestHandler; import java.sql.SQLException; -public class TableAlterTest extends EasySQLTest { +public class TableAlterTest extends TestHandler { @Override public void onTest(SQLManager sqlManager) throws SQLException { diff --git a/example/tester/src/main/java/cc/carm/lib/easysql/tester/tests/TableCreateTest.java b/example/demo/src/test/java/cc/carm/lib/easysql/tests/TableCreateTest.java similarity index 84% rename from example/tester/src/main/java/cc/carm/lib/easysql/tester/tests/TableCreateTest.java rename to example/demo/src/test/java/cc/carm/lib/easysql/tests/TableCreateTest.java index 6e86c88..9700983 100644 --- a/example/tester/src/main/java/cc/carm/lib/easysql/tester/tests/TableCreateTest.java +++ b/example/demo/src/test/java/cc/carm/lib/easysql/tests/TableCreateTest.java @@ -1,13 +1,13 @@ -package cc.carm.lib.easysql.tester.tests; +package cc.carm.lib.easysql.tests; import cc.carm.lib.easysql.api.SQLManager; import cc.carm.lib.easysql.api.enums.ForeignKeyRule; import cc.carm.lib.easysql.api.enums.IndexType; -import cc.carm.lib.easysql.tester.EasySQLTest; +import cc.carm.lib.easysql.TestHandler; import java.sql.SQLException; -public class TableCreateTest extends EasySQLTest { +public class TableCreateTest extends TestHandler { @Override public void onTest(SQLManager sqlManager) throws SQLException { @@ -30,7 +30,6 @@ public class TableCreateTest extends EasySQLTest { "test_user_table", "id", ForeignKeyRule.CASCADE, ForeignKeyRule.CASCADE ) - .setIndex(IndexType.FULLTEXT_INDEX, "sign", "info") .build().execute(); } diff --git a/example/tester/src/main/java/cc/carm/lib/easysql/tester/tests/TableRenameTest.java b/example/demo/src/test/java/cc/carm/lib/easysql/tests/TableRenameTest.java similarity index 70% rename from example/tester/src/main/java/cc/carm/lib/easysql/tester/tests/TableRenameTest.java rename to example/demo/src/test/java/cc/carm/lib/easysql/tests/TableRenameTest.java index 8c4441d..0f74a1b 100644 --- a/example/tester/src/main/java/cc/carm/lib/easysql/tester/tests/TableRenameTest.java +++ b/example/demo/src/test/java/cc/carm/lib/easysql/tests/TableRenameTest.java @@ -1,11 +1,11 @@ -package cc.carm.lib.easysql.tester.tests; +package cc.carm.lib.easysql.tests; import cc.carm.lib.easysql.api.SQLManager; -import cc.carm.lib.easysql.tester.EasySQLTest; +import cc.carm.lib.easysql.TestHandler; import java.sql.SQLException; -public class TableRenameTest extends EasySQLTest { +public class TableRenameTest extends TestHandler { @Override public void onTest(SQLManager sqlManager) throws SQLException { print(" 重命名 test_user_table"); diff --git a/example/tester/pom.xml b/example/tester/pom.xml deleted file mode 100644 index fc91259..0000000 --- a/example/tester/pom.xml +++ /dev/null @@ -1,135 +0,0 @@ - - - - easysql-parent - cc.carm.lib - 0.3.8 - ../../pom.xml - - 4.0.0 - - - 11 - 11 - UTF-8 - UTF-8 - true - true - - - easysql-tester - - EasySQL-Test - EasySQL的测试代码 - https://github.com/CarmJos/EasySQL - - - - ${project.parent.groupId} - easysql-beecp - ${project.version} - compile - - - - - mysql - mysql-connector-java - 8.0.28 - compile - - - - org.slf4j - slf4j-api - 1.7.36 - compile - - - - org.apache.logging.log4j - log4j-api - 2.17.2 - compile - - - - org.apache.logging.log4j - log4j-core - 2.17.2 - compile - - - - org.apache.logging.log4j - log4j-slf4j-impl - 2.17.2 - compile - - - - - - - - org.apache.maven.plugins - maven-compiler-plugin - - - org.apache.maven.plugins - maven-jar-plugin - - - org.apache.maven.plugins - maven-shade-plugin - 3.3.0 - - - package - - shade - - - - - - - *:* - - META-INF/MANIFEST.MF - - - - - true - - - - - maven-assembly-plugin - - - jar-with-dependencies - - - - cc.carm.lib.easysql.tester.Main - - - - - - make-assembly - package - - assembly - - - - - - - - \ No newline at end of file diff --git a/example/tester/src/main/java/cc/carm/lib/easysql/tester/Main.java b/example/tester/src/main/java/cc/carm/lib/easysql/tester/Main.java deleted file mode 100644 index 1651693..0000000 --- a/example/tester/src/main/java/cc/carm/lib/easysql/tester/Main.java +++ /dev/null @@ -1,93 +0,0 @@ -package cc.carm.lib.easysql.tester; - -import cc.carm.lib.easysql.EasySQL; -import cc.carm.lib.easysql.api.SQLManager; -import cc.carm.lib.easysql.tester.tests.DeleteTest; -import cc.carm.lib.easysql.tester.tests.QueryCloseTest; -import cc.carm.lib.easysql.tester.tests.QueryFunctionTest; -import cc.carm.lib.easysql.tester.tests.TableCreateTest; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.TestOnly; - -import java.util.Iterator; -import java.util.LinkedHashSet; -import java.util.Scanner; -import java.util.Set; - -@TestOnly -@SuppressWarnings("all") -public class Main { - - public static void main(String[] args) { - if (args.length < 4) { - print("请提供以下参数:<数据库驱动> <数据库地址(JDBC)> <数据库用户> <数据库密码> [是否采用DEBUG(y/n)]"); - return; - } - - SQLManager sqlManager; - try { - print("初始化 SQLManager..."); - print("数据库驱动 %s", args[0]); - print("数据库地址 %s", args[1]); - print("数据库用户 %s", args[2]); - print("数据库密码 %s", args[3]); - sqlManager = EasySQL.createManager(args[0], args[1], args[2], args[3]); - sqlManager.setDebugMode(args.length > 4); - print("SQLManager 初始化完成!"); - } catch (Exception exception) { - print("SQLManager 初始化失败,请检查数据库配置。"); - exception.printStackTrace(); - return; - } - - print("加载测试类..."); - Set tests = new LinkedHashSet<>(); - tests.add(new TableCreateTest()); -// tests.add(new TableAlterTest()); -// tests.add(new TableRenameTest()); -// tests.add(new QueryNotCloseTest()); - tests.add(new QueryCloseTest()); -// tests.add(new SQLUpdateBatchTests()); -// tests.add(new SQLUpdateReturnKeysTest()); - tests.add(new QueryFunctionTest()); - tests.add(new DeleteTest()); - - print("准备进行测试..."); - - int index = 1; - int success = 0; - - Iterator testIterator = tests.iterator(); - - print("准备完成,请按回车键开始执行测试。"); - - while (testIterator.hasNext()) { - Scanner scanner = new Scanner(System.in); - - if (scanner.nextLine() != null) { - - EasySQLTest currentTest = testIterator.next(); - - if (currentTest.executeTest(index, sqlManager)) { - success++; - } - - index++; - } - - } - - print(" "); - print("全部测试执行完毕,成功 %s 个,失败 %s 个。", - success, (tests.size() - success) - ); - - EasySQL.shutdownManager(sqlManager); - - } - - public static void print(@NotNull String format, Object... params) { - System.out.printf((format) + "%n", params); - } - -} diff --git a/example/tester/src/main/resources/log4j2.xml b/example/tester/src/main/resources/log4j2.xml deleted file mode 100644 index 9f357cf..0000000 --- a/example/tester/src/main/resources/log4j2.xml +++ /dev/null @@ -1,26 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/impl/pom.xml b/impl/pom.xml index a3e727f..9b26daa 100644 --- a/impl/pom.xml +++ b/impl/pom.xml @@ -5,13 +5,13 @@ easysql-parent cc.carm.lib - 0.3.8 + 0.3.9 4.0.0 - 11 - 11 + ${java.version} + ${java.version} UTF-8 UTF-8 diff --git a/impl/src/main/java/cc/carm/lib/easysql/manager/SQLManagerImpl.java b/impl/src/main/java/cc/carm/lib/easysql/manager/SQLManagerImpl.java index 448917c..be1aafa 100644 --- a/impl/src/main/java/cc/carm/lib/easysql/manager/SQLManagerImpl.java +++ b/impl/src/main/java/cc/carm/lib/easysql/manager/SQLManagerImpl.java @@ -10,6 +10,7 @@ import cc.carm.lib.easysql.api.action.PreparedSQLUpdateAction; import cc.carm.lib.easysql.api.action.PreparedSQLUpdateBatchAction; import cc.carm.lib.easysql.api.action.SQLUpdateBatchAction; import cc.carm.lib.easysql.api.builder.*; +import cc.carm.lib.easysql.api.function.SQLExceptionHandler; import cc.carm.lib.easysql.builder.impl.*; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -32,6 +33,8 @@ public class SQLManagerImpl implements SQLManager { protected ExecutorService executorPool; @NotNull Supplier debugMode = () -> Boolean.FALSE; + @NotNull SQLExceptionHandler exceptionHandler; + public SQLManagerImpl(@NotNull DataSource dataSource) { this(dataSource, null); } @@ -45,6 +48,8 @@ public class SQLManagerImpl implements SQLManager { thread.setDaemon(true); return thread; }); + + this.exceptionHandler = SQLExceptionHandler.detailed(getLogger()); } @Override @@ -85,6 +90,17 @@ public class SQLManagerImpl implements SQLManager { return this.activeQuery; } + @Override + public @NotNull SQLExceptionHandler getExceptionHandler() { + return this.exceptionHandler; + } + + @Override + public void setExceptionHandler(@Nullable SQLExceptionHandler handler) { + if (handler == null) this.exceptionHandler = SQLExceptionHandler.detailed(getLogger()); + else this.exceptionHandler = handler; + } + @Override public Integer executeSQL(String sql) { return new SQLUpdateActionImpl(this, sql).execute(null); diff --git a/pom.xml b/pom.xml index 312e5a1..f01f8be 100644 --- a/pom.xml +++ b/pom.xml @@ -17,7 +17,7 @@ cc.carm.lib easysql-parent pom - 0.3.8 + 0.3.9 api @@ -27,7 +27,6 @@ with-pool/hikaricp example/demo - example/tester EasySQL diff --git a/with-pool/beecp/pom.xml b/with-pool/beecp/pom.xml index 8c47a6a..61507d4 100644 --- a/with-pool/beecp/pom.xml +++ b/with-pool/beecp/pom.xml @@ -5,14 +5,14 @@ easysql-parent cc.carm.lib - 0.3.8 + 0.3.9 ../../pom.xml 4.0.0 - 11 - 11 + ${java.version} + ${java.version} UTF-8 UTF-8 diff --git a/with-pool/hikaricp/pom.xml b/with-pool/hikaricp/pom.xml index d030454..0372f16 100644 --- a/with-pool/hikaricp/pom.xml +++ b/with-pool/hikaricp/pom.xml @@ -5,14 +5,14 @@ easysql-parent cc.carm.lib - 0.3.8 + 0.3.9 ../../pom.xml 4.0.0 - 11 - 11 + ${java.version} + ${java.version} UTF-8 UTF-8