diff --git a/easysql-api/src/main/java/cc/carm/lib/easysql/api/SQLAction.java b/easysql-api/src/main/java/cc/carm/lib/easysql/api/SQLAction.java index 48e7fb9..7b71acd 100644 --- a/easysql-api/src/main/java/cc/carm/lib/easysql/api/SQLAction.java +++ b/easysql-api/src/main/java/cc/carm/lib/easysql/api/SQLAction.java @@ -98,7 +98,7 @@ public interface SQLAction { */ @Nullable default R execute(@NotNull SQLFunction function, - @Nullable SQLExceptionHandler exceptionHandler) { + @Nullable SQLExceptionHandler exceptionHandler) { return execute(function, null, exceptionHandler); } @@ -114,8 +114,8 @@ public interface SQLAction { @Nullable @Contract("_,!null,_ -> !null") default R execute(@NotNull SQLFunction function, - @Nullable R defaultResult, - @Nullable SQLExceptionHandler exceptionHandler) { + @Nullable R defaultResult, + @Nullable SQLExceptionHandler exceptionHandler) { try { return executeFunction(function, defaultResult); } catch (SQLException exception) { @@ -133,7 +133,7 @@ public interface SQLAction { * @throws SQLException 当SQL操作出现问题时抛出 */ @Nullable - default R executeFunction(@NotNull SQLFunction function) throws SQLException { + default R executeFunction(@NotNull SQLFunction<@NotNull T, R> function) throws SQLException { return executeFunction(function, null); } @@ -148,11 +148,10 @@ public interface SQLAction { */ @Nullable @Contract("_,!null -> !null") - default R executeFunction(@NotNull SQLFunction function, - @Nullable R defaultResult) throws SQLException { + default R executeFunction(@NotNull SQLFunction<@NotNull T, R> function, + @Nullable R defaultResult) throws SQLException { try { - T value = execute(); - R result = function.apply(value); + R result = function.apply(execute()); return result == null ? defaultResult : result; } catch (SQLException exception) { throw new SQLException(exception); @@ -182,7 +181,7 @@ public interface SQLAction { * @param failure 异常处理器 默认为 {@link SQLAction#defaultExceptionHandler()} */ void executeAsync(@Nullable SQLHandler success, - @Nullable SQLExceptionHandler failure); + @Nullable SQLExceptionHandler failure); default void handleException(@Nullable SQLExceptionHandler handler, SQLException exception) { if (handler == null) handler = defaultExceptionHandler(); diff --git a/easysql-api/src/main/java/cc/carm/lib/easysql/api/action/query/QueryAction.java b/easysql-api/src/main/java/cc/carm/lib/easysql/api/action/query/QueryAction.java index f94689e..2d5270b 100644 --- a/easysql-api/src/main/java/cc/carm/lib/easysql/api/action/query/QueryAction.java +++ b/easysql-api/src/main/java/cc/carm/lib/easysql/api/action/query/QueryAction.java @@ -32,20 +32,10 @@ import java.sql.SQLException; */ public interface QueryAction extends SQLAction { - /** - * 执行语句并处理返回值 - * - * @param defaultResult 默认结果,若处理后的结果为null,则返回该值 - * @param function 处理方法 - * @param 需要返回的内容 - * @return 指定类型数据 - * @throws SQLException 当SQL操作出现问题时抛出 - */ - @Nullable - @Contract("!null, _ -> !null") - default R executeFunction(@Nullable R defaultResult, - @NotNull SQLFunction function) throws SQLException { - + @Override + @Contract("_,!null -> !null") + default @Nullable R executeFunction(@NotNull SQLFunction<@NotNull SQLQuery, R> function, + @Nullable R defaultResult) throws SQLException { try (SQLQuery value = execute()) { R result = function.apply(value); return result == null ? defaultResult : result; diff --git a/example/easysql-test/src/main/java/cc/carm/lib/easysql/testrunner/Main.java b/example/easysql-test/src/main/java/cc/carm/lib/easysql/testrunner/Main.java index 1c06f66..f7196f0 100644 --- a/example/easysql-test/src/main/java/cc/carm/lib/easysql/testrunner/Main.java +++ b/example/easysql-test/src/main/java/cc/carm/lib/easysql/testrunner/Main.java @@ -2,8 +2,8 @@ package cc.carm.lib.easysql.testrunner; import cc.carm.lib.easysql.EasySQL; import cc.carm.lib.easysql.api.SQLManager; -import cc.carm.lib.easysql.testrunner.tests.SQLUpdateBatchTests; -import cc.carm.lib.easysql.testrunner.tests.SQLUpdateReturnKeysTest; +import cc.carm.lib.easysql.testrunner.tests.QueryCloseTest; +import cc.carm.lib.easysql.testrunner.tests.QueryFunctionTest; import cc.carm.lib.easysql.testrunner.tests.TableCreateTest; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.TestOnly; @@ -45,9 +45,10 @@ public class Main { // 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 QueryCloseTest()); +// tests.add(new SQLUpdateBatchTests()); +// tests.add(new SQLUpdateReturnKeysTest()); + tests.add(new QueryFunctionTest()); print("准备进行测试..."); diff --git a/example/easysql-test/src/main/java/cc/carm/lib/easysql/testrunner/tests/QueryFunctionTest.java b/example/easysql-test/src/main/java/cc/carm/lib/easysql/testrunner/tests/QueryFunctionTest.java new file mode 100644 index 0000000..3d90fc6 --- /dev/null +++ b/example/easysql-test/src/main/java/cc/carm/lib/easysql/testrunner/tests/QueryFunctionTest.java @@ -0,0 +1,34 @@ +package cc.carm.lib.easysql.testrunner.tests; + +import cc.carm.lib.easysql.api.SQLManager; +import cc.carm.lib.easysql.testrunner.EasySQLTest; + +import java.sql.SQLException; + +public class QueryFunctionTest extends EasySQLTest { + + + @Override + public void onTest(SQLManager sqlManager) throws SQLException { + + Integer id_1 = sqlManager.createQuery() + .inTable("test_user_table") + .orderBy("id", false) + .setLimit(1) + .build().executeFunction(query -> { + if (!query.getResultSet().next()) return -1; + else return query.getResultSet().getInt("id"); + }); + + System.out.println("id (ps): " + id_1); + + Integer id_2 = sqlManager.createQuery().withSQL("SELECT id FROM test_user_table ORDER BY id DESC LIMIT 1") + .executeFunction(query -> { + if (!query.getResultSet().next()) return -1; + else return query.getResultSet().getInt("id"); + }); + + System.out.println("id (s): " + id_2); + + } +}