1
mirror of https://github.com/CarmJos/EasySQL.git synced 2024-09-19 21:35:47 +00:00

feat(execute): 提供Future类型的操作支持。

This commit is contained in:
Carm Jos 2022-08-05 17:55:44 +08:00
parent 198a800196
commit 26ab19ec75
11 changed files with 54 additions and 7 deletions

View File

@ -5,7 +5,7 @@
<parent> <parent>
<groupId>cc.carm.lib</groupId> <groupId>cc.carm.lib</groupId>
<artifactId>easysql-parent</artifactId> <artifactId>easysql-parent</artifactId>
<version>0.4.3</version> <version>0.4.4</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@ -12,6 +12,7 @@ import java.sql.SQLException;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
/** /**
@ -206,6 +207,8 @@ public interface SQLAction<T> {
void executeAsync(@Nullable SQLHandler<T> success, void executeAsync(@Nullable SQLHandler<T> success,
@Nullable SQLExceptionHandler failure); @Nullable SQLExceptionHandler failure);
<R> @NotNull Future<R> executeFuture(@NotNull SQLFunction<T, R> handler);
default void handleException(@Nullable SQLExceptionHandler handler, SQLException exception) { default void handleException(@Nullable SQLExceptionHandler handler, SQLException exception) {
if (handler == null) handler = defaultExceptionHandler(); if (handler == null) handler = defaultExceptionHandler();
handler.accept(exception, this); handler.accept(exception, this);

View File

@ -5,7 +5,7 @@
<parent> <parent>
<artifactId>easysql-parent</artifactId> <artifactId>easysql-parent</artifactId>
<groupId>cc.carm.lib</groupId> <groupId>cc.carm.lib</groupId>
<version>0.4.3</version> <version>0.4.4</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@ -50,6 +50,7 @@ public class EasySQLTest {
tests.add(new SQLUpdateReturnKeysTest()); tests.add(new SQLUpdateReturnKeysTest());
tests.add(new QueryCloseTest()); tests.add(new QueryCloseTest());
tests.add(new QueryFunctionTest()); tests.add(new QueryFunctionTest());
tests.add(new QueryFutureTest());
tests.add(new QueryAsyncTest()); tests.add(new QueryAsyncTest());
// tests.add(new DeleteTest()); // tests.add(new DeleteTest());

View File

@ -5,6 +5,7 @@ import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.concurrent.ExecutionException;
public abstract class TestHandler { public abstract class TestHandler {
@ -13,7 +14,7 @@ public abstract class TestHandler {
} }
@ApiStatus.OverrideOnly @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) { public boolean executeTest(int index, SQLManager sqlManager) {
String testName = getClass().getSimpleName(); String testName = getClass().getSimpleName();

View File

@ -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<Integer> 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);
}
}

View File

@ -5,7 +5,7 @@
<parent> <parent>
<artifactId>easysql-parent</artifactId> <artifactId>easysql-parent</artifactId>
<groupId>cc.carm.lib</groupId> <groupId>cc.carm.lib</groupId>
<version>0.4.3</version> <version>0.4.4</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@ -2,6 +2,7 @@ package cc.carm.lib.easysql.action;
import cc.carm.lib.easysql.api.SQLAction; import cc.carm.lib.easysql.api.SQLAction;
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.SQLHandler; import cc.carm.lib.easysql.api.function.SQLHandler;
import cc.carm.lib.easysql.manager.SQLManagerImpl; import cc.carm.lib.easysql.manager.SQLManagerImpl;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -10,6 +11,8 @@ import java.sql.SQLException;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
public abstract class AbstractSQLAction<T> implements SQLAction<T> { public abstract class AbstractSQLAction<T> implements SQLAction<T> {
@ -91,4 +94,10 @@ public abstract class AbstractSQLAction<T> implements SQLAction<T> {
}); });
} }
@Override
public @NotNull <R> Future<R> executeFuture(@NotNull SQLFunction<T, R> handler) {
CompletableFuture<R> future = new CompletableFuture<>();
executeAsync((t -> future.complete(handler.apply(t))), (e, q) -> future.completeExceptionally(e));
return future;
}
} }

View File

@ -19,7 +19,7 @@
<groupId>cc.carm.lib</groupId> <groupId>cc.carm.lib</groupId>
<artifactId>easysql-parent</artifactId> <artifactId>easysql-parent</artifactId>
<packaging>pom</packaging> <packaging>pom</packaging>
<version>0.4.3</version> <version>0.4.4</version>
<modules> <modules>
<module>api</module> <module>api</module>

View File

@ -5,7 +5,7 @@
<parent> <parent>
<artifactId>easysql-parent</artifactId> <artifactId>easysql-parent</artifactId>
<groupId>cc.carm.lib</groupId> <groupId>cc.carm.lib</groupId>
<version>0.4.3</version> <version>0.4.4</version>
<relativePath>../../pom.xml</relativePath> <relativePath>../../pom.xml</relativePath>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent> <parent>
<artifactId>easysql-parent</artifactId> <artifactId>easysql-parent</artifactId>
<groupId>cc.carm.lib</groupId> <groupId>cc.carm.lib</groupId>
<version>0.4.3</version> <version>0.4.4</version>
<relativePath>../../pom.xml</relativePath> <relativePath>../../pom.xml</relativePath>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>