Add MariaDB driver support

This commit is contained in:
Vankka 2023-07-09 21:03:48 +03:00
parent 2e48e91a8f
commit cecd80bea0
No known key found for this signature in database
GPG Key ID: 6E50CB7A29B96AD0
8 changed files with 84 additions and 6 deletions

View File

@ -4,8 +4,9 @@ configurations {
hikari hikari
h2Driver h2Driver
mysqlDriver mysqlDriver
mariadbDriver
mcAuthLib mcAuthLib
compileOnly.extendsFrom hikari, h2Driver, mysqlDriver, mcAuthLib compileOnly.extendsFrom hikari, h2Driver, mysqlDriver, mariadbDriver, mcAuthLib
testRuntimeOnly.extendsFrom runtimeDownloadOnly testRuntimeOnly.extendsFrom runtimeDownloadOnly
} }
@ -24,6 +25,11 @@ tasks.register('generateResourceForMySQLDriver', GenerateDependencyDownloadResou
configuration = conf configuration = conf
file = 'dependencies/' + conf.name + '.txt' file = 'dependencies/' + conf.name + '.txt'
} }
tasks.register('generateResourceForMariaDBDriver', GenerateDependencyDownloadResourceTask) {
var conf = configurations.mariadbDriver
configuration = conf
file = 'dependencies/' + conf.name + '.txt'
}
tasks.register('generateResourceForMCAuthLib', GenerateDependencyDownloadResourceTask) { tasks.register('generateResourceForMCAuthLib', GenerateDependencyDownloadResourceTask) {
var conf = configurations.mcAuthLib var conf = configurations.mcAuthLib
configuration = conf configuration = conf
@ -76,6 +82,7 @@ dependencies {
hikari(libs.hikaricp) hikari(libs.hikaricp)
h2Driver(libs.h2) h2Driver(libs.h2)
mysqlDriver(libs.mysql) mysqlDriver(libs.mysql)
mariadbDriver(libs.mariadb)
// MinecraftAuthentication library // MinecraftAuthentication library
mcAuthLib(libs.minecraftauth.lib) { mcAuthLib(libs.minecraftauth.lib) {

View File

@ -567,6 +567,7 @@ public abstract class AbstractDiscordSRV<B extends IBootstrap, C extends MainCon
switch (backend.toLowerCase(Locale.ROOT)) { switch (backend.toLowerCase(Locale.ROOT)) {
case "h2": return StorageType.H2; case "h2": return StorageType.H2;
case "mysql": return StorageType.MYSQL; case "mysql": return StorageType.MYSQL;
case "mariadb": return StorageType.MARIADB;
} }
throw new StorageException("Unknown storage backend \"" + backend + "\""); throw new StorageException("Unknown storage backend \"" + backend + "\"");
} }

View File

@ -30,13 +30,14 @@ public class StorageConfig {
@Comment("The storage backend to use.\n\n" @Comment("The storage backend to use.\n\n"
+ "- H2\n" + "- H2\n"
+ "- MySQL") + "- MySQL\n"
+ "- MariaDB")
public String backend = "H2"; public String backend = "H2";
@Comment("SQL table prefix") @Comment("SQL table prefix")
public String sqlTablePrefix = "discordsrv_"; public String sqlTablePrefix = "discordsrv_";
@Comment("Connection options for remote databases (MySQL)") @Comment("Connection options for remote databases (MySQL, MariaDB)")
public Remote remote = new Remote(); public Remote remote = new Remote();
@Comment("Extra connection properties for database drivers") @Comment("Extra connection properties for database drivers")

View File

@ -61,6 +61,10 @@ public class DiscordSRVDependencyManager {
return loader(new String[] {"dependencies/mysqlDriver.txt"}); return loader(new String[] {"dependencies/mysqlDriver.txt"});
} }
public DependencyLoader mariadb() throws IOException {
return loader(new String[] {"dependencies/mariadbDriver.txt"});
}
public DependencyLoader mcAuthLib() throws IOException { public DependencyLoader mcAuthLib() throws IOException {
return loader(new String[] {"dependencies/mcAuthLib.txt"}); return loader(new String[] {"dependencies/mcAuthLib.txt"});
} }

View File

@ -20,6 +20,7 @@ package com.discordsrv.common.storage;
import com.discordsrv.common.DiscordSRV; import com.discordsrv.common.DiscordSRV;
import com.discordsrv.common.storage.impl.sql.file.H2Storage; 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 com.discordsrv.common.storage.impl.sql.hikari.MySQLStorage;
import java.util.function.Function; import java.util.function.Function;
@ -27,7 +28,8 @@ import java.util.function.Function;
public enum StorageType { public enum StorageType {
H2(H2Storage::new, "H2", false), 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 Function<DiscordSRV, Storage> storageFunction;
private final String prettyName; private final String prettyName;

View File

@ -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);
}
}

View File

@ -50,8 +50,7 @@ public class MySQLStorage extends HikariStorage {
} }
} }
@Override public static void createTablesMySQL(Connection connection, String tablePrefix) throws SQLException {
public void createTables(Connection connection, String tablePrefix) throws SQLException {
try (Statement statement = connection.createStatement()) { try (Statement statement = connection.createStatement()) {
statement.execute( statement.execute(
"create table if not exists " + tablePrefix + LINKED_ACCOUNTS_TABLE_NAME + " " "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 @Override
public void initialize() { public void initialize() {
try { try {

View File

@ -89,6 +89,7 @@ dependencyResolutionManagement {
} }
library('h2', 'com.h2database', 'h2').version('2.1.210') library('h2', 'com.h2database', 'h2').version('2.1.210')
library('mysql', 'mysql', 'mysql-connector-java').version('8.0.28') library('mysql', 'mysql', 'mysql-connector-java').version('8.0.28')
library('mariadb', 'org.mariadb.jdbc', 'mariadb-java-client').version('3.1.4')
// MinecraftAuth lib // MinecraftAuth lib
library('minecraftauth-lib', 'me.minecraftauth', 'lib').version('1.1.0') library('minecraftauth-lib', 'me.minecraftauth', 'lib').version('1.1.0')