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

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

This commit is contained in:
Carm Jos 2021-12-14 16:11:22 +08:00
parent 6de493afbc
commit d7db2fbb52
18 changed files with 59 additions and 61 deletions

View File

@ -5,7 +5,7 @@
<parent> <parent>
<groupId>cc.carm.lib</groupId> <groupId>cc.carm.lib</groupId>
<artifactId>easysql-parent</artifactId> <artifactId>easysql-parent</artifactId>
<version>v0.0.1</version> <version>0.2.0</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@ -5,6 +5,7 @@ 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; import java.util.function.Consumer;
public interface SQLAction<T> { public interface SQLAction<T> {
@ -22,13 +23,13 @@ public interface SQLAction<T> {
@NotNull T execute() throws SQLException; @NotNull T execute() throws SQLException;
@Nullable @Nullable
default T execute(@Nullable Consumer<SQLException> exceptionHandler) { default T execute(@Nullable BiConsumer<SQLException, SQLAction<T>> exceptionHandler) {
if (exceptionHandler == null) exceptionHandler = defaultExceptionHandler(); if (exceptionHandler == null) exceptionHandler = defaultExceptionHandler();
T value = null; T value = null;
try { try {
value = execute(); value = execute();
} catch (SQLException exception) { } catch (SQLException exception) {
exceptionHandler.accept(exception); exceptionHandler.accept(exception, this);
} }
return value; return value;
} }
@ -37,22 +38,16 @@ public interface SQLAction<T> {
executeAsync(null); executeAsync(null);
} }
default void executeAsync(Consumer<T> success) { default void executeAsync(@Nullable Consumer<T> success) {
executeAsync(success, null); executeAsync(success, null);
} }
void executeAsync(Consumer<T> success, Consumer<SQLException> failure); void executeAsync(@Nullable Consumer<T> success, @Nullable BiConsumer<SQLException, SQLAction<T>> failure);
SQLAction<T> handleException(Consumer<SQLException> failure); default BiConsumer<SQLException, SQLAction<T>> defaultExceptionHandler() {
return (exception, action) -> {
@NotNull Consumer<SQLException> getExceptionHandler(); getManager().getLogger().severe("Error when execute [" + action.getSQLContent() + "]");
getManager().getLogger().severe(exception.getLocalizedMessage());
default Consumer<SQLException> defaultExceptionHandler() {
return Throwable::printStackTrace;
}
default Consumer<T> defaultResultHandler() {
return t -> {
}; };
} }

View File

@ -12,9 +12,12 @@ import java.sql.SQLException;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.UUID; import java.util.UUID;
import java.util.logging.Logger;
public interface SQLManager { public interface SQLManager {
Logger getLogger();
boolean isDebugMode(); boolean isDebugMode();
void setDebugMode(boolean enable); void setDebugMode(boolean enable);

View File

@ -0,0 +1,4 @@
package cc.carm.lib.easysql.api.builder;
public interface UpsertBuilder {
}

View File

@ -5,7 +5,7 @@
<parent> <parent>
<artifactId>easysql-parent</artifactId> <artifactId>easysql-parent</artifactId>
<groupId>cc.carm.lib</groupId> <groupId>cc.carm.lib</groupId>
<version>v0.0.1</version> <version>0.2.0</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent> <parent>
<artifactId>easysql-parent</artifactId> <artifactId>easysql-parent</artifactId>
<groupId>cc.carm.lib</groupId> <groupId>cc.carm.lib</groupId>
<version>v0.0.1</version> <version>0.2.0</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent> <parent>
<artifactId>easysql-parent</artifactId> <artifactId>easysql-parent</artifactId>
<groupId>cc.carm.lib</groupId> <groupId>cc.carm.lib</groupId>
<version>v0.0.1</version> <version>0.2.0</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@ -3,9 +3,11 @@ 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.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 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; import java.util.function.Consumer;
public abstract class AbstractSQLAction<T> implements SQLAction<T> { 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 String sqlContent;
protected @NotNull Consumer<SQLException> exceptionHandler = defaultExceptionHandler(); protected @Nullable BiConsumer<SQLException, SQLAction<T>> exceptionHandler = null;
public AbstractSQLAction(@NotNull SQLManagerImpl manager, @NotNull String sql) { public AbstractSQLAction(@NotNull SQLManagerImpl manager, @NotNull String sql) {
this(manager, sql, System.currentTimeMillis()); this(manager, sql, System.currentTimeMillis());
@ -69,14 +71,25 @@ public abstract class AbstractSQLAction<T> implements SQLAction<T> {
getManager().debug("#" + getShortID() + " ->" + getSQLContent()); getManager().debug("#" + getShortID() + " ->" + getSQLContent());
} }
@Override public void handleException(SQLException exception) {
public SQLAction<T> handleException(Consumer<SQLException> handler) { if (this.exceptionHandler == null) {
this.exceptionHandler = handler; defaultExceptionHandler().accept(exception, this);
return this; } else {
this.exceptionHandler.accept(exception, this);
}
} }
@NotNull @Override
public Consumer<SQLException> getExceptionHandler() { public void executeAsync(Consumer<T> success, BiConsumer<SQLException, SQLAction<T>> failure) {
return exceptionHandler; getManager().getExecutorPool().submit(() -> {
try {
T returnedValue = execute();
if (success != null) success.accept(returnedValue);
} catch (SQLException e) {
(failure == null ? defaultExceptionHandler() : failure).accept(e, this);
}
});
} }
} }

View File

@ -72,8 +72,4 @@ public class PreparedSQLBatchUpdateActionImpl extends SQLUpdateBatchActionImpl i
return returnedValues; return returnedValues;
} }
@Override
public void executeAsync(Consumer<List<Integer>> success, Consumer<SQLException> failure) {
}
} }

View File

@ -11,7 +11,6 @@ import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.List; import java.util.List;
import java.util.function.Consumer;
public class PreparedSQLUpdateActionImpl extends SQLUpdateActionImpl implements PreparedSQLUpdateAction { public class PreparedSQLUpdateActionImpl extends SQLUpdateActionImpl implements PreparedSQLUpdateAction {
@ -63,9 +62,4 @@ public class PreparedSQLUpdateActionImpl extends SQLUpdateActionImpl implements
return value; return value;
} }
@Override
public void executeAsync(Consumer<Integer> success, Consumer<SQLException> failure) {
}
} }

View File

@ -43,10 +43,6 @@ public class SQLUpdateActionImpl extends AbstractSQLAction<Integer> implements S
return returnedValue; return returnedValue;
} }
@Override
public void executeAsync(Consumer<Integer> success, Consumer<SQLException> failure) {
}
@Override @Override
public SQLUpdateActionImpl setKeyIndex(int keyIndex) { public SQLUpdateActionImpl setKeyIndex(int keyIndex) {

View File

@ -10,7 +10,6 @@ import java.sql.Statement;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.function.Consumer;
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 {
@ -52,10 +51,4 @@ public class SQLUpdateBatchActionImpl extends AbstractSQLAction<List<Integer>> i
return returnedValues; return returnedValues;
} }
@Override
public void executeAsync(Consumer<List<Integer>> success, Consumer<SQLException> failure) {
}
} }

View File

@ -1,6 +1,5 @@
package cc.carm.lib.easysql.action.query; 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.api.action.query.PreparedQueryAction;
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;
@ -68,9 +67,4 @@ public class PreparedQueryActionImpl extends QueryActionImpl implements Prepared
return new SQLQueryImpl(getManager(), this, connection, preparedStatement, resultSet); return new SQLQueryImpl(getManager(), this, connection, preparedStatement, resultSet);
} }
@Override
public void executeAsync(Consumer<SQLQuery> success, Consumer<SQLException> failure) {
}
} }

View File

@ -32,9 +32,4 @@ public class QueryActionImpl extends AbstractSQLAction<SQLQuery> implements Quer
return query; return query;
} }
@Override
public void executeAsync(Consumer<SQLQuery> success, Consumer<SQLException> failure) {
}
} }

View File

@ -19,6 +19,8 @@ import java.sql.Connection;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.*; import java.util.*;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.logging.Logger; import java.util.logging.Logger;
public class SQLManagerImpl implements SQLManager { public class SQLManagerImpl implements SQLManager {
@ -29,13 +31,21 @@ public class SQLManagerImpl implements SQLManager {
boolean debug = false; boolean debug = false;
protected ExecutorService executorPool;
public SQLManagerImpl(@NotNull DataSource dataSource) { public SQLManagerImpl(@NotNull DataSource dataSource) {
this(dataSource, null); this(dataSource, null);
} }
public SQLManagerImpl(@NotNull DataSource dataSource, @Nullable String name) { 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.dataSource = dataSource;
this.executorPool = Executors.newFixedThreadPool(3, r -> {
Thread thread = new Thread(r, managerName);
thread.setDaemon(true);
return thread;
});
} }
@Override @Override
@ -56,6 +66,9 @@ public class SQLManagerImpl implements SQLManager {
return LOGGER; return LOGGER;
} }
public ExecutorService getExecutorPool() {
return executorPool;
}
@Override @Override
public @NotNull DataSource getDataSource() { public @NotNull DataSource getDataSource() {

View File

@ -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().getExceptionHandler().accept(e); getAction().handleException(e);
} }
this.queryAction = null; this.queryAction = null;
} }

View File

@ -1,5 +1,7 @@
package cc.carm.lib.easysql.util; package cc.carm.lib.easysql.util;
import cc.carm.lib.easysql.api.SQLAction;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.BigInteger; import java.math.BigInteger;
import java.sql.*; import java.sql.*;

View File

@ -7,7 +7,7 @@
<groupId>cc.carm.lib</groupId> <groupId>cc.carm.lib</groupId>
<artifactId>easysql-parent</artifactId> <artifactId>easysql-parent</artifactId>
<packaging>pom</packaging> <packaging>pom</packaging>
<version>v0.0.1</version> <version>0.2.0</version>
<modules> <modules>
<module>easysql-api</module> <module>easysql-api</module>