mirror of
https://github.com/CarmJos/MineSQL.git
synced 2026-06-04 16:43:03 +08:00
feat(proj): 项目完成,测试使用。
This commit is contained in:
+12
-7
@@ -3,7 +3,7 @@
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>easysql-plugin</artifactId>
|
||||
<artifactId>minesql-parent</artifactId>
|
||||
<groupId>cc.carm.plugin</groupId>
|
||||
<version>0.0.3-SNAPSHOT</version>
|
||||
</parent>
|
||||
@@ -16,12 +16,12 @@
|
||||
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
|
||||
</properties>
|
||||
|
||||
<artifactId>easysql-plugin-api</artifactId>
|
||||
<artifactId>minesql-api</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>EasySQL-Plugin-API</name>
|
||||
<name>MineSQL-API</name>
|
||||
<description>轻松(用)SQL的独立运行库插件的公用API接口部分。</description>
|
||||
<url>https://github.com/CarmJos/EasySQL-Plugin</url>
|
||||
<url>https://github.com/CarmJos/MineSQL</url>
|
||||
|
||||
<developers>
|
||||
<developer>
|
||||
@@ -45,12 +45,12 @@
|
||||
|
||||
<issueManagement>
|
||||
<system>GitHub Issues</system>
|
||||
<url>https://github.com/CarmJos/EasySQL-Plugin/issues</url>
|
||||
<url>https://github.com/CarmJos/MineSQL/issues</url>
|
||||
</issueManagement>
|
||||
|
||||
<ciManagement>
|
||||
<system>GitHub Actions</system>
|
||||
<url>https://github.com/CarmJos/EasySQL-Plugin/actions/workflows/maven.yml</url>
|
||||
<url>https://github.com/CarmJos/MineSQL/actions/workflows/maven.yml</url>
|
||||
</ciManagement>
|
||||
|
||||
<dependencies>
|
||||
@@ -61,6 +61,12 @@
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<artifactId>easyconfiguration-core</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
@@ -92,7 +98,6 @@
|
||||
<includeDependencySources>true</includeDependencySources>
|
||||
<dependencySourceIncludes>cc.carm.lib:easysql-api</dependencySourceIncludes>
|
||||
<dependencySourceIncludes>cc.carm.plugin:*</dependencySourceIncludes>
|
||||
<outputDirectory>${project.parent.basedir}/api-docs/</outputDirectory>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
package cc.carm.plugin.minesql;
|
||||
|
||||
import cc.carm.plugin.minesql.api.SQLRegistry;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
interface IMineSQL {
|
||||
|
||||
@NotNull SQLRegistry getRegistry();
|
||||
|
||||
@NotNull File getPluginFolder();
|
||||
|
||||
default @NotNull File getSourceFolder() {
|
||||
return new File(getPluginFolder(), "db-files");
|
||||
}
|
||||
|
||||
@NotNull Logger getLogger();
|
||||
|
||||
}
|
||||
@@ -2,16 +2,30 @@ package cc.carm.plugin.minesql;
|
||||
|
||||
import cc.carm.plugin.minesql.api.SQLRegistry;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class MineSQL {
|
||||
|
||||
protected static SQLRegistry api;
|
||||
private static IMineSQL instance;
|
||||
|
||||
protected static void initializeAPI(SQLRegistry api) {
|
||||
MineSQL.api = api;
|
||||
protected static void initializeAPI(IMineSQL api) {
|
||||
MineSQL.instance = api;
|
||||
}
|
||||
|
||||
public static SQLRegistry get() {
|
||||
return api;
|
||||
public static Logger getLogger() {
|
||||
return instance.getLogger();
|
||||
}
|
||||
|
||||
public static SQLRegistry getRegistry() {
|
||||
return instance.getRegistry();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return 数据库源文件所在目录,非插件数据目录。
|
||||
*/
|
||||
public static File getDataSourceFolder() {
|
||||
return instance.getSourceFolder();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,226 +0,0 @@
|
||||
package cc.carm.plugin.minesql.api;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class DBConfiguration {
|
||||
|
||||
public static DBConfiguration create(@NotNull SQLDriverType sourceType, @NotNull String url) {
|
||||
return new DBConfiguration(sourceType.getDriverClass(), sourceType.getUrlPrefix(), url, sourceType.getInitializeSQLs());
|
||||
}
|
||||
|
||||
private final @NotNull String driverClassName;
|
||||
private final @NotNull String urlPrefix;
|
||||
private final @NotNull List<String> initializeSQLs;
|
||||
|
||||
private @NotNull String url;
|
||||
|
||||
private @Nullable String username;
|
||||
private @Nullable String password;
|
||||
|
||||
private @Nullable String connectionInitSql;
|
||||
|
||||
private @Nullable String poolName;
|
||||
private @Nullable Integer maxPoolSize;
|
||||
private @Nullable Integer maxActive;
|
||||
|
||||
private @Nullable Integer maxHoldTime;
|
||||
|
||||
private @Nullable Long idleTimeout;
|
||||
|
||||
private @Nullable Long maxWaitTime;
|
||||
|
||||
|
||||
private @Nullable String schema;
|
||||
private @Nullable Boolean autoCommit;
|
||||
private @Nullable Boolean readOnly;
|
||||
|
||||
private @Nullable String validationSQL;
|
||||
private @Nullable Integer validationTimeout;
|
||||
private @Nullable Long validationInterval;
|
||||
|
||||
|
||||
private DBConfiguration(@NotNull String driverClass, @NotNull String urlPrefix,
|
||||
@NotNull String url, @NotNull String[] initializeSQLs) {
|
||||
this.driverClassName = driverClass;
|
||||
this.urlPrefix = urlPrefix;
|
||||
this.url = url;
|
||||
this.initializeSQLs = Arrays.asList(initializeSQLs);
|
||||
}
|
||||
|
||||
public @NotNull String getDriverClassName() {
|
||||
return driverClassName;
|
||||
}
|
||||
|
||||
public @NotNull String getUrlPrefix() {
|
||||
return urlPrefix;
|
||||
}
|
||||
|
||||
public @NotNull List<String> getInitializeSQLs() {
|
||||
return initializeSQLs;
|
||||
}
|
||||
|
||||
public DBConfiguration setInitializeSQLs(@Nullable String[] initializeSQLs) {
|
||||
this.initializeSQLs.clear();
|
||||
this.initializeSQLs.addAll(Arrays.asList(initializeSQLs));
|
||||
return this;
|
||||
}
|
||||
|
||||
public DBConfiguration addInitializeSQL(@NotNull String initializeSQL) {
|
||||
this.initializeSQLs.add(initializeSQL);
|
||||
return this;
|
||||
}
|
||||
|
||||
public DBConfiguration clearExtraSettings() {
|
||||
this.initializeSQLs.clear();
|
||||
return this;
|
||||
}
|
||||
|
||||
public @NotNull String getUrl() {
|
||||
return url;
|
||||
}
|
||||
|
||||
public DBConfiguration setUrl(String url) {
|
||||
this.url = url;
|
||||
return this;
|
||||
}
|
||||
|
||||
public @Nullable String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public DBConfiguration setUsername(String username) {
|
||||
this.username = username;
|
||||
return this;
|
||||
}
|
||||
|
||||
public @Nullable String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public DBConfiguration setPassword(String password) {
|
||||
this.password = password;
|
||||
return this;
|
||||
}
|
||||
|
||||
public @Nullable String getConnectionInitSql() {
|
||||
return connectionInitSql;
|
||||
}
|
||||
|
||||
public DBConfiguration setConnectionInitSql(String connectionInitSql) {
|
||||
this.connectionInitSql = connectionInitSql;
|
||||
return this;
|
||||
}
|
||||
|
||||
public @Nullable String getValidationSQL() {
|
||||
return validationSQL;
|
||||
}
|
||||
|
||||
public DBConfiguration setValidationSQL(String validationSQL) {
|
||||
this.validationSQL = validationSQL;
|
||||
return this;
|
||||
}
|
||||
|
||||
public @Nullable Long getValidationInterval() {
|
||||
return validationInterval;
|
||||
}
|
||||
|
||||
public DBConfiguration setValidationInterval(@Nullable Long validationInterval) {
|
||||
this.validationInterval = validationInterval;
|
||||
return this;
|
||||
}
|
||||
|
||||
public @Nullable String getPoolName() {
|
||||
return poolName;
|
||||
}
|
||||
|
||||
public DBConfiguration setPoolName(String poolName) {
|
||||
this.poolName = poolName;
|
||||
return this;
|
||||
}
|
||||
|
||||
public @Nullable String getSchema() {
|
||||
return schema;
|
||||
}
|
||||
|
||||
public DBConfiguration setSchema(String schema) {
|
||||
this.schema = schema;
|
||||
return this;
|
||||
}
|
||||
|
||||
public @Nullable Boolean getAutoCommit() {
|
||||
return autoCommit;
|
||||
}
|
||||
|
||||
public DBConfiguration setAutoCommit(Boolean autoCommit) {
|
||||
this.autoCommit = autoCommit;
|
||||
return this;
|
||||
}
|
||||
|
||||
public @Nullable Boolean getReadOnly() {
|
||||
return readOnly;
|
||||
}
|
||||
|
||||
public DBConfiguration setReadOnly(Boolean readOnly) {
|
||||
this.readOnly = readOnly;
|
||||
return this;
|
||||
}
|
||||
|
||||
public @Nullable Integer getMaxHoldTime() {
|
||||
return maxHoldTime;
|
||||
}
|
||||
|
||||
public DBConfiguration setMaxHoldTime(@Nullable Integer maxHoldTime) {
|
||||
this.maxHoldTime = maxHoldTime;
|
||||
return this;
|
||||
}
|
||||
|
||||
public @Nullable Integer getValidationTimeout() {
|
||||
return validationTimeout;
|
||||
}
|
||||
|
||||
public DBConfiguration setValidationTimeout(Integer validationTimeout) {
|
||||
this.validationTimeout = validationTimeout;
|
||||
return this;
|
||||
}
|
||||
|
||||
public @Nullable Long getIdleTimeout() {
|
||||
return idleTimeout;
|
||||
}
|
||||
|
||||
public DBConfiguration setIdleTimeout(Long idleTimeout) {
|
||||
this.idleTimeout = idleTimeout;
|
||||
return this;
|
||||
}
|
||||
|
||||
public @Nullable Integer getMaxActive() {
|
||||
return maxActive;
|
||||
}
|
||||
|
||||
public DBConfiguration setMaxActive(Integer maxActive) {
|
||||
this.maxActive = maxActive;
|
||||
return this;
|
||||
}
|
||||
|
||||
public @Nullable Integer getMaxPoolSize() {
|
||||
return maxPoolSize;
|
||||
}
|
||||
|
||||
public DBConfiguration setMaxPoolSize(@Nullable Integer maxPoolSize) {
|
||||
this.maxPoolSize = maxPoolSize;
|
||||
return this;
|
||||
}
|
||||
|
||||
public @Nullable Long getMaxWaitTime() {
|
||||
return maxWaitTime;
|
||||
}
|
||||
|
||||
public DBConfiguration setMaxWaitTime(Long maxWaitTime) {
|
||||
this.maxWaitTime = maxWaitTime;
|
||||
return this;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,37 +1,54 @@
|
||||
package cc.carm.plugin.minesql.api;
|
||||
|
||||
import cc.carm.lib.easysql.api.SQLManager;
|
||||
import cc.carm.lib.easysql.api.function.SQLHandler;
|
||||
import org.jetbrains.annotations.Contract;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Optional;
|
||||
|
||||
public enum SQLDriverType {
|
||||
|
||||
MARIADB("org.mariadb.jdbc.Driver", "jdbc:mariadb://",
|
||||
new String[]{"mariadb", "maria-db"}, new String[]{}
|
||||
MARIADB(
|
||||
"org.mariadb.jdbc.Driver", "jdbc:mariadb://",
|
||||
new String[]{"maria-db"}, null
|
||||
),
|
||||
MYSQL("com.mysql.jdbc.Driver", "jdbc:mysql://",
|
||||
new String[]{"mysql"}, new String[]{}
|
||||
|
||||
MYSQL("com.mysql.jdbc.Driver", "jdbc:mysql://", null, null),
|
||||
|
||||
H2_FILE("org.h2.Driver", "jdbc:h2:file:",
|
||||
new String[]{"h2"},
|
||||
(manager) -> {
|
||||
manager.executeSQL("SET MODE=MySQL");
|
||||
manager.executeSQL("SET DB_CLOSE_DELAY=-1");
|
||||
manager.executeSQL("SET DB_CLOSE_ON_EXIT=FALSE");
|
||||
}
|
||||
),
|
||||
H2("org.h2.Driver", "jdbc:h2:",
|
||||
new String[]{"h2", "h2-db", "h2-database"},
|
||||
new String[]{"SET MODE=MySQL", "SET DB_CLOSE_DELAY=-1"}
|
||||
|
||||
H2_MEM("org.h2.Driver", "jdbc:h2:mem:",
|
||||
new String[]{"h2-memory", "h2-temp"},
|
||||
(manager) -> {
|
||||
manager.executeSQL("SET MODE=MySQL");
|
||||
manager.executeSQL("SET DB_CLOSE_DELAY=-1");
|
||||
manager.executeSQL("SET DB_CLOSE_ON_EXIT=FALSE");
|
||||
}
|
||||
);
|
||||
|
||||
private final @NotNull String driverClass;
|
||||
private final @NotNull String urlPrefix;
|
||||
private final @NotNull String jdbcPrefix;
|
||||
private final @NotNull String[] databaseAlias;
|
||||
private final @NotNull String[] initializeSQLs;
|
||||
private final @Nullable SQLHandler<SQLManager> initializer;
|
||||
|
||||
SQLDriverType(@NotNull String driverClass, @NotNull String urlPrefix,
|
||||
@NotNull String[] databaseAlias,
|
||||
@NotNull String[] initializeSQLs) {
|
||||
SQLDriverType(@NotNull String driverClass, @NotNull String jdbcPrefix,
|
||||
@Nullable String[] databaseAlias,
|
||||
@Nullable SQLHandler<SQLManager> initializer) {
|
||||
|
||||
this.driverClass = driverClass;
|
||||
this.urlPrefix = urlPrefix;
|
||||
this.databaseAlias = databaseAlias;
|
||||
this.initializeSQLs = initializeSQLs;
|
||||
this.jdbcPrefix = jdbcPrefix;
|
||||
this.databaseAlias = Optional.ofNullable(databaseAlias).orElse(new String[0]);
|
||||
this.initializer = initializer;
|
||||
}
|
||||
|
||||
public @NotNull String[] getDatabaseAlias() {
|
||||
@@ -42,24 +59,24 @@ public enum SQLDriverType {
|
||||
return driverClass;
|
||||
}
|
||||
|
||||
public @NotNull String getUrlPrefix() {
|
||||
return urlPrefix;
|
||||
public @NotNull String getJdbcPrefix() {
|
||||
return jdbcPrefix;
|
||||
}
|
||||
|
||||
public @NotNull String[] getInitializeSQLs() {
|
||||
return initializeSQLs;
|
||||
public @Nullable SQLHandler<SQLManager> getInitializer() {
|
||||
return initializer;
|
||||
}
|
||||
|
||||
@Contract("null->null")
|
||||
public static @Nullable SQLDriverType parse(@Nullable String driverString) {
|
||||
if (driverString == null) return null;
|
||||
return Arrays.stream(values())
|
||||
.filter(value -> value.name().equalsIgnoreCase(driverString) || has(value.getDatabaseAlias(), driverString))
|
||||
.filter(value -> value.name().equalsIgnoreCase(driverString) || anyMatch(value.getDatabaseAlias(), driverString))
|
||||
.findFirst().orElse(null);
|
||||
}
|
||||
|
||||
|
||||
private static boolean has(String[] array, String value) {
|
||||
private static boolean anyMatch(String[] array, String value) {
|
||||
return Arrays.stream(array).anyMatch(s -> s.equalsIgnoreCase(value));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package cc.carm.plugin.minesql.api;
|
||||
|
||||
import cc.carm.lib.easysql.api.SQLManager;
|
||||
import cc.carm.lib.easysql.api.SQLQuery;
|
||||
import cc.carm.plugin.minesql.api.source.SQLSourceConfig;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.annotations.Unmodifiable;
|
||||
@@ -52,7 +53,7 @@ public interface SQLRegistry {
|
||||
* @throws Exception 若创建失败则抛出异常
|
||||
*/
|
||||
@NotNull SQLManager create(@NotNull String name,
|
||||
@NotNull DBConfiguration configuration) throws Exception;
|
||||
@NotNull SQLSourceConfig configuration) throws Exception;
|
||||
|
||||
/**
|
||||
* 创建并注册一个新的 SQLManager 实例
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
package cc.carm.plugin.minesql.api.conf;
|
||||
|
||||
import cc.carm.plugin.minesql.api.SQLDriverType;
|
||||
import cc.carm.plugin.minesql.api.source.SQLSourceConfig;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public abstract class SQLDriverConfig {
|
||||
|
||||
protected final @NotNull SQLDriverType type;
|
||||
|
||||
public SQLDriverConfig(@NotNull SQLDriverType type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public abstract SQLSourceConfig createSource();
|
||||
|
||||
public @NotNull SQLDriverType getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public abstract @NotNull Map<String, Object> serialize();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package cc.carm.plugin.minesql.api.conf.drivers;
|
||||
|
||||
import cc.carm.plugin.minesql.api.SQLDriverType;
|
||||
import cc.carm.plugin.minesql.api.conf.SQLDriverConfig;
|
||||
import cc.carm.plugin.minesql.api.source.SQLSourceConfig;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
public class H2MemConfig extends SQLDriverConfig {
|
||||
|
||||
protected final @Nullable String database;
|
||||
|
||||
public H2MemConfig(@Nullable String database) {
|
||||
super(SQLDriverType.H2_MEM);
|
||||
this.database = database;
|
||||
}
|
||||
|
||||
public @Nullable String getDatabase() {
|
||||
return database;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SQLSourceConfig createSource() {
|
||||
return SQLSourceConfig.create(getType().getDriverClass(), buildJDBC(), getType().getInitializer());
|
||||
}
|
||||
|
||||
protected String buildJDBC() {
|
||||
return getType().getJdbcPrefix() + Optional.ofNullable(getDatabase()).orElse("");
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Map<String, Object> serialize() {
|
||||
Map<String, Object> values = new LinkedHashMap<>();
|
||||
|
||||
values.put("type", getType().name());
|
||||
if (getDatabase() != null) values.put("database", getDatabase());
|
||||
|
||||
return values;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package cc.carm.plugin.minesql.api.conf.impl;
|
||||
|
||||
import cc.carm.plugin.minesql.MineSQL;
|
||||
import cc.carm.plugin.minesql.api.SQLDriverType;
|
||||
import cc.carm.plugin.minesql.api.conf.SQLDriverConfig;
|
||||
import cc.carm.plugin.minesql.api.source.SQLSourceConfig;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class FileBasedConfig extends SQLDriverConfig {
|
||||
|
||||
protected final @NotNull String filePath;
|
||||
|
||||
public FileBasedConfig(@NotNull SQLDriverType type,
|
||||
@NotNull String filePath) {
|
||||
super(type);
|
||||
this.filePath = filePath;
|
||||
}
|
||||
|
||||
public @NotNull String getFilePath() {
|
||||
return filePath;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SQLSourceConfig createSource() {
|
||||
File file = new File(MineSQL.getDataSourceFolder(), filePath);
|
||||
return SQLSourceConfig.create(getType().getDriverClass(), file.getAbsolutePath(), getType().getInitializer());
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Map<String, Object> serialize() {
|
||||
Map<String, Object> values = new LinkedHashMap<>();
|
||||
|
||||
values.put("type", getType().name());
|
||||
values.put("file", getFilePath());
|
||||
|
||||
return values;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,87 @@
|
||||
package cc.carm.plugin.minesql.api.conf.impl;
|
||||
|
||||
import cc.carm.plugin.minesql.api.SQLDriverType;
|
||||
import cc.carm.plugin.minesql.api.conf.SQLDriverConfig;
|
||||
import cc.carm.plugin.minesql.api.source.SQLSourceConfig;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class RemoteAuthConfig extends SQLDriverConfig {
|
||||
|
||||
protected final @NotNull String host;
|
||||
protected final int port;
|
||||
protected final @NotNull String database;
|
||||
protected final @Nullable String username;
|
||||
protected final @Nullable String password;
|
||||
|
||||
protected final @Nullable String extraSettings;
|
||||
|
||||
public RemoteAuthConfig(@NotNull SQLDriverType type,
|
||||
@NotNull String host, int port, @NotNull String database,
|
||||
@Nullable String username, @Nullable String password,
|
||||
@Nullable String extraSettings) {
|
||||
super(type);
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
this.database = database;
|
||||
this.username = username;
|
||||
this.password = password;
|
||||
this.extraSettings = extraSettings;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SQLSourceConfig createSource() {
|
||||
return SQLSourceConfig.create(
|
||||
getType().getDriverClass(), buildJDBC(), getType().getInitializer()
|
||||
).setUsername(getUsername()).setPassword(getPassword());
|
||||
}
|
||||
|
||||
public @NotNull String getHost() {
|
||||
return host;
|
||||
}
|
||||
|
||||
public int getPort() {
|
||||
return port;
|
||||
}
|
||||
|
||||
public @NotNull String getDatabase() {
|
||||
return database;
|
||||
}
|
||||
|
||||
public @Nullable String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
|
||||
public @Nullable String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public @Nullable String getExtraSettings() {
|
||||
return extraSettings;
|
||||
}
|
||||
|
||||
protected String buildJDBC() {
|
||||
return String.format("%s%s:%s/%s%s", getType().getJdbcPrefix(), getHost(), getPort(), getDatabase(), getExtraSettings());
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Map<String, Object> serialize() {
|
||||
Map<String, Object> values = new LinkedHashMap<>();
|
||||
|
||||
values.put("type", getType().name());
|
||||
|
||||
values.put("host", getHost());
|
||||
values.put("port", getPort());
|
||||
values.put("database", getDatabase());
|
||||
if (getUsername() != null) values.put("username", username);
|
||||
if (getPassword() != null) values.put("password", password);
|
||||
if (getExtraSettings() != null) values.put("extra", extraSettings);
|
||||
|
||||
return values;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,134 @@
|
||||
package cc.carm.plugin.minesql.api.source;
|
||||
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class SQLPoolSettings {
|
||||
|
||||
private @Nullable String poolName;
|
||||
private @Nullable Integer maxPoolSize;
|
||||
private @Nullable Integer maxActive;
|
||||
|
||||
private @Nullable Integer maxHoldTime;
|
||||
|
||||
private @Nullable Long idleTimeout;
|
||||
|
||||
private @Nullable Long maxWaitTime;
|
||||
|
||||
private @Nullable String schema;
|
||||
private @Nullable Boolean autoCommit;
|
||||
private @Nullable Boolean readOnly;
|
||||
|
||||
private @Nullable String validationSQL;
|
||||
private @Nullable Integer validationTimeout;
|
||||
private @Nullable Long validationInterval;
|
||||
|
||||
public @Nullable String getValidationSQL() {
|
||||
return validationSQL;
|
||||
}
|
||||
|
||||
public SQLPoolSettings setValidationSQL(String validationSQL) {
|
||||
this.validationSQL = validationSQL;
|
||||
return this;
|
||||
}
|
||||
|
||||
public @Nullable Long getValidationInterval() {
|
||||
return validationInterval;
|
||||
}
|
||||
|
||||
public SQLPoolSettings setValidationInterval(@Nullable Long validationInterval) {
|
||||
this.validationInterval = validationInterval;
|
||||
return this;
|
||||
}
|
||||
|
||||
public @Nullable String getPoolName() {
|
||||
return poolName;
|
||||
}
|
||||
|
||||
public SQLPoolSettings setPoolName(String poolName) {
|
||||
this.poolName = poolName;
|
||||
return this;
|
||||
}
|
||||
|
||||
public @Nullable String getSchema() {
|
||||
return schema;
|
||||
}
|
||||
|
||||
public SQLPoolSettings setSchema(String schema) {
|
||||
this.schema = schema;
|
||||
return this;
|
||||
}
|
||||
|
||||
public @Nullable Boolean getAutoCommit() {
|
||||
return autoCommit;
|
||||
}
|
||||
|
||||
public SQLPoolSettings setAutoCommit(Boolean autoCommit) {
|
||||
this.autoCommit = autoCommit;
|
||||
return this;
|
||||
}
|
||||
|
||||
public @Nullable Boolean getReadOnly() {
|
||||
return readOnly;
|
||||
}
|
||||
|
||||
public SQLPoolSettings setReadOnly(Boolean readOnly) {
|
||||
this.readOnly = readOnly;
|
||||
return this;
|
||||
}
|
||||
|
||||
public @Nullable Integer getMaxHoldTime() {
|
||||
return maxHoldTime;
|
||||
}
|
||||
|
||||
public SQLPoolSettings setMaxHoldTime(@Nullable Integer maxHoldTime) {
|
||||
this.maxHoldTime = maxHoldTime;
|
||||
return this;
|
||||
}
|
||||
|
||||
public @Nullable Integer getValidationTimeout() {
|
||||
return validationTimeout;
|
||||
}
|
||||
|
||||
public SQLPoolSettings setValidationTimeout(Integer validationTimeout) {
|
||||
this.validationTimeout = validationTimeout;
|
||||
return this;
|
||||
}
|
||||
|
||||
public @Nullable Long getIdleTimeout() {
|
||||
return idleTimeout;
|
||||
}
|
||||
|
||||
public SQLPoolSettings setIdleTimeout(Long idleTimeout) {
|
||||
this.idleTimeout = idleTimeout;
|
||||
return this;
|
||||
}
|
||||
|
||||
public @Nullable Integer getMaxActive() {
|
||||
return maxActive;
|
||||
}
|
||||
|
||||
public SQLPoolSettings setMaxActive(Integer maxActive) {
|
||||
this.maxActive = maxActive;
|
||||
return this;
|
||||
}
|
||||
|
||||
public @Nullable Integer getMaxPoolSize() {
|
||||
return maxPoolSize;
|
||||
}
|
||||
|
||||
public SQLPoolSettings setMaxPoolSize(@Nullable Integer maxPoolSize) {
|
||||
this.maxPoolSize = maxPoolSize;
|
||||
return this;
|
||||
}
|
||||
|
||||
public @Nullable Long getMaxWaitTime() {
|
||||
return maxWaitTime;
|
||||
}
|
||||
|
||||
public SQLPoolSettings setMaxWaitTime(Long maxWaitTime) {
|
||||
this.maxWaitTime = maxWaitTime;
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,135 @@
|
||||
package cc.carm.plugin.minesql.api.source;
|
||||
|
||||
import cc.carm.lib.easysql.api.SQLManager;
|
||||
import cc.carm.lib.easysql.api.function.SQLHandler;
|
||||
import cc.carm.plugin.minesql.api.SQLDriverType;
|
||||
import cc.carm.plugin.minesql.api.conf.drivers.H2MemConfig;
|
||||
import cc.carm.plugin.minesql.api.conf.impl.FileBasedConfig;
|
||||
import cc.carm.plugin.minesql.api.conf.impl.RemoteAuthConfig;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public class SQLSourceConfig {
|
||||
|
||||
public static SQLSourceConfig createMySQL(@NotNull String host, int port, @NotNull String database,
|
||||
@NotNull String username, @Nullable String password,
|
||||
@Nullable String extraSettings) {
|
||||
return new RemoteAuthConfig(SQLDriverType.MYSQL, host, port, database, username, password, extraSettings).createSource();
|
||||
}
|
||||
|
||||
public static SQLSourceConfig createMariaDB(@NotNull String host, int port, @NotNull String database,
|
||||
@NotNull String username, @Nullable String password,
|
||||
@Nullable String extraSettings) {
|
||||
return new RemoteAuthConfig(SQLDriverType.MARIADB, host, port, database, username, password, extraSettings).createSource();
|
||||
}
|
||||
|
||||
public static SQLSourceConfig createH2File(@NotNull File file) {
|
||||
return create(SQLDriverType.H2_FILE, file.getAbsolutePath());
|
||||
}
|
||||
|
||||
public static SQLSourceConfig createH2File(@NotNull String filePath) {
|
||||
return new FileBasedConfig(SQLDriverType.H2_FILE, filePath).createSource();
|
||||
}
|
||||
|
||||
public static SQLSourceConfig createH2Mem(@Nullable String databaseName) {
|
||||
return new H2MemConfig(databaseName).createSource();
|
||||
}
|
||||
|
||||
public static SQLSourceConfig create(@NotNull SQLDriverType sourceType, @NotNull String url) {
|
||||
return create(sourceType.getDriverClass(), sourceType.getJdbcPrefix() + url, sourceType.getInitializer());
|
||||
}
|
||||
|
||||
public static SQLSourceConfig create(@NotNull String driverClass, @NotNull String jdbcURL) {
|
||||
return create(driverClass, jdbcURL, null);
|
||||
}
|
||||
|
||||
public static SQLSourceConfig create(@NotNull String driverClassName, @NotNull String jdbcURL,
|
||||
@Nullable SQLHandler<SQLManager> initializer) {
|
||||
return new SQLSourceConfig(driverClassName, jdbcURL, initializer, null);
|
||||
}
|
||||
|
||||
public static SQLSourceConfig create(@NotNull String driverClassName, @NotNull String jdbcURL,
|
||||
@Nullable SQLHandler<SQLManager> initializer, @Nullable SQLPoolSettings settings) {
|
||||
return new SQLSourceConfig(driverClassName, jdbcURL, initializer, settings);
|
||||
}
|
||||
|
||||
private @NotNull String driverClassName;
|
||||
private @NotNull String jdbcURL;
|
||||
private @Nullable String username;
|
||||
private @Nullable String password;
|
||||
|
||||
private @Nullable SQLHandler<SQLManager> initializer;
|
||||
private @NotNull SQLPoolSettings settings;
|
||||
|
||||
public SQLSourceConfig(@NotNull String driverClassName, @NotNull String jdbcURL,
|
||||
@Nullable SQLHandler<SQLManager> initializer, @Nullable SQLPoolSettings settings) {
|
||||
this.driverClassName = driverClassName;
|
||||
this.jdbcURL = jdbcURL;
|
||||
this.initializer = initializer;
|
||||
this.settings = Optional.ofNullable(settings).orElse(new SQLPoolSettings());
|
||||
}
|
||||
|
||||
public @NotNull String getDriverClassName() {
|
||||
return driverClassName;
|
||||
}
|
||||
|
||||
public SQLSourceConfig setDriverClassName(String driverClassName) {
|
||||
this.driverClassName = driverClassName;
|
||||
return this;
|
||||
}
|
||||
|
||||
public @NotNull String getJdbcURL() {
|
||||
return jdbcURL;
|
||||
}
|
||||
|
||||
public SQLSourceConfig setJdbcURL(String jdbcURL) {
|
||||
this.jdbcURL = jdbcURL;
|
||||
return this;
|
||||
}
|
||||
|
||||
public @Nullable String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public SQLSourceConfig setUsername(String username) {
|
||||
this.username = username;
|
||||
return this;
|
||||
}
|
||||
|
||||
public @Nullable String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public SQLSourceConfig setPassword(String password) {
|
||||
this.password = password;
|
||||
return this;
|
||||
}
|
||||
|
||||
public @Nullable SQLHandler<SQLManager> getInitializer() {
|
||||
return initializer;
|
||||
}
|
||||
|
||||
public SQLSourceConfig setInitializer(SQLHandler<SQLManager> initializer) {
|
||||
this.initializer = initializer;
|
||||
return this;
|
||||
}
|
||||
|
||||
public @NotNull SQLPoolSettings getSettings() {
|
||||
return settings;
|
||||
}
|
||||
|
||||
public SQLSourceConfig setSettings(SQLPoolSettings settings) {
|
||||
this.settings = settings;
|
||||
return this;
|
||||
}
|
||||
|
||||
public SQLSourceConfig editSettings(Consumer<SQLPoolSettings> consumer) {
|
||||
consumer.accept(settings);
|
||||
return this;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user