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

chore(sql): 尝试分离async部分

This commit is contained in:
Carm Jos 2022-12-22 09:49:57 +08:00
parent 81f065a85a
commit f606fd4efc
23 changed files with 147 additions and 204 deletions

View File

@ -1,7 +1,7 @@
package cc.carm.lib.easysql.api;
import cc.carm.lib.easysql.api.action.asyncable.AsyncableBatchUpdateAction;
import cc.carm.lib.easysql.api.action.asyncable.AsyncableUpdateAction;
import cc.carm.lib.easysql.api.action.asyncable.AsyncablePreparedBatchUpdateAction;
import cc.carm.lib.easysql.api.action.asyncable.AsyncablePreparedUpdateAction;
import cc.carm.lib.easysql.api.builder.*;
import cc.carm.lib.easysql.api.enums.IsolationLevel;
import cc.carm.lib.easysql.api.function.SQLBiFunction;
@ -36,7 +36,7 @@ public interface SQLManager extends SQLSource {
* @param tableName 目标表名
* @return {@link InsertBuilder}
*/
@NotNull InsertBuilder<AsyncableUpdateAction<Integer>> insertInto(@NotNull String tableName);
@NotNull InsertBuilder<AsyncablePreparedUpdateAction<Integer>> insertInto(@NotNull String tableName);
/**
* 创建支持多组数据的插入操作
@ -44,7 +44,7 @@ public interface SQLManager extends SQLSource {
* @param tableName 目标表名
* @return {@link InsertBuilder}
*/
@NotNull InsertBuilder<AsyncableBatchUpdateAction<Integer>> insertBatchInto(@NotNull String tableName);
@NotNull InsertBuilder<AsyncablePreparedBatchUpdateAction<Integer>> insertBatchInto(@NotNull String tableName);
/**
* 创建一条替换操作
@ -52,7 +52,7 @@ public interface SQLManager extends SQLSource {
* @param tableName 目标表名
* @return {@link ReplaceBuilder}
*/
@NotNull ReplaceBuilder<AsyncableUpdateAction<Integer>> replaceInto(@NotNull String tableName);
@NotNull ReplaceBuilder<AsyncablePreparedUpdateAction<Integer>> replaceInto(@NotNull String tableName);
/**
* 创建支持多组数据的替换操作
@ -60,7 +60,7 @@ public interface SQLManager extends SQLSource {
* @param tableName 目标表名
* @return {@link ReplaceBuilder}
*/
@NotNull ReplaceBuilder<AsyncableBatchUpdateAction<Integer>> replaceBatchInto(@NotNull String tableName);
@NotNull ReplaceBuilder<AsyncablePreparedBatchUpdateAction<Integer>> replaceBatchInto(@NotNull String tableName);
/**
* 创建更新操作

View File

@ -1,7 +1,7 @@
package cc.carm.lib.easysql.api;
import cc.carm.lib.easysql.api.action.query.QueryAction;
import cc.carm.lib.easysql.api.action.query.PreparedQueryAction;
import cc.carm.lib.easysql.api.action.base.QueryAction;
import cc.carm.lib.easysql.api.action.base.PreparedQueryAction;
import java.sql.Connection;
import java.sql.ResultSet;

View File

@ -1,8 +1,8 @@
package cc.carm.lib.easysql.api.action;
import cc.carm.lib.easysql.api.SQLSource;
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 org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@ -12,8 +12,6 @@ import java.sql.SQLException;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
public interface SQLAction<T> {
@ -68,11 +66,11 @@ public interface SQLAction<T> {
}
/**
* 得到承载该Action的对应{@link SQLManager}
* 得到承载该Action的对应{@link SQLSource}
*
* @return {@link SQLManager}
* @return {@link SQLSource}
*/
@NotNull SQLManager getManager();
@NotNull SQLSource getSource();
/**
* 执行该Action对应的SQL语句
@ -82,7 +80,6 @@ public interface SQLAction<T> {
*/
@NotNull T execute() throws SQLException;
/**
* 执行语句并返回值
*
@ -164,47 +161,6 @@ public interface SQLAction<T> {
}
}
/**
* 异步执行SQL语句采用默认异常处理无需返回值
*/
default void executeAsync() {
executeAsync(null);
}
/**
* 异步执行SQL语句
*
* @param success 成功时的操作
*/
default void executeAsync(@Nullable SQLHandler<T> success) {
executeAsync(success, null);
}
/**
* 异步执行SQL语句
*
* @param success 成功时的操作
* @param failure 异常处理器 默认为 {@link SQLAction#defaultExceptionHandler()}
*/
void executeAsync(@Nullable SQLHandler<T> success,
@Nullable SQLExceptionHandler failure);
/**
* 以异步Future方式执行SQL语句
*
* @return 异步执行的Future实例可通过 {@link Future#get()} 阻塞并等待结果
*/
default @NotNull CompletableFuture<Void> executeFuture() {
return executeFuture((t -> null));
}
/**
* 以异步Future方式执行SQL语句
*
* @return 异步执行的Future实例可通过 {@link Future#get()} 阻塞并等待结果
*/
<R> @NotNull CompletableFuture<R> executeFuture(@NotNull SQLFunction<T, R> handler);
default void handleException(@Nullable SQLExceptionHandler handler, SQLException exception) {
if (handler == null) handler = defaultExceptionHandler();
handler.accept(exception, this);
@ -218,7 +174,7 @@ public interface SQLAction<T> {
* @return {@link SQLExceptionHandler}
*/
default SQLExceptionHandler defaultExceptionHandler() {
return getManager().getExceptionHandler();
return getSource().getExceptionHandler();
}
/**
@ -229,7 +185,7 @@ public interface SQLAction<T> {
* @param handler 异常处理器
*/
default void setExceptionHandler(@Nullable SQLExceptionHandler handler) {
getManager().setExceptionHandler(handler);
getSource().setExceptionHandler(handler);
}
}

View File

@ -0,0 +1,35 @@
package cc.carm.lib.easysql.api.action.asyncable;
import org.jetbrains.annotations.Nullable;
import java.sql.PreparedStatement;
import java.util.function.Consumer;
public interface AsyncablePreparedQueryAction extends AsyncableQueryAction {
/**
* 设定SQL语句中所有 ? 对应的参数
*
* @param params 参数内容
* @return {@link AsyncablePreparedQueryAction}
*/
AsyncablePreparedQueryAction setParams(@Nullable Object... params);
/**
* 设定SQL语句中所有 ? 对应的参数
*
* @param params 参数内容
* @return {@link AsyncablePreparedQueryAction}
*/
AsyncablePreparedQueryAction setParams(@Nullable Iterable<Object> params);
/**
* 直接对 {@link PreparedStatement} 进行处理
*
* @param statement {@link Consumer} 处理操作
* 若为空则不进行处理
* @return {@link AsyncablePreparedQueryAction}
*/
AsyncablePreparedQueryAction handleStatement(@Nullable Consumer<PreparedStatement> statement);
}

View File

@ -0,0 +1,45 @@
package cc.carm.lib.easysql.api.action.asyncable;
import cc.carm.lib.easysql.api.SQLQuery;
import cc.carm.lib.easysql.api.action.SQLAsyncableAction;
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 org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.sql.SQLException;
/**
* SQLQueryAction 是用于承载SQL查询语句并进行处理返回并自动关闭连接的基本类
*
* <ul>
* <li>同步执行 {@link #execute()}, {@link #execute(SQLFunction, SQLExceptionHandler)}
* <br>同步执行方法中有会抛出异常的方法与不抛出异常的方法
* <br>若选择不抛出异常则返回值可能为空需要特殊处理</li>
*
* <li>异步执行 {@link #executeAsync(SQLHandler, SQLExceptionHandler)}
* <br>异步执行时将提供成功与异常两种处理方式
* <br>可自行选择是否对数据或异常进行处理
* <br>默认的异常处理器为 {@link #defaultExceptionHandler()}</li>
* </ul>
*
* <b>注意 无论是否异步都不需要自行关闭ResultSet本API已自动关闭</b>
*
* @author CarmJos
* @since 0.2.6
*/
public interface AsyncableQueryAction extends SQLAsyncableAction<SQLQuery> {
@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;
}
}
}

View File

@ -1,9 +1,27 @@
package cc.carm.lib.easysql.api.action.asyncable;
import cc.carm.lib.easysql.api.action.SQLAsyncableAction;
import cc.carm.lib.easysql.api.newactions.SQLUpdateAction;
import cc.carm.lib.easysql.api.action.base.UpdateAction;
public interface AsyncableUpdateAction<T extends Number>
extends SQLUpdateAction<T>, SQLAsyncableAction<T> {
extends SQLAsyncableAction<T> {
/**
* 设定该操作返回自增键序列
*
* @return {@link UpdateAction}
*/
AsyncableUpdateAction<T> returnGeneratedKey();
/**
* 设定该操作返回自增键序列
*
* @param keyTypeClass 自增序列的数字类型
* @param <N> 自增键序列类型 {@link Number}
* @return {@link UpdateAction}
* @since 0.4.0
*/
<N extends Number> AsyncableUpdateAction<N> returnGeneratedKey(Class<N> keyTypeClass);
}

View File

@ -1,4 +1,4 @@
package cc.carm.lib.easysql.api.action.query;
package cc.carm.lib.easysql.api.action.base;
import org.jetbrains.annotations.Nullable;

View File

@ -1,5 +1,6 @@
package cc.carm.lib.easysql.api.action.query;
package cc.carm.lib.easysql.api.action.base;
import cc.carm.lib.easysql.api.action.SQLAction;
import cc.carm.lib.easysql.api.function.SQLFunction;
import cc.carm.lib.easysql.api.SQLQuery;
import cc.carm.lib.easysql.api.function.SQLExceptionHandler;

View File

@ -1,8 +1,8 @@
package cc.carm.lib.easysql.api.builder;
import cc.carm.lib.easysql.api.SQLBuilder;
import cc.carm.lib.easysql.api.action.query.QueryAction;
import cc.carm.lib.easysql.api.action.query.PreparedQueryAction;
import cc.carm.lib.easysql.api.action.base.QueryAction;
import cc.carm.lib.easysql.api.action.base.PreparedQueryAction;
import org.jetbrains.annotations.NotNull;
public interface QueryBuilder extends SQLBuilder {

View File

@ -1,5 +1,7 @@
package cc.carm.lib.easysql.api.builder;
import cc.carm.lib.easysql.api.action.SQLAction;
import java.util.Arrays;
import java.util.List;

View File

@ -1,6 +1,6 @@
package cc.carm.lib.easysql.api.builder;
import cc.carm.lib.easysql.api.action.query.PreparedQueryAction;
import cc.carm.lib.easysql.api.action.base.PreparedQueryAction;
import org.jetbrains.annotations.NotNull;
public interface TableQueryBuilder extends ConditionalBuilder<TableQueryBuilder, PreparedQueryAction> {

View File

@ -1,8 +1,9 @@
package cc.carm.lib.easysql.api.function;
import cc.carm.lib.easysql.api.SQLQuery;
import cc.carm.lib.easysql.api.action.base.PreparedUpdateAction;
import cc.carm.lib.easysql.api.action.SQLAction;
import cc.carm.lib.easysql.api.action.base.PreparedBatchUpdateAction;
import cc.carm.lib.easysql.api.action.base.PreparedUpdateAction;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;

View File

@ -1,42 +0,0 @@
package cc.carm.lib.easysql.api.newactions;
import cc.carm.lib.easysql.api.action.SQLAction;
import java.util.List;
public interface PreparedBatchUpdateAction<T extends Number> extends SQLAction<List<T>> {
/**
* 设定多组SQL语句中所有 ? 对应的参数
*
* @param allValues 所有参数内容
* @return {@link PreparedBatchUpdateAction}
*/
PreparedBatchUpdateAction<T> allValues(Iterable<Object[]> allValues);
/**
* 添加一组SQL语句中所有 ? 对应的参数
*
* @param values 参数内容
* @return {@link PreparedBatchUpdateAction}
*/
PreparedBatchUpdateAction<T> values(Object... values);
/**
* 设定该操作返回自增键序列
*
* @return {@link SQLUpdateAction}
*/
PreparedBatchUpdateAction<T> returnGeneratedKeys();
/**
* 设定该操作返回自增键序列
*
* @param keyTypeClass 自增序列的数字类型
* @param <N> 自增键序列类型 {@link Number}
* @return {@link SQLUpdateAction}
* @since 0.4.0
*/
<N extends Number> PreparedBatchUpdateAction<N> returnGeneratedKeys(Class<N> keyTypeClass);
}

View File

@ -1,24 +0,0 @@
package cc.carm.lib.easysql.api.newactions;
import org.jetbrains.annotations.Nullable;
public interface PreparedUpdateAction<T extends Number> extends SQLUpdateAction<T> {
/**
* 设定SQL语句中所有 ? 对应的参数
*
* @param params 参数内容
* @return {@link PreparedUpdateAction}
*/
PreparedUpdateAction<T> values(Object... params);
/**
* 设定SQL语句中所有 ? 对应的参数
*
* @param params 参数内容
* @return {@link PreparedUpdateAction}
* @since 0.4.0
*/
PreparedUpdateAction<T> values(@Nullable Iterable<Object> params);
}

View File

@ -1,26 +0,0 @@
package cc.carm.lib.easysql.api.newactions;
import org.jetbrains.annotations.NotNull;
import java.util.List;
@SuppressWarnings("UnusedReturnValue")
public interface SQLBatchUpdateAction extends SQLAction<List<Integer>> {
/**
* 添加一条批量执行的SQL语句
*
* @param sql SQL语句
* @return {@link SQLBatchUpdateAction}
*/
SQLBatchUpdateAction addBatch(@NotNull String sql);
@Override
default @NotNull String getSQLContent() {
return getSQLContents().get(0);
}
@Override
@NotNull List<String> getSQLContents();
}

View File

@ -1,24 +0,0 @@
package cc.carm.lib.easysql.api.newactions;
public interface SQLUpdateAction<T extends Number> extends SQLAction<T> {
/**
* 设定该操作返回自增键序列
*
* @return {@link SQLUpdateAction}
*/
SQLUpdateAction<T> returnGeneratedKey();
/**
* 设定该操作返回自增键序列
*
* @param keyTypeClass 自增序列的数字类型
* @param <N> 自增键序列类型 {@link Number}
* @return {@link SQLUpdateAction}
* @since 0.4.0
*/
<N extends Number> SQLUpdateAction<N> returnGeneratedKey(Class<N> keyTypeClass);
}

View File

@ -1,5 +1,6 @@
package cc.carm.lib.easysql.api.table;
import cc.carm.lib.easysql.api.SQLManager;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

View File

@ -1,9 +1,9 @@
package cc.carm.lib.easysql.api.table;
import cc.carm.lib.easysql.api.action.base.PreparedUpdateAction;
import cc.carm.lib.easysql.api.action.base.PreparedBatchUpdateAction;
import cc.carm.lib.easysql.api.SQLManager;
import cc.carm.lib.easysql.api.action.asyncable.AsyncablePreparedBatchUpdateAction;
import cc.carm.lib.easysql.api.action.asyncable.AsyncablePreparedUpdateAction;
import cc.carm.lib.easysql.api.builder.*;
import cc.carm.lib.easysql.api.old.builder.*;
import cc.carm.lib.easysql.api.function.SQLHandler;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@ -88,7 +88,7 @@ public interface SQLTable {
}
default @NotNull DeleteBuilder createDelete(@NotNull SQLManager sqlManager) {
return sqlManager.createDelete(getTableName());
return sqlManager.deleteFrom(getTableName());
}
default @NotNull UpdateBuilder createUpdate() {
@ -97,44 +97,44 @@ public interface SQLTable {
}
default @NotNull UpdateBuilder createUpdate(@NotNull SQLManager sqlManager) {
return sqlManager.createUpdate(getTableName());
return sqlManager.updateInto(getTableName());
}
default @NotNull InsertBuilder<PreparedUpdateAction<Integer>> createInsert() {
default @NotNull InsertBuilder<AsyncablePreparedUpdateAction<Integer>> createInsert() {
return Optional.ofNullable(getSQLManager()).map(this::createInsert)
.orElseThrow(() -> new NullPointerException("This table doesn't have a SQLManger."));
}
default @NotNull InsertBuilder<PreparedUpdateAction<Integer>> createInsert(@NotNull SQLManager sqlManager) {
default @NotNull InsertBuilder<AsyncablePreparedUpdateAction<Integer>> createInsert(@NotNull SQLManager sqlManager) {
return sqlManager.insertInto(getTableName());
}
default @NotNull InsertBuilder<PreparedBatchUpdateAction<Integer>> createInsertBatch() {
default @NotNull InsertBuilder<AsyncablePreparedBatchUpdateAction<Integer>> createInsertBatch() {
return Optional.ofNullable(getSQLManager()).map(this::createInsertBatch)
.orElseThrow(() -> new NullPointerException("This table doesn't have a SQLManger."));
}
default @NotNull InsertBuilder<PreparedBatchUpdateAction<Integer>> createInsertBatch(@NotNull SQLManager sqlManager) {
return sqlManager.createInsertBatch(getTableName());
default @NotNull InsertBuilder<AsyncablePreparedBatchUpdateAction<Integer>> createInsertBatch(@NotNull SQLManager sqlManager) {
return sqlManager.insertBatchInto(getTableName());
}
default @NotNull ReplaceBuilder<PreparedUpdateAction<Integer>> createReplace() {
default @NotNull ReplaceBuilder<AsyncablePreparedUpdateAction<Integer>> createReplace() {
return Optional.ofNullable(getSQLManager()).map(this::createReplace)
.orElseThrow(() -> new NullPointerException("This table doesn't have a SQLManger."));
}
default @NotNull ReplaceBuilder<PreparedUpdateAction<Integer>> createReplace(@NotNull SQLManager sqlManager) {
return sqlManager.createReplace(getTableName());
default @NotNull ReplaceBuilder<AsyncablePreparedUpdateAction<Integer>> createReplace(@NotNull SQLManager sqlManager) {
return sqlManager.replaceInto(getTableName());
}
default @NotNull ReplaceBuilder<PreparedBatchUpdateAction<Integer>> createReplaceBatch() {
default @NotNull ReplaceBuilder<AsyncablePreparedBatchUpdateAction<Integer>> createReplaceBatch() {
return Optional.ofNullable(getSQLManager()).map(this::createReplaceBatch)
.orElseThrow(() -> new NullPointerException("This table doesn't have a SQLManger."));
}
default @NotNull ReplaceBuilder<PreparedBatchUpdateAction<Integer>> createReplaceBatch(@NotNull SQLManager sqlManager) {
return sqlManager.createReplaceBatch(getTableName());
default @NotNull ReplaceBuilder<AsyncablePreparedBatchUpdateAction<Integer>> createReplaceBatch(@NotNull SQLManager sqlManager) {
return sqlManager.replaceBatchInto(getTableName());
}
default @NotNull TableAlterBuilder alter() {

View File

@ -1,7 +1,7 @@
package cc.carm.lib.easysql.api.transaction;
import cc.carm.lib.easysql.api.SQLSource;
import cc.carm.lib.easysql.api.action.SQLAction;
import cc.carm.lib.easysql.api.old.SQLSource;
import org.jetbrains.annotations.NotNull;
public interface TransactionAction<T> extends SQLAction<T> {

View File

@ -1,6 +1,6 @@
package cc.carm.lib.easysql.action.query;
import cc.carm.lib.easysql.api.action.query.PreparedQueryAction;
import cc.carm.lib.easysql.api.action.base.PreparedQueryAction;
import cc.carm.lib.easysql.manager.SQLManagerImpl;
import cc.carm.lib.easysql.query.SQLQueryImpl;
import cc.carm.lib.easysql.util.StatementUtil;

View File

@ -2,7 +2,7 @@ package cc.carm.lib.easysql.action.query;
import cc.carm.lib.easysql.action.AbstractSQLAction;
import cc.carm.lib.easysql.api.SQLQuery;
import cc.carm.lib.easysql.api.action.query.QueryAction;
import cc.carm.lib.easysql.api.action.base.QueryAction;
import cc.carm.lib.easysql.api.function.SQLExceptionHandler;
import cc.carm.lib.easysql.api.function.SQLHandler;
import cc.carm.lib.easysql.manager.SQLManagerImpl;

View File

@ -2,8 +2,8 @@ package cc.carm.lib.easysql.builder.impl;
import cc.carm.lib.easysql.action.query.PreparedQueryActionImpl;
import cc.carm.lib.easysql.action.query.QueryActionImpl;
import cc.carm.lib.easysql.api.action.query.PreparedQueryAction;
import cc.carm.lib.easysql.api.action.query.QueryAction;
import cc.carm.lib.easysql.api.action.base.PreparedQueryAction;
import cc.carm.lib.easysql.api.action.base.QueryAction;
import cc.carm.lib.easysql.api.builder.QueryBuilder;
import cc.carm.lib.easysql.api.builder.TableQueryBuilder;
import cc.carm.lib.easysql.builder.AbstractSQLBuilder;

View File

@ -1,7 +1,7 @@
package cc.carm.lib.easysql.builder.impl;
import cc.carm.lib.easysql.action.query.PreparedQueryActionImpl;
import cc.carm.lib.easysql.api.action.query.PreparedQueryAction;
import cc.carm.lib.easysql.api.action.base.PreparedQueryAction;
import cc.carm.lib.easysql.api.builder.TableQueryBuilder;
import cc.carm.lib.easysql.manager.SQLManagerImpl;
import org.jetbrains.annotations.NotNull;