Skip to content

Commit

Permalink
Add support for mariadb driver
Browse files Browse the repository at this point in the history
  • Loading branch information
dig committed Jul 29, 2020
1 parent 73a6621 commit 0c11c52
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

public interface DataStorage {

boolean init();
boolean init(Storage storage);

void close();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
public enum Storage {

FLATFILE,
MYSQL
MYSQL,
MARIADB

}
2 changes: 2 additions & 0 deletions bukkit/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ dependencies {
compileOnly 'org.spigotmc:spigot-api:1.16.1-R0.1-SNAPSHOT'
compileOnly 'org.bukkit:craftbukkit:1.16.1-R0.1-SNAPSHOT'
compile 'com.zaxxer:HikariCP:3.4.5'
compile 'org.mariadb.jdbc:mariadb-java-client:2.6.2'
compile 'de.tr7zw:item-nbt-api:2.4.1'
}

Expand All @@ -20,5 +21,6 @@ shadowJar {

relocate 'de.tr7zw.changeme.nbtapi', 'com.github.dig.endervaults.lib.nbt'
relocate 'com.zaxxer.hikari', 'com.github.dig.endervaults.lib.hikari'
relocate 'org.mariadb.jdbc', 'com.github.dig.endervaults.lib.mariadb'
relocate 'org.javatuples', 'com.github.dig.endervaults.lib.javatuples'
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import com.github.dig.endervaults.bukkit.ui.icon.SelectIconListener;
import com.github.dig.endervaults.bukkit.ui.selector.SelectorListener;
import com.github.dig.endervaults.bukkit.storage.YamlStorage;
import com.github.dig.endervaults.bukkit.storage.HikariMySQLStorage;
import com.github.dig.endervaults.bukkit.storage.HikariStorage;
import com.github.dig.endervaults.bukkit.vault.BukkitVaultAutoSave;
import com.github.dig.endervaults.bukkit.vault.BukkitVaultPersister;
import com.github.dig.endervaults.bukkit.vault.metadata.BukkitVaultMetadataRegistry;
Expand Down Expand Up @@ -201,13 +201,14 @@ private boolean setupDataStorage() {
case FLATFILE:
dataStorage = new YamlStorage();
break;
case MARIADB:
case MYSQL:
dataStorage = new HikariMySQLStorage();
dataStorage = new HikariStorage();
break;
}

log.log(Level.INFO, "[EnderVaults] Using data storage: " + storage.toString() + ".");
if (!dataStorage.init()) {
if (!dataStorage.init(storage)) {
log.log(Level.SEVERE, "[EnderVaults] Error with data storage, disabling...");
Bukkit.getPluginManager().disablePlugin(this);
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.github.dig.endervaults.api.PluginProvider;
import com.github.dig.endervaults.api.lang.Lang;
import com.github.dig.endervaults.api.storage.DataStorage;
import com.github.dig.endervaults.api.storage.Storage;
import com.github.dig.endervaults.api.util.VaultSerializable;
import com.github.dig.endervaults.api.vault.Vault;
import com.github.dig.endervaults.api.vault.metadata.MetadataConverter;
Expand All @@ -24,7 +25,7 @@
import java.util.logging.Level;

@Log
public class HikariMySQLStorage implements DataStorage {
public class HikariStorage implements DataStorage {

private final EVBukkitPlugin plugin = (EVBukkitPlugin) PluginProvider.getPlugin();

Expand All @@ -33,21 +34,26 @@ public class HikariMySQLStorage implements DataStorage {
private String metadataTable;

@Override
public boolean init() {
public boolean init(Storage storage) {
FileConfiguration config = (FileConfiguration) plugin.getConfigFile().getConfiguration();
ConfigurationSection settings = config.getConfigurationSection("storage.settings.mysql");
ConfigurationSection settings = config.getConfigurationSection(storage == Storage.MYSQL ? "storage.settings.mysql" : "storage.settings.mariadb");

String address = settings.getString("address", "localhost");
String database = settings.getString("database", "minecraft");
String user = settings.getString("user", "minecraft");
String password = settings.getString("password", "123");

HikariConfig hikariConfig = new HikariConfig();
hikariConfig.setJdbcUrl(String.format("jdbc:mysql://%s/%s", address, database));
hikariConfig.setJdbcUrl(String.format("jdbc:%s://%s/%s", storage == Storage.MYSQL ? "mysql" : "mariadb", address, database));

hikariConfig.setUsername(user);
hikariConfig.setPassword(password);

if (storage == Storage.MARIADB) {
hikariConfig.setDriverClassName("org.mariadb.jdbc.Driver");
hikariConfig.setAutoCommit(false);
}

ConfigurationSection properties = settings.getConfigurationSection("properties");
for (String key : properties.getKeys(false)) {
hikariConfig.addDataSourceProperty(key, properties.getString(key));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.github.dig.endervaults.api.PluginProvider;
import com.github.dig.endervaults.api.lang.Lang;
import com.github.dig.endervaults.api.storage.DataStorage;
import com.github.dig.endervaults.api.storage.Storage;
import com.github.dig.endervaults.api.util.VaultSerializable;
import com.github.dig.endervaults.api.vault.Vault;
import com.github.dig.endervaults.api.vault.metadata.VaultMetadataRegistry;
Expand All @@ -25,7 +26,7 @@ public class YamlStorage implements DataStorage {
private final EVBukkitPlugin plugin = (EVBukkitPlugin) PluginProvider.getPlugin();

@Override
public boolean init() {
public boolean init(Storage storage) {
return true;
}

Expand Down
30 changes: 29 additions & 1 deletion bukkit/src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,19 @@ storage:

# All the different settings for the storage methods.
settings:

# Flat file settings
flatfile:
# Possible file formats:
# - YAML
type: YAML
# Where the data will be saved
directory: 'data'

# MySQL settings
mysql:
# Address and port. Can specify as "host:port" or "host".
# Default port for MySQL and MariaDB is 3306.
# Default port for MySQL is 3306.
address: 'localhost'

# Database to create tables in
Expand All @@ -144,3 +148,27 @@ storage:
tables:
vault: 'endervaults_vault'
vault-metadata: 'endervaults_vault_metadata'

# MariaDB settings
mariadb:
# Address and port. Can specify as "host:port" or "host".
# Default port for MariaDB is 3306.
address: 'localhost'

# Database to create tables in
database: 'minecraft'

# Credentials
user: 'minecraft'
password: '123'

# Connection properties
properties:
cachePrepStmts: 'true'
prepStmtCacheSize: '250'
prepStmtCacheSqlLimit: '2048'

# Table names
tables:
vault: 'endervaults_vault'
vault-metadata: 'endervaults_vault_metadata'

0 comments on commit 0c11c52

Please sign in to comment.