mirror of
https://github.com/CarmJos/MineSQL.git
synced 2026-06-04 16:43:03 +08:00
feat(table): 添加单独的表声明类及快速初始化表方法。
This commit is contained in:
+1
-1
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>minesql-parent</artifactId>
|
||||
<groupId>cc.carm.plugin</groupId>
|
||||
<version>1.1.1</version>
|
||||
<version>1.2.0</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<properties>
|
||||
|
||||
@@ -8,6 +8,8 @@ import cc.carm.lib.easysql.api.SQLQuery;
|
||||
import cc.carm.lib.easysql.manager.SQLManagerImpl;
|
||||
import cc.carm.lib.githubreleases4j.GithubReleases4J;
|
||||
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.MineSQLHelpFormatter;
|
||||
import cc.carm.plugin.minesql.conf.PluginConfiguration;
|
||||
@@ -23,6 +25,8 @@ import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.io.File;
|
||||
import java.lang.reflect.Field;
|
||||
import java.sql.SQLException;
|
||||
import java.util.*;
|
||||
import java.util.function.Consumer;
|
||||
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() {
|
||||
this.registry.getManagers().forEach((k, manager) -> {
|
||||
getLogger().info(" 正在关闭数据库 " + k + "...");
|
||||
|
||||
Reference in New Issue
Block a user