mirror of
https://github.com/CarmJos/EasySQL.git
synced 2024-09-19 21:35:47 +00:00
Compare commits
4 Commits
c079c98e3e
...
e9ce0a769c
Author | SHA1 | Date | |
---|---|---|---|
e9ce0a769c | |||
e98e9586ef | |||
6cd080210f | |||
20ac8f3908 |
@ -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>0.3.12</version>
|
<version>0.3.15</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
@ -5,9 +5,10 @@ import cc.carm.lib.easysql.api.SQLQuery;
|
|||||||
import cc.carm.lib.easysql.api.action.PreparedSQLUpdateAction;
|
import cc.carm.lib.easysql.api.action.PreparedSQLUpdateAction;
|
||||||
import cc.carm.lib.easysql.api.action.PreparedSQLUpdateBatchAction;
|
import cc.carm.lib.easysql.api.action.PreparedSQLUpdateBatchAction;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -24,7 +25,7 @@ public interface SQLDebugHandler {
|
|||||||
* @param params 执行传入的参数列表。
|
* @param params 执行传入的参数列表。
|
||||||
* 实际上,仅有 {@link PreparedSQLUpdateAction} 和 {@link PreparedSQLUpdateBatchAction} 才会有传入参数。
|
* 实际上,仅有 {@link PreparedSQLUpdateAction} 和 {@link PreparedSQLUpdateBatchAction} 才会有传入参数。
|
||||||
*/
|
*/
|
||||||
void beforeExecute(@NotNull SQLAction<?> action, @NotNull List<Object[]> params);
|
void beforeExecute(@NotNull SQLAction<?> action, @NotNull List<@Nullable Object[]> params);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 该方法将在 {@link SQLQuery#close()} 执行后调用。
|
* 该方法将在 {@link SQLQuery#close()} 执行后调用。
|
||||||
@ -35,11 +36,23 @@ public interface SQLDebugHandler {
|
|||||||
*/
|
*/
|
||||||
void afterQuery(@NotNull SQLQuery query, long executeTime, long closeTime);
|
void afterQuery(@NotNull SQLQuery query, long executeTime, long closeTime);
|
||||||
|
|
||||||
|
default String parseParams(@Nullable Object[] params) {
|
||||||
|
if (params == null) return "<#NULL>";
|
||||||
|
else if (params.length == 0) return "<#EMPTY>";
|
||||||
|
|
||||||
|
List<String> paramsString = new ArrayList<>();
|
||||||
|
for (Object param : params) {
|
||||||
|
if (param == null) paramsString.add("NULL");
|
||||||
|
else paramsString.add(param.toString());
|
||||||
|
}
|
||||||
|
return String.join(", ", paramsString);
|
||||||
|
}
|
||||||
|
|
||||||
@SuppressWarnings("DuplicatedCode")
|
@SuppressWarnings("DuplicatedCode")
|
||||||
static SQLDebugHandler defaultHandler(Logger logger) {
|
static SQLDebugHandler defaultHandler(Logger logger) {
|
||||||
return new SQLDebugHandler() {
|
return new SQLDebugHandler() {
|
||||||
@Override
|
@Override
|
||||||
public void beforeExecute(@NotNull SQLAction<?> action, @NotNull List<Object[]> params) {
|
public void beforeExecute(@NotNull SQLAction<?> action, @NotNull List<@Nullable Object[]> params) {
|
||||||
logger.info("┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
|
logger.info("┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
|
||||||
logger.info("┣# ActionUUID: {}", action.getActionUUID());
|
logger.info("┣# ActionUUID: {}", action.getActionUUID());
|
||||||
logger.info("┣# ActionType: {}", action.getClass().getName());
|
logger.info("┣# ActionType: {}", action.getClass().getName());
|
||||||
@ -54,12 +67,12 @@ public interface SQLDebugHandler {
|
|||||||
}
|
}
|
||||||
if (params.size() == 1) {
|
if (params.size() == 1) {
|
||||||
Object[] param = params.get(0);
|
Object[] param = params.get(0);
|
||||||
logger.info("┣# SQLParams({}): {}", param.length, Arrays.stream(param).map(Object::toString).reduce((a, b) -> a + ", " + b).orElse(""));
|
logger.info("┣# SQLParams: {}", parseParams(param));
|
||||||
} else if (params.size() > 1) {
|
} else if (params.size() > 1) {
|
||||||
logger.info("┣# SQLParams: ");
|
logger.info("┣# SQLParams: ");
|
||||||
int i = 0;
|
int i = 0;
|
||||||
for (Object[] param : params) {
|
for (Object[] param : params) {
|
||||||
logger.info("┃ [{}] {}", ++i, Arrays.stream(param).map(Object::toString).reduce((a, b) -> a + ", " + b).orElse(""));
|
logger.info("┃ [{}] {}", ++i, parseParams(param));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
logger.info("┣# createTime: {}", action.getCreateTime());
|
logger.info("┣# createTime: {}", action.getCreateTime());
|
||||||
@ -71,7 +84,7 @@ public interface SQLDebugHandler {
|
|||||||
logger.info("┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
|
logger.info("┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
|
||||||
logger.info("┣# ActionUUID: {}", query.getAction().getActionUUID());
|
logger.info("┣# ActionUUID: {}", query.getAction().getActionUUID());
|
||||||
logger.info("┣# SQLContent: {}", query.getSQLContent());
|
logger.info("┣# SQLContent: {}", query.getSQLContent());
|
||||||
logger.info("┣# executeCote: {} ms", (closeTime - executeTime));
|
logger.info("┣# executeCost: {} ms", (closeTime - executeTime));
|
||||||
logger.info("┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
|
logger.info("┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -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>0.3.12</version>
|
<version>0.3.15</version>
|
||||||
<relativePath>../../pom.xml</relativePath>
|
<relativePath>../../pom.xml</relativePath>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
@ -17,6 +17,8 @@
|
|||||||
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
|
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
|
||||||
<maven.javadoc.skip>true</maven.javadoc.skip>
|
<maven.javadoc.skip>true</maven.javadoc.skip>
|
||||||
<maven.deploy.skip>true</maven.deploy.skip>
|
<maven.deploy.skip>true</maven.deploy.skip>
|
||||||
|
|
||||||
|
<log4j.version>2.17.2</log4j.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<artifactId>easysql-demo</artifactId>
|
<artifactId>easysql-demo</artifactId>
|
||||||
@ -80,6 +82,27 @@
|
|||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.logging.log4j</groupId>
|
||||||
|
<artifactId>log4j-api</artifactId>
|
||||||
|
<version>${log4j.version}</version>
|
||||||
|
<scope>compile</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.logging.log4j</groupId>
|
||||||
|
<artifactId>log4j-core</artifactId>
|
||||||
|
<version>${log4j.version}</version>
|
||||||
|
<scope>compile</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.logging.log4j</groupId>
|
||||||
|
<artifactId>log4j-slf4j-impl</artifactId>
|
||||||
|
<version>${log4j.version}</version>
|
||||||
|
<scope>compile</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
@ -22,6 +22,7 @@ public class EasySQLTest {
|
|||||||
config.setJdbcUrl("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;MODE=MYSQL;");
|
config.setJdbcUrl("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;MODE=MYSQL;");
|
||||||
|
|
||||||
SQLManager sqlManager = new SQLManagerImpl(new HikariDataSource(config), "test");
|
SQLManager sqlManager = new SQLManagerImpl(new HikariDataSource(config), "test");
|
||||||
|
sqlManager.setDebugMode(true);
|
||||||
|
|
||||||
print("加载测试类...");
|
print("加载测试类...");
|
||||||
Set<TestHandler> tests = new LinkedHashSet<>();
|
Set<TestHandler> tests = new LinkedHashSet<>();
|
||||||
|
@ -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>0.3.12</version>
|
<version>0.3.15</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
@ -69,7 +69,11 @@ public abstract class AbstractSQLAction<T> implements SQLAction<T> {
|
|||||||
|
|
||||||
protected void debugMessage(List<Object[]> params) {
|
protected void debugMessage(List<Object[]> params) {
|
||||||
if (getManager().isDebugMode()) {
|
if (getManager().isDebugMode()) {
|
||||||
|
try {
|
||||||
getManager().getDebugHandler().beforeExecute(this, params);
|
getManager().getDebugHandler().beforeExecute(this, params);
|
||||||
|
} catch (Exception exception) {
|
||||||
|
exception.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -19,7 +19,7 @@ public class PreparedSQLBatchUpdateActionImpl
|
|||||||
implements PreparedSQLUpdateBatchAction {
|
implements PreparedSQLUpdateBatchAction {
|
||||||
|
|
||||||
boolean returnKeys = false;
|
boolean returnKeys = false;
|
||||||
List<Object[]> allParams;
|
@NotNull List<Object[]> allParams;
|
||||||
|
|
||||||
public PreparedSQLBatchUpdateActionImpl(@NotNull SQLManagerImpl manager, @NotNull String sql) {
|
public PreparedSQLBatchUpdateActionImpl(@NotNull SQLManagerImpl manager, @NotNull String sql) {
|
||||||
super(manager, sql);
|
super(manager, sql);
|
||||||
|
@ -72,7 +72,11 @@ public class SQLQueryImpl implements SQLQuery {
|
|||||||
if (getConnection() != null && !getConnection().isClosed()) getConnection().close();
|
if (getConnection() != null && !getConnection().isClosed()) getConnection().close();
|
||||||
|
|
||||||
if (getManager().isDebugMode()) {
|
if (getManager().isDebugMode()) {
|
||||||
|
try {
|
||||||
getManager().getDebugHandler().afterQuery(this, getExecuteTime(), System.currentTimeMillis());
|
getManager().getDebugHandler().afterQuery(this, getExecuteTime(), System.currentTimeMillis());
|
||||||
|
} catch (Exception ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
getManager().getActiveQuery().remove(getAction().getActionUUID());
|
getManager().getActiveQuery().remove(getAction().getActionUUID());
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
|
2
pom.xml
2
pom.xml
@ -17,7 +17,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>0.3.12</version>
|
<version>0.3.15</version>
|
||||||
|
|
||||||
<modules>
|
<modules>
|
||||||
<module>api</module>
|
<module>api</module>
|
||||||
|
@ -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>0.3.12</version>
|
<version>0.3.15</version>
|
||||||
<relativePath>../../pom.xml</relativePath>
|
<relativePath>../../pom.xml</relativePath>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
@ -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>0.3.12</version>
|
<version>0.3.15</version>
|
||||||
<relativePath>../../pom.xml</relativePath>
|
<relativePath>../../pom.xml</relativePath>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
Loading…
Reference in New Issue
Block a user