diff --git a/pom.xml b/pom.xml index 05bae52..8445636 100644 --- a/pom.xml +++ b/pom.xml @@ -25,8 +25,9 @@ providers/yaml providers/gson - - + providers/hocon + providers/sql + providers/mongodb demo diff --git a/providers/mongodb/pom.xml b/providers/mongodb/pom.xml new file mode 100644 index 0000000..ec26ee8 --- /dev/null +++ b/providers/mongodb/pom.xml @@ -0,0 +1,95 @@ + + + + easyconfiguration-parent + cc.carm.lib + 4.0.0 + ../../pom.xml + + 4.0.0 + + ${project.jdk.version} + ${project.jdk.version} + UTF-8 + UTF-8 + + 2.24.3 + + easyconfiguration-mongodb + + + + + ${project.parent.groupId} + easyconfiguration-core + ${project.parent.version} + compile + + + + ${project.parent.groupId} + easyconfiguration-gson + ${project.parent.version} + compile + + + + org.mongodb + mongodb-driver-sync + 5.3.0 + + + + ${project.parent.groupId} + easyconfiguration-demo + ${project.parent.version} + test + + + + org.apache.logging.log4j + log4j-api + ${log4j.version} + test + + + + org.apache.logging.log4j + log4j-core + ${log4j.version} + test + + + + org.apache.logging.log4j + log4j-slf4j-impl + ${log4j.version} + test + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + + org.apache.maven.plugins + maven-jar-plugin + + + org.apache.maven.plugins + maven-source-plugin + + + org.apache.maven.plugins + maven-javadoc-plugin + + + + + \ No newline at end of file diff --git a/providers/mongodb/src/main/java/cc/carm/lib/configuration/source/mongodb/MongoSource.java b/providers/mongodb/src/main/java/cc/carm/lib/configuration/source/mongodb/MongoSource.java new file mode 100644 index 0000000..c915857 --- /dev/null +++ b/providers/mongodb/src/main/java/cc/carm/lib/configuration/source/mongodb/MongoSource.java @@ -0,0 +1,4 @@ +package cc.carm.lib.configuration.source.mongodb; + +public class MongoSource { +} diff --git a/providers/mongodb/src/test/resources/log4j2.xml b/providers/mongodb/src/test/resources/log4j2.xml new file mode 100644 index 0000000..f06fcad --- /dev/null +++ b/providers/mongodb/src/test/resources/log4j2.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + + + + + diff --git a/providers/sql/README.md b/providers/sql/README.md index c2b19fd..6724c5a 100644 --- a/providers/sql/README.md +++ b/providers/sql/README.md @@ -6,15 +6,16 @@ SQL database implementation, support for MySQL or MariaDB. ```mysql CREATE TABLE IF NOT EXISTS conf ( - `namespace` VARCHAR(32) NOT NULL, # 命名空间 (代表其属于谁,类似于单个配置文件地址的概念) - `path` VARCHAR(96) NOT NULL, # 配置路径 (ConfigPath) - `type` TINYINT UNSIGNED NOT NULL DEFAULT 0, # 数据类型 (Integer/Byte/List/Map/...) - `value` MEDIUMTEXT, # 配置项的值 (可能为JSON格式) - `usage` TEXT, # 配置项的用法,本质是行内注释 - `descriptions` MEDIUMTEXT, # 配置项的描述,本质是顶部注释 - `version` INT UNSIGNED NOT NULL DEFAULT 0, # 配置项的版本 - `create_time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, # 创建时间 - `update_time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + `namespace` VARCHAR(32) NOT NULL, # 命名空间 (代表其属于谁,类似于单个配置文件地址的概念) + `path` VARCHAR(96) NOT NULL, # 配置路径 (ConfigPath) + `type` TINYINT UNSIGNED NOT NULL DEFAULT 0, # 数据类型 (Integer/Byte/List/Map/...) + `value` MEDIUMTEXT, # 配置项的值 (可能为JSON格式) + `inline_comment` TEXT, # 配置项的用法,本质是行内注释 + `header_comment` MEDIUMTEXT, # 配置项的描述,本质是顶部注释 + `footer_comment` MEDIUMTEXT, # 配置项的描述,本质是顶部注释 + `version` INT UNSIGNED NOT NULL DEFAULT 0, # 配置项的版本 + `create_time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, # 创建时间 + `update_time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (`namespace`, `path`) ) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4; diff --git a/providers/sql/pom.xml b/providers/sql/pom.xml index dd35069..cc0f363 100644 --- a/providers/sql/pom.xml +++ b/providers/sql/pom.xml @@ -2,13 +2,13 @@ + 4.0.0 easyconfiguration-parent cc.carm.lib - 3.9.1 + 4.0.0 ../../pom.xml - 4.0.0 ${project.jdk.version} ${project.jdk.version} @@ -27,6 +27,26 @@ ${project.parent.version} compile + + ${project.parent.groupId} + easyconfiguration-feature-commentable + ${project.parent.version} + compile + + + + ${project.parent.groupId} + easyconfiguration-feature-section + ${project.parent.version} + compile + + + + ${project.parent.groupId} + easyconfiguration-feature-versioned + ${project.parent.version} + compile + com.google.code.gson diff --git a/providers/sql/src/main/java/cc/carm/lib/configuration/EasyConfiguration.java b/providers/sql/src/main/java/cc/carm/lib/configuration/EasyConfiguration.java deleted file mode 100644 index c027e5d..0000000 --- a/providers/sql/src/main/java/cc/carm/lib/configuration/EasyConfiguration.java +++ /dev/null @@ -1,26 +0,0 @@ -package cc.carm.lib.configuration; - -import cc.carm.lib.configuration.sql.SQLConfigProvider; -import cc.carm.lib.easysql.api.SQLManager; -import org.jetbrains.annotations.NotNull; - -public class EasyConfiguration { - - private EasyConfiguration() { - } - - public static SQLConfigProvider from(@NotNull SQLManager sqlManager, @NotNull String tableName, @NotNull String namespace) { - SQLConfigProvider provider = new SQLConfigProvider(sqlManager, tableName, namespace); - try { - provider.initializeConfig(); - } catch (Exception e) { - e.printStackTrace(); - } - return provider; - } - - public static SQLConfigProvider from(@NotNull SQLManager sqlManager, @NotNull String tableName) { - return from(sqlManager, tableName, "base"); - } - -} diff --git a/providers/sql/src/main/java/cc/carm/lib/configuration/source/sql/SQLOptions.java b/providers/sql/src/main/java/cc/carm/lib/configuration/source/sql/SQLOptions.java new file mode 100644 index 0000000..2777d98 --- /dev/null +++ b/providers/sql/src/main/java/cc/carm/lib/configuration/source/sql/SQLOptions.java @@ -0,0 +1,6 @@ +package cc.carm.lib.configuration.source.sql; + +public interface SQLOptions { + + +} 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 new file mode 100644 index 0000000..4307766 --- /dev/null +++ b/providers/sql/src/main/java/cc/carm/lib/configuration/source/sql/SQLSource.java @@ -0,0 +1,136 @@ +package cc.carm.lib.configuration.source.sql; + +import cc.carm.lib.configuration.source.ConfigurationHolder; +import cc.carm.lib.configuration.source.section.ConfigureSource; +import cc.carm.lib.configuration.source.section.MemorySection; +import cc.carm.lib.easysql.api.SQLManager; +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 java.util.*; + +public class SQLSource extends ConfigureSource, SQLSource> { + + protected static final @NotNull Gson DEFAULT_GSON = new GsonBuilder() + .serializeNulls().disableHtmlEscaping().setPrettyPrinting() + .create(); + + protected final @NotNull Gson gson; + protected final @NotNull SQLManager sqlManager; + protected final @NotNull String namespace; + protected final @NotNull SQLTable table; + + protected final @NotNull Set updated = new HashSet<>(); + protected MemorySection rootSection; + + public SQLSource(@NotNull ConfigurationHolder holder, long lastUpdateMillis, + @NotNull Gson gson, @NotNull SQLManager sqlManager, @NotNull String tableName, @NotNull String namespace) { + super(holder, lastUpdateMillis); + this.gson = gson; + this.sqlManager = sqlManager; + 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("type", "TINYINT NOT NULL DEFAULT 0"); + builder.addColumn("value", "TEXT"); + + builder.addColumn("inline_comment", "TEXT"); + builder.addColumn("header_comments", "MEDIUMTEXT"); + builder.addColumn("footer_comments", "MEDIUMTEXT"); + + 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"); + + builder.setIndex( + IndexType.PRIMARY_KEY, "pk_" + tableName.toLowerCase(), + "namespace", "path" + ); + builder.setTableSettings("ENGINE=InnoDB DEFAULT CHARSET=utf8mb4"); + }); + } + + @Override + protected SQLSource self() { + return this; + } + + @Override + public @NotNull Map original() { + return section().data(); + } + + @Override + public @NotNull MemorySection section() { + return Objects.requireNonNull(this.rootSection, "Root section is not initialized."); + } + + @Override + public void save() throws Exception { + if (this.updated.isEmpty()) return; // Nothing to save + + Date date = new Date(); // Update time + List values = new ArrayList<>(); + + for (String path : this.updated) { + Object value = get(path); + +// if (value instanceof SQLConfigWrapper) { +// value = getSourceMap(((SQLConfigWrapper) value).getSource()); +// } +// +// SQLValueResolver type = SQLValueTypes.get(value.getClass()); +// if (type != null) { +// values.add(new Object[]{ +// this.namespace, path, date, +// type.getID(), type.serializeObject(value), +// getComments().getInlineComment(path), +// GSON.toJson(getComments().getHeaderComment(path)) +// }); +// } + } + + this.updated.clear(); + + this.table.createReplaceBatch() + .setColumnNames("namespace", "path", "update_time", "type", "value", "inline_comment", "header_comments") + .setAllParams(values) + .execute(); + } + + @Override + protected void onReload() throws Exception { + LinkedHashMap values = new LinkedHashMap<>(); + +// try (SQLQuery query = this.table.createQuery() +// .addCondition("namespace", namespace) +// .build().execute()) { +// ResultSet rs = query.getResultSet(); +// while (rs.next()) { +// String path = rs.getString("path"); +// int type = rs.getInt("type"); +// try { +// SQLValueResolver resolver = SQLValueTypes.get(type); +// if (resolver == null) throw new IllegalStateException("No resolver for type #" + type); +// String value = rs.getString("value"); +// values.put(path, resolver.resolve(value)); +// +// loadInlineComment(path, rs.getString("inline_comment")); +// loadHeaderComment(path, rs.getString("header_comments")); +// } catch (Exception ex) { +// ex.printStackTrace(); +// } +// } +// } +// +// this.rootConfiguration = new SQLConfigWrapper(this, values); + } + +} diff --git a/providers/sql/src/main/java/cc/carm/lib/configuration/sql/SQLConfigProvider.java b/providers/sql/src/main/java/cc/carm/lib/configuration/sql/SQLConfigProvider.java deleted file mode 100644 index e2f4ea3..0000000 --- a/providers/sql/src/main/java/cc/carm/lib/configuration/sql/SQLConfigProvider.java +++ /dev/null @@ -1,184 +0,0 @@ -package cc.carm.lib.configuration.sql; - -import cc.carm.lib.configuration.source.comment.ConfigurationComments; -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 com.google.gson.reflect.TypeToken; -import org.jetbrains.annotations.NotNull; - -import java.sql.ResultSet; -import java.util.*; - -public class SQLConfigProvider extends ConfigurationProvider { - - public static Gson GSON = new GsonBuilder().serializeNulls().disableHtmlEscaping().setPrettyPrinting().create(); - - protected final @NotNull SQLManager sqlManager; - protected final @NotNull SQLTable table; - protected final @NotNull String namespace; - - protected ConfigInitializer initializer; - protected ConfigurationComments comments = new ConfigurationComments(); - protected SQLConfigWrapper rootConfiguration; - - protected final @NotNull Set updated = new HashSet<>(); - - public SQLConfigProvider(@NotNull SQLManager sqlManager, @NotNull String tableName, @NotNull String namespace) { - this.sqlManager = sqlManager; - this.table = SQLTable.of(tableName, builder -> { - - builder.addColumn("namespace", "VARCHAR(32) NOT NULL"); - builder.addColumn("path", "VARCHAR(96) NOT NULL"); - - builder.addColumn("type", "TINYINT NOT NULL DEFAULT 0"); - builder.addColumn("value", "TEXT"); - - builder.addColumn("inline_comment", "TEXT"); - builder.addColumn("header_comments", "MEDIUMTEXT"); - - builder.addColumn("create_time", "TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP"); - builder.addColumn("update_time", "TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP"); - - builder.setIndex( - IndexType.PRIMARY_KEY, "pk_" + tableName.toLowerCase(), - "namespace", "path" - ); - builder.setTableSettings("ENGINE=InnoDB DEFAULT CHARSET=utf8mb4"); - }); - this.namespace = namespace; - } - - @Override - public @NotNull SQLConfigWrapper getConfiguration() { - return rootConfiguration; - } - - public void initializeConfig() throws Exception { - this.table.create(this.sqlManager); - this.initializer = new ConfigInitializer<>(this); - onReload(); - } - - @Override - protected void onReload() throws Exception { - this.comments = new ConfigurationComments(); - LinkedHashMap values = new LinkedHashMap<>(); - - try (SQLQuery query = this.table.createQuery() - .addCondition("namespace", namespace) - .build().execute()) { - ResultSet rs = query.getResultSet(); - while (rs.next()) { - String path = rs.getString("path"); - int type = rs.getInt("type"); - try { - SQLValueResolver resolver = SQLValueTypes.get(type); - if (resolver == null) throw new IllegalStateException("No resolver for type #" + type); - String value = rs.getString("value"); - values.put(path, resolver.resolve(value)); - - loadInlineComment(path, rs.getString("inline_comment")); - loadHeaderComment(path, rs.getString("header_comments")); - } catch (Exception ex) { - ex.printStackTrace(); - } - } - } - - this.rootConfiguration = new SQLConfigWrapper(this, values); - } - - @Override - public void save() throws Exception { - if (this.updated.isEmpty()) return; - - Date date = new Date(); - List values = new ArrayList<>(); - List deletes = new ArrayList<>(); - - for (String path : this.updated) { - Object value = this.rootConfiguration.get(path); - if (value == null) { - deletes.add(path); - continue; - } - - if (value instanceof SQLConfigWrapper) { - value = getSourceMap(((SQLConfigWrapper) value).getSource()); - } - - SQLValueResolver type = SQLValueTypes.get(value.getClass()); - if (type != null) { - values.add(new Object[]{ - this.namespace, path, date, - type.getID(), type.serializeObject(value), - getComments().getInlineComment(path), - GSON.toJson(getComments().getHeaderComment(path)) - }); - } - } - - this.updated.clear(); - - this.table.createReplaceBatch() - .setColumnNames("namespace", "path", "update_time", "type", "value", "inline_comment", "header_comments") - .setAllParams(values) - .execute(); - - for (String path : deletes) { - this.table.createDelete() - .addCondition("namespace", this.namespace) - .addCondition("path", path) - .build().execute(); - } - } - - - @Override - public @NotNull ConfigurationComments getComments() { - return this.comments; - } - - @Override - public @NotNull ConfigInitializer> getInitializer() { - return this.initializer; - } - - - protected void loadInlineComment(String path, String comment) { - if (comment == null) return; - comment = comment.trim(); - if (comment.isEmpty()) return; - - this.comments.setInlineComment(path, comment); - } - - protected void loadHeaderComment(String path, String commentJson) { - if (commentJson == null) return; - commentJson = commentJson.trim(); - if (commentJson.isEmpty()) return; - - List headerComments = GSON.fromJson(commentJson, new TypeToken>() { - }.getType()); - if (headerComments == null || headerComments.isEmpty()) return; - - this.comments.setHeaderComments(path, headerComments); - } - - protected static Map getSourceMap(Map map) { - Map source = new LinkedHashMap<>(); - for (Map.Entry entry : map.entrySet()) { - if (entry.getValue() instanceof SQLConfigWrapper) { - source.put(entry.getKey(), getSourceMap(((SQLConfigWrapper) entry.getValue()).getSource())); - } else { - source.put(entry.getKey(), entry.getValue()); - } - } - return source; - } - -} diff --git a/providers/sql/src/main/java/cc/carm/lib/configuration/sql/SQLConfigWrapper.java b/providers/sql/src/main/java/cc/carm/lib/configuration/sql/SQLConfigWrapper.java deleted file mode 100644 index 060268d..0000000 --- a/providers/sql/src/main/java/cc/carm/lib/configuration/sql/SQLConfigWrapper.java +++ /dev/null @@ -1,121 +0,0 @@ -package cc.carm.lib.configuration.sql; - -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; -import java.util.Set; - -/** - * For SQL configs, that primary path will be directly mapped to the value. - * - * @author CarmJos - */ -public class SQLConfigWrapper implements ConfigurationWrapper> { - - private static final char SEPARATOR = '.'; - protected final @NotNull SQLConfigProvider provider; - protected final @NotNull Map data; - - SQLConfigWrapper(@NotNull SQLConfigProvider provider, @NotNull Map map) { - this.provider = provider; - this.data = new LinkedHashMap<>(); - - for (Map.Entry entry : map.entrySet()) { - String key = (entry.getKey() == null) ? "null" : entry.getKey().toString(); - - if (entry.getValue() instanceof Map) { - this.data.put(key, new SQLConfigWrapper(provider, (Map) entry.getValue())); - } else { - this.data.put(key, entry.getValue()); - } - } - } - - @Override - public @NotNull Map getSource() { - return this.data; - } - - @Override - public @NotNull Set getKeys(boolean deep) { - return getValues(deep).keySet(); - } - - @Override - public @NotNull Map getValues(boolean deep) { - // Deep is not supported for SQL configs. - return new LinkedHashMap<>(this.data); - } - - @Override - public void set(@NotNull String path, @Nullable Object value) { - if (value instanceof Map) { - value = new SQLConfigWrapper(this.provider, (Map) value); - } - - SQLConfigWrapper section = getSectionFor(path); - if (section == this) { - if (value == null) { - this.data.remove(path); - } else { - this.data.put(path, value); - } - } else { - section.set(getChild(path), value); - } - - this.provider.updated.add(path); - } - - @Override - public boolean contains(@NotNull String path) { - return get(path) != null; - } - - @Override - public @Nullable Object get(@NotNull String path) { - SQLConfigWrapper section = getSectionFor(path); - return section == this ? data.get(path) : section.get(getChild(path)); - } - - @Override - public boolean isList(@NotNull String path) { - return get(path) instanceof List; - } - - @Override - public @Nullable List getList(@NotNull String path) { - Object val = get(path); - return (val instanceof List) ? (List) val : null; - } - - @Override - public boolean isConfigurationSection(@NotNull String path) { - return get(path) instanceof SQLConfigWrapper; - } - - @Override - public @Nullable SQLConfigWrapper getConfigurationSection(@NotNull String path) { - Object val = get(path); - return (val instanceof SQLConfigWrapper) ? (SQLConfigWrapper) val : null; - } - - private SQLConfigWrapper getSectionFor(String path) { - int index = path.indexOf(SEPARATOR); - if (index == -1) return this; - - String root = path.substring(0, index); - Object section = this.data.computeIfAbsent(root, k -> new SQLConfigWrapper(this.provider, new LinkedHashMap<>())); - - return (SQLConfigWrapper) section; - } - - private String getChild(String path) { - int index = path.indexOf(SEPARATOR); - return (index == -1) ? path : path.substring(index + 1); - } - -} diff --git a/providers/sql/src/main/java/cc/carm/lib/configuration/sql/SQLValueResolver.java b/providers/sql/src/main/java/cc/carm/lib/configuration/sql/SQLValueResolver.java deleted file mode 100644 index 867fae3..0000000 --- a/providers/sql/src/main/java/cc/carm/lib/configuration/sql/SQLValueResolver.java +++ /dev/null @@ -1,60 +0,0 @@ -package cc.carm.lib.configuration.sql; - -import cc.carm.lib.configuration.core.function.ConfigDataFunction; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -import java.util.function.Function; - -public class SQLValueResolver { - - public static SQLValueResolver of(int id, @NotNull Class clazz, - @NotNull ConfigDataFunction resolver) { - return of(id, clazz, resolver, String::valueOf); - } - - public static SQLValueResolver of(int id, @NotNull Class clazz, - @NotNull ConfigDataFunction resolver, - @NotNull Function serializer) { - return new SQLValueResolver<>(id, clazz, resolver, serializer); - } - - protected final int id; - protected final @NotNull Class clazz; - protected final @NotNull ConfigDataFunction resolver; - protected final @NotNull Function serializer; - - protected SQLValueResolver(int id, @NotNull Class clazz, - @NotNull ConfigDataFunction resolver, - @NotNull Function serializer) { - this.id = id; - this.clazz = clazz; - this.resolver = resolver; - this.serializer = serializer; - } - - public int getID() { - return id; - } - - public @NotNull Class getClazz() { - return clazz; - } - - public boolean isTypeOf(@NotNull Class clazz) { - return this.clazz.isAssignableFrom(clazz); - } - - public @NotNull T resolve(String data) throws Exception { - return resolver.parse(data); - } - - public @Nullable String serialize(T value) { - return String.valueOf(value); - } - - public @Nullable String serializeObject(Object value) { - return isTypeOf(value.getClass()) ? serialize(clazz.cast(value)) : null; - } - -} diff --git a/providers/sql/src/main/java/cc/carm/lib/configuration/sql/SQLValueTypes.java b/providers/sql/src/main/java/cc/carm/lib/configuration/sql/SQLValueTypes.java deleted file mode 100644 index 9223a3c..0000000 --- a/providers/sql/src/main/java/cc/carm/lib/configuration/sql/SQLValueTypes.java +++ /dev/null @@ -1,54 +0,0 @@ -package cc.carm.lib.configuration.sql; - -import cc.carm.lib.configuration.core.function.ConfigDataFunction; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -import java.util.Arrays; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; - -interface SQLValueTypes { - - SQLValueResolver STRING = SQLValueResolver.of(0, String.class, ConfigDataFunction.identity()); - SQLValueResolver BYTE = SQLValueResolver.of(1, Byte.class, Byte::parseByte); - SQLValueResolver SHORT = SQLValueResolver.of(2, Short.class, Short::parseShort); - SQLValueResolver INTEGER = SQLValueResolver.of(3, Integer.class, Integer::parseInt); - SQLValueResolver LONG = SQLValueResolver.of(4, Long.class, Long::parseLong); - SQLValueResolver FLOAT = SQLValueResolver.of(5, Float.class, Float::parseFloat); - SQLValueResolver DOUBLE = SQLValueResolver.of(6, Double.class, Double::parseDouble); - SQLValueResolver BOOLEAN = SQLValueResolver.of(7, Boolean.class, Boolean::parseBoolean); - SQLValueResolver CHAR = SQLValueResolver.of(8, Character.class, s -> s.charAt(0)); - - @SuppressWarnings("rawtypes") - SQLValueResolver LIST = SQLValueResolver.of(10, List.class, - array -> SQLConfigProvider.GSON.fromJson(array, List.class), - list -> SQLConfigProvider.GSON.toJson(list) - ); - - @SuppressWarnings("rawtypes") - SQLValueResolver SECTION = SQLValueResolver.of(20, Map.class, - section -> SQLConfigProvider.GSON.fromJson(section, LinkedHashMap.class), - section -> SQLConfigProvider.GSON.toJson(section) - ); - - static @NotNull SQLValueResolver[] values() { - return new SQLValueResolver[]{ - STRING, BYTE, SHORT, INTEGER, LONG, FLOAT, DOUBLE, BOOLEAN, CHAR, LIST, SECTION - }; - } - - static SQLValueResolver valueOf(int index) { - return values()[index]; - } - - static @Nullable SQLValueResolver get(int id) { - return Arrays.stream(values()).filter(v -> v.id == id).findFirst().orElse(null); - } - - static @Nullable SQLValueResolver get(Class typeClazz) { - return Arrays.stream(values()).filter(v -> v.isTypeOf(typeClazz)).findFirst().orElse(null); - } - -} diff --git a/providers/sql/src/test/java/config/SQLConfigTest.java b/providers/sql/src/test/java/config/SQLConfigTest.java index f2c5856..0bc1a93 100644 --- a/providers/sql/src/test/java/config/SQLConfigTest.java +++ b/providers/sql/src/test/java/config/SQLConfigTest.java @@ -1,6 +1,5 @@ package config; -import cc.carm.lib.configuration.EasyConfiguration; import cc.carm.lib.configuration.demo.tests.ConfigurationTest; import cc.carm.lib.configuration.sql.SQLConfigProvider; import cc.carm.lib.easysql.EasySQL; @@ -19,7 +18,7 @@ public class SQLConfigTest { public void test() { BeeDataSourceConfig config = new BeeDataSourceConfig(); config.setDriverClassName("org.h2.Driver"); - config.setJdbcUrl("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;MODE=MySQL;"); + config.setJdbcUrl("jdbc:h2:file:target/test;DB_CLOSE_DELAY=-1;MODE=MySQL;"); SQLManager manager = EasySQL.createManager(config); manager.setDebugMode(true);