1
mirror of https://github.com/CarmJos/EasySQL.git synced 2026-06-04 23:41:15 +08:00

[v0.2.7] [U] 进一步简化操作使用逻辑

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