From f828aa0c0a5d1ae9064a27fb8d4c17053eb1b856 Mon Sep 17 00:00:00 2001 From: GeorgH93 Date: Sat, 18 May 2019 15:50:15 +0200 Subject: [PATCH] Use connection provider classes from PluginLib --- pom.xml | 3 +- .../Minepacks/Bukkit/Database/Config.java | 24 +++++--- .../Minepacks/Bukkit/Database/Database.java | 43 +++++++------- .../Database/Migration/MigrationManager.java | 6 +- .../Database/Migration/ToSQLMigration.java | 18 +++--- .../Minepacks/Bukkit/Database/MySQL.java | 21 ++----- .../Bukkit/Database/MySQLShared.java | 51 ----------------- .../Minepacks/Bukkit/Database/SQL.java | 20 ++----- .../Minepacks/Bukkit/Database/SQLite.java | 29 ++-------- .../Bukkit/Database/SQLiteShared.java | 57 ------------------- 10 files changed, 67 insertions(+), 205 deletions(-) delete mode 100644 src/at/pcgamingfreaks/Minepacks/Bukkit/Database/MySQLShared.java delete mode 100644 src/at/pcgamingfreaks/Minepacks/Bukkit/Database/SQLiteShared.java diff --git a/pom.xml b/pom.xml index 2511fe4..c886987 100644 --- a/pom.xml +++ b/pom.xml @@ -75,7 +75,8 @@ at.pcgamingfreaks PluginLib - 1.0.8-SNAPSHOT + 1.0.10-SNAPSHOT + provided diff --git a/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/Config.java b/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/Config.java index 9268d81..39dc356 100644 --- a/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/Config.java +++ b/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/Config.java @@ -20,6 +20,7 @@ import at.pcgamingfreaks.Bukkit.Configuration; import at.pcgamingfreaks.Bukkit.MinecraftMaterial; import at.pcgamingfreaks.ConsoleColor; +import at.pcgamingfreaks.Database.DatabaseConnectionConfiguration; import at.pcgamingfreaks.Minepacks.Bukkit.Database.Helper.OldFileUpdater; import at.pcgamingfreaks.Minepacks.Bukkit.Database.Helper.WorldBlacklistMode; import at.pcgamingfreaks.YamlFileManager; @@ -36,7 +37,7 @@ import java.util.List; @SuppressWarnings("ConstantConditions") -public class Config extends Configuration +public class Config extends Configuration implements DatabaseConnectionConfiguration { private static final int CONFIG_VERSION = 21, UPGRADE_THRESHOLD = 21; @@ -91,36 +92,41 @@ public void setDatabaseType(String type) } } - public String getSQLHost() + @Override + public @NotNull String getSQLHost() { return getConfig().getString("Database.SQL.Host", "localhost"); } - public String getSQLDatabase() + @Override + public @NotNull String getSQLDatabase() { return getConfig().getString("Database.SQL.Database", "minecraft"); } - public String getSQLUser() + @Override + public @NotNull String getSQLUser() { return getConfig().getString("Database.SQL.User", "minecraft"); } - public String getSQLPassword() + @Override + public @NotNull String getSQLPassword() { return getConfig().getString("Database.SQL.Password", ""); } + @Override public int getSQLMaxConnections() { return getConfig().getInt("Database.SQL.MaxConnections", 2); } - public String getSQLProperties() + @Override + public @NotNull String getSQLConnectionProperties() { - List list = getConfig().getStringList("Database.MySQL.Properties", null); - StringBuilder str = new StringBuilder(); - if(list == null) return ""; + List list = getConfig().getStringList("Database.SQL.Properties", new LinkedList<>()); + StringBuilder str = new StringBuilder("?allowMultiQueries=true&autoReconnect=true"); for(String s : list) { str.append("&").append(s); diff --git a/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/Database.java b/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/Database.java index eb1d70d..81e3c2b 100644 --- a/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/Database.java +++ b/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/Database.java @@ -18,6 +18,7 @@ package at.pcgamingfreaks.Minepacks.Bukkit.Database; import at.pcgamingfreaks.ConsoleColor; +import at.pcgamingfreaks.Database.ConnectionProvider.ConnectionProvider; import at.pcgamingfreaks.Minepacks.Bukkit.API.Callback; import at.pcgamingfreaks.Minepacks.Bukkit.Backpack; import at.pcgamingfreaks.Minepacks.Bukkit.Database.UnCacheStrategies.OnDisconnect; @@ -46,6 +47,7 @@ public abstract class Database implements Listener { protected static final String START_UUID_UPDATE = "Start updating database to UUIDs ...", UUIDS_UPDATED = "Updated %d accounts to UUIDs."; + public static final String MESSAGE_UNKNOWN_DB_TYPE = ConsoleColor.RED + "Unknown database type \"%s\"!" + ConsoleColor.RESET; protected final Minepacks plugin; protected final InventorySerializer itsSerializer; @@ -84,34 +86,29 @@ public void close() public static Database getDatabase(Minepacks plugin) { - Database database; - switch(plugin.getConfiguration().getDatabaseType().toLowerCase()) + String dbType = plugin.getConfiguration().getDatabaseType().toLowerCase(); + ConnectionProvider connectionProvider = null; + if(dbType.equals("shared") || dbType.equals("external") || dbType.equals("global")) { - case "mysql": - database = new MySQL(plugin); break; + DatabaseConnectionPool pool = PluginLib.getInstance().getDatabaseConnectionPool(); + if(pool == null) + { + plugin.getLogger().warning(ConsoleColor.RED + "The shared connection pool is not initialized correctly!" + ConsoleColor.RESET); + return null; + } + dbType = pool.getDatabaseType().toLowerCase(); + connectionProvider = pool.getConnectionProvider(); + } + Database database; + switch(dbType) + { + case "mysql": database = new MySQL(plugin, connectionProvider); break; + case "sqlite": database = new SQLite(plugin, connectionProvider); break; case "flat": case "file": case "files": database = new Files(plugin); break; - case "external": - case "global": - case "shared": - DatabaseConnectionPool pool = PluginLib.getInstance().getDatabaseConnectionPool(); - if(pool == null) - { - plugin.getLogger().warning(ConsoleColor.RED + "The shared connection pool is not initialized correctly!" + ConsoleColor.RESET); - return null; - } - switch(pool.getDatabaseType().toLowerCase()) - { - case "mysql": database = new MySQLShared(plugin, pool); break; - case "sqlite": database = new SQLiteShared(plugin, pool); break; - default: plugin.getLogger().warning(ConsoleColor.RED + "The database type of the shared pool is currently not supported!" + ConsoleColor.RESET); return null; - } - break; - case "sqlite": - default: - database = new SQLite(plugin); + default: plugin.getLogger().warning(String.format(MESSAGE_UNKNOWN_DB_TYPE, plugin.getConfiguration().getDatabaseType())); return null; } database.init(); return database; diff --git a/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/Migration/MigrationManager.java b/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/Migration/MigrationManager.java index ae99c93..dfc20ab 100644 --- a/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/Migration/MigrationManager.java +++ b/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/Migration/MigrationManager.java @@ -127,14 +127,14 @@ public Migration getMigrationPerformer(String targetDatabaseType) if(!(plugin.getDatabase() instanceof SQL)) return null; return new SQLtoFilesMigration(plugin, (SQL) plugin.getDatabase()); case "mysql": - if(global && plugin.getDatabase() instanceof MySQLShared || !global && plugin.getDatabase() instanceof MySQL) return null; + if(plugin.getDatabase() instanceof MySQL) return null; if(plugin.getDatabase() instanceof SQL) return new SQLtoSQLMigration(plugin, (SQL) plugin.getDatabase(), "mysql", global); else return new FilesToSQLMigration(plugin, (Files) plugin.getDatabase(), "mysql", global); case "sqlite": - default: - if(global && plugin.getDatabase() instanceof SQLiteShared || !global && plugin.getDatabase() instanceof SQLite) return null; + if(plugin.getDatabase() instanceof SQLite) return null; if(plugin.getDatabase() instanceof SQL) return new SQLtoSQLMigration(plugin, (SQL) plugin.getDatabase(), "sqlite", global); else return new FilesToSQLMigration(plugin, (Files) plugin.getDatabase(), "sqlite", global); + default: plugin.getLogger().warning(String.format(Database.MESSAGE_UNKNOWN_DB_TYPE, plugin.getConfiguration().getDatabaseType())); return null; } } catch(Exception e) diff --git a/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/Migration/ToSQLMigration.java b/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/Migration/ToSQLMigration.java index dbb9323..cbeb294 100644 --- a/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/Migration/ToSQLMigration.java +++ b/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/Migration/ToSQLMigration.java @@ -17,10 +17,13 @@ package at.pcgamingfreaks.Minepacks.Bukkit.Database.Migration; -import at.pcgamingfreaks.Minepacks.Bukkit.Database.*; +import at.pcgamingfreaks.Database.ConnectionProvider.ConnectionProvider; +import at.pcgamingfreaks.Minepacks.Bukkit.Database.Database; +import at.pcgamingfreaks.Minepacks.Bukkit.Database.MySQL; +import at.pcgamingfreaks.Minepacks.Bukkit.Database.SQL; +import at.pcgamingfreaks.Minepacks.Bukkit.Database.SQLite; import at.pcgamingfreaks.Minepacks.Bukkit.Minepacks; import at.pcgamingfreaks.PluginLib.Bukkit.PluginLib; -import at.pcgamingfreaks.PluginLib.Database.DatabaseConnectionPool; import at.pcgamingfreaks.Reflection; import org.intellij.lang.annotations.Language; @@ -50,13 +53,12 @@ public abstract class ToSQLMigration extends Migration protected ToSQLMigration(@NotNull Minepacks plugin, @NotNull Database oldDb, @NotNull String dbType, boolean global) throws Exception { super(plugin, oldDb); - if(global) + ConnectionProvider connectionProvider = (global) ? PluginLib.getInstance().getDatabaseConnectionPool().getConnectionProvider() : null; + switch(dbType) { - newDb = (SQL) Reflection.getConstructor((dbType.equals("mysql")) ? MySQLShared.class : SQLiteShared.class, Minepacks.class, DatabaseConnectionPool.class).newInstance(plugin, PluginLib.getInstance().getDatabaseConnectionPool()); - } - else - { - newDb = (SQL) Reflection.getConstructor((dbType.equals("mysql")) ? MySQL.class : SQLite.class, Minepacks.class).newInstance(plugin); + case "mysql": newDb = new MySQL(plugin, connectionProvider); break; + case "sqlite": newDb = new SQLite(plugin, connectionProvider); break; + default: newDb = null; } uuid = plugin.getConfiguration().getUseUUIDs(); } diff --git a/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/MySQL.java b/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/MySQL.java index ce9172b..8058333 100644 --- a/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/MySQL.java +++ b/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/MySQL.java @@ -17,10 +17,13 @@ package at.pcgamingfreaks.Minepacks.Bukkit.Database; +import at.pcgamingfreaks.Database.ConnectionProvider.ConnectionProvider; +import at.pcgamingfreaks.Database.ConnectionProvider.MySQLConnectionProvider; import at.pcgamingfreaks.Database.DBTools; import at.pcgamingfreaks.Minepacks.Bukkit.Minepacks; -import com.zaxxer.hikari.HikariConfig; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.sql.Connection; import java.sql.SQLException; @@ -28,21 +31,9 @@ public class MySQL extends SQL { //TODO add cooldown sync table - public MySQL(Minepacks plugin) + public MySQL(@NotNull Minepacks plugin, @Nullable ConnectionProvider connectionProvider) { - super(plugin); // Load Settings - } - - @Override - protected HikariConfig getPoolConfig() - { - HikariConfig poolConfig = new HikariConfig(); - poolConfig.setJdbcUrl("jdbc:mysql://" + plugin.getConfiguration().getSQLHost() + "/" + plugin.getConfiguration().getSQLDatabase() + "?allowMultiQueries=true&autoReconnect=true" + plugin.getConfiguration().getSQLProperties()); - poolConfig.setUsername(plugin.getConfiguration().getSQLUser()); - poolConfig.setPassword(plugin.getConfiguration().getSQLPassword()); - poolConfig.setMinimumIdle(1); - poolConfig.setMaximumPoolSize(plugin.getConfiguration().getSQLMaxConnections()); - return poolConfig; + super(plugin, (connectionProvider == null) ? new MySQLConnectionProvider(plugin.getLogger(), plugin.getDescription().getName(), plugin.getConfiguration()) : connectionProvider); } @Override diff --git a/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/MySQLShared.java b/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/MySQLShared.java deleted file mode 100644 index 99996d8..0000000 --- a/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/MySQLShared.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright (C) 2018 GeorgH93 - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package at.pcgamingfreaks.Minepacks.Bukkit.Database; - -import at.pcgamingfreaks.Minepacks.Bukkit.Minepacks; -import at.pcgamingfreaks.PluginLib.Database.DatabaseConnectionPool; - -import com.zaxxer.hikari.HikariConfig; - -import org.jetbrains.annotations.NotNull; - -import java.sql.Connection; -import java.sql.SQLException; - -public class MySQLShared extends MySQL -{ - private DatabaseConnectionPool pool; - - protected MySQLShared(Minepacks plugin, DatabaseConnectionPool pool) - { - super(plugin); - this.pool = pool; - } - - @Override - protected HikariConfig getPoolConfig() - { - return null; - } - - @Override - public @NotNull Connection getConnection() throws SQLException - { - return pool.getConnection(); - } -} \ No newline at end of file diff --git a/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/SQL.java b/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/SQL.java index 256f10c..2f05cfb 100644 --- a/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/SQL.java +++ b/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/SQL.java @@ -17,6 +17,7 @@ package at.pcgamingfreaks.Minepacks.Bukkit.Database; +import at.pcgamingfreaks.Database.ConnectionProvider.ConnectionProvider; import at.pcgamingfreaks.Database.DBTools; import at.pcgamingfreaks.Minepacks.Bukkit.API.Callback; import at.pcgamingfreaks.Minepacks.Bukkit.Backpack; @@ -24,15 +25,12 @@ import at.pcgamingfreaks.UUIDConverter; import at.pcgamingfreaks.Utils; -import com.zaxxer.hikari.HikariConfig; -import com.zaxxer.hikari.HikariDataSource; - import org.bukkit.Bukkit; import org.bukkit.OfflinePlayer; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; import org.intellij.lang.annotations.Language; -import org.jetbrains.annotations.Nullable; +import org.jetbrains.annotations.NotNull; import java.sql.*; import java.util.HashMap; @@ -42,7 +40,7 @@ public abstract class SQL extends Database { - private HikariDataSource dataSource; + private ConnectionProvider dataSource; protected String tablePlayers, tableBackpacks, tableCooldowns; // Table Names protected String fieldPlayerName, fieldPlayerID, fieldPlayerUUID, fieldBpOwner, fieldBpIts, fieldBpVersion, fieldBpLastUpdate, fieldCdPlayer, fieldCdTime; // Table Fields @@ -50,17 +48,11 @@ public abstract class SQL extends Database @Language("SQL") protected String queryDeleteOldCooldowns, querySyncCooldown, queryGetCooldown; // DB Querys protected boolean updatePlayer, syncCooldown; - public SQL(Minepacks plugin) + public SQL(@NotNull Minepacks plugin, @NotNull ConnectionProvider connectionProvider) { super(plugin); - HikariConfig poolConfig = getPoolConfig(); - if(poolConfig != null) - { - poolConfig.setPoolName("Minepacks-Connection-Pool"); - poolConfig.addDataSourceProperty("cachePrepStmts", "true"); - dataSource = new HikariDataSource(poolConfig); - } + dataSource = connectionProvider; loadSettings(); buildQuerys(); @@ -96,8 +88,6 @@ public SQL(Minepacks plugin) } } - protected abstract @Nullable HikariConfig getPoolConfig(); - protected void loadSettings() { // Load table and field names diff --git a/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/SQLite.java b/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/SQLite.java index 2dddf2d..8d29309 100644 --- a/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/SQLite.java +++ b/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/SQLite.java @@ -17,12 +17,14 @@ package at.pcgamingfreaks.Minepacks.Bukkit.Database; +import at.pcgamingfreaks.Database.ConnectionProvider.ConnectionProvider; +import at.pcgamingfreaks.Database.ConnectionProvider.SQLiteConnectionProvider; import at.pcgamingfreaks.Minepacks.Bukkit.Minepacks; -import com.zaxxer.hikari.HikariConfig; - import org.bukkit.Bukkit; import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.io.File; import java.sql.Connection; @@ -33,9 +35,9 @@ public class SQLite extends SQL { //TODO add cooldown sync table - public SQLite(Minepacks plugin) + public SQLite(@NotNull Minepacks plugin, @Nullable ConnectionProvider connectionProvider) { - super(plugin); + super(plugin, (connectionProvider == null) ? new SQLiteConnectionProvider(plugin.getLogger(), plugin.getDescription().getName(), plugin.getDataFolder().getAbsolutePath() + File.separator + "backpack.db") : connectionProvider); } @Override @@ -62,25 +64,6 @@ protected void loadSettings() syncCooldown = false; } - @Override - protected HikariConfig getPoolConfig() - { - try - { - Class.forName("org.sqlite.JDBC"); - } - catch(ClassNotFoundException e) - { - e.printStackTrace(); - return null; - } - HikariConfig poolConfig = new HikariConfig(); - poolConfig.setMaximumPoolSize(1); - poolConfig.setJdbcUrl("jdbc:sqlite:" + plugin.getDataFolder().getAbsolutePath() + File.separator + "backpack.db"); - poolConfig.setConnectionTestQuery("SELECT 1;"); - return poolConfig; - } - @Override protected void updateQuerysForDialect() { diff --git a/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/SQLiteShared.java b/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/SQLiteShared.java deleted file mode 100644 index ae27566..0000000 --- a/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/SQLiteShared.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Copyright (C) 2018 GeorgH93 - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package at.pcgamingfreaks.Minepacks.Bukkit.Database; - -import at.pcgamingfreaks.Minepacks.Bukkit.Minepacks; -import at.pcgamingfreaks.PluginLib.Database.DatabaseConnectionPool; - -import com.zaxxer.hikari.HikariConfig; - -import org.jetbrains.annotations.NotNull; - -import java.sql.Connection; -import java.sql.SQLException; -import java.sql.Statement; - -public class SQLiteShared extends SQLite -{ - private DatabaseConnectionPool pool; - - protected SQLiteShared(Minepacks plugin, DatabaseConnectionPool pool) - { - super(plugin); - this.pool = pool; - } - - @Override - protected HikariConfig getPoolConfig() - { - return null; - } - - @Override - public @NotNull Connection getConnection() throws SQLException - { - Connection connection = pool.getConnection(); - try(Statement statement = connection.createStatement()) - { - statement.execute("PRAGMA foreign_keys = ON"); // We need foreign keys! - } - return connection; - } -} \ No newline at end of file