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
@ -98,7 +98,7 @@ public interface SQLAction<T> {
|
|||||||
*/
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
default <R> R execute(@NotNull SQLFunction<T, R> function,
|
default <R> R execute(@NotNull SQLFunction<T, R> function,
|
||||||
@Nullable SQLExceptionHandler exceptionHandler) {
|
@Nullable SQLExceptionHandler exceptionHandler) {
|
||||||
return execute(function, null, exceptionHandler);
|
return execute(function, null, exceptionHandler);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -114,8 +114,8 @@ public interface SQLAction<T> {
|
|||||||
@Nullable
|
@Nullable
|
||||||
@Contract("_,!null,_ -> !null")
|
@Contract("_,!null,_ -> !null")
|
||||||
default <R> R execute(@NotNull SQLFunction<T, R> function,
|
default <R> R execute(@NotNull SQLFunction<T, R> function,
|
||||||
@Nullable R defaultResult,
|
@Nullable R defaultResult,
|
||||||
@Nullable SQLExceptionHandler exceptionHandler) {
|
@Nullable SQLExceptionHandler exceptionHandler) {
|
||||||
try {
|
try {
|
||||||
return executeFunction(function, defaultResult);
|
return executeFunction(function, defaultResult);
|
||||||
} catch (SQLException exception) {
|
} catch (SQLException exception) {
|
||||||
@ -133,7 +133,7 @@ public interface SQLAction<T> {
|
|||||||
* @throws SQLException 当SQL操作出现问题时抛出
|
* @throws SQLException 当SQL操作出现问题时抛出
|
||||||
*/
|
*/
|
||||||
@Nullable
|
@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);
|
return executeFunction(function, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -148,11 +148,10 @@ public interface SQLAction<T> {
|
|||||||
*/
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
@Contract("_,!null -> !null")
|
@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 {
|
@Nullable R defaultResult) throws SQLException {
|
||||||
try {
|
try {
|
||||||
T value = execute();
|
R result = function.apply(execute());
|
||||||
R result = function.apply(value);
|
|
||||||
return result == null ? defaultResult : result;
|
return result == null ? defaultResult : result;
|
||||||
} catch (SQLException exception) {
|
} catch (SQLException exception) {
|
||||||
throw new SQLException(exception);
|
throw new SQLException(exception);
|
||||||
@ -182,7 +181,7 @@ public interface SQLAction<T> {
|
|||||||
* @param failure 异常处理器 默认为 {@link SQLAction#defaultExceptionHandler()}
|
* @param failure 异常处理器 默认为 {@link SQLAction#defaultExceptionHandler()}
|
||||||
*/
|
*/
|
||||||
void executeAsync(@Nullable SQLHandler<T> success,
|
void executeAsync(@Nullable SQLHandler<T> success,
|
||||||
@Nullable SQLExceptionHandler failure);
|
@Nullable SQLExceptionHandler failure);
|
||||||
|
|
||||||
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();
|
||||||
|
@ -32,20 +32,10 @@ import java.sql.SQLException;
|
|||||||
*/
|
*/
|
||||||
public interface QueryAction extends SQLAction<SQLQuery> {
|
public interface QueryAction extends SQLAction<SQLQuery> {
|
||||||
|
|
||||||
/**
|
@Override
|
||||||
* 执行语句并处理返回值
|
@Contract("_,!null -> !null")
|
||||||
*
|
default <R> @Nullable R executeFunction(@NotNull SQLFunction<@NotNull SQLQuery, R> function,
|
||||||
* @param defaultResult 默认结果,若处理后的结果为null,则返回该值
|
@Nullable R defaultResult) throws SQLException {
|
||||||
* @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 {
|
|
||||||
|
|
||||||
try (SQLQuery value = execute()) {
|
try (SQLQuery value = execute()) {
|
||||||
R result = function.apply(value);
|
R result = function.apply(value);
|
||||||
return result == null ? defaultResult : result;
|
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.EasySQL;
|
||||||
import cc.carm.lib.easysql.api.SQLManager;
|
import cc.carm.lib.easysql.api.SQLManager;
|
||||||
import cc.carm.lib.easysql.testrunner.tests.SQLUpdateBatchTests;
|
import cc.carm.lib.easysql.testrunner.tests.QueryCloseTest;
|
||||||
import cc.carm.lib.easysql.testrunner.tests.SQLUpdateReturnKeysTest;
|
import cc.carm.lib.easysql.testrunner.tests.QueryFunctionTest;
|
||||||
import cc.carm.lib.easysql.testrunner.tests.TableCreateTest;
|
import cc.carm.lib.easysql.testrunner.tests.TableCreateTest;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.TestOnly;
|
import org.jetbrains.annotations.TestOnly;
|
||||||
@ -45,9 +45,10 @@ public class Main {
|
|||||||
// tests.add(new TableAlterTest());
|
// tests.add(new TableAlterTest());
|
||||||
// tests.add(new TableRenameTest());
|
// tests.add(new TableRenameTest());
|
||||||
// tests.add(new QueryNotCloseTest());
|
// tests.add(new QueryNotCloseTest());
|
||||||
// tests.add(new QueryCloseTest());
|
tests.add(new QueryCloseTest());
|
||||||
tests.add(new SQLUpdateBatchTests());
|
// tests.add(new SQLUpdateBatchTests());
|
||||||
tests.add(new SQLUpdateReturnKeysTest());
|
// tests.add(new SQLUpdateReturnKeysTest());
|
||||||
|
tests.add(new QueryFunctionTest());
|
||||||
|
|
||||||
print("准备进行测试...");
|
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