Skip to content

Commit

Permalink
feat(execute): 提供Future类型的操作支持。
Browse files Browse the repository at this point in the history
  • Loading branch information
CarmJos committed Aug 5, 2022
1 parent 198a800 commit 26ab19e
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 7 deletions.
2 changes: 1 addition & 1 deletion api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>cc.carm.lib</groupId>
<artifactId>easysql-parent</artifactId>
<version>0.4.3</version>
<version>0.4.4</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
3 changes: 3 additions & 0 deletions api/src/main/java/cc/carm/lib/easysql/api/SQLAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -206,6 +207,8 @@ default void executeAsync(@Nullable SQLHandler<T> success) {
void executeAsync(@Nullable SQLHandler<T> success,
@Nullable SQLExceptionHandler failure);

<R> @NotNull Future<R> executeFuture(@NotNull SQLFunction<T, R> handler);

default void handleException(@Nullable SQLExceptionHandler handler, SQLException exception) {
if (handler == null) handler = defaultExceptionHandler();
handler.accept(exception, this);
Expand Down
2 changes: 1 addition & 1 deletion demo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>easysql-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>0.4.3</version>
<version>0.4.4</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
1 change: 1 addition & 0 deletions demo/src/test/java/cc/carm/lib/easysql/EasySQLTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down
3 changes: 2 additions & 1 deletion demo/src/test/java/cc/carm/lib/easysql/TestHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.jetbrains.annotations.NotNull;

import java.sql.SQLException;
import java.util.concurrent.ExecutionException;

public abstract class TestHandler {

Expand All @@ -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();
Expand Down
33 changes: 33 additions & 0 deletions demo/src/test/java/cc/carm/lib/easysql/tests/QueryFutureTest.java
Original file line number Diff line number Diff line change
@@ -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);

}
}
2 changes: 1 addition & 1 deletion impl/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>easysql-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>0.4.3</version>
<version>0.4.4</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<T> implements SQLAction<T> {
Expand Down Expand Up @@ -91,4 +94,10 @@ public void executeAsync(SQLHandler<T> success, SQLExceptionHandler failure) {
});
}

@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;
}
}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<groupId>cc.carm.lib</groupId>
<artifactId>easysql-parent</artifactId>
<packaging>pom</packaging>
<version>0.4.3</version>
<version>0.4.4</version>

<modules>
<module>api</module>
Expand Down
2 changes: 1 addition & 1 deletion with-pool/beecp/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>easysql-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>0.4.3</version>
<version>0.4.4</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
Expand Down
2 changes: 1 addition & 1 deletion with-pool/hikaricp/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>easysql-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>0.4.3</version>
<version>0.4.4</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
Expand Down

0 comments on commit 26ab19e

Please sign in to comment.