mirror of
https://github.com/CarmJos/MineSQL.git
synced 2026-06-05 00:48:16 +08:00
修改实现方式
This commit is contained in:
@@ -19,7 +19,7 @@
|
||||
<artifactId>easysql-plugin-api</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>EasySQL-Plugin-API</name>
|
||||
<name>EasySQL-Plugin-Common</name>
|
||||
|
||||
<dependencies>
|
||||
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
package cc.carm.plugin.easysql;
|
||||
|
||||
import cc.carm.plugin.easysql.api.EasySQLManager;
|
||||
|
||||
public class EasySQLAPI {
|
||||
|
||||
protected static EasySQLManager api;
|
||||
|
||||
protected static void init(EasySQLManager api) {
|
||||
EasySQLAPI.api = api;
|
||||
}
|
||||
|
||||
public static EasySQLManager get() {
|
||||
return api;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,247 @@
|
||||
package cc.carm.plugin.easysql.api;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class DBConfiguration {
|
||||
|
||||
public DBConfiguration create(@NotNull SQLSourceType sourceType, @NotNull String url) {
|
||||
return new DBConfiguration(sourceType.getDriverClass(), sourceType.getUrlPrefix(), url, sourceType.getDefaultSettings());
|
||||
}
|
||||
|
||||
private final @NotNull String driverClassName;
|
||||
private final @NotNull String urlPrefix;
|
||||
private final @NotNull List<String> defaultSettings;
|
||||
private final @NotNull List<String> extraSettings;
|
||||
|
||||
private @NotNull String url;
|
||||
|
||||
private @Nullable String username;
|
||||
private @Nullable String password;
|
||||
|
||||
private @Nullable String connectionInitSql;
|
||||
private @Nullable String connectionTestQuery;
|
||||
private @Nullable String poolName;
|
||||
private @Nullable String schema;
|
||||
private @Nullable Boolean autoCommit;
|
||||
private @Nullable Boolean readOnly;
|
||||
|
||||
private @Nullable Long connectionTimeout;
|
||||
private @Nullable Long validationTimeout;
|
||||
private @Nullable Long idleTimeout;
|
||||
private @Nullable Long leakDetectionThreshold;
|
||||
private @Nullable Long maxLifetime;
|
||||
private @Nullable Long keepaliveTime;
|
||||
private @Nullable Integer maxPoolSize;
|
||||
private @Nullable Integer minIdle;
|
||||
|
||||
private DBConfiguration(@NotNull String driverClass, @NotNull String urlPrefix,
|
||||
@NotNull String url, @NotNull String[] defaultSettings) {
|
||||
this.driverClassName = driverClass;
|
||||
this.urlPrefix = urlPrefix;
|
||||
this.url = url;
|
||||
this.defaultSettings = Arrays.asList(defaultSettings);
|
||||
this.extraSettings = new ArrayList<>();
|
||||
}
|
||||
|
||||
public @NotNull String getDriverClassName() {
|
||||
return driverClassName;
|
||||
}
|
||||
|
||||
public @NotNull String getUrlPrefix() {
|
||||
return urlPrefix;
|
||||
}
|
||||
|
||||
public @NotNull List<String> getDefaultSettings() {
|
||||
return defaultSettings;
|
||||
}
|
||||
|
||||
public @NotNull List<String> getExtraSettings() {
|
||||
return extraSettings;
|
||||
}
|
||||
|
||||
public DBConfiguration setDefaultSettings(@Nullable String[] defaultSettings) {
|
||||
this.defaultSettings.clear();
|
||||
this.defaultSettings.addAll(Arrays.asList(defaultSettings));
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
public DBConfiguration clearDefaultSettings() {
|
||||
this.defaultSettings.clear();
|
||||
return this;
|
||||
}
|
||||
|
||||
public DBConfiguration setExtraSettings(@Nullable String[] extraSettings) {
|
||||
this.defaultSettings.clear();
|
||||
this.extraSettings.addAll(Arrays.asList(extraSettings));
|
||||
return this;
|
||||
}
|
||||
|
||||
public DBConfiguration addExtraSetting(@NotNull String extraSetting) {
|
||||
this.extraSettings.add(extraSetting);
|
||||
return this;
|
||||
}
|
||||
|
||||
public DBConfiguration clearExtraSettings() {
|
||||
this.defaultSettings.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 getConnectionTestQuery() {
|
||||
return connectionTestQuery;
|
||||
}
|
||||
|
||||
public DBConfiguration setConnectionTestQuery(String connectionTestQuery) {
|
||||
this.connectionTestQuery = connectionTestQuery;
|
||||
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 Long getConnectionTimeout() {
|
||||
return connectionTimeout;
|
||||
}
|
||||
|
||||
public DBConfiguration setConnectionTimeout(Long connectionTimeout) {
|
||||
this.connectionTimeout = connectionTimeout;
|
||||
return this;
|
||||
}
|
||||
|
||||
public @Nullable Long getValidationTimeout() {
|
||||
return validationTimeout;
|
||||
}
|
||||
|
||||
public DBConfiguration setValidationTimeout(Long validationTimeout) {
|
||||
this.validationTimeout = validationTimeout;
|
||||
return this;
|
||||
}
|
||||
|
||||
public @Nullable Long getIdleTimeout() {
|
||||
return idleTimeout;
|
||||
}
|
||||
|
||||
public DBConfiguration setIdleTimeout(Long idleTimeout) {
|
||||
this.idleTimeout = idleTimeout;
|
||||
return this;
|
||||
}
|
||||
|
||||
public @Nullable Long getLeakDetectionThreshold() {
|
||||
return leakDetectionThreshold;
|
||||
}
|
||||
|
||||
public DBConfiguration setLeakDetectionThreshold(Long leakDetectionThreshold) {
|
||||
this.leakDetectionThreshold = leakDetectionThreshold;
|
||||
return this;
|
||||
}
|
||||
|
||||
public @Nullable Long getMaxLifetime() {
|
||||
return maxLifetime;
|
||||
}
|
||||
|
||||
public DBConfiguration setMaxLifetime(Long maxLifetime) {
|
||||
this.maxLifetime = maxLifetime;
|
||||
return this;
|
||||
}
|
||||
|
||||
public @Nullable Long getKeepaliveTime() {
|
||||
return keepaliveTime;
|
||||
}
|
||||
|
||||
public DBConfiguration setKeepaliveTime(Long keepaliveTime) {
|
||||
this.keepaliveTime = keepaliveTime;
|
||||
return this;
|
||||
}
|
||||
|
||||
public @Nullable Integer getMaxPoolSize() {
|
||||
return maxPoolSize;
|
||||
}
|
||||
|
||||
public DBConfiguration setMaxPoolSize(Integer maxPoolSize) {
|
||||
this.maxPoolSize = maxPoolSize;
|
||||
return this;
|
||||
}
|
||||
|
||||
public @Nullable Integer getMinIdle() {
|
||||
return minIdle;
|
||||
}
|
||||
|
||||
public DBConfiguration setMinIdle(Integer minIdle) {
|
||||
this.minIdle = minIdle;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
@@ -1,62 +0,0 @@
|
||||
package cc.carm.plugin.easysql.api;
|
||||
|
||||
import cc.carm.lib.easysql.api.SQLManager;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 入口类
|
||||
*/
|
||||
public interface EasySQLAPI {
|
||||
/**
|
||||
* 获取指定名称的 SQLManager 实例
|
||||
* @param name 要获取的 SQLManager 实例名称
|
||||
* @return SQLManager 实例
|
||||
*/
|
||||
@Nullable SQLManager getSQLManager(@Nullable String name);
|
||||
|
||||
/**
|
||||
* 获取所有 SQLManager 实例
|
||||
* @return SQLManager 实例集合
|
||||
*/
|
||||
@NotNull Map<String, SQLManager> getSQLManagers();
|
||||
|
||||
/**
|
||||
* 获取指定名称的 SQLManager 实例的命名空间
|
||||
* @param name SQLManager 名称
|
||||
* @return 命名空间
|
||||
*/
|
||||
@Nullable
|
||||
String getNameSpace(@Nullable String name);
|
||||
|
||||
/**
|
||||
* 获取指定 SQLManager 的注册名
|
||||
* @param sqlManager SQLManager 实例
|
||||
* @return 注册名
|
||||
*/
|
||||
@Nullable
|
||||
String getName(@NotNull SQLManager sqlManager);
|
||||
|
||||
/**
|
||||
* 注册 SQLManager 实例
|
||||
* @param namespace 命名空间,通常为插件名称
|
||||
* @param name 实例名称
|
||||
* @param sqlManager SQLManager 实例
|
||||
*/
|
||||
void registerSQLManager(@NotNull String namespace,@NotNull String name, @NotNull SQLManager sqlManager);
|
||||
|
||||
/**
|
||||
* 注销指定的 SQLManager 实例
|
||||
* @param name 实例名称
|
||||
*/
|
||||
void unregisterSQLManager(@NotNull String name);
|
||||
|
||||
/**
|
||||
* 注销指定命名空间下的所有 SQLManager 实例
|
||||
* @param namespace 命名空间
|
||||
*/
|
||||
void unregisterSQLManagerAll(@NotNull String namespace);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
package cc.carm.plugin.easysql.api;
|
||||
|
||||
import cc.carm.lib.easysql.api.SQLManager;
|
||||
import cc.carm.lib.easysql.api.SQLQuery;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.annotations.Unmodifiable;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Properties;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* 入口类
|
||||
*/
|
||||
public interface EasySQLManager {
|
||||
|
||||
/**
|
||||
* 获取原生注册的指定名称的 SQLManager 实例
|
||||
*
|
||||
* @param name 要获取的 SQLManager 实例名称, 如果为 null 则获取首个实例
|
||||
* @return {@link SQLManager} 实例
|
||||
* @throws NullPointerException 若不存在对应实例则抛出空指针异常
|
||||
*/
|
||||
@NotNull SQLManager get(@Nullable String name) throws NullPointerException;
|
||||
|
||||
/**
|
||||
* 获取原生注册的指定名称的 SQLManager 实例
|
||||
*
|
||||
* @param name 要获取的 SQLManager 实例名称, 如果为 null 则获取首个实例
|
||||
* @return {@link SQLManager} 实例
|
||||
*/
|
||||
@NotNull Optional<@Nullable SQLManager> getOptional(@Nullable String name);
|
||||
|
||||
/**
|
||||
* 获取某命名空间下所有 SQLManager 实例
|
||||
*
|
||||
* @return {@link SQLManager} 实例集合
|
||||
*/
|
||||
@Unmodifiable
|
||||
@NotNull Map<String, SQLManager> list();
|
||||
|
||||
/**
|
||||
* 创建并注册一个新的 SQLManager 实例
|
||||
*
|
||||
* @param name 实例名称
|
||||
* @param configuration SQLManager 实例的配置
|
||||
* @return {@link SQLManager} 实例
|
||||
* @throws Exception 若创建失败则抛出异常
|
||||
*/
|
||||
@NotNull SQLManager create(@Nullable String name,
|
||||
@NotNull DBConfiguration configuration) throws Exception;
|
||||
|
||||
/**
|
||||
* 创建并注册一个新的 SQLManager 实例
|
||||
*
|
||||
* @param name 实例名称
|
||||
* @param properties SQLManager 实例的配置文件
|
||||
* @return {@link SQLManager} 实例
|
||||
* @throws Exception 若创建失败则抛出异常
|
||||
*/
|
||||
@NotNull SQLManager create(@Nullable String name,
|
||||
@NotNull Properties properties) throws Exception;
|
||||
|
||||
/**
|
||||
* 创建并注册一个新的 SQLManager 实例
|
||||
*
|
||||
* @param name 实例名称
|
||||
* @param propertyFileName 配置文件的资源名称
|
||||
* @return {@link SQLManager} 实例
|
||||
* @throws Exception 若创建失败则抛出异常
|
||||
*/
|
||||
@NotNull SQLManager create(@Nullable String name,
|
||||
@NotNull String propertyFileName) throws Exception;
|
||||
|
||||
/**
|
||||
* 终止一个 SQLManager 实例。
|
||||
*
|
||||
* @param manager
|
||||
* @return
|
||||
*/
|
||||
default Map<UUID, SQLQuery> shutdown(SQLManager manager) {
|
||||
return shutdown(manager, true);
|
||||
}
|
||||
|
||||
Map<UUID, SQLQuery> shutdown(SQLManager manager, boolean forceClose);
|
||||
|
||||
}
|
||||
-15
@@ -1,15 +0,0 @@
|
||||
package cc.carm.plugin.easysql.api;
|
||||
|
||||
import cc.carm.lib.easysql.api.SQLManager;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public interface EasySQLPluginPlatform {
|
||||
/**
|
||||
* 获取 SQL 管理器
|
||||
* @param name 注册键
|
||||
* @return SQL管理器
|
||||
*/
|
||||
@Nullable
|
||||
SQLManager getSqlManager(@NotNull String name);
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package cc.carm.plugin.easysql.api;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public enum SQLSourceType {
|
||||
|
||||
MARIADB("org.mariadb.jdbc.Driver", "jdbc:mariadb://", "mariadb", new String[]{}),
|
||||
MYSQL("com.mysql.jdbc.Driver", "jdbc:mysql://", "mysql", new String[]{}),
|
||||
H2("org.h2.Driver", "jdbc:h2:", "h2", new String[]{"MODE=MySQL", "DB_CLOSE_DELAY=-1"});
|
||||
|
||||
private final @NotNull String databaseID;
|
||||
private final @NotNull String driverClass;
|
||||
private final @NotNull String urlPrefix;
|
||||
private final @NotNull String[] defaultSettings;
|
||||
|
||||
SQLSourceType(@NotNull String driverClass, @NotNull String urlPrefix,
|
||||
@NotNull String databaseID, @NotNull String[] defaultSettings) {
|
||||
this.databaseID = databaseID;
|
||||
this.driverClass = driverClass;
|
||||
this.urlPrefix = urlPrefix;
|
||||
this.defaultSettings = defaultSettings;
|
||||
}
|
||||
|
||||
public @NotNull String getDatabaseID() {
|
||||
return databaseID;
|
||||
}
|
||||
|
||||
public @NotNull String getDriverClass() {
|
||||
return driverClass;
|
||||
}
|
||||
|
||||
public @NotNull String getUrlPrefix() {
|
||||
return urlPrefix;
|
||||
}
|
||||
|
||||
public @NotNull String[] getDefaultSettings() {
|
||||
return defaultSettings;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user