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

[v0.2.8] [R] 使用Supplier获取是否为Debug模式。

This commit is contained in:
Carm Jos 2022-01-24 15:25:12 +08:00
parent edadc50c22
commit 43baf4aa24
8 changed files with 266 additions and 259 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>0.2.7</version> <version>0.2.8</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@ -14,149 +14,154 @@ 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.function.Supplier;
import java.util.logging.Logger; import java.util.logging.Logger;
public interface SQLManager { public interface SQLManager {
Logger getLogger(); Logger getLogger();
boolean isDebugMode(); boolean isDebugMode();
void setDebugMode(boolean enable); void setDebugMode(@NotNull Supplier<@NotNull Boolean> debugMode);
/** default void setDebugMode(boolean enable) {
* 得到连接池源 setDebugMode(() -> enable);
* }
* @return DataSource
*/ /**
@NotNull DataSource getDataSource(); * 得到连接池源
*
* @return DataSource
*/
@NotNull DataSource getDataSource();
/** /**
* 得到一个数据库连接实例 * 得到一个数据库连接实例
* *
* @return Connection * @return Connection
* @throws SQLException {@link DataSource#getConnection()} * @throws SQLException {@link DataSource#getConnection()}
*/ */
@NotNull Connection getConnection() throws SQLException; @NotNull Connection getConnection() throws SQLException;
/** /**
* 得到正使用的查询 * 得到正使用的查询
* *
* @return 查询列表 * @return 查询列表
*/ */
@NotNull Map<UUID, SQLQuery> getActiveQuery(); @NotNull Map<UUID, SQLQuery> getActiveQuery();
/** /**
* 执行一条不需要返回结果的SQL语句(多用于UPDATEREPLACEDELETE方法) * 执行一条不需要返回结果的SQL语句(多用于UPDATEREPLACEDELETE方法)
* 该方法使用 Statement 实现请注意SQL注入风险 * 该方法使用 Statement 实现请注意SQL注入风险
* *
* @param sql SQL语句内容 * @param sql SQL语句内容
* @return 更新的行数 * @return 更新的行数
* @see SQLUpdateAction * @see SQLUpdateAction
*/ */
@Nullable Integer executeSQL(String sql); @Nullable Integer executeSQL(String sql);
/** /**
* 执行一条不需要返回结果的预处理SQL更改(UPDATEREPLACEDELETE) * 执行一条不需要返回结果的预处理SQL更改(UPDATEREPLACEDELETE)
* *
* @param sql SQL语句内容 * @param sql SQL语句内容
* @param params SQL语句中 ? 的对应参数 * @param params SQL语句中 ? 的对应参数
* @return 更新的行数 * @return 更新的行数
* @see PreparedSQLUpdateAction * @see PreparedSQLUpdateAction
*/ */
@Nullable Integer executeSQL(String sql, Object[] params); @Nullable Integer executeSQL(String sql, Object[] params);
/** /**
* 执行多条不需要返回结果的SQL更改(UPDATEREPLACEDELETE) * 执行多条不需要返回结果的SQL更改(UPDATEREPLACEDELETE)
* *
* @param sql SQL语句内容 * @param sql SQL语句内容
* @param paramsBatch SQL语句中对应?的参数组 * @param paramsBatch SQL语句中对应?的参数组
* @return 对应参数返回的行数 * @return 对应参数返回的行数
* @see PreparedSQLUpdateBatchAction * @see PreparedSQLUpdateBatchAction
*/ */
@Nullable List<Integer> executeSQLBatch(String sql, Iterable<Object[]> paramsBatch); @Nullable List<Integer> executeSQLBatch(String sql, Iterable<Object[]> paramsBatch);
/** /**
* 执行多条不需要返回结果的SQL * 执行多条不需要返回结果的SQL
* 该方法使用 Statement 实现请注意SQL注入风险 * 该方法使用 Statement 实现请注意SQL注入风险
* *
* @param sql SQL语句内容 * @param sql SQL语句内容
* @param moreSQL 更多SQL语句内容 * @param moreSQL 更多SQL语句内容
* @return 对应参数返回的行数 * @return 对应参数返回的行数
* @see SQLUpdateBatchAction * @see SQLUpdateBatchAction
*/ */
@Nullable List<Integer> executeSQLBatch(@NotNull String sql, String... moreSQL); @Nullable List<Integer> executeSQLBatch(@NotNull String sql, String... moreSQL);
/** /**
* 执行多条不需要返回结果的SQL * 执行多条不需要返回结果的SQL
* *
* @param sqlBatch SQL语句内容 * @param sqlBatch SQL语句内容
* @return 对应参数返回的行数 * @return 对应参数返回的行数
*/ */
@Nullable List<Integer> executeSQLBatch(@NotNull Iterable<String> sqlBatch); @Nullable List<Integer> executeSQLBatch(@NotNull Iterable<String> sqlBatch);
/** /**
* 在库中创建一个表 * 在库中创建一个表
* *
* @param tableName 表名 * @param tableName 表名
* @return {@link TableCreateBuilder} * @return {@link TableCreateBuilder}
*/ */
TableCreateBuilder createTable(@NotNull String tableName); TableCreateBuilder createTable(@NotNull String tableName);
/** /**
* 新建一个查询 * 新建一个查询
* *
* @return {@link QueryBuilder} * @return {@link QueryBuilder}
*/ */
QueryBuilder createQuery(); QueryBuilder createQuery();
/** /**
* 创建一条插入操作 * 创建一条插入操作
* *
* @param tableName 目标表名 * @param tableName 目标表名
* @return {@link InsertBuilder} * @return {@link InsertBuilder}
*/ */
InsertBuilder<PreparedSQLUpdateAction> createInsert(@NotNull String tableName); InsertBuilder<PreparedSQLUpdateAction> createInsert(@NotNull String tableName);
/** /**
* 创建支持多组数据的插入操作 * 创建支持多组数据的插入操作
* *
* @param tableName 目标表名 * @param tableName 目标表名
* @return {@link InsertBuilder} * @return {@link InsertBuilder}
*/ */
InsertBuilder<PreparedSQLUpdateBatchAction> createInsertBatch(@NotNull String tableName); InsertBuilder<PreparedSQLUpdateBatchAction> createInsertBatch(@NotNull String tableName);
/** /**
* 创建一条替换操作 * 创建一条替换操作
* *
* @param tableName 目标表名 * @param tableName 目标表名
* @return {@link ReplaceBuilder} * @return {@link ReplaceBuilder}
*/ */
ReplaceBuilder<PreparedSQLUpdateAction> createReplace(@NotNull String tableName); ReplaceBuilder<PreparedSQLUpdateAction> createReplace(@NotNull String tableName);
/** /**
* 创建支持多组数据的替换操作 * 创建支持多组数据的替换操作
* *
* @param tableName 目标表名 * @param tableName 目标表名
* @return {@link ReplaceBuilder} * @return {@link ReplaceBuilder}
*/ */
ReplaceBuilder<PreparedSQLUpdateBatchAction> createReplaceBatch(@NotNull String tableName); ReplaceBuilder<PreparedSQLUpdateBatchAction> createReplaceBatch(@NotNull String tableName);
/** /**
* 创建更新操作 * 创建更新操作
* *
* @param tableName 目标表名 * @param tableName 目标表名
* @return {@link UpdateBuilder} * @return {@link UpdateBuilder}
*/ */
UpdateBuilder createUpdate(@NotNull String tableName); UpdateBuilder createUpdate(@NotNull String tableName);
/** /**
* 创建删除操作 * 创建删除操作
* *
* @param tableName 目标表名 * @param tableName 目标表名
* @return {@link DeleteBuilder} * @return {@link DeleteBuilder}
*/ */
DeleteBuilder createDelete(@NotNull String tableName); DeleteBuilder createDelete(@NotNull String tableName);
} }

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>0.2.7</version> <version>0.2.8</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>0.2.7</version> <version>0.2.8</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>0.2.7</version> <version>0.2.8</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>0.2.7</version> <version>0.2.8</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@ -5,10 +5,10 @@ import cc.carm.lib.easysql.action.PreparedSQLUpdateActionImpl;
import cc.carm.lib.easysql.action.SQLUpdateActionImpl; import cc.carm.lib.easysql.action.SQLUpdateActionImpl;
import cc.carm.lib.easysql.action.SQLUpdateBatchActionImpl; import cc.carm.lib.easysql.action.SQLUpdateBatchActionImpl;
import cc.carm.lib.easysql.api.SQLManager; import cc.carm.lib.easysql.api.SQLManager;
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 cc.carm.lib.easysql.api.action.SQLUpdateBatchAction; import cc.carm.lib.easysql.api.action.SQLUpdateBatchAction;
import cc.carm.lib.easysql.api.SQLQuery;
import cc.carm.lib.easysql.api.builder.*; import cc.carm.lib.easysql.api.builder.*;
import cc.carm.lib.easysql.builder.impl.*; import cc.carm.lib.easysql.builder.impl.*;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -21,166 +21,168 @@ import java.util.*;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.function.Supplier;
import java.util.logging.Logger; import java.util.logging.Logger;
public class SQLManagerImpl implements SQLManager { public class SQLManagerImpl implements SQLManager {
private final Logger LOGGER; private final Logger LOGGER;
private final DataSource dataSource; private final DataSource dataSource;
protected ExecutorService executorPool; protected ExecutorService executorPool;
ConcurrentHashMap<UUID, SQLQuery> activeQuery = new ConcurrentHashMap<>(); ConcurrentHashMap<UUID, SQLQuery> activeQuery = new ConcurrentHashMap<>();
boolean debug = false;
public SQLManagerImpl(@NotNull DataSource dataSource) { @NotNull Supplier<Boolean> debugMode = () -> false;
this(dataSource, null);
}
public SQLManagerImpl(@NotNull DataSource dataSource, @Nullable String name) { public SQLManagerImpl(@NotNull DataSource dataSource) {
String managerName = "SQLManager" + (name != null ? "#" + name : ""); this(dataSource, null);
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 public SQLManagerImpl(@NotNull DataSource dataSource, @Nullable String name) {
public boolean isDebugMode() { String managerName = "SQLManager" + (name != null ? "#" + name : "");
return this.debug; 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 @Override
public void setDebugMode(boolean enable) { public boolean isDebugMode() {
this.debug = enable; return this.debugMode.get();
} }
public void debug(String msg) { @Override
if (isDebugMode()) getLogger().info("[DEBUG] " + msg); public void setDebugMode(@NotNull Supplier<@NotNull Boolean> debugMode) {
} this.debugMode = debugMode;
}
public Logger getLogger() { public void debug(String msg) {
return LOGGER; if (isDebugMode()) getLogger().info("[DEBUG] " + msg);
} }
public ExecutorService getExecutorPool() { public Logger getLogger() {
return executorPool; return LOGGER;
} }
@Override public ExecutorService getExecutorPool() {
public @NotNull DataSource getDataSource() { return executorPool;
return this.dataSource; }
}
@Override @Override
public @NotNull Connection getConnection() throws SQLException { public @NotNull DataSource getDataSource() {
return getDataSource().getConnection(); return this.dataSource;
} }
@Override @Override
public @NotNull Map<UUID, SQLQuery> getActiveQuery() { public @NotNull Connection getConnection() throws SQLException {
return this.activeQuery; return getDataSource().getConnection();
} }
@Override @Override
public Integer executeSQL(String sql) { public @NotNull Map<UUID, SQLQuery> getActiveQuery() {
return new SQLUpdateActionImpl(this, sql).execute(null); return this.activeQuery;
} }
@Override @Override
public Integer executeSQL(String sql, Object[] params) { public Integer executeSQL(String sql) {
return new PreparedSQLUpdateActionImpl(this, sql, params).execute(null); return new SQLUpdateActionImpl(this, sql).execute(null);
} }
@Override @Override
public List<Integer> executeSQLBatch(String sql, Iterable<Object[]> paramsBatch) { public Integer executeSQL(String sql, Object[] params) {
return new PreparedSQLBatchUpdateActionImpl(this, sql) return new PreparedSQLUpdateActionImpl(this, sql, params).execute(null);
.setAllParams(paramsBatch) }
.execute(null);
}
@Override @Override
public List<Integer> executeSQLBatch(@NotNull String sql, String[] moreSQL) { public List<Integer> executeSQLBatch(String sql, Iterable<Object[]> paramsBatch) {
SQLUpdateBatchAction action = new SQLUpdateBatchActionImpl(this, sql); return new PreparedSQLBatchUpdateActionImpl(this, sql)
if (moreSQL != null && moreSQL.length > 0) { .setAllParams(paramsBatch)
Arrays.stream(moreSQL).forEach(action::addBatch); .execute(null);
} }
return action.execute(null);
}
@Override @Override
public @Nullable List<Integer> executeSQLBatch(@NotNull Iterable<String> sqlBatch) { public List<Integer> executeSQLBatch(@NotNull String sql, String[] moreSQL) {
Iterator<String> iterator = sqlBatch.iterator(); SQLUpdateBatchAction action = new SQLUpdateBatchActionImpl(this, sql);
if (!iterator.hasNext()) return null; // PLEASE GIVE IT SOMETHING if (moreSQL != null && moreSQL.length > 0) {
Arrays.stream(moreSQL).forEach(action::addBatch);
}
return action.execute(null);
}
SQLUpdateBatchAction action = new SQLUpdateBatchActionImpl(this, iterator.next()); @Override
while (iterator.hasNext()) { public @Nullable List<Integer> executeSQLBatch(@NotNull Iterable<String> sqlBatch) {
action.addBatch(iterator.next()); Iterator<String> iterator = sqlBatch.iterator();
} if (!iterator.hasNext()) return null; // PLEASE GIVE IT SOMETHING
return action.execute(null); SQLUpdateBatchAction action = new SQLUpdateBatchActionImpl(this, iterator.next());
} while (iterator.hasNext()) {
action.addBatch(iterator.next());
}
@Override return action.execute(null);
public TableCreateBuilder createTable(@NotNull String tableName) { }
return new TableCreateBuilderImpl(this, tableName);
}
@Override @Override
public QueryBuilder createQuery() { public TableCreateBuilder createTable(@NotNull String tableName) {
return new QueryBuilderImpl(this); return new TableCreateBuilderImpl(this, tableName);
} }
@Override @Override
public InsertBuilder<PreparedSQLUpdateBatchAction> createInsertBatch(@NotNull String tableName) { public QueryBuilder createQuery() {
return new InsertBuilderImpl<PreparedSQLUpdateBatchAction>(this, tableName) { return new QueryBuilderImpl(this);
@Override }
public PreparedSQLUpdateBatchAction setColumnNames(List<String> columnNames) {
return new PreparedSQLBatchUpdateActionImpl(getManager(), buildSQL(getTableName(), columnNames));
}
};
}
@Override @Override
public InsertBuilder<PreparedSQLUpdateAction> createInsert(@NotNull String tableName) { public InsertBuilder<PreparedSQLUpdateBatchAction> createInsertBatch(@NotNull String tableName) {
return new InsertBuilderImpl<PreparedSQLUpdateAction>(this, tableName) { return new InsertBuilderImpl<PreparedSQLUpdateBatchAction>(this, tableName) {
@Override @Override
public PreparedSQLUpdateAction setColumnNames(List<String> columnNames) { public PreparedSQLUpdateBatchAction setColumnNames(List<String> columnNames) {
return new PreparedSQLUpdateActionImpl(getManager(), buildSQL(getTableName(), columnNames)); return new PreparedSQLBatchUpdateActionImpl(getManager(), buildSQL(getTableName(), columnNames));
} }
}; };
} }
@Override @Override
public ReplaceBuilder<PreparedSQLUpdateBatchAction> createReplaceBatch(@NotNull String tableName) { public InsertBuilder<PreparedSQLUpdateAction> createInsert(@NotNull String tableName) {
return new ReplaceBuilderImpl<PreparedSQLUpdateBatchAction>(this, tableName) { return new InsertBuilderImpl<PreparedSQLUpdateAction>(this, tableName) {
@Override @Override
public PreparedSQLUpdateBatchAction setColumnNames(List<String> columnNames) { public PreparedSQLUpdateAction setColumnNames(List<String> columnNames) {
return new PreparedSQLBatchUpdateActionImpl(getManager(), buildSQL(getTableName(), columnNames)); return new PreparedSQLUpdateActionImpl(getManager(), buildSQL(getTableName(), columnNames));
} }
}; };
} }
@Override @Override
public ReplaceBuilder<PreparedSQLUpdateAction> createReplace(@NotNull String tableName) { public ReplaceBuilder<PreparedSQLUpdateBatchAction> createReplaceBatch(@NotNull String tableName) {
return new ReplaceBuilderImpl<PreparedSQLUpdateAction>(this, tableName) { return new ReplaceBuilderImpl<PreparedSQLUpdateBatchAction>(this, tableName) {
@Override @Override
public PreparedSQLUpdateAction setColumnNames(List<String> columnNames) { public PreparedSQLUpdateBatchAction setColumnNames(List<String> columnNames) {
return new PreparedSQLUpdateActionImpl(getManager(), buildSQL(getTableName(), columnNames)); return new PreparedSQLBatchUpdateActionImpl(getManager(), buildSQL(getTableName(), columnNames));
} }
}; };
} }
@Override @Override
public UpdateBuilder createUpdate(@NotNull String tableName) { public ReplaceBuilder<PreparedSQLUpdateAction> createReplace(@NotNull String tableName) {
return new UpdateBuilderImpl(this, tableName); return new ReplaceBuilderImpl<PreparedSQLUpdateAction>(this, tableName) {
} @Override
public PreparedSQLUpdateAction setColumnNames(List<String> columnNames) {
return new PreparedSQLUpdateActionImpl(getManager(), buildSQL(getTableName(), columnNames));
}
};
}
@Override @Override
public DeleteBuilder createDelete(@NotNull String tableName) { public UpdateBuilder createUpdate(@NotNull String tableName) {
return new DeleteBuilderImpl(this, tableName); return new UpdateBuilderImpl(this, tableName);
} }
@Override
public DeleteBuilder createDelete(@NotNull String tableName) {
return new DeleteBuilderImpl(this, tableName);
}
} }

View File

@ -16,7 +16,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.2.7</version> <version>0.2.8</version>
<modules> <modules>
<module>easysql-api</module> <module>easysql-api</module>