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

实装 executeAsync 功能,修改异常处理方式,提供action方便获取相关信息。

This commit is contained in:
2021-12-14 16:11:22 +08:00
parent 6de493afbc
commit d7db2fbb52
18 changed files with 59 additions and 61 deletions
+1 -1
View File
@@ -5,7 +5,7 @@
<parent>
<artifactId>easysql-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>v0.0.1</version>
<version>0.2.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
@@ -3,9 +3,11 @@ package cc.carm.lib.easysql.action;
import cc.carm.lib.easysql.api.SQLAction;
import cc.carm.lib.easysql.manager.SQLManagerImpl;
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;
public abstract class AbstractSQLAction<T> implements SQLAction<T> {
@@ -17,7 +19,7 @@ public abstract class AbstractSQLAction<T> implements SQLAction<T> {
protected @NotNull String sqlContent;
protected @NotNull Consumer<SQLException> exceptionHandler = defaultExceptionHandler();
protected @Nullable BiConsumer<SQLException, SQLAction<T>> exceptionHandler = null;
public AbstractSQLAction(@NotNull SQLManagerImpl manager, @NotNull String sql) {
this(manager, sql, System.currentTimeMillis());
@@ -69,14 +71,25 @@ public abstract class AbstractSQLAction<T> implements SQLAction<T> {
getManager().debug("#" + getShortID() + " ->" + getSQLContent());
}
@Override
public SQLAction<T> handleException(Consumer<SQLException> handler) {
this.exceptionHandler = handler;
return this;
public void handleException(SQLException exception) {
if (this.exceptionHandler == null) {
defaultExceptionHandler().accept(exception, this);
} else {
this.exceptionHandler.accept(exception, this);
}
}
@NotNull
public Consumer<SQLException> getExceptionHandler() {
return exceptionHandler;
@Override
public void executeAsync(Consumer<T> success, BiConsumer<SQLException, SQLAction<T>> failure) {
getManager().getExecutorPool().submit(() -> {
try {
T returnedValue = execute();
if (success != null) success.accept(returnedValue);
} catch (SQLException e) {
(failure == null ? defaultExceptionHandler() : failure).accept(e, this);
}
});
}
}
@@ -72,8 +72,4 @@ public class PreparedSQLBatchUpdateActionImpl extends SQLUpdateBatchActionImpl i
return returnedValues;
}
@Override
public void executeAsync(Consumer<List<Integer>> success, Consumer<SQLException> failure) {
}
}
@@ -11,7 +11,6 @@ import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.function.Consumer;
public class PreparedSQLUpdateActionImpl extends SQLUpdateActionImpl implements PreparedSQLUpdateAction {
@@ -63,9 +62,4 @@ public class PreparedSQLUpdateActionImpl extends SQLUpdateActionImpl implements
return value;
}
@Override
public void executeAsync(Consumer<Integer> success, Consumer<SQLException> failure) {
}
}
@@ -43,10 +43,6 @@ public class SQLUpdateActionImpl extends AbstractSQLAction<Integer> implements S
return returnedValue;
}
@Override
public void executeAsync(Consumer<Integer> success, Consumer<SQLException> failure) {
}
@Override
public SQLUpdateActionImpl setKeyIndex(int keyIndex) {
@@ -10,7 +10,6 @@ import java.sql.Statement;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Collectors;
public class SQLUpdateBatchActionImpl extends AbstractSQLAction<List<Integer>> implements SQLUpdateBatchAction {
@@ -52,10 +51,4 @@ public class SQLUpdateBatchActionImpl extends AbstractSQLAction<List<Integer>> i
return returnedValues;
}
@Override
public void executeAsync(Consumer<List<Integer>> success, Consumer<SQLException> failure) {
}
}
@@ -1,6 +1,5 @@
package cc.carm.lib.easysql.action.query;
import cc.carm.lib.easysql.api.SQLQuery;
import cc.carm.lib.easysql.api.action.query.PreparedQueryAction;
import cc.carm.lib.easysql.manager.SQLManagerImpl;
import cc.carm.lib.easysql.query.SQLQueryImpl;
@@ -68,9 +67,4 @@ public class PreparedQueryActionImpl extends QueryActionImpl implements Prepared
return new SQLQueryImpl(getManager(), this, connection, preparedStatement, resultSet);
}
@Override
public void executeAsync(Consumer<SQLQuery> success, Consumer<SQLException> failure) {
}
}
@@ -32,9 +32,4 @@ public class QueryActionImpl extends AbstractSQLAction<SQLQuery> implements Quer
return query;
}
@Override
public void executeAsync(Consumer<SQLQuery> success, Consumer<SQLException> failure) {
}
}
@@ -19,6 +19,8 @@ import java.sql.Connection;
import java.sql.SQLException;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.logging.Logger;
public class SQLManagerImpl implements SQLManager {
@@ -29,13 +31,21 @@ public class SQLManagerImpl implements SQLManager {
boolean debug = false;
protected ExecutorService executorPool;
public SQLManagerImpl(@NotNull DataSource dataSource) {
this(dataSource, null);
}
public SQLManagerImpl(@NotNull DataSource dataSource, @Nullable String name) {
this.LOGGER = Logger.getLogger("SQLManager" + (name != null ? "#" + name : ""));
String managerName = "SQLManager" + (name != null ? "#" + name : "");
this.LOGGER = Logger.getLogger(managerName);
this.dataSource = dataSource;
this.executorPool = Executors.newFixedThreadPool(3, r -> {
Thread thread = new Thread(r, managerName);
thread.setDaemon(true);
return thread;
});
}
@Override
@@ -56,6 +66,9 @@ public class SQLManagerImpl implements SQLManager {
return LOGGER;
}
public ExecutorService getExecutorPool() {
return executorPool;
}
@Override
public @NotNull DataSource getDataSource() {
@@ -70,7 +70,7 @@ public class SQLQueryImpl implements SQLQuery {
);
getManager().getActiveQuery().remove(getAction().getActionUUID());
} catch (SQLException e) {
getAction().getExceptionHandler().accept(e);
getAction().handleException(e);
}
this.queryAction = null;
}
@@ -1,5 +1,7 @@
package cc.carm.lib.easysql.util;
import cc.carm.lib.easysql.api.SQLAction;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.*;