mirror of
https://github.com/CarmJos/MineSQL.git
synced 2024-09-19 20:25:45 +00:00
feat(table): 添加单独的表声明类及快速初始化表方法。
This commit is contained in:
parent
8c9c6b72e2
commit
054f9bb04c
@ -5,7 +5,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>minesql-parent</artifactId>
|
<artifactId>minesql-parent</artifactId>
|
||||||
<groupId>cc.carm.plugin</groupId>
|
<groupId>cc.carm.plugin</groupId>
|
||||||
<version>1.1.1</version>
|
<version>1.2.0</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
@ -4,11 +4,14 @@ import cc.carm.lib.easysql.api.SQLManager;
|
|||||||
import cc.carm.lib.easysql.api.SQLQuery;
|
import cc.carm.lib.easysql.api.SQLQuery;
|
||||||
import cc.carm.plugin.minesql.api.SQLRegistry;
|
import cc.carm.plugin.minesql.api.SQLRegistry;
|
||||||
import cc.carm.plugin.minesql.api.source.SQLSourceConfig;
|
import cc.carm.plugin.minesql.api.source.SQLSourceConfig;
|
||||||
|
import cc.carm.plugin.minesql.api.table.SQLTablesRoot;
|
||||||
|
import cc.carm.plugin.minesql.api.table.SimpleSQLTable;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
import javax.sql.DataSource;
|
import javax.sql.DataSource;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.sql.SQLException;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
@ -61,4 +64,18 @@ interface IMineSQL {
|
|||||||
*/
|
*/
|
||||||
void shutdown(SQLManager manager, @Nullable Consumer<Map<UUID, SQLQuery>> activeQueries);
|
void shutdown(SQLManager manager, @Nullable Consumer<Map<UUID, SQLQuery>> activeQueries);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 读取一个 {@link SQLTablesRoot} 中全部的 {@link SimpleSQLTable} 实例并初始化。
|
||||||
|
*
|
||||||
|
* @param tablesRoot {@link SQLTablesRoot}实例
|
||||||
|
*/
|
||||||
|
void createTables(@NotNull SQLTablesRoot tablesRoot) throws Exception;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 读取一个 {@link SQLTablesRoot}类中 中全部的静态 {@link SimpleSQLTable} 实例并初始化。
|
||||||
|
*
|
||||||
|
* @param tablesRootClazz {@link SQLTablesRoot}静态类
|
||||||
|
*/
|
||||||
|
void createTables(@NotNull Class<? extends SQLTablesRoot> tablesRootClazz) throws Exception;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,8 @@ import cc.carm.lib.easysql.api.SQLManager;
|
|||||||
import cc.carm.lib.easysql.api.SQLQuery;
|
import cc.carm.lib.easysql.api.SQLQuery;
|
||||||
import cc.carm.plugin.minesql.api.SQLRegistry;
|
import cc.carm.plugin.minesql.api.SQLRegistry;
|
||||||
import cc.carm.plugin.minesql.api.source.SQLSourceConfig;
|
import cc.carm.plugin.minesql.api.source.SQLSourceConfig;
|
||||||
|
import cc.carm.plugin.minesql.api.table.SQLTablesRoot;
|
||||||
|
import cc.carm.plugin.minesql.api.table.SimpleSQLTable;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
@ -114,4 +116,23 @@ public class MineSQL {
|
|||||||
shutdown(manager, true);
|
shutdown(manager, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 读取一个 {@link SQLTablesRoot} 中全部的 {@link SimpleSQLTable} 实例并初始化。
|
||||||
|
*
|
||||||
|
* @param tablesRoot {@link SQLTablesRoot}实例
|
||||||
|
*/
|
||||||
|
public static void createTables(@NotNull SQLTablesRoot tablesRoot) throws Exception {
|
||||||
|
instance.createTables(tablesRoot);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 读取一个 {@link SQLTablesRoot}类中 中全部的静态 {@link SimpleSQLTable} 实例并初始化。
|
||||||
|
*
|
||||||
|
* @param tablesRootClazz {@link SQLTablesRoot}静态类
|
||||||
|
*/
|
||||||
|
public static void createTables(@NotNull Class<? extends SQLTablesRoot> tablesRootClazz) throws Exception {
|
||||||
|
instance.createTables(tablesRootClazz);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,12 @@
|
|||||||
|
package cc.carm.plugin.minesql.api.table;
|
||||||
|
|
||||||
|
import cc.carm.lib.easysql.api.function.SQLHandler;
|
||||||
|
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 表声明类的根节点,用于标注该类用于记录表的结构信息。
|
||||||
|
* <br> 创建表请使用 {@link SimpleSQLTable#of(String, String, Supplier, SQLHandler)}。
|
||||||
|
*/
|
||||||
|
public abstract class SQLTablesRoot {
|
||||||
|
}
|
@ -0,0 +1,156 @@
|
|||||||
|
package cc.carm.plugin.minesql.api.table;
|
||||||
|
|
||||||
|
import cc.carm.lib.easysql.api.SQLManager;
|
||||||
|
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.plugin.minesql.MineSQL;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
|
public class SimpleSQLTable {
|
||||||
|
|
||||||
|
public static @NotNull SimpleSQLTable of(@NotNull String tableName,
|
||||||
|
@NotNull SQLHandler<TableCreateBuilder> tableBuilder) {
|
||||||
|
return new SimpleSQLTable(null, tableName, null, tableBuilder);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static @NotNull SimpleSQLTable of(@Nullable String database, @NotNull String tableName,
|
||||||
|
@NotNull SQLHandler<TableCreateBuilder> tableBuilder) {
|
||||||
|
return new SimpleSQLTable(database, tableName, null, tableBuilder);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static @NotNull SimpleSQLTable of(@Nullable String database, @NotNull String tableName,
|
||||||
|
@Nullable String tablePrefix, @NotNull SQLHandler<TableCreateBuilder> tableBuilder) {
|
||||||
|
return new SimpleSQLTable(database, tableName, () -> tablePrefix, tableBuilder);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static @NotNull SimpleSQLTable of(@Nullable String database, @NotNull String tableName,
|
||||||
|
@Nullable Supplier<String> tablePrefix, @NotNull SQLHandler<TableCreateBuilder> tableBuilder) {
|
||||||
|
return new SimpleSQLTable(database, tableName, tablePrefix, tableBuilder);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected final @Nullable String database;
|
||||||
|
protected final @NotNull String tableName;
|
||||||
|
|
||||||
|
protected final @Nullable Supplier<String> tablePrefix;
|
||||||
|
protected final @NotNull SQLHandler<TableCreateBuilder> tableCreator;
|
||||||
|
|
||||||
|
public SimpleSQLTable(@Nullable String database, @NotNull String tableName,
|
||||||
|
@Nullable Supplier<String> tablePrefix, @NotNull SQLHandler<TableCreateBuilder> table) {
|
||||||
|
this.database = database;
|
||||||
|
this.tableName = tableName;
|
||||||
|
this.tablePrefix = tablePrefix;
|
||||||
|
this.tableCreator = table;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean create() throws SQLException {
|
||||||
|
SQLManager sqlManager = getSQLManager();
|
||||||
|
if (sqlManager == null) throw new SQLException(getExceptionReason());
|
||||||
|
|
||||||
|
TableCreateBuilder tableBuilder = sqlManager.createTable(getTableName());
|
||||||
|
tableCreator.accept(tableBuilder);
|
||||||
|
return tableBuilder.build().executeFunction(l -> l > 0, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public @Nullable String getDatabase() {
|
||||||
|
return database;
|
||||||
|
}
|
||||||
|
|
||||||
|
public @Nullable SQLManager getSQLManager() {
|
||||||
|
return MineSQL.getRegistry().get(getDatabase());
|
||||||
|
}
|
||||||
|
|
||||||
|
public @NotNull String getTableName() {
|
||||||
|
String prefix = getTablePrefix();
|
||||||
|
return (prefix != null ? prefix : "") + tableName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public @Nullable String getTablePrefix() {
|
||||||
|
return Optional.ofNullable(tablePrefix).map(Supplier::get).orElse(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public @NotNull TableQueryBuilder createQuery() {
|
||||||
|
return Optional.ofNullable(getSQLManager()).map(this::createQuery)
|
||||||
|
.orElseThrow(() -> new NullPointerException(getExceptionReason()));
|
||||||
|
}
|
||||||
|
|
||||||
|
public @NotNull TableQueryBuilder createQuery(@NotNull SQLManager sqlManager) {
|
||||||
|
return sqlManager.createQuery().inTable(getTableName());
|
||||||
|
}
|
||||||
|
|
||||||
|
public @NotNull DeleteBuilder createDelete() {
|
||||||
|
return Optional.ofNullable(getSQLManager()).map(this::createDelete)
|
||||||
|
.orElseThrow(() -> new NullPointerException(getExceptionReason()));
|
||||||
|
}
|
||||||
|
|
||||||
|
public @NotNull DeleteBuilder createDelete(@NotNull SQLManager sqlManager) {
|
||||||
|
return sqlManager.createDelete(getTableName());
|
||||||
|
}
|
||||||
|
|
||||||
|
public @NotNull UpdateBuilder createUpdate() {
|
||||||
|
return Optional.ofNullable(getSQLManager()).map(this::createUpdate)
|
||||||
|
.orElseThrow(() -> new NullPointerException(getExceptionReason()));
|
||||||
|
}
|
||||||
|
|
||||||
|
public @NotNull UpdateBuilder createUpdate(@NotNull SQLManager sqlManager) {
|
||||||
|
return sqlManager.createUpdate(getTableName());
|
||||||
|
}
|
||||||
|
|
||||||
|
public @NotNull InsertBuilder<PreparedSQLUpdateAction<Integer>> createInsert() {
|
||||||
|
return Optional.ofNullable(getSQLManager()).map(this::createInsert)
|
||||||
|
.orElseThrow(() -> new NullPointerException(getExceptionReason()));
|
||||||
|
}
|
||||||
|
|
||||||
|
public @NotNull InsertBuilder<PreparedSQLUpdateAction<Integer>> createInsert(@NotNull SQLManager sqlManager) {
|
||||||
|
return sqlManager.createInsert(getTableName());
|
||||||
|
}
|
||||||
|
|
||||||
|
public @NotNull InsertBuilder<PreparedSQLUpdateBatchAction<Integer>> createInsertBatch() {
|
||||||
|
return Optional.ofNullable(getSQLManager()).map(this::createInsertBatch)
|
||||||
|
.orElseThrow(() -> new NullPointerException(getExceptionReason()));
|
||||||
|
}
|
||||||
|
|
||||||
|
public @NotNull InsertBuilder<PreparedSQLUpdateBatchAction<Integer>> createInsertBatch(@NotNull SQLManager sqlManager) {
|
||||||
|
return sqlManager.createInsertBatch(getTableName());
|
||||||
|
}
|
||||||
|
|
||||||
|
public @NotNull ReplaceBuilder<PreparedSQLUpdateAction<Integer>> createReplace() {
|
||||||
|
return Optional.ofNullable(getSQLManager()).map(this::createReplace)
|
||||||
|
.orElseThrow(() -> new NullPointerException(getExceptionReason()));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public @NotNull ReplaceBuilder<PreparedSQLUpdateAction<Integer>> createReplace(@NotNull SQLManager sqlManager) {
|
||||||
|
return sqlManager.createReplace(getTableName());
|
||||||
|
}
|
||||||
|
|
||||||
|
public @NotNull ReplaceBuilder<PreparedSQLUpdateBatchAction<Integer>> createReplaceBatch() {
|
||||||
|
return Optional.ofNullable(getSQLManager()).map(this::createReplaceBatch)
|
||||||
|
.orElseThrow(() -> new NullPointerException(getExceptionReason()));
|
||||||
|
}
|
||||||
|
|
||||||
|
public @NotNull ReplaceBuilder<PreparedSQLUpdateBatchAction<Integer>> createReplaceBatch(@NotNull SQLManager sqlManager) {
|
||||||
|
return sqlManager.createReplaceBatch(getTableName());
|
||||||
|
}
|
||||||
|
|
||||||
|
public @NotNull TableAlterBuilder alter() {
|
||||||
|
return Optional.ofNullable(getSQLManager()).map(this::alter)
|
||||||
|
.orElseThrow(() -> new NullPointerException(getExceptionReason()));
|
||||||
|
}
|
||||||
|
|
||||||
|
public @NotNull TableAlterBuilder alter(@NotNull SQLManager sqlManager) {
|
||||||
|
return sqlManager.alterTable(getTableName());
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getExceptionReason() {
|
||||||
|
if (getDatabase() == null) return "Cannot find any SQLManager.";
|
||||||
|
else return "Cannot find any SQLManager for \"" + getDatabase() + "\".";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -5,7 +5,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>minesql-parent</artifactId>
|
<artifactId>minesql-parent</artifactId>
|
||||||
<groupId>cc.carm.plugin</groupId>
|
<groupId>cc.carm.plugin</groupId>
|
||||||
<version>1.1.1</version>
|
<version>1.2.0</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<properties>
|
<properties>
|
||||||
|
@ -8,6 +8,8 @@ import cc.carm.lib.easysql.api.SQLQuery;
|
|||||||
import cc.carm.lib.easysql.manager.SQLManagerImpl;
|
import cc.carm.lib.easysql.manager.SQLManagerImpl;
|
||||||
import cc.carm.lib.githubreleases4j.GithubReleases4J;
|
import cc.carm.lib.githubreleases4j.GithubReleases4J;
|
||||||
import cc.carm.plugin.minesql.api.source.SQLSourceConfig;
|
import cc.carm.plugin.minesql.api.source.SQLSourceConfig;
|
||||||
|
import cc.carm.plugin.minesql.api.table.SQLTablesRoot;
|
||||||
|
import cc.carm.plugin.minesql.api.table.SimpleSQLTable;
|
||||||
import cc.carm.plugin.minesql.command.MineSQLCommand;
|
import cc.carm.plugin.minesql.command.MineSQLCommand;
|
||||||
import cc.carm.plugin.minesql.command.MineSQLHelpFormatter;
|
import cc.carm.plugin.minesql.command.MineSQLHelpFormatter;
|
||||||
import cc.carm.plugin.minesql.conf.PluginConfiguration;
|
import cc.carm.plugin.minesql.conf.PluginConfiguration;
|
||||||
@ -23,6 +25,8 @@ import org.jetbrains.annotations.Nullable;
|
|||||||
|
|
||||||
import javax.sql.DataSource;
|
import javax.sql.DataSource;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
import java.sql.SQLException;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
@ -157,6 +161,48 @@ public class MineSQLCore implements IMineSQL {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void createTables(@NotNull SQLTablesRoot tablesRoot) throws SQLException {
|
||||||
|
for (Field field : tablesRoot.getClass().getDeclaredFields()) {
|
||||||
|
initializeTableField(tablesRoot, field);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void createTables(@NotNull Class<? extends SQLTablesRoot> clazz) throws SQLException {
|
||||||
|
initializeTableClass(clazz);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void initializeTableClass(@NotNull Class<?> clazz) throws SQLException {
|
||||||
|
if (!SQLTablesRoot.class.isAssignableFrom(clazz)) return;
|
||||||
|
|
||||||
|
for (Field field : clazz.getDeclaredFields()) {
|
||||||
|
initializeTableField(clazz, field);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (Class<?> subClass : clazz.getDeclaredClasses()) {
|
||||||
|
initializeTableClass(subClass);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void initializeTableField(@NotNull Object source, @NotNull Field field) throws SQLException {
|
||||||
|
try {
|
||||||
|
field.setAccessible(true);
|
||||||
|
Object object = field.get(source);
|
||||||
|
|
||||||
|
if (object instanceof SimpleSQLTable) {
|
||||||
|
((SimpleSQLTable) object).create();
|
||||||
|
} else if (source instanceof SQLTablesRoot && object instanceof SQLTablesRoot) {
|
||||||
|
createTables((SQLTablesRoot) object);
|
||||||
|
} else if (source instanceof Class<?> && object instanceof Class<?>) {
|
||||||
|
// 当且仅当 源字段与字段 均为静态类时,才对目标字段进行下一步初始化加载。
|
||||||
|
initializeTableClass((Class<?>) object);
|
||||||
|
}
|
||||||
|
} catch (IllegalAccessException ignored) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void shutdownAll() {
|
public void shutdownAll() {
|
||||||
this.registry.getManagers().forEach((k, manager) -> {
|
this.registry.getManagers().forEach((k, manager) -> {
|
||||||
getLogger().info(" 正在关闭数据库 " + k + "...");
|
getLogger().info(" 正在关闭数据库 " + k + "...");
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>minesql-parent</artifactId>
|
<artifactId>minesql-parent</artifactId>
|
||||||
<groupId>cc.carm.plugin</groupId>
|
<groupId>cc.carm.plugin</groupId>
|
||||||
<version>1.1.1</version>
|
<version>1.2.0</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>minesql-parent</artifactId>
|
<artifactId>minesql-parent</artifactId>
|
||||||
<groupId>cc.carm.plugin</groupId>
|
<groupId>cc.carm.plugin</groupId>
|
||||||
<version>1.1.1</version>
|
<version>1.2.0</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>minesql-parent</artifactId>
|
<artifactId>minesql-parent</artifactId>
|
||||||
<groupId>cc.carm.plugin</groupId>
|
<groupId>cc.carm.plugin</groupId>
|
||||||
<version>1.1.1</version>
|
<version>1.2.0</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>minesql-parent</artifactId>
|
<artifactId>minesql-parent</artifactId>
|
||||||
<groupId>cc.carm.plugin</groupId>
|
<groupId>cc.carm.plugin</groupId>
|
||||||
<version>1.1.1</version>
|
<version>1.2.0</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<properties>
|
<properties>
|
||||||
|
2
pom.xml
2
pom.xml
@ -24,7 +24,7 @@
|
|||||||
<groupId>cc.carm.plugin</groupId>
|
<groupId>cc.carm.plugin</groupId>
|
||||||
<artifactId>minesql-parent</artifactId>
|
<artifactId>minesql-parent</artifactId>
|
||||||
<packaging>pom</packaging>
|
<packaging>pom</packaging>
|
||||||
<version>1.1.1</version>
|
<version>1.2.0</version>
|
||||||
<modules>
|
<modules>
|
||||||
<module>api</module>
|
<module>api</module>
|
||||||
<module>core</module>
|
<module>core</module>
|
||||||
|
Loading…
Reference in New Issue
Block a user