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
|
// Prepare database connection
|
||||||
this.database = new MySqlDatabase(this);
|
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());
|
initialized.set(this.database.initialize());
|
||||||
if (initialized.get()) {
|
if (initialized.get()) {
|
||||||
log(Level.INFO, "Successfully established a connection to the database");
|
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.YamlComment;
|
||||||
import net.william278.annotaml.YamlFile;
|
import net.william278.annotaml.YamlFile;
|
||||||
import net.william278.annotaml.YamlKey;
|
import net.william278.annotaml.YamlKey;
|
||||||
|
import net.william278.husksync.database.Database;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
@@ -54,6 +55,10 @@ public class Settings {
|
|||||||
|
|
||||||
|
|
||||||
// Database 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")
|
@YamlComment("Database connection settings")
|
||||||
@YamlKey("database.credentials.host")
|
@YamlKey("database.credentials.host")
|
||||||
private String mySqlHost = "localhost";
|
private String mySqlHost = "localhost";
|
||||||
@@ -167,6 +172,12 @@ public class Settings {
|
|||||||
return debugLogging;
|
return debugLogging;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public Database.Type getSqlType() {
|
||||||
|
return databaseType;
|
||||||
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public String getMySqlHost() {
|
public String getMySqlHost() {
|
||||||
return mySqlHost;
|
return mySqlHost;
|
||||||
|
|||||||
@@ -191,4 +191,30 @@ public abstract class Database {
|
|||||||
*/
|
*/
|
||||||
public abstract void close();
|
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 {
|
public class MySqlDatabase extends Database {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* MySQL protocol
|
||||||
|
*/
|
||||||
|
private final Database.Type type;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* MySQL server hostname
|
* MySQL server hostname
|
||||||
*/
|
*/
|
||||||
@@ -74,6 +79,7 @@ public class MySqlDatabase extends Database {
|
|||||||
public MySqlDatabase(@NotNull HuskSync plugin) {
|
public MySqlDatabase(@NotNull HuskSync plugin) {
|
||||||
super(plugin);
|
super(plugin);
|
||||||
final Settings settings = plugin.getSettings();
|
final Settings settings = plugin.getSettings();
|
||||||
|
this.type = settings.getSqlType();
|
||||||
this.mySqlHost = settings.getMySqlHost();
|
this.mySqlHost = settings.getMySqlHost();
|
||||||
this.mySqlPort = settings.getMySqlPort();
|
this.mySqlPort = settings.getMySqlPort();
|
||||||
this.mySqlDatabaseName = settings.getMySqlDatabase();
|
this.mySqlDatabaseName = settings.getMySqlDatabase();
|
||||||
@@ -101,7 +107,7 @@ public class MySqlDatabase extends Database {
|
|||||||
public boolean initialize() {
|
public boolean initialize() {
|
||||||
try {
|
try {
|
||||||
// Create jdbc driver connection url
|
// 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 = new HikariDataSource();
|
||||||
connectionPool.setJdbcUrl(jdbcUrl);
|
connectionPool.setJdbcUrl(jdbcUrl);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user