mirror of
https://github.com/CarmJos/EasySQL.git
synced 2026-06-05 09:01:26 +08:00
Compare commits
11 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 960989990b | |||
| b6026cb9f4 | |||
| 0d74b684e3 | |||
| 07e47c2220 | |||
| f795e6b421 | |||
| c79d833d04 | |||
| daa430cb14 | |||
| 248a6d6f34 | |||
| 0495928e49 | |||
| 421fe9f454 | |||
| f7745a2afe |
@@ -5,10 +5,19 @@
|
||||
|
||||
## 目录
|
||||
|
||||
- [Bob的EasySQL之旅(HikariCP)](USAGE-HIKARI.md) `@Ghost-Chu`
|
||||
### 文章
|
||||
|
||||
- [Bob的EasySQL之旅(HikariCP)](USAGE-HIKARI.md) `by @Ghost-Chu`
|
||||
- [在**小项目中**推荐使用的**数据库表**实现方案](USAGE-TABLE.md) `by @CarmJos`
|
||||
|
||||
### 视频
|
||||
|
||||
- [EasySql快速操作Mysql数据库:我的世界插件开发](https://www.bilibili.com/video/BV1w34y1p7Xs) `by @Shinyoki`
|
||||
|
||||
## 实例项目
|
||||
以下是一些实例项目,可供各位参考。
|
||||
|
||||
- UltraDepository 超级仓库插件 `@CarmJos`
|
||||
- [storage/MySQLStorage](https://github.com/CarmJos/UltraDepository/blob/master/src/main/java/cc/carm/plugin/ultradepository/storage/impl/MySQLStorage.java)
|
||||
- QuickShop-Hikari 快速商店插件 `@Ghost-Chu`
|
||||
- [database/](https://github.com/Ghost-chu/QuickShop-Hikari/tree/hikari/quickshop-bukkit/src/main/java/com/ghostchu/quickshop/database)
|
||||
@@ -1,6 +1,6 @@
|
||||
> 本文档由 GitHub 用户 @Ghost-chu 创建。
|
||||
> 本文撰写于 2022/02/09,适配 EasySQL 版本 `v0.3.6`。
|
||||
> 本文基于 `EasySQL-HikariPool` 版本编写。
|
||||
> 本文撰写于 2022/02/09,适配 EasySQL 版本 `v0.3.6` **(部分接口已变更)**。
|
||||
> 本文基于 `EasySQL-Hikari` 版本编写。
|
||||
|
||||
# EasySQL - HikariPool 使用指南
|
||||
|
||||
|
||||
@@ -0,0 +1,161 @@
|
||||
> 本文档由 GitHub 用户 @CarmJos 创建。
|
||||
> 本文撰写于 2022/07/01,基于 EasySQL 版本 `0.4.2` 。
|
||||
|
||||
# 在**小项目中**推荐使用的**数据库表**实现方案
|
||||
|
||||
|
||||
## 简介
|
||||
在小型项目中,我们常常需要编写数据库的表结构,并需要在开发中不断地参考、维护该结构。
|
||||
|
||||
在 EasySQL 中,我们提供了一个简单快捷的数据库表创建工具 `TableCreateBuilder` 。
|
||||
基于该工具,又在后续版本中提供了 `SQLTable` 类用于快速针对指定表创建不同的数据库操作。
|
||||
|
||||
_SQLTable同时提供了有SQLManager参数与无参的操作方法,其中无参方法将自动调用初始化时使用的SQLManager进行操作。_
|
||||
|
||||
以下内容是我在许多项目中的使用方法,由于其 `便捷`、`易于管理` 且 `支持引用查询` ,我十分推荐您参考我的方案,并应用到自己的项目中。
|
||||
|
||||
### 实例项目:
|
||||
- [QuickShop-Hikari (DataTables)](https://github.com/Ghost-chu/QuickShop-Hikari/blob/hikari/quickshop-bukkit/src/main/java/com/ghostchu/quickshop/database/DataTables.java)
|
||||
|
||||
## 利用 NamedSQLTable 快速创建枚举类以管理
|
||||
|
||||
这种方案的优势在于无需复制大量代码,仅需使用EasySQL已经提供的 `NamedSQLTable` 类快捷进行数据库操作。
|
||||
|
||||
首先,我们需要创建一个枚举类,[示例代码](../demo/src/main/java/DataTables1.java)如下所示:
|
||||
|
||||
```java
|
||||
import cc.carm.lib.easysql.api.enums.IndexType;
|
||||
import cc.carm.lib.easysql.api.enums.NumberType;
|
||||
import cc.carm.lib.easysql.api.table.NamedSQLTable;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.sql.SQLException;
|
||||
|
||||
public enum DataTables {
|
||||
|
||||
DATA(SQLTable.of("data", (table) -> {
|
||||
table.addAutoIncrementColumn("id", true);
|
||||
table.addColumn("user", "INT UNSIGNED NOT NULL");
|
||||
table.addColumn("content", "TEXT NOT NULL");
|
||||
table.addColumn("time", "DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP");
|
||||
})),
|
||||
|
||||
USER(SQLTable.of("user", (table) -> {
|
||||
table.addAutoIncrementColumn("id", NumberType.INT, true, true);
|
||||
table.addColumn("uuid", "VARCHAR(32) NOT NULL UNIQUE KEY");
|
||||
table.addColumn("username", "VARCHAR(16) NOT NULL");
|
||||
table.addColumn("age", "TINYINT NOT NULL DEFAULT 1");
|
||||
table.addColumn("email", "VARCHAR(32)");
|
||||
table.addColumn("phone", "VARCHAR(16)");
|
||||
table.addColumn("registerTime", "DATETIME NOT NULL");
|
||||
table.setIndex("username", IndexType.UNIQUE_KEY); // 添加唯一索引
|
||||
table.setIndex(IndexType.INDEX, "contact", "email", "phone"); //添加联合索引 (示例)
|
||||
}));
|
||||
|
||||
private final NamedSQLTable table;
|
||||
|
||||
DataTables(NamedSQLTable table) {
|
||||
this.table = table;
|
||||
}
|
||||
|
||||
public NamedSQLTable get() {
|
||||
return this.table;
|
||||
}
|
||||
|
||||
public static void initialize(@NotNull SQLManager manager, @Nullable String tablePrefix) {
|
||||
for (DataTables value : values()) {
|
||||
try {
|
||||
value.get().create(manager, tablePrefix);
|
||||
} catch (SQLException e) {
|
||||
// 提示异常
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
随后,我们便可以在数据库初始化时调用 `DataTables#initialize(manager,tablePrefix)` 方法快捷的进行表的初始化。
|
||||
|
||||
初始化后,我们便可以通过 `DataTables#get()` 方法获取对应表的 `NamedSQLTable` 实例,以进行 `createQuery()` 等操作。
|
||||
|
||||
## 利用枚举类实现 SQLTable 进行操作
|
||||
|
||||
这种方法相较于前者代码量稍多些,但无需在每次调用先通过 `DataTables#get()` 方法获取 NamedSQLTable 实例,代码上更为简洁。
|
||||
|
||||
且可以通过重写 `getTableName()` 方法来自行规定表前缀。
|
||||
|
||||
_该方法为本人最常用,也是最推荐的方法。_
|
||||
|
||||
[示例代码](../demo/src/main/java/DataTables2.java)如下:
|
||||
|
||||
```java
|
||||
import cc.carm.lib.easysql.api.builder.TableCreateBuilder;
|
||||
import cc.carm.lib.easysql.api.enums.IndexType;
|
||||
import cc.carm.lib.easysql.api.enums.NumberType;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public enum DataTables implements SQLTable {
|
||||
|
||||
USER((table) -> {
|
||||
table.addAutoIncrementColumn("id", NumberType.INT, true, true);
|
||||
table.addColumn("uuid", "VARCHAR(32) NOT NULL UNIQUE KEY");
|
||||
table.addColumn("username", "VARCHAR(16) NOT NULL");
|
||||
table.addColumn("age", "TINYINT NOT NULL DEFAULT 1");
|
||||
table.addColumn("email", "VARCHAR(32)");
|
||||
table.addColumn("phone", "VARCHAR(16)");
|
||||
table.addColumn("registerTime", "DATETIME NOT NULL");
|
||||
table.setIndex("username", IndexType.UNIQUE_KEY); // 添加唯一索引
|
||||
table.setIndex(IndexType.INDEX, "contact", "email", "phone"); //添加联合索引 (示例)
|
||||
});
|
||||
|
||||
private final Consumer<TableCreateBuilder> builder;
|
||||
private @Nullable String tablePrefix;
|
||||
private @Nullable SQLManager manager;
|
||||
|
||||
DataTables(Consumer<TableCreateBuilder> builder) {
|
||||
this.builder = builder;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable SQLManager getSQLManager() {
|
||||
return this.manager;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull String getTableName() {
|
||||
// 这里直接选择用枚举的名称作为table的主名称
|
||||
return (tablePrefix != null ? tablePrefix : "") + name().toLowerCase();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean create(SQLManager sqlManager) throws SQLException {
|
||||
return create(sqlManager, null);
|
||||
}
|
||||
|
||||
public boolean create(@NotNull SQLManager sqlManager, @Nullable String tablePrefix) throws SQLException {
|
||||
if (this.manager == null) this.manager = sqlManager;
|
||||
this.tablePrefix = tablePrefix;
|
||||
|
||||
TableCreateBuilder tableBuilder = sqlManager.createTable(getTableName());
|
||||
if (builder != null) builder.accept(tableBuilder);
|
||||
return tableBuilder.build().executeFunction(l -> l > 0, false);
|
||||
}
|
||||
|
||||
public static void initialize(@NotNull SQLManager manager, @Nullable String tablePrefix) {
|
||||
for (DataTables value : values()) {
|
||||
try {
|
||||
value.create(manager, tablePrefix);
|
||||
} catch (SQLException e) {
|
||||
// 提示异常
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
```
|
||||
@@ -83,14 +83,14 @@ jobs:
|
||||
rm -rf deploy
|
||||
mkdir -vp deploy
|
||||
cp -vrf $HOME/local-deploy/* deploy/
|
||||
cp -vrf .documentation/REPOSITORY-README.md deploy/README.md
|
||||
cp -vrf .documentation/repository/README.md deploy/README.md
|
||||
|
||||
- name: "Copy Javadoc"
|
||||
run: |
|
||||
rm -rf docs
|
||||
mkdir -vp docs
|
||||
cp -vrf api/target/apidocs/* docs/
|
||||
cp -vrf .documentation/JAVADOC-README.md docs/README.md
|
||||
cp -vrf .documentation/javadoc/README.md docs/README.md
|
||||
|
||||
- name: "Generate the Javadoc sitemap"
|
||||
id: sitemap
|
||||
|
||||
+1
-1
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<artifactId>easysql-parent</artifactId>
|
||||
<version>0.4.0</version>
|
||||
<version>0.4.2</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
||||
@@ -17,6 +17,8 @@ import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
@@ -30,6 +32,23 @@ public interface SQLManager {
|
||||
|
||||
boolean isDebugMode();
|
||||
|
||||
|
||||
/**
|
||||
* 获取用于执行 {@link SQLAction#executeAsync()} 的线程池。
|
||||
* <br> 默认线程池为 {@link ThreadPoolExecutor} ,大小为 3。
|
||||
*
|
||||
* @return {@link ExecutorService}
|
||||
*/
|
||||
@NotNull ExecutorService getExecutorPool();
|
||||
|
||||
/**
|
||||
* 设定用于执行 {@link SQLAction#executeAsync()} 的线程池。
|
||||
*
|
||||
* @param executorPool {@link ExecutorService}
|
||||
*/
|
||||
void setExecutorPool(@NotNull ExecutorService executorPool);
|
||||
|
||||
|
||||
/**
|
||||
* 设定是否启用调试模式。
|
||||
* 启用调试模式后,会在每次执行SQL语句时,调用 {@link #getDebugHandler()} 来输出调试信息。
|
||||
|
||||
@@ -4,26 +4,28 @@ import cc.carm.lib.easysql.api.action.PreparedSQLUpdateAction;
|
||||
import cc.carm.lib.easysql.api.action.PreparedSQLUpdateBatchAction;
|
||||
import cc.carm.lib.easysql.api.builder.*;
|
||||
import cc.carm.lib.easysql.api.function.SQLHandler;
|
||||
import cc.carm.lib.easysql.api.table.NamedSQLTable;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* SQLTable 基于 {@link TableCreateBuilder} 构建表,用于快速创建与该表相关的操作。
|
||||
* <ul>
|
||||
* <li>1. 调用 {@link SQLTable#of(String, String[])} 方法创建一个 SQLTable 对象;</li>
|
||||
* <li>2. 在应用初始化阶段调用 {@link SQLTable#create(SQLManager)} 方法初始化 SQLTable 对象;</li>
|
||||
* <li>3. 获取已创建的{@link SQLTable} 实例,直接调用对应方法进行关于表的相关操作。</li>
|
||||
* <li>1. 调用 {@link NamedSQLTable#of(String, String[])} 方法创建一个 SQLTable 对象;</li>
|
||||
* <li>2. 在应用初始化阶段调用 {@link NamedSQLTable#create(SQLManager)} 方法初始化 SQLTable 对象;</li>
|
||||
* <li>3. 获取已创建的{@link NamedSQLTable} 实例,直接调用对应方法进行关于表的相关操作。</li>
|
||||
* </ul>
|
||||
*
|
||||
* @author CarmJos
|
||||
* @since 0.3.10
|
||||
*/
|
||||
public abstract class SQLTable {
|
||||
public interface SQLTable {
|
||||
|
||||
public static @NotNull SQLTable of(@NotNull String tableName, @Nullable SQLHandler<TableCreateBuilder> table) {
|
||||
return new SQLTable(tableName) {
|
||||
static @NotNull NamedSQLTable of(@NotNull String tableName, @Nullable SQLHandler<TableCreateBuilder> table) {
|
||||
return new NamedSQLTable(tableName) {
|
||||
@Override
|
||||
public boolean create(@NotNull SQLManager sqlManager, String tablePrefix) throws SQLException {
|
||||
if (this.manager == null) this.manager = sqlManager;
|
||||
@@ -36,108 +38,113 @@ public abstract class SQLTable {
|
||||
};
|
||||
}
|
||||
|
||||
public static @NotNull SQLTable of(@NotNull String tableName, @NotNull String[] columns) {
|
||||
static @NotNull NamedSQLTable of(@NotNull String tableName, @NotNull String[] columns) {
|
||||
return of(tableName, columns, null);
|
||||
}
|
||||
|
||||
public static @NotNull SQLTable of(@NotNull String tableName,
|
||||
@NotNull String[] columns, @Nullable String tableSettings) {
|
||||
static @NotNull NamedSQLTable of(@NotNull String tableName,
|
||||
@NotNull String[] columns, @Nullable String tableSettings) {
|
||||
return of(tableName, builder -> {
|
||||
builder.setColumns(columns);
|
||||
if (tableSettings != null) builder.setTableSettings(tableSettings);
|
||||
});
|
||||
}
|
||||
|
||||
private final @NotNull String tableName;
|
||||
|
||||
protected String tablePrefix;
|
||||
protected SQLManager manager;
|
||||
/**
|
||||
* 以指定的 {@link SQLManager} 实例初始化并创建该表
|
||||
*
|
||||
* @param sqlManager {@link SQLManager} 实例
|
||||
* @return 是否新创建了本表 (若已创建或创建失败则返回false)
|
||||
* @throws SQLException 当数据库返回异常时抛出
|
||||
*/
|
||||
boolean create(SQLManager sqlManager) throws SQLException;
|
||||
|
||||
/**
|
||||
* 请调用 {@link SQLTable} 下的静态方法进行对象的初始化。
|
||||
* 得到 {@link #create(SQLManager)} 用于初始化本实例的 {@link SQLManager} 实例
|
||||
*
|
||||
* @param tableName 该表的名称
|
||||
* @return {@link SQLManager} 实例
|
||||
*/
|
||||
private SQLTable(@NotNull String tableName) {
|
||||
this.tableName = tableName;
|
||||
}
|
||||
|
||||
public @NotNull String getTableName() {
|
||||
return (tablePrefix != null ? tablePrefix : "") + tableName;
|
||||
}
|
||||
@Nullable SQLManager getSQLManager();
|
||||
|
||||
/**
|
||||
* 使用指定 SQLManager 进行本示例的初始化。
|
||||
* 得到本表表名,不得为空。
|
||||
*
|
||||
* @param sqlManager {@link SQLManager}
|
||||
* @param tablePrefix 表名前缀
|
||||
* @return 本表是否为首次创建
|
||||
* @throws SQLException 出现任何错误时抛出
|
||||
* @return 本表表名
|
||||
*/
|
||||
public abstract boolean create(@NotNull SQLManager sqlManager, @Nullable String tablePrefix) throws SQLException;
|
||||
@NotNull String getTableName();
|
||||
|
||||
public boolean create(@NotNull SQLManager sqlManager) throws SQLException {
|
||||
return create(manager, null);
|
||||
default @NotNull TableQueryBuilder createQuery() {
|
||||
return Optional.ofNullable(getSQLManager()).map(this::createQuery)
|
||||
.orElseThrow(() -> new NullPointerException("This table doesn't have a SQLManger."));
|
||||
}
|
||||
|
||||
public @NotNull TableQueryBuilder createQuery(@NotNull SQLManager sqlManager) {
|
||||
default @NotNull TableQueryBuilder createQuery(@NotNull SQLManager sqlManager) {
|
||||
return sqlManager.createQuery().inTable(getTableName());
|
||||
}
|
||||
|
||||
public @NotNull TableQueryBuilder createQuery() {
|
||||
return createQuery(this.manager);
|
||||
default @NotNull DeleteBuilder createDelete() {
|
||||
return Optional.ofNullable(getSQLManager()).map(this::createDelete)
|
||||
.orElseThrow(() -> new NullPointerException("This table doesn't have a SQLManger."));
|
||||
}
|
||||
|
||||
public @NotNull DeleteBuilder createDelete() {
|
||||
return createDelete(this.manager);
|
||||
}
|
||||
|
||||
public @NotNull DeleteBuilder createDelete(@NotNull SQLManager sqlManager) {
|
||||
default @NotNull DeleteBuilder createDelete(@NotNull SQLManager sqlManager) {
|
||||
return sqlManager.createDelete(getTableName());
|
||||
}
|
||||
|
||||
public @NotNull UpdateBuilder createUpdate() {
|
||||
return createUpdate(this.manager);
|
||||
default @NotNull UpdateBuilder createUpdate() {
|
||||
return Optional.ofNullable(getSQLManager()).map(this::createUpdate)
|
||||
.orElseThrow(() -> new NullPointerException("This table doesn't have a SQLManger."));
|
||||
}
|
||||
|
||||
public @NotNull UpdateBuilder createUpdate(@NotNull SQLManager sqlManager) {
|
||||
default @NotNull UpdateBuilder createUpdate(@NotNull SQLManager sqlManager) {
|
||||
return sqlManager.createUpdate(getTableName());
|
||||
}
|
||||
|
||||
|
||||
public @NotNull InsertBuilder<PreparedSQLUpdateAction<Integer>> createInsert() {
|
||||
return createInsert(this.manager);
|
||||
default @NotNull InsertBuilder<PreparedSQLUpdateAction<Integer>> createInsert() {
|
||||
return Optional.ofNullable(getSQLManager()).map(this::createInsert)
|
||||
.orElseThrow(() -> new NullPointerException("This table doesn't have a SQLManger."));
|
||||
}
|
||||
|
||||
public @NotNull InsertBuilder<PreparedSQLUpdateAction<Integer>> createInsert(@NotNull SQLManager sqlManager) {
|
||||
default @NotNull InsertBuilder<PreparedSQLUpdateAction<Integer>> createInsert(@NotNull SQLManager sqlManager) {
|
||||
return sqlManager.createInsert(getTableName());
|
||||
}
|
||||
|
||||
|
||||
public @NotNull InsertBuilder<PreparedSQLUpdateBatchAction<Integer>> createInsertBatch() {
|
||||
return createInsertBatch(this.manager);
|
||||
default @NotNull InsertBuilder<PreparedSQLUpdateBatchAction<Integer>> createInsertBatch() {
|
||||
return Optional.ofNullable(getSQLManager()).map(this::createInsertBatch)
|
||||
.orElseThrow(() -> new NullPointerException("This table doesn't have a SQLManger."));
|
||||
}
|
||||
|
||||
public @NotNull InsertBuilder<PreparedSQLUpdateBatchAction<Integer>> createInsertBatch(@NotNull SQLManager sqlManager) {
|
||||
default @NotNull InsertBuilder<PreparedSQLUpdateBatchAction<Integer>> createInsertBatch(@NotNull SQLManager sqlManager) {
|
||||
return sqlManager.createInsertBatch(getTableName());
|
||||
}
|
||||
|
||||
default @NotNull ReplaceBuilder<PreparedSQLUpdateAction<Integer>> createReplace() {
|
||||
return Optional.ofNullable(getSQLManager()).map(this::createReplace)
|
||||
.orElseThrow(() -> new NullPointerException("This table doesn't have a SQLManger."));
|
||||
|
||||
public @NotNull ReplaceBuilder<PreparedSQLUpdateAction<Integer>> createReplace() {
|
||||
return createReplace(this.manager);
|
||||
}
|
||||
|
||||
public @NotNull ReplaceBuilder<PreparedSQLUpdateAction<Integer>> createReplace(@NotNull SQLManager sqlManager) {
|
||||
return sqlManager.createReplace(getTableName());
|
||||
default @NotNull ReplaceBuilder<PreparedSQLUpdateAction<Integer>> createReplace(@NotNull SQLManager sqlManager) {
|
||||
return Optional.ofNullable(getSQLManager()).map(this::createReplace)
|
||||
.orElseThrow(() -> new NullPointerException("This table doesn't have a SQLManger."));
|
||||
}
|
||||
|
||||
|
||||
public @NotNull ReplaceBuilder<PreparedSQLUpdateBatchAction<Integer>> createReplaceBatch() {
|
||||
return createReplaceBatch(this.manager);
|
||||
default @NotNull ReplaceBuilder<PreparedSQLUpdateBatchAction<Integer>> createReplaceBatch() {
|
||||
return Optional.ofNullable(getSQLManager()).map(this::createReplaceBatch)
|
||||
.orElseThrow(() -> new NullPointerException("This table doesn't have a SQLManger."));
|
||||
}
|
||||
|
||||
public @NotNull ReplaceBuilder<PreparedSQLUpdateBatchAction<Integer>> createReplaceBatch(@NotNull SQLManager sqlManager) {
|
||||
default @NotNull ReplaceBuilder<PreparedSQLUpdateBatchAction<Integer>> createReplaceBatch(@NotNull SQLManager sqlManager) {
|
||||
return sqlManager.createReplaceBatch(getTableName());
|
||||
}
|
||||
|
||||
default @NotNull TableAlterBuilder alter() {
|
||||
return Optional.ofNullable(getSQLManager()).map(this::alter)
|
||||
.orElseThrow(() -> new NullPointerException("This table doesn't have a SQLManger."));
|
||||
}
|
||||
|
||||
default @NotNull TableAlterBuilder alter(@NotNull SQLManager sqlManager) {
|
||||
return sqlManager.alterTable(getTableName());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
package cc.carm.lib.easysql.api.table;
|
||||
|
||||
import cc.carm.lib.easysql.api.SQLManager;
|
||||
import cc.carm.lib.easysql.api.SQLTable;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.sql.SQLException;
|
||||
|
||||
|
||||
public abstract class NamedSQLTable implements SQLTable {
|
||||
|
||||
private final @NotNull String tableName;
|
||||
|
||||
protected @Nullable String tablePrefix;
|
||||
protected @Nullable SQLManager manager;
|
||||
|
||||
/**
|
||||
* 请调用 {@link NamedSQLTable} 下的静态方法进行对象的初始化。
|
||||
*
|
||||
* @param tableName 该表的名称
|
||||
*/
|
||||
public NamedSQLTable(@NotNull String tableName) {
|
||||
this.tableName = tableName;
|
||||
}
|
||||
|
||||
public @NotNull String getTableName() {
|
||||
return (tablePrefix != null ? tablePrefix : "") + tableName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable SQLManager getSQLManager() {
|
||||
return this.manager;
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用指定 SQLManager 进行本示例的初始化。
|
||||
*
|
||||
* @param sqlManager {@link SQLManager}
|
||||
* @param tablePrefix 表名前缀
|
||||
* @return 本表是否为首次创建
|
||||
* @throws SQLException 出现任何错误时抛出
|
||||
*/
|
||||
public abstract boolean create(@NotNull SQLManager sqlManager, @Nullable String tablePrefix) throws SQLException;
|
||||
|
||||
public boolean create(@NotNull SQLManager sqlManager) throws SQLException {
|
||||
return create(sqlManager, null);
|
||||
}
|
||||
|
||||
}
|
||||
+3
-7
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>easysql-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>0.4.0</version>
|
||||
<version>0.4.2</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
@@ -70,36 +70,31 @@
|
||||
<groupId>com.zaxxer</groupId>
|
||||
<artifactId>HikariCP</artifactId>
|
||||
<version>4.0.3</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- https://mvnrepository.com/artifact/com.h2database/h2 -->
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<version>2.1.212</version>
|
||||
<scope>test</scope>
|
||||
<version>2.1.214</version>
|
||||
</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>
|
||||
@@ -120,4 +115,5 @@
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,52 @@
|
||||
import cc.carm.lib.easysql.api.SQLManager;
|
||||
import cc.carm.lib.easysql.api.SQLTable;
|
||||
import cc.carm.lib.easysql.api.enums.IndexType;
|
||||
import cc.carm.lib.easysql.api.enums.NumberType;
|
||||
import cc.carm.lib.easysql.api.table.NamedSQLTable;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.sql.SQLException;
|
||||
|
||||
public enum DataTables1 {
|
||||
|
||||
DATA(SQLTable.of("data", (table) -> {
|
||||
table.addAutoIncrementColumn("id", true);
|
||||
table.addColumn("user", "INT UNSIGNED NOT NULL");
|
||||
table.addColumn("content", "TEXT NOT NULL");
|
||||
table.addColumn("time", "DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP");
|
||||
})),
|
||||
|
||||
USER(SQLTable.of("user", (table) -> {
|
||||
table.addAutoIncrementColumn("id", NumberType.INT, true, true);
|
||||
table.addColumn("uuid", "VARCHAR(32) NOT NULL UNIQUE KEY");
|
||||
table.addColumn("username", "VARCHAR(16) NOT NULL");
|
||||
table.addColumn("age", "TINYINT NOT NULL DEFAULT 1");
|
||||
table.addColumn("email", "VARCHAR(32)");
|
||||
table.addColumn("phone", "VARCHAR(16)");
|
||||
table.addColumn("registerTime", "DATETIME NOT NULL");
|
||||
table.setIndex("username", IndexType.UNIQUE_KEY); // 添加唯一索引
|
||||
table.setIndex(IndexType.INDEX, "contact", "email", "phone"); //添加联合索引 (示例)
|
||||
}));
|
||||
|
||||
private final NamedSQLTable table;
|
||||
|
||||
DataTables1(NamedSQLTable table) {
|
||||
this.table = table;
|
||||
}
|
||||
|
||||
public NamedSQLTable get() {
|
||||
return this.table;
|
||||
}
|
||||
|
||||
public static void initialize(@NotNull SQLManager manager, @Nullable String tablePrefix) {
|
||||
for (DataTables1 value : values()) {
|
||||
try {
|
||||
value.get().create(manager, tablePrefix);
|
||||
} catch (SQLException e) {
|
||||
// 提示异常
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
import cc.carm.lib.easysql.api.SQLManager;
|
||||
import cc.carm.lib.easysql.api.SQLTable;
|
||||
import cc.carm.lib.easysql.api.builder.TableCreateBuilder;
|
||||
import cc.carm.lib.easysql.api.enums.IndexType;
|
||||
import cc.carm.lib.easysql.api.enums.NumberType;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public enum DataTables2 implements SQLTable {
|
||||
|
||||
USER((table) -> {
|
||||
table.addAutoIncrementColumn("id", NumberType.INT, true, true);
|
||||
table.addColumn("uuid", "VARCHAR(32) NOT NULL UNIQUE KEY");
|
||||
table.addColumn("username", "VARCHAR(16) NOT NULL");
|
||||
table.addColumn("age", "TINYINT NOT NULL DEFAULT 1");
|
||||
table.addColumn("email", "VARCHAR(32)");
|
||||
table.addColumn("phone", "VARCHAR(16)");
|
||||
table.addColumn("registerTime", "DATETIME NOT NULL");
|
||||
table.setIndex("username", IndexType.UNIQUE_KEY); // 添加唯一索引
|
||||
table.setIndex(IndexType.INDEX, "contact", "email", "phone"); //添加联合索引 (示例)
|
||||
});
|
||||
|
||||
private final Consumer<TableCreateBuilder> builder;
|
||||
private @Nullable String tablePrefix;
|
||||
private @Nullable SQLManager manager;
|
||||
|
||||
DataTables2(Consumer<TableCreateBuilder> builder) {
|
||||
this.builder = builder;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable SQLManager getSQLManager() {
|
||||
return this.manager;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull String getTableName() {
|
||||
// 这里直接选择用枚举的名称作为table的主名称
|
||||
return (tablePrefix != null ? tablePrefix : "") + name().toLowerCase();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean create(SQLManager sqlManager) throws SQLException {
|
||||
return create(sqlManager, null);
|
||||
}
|
||||
|
||||
public boolean create(@NotNull SQLManager sqlManager, @Nullable String tablePrefix) throws SQLException {
|
||||
if (this.manager == null) this.manager = sqlManager;
|
||||
this.tablePrefix = tablePrefix;
|
||||
|
||||
TableCreateBuilder tableBuilder = sqlManager.createTable(getTableName());
|
||||
if (builder != null) builder.accept(tableBuilder);
|
||||
return tableBuilder.build().executeFunction(l -> l > 0, false);
|
||||
}
|
||||
|
||||
public static void initialize(@NotNull SQLManager manager, @Nullable String tablePrefix) {
|
||||
for (DataTables2 value : values()) {
|
||||
try {
|
||||
value.create(manager, tablePrefix);
|
||||
} catch (SQLException e) {
|
||||
// 提示异常
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -18,7 +18,7 @@ public class EasySQLTest {
|
||||
protected SQLManager sqlManager;
|
||||
|
||||
@Before
|
||||
public void initDatabase() {
|
||||
public void initialize() {
|
||||
HikariConfig config = new HikariConfig();
|
||||
config.setDriverClassName("org.h2.Driver");
|
||||
config.setJdbcUrl("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;MODE=MYSQL;");
|
||||
@@ -28,7 +28,7 @@ public class EasySQLTest {
|
||||
}
|
||||
|
||||
@After
|
||||
public void shutdownDatabase() {
|
||||
public void shutdown() {
|
||||
if (sqlManager.getDataSource() instanceof HikariDataSource) {
|
||||
//Close bee connection pool
|
||||
((HikariDataSource) sqlManager.getDataSource()).close();
|
||||
@@ -50,6 +50,7 @@ public class EasySQLTest {
|
||||
tests.add(new SQLUpdateReturnKeysTest());
|
||||
tests.add(new QueryCloseTest());
|
||||
tests.add(new QueryFunctionTest());
|
||||
tests.add(new QueryAsyncTest());
|
||||
// tests.add(new DeleteTest());
|
||||
|
||||
print("准备进行测试...");
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
package cc.carm.lib.easysql.tests;
|
||||
|
||||
import cc.carm.lib.easysql.TestHandler;
|
||||
import cc.carm.lib.easysql.api.SQLManager;
|
||||
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class QueryAsyncTest extends TestHandler {
|
||||
|
||||
|
||||
@Override
|
||||
public void onTest(SQLManager sqlManager) throws SQLException {
|
||||
|
||||
sqlManager.createQuery()
|
||||
.inTable("test_user_table")
|
||||
.orderBy("id", false)
|
||||
.setLimit(1)
|
||||
.build().executeAsync(query -> {
|
||||
try {
|
||||
Thread.sleep(1000L);
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
if (!query.getResultSet().next()) {
|
||||
System.out.println("id (ps): NULL");
|
||||
} else {
|
||||
System.out.println("id (ps): " + query.getResultSet().getInt("id"));
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>easysql-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>0.4.0</version>
|
||||
<version>0.4.2</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
||||
@@ -53,10 +53,12 @@ public class QueryActionImpl extends AbstractSQLAction<SQLQuery> implements Quer
|
||||
|
||||
@Override
|
||||
public void executeAsync(SQLHandler<SQLQuery> success, SQLExceptionHandler failure) {
|
||||
try (SQLQueryImpl query = execute()) {
|
||||
if (success != null) success.accept(query);
|
||||
} catch (SQLException exception) {
|
||||
handleException(failure, exception);
|
||||
}
|
||||
getManager().getExecutorPool().submit(() -> {
|
||||
try (SQLQueryImpl query = execute()) {
|
||||
if (success != null) success.accept(query);
|
||||
} catch (SQLException exception) {
|
||||
handleException(failure, exception);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -89,10 +89,14 @@ public class SQLManagerImpl implements SQLManager {
|
||||
return LOGGER;
|
||||
}
|
||||
|
||||
public ExecutorService getExecutorPool() {
|
||||
public @NotNull ExecutorService getExecutorPool() {
|
||||
return executorPool;
|
||||
}
|
||||
|
||||
public void setExecutorPool(@NotNull ExecutorService executorPool) {
|
||||
this.executorPool = executorPool;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull DataSource getDataSource() {
|
||||
return this.dataSource;
|
||||
|
||||
@@ -19,7 +19,7 @@
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<artifactId>easysql-parent</artifactId>
|
||||
<packaging>pom</packaging>
|
||||
<version>0.4.0</version>
|
||||
<version>0.4.2</version>
|
||||
|
||||
<modules>
|
||||
<module>api</module>
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>easysql-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>0.4.0</version>
|
||||
<version>0.4.2</version>
|
||||
<relativePath>../../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>easysql-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>0.4.0</version>
|
||||
<version>0.4.2</version>
|
||||
<relativePath>../../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
Reference in New Issue
Block a user