From 26ab19ec75de9c2cfb0dbcf3f753be769a3beb82 Mon Sep 17 00:00:00 2001 From: carm Date: Fri, 5 Aug 2022 17:55:44 +0800 Subject: [PATCH] =?UTF-8?q?feat(execute):=20=E6=8F=90=E4=BE=9BFuture?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E7=9A=84=E6=93=8D=E4=BD=9C=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/pom.xml | 2 +- .../cc/carm/lib/easysql/api/SQLAction.java | 3 ++ demo/pom.xml | 2 +- .../java/cc/carm/lib/easysql/EasySQLTest.java | 1 + .../java/cc/carm/lib/easysql/TestHandler.java | 3 +- .../lib/easysql/tests/QueryFutureTest.java | 33 +++++++++++++++++++ impl/pom.xml | 2 +- .../lib/easysql/action/AbstractSQLAction.java | 9 +++++ pom.xml | 2 +- with-pool/beecp/pom.xml | 2 +- with-pool/hikaricp/pom.xml | 2 +- 11 files changed, 54 insertions(+), 7 deletions(-) create mode 100644 demo/src/test/java/cc/carm/lib/easysql/tests/QueryFutureTest.java diff --git a/api/pom.xml b/api/pom.xml index ac3edcd1..5817a53c 100644 --- a/api/pom.xml +++ b/api/pom.xml @@ -5,7 +5,7 @@ cc.carm.lib easysql-parent - 0.4.3 + 0.4.4 4.0.0 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 fe12acc7..7cbc64c4 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 @@ -12,6 +12,7 @@ import java.util.Collections; import java.util.List; import java.util.UUID; +import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; /** @@ -206,6 +207,8 @@ default void executeAsync(@Nullable SQLHandler success) { void executeAsync(@Nullable SQLHandler success, @Nullable SQLExceptionHandler failure); + @NotNull Future executeFuture(@NotNull SQLFunction handler); + default void handleException(@Nullable SQLExceptionHandler handler, SQLException exception) { if (handler == null) handler = defaultExceptionHandler(); handler.accept(exception, this); diff --git a/demo/pom.xml b/demo/pom.xml index 0883478d..36f8f223 100644 --- a/demo/pom.xml +++ b/demo/pom.xml @@ -5,7 +5,7 @@ easysql-parent cc.carm.lib - 0.4.3 + 0.4.4 4.0.0 diff --git a/demo/src/test/java/cc/carm/lib/easysql/EasySQLTest.java b/demo/src/test/java/cc/carm/lib/easysql/EasySQLTest.java index b5821d02..b361946e 100644 --- a/demo/src/test/java/cc/carm/lib/easysql/EasySQLTest.java +++ b/demo/src/test/java/cc/carm/lib/easysql/EasySQLTest.java @@ -50,6 +50,7 @@ public void onTest() { tests.add(new SQLUpdateReturnKeysTest()); tests.add(new QueryCloseTest()); tests.add(new QueryFunctionTest()); + tests.add(new QueryFutureTest()); tests.add(new QueryAsyncTest()); // tests.add(new DeleteTest()); diff --git a/demo/src/test/java/cc/carm/lib/easysql/TestHandler.java b/demo/src/test/java/cc/carm/lib/easysql/TestHandler.java index eaba2add..d6480888 100644 --- a/demo/src/test/java/cc/carm/lib/easysql/TestHandler.java +++ b/demo/src/test/java/cc/carm/lib/easysql/TestHandler.java @@ -5,6 +5,7 @@ import org.jetbrains.annotations.NotNull; import java.sql.SQLException; +import java.util.concurrent.ExecutionException; public abstract class TestHandler { @@ -13,7 +14,7 @@ protected static void print(@NotNull String format, Object... params) { } @ApiStatus.OverrideOnly - public abstract void onTest(SQLManager sqlManager) throws SQLException; + public abstract void onTest(SQLManager sqlManager) throws SQLException, ExecutionException, InterruptedException; public boolean executeTest(int index, SQLManager sqlManager) { String testName = getClass().getSimpleName(); diff --git a/demo/src/test/java/cc/carm/lib/easysql/tests/QueryFutureTest.java b/demo/src/test/java/cc/carm/lib/easysql/tests/QueryFutureTest.java new file mode 100644 index 00000000..164ea880 --- /dev/null +++ b/demo/src/test/java/cc/carm/lib/easysql/tests/QueryFutureTest.java @@ -0,0 +1,33 @@ +package cc.carm.lib.easysql.tests; + +import cc.carm.lib.easysql.TestHandler; +import cc.carm.lib.easysql.api.SQLManager; + +import java.sql.SQLException; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Future; + +public class QueryFutureTest extends TestHandler { + + + @Override + public void onTest(SQLManager sqlManager) throws SQLException, ExecutionException, InterruptedException { + + + Future future = sqlManager.createQuery() + .inTable("test_user_table") + .orderBy("id", false) + .setLimit(1) + .build().executeFuture((query) -> { + if (!query.getResultSet().next()) { + return -1; + } else { + return query.getResultSet().getInt("id"); + } + }); + + int id = future.get(); + System.out.println("id(future): " + id); + + } +} diff --git a/impl/pom.xml b/impl/pom.xml index 5a847132..660d0fdf 100644 --- a/impl/pom.xml +++ b/impl/pom.xml @@ -5,7 +5,7 @@ easysql-parent cc.carm.lib - 0.4.3 + 0.4.4 4.0.0 diff --git a/impl/src/main/java/cc/carm/lib/easysql/action/AbstractSQLAction.java b/impl/src/main/java/cc/carm/lib/easysql/action/AbstractSQLAction.java index aa32f9b4..9a930161 100644 --- a/impl/src/main/java/cc/carm/lib/easysql/action/AbstractSQLAction.java +++ b/impl/src/main/java/cc/carm/lib/easysql/action/AbstractSQLAction.java @@ -2,6 +2,7 @@ import cc.carm.lib.easysql.api.SQLAction; 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.manager.SQLManagerImpl; import org.jetbrains.annotations.NotNull; @@ -10,6 +11,8 @@ import java.util.List; import java.util.Objects; import java.util.UUID; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; public abstract class AbstractSQLAction implements SQLAction { @@ -91,4 +94,10 @@ public void executeAsync(SQLHandler success, SQLExceptionHandler failure) { }); } + @Override + public @NotNull Future executeFuture(@NotNull SQLFunction handler) { + CompletableFuture future = new CompletableFuture<>(); + executeAsync((t -> future.complete(handler.apply(t))), (e, q) -> future.completeExceptionally(e)); + return future; + } } diff --git a/pom.xml b/pom.xml index 7056978d..c4390d49 100644 --- a/pom.xml +++ b/pom.xml @@ -19,7 +19,7 @@ cc.carm.lib easysql-parent pom - 0.4.3 + 0.4.4 api diff --git a/with-pool/beecp/pom.xml b/with-pool/beecp/pom.xml index dfe001bb..9681d330 100644 --- a/with-pool/beecp/pom.xml +++ b/with-pool/beecp/pom.xml @@ -5,7 +5,7 @@ easysql-parent cc.carm.lib - 0.4.3 + 0.4.4 ../../pom.xml 4.0.0 diff --git a/with-pool/hikaricp/pom.xml b/with-pool/hikaricp/pom.xml index fc750967..83205f4a 100644 --- a/with-pool/hikaricp/pom.xml +++ b/with-pool/hikaricp/pom.xml @@ -5,7 +5,7 @@ easysql-parent cc.carm.lib - 0.4.3 + 0.4.4 ../../pom.xml 4.0.0