1
mirror of https://github.com/CarmJos/MineSQL.git synced 2024-09-19 12:15:45 +00:00

改用BeeCP,修改部分接口方法。

This commit is contained in:
Carm Jos 2022-02-22 02:13:54 +08:00
parent 48eed63ee7
commit 67bd7a4141
13 changed files with 207 additions and 142 deletions

2
.github/FUNDING.yml vendored Normal file
View File

@ -0,0 +1,2 @@
github: [ CarmJos ]
custom: [ 'https://donate.carm.cc' ]

View File

@ -9,4 +9,55 @@
|___/ |___/
```
# EasySQL-Plugin
# EasySQL-Plugin
轻松(用)SQL的独立运行库插件支持多种服务端适用于MineCraft全版本。
## 安装
## 配置
## 开发
### 依赖方式
## 指令
插件主指令为 `/easysql` ,所有指令只允许后台执行。
```text
# help
- 查看插件指令帮助。
# version
- 查看插件版本并进行版本更新查询。
# list
- 列出当前所有的数据源配置与相关信息。
# stats <数据源名称>
- 查看指定数据源的统计信息与当前仍未关闭的查询。
```
## 开源协议
本项目源码采用 [GNU General Public License v3.0](https://opensource.org/licenses/GPL-3.0) 开源协议。
<details>
<summary>关于 GPL 协议</summary>
> GNU General Public Licence (GPL) 有可能是开源界最常用的许可模式。GPL 保证了所有开发者的权利,同时为使用者提供了足够的复制,分发,修改的权利:
>
> #### 可自由复制
> 你可以将软件复制到你的电脑,你客户的电脑,或者任何地方。复制份数没有任何限制。
> #### 可自由分发
> 在你的网站提供下载拷贝到U盘送人或者将源代码打印出来从窗户扔出去环保起见请别这样做
> #### 可以用来盈利
> 你可以在分发软件的时候收费,但你必须在收费前向你的客户提供该软件的 GNU GPL 许可协议,以便让他们知道,他们可以从别的渠道免费得到这份软件,以及你收费的理由。
> #### 可自由修改
> 如果你想添加或删除某个功能,没问题,如果你想在别的项目中使用部分代码,也没问题,唯一的要求是,使用了这段代码的项目也必须使用 GPL 协议。
>
> 需要注意的是,分发的时候,需要明确提供源代码和二进制文件,另外,用于某些程序的某些协议有一些问题和限制,你可以看一下 @PierreJoye 写的 Practical Guide to GPL Compliance 一文。使用 GPL 协议,你必须在源代码代码中包含相应信息,以及协议本身。
>
> *以上文字来自 [五种开源协议GPL,LGPL,BSD,MIT,Apache](https://www.oschina.net/question/54100_9455) 。*
</details>

View File

@ -1,16 +1,16 @@
package cc.carm.plugin.easysql;
import cc.carm.plugin.easysql.api.EasySQLManager;
import cc.carm.plugin.easysql.api.EasySQLRegistry;
public class EasySQLAPI {
protected static EasySQLManager api;
protected static EasySQLRegistry api;
protected static void init(EasySQLManager api) {
protected static void init(EasySQLRegistry api) {
EasySQLAPI.api = api;
}
public static EasySQLManager get() {
public static EasySQLRegistry get() {
return api;
}

View File

@ -9,14 +9,13 @@ import java.util.List;
public class DBConfiguration {
public DBConfiguration create(@NotNull SQLSourceType sourceType, @NotNull String url) {
public DBConfiguration create(@NotNull SQLDriverType 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 final @NotNull List<String> initializeSQLs;
private @NotNull String url;
@ -24,28 +23,33 @@ public class DBConfiguration {
private @Nullable String password;
private @Nullable String connectionInitSql;
private @Nullable String connectionTestQuery;
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 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 @Nullable String validationSQL;
private @Nullable Integer validationTimeout;
private @Nullable Long validationInterval;
private DBConfiguration(@NotNull String driverClass, @NotNull String urlPrefix,
@NotNull String url, @NotNull String[] defaultSettings) {
@NotNull String url, @NotNull String[] initializeSQLs) {
this.driverClassName = driverClass;
this.urlPrefix = urlPrefix;
this.url = url;
this.defaultSettings = Arrays.asList(defaultSettings);
this.extraSettings = new ArrayList<>();
this.initializeSQLs = Arrays.asList(initializeSQLs);
}
public @NotNull String getDriverClassName() {
@ -56,39 +60,23 @@ public class DBConfiguration {
return urlPrefix;
}
public @NotNull List<String> getDefaultSettings() {
return defaultSettings;
public @NotNull List<String> getInitializeSQLs() {
return initializeSQLs;
}
public @NotNull List<String> getExtraSettings() {
return extraSettings;
}
public DBConfiguration setDefaultSettings(@Nullable String[] defaultSettings) {
this.defaultSettings.clear();
this.defaultSettings.addAll(Arrays.asList(defaultSettings));
public DBConfiguration setInitializeSQLs(@Nullable String[] initializeSQLs) {
this.initializeSQLs.clear();
this.initializeSQLs.addAll(Arrays.asList(initializeSQLs));
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);
public DBConfiguration addInitializeSQL(@NotNull String initializeSQL) {
this.initializeSQLs.add(initializeSQL);
return this;
}
public DBConfiguration clearExtraSettings() {
this.defaultSettings.clear();
this.initializeSQLs.clear();
return this;
}
@ -128,12 +116,21 @@ public class DBConfiguration {
return this;
}
public @Nullable String getConnectionTestQuery() {
return connectionTestQuery;
public @Nullable String getValidationSQL() {
return validationSQL;
}
public DBConfiguration setConnectionTestQuery(String connectionTestQuery) {
this.connectionTestQuery = connectionTestQuery;
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;
}
@ -173,20 +170,20 @@ public class DBConfiguration {
return this;
}
public @Nullable Long getConnectionTimeout() {
return connectionTimeout;
public @Nullable Integer getMaxHoldTime() {
return maxHoldTime;
}
public DBConfiguration setConnectionTimeout(Long connectionTimeout) {
this.connectionTimeout = connectionTimeout;
public DBConfiguration setMaxHoldTime(@Nullable Integer maxHoldTime) {
this.maxHoldTime = maxHoldTime;
return this;
}
public @Nullable Long getValidationTimeout() {
public @Nullable Integer getValidationTimeout() {
return validationTimeout;
}
public DBConfiguration setValidationTimeout(Long validationTimeout) {
public DBConfiguration setValidationTimeout(Integer validationTimeout) {
this.validationTimeout = validationTimeout;
return this;
}
@ -200,30 +197,12 @@ public class DBConfiguration {
return this;
}
public @Nullable Long getLeakDetectionThreshold() {
return leakDetectionThreshold;
public @Nullable Integer getMaxActive() {
return maxActive;
}
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;
public DBConfiguration setMaxActive(Integer maxActive) {
this.maxActive = maxActive;
return this;
}
@ -231,17 +210,18 @@ public class DBConfiguration {
return maxPoolSize;
}
public DBConfiguration setMaxPoolSize(Integer maxPoolSize) {
public DBConfiguration setMaxPoolSize(@Nullable Integer maxPoolSize) {
this.maxPoolSize = maxPoolSize;
return this;
}
public @Nullable Integer getMinIdle() {
return minIdle;
public @Nullable Long getMaxWaitTime() {
return maxWaitTime;
}
public DBConfiguration setMinIdle(Integer minIdle) {
this.minIdle = minIdle;
public DBConfiguration setMaxWaitTime(Long maxWaitTime) {
this.maxWaitTime = maxWaitTime;
return this;
}
}

View File

@ -10,11 +10,12 @@ import java.util.Map;
import java.util.Optional;
import java.util.Properties;
import java.util.UUID;
import java.util.function.Consumer;
/**
* 入口类
*/
public interface EasySQLManager {
public interface EasySQLRegistry {
/**
* 获取原生注册的指定名称的 SQLManager 实例
@ -75,15 +76,33 @@ public interface EasySQLManager {
@NotNull String propertyFileName) throws Exception;
/**
* 终止一个 SQLManager 实例
* 终止并关闭一个 SQLManager 实例
*
* @param manager
* @return
* @param manager SQLManager实例
* @param activeQueries 终止前仍未被关闭的SQLQuery列表
*/
default Map<UUID, SQLQuery> shutdown(SQLManager manager) {
return shutdown(manager, true);
void shutdown(SQLManager manager, @Nullable Consumer<Map<UUID, SQLQuery>> activeQueries);
/**
* 终止并关闭一个 SQLManager 实例
*
* @param manager SQLManager实例
* @param forceClose 是否强制关闭进行中的查询
*/
default void shutdown(SQLManager manager, boolean forceClose) {
shutdown(manager, (unclosedQueries) -> {
if (forceClose) unclosedQueries.values().forEach(SQLQuery::close);
});
}
Map<UUID, SQLQuery> shutdown(SQLManager manager, boolean forceClose);
/**
* 终止并关闭一个 SQLManager 实例
* <br>若在终止时仍有活跃的查询则将会强制关闭
*
* @param manager SQLManager实例
*/
default void shutdown(SQLManager manager) {
shutdown(manager, true);
}
}

View File

@ -2,7 +2,7 @@ package cc.carm.plugin.easysql.api;
import org.jetbrains.annotations.NotNull;
public enum SQLSourceType {
public enum SQLDriverType {
MARIADB("org.mariadb.jdbc.Driver", "jdbc:mariadb://", "mariadb", new String[]{}),
MYSQL("com.mysql.jdbc.Driver", "jdbc:mysql://", "mysql", new String[]{}),
@ -13,12 +13,12 @@ public enum SQLSourceType {
private final @NotNull String urlPrefix;
private final @NotNull String[] defaultSettings;
SQLSourceType(@NotNull String driverClass, @NotNull String urlPrefix,
@NotNull String databaseID, @NotNull String[] defaultSettings) {
SQLDriverType(@NotNull String driverClass, @NotNull String urlPrefix,
@NotNull String databaseID, @NotNull String[] initializeSQLs) {
this.databaseID = databaseID;
this.driverClass = driverClass;
this.urlPrefix = urlPrefix;
this.defaultSettings = defaultSettings;
this.defaultSettings = initializeSQLs;
}
public @NotNull String getDatabaseID() {

View File

@ -31,8 +31,8 @@
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<groupId>com.github.chris2018998</groupId>
<artifactId>beecp</artifactId>
<scope>compile</scope>
</dependency>

View File

@ -9,6 +9,8 @@ import java.util.logging.Logger;
public interface EasySQLPluginPlatform {
@NotNull Map<String, DBConfiguration> readConfigurations();
@NotNull Map<String, Properties> readProperties();

View File

@ -4,21 +4,22 @@ import cc.carm.lib.easysql.api.SQLManager;
import cc.carm.lib.easysql.api.SQLQuery;
import cc.carm.lib.easysql.manager.SQLManagerImpl;
import cc.carm.plugin.easysql.api.DBConfiguration;
import cc.carm.plugin.easysql.api.EasySQLManager;
import cc.carm.plugin.easysql.api.EasySQLRegistry;
import cn.beecp.BeeDataSource;
import cn.beecp.BeeDataSourceConfig;
import com.google.common.collect.ImmutableMap;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Unmodifiable;
import java.util.*;
import java.util.function.Consumer;
public class EasySQLManagerImpl implements EasySQLManager {
public class EasySQLRegistryImpl implements EasySQLRegistry {
private final HashMap<String, SQLManager> sqlManagerRegistry = new HashMap<>();
public EasySQLManagerImpl(@NotNull EasySQLPluginPlatform platform) {
public EasySQLRegistryImpl(@NotNull EasySQLPluginPlatform platform) {
Map<String, DBConfiguration> configurations = platform.readConfigurations();
if (configurations.isEmpty()) {
@ -64,54 +65,53 @@ public class EasySQLManagerImpl implements EasySQLManager {
@Override
public @NotNull SQLManager create(@Nullable String name, @NotNull DBConfiguration configuration) {
HikariConfig config = new HikariConfig();
BeeDataSourceConfig config = new BeeDataSourceConfig();
config.setDriverClassName(configuration.getDriverClassName());
config.setJdbcUrl(configuration.getUrlPrefix() + configuration.getUrl());
Optional.ofNullable(configuration.getPoolName()).ifPresent(config::setPoolName);
Optional.ofNullable(configuration.getUsername()).ifPresent(config::setUsername);
Optional.ofNullable(configuration.getPassword()).ifPresent(config::setPassword);
Optional.ofNullable(configuration.getMaxPoolSize()).ifPresent(config::setMaximumPoolSize);
Optional.ofNullable(configuration.getMinIdle()).ifPresent(config::setMinimumIdle);
Optional.ofNullable(configuration.getPoolName()).ifPresent(config::setPoolName);
Optional.ofNullable(configuration.getMaxPoolSize()).ifPresent(config::setMaxActive);
Optional.ofNullable(configuration.getMaxActive()).ifPresent(config::setMaxActive);
Optional.ofNullable(configuration.getIdleTimeout()).ifPresent(config::setIdleTimeout);
Optional.ofNullable(configuration.getKeepaliveTime()).ifPresent(config::setKeepaliveTime);
Optional.ofNullable(configuration.getConnectionTimeout()).ifPresent(config::setConnectionTimeout);
Optional.ofNullable(configuration.getValidationTimeout()).ifPresent(config::setValidationTimeout);
Optional.ofNullable(configuration.getMaxLifetime()).ifPresent(config::setMaxLifetime);
Optional.ofNullable(configuration.getLeakDetectionThreshold()).ifPresent(config::setLeakDetectionThreshold);
Optional.ofNullable(configuration.getConnectionTestQuery()).ifPresent(config::setConnectionTestQuery);
Optional.ofNullable(configuration.getConnectionInitSql()).ifPresent(config::setConnectionInitSql);
Optional.ofNullable(configuration.getAutoCommit()).ifPresent(config::setAutoCommit);
Optional.ofNullable(configuration.getReadOnly()).ifPresent(config::setReadOnly);
Optional.ofNullable(configuration.getSchema()).ifPresent(config::setSchema);
Optional.ofNullable(configuration.getMaxWaitTime()).ifPresent(config::setMaxWait);
Optional.ofNullable(configuration.getMaxHoldTime()).ifPresent(config::setHoldTimeout);
Optional.ofNullable(configuration.getAutoCommit()).ifPresent(config::setDefaultAutoCommit);
Optional.ofNullable(configuration.getReadOnly()).ifPresent(config::setDefaultReadOnly);
Optional.ofNullable(configuration.getSchema()).ifPresent(config::setDefaultSchema);
Optional.ofNullable(configuration.getValidationSQL()).ifPresent(config::setValidTestSql);
Optional.ofNullable(configuration.getValidationTimeout()).ifPresent(config::setValidTestTimeout);
Optional.ofNullable(configuration.getValidationInterval()).ifPresent(config::setValidAssumeTime);
return create(name, config);
}
@Override
public @NotNull SQLManager create(@Nullable String name, @NotNull Properties properties) {
return create(name, new HikariConfig(properties));
return create(name, new BeeDataSourceConfig(properties));
}
@Override
public @NotNull SQLManager create(@Nullable String name, @NotNull String propertyFileName) {
return create(name, new HikariConfig(propertyFileName));
return create(name, new BeeDataSourceConfig(propertyFileName));
}
public @NotNull SQLManager create(@Nullable String name, @NotNull HikariConfig configuration) {
return new SQLManagerImpl(new HikariDataSource(configuration), name);
public @NotNull SQLManager create(@Nullable String name, @NotNull BeeDataSourceConfig configuration) {
return new SQLManagerImpl(new BeeDataSource(configuration), name);
}
@Override
public Map<UUID, SQLQuery> shutdown(SQLManager manager, boolean forceClose) {
Map<UUID, SQLQuery> queries = manager.getActiveQuery();
if (forceClose) manager.getActiveQuery().values().forEach(SQLQuery::close);
public void shutdown(SQLManager manager, @Nullable Consumer<Map<UUID, SQLQuery>> activeQueries) {
if (activeQueries != null) activeQueries.accept(manager.getActiveQuery());
if (manager.getDataSource() instanceof HikariDataSource) {
//Close hikari pool
((HikariDataSource) manager.getDataSource()).close();
if (manager.getDataSource() instanceof BeeDataSource) {
BeeDataSource dataSource = (BeeDataSource) manager.getDataSource();
dataSource.close(); //Close bee connection pool
}
return queries;
}
}

View File

@ -0,0 +1,11 @@
&5&l ______ _____ ____ _ &d&l_____ _ _
&5&l| ____| / ____|/ __ \| | &d&l| __ \| | (_)
&5&l| |__ __ _ ___ _ _| (___ | | | | | &d&l| |__) | |_ _ __ _ _ _ __
&5&l| __| / _` / __| | | |\___ \| | | | | &d&l| ___/| | | | |/ _` | | '_ \
&5&l| |___| (_| \__ \ |_| |____) | |__| | |____ &d&l| | | | |_| | (_| | | | | |
&5&l|______\__,_|___/\__, |_____/ \___\_\______| &d&l|_| |_|\__,_|\__, |_|_| |_|
&5&l __/ | &d&l __/ |
&5&l |___/ &d&l|___/
&f View more information at Github &d&lhttps://github.com/CarmJos/EasySQL-Plugin

View File

@ -3,16 +3,17 @@
# 检查更新为异步操作,绝不会影响性能与使用体验。
check-update: true
properties:
enable: true
folder: "properties/"
instances:
name1:
configurations:
"example-mariadb": # database id
driver-type: mariadb
url: 127.0.0.1 # 数据库地址
port: 3306 # 数据库端口
use-ssl: false # 是否使用SSL连接
username: minecraft # 数据库用户名
password: password #数据库连接密码
database: minecraft #数据库名
url: jdbc:mysql://localhost:3306/name1
port: 3306
username: mc
password: password
use-ssl: false
name2:
url: jdbc:mysql://localhost:3306/name2
username: mc
password: password

View File

@ -124,11 +124,10 @@
<dependencies>
<dependency>
<!--项目地址 https://github.com/brettwooldridge/HikariCP/ -->
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>${hikaricp.version}</version>
<optional>true</optional>
<!--项目地址 https://github.com/Chris2018998/BeeCP -->
<groupId>com.github.chris2018998</groupId>
<artifactId>beecp</artifactId>
<version>3.3.2</version>
</dependency>
<dependency>