mirror of
https://github.com/DiscordSRV/Ascension.git
synced 2024-12-25 17:08:27 +01:00
Add MariaDB driver support
This commit is contained in:
parent
2e48e91a8f
commit
cecd80bea0
@ -4,8 +4,9 @@ configurations {
|
||||
hikari
|
||||
h2Driver
|
||||
mysqlDriver
|
||||
mariadbDriver
|
||||
mcAuthLib
|
||||
compileOnly.extendsFrom hikari, h2Driver, mysqlDriver, mcAuthLib
|
||||
compileOnly.extendsFrom hikari, h2Driver, mysqlDriver, mariadbDriver, mcAuthLib
|
||||
testRuntimeOnly.extendsFrom runtimeDownloadOnly
|
||||
}
|
||||
|
||||
@ -24,6 +25,11 @@ tasks.register('generateResourceForMySQLDriver', GenerateDependencyDownloadResou
|
||||
configuration = conf
|
||||
file = 'dependencies/' + conf.name + '.txt'
|
||||
}
|
||||
tasks.register('generateResourceForMariaDBDriver', GenerateDependencyDownloadResourceTask) {
|
||||
var conf = configurations.mariadbDriver
|
||||
configuration = conf
|
||||
file = 'dependencies/' + conf.name + '.txt'
|
||||
}
|
||||
tasks.register('generateResourceForMCAuthLib', GenerateDependencyDownloadResourceTask) {
|
||||
var conf = configurations.mcAuthLib
|
||||
configuration = conf
|
||||
@ -76,6 +82,7 @@ dependencies {
|
||||
hikari(libs.hikaricp)
|
||||
h2Driver(libs.h2)
|
||||
mysqlDriver(libs.mysql)
|
||||
mariadbDriver(libs.mariadb)
|
||||
|
||||
// MinecraftAuthentication library
|
||||
mcAuthLib(libs.minecraftauth.lib) {
|
||||
|
@ -567,6 +567,7 @@ public abstract class AbstractDiscordSRV<B extends IBootstrap, C extends MainCon
|
||||
switch (backend.toLowerCase(Locale.ROOT)) {
|
||||
case "h2": return StorageType.H2;
|
||||
case "mysql": return StorageType.MYSQL;
|
||||
case "mariadb": return StorageType.MARIADB;
|
||||
}
|
||||
throw new StorageException("Unknown storage backend \"" + backend + "\"");
|
||||
}
|
||||
|
@ -30,13 +30,14 @@ public class StorageConfig {
|
||||
|
||||
@Comment("The storage backend to use.\n\n"
|
||||
+ "- H2\n"
|
||||
+ "- MySQL")
|
||||
+ "- MySQL\n"
|
||||
+ "- MariaDB")
|
||||
public String backend = "H2";
|
||||
|
||||
@Comment("SQL table prefix")
|
||||
public String sqlTablePrefix = "discordsrv_";
|
||||
|
||||
@Comment("Connection options for remote databases (MySQL)")
|
||||
@Comment("Connection options for remote databases (MySQL, MariaDB)")
|
||||
public Remote remote = new Remote();
|
||||
|
||||
@Comment("Extra connection properties for database drivers")
|
||||
|
@ -61,6 +61,10 @@ public class DiscordSRVDependencyManager {
|
||||
return loader(new String[] {"dependencies/mysqlDriver.txt"});
|
||||
}
|
||||
|
||||
public DependencyLoader mariadb() throws IOException {
|
||||
return loader(new String[] {"dependencies/mariadbDriver.txt"});
|
||||
}
|
||||
|
||||
public DependencyLoader mcAuthLib() throws IOException {
|
||||
return loader(new String[] {"dependencies/mcAuthLib.txt"});
|
||||
}
|
||||
|
@ -20,6 +20,7 @@ package com.discordsrv.common.storage;
|
||||
|
||||
import com.discordsrv.common.DiscordSRV;
|
||||
import com.discordsrv.common.storage.impl.sql.file.H2Storage;
|
||||
import com.discordsrv.common.storage.impl.sql.hikari.MariaDBStorage;
|
||||
import com.discordsrv.common.storage.impl.sql.hikari.MySQLStorage;
|
||||
|
||||
import java.util.function.Function;
|
||||
@ -27,7 +28,8 @@ import java.util.function.Function;
|
||||
public enum StorageType {
|
||||
|
||||
H2(H2Storage::new, "H2", false),
|
||||
MYSQL(MySQLStorage::new, "MySQL", true);
|
||||
MYSQL(MySQLStorage::new, "MySQL", true),
|
||||
MARIADB(MariaDBStorage::new, "MariaDB", true);
|
||||
|
||||
private final Function<DiscordSRV, Storage> storageFunction;
|
||||
private final String prettyName;
|
||||
|
@ -0,0 +1,58 @@
|
||||
package com.discordsrv.common.storage.impl.sql.hikari;
|
||||
|
||||
import com.discordsrv.common.DiscordSRV;
|
||||
import com.discordsrv.common.config.connection.StorageConfig;
|
||||
import com.discordsrv.common.exception.StorageException;
|
||||
import com.zaxxer.hikari.HikariConfig;
|
||||
import dev.vankka.dependencydownload.classloader.IsolatedClassLoader;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class MariaDBStorage extends HikariStorage {
|
||||
|
||||
private IsolatedClassLoader classLoader;
|
||||
|
||||
public MariaDBStorage(DiscordSRV discordSRV) {
|
||||
super(discordSRV);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
super.close();
|
||||
if (classLoader != null) {
|
||||
try {
|
||||
classLoader.close();
|
||||
} catch (IOException e) {
|
||||
discordSRV.logger().error("Failed to close isolated classloader", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initialize() {
|
||||
try {
|
||||
initializeWithContext(classLoader = discordSRV.dependencyManager().mariadb().loadIntoIsolated());
|
||||
} catch (IOException e) {
|
||||
throw new StorageException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void createTables(Connection connection, String tablePrefix) throws SQLException {
|
||||
// Same table creation language
|
||||
MySQLStorage.createTablesMySQL(connection, tablePrefix);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void applyConfiguration(HikariConfig config, StorageConfig storageConfig) {
|
||||
String address = storageConfig.remote.databaseAddress;
|
||||
if (!address.contains(":")) {
|
||||
address += ":3306";
|
||||
}
|
||||
|
||||
config.setDriverClassName("org.mariadb.jdbc.Driver");
|
||||
config.setJdbcUrl("jdbc:mariadb://" + address + "/" + storageConfig.remote.databaseName);
|
||||
}
|
||||
}
|
@ -50,8 +50,7 @@ public class MySQLStorage extends HikariStorage {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void createTables(Connection connection, String tablePrefix) throws SQLException {
|
||||
public static void createTablesMySQL(Connection connection, String tablePrefix) throws SQLException {
|
||||
try (Statement statement = connection.createStatement()) {
|
||||
statement.execute(
|
||||
"create table if not exists " + tablePrefix + LINKED_ACCOUNTS_TABLE_NAME + " "
|
||||
@ -63,6 +62,11 @@ public class MySQLStorage extends HikariStorage {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void createTables(Connection connection, String tablePrefix) throws SQLException {
|
||||
createTablesMySQL(connection, tablePrefix);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initialize() {
|
||||
try {
|
||||
|
@ -89,6 +89,7 @@ dependencyResolutionManagement {
|
||||
}
|
||||
library('h2', 'com.h2database', 'h2').version('2.1.210')
|
||||
library('mysql', 'mysql', 'mysql-connector-java').version('8.0.28')
|
||||
library('mariadb', 'org.mariadb.jdbc', 'mariadb-java-client').version('3.1.4')
|
||||
|
||||
// MinecraftAuth lib
|
||||
library('minecraftauth-lib', 'me.minecraftauth', 'lib').version('1.1.0')
|
||||
|
Loading…
Reference in New Issue
Block a user