From cecd80bea0c8c0d1c8c37a8d755953b2e959e1c3 Mon Sep 17 00:00:00 2001 From: Vankka Date: Sun, 9 Jul 2023 21:03:48 +0300 Subject: [PATCH] Add MariaDB driver support --- common/build.gradle | 9 ++- .../discordsrv/common/AbstractDiscordSRV.java | 1 + .../config/connection/StorageConfig.java | 5 +- .../DiscordSRVDependencyManager.java | 4 ++ .../common/storage/StorageType.java | 4 +- .../impl/sql/hikari/MariaDBStorage.java | 58 +++++++++++++++++++ .../storage/impl/sql/hikari/MySQLStorage.java | 8 ++- settings.gradle | 1 + 8 files changed, 84 insertions(+), 6 deletions(-) create mode 100644 common/src/main/java/com/discordsrv/common/storage/impl/sql/hikari/MariaDBStorage.java diff --git a/common/build.gradle b/common/build.gradle index 98f9742a..b515e96c 100644 --- a/common/build.gradle +++ b/common/build.gradle @@ -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) { diff --git a/common/src/main/java/com/discordsrv/common/AbstractDiscordSRV.java b/common/src/main/java/com/discordsrv/common/AbstractDiscordSRV.java index b3238b92..2c227476 100644 --- a/common/src/main/java/com/discordsrv/common/AbstractDiscordSRV.java +++ b/common/src/main/java/com/discordsrv/common/AbstractDiscordSRV.java @@ -567,6 +567,7 @@ public abstract class AbstractDiscordSRV storageFunction; private final String prettyName; diff --git a/common/src/main/java/com/discordsrv/common/storage/impl/sql/hikari/MariaDBStorage.java b/common/src/main/java/com/discordsrv/common/storage/impl/sql/hikari/MariaDBStorage.java new file mode 100644 index 00000000..ad11d596 --- /dev/null +++ b/common/src/main/java/com/discordsrv/common/storage/impl/sql/hikari/MariaDBStorage.java @@ -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); + } +} diff --git a/common/src/main/java/com/discordsrv/common/storage/impl/sql/hikari/MySQLStorage.java b/common/src/main/java/com/discordsrv/common/storage/impl/sql/hikari/MySQLStorage.java index 93d49918..72653124 100644 --- a/common/src/main/java/com/discordsrv/common/storage/impl/sql/hikari/MySQLStorage.java +++ b/common/src/main/java/com/discordsrv/common/storage/impl/sql/hikari/MySQLStorage.java @@ -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 { diff --git a/settings.gradle b/settings.gradle index f75cb6eb..1b51f8c9 100644 --- a/settings.gradle +++ b/settings.gradle @@ -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')