Add option to handle database connections from the PCGF PluginLib

This commit is contained in:
GeorgH93 2018-05-22 13:55:39 +02:00
parent 63b97cc201
commit 74d2a547be
No known key found for this signature in database
GPG Key ID: D1630D37F9E4B3C8
6 changed files with 140 additions and 10 deletions

View File

@ -2,7 +2,9 @@
<project version="4">
<component name="SqlDialectMappings">
<file url="file://$PROJECT_DIR$/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/MySQL.java" dialect="MySQL" />
<file url="file://$PROJECT_DIR$/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/MySQLShared.java" dialect="MySQL" />
<file url="file://$PROJECT_DIR$/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/SQL.java" dialect="GenericSQL" />
<file url="file://$PROJECT_DIR$/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/SQLite.java" dialect="SQLite" />
<file url="file://$PROJECT_DIR$/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/SQLiteShared.java" dialect="SQLite" />
</component>
</project>

View File

@ -6,13 +6,13 @@
<version>2.0-BETA-SNAPSHOT</version>
<scm>
<connection>scm:git:git@github.com:GeorgH93/Bukkit_Minepacks.git</connection>
<developerConnection>scm:git:git@github.com:GeorgH93/Bukkit_Minepacks.git</developerConnection>
<url>git@github.com:GeorgH93/Bukkit_Minepacks.git</url>
<connection>scm:git:git@github.com:GeorgH93/Minepacks.git</connection>
<developerConnection>scm:git:git@github.com:GeorgH93/Minepacks.git</developerConnection>
<url>git@github.com:GeorgH93/Minepacks.git</url>
</scm>
<issueManagement>
<system>GitHub</system>
<url>https://github.com/GeorgH93/Bukkit_Minepacks/issues</url>
<url>https://github.com/GeorgH93/Minepacks/issues</url>
</issueManagement>
<ciManagement>
<system>jenkins</system>

View File

@ -29,7 +29,7 @@ AllowedGameModes: [ "SURVIVAL" ]
# Defines how long a player have to wait till he can reopen his backpack.
# Time is in seconds. Values < 1 disable the cooldown.
CommandCooldown: -1
# If enabled whe cooldown will be synced between servers (bungeecord network). It will also prevent players from leaving and joining to bypass the cooldown.
# If enabled whe cooldown will be synced between servers (BungeeCord network). It will also prevent players from leaving and joining to bypass the cooldown.
SyncCooldown: false
# Controls for the auto pickup on full inventory function

View File

@ -17,11 +17,14 @@
package at.pcgamingfreaks.Minepacks.Bukkit.Database;
import at.pcgamingfreaks.ConsoleColor;
import at.pcgamingfreaks.Minepacks.Bukkit.API.Callback;
import at.pcgamingfreaks.Minepacks.Bukkit.Backpack;
import at.pcgamingfreaks.Minepacks.Bukkit.Database.UnCacheStrategies.OnDisconnect;
import at.pcgamingfreaks.Minepacks.Bukkit.Database.UnCacheStrategies.UnCacheStrategie;
import at.pcgamingfreaks.Minepacks.Bukkit.Minepacks;
import at.pcgamingfreaks.PluginLib.Bukkit.PluginLib;
import at.pcgamingfreaks.PluginLib.Database.DatabaseConnectionPool;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
@ -69,20 +72,36 @@ public abstract class Database implements Listener
unCacheStrategie.close();
}
public static Database getDatabase(Minepacks Plugin)
public static Database getDatabase(Minepacks plugin)
{
Database database;
switch(Plugin.config.getDatabaseType().toLowerCase())
switch(plugin.config.getDatabaseType().toLowerCase())
{
case "mysql":
database = new MySQL(Plugin); break;
database = new MySQL(plugin); break;
case "flat":
case "file":
case "files":
database = new Files(Plugin); break;
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);
database = new SQLite(plugin);
}
database.init();
return database;

View File

@ -0,0 +1,52 @@
/*
* 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 minepacks, DatabaseConnectionPool pool)
{
super(minepacks);
this.pool = pool;
}
@Override
protected HikariConfig getPoolConfig()
{
return null;
}
@Override
@NotNull
public Connection getConnection() throws SQLException
{
return pool.getConnection();
}
}

View File

@ -0,0 +1,57 @@
/*
* 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 minepacks, DatabaseConnectionPool pool)
{
super(minepacks);
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;
}
}