diff --git a/api/pom.xml b/api/pom.xml index 3901d657..7462461c 100644 --- a/api/pom.xml +++ b/api/pom.xml @@ -5,7 +5,7 @@ com.ghostchu.fork.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 2b667854..6f892fd9 100644 --- a/demo/pom.xml +++ b/demo/pom.xml @@ -5,7 +5,7 @@ easysql-parent com.ghostchu.fork.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 29772f31..4b15a1eb 100644 --- a/impl/pom.xml +++ b/impl/pom.xml @@ -5,7 +5,7 @@ easysql-parent com.ghostchu.fork.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 4f87f7aa..733a8855 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 { @@ -94,4 +97,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 c434ca77..2eb32937 100644 --- a/pom.xml +++ b/pom.xml @@ -19,7 +19,7 @@ com.ghostchu.fork.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 07441707..31cb823a 100644 --- a/with-pool/beecp/pom.xml +++ b/with-pool/beecp/pom.xml @@ -5,7 +5,7 @@ easysql-parent com.ghostchu.fork.cc.carm.lib - 0.4.3 + 0.4.4 ../../pom.xml 4.0.0 @@ -79,7 +79,7 @@ com.github.chris2018998 beecp - 3.3.7 + 3.3.8 true compile diff --git a/with-pool/hikaricp/pom.xml b/with-pool/hikaricp/pom.xml index 686e4fe0..d372d739 100644 --- a/with-pool/hikaricp/pom.xml +++ b/with-pool/hikaricp/pom.xml @@ -5,7 +5,7 @@ easysql-parent com.ghostchu.fork.cc.carm.lib - 0.4.3 + 0.4.4 ../../pom.xml 4.0.0