mirror of
https://github.com/CarmJos/EasySQL.git
synced 2026-06-05 00:11:48 +08:00
[v0.2.7] [U] 进一步简化操作使用逻辑
This commit is contained in:
@@ -1,23 +1,23 @@
|
||||
package cc.carm.lib.easysql.api;
|
||||
|
||||
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.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.UUID;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
* SQLAction 是用于承载SQL语句并进行处理、返回的基本类。
|
||||
*
|
||||
* <ul>
|
||||
* <li>同步执行 {@link #execute()}, {@link #execute(SQLFunction, BiConsumer)}
|
||||
* <li>同步执行 {@link #execute()}, {@link #execute(SQLFunction, SQLExceptionHandler)}
|
||||
* <br>同步执行方法中有会抛出异常的方法与不抛出异常的方法,
|
||||
* <br>若选择不抛出异常,则返回值可能为空,需要特殊处理。</li>
|
||||
*
|
||||
* <li>异步执行 {@link #executeAsync(Consumer, BiConsumer)}
|
||||
* <li>异步执行 {@link #executeAsync(SQLHandler, SQLExceptionHandler)}
|
||||
* <br>异步执行时将提供成功与异常两种处理方式
|
||||
* <br>可自行选择是否对数据或异常进行处理
|
||||
* <br>默认的异常处理器为 {@link #defaultExceptionHandler()}</li>
|
||||
@@ -80,7 +80,7 @@ public interface SQLAction<T> {
|
||||
* @return 指定类型数据
|
||||
*/
|
||||
@Nullable
|
||||
default T execute(@Nullable BiConsumer<SQLException, SQLAction<T>> exceptionHandler) {
|
||||
default T execute(@Nullable SQLExceptionHandler exceptionHandler) {
|
||||
return execute(t -> t, exceptionHandler);
|
||||
}
|
||||
|
||||
@@ -112,7 +112,7 @@ public interface SQLAction<T> {
|
||||
*/
|
||||
@Nullable
|
||||
default <R> R execute(@NotNull SQLFunction<T, R> function,
|
||||
@Nullable BiConsumer<SQLException, SQLAction<T>> exceptionHandler) {
|
||||
@Nullable SQLExceptionHandler exceptionHandler) {
|
||||
try {
|
||||
return executeFunction(function);
|
||||
} catch (SQLException exception) {
|
||||
@@ -133,7 +133,7 @@ public interface SQLAction<T> {
|
||||
*
|
||||
* @param success 成功时的操作
|
||||
*/
|
||||
default void executeAsync(@Nullable Consumer<T> success) {
|
||||
default void executeAsync(@Nullable SQLHandler<T> success) {
|
||||
executeAsync(success, null);
|
||||
}
|
||||
|
||||
@@ -143,10 +143,10 @@ public interface SQLAction<T> {
|
||||
* @param success 成功时的操作
|
||||
* @param failure 异常处理器 默认为 {@link SQLAction#defaultExceptionHandler()}
|
||||
*/
|
||||
void executeAsync(@Nullable Consumer<T> success, @Nullable BiConsumer<SQLException, SQLAction<T>> failure);
|
||||
void executeAsync(@Nullable SQLHandler<T> success,
|
||||
@Nullable SQLExceptionHandler failure);
|
||||
|
||||
|
||||
default void handleException(@Nullable BiConsumer<SQLException, SQLAction<T>> handler, SQLException exception) {
|
||||
default void handleException(@Nullable SQLExceptionHandler handler, SQLException exception) {
|
||||
if (handler == null) handler = defaultExceptionHandler();
|
||||
handler.accept(exception, this);
|
||||
}
|
||||
@@ -154,7 +154,7 @@ public interface SQLAction<T> {
|
||||
/**
|
||||
* @return 默认的异常处理器
|
||||
*/
|
||||
default BiConsumer<SQLException, SQLAction<T>> defaultExceptionHandler() {
|
||||
default SQLExceptionHandler defaultExceptionHandler() {
|
||||
return (exception, action) -> {
|
||||
getManager().getLogger().severe("Error when execute [" + action.getSQLContent() + "]");
|
||||
getManager().getLogger().severe(exception.getLocalizedMessage());
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
package cc.carm.lib.easysql.api.action;
|
||||
|
||||
import cc.carm.lib.easysql.api.SQLAction;
|
||||
import cc.carm.lib.easysql.api.function.SQLExceptionHandler;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
import java.util.function.BiConsumer;
|
||||
|
||||
public interface SQLUpdateBatchAction extends SQLAction<List<Integer>> {
|
||||
|
||||
@@ -20,7 +19,7 @@ public interface SQLUpdateBatchAction extends SQLAction<List<Integer>> {
|
||||
List<String> getSQLContents();
|
||||
|
||||
@Override
|
||||
default BiConsumer<SQLException, SQLAction<List<Integer>>> defaultExceptionHandler() {
|
||||
default SQLExceptionHandler<List<Integer>> defaultExceptionHandler() {
|
||||
return (exception, action) -> {
|
||||
getManager().getLogger().severe("Error when execute SQLs : ");
|
||||
int i = 1;
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
package cc.carm.lib.easysql.api.function;
|
||||
|
||||
import cc.carm.lib.easysql.api.SQLAction;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.function.BiConsumer;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface SQLExceptionHandler extends BiConsumer<SQLException, SQLAction<?>> {
|
||||
|
||||
}
|
||||
@@ -1,9 +1,11 @@
|
||||
package cc.carm.lib.easysql.api.function;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.sql.SQLException;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface SQLFunction<T, R> {
|
||||
|
||||
@Nullable
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
package cc.carm.lib.easysql.api.function;
|
||||
|
||||
import org.jetbrains.annotations.Contract;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.Objects;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface SQLHandler<T> {
|
||||
|
||||
void accept(@NotNull T t) throws SQLException;
|
||||
|
||||
@NotNull
|
||||
@Contract(pure = true)
|
||||
default SQLHandler<T> andThen(@NotNull SQLHandler<? super T> after) {
|
||||
Objects.requireNonNull(after);
|
||||
return (T t) -> {
|
||||
accept(t);
|
||||
after.accept(t);
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -64,13 +64,9 @@ public class EasySQLDemo {
|
||||
.setLimit(1) // 只取出一个数据
|
||||
.build().executeAsync(success -> {
|
||||
ResultSet resultSet = success.getResultSet();
|
||||
try {
|
||||
if (resultSet != null && resultSet.next()) {
|
||||
String username = resultSet.getString("username");
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}, (exception, action) -> {
|
||||
//do something
|
||||
long createTIme = action.getCreateTime();
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package cc.carm.lib.easysql.action;
|
||||
|
||||
import cc.carm.lib.easysql.api.SQLAction;
|
||||
import cc.carm.lib.easysql.api.function.SQLExceptionHandler;
|
||||
import cc.carm.lib.easysql.api.function.SQLHandler;
|
||||
import cc.carm.lib.easysql.manager.SQLManagerImpl;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
@@ -8,7 +10,6 @@ import org.jetbrains.annotations.Nullable;
|
||||
import java.sql.SQLException;
|
||||
import java.util.UUID;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public abstract class AbstractSQLAction<T> implements SQLAction<T> {
|
||||
|
||||
@@ -71,25 +72,16 @@ public abstract class AbstractSQLAction<T> implements SQLAction<T> {
|
||||
getManager().debug("#" + getShortID() + " ->" + getSQLContent());
|
||||
}
|
||||
|
||||
public void handleException(SQLException exception) {
|
||||
if (this.exceptionHandler == null) {
|
||||
defaultExceptionHandler().accept(exception, this);
|
||||
} else {
|
||||
this.exceptionHandler.accept(exception, this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void executeAsync(Consumer<T> success, BiConsumer<SQLException, SQLAction<T>> failure) {
|
||||
public void executeAsync(SQLHandler<T> success, SQLExceptionHandler failure) {
|
||||
getManager().getExecutorPool().submit(() -> {
|
||||
try {
|
||||
T returnedValue = execute();
|
||||
if (success != null) success.accept(returnedValue);
|
||||
} catch (SQLException e) {
|
||||
(failure == null ? defaultExceptionHandler() : failure).accept(e, this);
|
||||
handleException(failure, e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
+3
-1
@@ -14,7 +14,9 @@ import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class PreparedSQLBatchUpdateActionImpl extends AbstractSQLAction<List<Integer>> implements PreparedSQLUpdateBatchAction {
|
||||
public class PreparedSQLBatchUpdateActionImpl
|
||||
extends AbstractSQLAction<List<Integer>>
|
||||
implements PreparedSQLUpdateBatchAction {
|
||||
|
||||
int keyIndex = -1;
|
||||
List<Object[]> allParams;
|
||||
|
||||
+3
-1
@@ -13,7 +13,9 @@ import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class PreparedSQLUpdateActionImpl extends SQLUpdateActionImpl implements PreparedSQLUpdateAction {
|
||||
public class PreparedSQLUpdateActionImpl
|
||||
extends SQLUpdateActionImpl
|
||||
implements PreparedSQLUpdateAction {
|
||||
|
||||
Object[] params;
|
||||
|
||||
|
||||
@@ -9,7 +9,9 @@ import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
|
||||
public class SQLUpdateActionImpl extends AbstractSQLAction<Integer> implements SQLUpdateAction {
|
||||
public class SQLUpdateActionImpl
|
||||
extends AbstractSQLAction<Integer>
|
||||
implements SQLUpdateAction {
|
||||
|
||||
int keyIndex = -1;
|
||||
|
||||
|
||||
@@ -12,7 +12,9 @@ import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class SQLUpdateBatchActionImpl extends AbstractSQLAction<List<Integer>> implements SQLUpdateBatchAction {
|
||||
public class SQLUpdateBatchActionImpl
|
||||
extends AbstractSQLAction<List<Integer>>
|
||||
implements SQLUpdateBatchAction {
|
||||
|
||||
List<String> sqlContents = new ArrayList<>();
|
||||
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
package cc.carm.lib.easysql.action.query;
|
||||
|
||||
import cc.carm.lib.easysql.action.AbstractSQLAction;
|
||||
import cc.carm.lib.easysql.api.SQLAction;
|
||||
import cc.carm.lib.easysql.api.action.query.QueryAction;
|
||||
import cc.carm.lib.easysql.api.action.query.SQLQuery;
|
||||
import cc.carm.lib.easysql.api.function.SQLExceptionHandler;
|
||||
import cc.carm.lib.easysql.api.function.SQLHandler;
|
||||
import cc.carm.lib.easysql.manager.SQLManagerImpl;
|
||||
import cc.carm.lib.easysql.query.SQLQueryImpl;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@@ -12,8 +13,6 @@ import java.sql.Connection;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public class QueryActionImpl extends AbstractSQLAction<SQLQuery> implements QueryAction {
|
||||
|
||||
@@ -37,11 +36,11 @@ public class QueryActionImpl extends AbstractSQLAction<SQLQuery> implements Quer
|
||||
|
||||
|
||||
@Override
|
||||
public void executeAsync(Consumer<SQLQuery> success, BiConsumer<SQLException, SQLAction<SQLQuery>> failure) {
|
||||
public void executeAsync(SQLHandler<SQLQuery> success, SQLExceptionHandler failure) {
|
||||
try (SQLQueryImpl query = execute()) {
|
||||
if (success != null) success.accept(query);
|
||||
} catch (SQLException exception) {
|
||||
(exceptionHandler == null ? defaultExceptionHandler() : exceptionHandler).accept(exception, this);
|
||||
handleException(failure, exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -70,7 +70,7 @@ public class SQLQueryImpl implements SQLQuery {
|
||||
);
|
||||
getManager().getActiveQuery().remove(getAction().getActionUUID());
|
||||
} catch (SQLException e) {
|
||||
getAction().handleException(e);
|
||||
getAction().handleException(getAction().defaultExceptionHandler(), e);
|
||||
}
|
||||
this.queryAction = null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user