mirror of
https://github.com/CarmJos/EasySQL.git
synced 2024-09-19 21:35:47 +00:00
[0.3.3] 版本更新
- `[F]` 修复上个版本中 QueryAction 的 executeFunction 方法未重写 SQLAction 中同方法导致的链接未被自动关闭的问题。 - `[U]` 更新软件依赖于Maven相关插件的版本。
This commit is contained in:
parent
6322689d39
commit
8924258635
@ -133,7 +133,7 @@ public interface SQLAction<T> {
|
||||
* @throws SQLException 当SQL操作出现问题时抛出
|
||||
*/
|
||||
@Nullable
|
||||
default <R> R executeFunction(@NotNull SQLFunction<T, R> function) throws SQLException {
|
||||
default <R> R executeFunction(@NotNull SQLFunction<@NotNull T, R> function) throws SQLException {
|
||||
return executeFunction(function, null);
|
||||
}
|
||||
|
||||
@ -148,11 +148,10 @@ public interface SQLAction<T> {
|
||||
*/
|
||||
@Nullable
|
||||
@Contract("_,!null -> !null")
|
||||
default <R> R executeFunction(@NotNull SQLFunction<T, R> function,
|
||||
default <R> 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);
|
||||
|
@ -32,20 +32,10 @@ import java.sql.SQLException;
|
||||
*/
|
||||
public interface QueryAction extends SQLAction<SQLQuery> {
|
||||
|
||||
/**
|
||||
* 执行语句并处理返回值
|
||||
*
|
||||
* @param defaultResult 默认结果,若处理后的结果为null,则返回该值
|
||||
* @param function 处理方法
|
||||
* @param <R> 需要返回的内容
|
||||
* @return 指定类型数据
|
||||
* @throws SQLException 当SQL操作出现问题时抛出
|
||||
*/
|
||||
@Nullable
|
||||
@Contract("!null, _ -> !null")
|
||||
default <R> R executeFunction(@Nullable R defaultResult,
|
||||
@NotNull SQLFunction<SQLQuery, R> function) throws SQLException {
|
||||
|
||||
@Override
|
||||
@Contract("_,!null -> !null")
|
||||
default <R> @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;
|
||||
|
@ -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("准备进行测试...");
|
||||
|
||||
|
@ -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);
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user