Use connection provider classes from PluginLib

This commit is contained in:
GeorgH93 2019-05-18 15:50:15 +02:00
parent 6aca1cbbb5
commit f828aa0c0a
No known key found for this signature in database
GPG Key ID: D1630D37F9E4B3C8
10 changed files with 67 additions and 205 deletions

View File

@ -75,7 +75,8 @@
<dependency>
<groupId>at.pcgamingfreaks</groupId>
<artifactId>PluginLib</artifactId>
<version>1.0.8-SNAPSHOT</version>
<version>1.0.10-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>

View File

@ -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<String> list = getConfig().getStringList("Database.MySQL.Properties", null);
StringBuilder str = new StringBuilder();
if(list == null) return "";
List<String> list = getConfig().getStringList("Database.SQL.Properties", new LinkedList<>());
StringBuilder str = new StringBuilder("?allowMultiQueries=true&autoReconnect=true");
for(String s : list)
{
str.append("&").append(s);

View File

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

View File

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

View File

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

View File

@ -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

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
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();
}
}

View File

@ -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

View File

@ -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()
{

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
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;
}
}