mirror of
https://github.com/WiIIiam278/HuskSync.git
synced 2025-12-23 16:49:19 +00:00
Add mariadb protocol option type (#145)
Co-authored-by: William <will27528@gmail.com>
This commit is contained in:
@@ -130,7 +130,7 @@ public class BukkitHuskSync extends JavaPlugin implements HuskSync {
|
||||
|
||||
// Prepare database connection
|
||||
this.database = new MySqlDatabase(this);
|
||||
log(Level.INFO, "Attempting to establish connection to the database...");
|
||||
log(Level.INFO, "Attempting to establish connection to the " + settings.getSqlType().getDisplayName() + " database...");
|
||||
initialized.set(this.database.initialize());
|
||||
if (initialized.get()) {
|
||||
log(Level.INFO, "Successfully established a connection to the database");
|
||||
|
||||
@@ -22,6 +22,7 @@ package net.william278.husksync.config;
|
||||
import net.william278.annotaml.YamlComment;
|
||||
import net.william278.annotaml.YamlFile;
|
||||
import net.william278.annotaml.YamlKey;
|
||||
import net.william278.husksync.database.Database;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.*;
|
||||
@@ -54,6 +55,10 @@ public class Settings {
|
||||
|
||||
|
||||
// Database settings
|
||||
@YamlComment("Type of database to use (MYSQL, SQLITE)")
|
||||
@YamlKey("database.type")
|
||||
private Database.Type databaseType = Database.Type.MYSQL;
|
||||
|
||||
@YamlComment("Database connection settings")
|
||||
@YamlKey("database.credentials.host")
|
||||
private String mySqlHost = "localhost";
|
||||
@@ -167,6 +172,12 @@ public class Settings {
|
||||
return debugLogging;
|
||||
}
|
||||
|
||||
|
||||
@NotNull
|
||||
public Database.Type getSqlType() {
|
||||
return databaseType;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public String getMySqlHost() {
|
||||
return mySqlHost;
|
||||
|
||||
@@ -191,4 +191,30 @@ public abstract class Database {
|
||||
*/
|
||||
public abstract void close();
|
||||
|
||||
/**
|
||||
* Identifies types of databases
|
||||
*/
|
||||
public enum Type {
|
||||
MYSQL("MySQL", "mysql"),
|
||||
MARIADB("MariaDB", "mariadb");
|
||||
|
||||
private final String displayName;
|
||||
private final String protocol;
|
||||
|
||||
Type(@NotNull String displayName, @NotNull String protocol) {
|
||||
this.displayName = displayName;
|
||||
this.protocol = protocol;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public String getDisplayName() {
|
||||
return displayName;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public String getProtocol() {
|
||||
return protocol;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -40,6 +40,11 @@ import java.util.logging.Level;
|
||||
|
||||
public class MySqlDatabase extends Database {
|
||||
|
||||
/**
|
||||
* MySQL protocol
|
||||
*/
|
||||
private final Database.Type type;
|
||||
|
||||
/**
|
||||
* MySQL server hostname
|
||||
*/
|
||||
@@ -74,6 +79,7 @@ public class MySqlDatabase extends Database {
|
||||
public MySqlDatabase(@NotNull HuskSync plugin) {
|
||||
super(plugin);
|
||||
final Settings settings = plugin.getSettings();
|
||||
this.type = settings.getSqlType();
|
||||
this.mySqlHost = settings.getMySqlHost();
|
||||
this.mySqlPort = settings.getMySqlPort();
|
||||
this.mySqlDatabaseName = settings.getMySqlDatabase();
|
||||
@@ -101,7 +107,7 @@ public class MySqlDatabase extends Database {
|
||||
public boolean initialize() {
|
||||
try {
|
||||
// Create jdbc driver connection url
|
||||
final String jdbcUrl = "jdbc:mysql://" + mySqlHost + ":" + mySqlPort + "/" + mySqlDatabaseName + mySqlConnectionParameters;
|
||||
final String jdbcUrl = "jdbc:" + type.getProtocol() + "://" + mySqlHost + ":" + mySqlPort + "/" + mySqlDatabaseName + mySqlConnectionParameters;
|
||||
connectionPool = new HikariDataSource();
|
||||
connectionPool.setJdbcUrl(jdbcUrl);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user