1
mirror of https://github.com/CarmJos/EasySQL.git synced 2026-06-05 00:25:32 +08:00

[0.3.12] 版本更新 (破坏性)

- `[R]` 采用 slf4j-api 替代Java原生的Logger库。
- `[A]` 新增 SQLDebugHandler 用于更好的处理调试消息。
This commit is contained in:
2022-04-13 06:30:20 +08:00
parent 03e157d3d9
commit 18dd618c21
21 changed files with 187 additions and 44 deletions
@@ -7,6 +7,7 @@ import cc.carm.lib.easysql.manager.SQLManagerImpl;
import org.jetbrains.annotations.NotNull;
import java.sql.SQLException;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
@@ -66,8 +67,10 @@ public abstract class AbstractSQLAction<T> implements SQLAction<T> {
return this.sqlManager;
}
protected void outputDebugMessage() {
getManager().debug("# " + getShortID() + " -> { " + getSQLContent() + " }");
protected void debugMessage(List<Object[]> params) {
if (getManager().isDebugMode()) {
getManager().getDebugHandler().beforeExecute(this, params);
}
}
@Override
@@ -48,12 +48,13 @@ public class PreparedSQLBatchUpdateActionImpl
@Override
public @NotNull List<Long> execute() throws SQLException {
debugMessage(allParams);
try (Connection connection = getManager().getConnection()) {
try (PreparedStatement statement = StatementUtil.createPrepareStatementBatch(
connection, getSQLContent(), allParams, returnKeys
)) {
outputDebugMessage();
int[] executed = statement.executeBatch();
if (!returnKeys) {
@@ -11,6 +11,7 @@ import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class PreparedSQLUpdateActionImpl
@@ -53,14 +54,14 @@ public class PreparedSQLUpdateActionImpl
@Override
public @NotNull Long execute() throws SQLException {
debugMessage(Collections.singletonList(params));
try (Connection connection = getManager().getConnection()) {
try (PreparedStatement statement = StatementUtil.createPrepareStatement(
connection, getSQLContent(), params, returnGeneratedKeys
)) {
outputDebugMessage();
int changes = statement.executeUpdate();
if (!returnGeneratedKeys) return (long) changes;
else {
@@ -8,6 +8,7 @@ import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
public class SQLUpdateActionImpl
extends AbstractSQLAction<Long>
@@ -21,9 +22,10 @@ public class SQLUpdateActionImpl
@Override
public @NotNull Long execute() throws SQLException {
debugMessage(new ArrayList<>());
try (Connection connection = getManager().getConnection()) {
try (Statement statement = connection.createStatement()) {
outputDebugMessage();
if (!returnGeneratedKeys) {
return (long) statement.executeUpdate(getSQLContent());
@@ -38,10 +38,11 @@ public class SQLUpdateBatchActionImpl
@Override
public @NotNull List<Integer> execute() throws SQLException {
debugMessage(new ArrayList<>());
try (Connection connection = getManager().getConnection()) {
try (Statement statement = connection.createStatement()) {
outputDebugMessage();
for (String content : this.sqlContents) {
statement.addBatch(content);
@@ -55,12 +56,4 @@ public class SQLUpdateBatchActionImpl
}
}
@Override
protected void outputDebugMessage() {
getManager().debug("# " + getShortID() + " -> [");
for (String content : getSQLContents()) getManager().debug(String.format(" { %s }", content));
getManager().debug("]");
}
}
@@ -11,6 +11,7 @@ import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;
@@ -49,8 +50,8 @@ public class PreparedQueryActionImpl extends QueryActionImpl implements Prepared
@Override
public @NotNull SQLQueryImpl execute() throws SQLException {
outputDebugMessage();
debugMessage(Collections.singletonList(params));
Connection connection = getManager().getConnection();
PreparedStatement preparedStatement;
try {
@@ -74,7 +75,6 @@ public class PreparedQueryActionImpl extends QueryActionImpl implements Prepared
executeTime
);
getManager().getActiveQuery().put(getActionUUID(), query);
return query;
} catch (SQLException exception) {
preparedStatement.close();
@@ -12,6 +12,7 @@ import org.jetbrains.annotations.NotNull;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
public class QueryActionImpl extends AbstractSQLAction<SQLQuery> implements QueryAction {
@@ -21,6 +22,7 @@ public class QueryActionImpl extends AbstractSQLAction<SQLQuery> implements Quer
@Override
public @NotNull SQLQueryImpl execute() throws SQLException {
debugMessage(new ArrayList<>());
Connection connection = getManager().getConnection();
Statement statement;
@@ -32,7 +34,6 @@ public class QueryActionImpl extends AbstractSQLAction<SQLQuery> implements Quer
throw ex;
}
outputDebugMessage();
try {
long executeTime = System.currentTimeMillis();
SQLQueryImpl query = new SQLQueryImpl(
@@ -10,6 +10,8 @@ import cc.carm.lib.easysql.builder.AbstractSQLBuilder;
import cc.carm.lib.easysql.manager.SQLManagerImpl;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Objects;
public class QueryBuilderImpl extends AbstractSQLBuilder implements QueryBuilder {
@@ -10,10 +10,13 @@ import cc.carm.lib.easysql.api.action.PreparedSQLUpdateAction;
import cc.carm.lib.easysql.api.action.PreparedSQLUpdateBatchAction;
import cc.carm.lib.easysql.api.action.SQLUpdateBatchAction;
import cc.carm.lib.easysql.api.builder.*;
import cc.carm.lib.easysql.api.function.SQLDebugHandler;
import cc.carm.lib.easysql.api.function.SQLExceptionHandler;
import cc.carm.lib.easysql.builder.impl.*;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.sql.DataSource;
import java.sql.Connection;
@@ -23,7 +26,6 @@ import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.Supplier;
import java.util.logging.Logger;
public class SQLManagerImpl implements SQLManager {
@@ -34,14 +36,19 @@ public class SQLManagerImpl implements SQLManager {
@NotNull Supplier<Boolean> debugMode = () -> Boolean.FALSE;
@NotNull SQLExceptionHandler exceptionHandler;
@NotNull SQLDebugHandler debugHandler;
public SQLManagerImpl(@NotNull DataSource dataSource) {
this(dataSource, null);
}
public SQLManagerImpl(@NotNull DataSource dataSource, @Nullable String name) {
this(dataSource, LoggerFactory.getLogger(SQLManagerImpl.class), name);
}
public SQLManagerImpl(@NotNull DataSource dataSource, @NotNull Logger logger, @Nullable String name) {
String managerName = "SQLManager" + (name != null ? "#" + name : "");
this.LOGGER = Logger.getLogger(managerName);
this.LOGGER = logger;
this.dataSource = dataSource;
this.executorPool = Executors.newFixedThreadPool(3, r -> {
Thread thread = new Thread(r, managerName);
@@ -50,6 +57,7 @@ public class SQLManagerImpl implements SQLManager {
});
this.exceptionHandler = SQLExceptionHandler.detailed(getLogger());
this.debugHandler = SQLDebugHandler.defaultHandler(getLogger());
}
@Override
@@ -62,6 +70,16 @@ public class SQLManagerImpl implements SQLManager {
this.debugMode = debugMode;
}
@Override
public @NotNull SQLDebugHandler getDebugHandler() {
return this.debugHandler;
}
@Override
public void setDebugHandler(@NotNull SQLDebugHandler debugHandler) {
this.debugHandler = debugHandler;
}
public void debug(String msg) {
if (isDebugMode()) getLogger().info("[DEBUG] " + msg);
}
@@ -71,9 +71,9 @@ public class SQLQueryImpl implements SQLQuery {
if (getStatement() != null && !getStatement().isClosed()) getStatement().close();
if (getConnection() != null && !getConnection().isClosed()) getConnection().close();
getManager().debug("#" + getAction().getShortID() +
" -> finished after " + (System.currentTimeMillis() - getExecuteTime()) + " ms."
);
if (getManager().isDebugMode()) {
getManager().getDebugHandler().afterQuery(this, getExecuteTime(), System.currentTimeMillis());
}
getManager().getActiveQuery().remove(getAction().getActionUUID());
} catch (SQLException e) {
getAction().handleException(getAction().defaultExceptionHandler(), e);