diff --git a/providers/sql/src/main/java/cc/carm/lib/configuration/source/sql/SQLConfigFactory.java b/providers/sql/src/main/java/cc/carm/lib/configuration/source/sql/SQLConfigFactory.java index 16e5033..0ce5f13 100644 --- a/providers/sql/src/main/java/cc/carm/lib/configuration/source/sql/SQLConfigFactory.java +++ b/providers/sql/src/main/java/cc/carm/lib/configuration/source/sql/SQLConfigFactory.java @@ -7,12 +7,17 @@ import cc.carm.lib.configuration.source.ConfigurationFactory; import cc.carm.lib.configuration.source.ConfigurationHolder; import cc.carm.lib.configuration.versioned.VersionedMetaTypes; 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.function.SQLHandler; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Range; import java.util.HashMap; +import java.util.function.BiConsumer; import java.util.function.Consumer; import java.util.function.Supplier; @@ -26,11 +31,43 @@ public class SQLConfigFactory extends ConfigurationFactory manager); } + protected static final @NotNull Gson DEFAULT_GSON = new GsonBuilder() + .serializeNulls().disableHtmlEscaping().create(); + + protected static final @NotNull BiConsumer DEFAULT_TABLE_SCHEMA = (tableName, builder) -> { + builder.addColumn("namespace", "VARCHAR(32) NOT NULL"); + builder.addColumn("path", "VARCHAR(96) NOT NULL"); + + builder.addColumn("value", "TEXT"); + + builder.addColumn("inline_comment", "TEXT"); + builder.addColumn("header_comments", "MEDIUMTEXT"); + builder.addColumn("footer_comments", "MEDIUMTEXT"); + + builder.addColumn("type", "TINYINT NOT NULL DEFAULT 0"); + builder.addColumn("version", "MEDIUMINT UNSIGNED NOT NULL DEFAULT 0"); + + builder.addColumn( + "create_time", + "TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP" + ); + builder.addColumn( + "update_time", + "TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP" + ); + + builder.setIndex( + IndexType.PRIMARY_KEY, "pk_" + tableName.toLowerCase(), + "namespace", "path" + ); + builder.setTableSettings("ENGINE=InnoDB DEFAULT CHARSET=utf8mb4"); + }; + protected @NotNull Supplier managerSupplier; - protected Supplier gsonSupplier = () -> SQLSource.DEFAULT_GSON; + protected Supplier gsonSupplier = () -> DEFAULT_GSON; protected HashMap> resolvers = new HashMap<>(SQLValueResolver.STANDARD_RESOLVERS); - protected String tableName = "configs"; + protected SQLTable tableName = SQLTable.of("config", (table) -> DEFAULT_TABLE_SCHEMA.accept("config", table)); protected String namespace = "default"; public SQLConfigFactory(@NotNull Supplier managerSupplier) { @@ -95,11 +132,19 @@ public class SQLConfigFactory extends ConfigurationFactory builder) { + return table(SQLTable.of(tableName, builder)); + } + + public SQLConfigFactory tableName(@NotNull String tableName) { + return table(tableName, table -> DEFAULT_TABLE_SCHEMA.accept(tableName, table)); + } + public SQLConfigFactory namespace(@NotNull String namespace) { this.namespace = namespace; return self(); diff --git a/providers/sql/src/main/java/cc/carm/lib/configuration/source/sql/SQLSource.java b/providers/sql/src/main/java/cc/carm/lib/configuration/source/sql/SQLSource.java index 8cddcfe..1544c2c 100644 --- a/providers/sql/src/main/java/cc/carm/lib/configuration/source/sql/SQLSource.java +++ b/providers/sql/src/main/java/cc/carm/lib/configuration/source/sql/SQLSource.java @@ -9,9 +9,7 @@ import cc.carm.lib.configuration.versioned.VersionedMetaTypes; import cc.carm.lib.easysql.api.SQLManager; import cc.carm.lib.easysql.api.SQLQuery; import cc.carm.lib.easysql.api.SQLTable; -import cc.carm.lib.easysql.api.enums.IndexType; import com.google.gson.Gson; -import com.google.gson.GsonBuilder; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -21,9 +19,6 @@ import java.util.*; public class SQLSource extends ConfigureSource, SQLSource> { - protected static final @NotNull Gson DEFAULT_GSON = new GsonBuilder() - .serializeNulls().disableHtmlEscaping().create(); - protected final @NotNull Gson gson; protected final @NotNull SQLManager sqlManager; protected final @NotNull String namespace; @@ -35,41 +30,13 @@ public class SQLSource extends ConfigureSource holder, long lastUpdateMillis, @NotNull Gson gson, @NotNull SQLManager sqlManager, @NotNull Map> resolvers, - @NotNull String tableName, @NotNull String namespace) { + @NotNull SQLTable table, @NotNull String namespace) { super(holder, lastUpdateMillis); this.gson = gson; this.sqlManager = sqlManager; this.resolvers = resolvers; this.namespace = namespace; - this.table = SQLTable.of(tableName, builder -> { - - builder.addColumn("namespace", "VARCHAR(32) NOT NULL"); - builder.addColumn("path", "VARCHAR(96) NOT NULL"); - - builder.addColumn("value", "TEXT"); - - builder.addColumn("inline_comment", "TEXT"); - builder.addColumn("header_comments", "MEDIUMTEXT"); - builder.addColumn("footer_comments", "MEDIUMTEXT"); - - builder.addColumn("type", "TINYINT NOT NULL DEFAULT 0"); - builder.addColumn("version", "MEDIUMINT UNSIGNED NOT NULL DEFAULT 0"); - - builder.addColumn( - "create_time", - "TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP" - ); - builder.addColumn( - "update_time", - "TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP" - ); - - builder.setIndex( - IndexType.PRIMARY_KEY, "pk_" + tableName.toLowerCase(), - "namespace", "path" - ); - builder.setTableSettings("ENGINE=InnoDB DEFAULT CHARSET=utf8mb4"); - }); + this.table = table; try { this.table.create(this.sqlManager);