This commit is contained in:
Phillipp Glanz 2024-04-14 08:37:18 +00:00 committed by GitHub
commit 1dbac5de13
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 168 additions and 233 deletions

View File

@ -45,11 +45,6 @@ public class SQLiteUUIDService implements UUIDService, Consumer<List<UUIDMapping
public SQLiteUUIDService(final String fileName) { public SQLiteUUIDService(final String fileName) {
this.sqlite = this.sqlite =
new SQLite(FileUtils.getFile(PlotSquared.platform().getDirectory(), fileName)); new SQLite(FileUtils.getFile(PlotSquared.platform().getDirectory(), fileName));
try {
this.sqlite.openConnection();
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
try (PreparedStatement stmt = getConnection().prepareStatement( try (PreparedStatement stmt = getConnection().prepareStatement(
"CREATE TABLE IF NOT EXISTS `usercache` (uuid VARCHAR(32) NOT NULL, username VARCHAR(32) NOT NULL, PRIMARY KEY (uuid))")) { "CREATE TABLE IF NOT EXISTS `usercache` (uuid VARCHAR(32) NOT NULL, username VARCHAR(32) NOT NULL, PRIMARY KEY (uuid))")) {

View File

@ -44,6 +44,9 @@ dependencies {
api(libs.arkitektonika) api(libs.arkitektonika)
api(libs.paster) api(libs.paster)
api(libs.informativeAnnotations) api(libs.informativeAnnotations)
// Database
implementation(libs.hikaricp)
} }
tasks.processResources { tasks.processResources {

View File

@ -31,25 +31,6 @@ import java.sql.Statement;
*/ */
public abstract class Database { public abstract class Database {
public abstract Connection forceConnection() throws SQLException, ClassNotFoundException;
/**
* Opens a connection with the database.
*
* @return Opened connection
* @throws SQLException if the connection can not be opened
* @throws ClassNotFoundException if the driver cannot be found
*/
public abstract Connection openConnection() throws SQLException, ClassNotFoundException;
/**
* Checks if a connection is open with the database.
*
* @return {@code true} if the connection is open
* @throws SQLException if the connection cannot be checked
*/
public abstract boolean checkConnection() throws SQLException;
/** /**
* Gets the connection with the database. * Gets the connection with the database.
* *
@ -72,7 +53,6 @@ public abstract class Database {
* @param query Query to be run * @param query Query to be run
* @return the results of the query * @return the results of the query
* @throws SQLException If the query cannot be executed * @throws SQLException If the query cannot be executed
* @throws ClassNotFoundException If the driver cannot be found; see {@link #openConnection()}
*/ */
public abstract ResultSet querySQL(String query) throws SQLException, ClassNotFoundException; public abstract ResultSet querySQL(String query) throws SQLException, ClassNotFoundException;
@ -84,7 +64,6 @@ public abstract class Database {
* @param query Query to be run * @param query Query to be run
* @return Result Code, see {@link Statement#executeUpdate(String)} * @return Result Code, see {@link Statement#executeUpdate(String)}
* @throws SQLException If the query cannot be executed * @throws SQLException If the query cannot be executed
* @throws ClassNotFoundException If the driver cannot be found; see {@link #openConnection()}
*/ */
public abstract int updateSQL(String query) throws SQLException, ClassNotFoundException; public abstract int updateSQL(String query) throws SQLException, ClassNotFoundException;

View File

@ -19,10 +19,10 @@
package com.plotsquared.core.database; package com.plotsquared.core.database;
import com.plotsquared.core.configuration.Storage; import com.plotsquared.core.configuration.Storage;
import com.plotsquared.core.util.StringMan; import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Statement; import java.sql.Statement;
@ -35,12 +35,7 @@ import java.sql.Statement;
*/ */
public class MySQL extends Database { public class MySQL extends Database {
private final String user; private final HikariDataSource hikariDataSource;
private final String database;
private final String password;
private final String port;
private final String hostname;
private Connection connection;
/** /**
* Creates a new MySQL instance. * Creates a new MySQL instance.
@ -52,66 +47,52 @@ public class MySQL extends Database {
* @param password Password * @param password Password
*/ */
public MySQL(String hostname, String port, String database, String username, String password) { public MySQL(String hostname, String port, String database, String username, String password) {
this.hostname = hostname; HikariConfig config = new HikariConfig();
this.port = port; config.setJdbcUrl("jdbc:mysql://" + hostname + ':' + port + '/' + database);
this.database = database; config.setUsername(username);
this.user = username; config.setPassword(password);
this.password = password; for (final String property : Storage.MySQL.PROPERTIES) {
this.connection = null; if (property.contains("=")) {
} String[] splittedProperty = property.split("=");
if (splittedProperty.length != 2) {
@Override continue;
public Connection forceConnection() throws SQLException { }
this.connection = DriverManager.getConnection( String key = splittedProperty[0];
"jdbc:mysql://" + this.hostname + ':' + this.port + '/' + this.database + "?" String value = splittedProperty[1];
+ StringMan.join(Storage.MySQL.PROPERTIES, "&"), this.user, this.password); config.addDataSourceProperty(key,value);
return this.connection; }
}
@Override
public Connection openConnection() throws SQLException {
if (checkConnection()) {
return this.connection;
} }
return forceConnection(); this.hikariDataSource = new HikariDataSource(config);
}
@Override
public boolean checkConnection() throws SQLException {
return (this.connection != null) && !this.connection.isClosed();
} }
@Override @Override
public Connection getConnection() { public Connection getConnection() {
return this.connection; try {
return this.hikariDataSource.getConnection();
} catch (SQLException e) {
throw new RuntimeException(e);
}
} }
@Override @Override
public boolean closeConnection() throws SQLException { public boolean closeConnection() throws SQLException {
if (this.connection == null) { if (this.hikariDataSource == null) {
return false; return false;
} }
this.connection.close(); this.hikariDataSource.close();
this.connection = null;
return true; return true;
} }
@Override @Override
public ResultSet querySQL(String query) throws SQLException { public ResultSet querySQL(String query) throws SQLException {
if (checkConnection()) { try (Statement statement = this.getConnection().createStatement()) {
openConnection();
}
try (Statement statement = this.connection.createStatement()) {
return statement.executeQuery(query); return statement.executeQuery(query);
} }
} }
@Override @Override
public int updateSQL(String query) throws SQLException { public int updateSQL(String query) throws SQLException {
if (checkConnection()) { try (Statement statement = this.getConnection().createStatement()) {
openConnection();
}
try (Statement statement = this.connection.createStatement()) {
return statement.executeUpdate(query); return statement.executeUpdate(query);
} }
} }

View File

@ -154,7 +154,6 @@ public class SQLManager implements AbstractDB {
this.plotListener = plotListener; this.plotListener = plotListener;
this.worldConfiguration = worldConfiguration; this.worldConfiguration = worldConfiguration;
this.database = database; this.database = database;
this.connection = database.openConnection();
final DatabaseMetaData databaseMetaData = this.connection.getMetaData(); final DatabaseMetaData databaseMetaData = this.connection.getMetaData();
this.supportsGetGeneratedKeys = databaseMetaData.supportsGetGeneratedKeys(); this.supportsGetGeneratedKeys = databaseMetaData.supportsGetGeneratedKeys();
this.mySQL = database instanceof MySQL; this.mySQL = database instanceof MySQL;
@ -251,13 +250,13 @@ public class SQLManager implements AbstractDB {
public boolean isValid() { public boolean isValid() {
try { try {
if (connection.isClosed()) { if (getConnection().isClosed()) {
return false; return false;
} }
} catch (SQLException e) { } catch (SQLException e) {
return false; return false;
} }
try (PreparedStatement stmt = this.connection.prepareStatement("SELECT 1")) { try (PreparedStatement stmt = this.getConnection().prepareStatement("SELECT 1")) {
stmt.execute(); stmt.execute();
return true; return true;
} catch (Throwable e) { } catch (Throwable e) {
@ -266,13 +265,8 @@ public class SQLManager implements AbstractDB {
} }
public void reconnect() { public void reconnect() {
try { close();
close(); SQLManager.this.closed = false;
SQLManager.this.closed = false;
SQLManager.this.connection = database.forceConnection();
} catch (SQLException | ClassNotFoundException e) {
e.printStackTrace();
}
} }
public synchronized Queue<Runnable> getGlobalTasks() { public synchronized Queue<Runnable> getGlobalTasks() {
@ -392,8 +386,8 @@ public class SQLManager implements AbstractDB {
public boolean sendBatch() { public boolean sendBatch() {
try { try {
if (!getGlobalTasks().isEmpty()) { if (!getGlobalTasks().isEmpty()) {
if (this.connection.getAutoCommit()) { if (this.getConnection().getAutoCommit()) {
this.connection.setAutoCommit(false); this.getConnection().setAutoCommit(false);
} }
Runnable task = getGlobalTasks().remove(); Runnable task = getGlobalTasks().remove();
if (task != null) { if (task != null) {
@ -414,8 +408,8 @@ public class SQLManager implements AbstractDB {
int count = -1; int count = -1;
if (!this.plotTasks.isEmpty()) { if (!this.plotTasks.isEmpty()) {
count = Math.max(count, 0); count = Math.max(count, 0);
if (this.connection.getAutoCommit()) { if (this.getConnection().getAutoCommit()) {
this.connection.setAutoCommit(false); this.getConnection().setAutoCommit(false);
} }
String method = null; String method = null;
PreparedStatement statement = null; PreparedStatement statement = null;
@ -469,8 +463,8 @@ public class SQLManager implements AbstractDB {
} }
if (!this.playerTasks.isEmpty()) { if (!this.playerTasks.isEmpty()) {
count = Math.max(count, 0); count = Math.max(count, 0);
if (this.connection.getAutoCommit()) { if (this.getConnection().getAutoCommit()) {
this.connection.setAutoCommit(false); this.getConnection().setAutoCommit(false);
} }
String method = null; String method = null;
PreparedStatement statement = null; PreparedStatement statement = null;
@ -514,8 +508,8 @@ public class SQLManager implements AbstractDB {
} }
if (!this.clusterTasks.isEmpty()) { if (!this.clusterTasks.isEmpty()) {
count = Math.max(count, 0); count = Math.max(count, 0);
if (this.connection.getAutoCommit()) { if (this.getConnection().getAutoCommit()) {
this.connection.setAutoCommit(false); this.getConnection().setAutoCommit(false);
} }
String method = null; String method = null;
PreparedStatement statement = null; PreparedStatement statement = null;
@ -563,8 +557,8 @@ public class SQLManager implements AbstractDB {
return true; return true;
} }
if (count != -1) { if (count != -1) {
if (!this.connection.getAutoCommit()) { if (!this.getConnection().getAutoCommit()) {
this.connection.setAutoCommit(true); this.getConnection().setAutoCommit(true);
} }
} }
if (!this.clusterTasks.isEmpty()) { if (!this.clusterTasks.isEmpty()) {
@ -585,7 +579,7 @@ public class SQLManager implements AbstractDB {
} }
public Connection getConnection() { public Connection getConnection() {
return this.connection; return this.database.getConnection();
} }
/** /**
@ -607,7 +601,7 @@ public class SQLManager implements AbstractDB {
@Override @Override
public PreparedStatement get() throws SQLException { public PreparedStatement get() throws SQLException {
return SQLManager.this.connection.prepareStatement(SQLManager.this.SET_OWNER); return SQLManager.this.getConnection().prepareStatement(SQLManager.this.SET_OWNER);
} }
}); });
} }
@ -632,7 +626,7 @@ public class SQLManager implements AbstractDB {
final ArrayList<UUIDPair> denied = new ArrayList<>(); final ArrayList<UUIDPair> denied = new ArrayList<>();
// Populating structures // Populating structures
try (PreparedStatement stmt = SQLManager.this.connection try (PreparedStatement stmt = SQLManager.this.getConnection()
.prepareStatement(SQLManager.this.GET_ALL_PLOTS); .prepareStatement(SQLManager.this.GET_ALL_PLOTS);
ResultSet result = stmt.executeQuery()) { ResultSet result = stmt.executeQuery()) {
while (result.next()) { while (result.next()) {
@ -663,7 +657,7 @@ public class SQLManager implements AbstractDB {
() -> createTiers(trusted, "trusted", () -> createTiers(trusted, "trusted",
() -> createTiers(denied, "denied", () -> { () -> createTiers(denied, "denied", () -> {
try { try {
SQLManager.this.connection.commit(); SQLManager.this.getConnection().commit();
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -677,7 +671,7 @@ public class SQLManager implements AbstractDB {
} catch (SQLException e) { } catch (SQLException e) {
LOGGER.warn("Failed to set all flags and member tiers for plots", e); LOGGER.warn("Failed to set all flags and member tiers for plots", e);
try { try {
SQLManager.this.connection.commit(); SQLManager.this.getConnection().commit();
} catch (SQLException e1) { } catch (SQLException e1) {
e1.printStackTrace(); e1.printStackTrace();
} }
@ -686,7 +680,7 @@ public class SQLManager implements AbstractDB {
} catch (Exception e) { } catch (Exception e) {
LOGGER.warn("Warning! Failed to set all helper for plots", e); LOGGER.warn("Warning! Failed to set all helper for plots", e);
try { try {
SQLManager.this.connection.commit(); SQLManager.this.getConnection().commit();
} catch (SQLException e1) { } catch (SQLException e1) {
e1.printStackTrace(); e1.printStackTrace();
} }
@ -747,7 +741,7 @@ public class SQLManager implements AbstractDB {
} }
public void createFlags(Map<PlotId, Integer> ids, List<Plot> plots, Runnable whenDone) { public void createFlags(Map<PlotId, Integer> ids, List<Plot> plots, Runnable whenDone) {
try (final PreparedStatement preparedStatement = this.connection.prepareStatement( try (final PreparedStatement preparedStatement = this.getConnection().prepareStatement(
"INSERT INTO `" + SQLManager.this.prefix "INSERT INTO `" + SQLManager.this.prefix
+ "plot_flags`(`plot_id`, `flag`, `value`) VALUES(?, ?, ?)")) { + "plot_flags`(`plot_id`, `flag`, `value`) VALUES(?, ?, ?)")) {
for (final Plot plot : plots) { for (final Plot plot : plots) {
@ -872,13 +866,13 @@ public class SQLManager implements AbstractDB {
if (last == -1) { if (last == -1) {
last = subList.size(); last = subList.size();
statement = mod.getCreateMySQL(subList.size()); statement = mod.getCreateMySQL(subList.size());
preparedStmt = this.connection.prepareStatement(statement); preparedStmt = this.getConnection().prepareStatement(statement);
} }
if (subList.size() != last || count % 5000 == 0 && count > 0) { if (subList.size() != last || count % 5000 == 0 && count > 0) {
preparedStmt.executeBatch(); preparedStmt.executeBatch();
preparedStmt.close(); preparedStmt.close();
statement = mod.getCreateMySQL(subList.size()); statement = mod.getCreateMySQL(subList.size());
preparedStmt = this.connection.prepareStatement(statement); preparedStmt = this.getConnection().prepareStatement(statement);
} }
for (int i = 0; i < subList.size(); i++) { for (int i = 0; i < subList.size(); i++) {
count++; count++;
@ -914,13 +908,13 @@ public class SQLManager implements AbstractDB {
if (last == -1) { if (last == -1) {
last = subList.size(); last = subList.size();
statement = mod.getCreateSQLite(subList.size()); statement = mod.getCreateSQLite(subList.size());
preparedStmt = this.connection.prepareStatement(statement); preparedStmt = this.getConnection().prepareStatement(statement);
} }
if (subList.size() != last || count % 5000 == 0 && count > 0) { if (subList.size() != last || count % 5000 == 0 && count > 0) {
preparedStmt.executeBatch(); preparedStmt.executeBatch();
preparedStmt.clearParameters(); preparedStmt.clearParameters();
statement = mod.getCreateSQLite(subList.size()); statement = mod.getCreateSQLite(subList.size());
preparedStmt = this.connection.prepareStatement(statement); preparedStmt = this.getConnection().prepareStatement(statement);
} }
for (int i = 0; i < subList.size(); i++) { for (int i = 0; i < subList.size(); i++) {
count++; count++;
@ -937,7 +931,7 @@ public class SQLManager implements AbstractDB {
e.printStackTrace(); e.printStackTrace();
LOGGER.error("2: | {}", objList.get(0).getClass().getCanonicalName()); LOGGER.error("2: | {}", objList.get(0).getClass().getCanonicalName());
LOGGER.error("Could not bulk save!"); LOGGER.error("Could not bulk save!");
try (PreparedStatement preparedStmt = this.connection try (PreparedStatement preparedStmt = this.getConnection()
.prepareStatement(mod.getCreateSQL())) { .prepareStatement(mod.getCreateSQL())) {
for (T obj : objList) { for (T obj : objList) {
mod.setSQL(preparedStmt, obj); mod.setSQL(preparedStmt, obj);
@ -955,7 +949,7 @@ public class SQLManager implements AbstractDB {
} }
public void createSettings(final ArrayList<LegacySettings> myList, final Runnable whenDone) { public void createSettings(final ArrayList<LegacySettings> myList, final Runnable whenDone) {
try (final PreparedStatement preparedStatement = this.connection.prepareStatement( try (final PreparedStatement preparedStatement = this.getConnection().prepareStatement(
"INSERT INTO `" + SQLManager.this.prefix + "plot_settings`" "INSERT INTO `" + SQLManager.this.prefix + "plot_settings`"
+ "(`plot_plot_id`,`biome`,`rain`,`custom_time`,`time`,`deny_entry`,`alias`,`merged`,`position`) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)")) { + "(`plot_plot_id`,`biome`,`rain`,`custom_time`,`time`,`deny_entry`,`alias`,`merged`,`position`) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)")) {
@ -1083,7 +1077,7 @@ public class SQLManager implements AbstractDB {
@Override @Override
public PreparedStatement get() throws SQLException { public PreparedStatement get() throws SQLException {
return SQLManager.this.connection.prepareStatement( return SQLManager.this.getConnection().prepareStatement(
SQLManager.this.CREATE_PLOT_SAFE, SQLManager.this.CREATE_PLOT_SAFE,
Statement.RETURN_GENERATED_KEYS Statement.RETURN_GENERATED_KEYS
); );
@ -1110,7 +1104,7 @@ public class SQLManager implements AbstractDB {
@Override @Override
public PreparedStatement get() throws SQLException { public PreparedStatement get() throws SQLException {
return SQLManager.this.connection.prepareStatement( return SQLManager.this.getConnection().prepareStatement(
"INSERT INTO `" + SQLManager.this.prefix "INSERT INTO `" + SQLManager.this.prefix
+ "plot_settings`(`plot_plot_id`) VALUES(?)"); + "plot_settings`(`plot_plot_id`) VALUES(?)");
} }
@ -1134,9 +1128,9 @@ public class SQLManager implements AbstractDB {
return; return;
} }
try { try {
if (!this.connection.getAutoCommit()) { if (!this.getConnection().getAutoCommit()) {
this.connection.commit(); this.getConnection().commit();
this.connection.setAutoCommit(true); this.getConnection().setAutoCommit(true);
} }
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
@ -1157,7 +1151,7 @@ public class SQLManager implements AbstractDB {
@Override @Override
public PreparedStatement get() throws SQLException { public PreparedStatement get() throws SQLException {
return SQLManager.this.connection return SQLManager.this.getConnection()
.prepareStatement(SQLManager.this.CREATE_PLOT, Statement.RETURN_GENERATED_KEYS); .prepareStatement(SQLManager.this.CREATE_PLOT, Statement.RETURN_GENERATED_KEYS);
} }
@ -1183,7 +1177,7 @@ public class SQLManager implements AbstractDB {
@Override @Override
public PreparedStatement get() throws SQLException { public PreparedStatement get() throws SQLException {
return SQLManager.this.connection.prepareStatement( return SQLManager.this.getConnection().prepareStatement(
"INSERT INTO `" + SQLManager.this.prefix "INSERT INTO `" + SQLManager.this.prefix
+ "plot_settings`(`plot_plot_id`) VALUES(?)"); + "plot_settings`(`plot_plot_id`) VALUES(?)");
} }
@ -1201,7 +1195,7 @@ public class SQLManager implements AbstractDB {
String[] tables = String[] tables =
new String[]{"plot", "plot_denied", "plot_helpers", "plot_comments", "plot_trusted", new String[]{"plot", "plot_denied", "plot_helpers", "plot_comments", "plot_trusted",
"plot_rating", "plot_settings", "cluster", "player_meta", "plot_flags"}; "plot_rating", "plot_settings", "cluster", "player_meta", "plot_flags"};
DatabaseMetaData meta = this.connection.getMetaData(); DatabaseMetaData meta = this.getConnection().getMetaData();
int create = 0; int create = 0;
for (String s : tables) { for (String s : tables) {
ResultSet set = meta.getTables(null, null, this.prefix + s, new String[]{"TABLE"}); ResultSet set = meta.getTables(null, null, this.prefix + s, new String[]{"TABLE"});
@ -1215,7 +1209,7 @@ public class SQLManager implements AbstractDB {
return; return;
} }
boolean addConstraint = create == tables.length; boolean addConstraint = create == tables.length;
try (Statement stmt = this.connection.createStatement()) { try (Statement stmt = this.getConnection().createStatement()) {
if (this.mySQL) { if (this.mySQL) {
stmt.addBatch("CREATE TABLE IF NOT EXISTS `" + this.prefix + "plot` (" stmt.addBatch("CREATE TABLE IF NOT EXISTS `" + this.prefix + "plot` ("
+ "`id` INT(11) NOT NULL AUTO_INCREMENT," + "`plot_id_x` INT(11) NOT NULL," + "`id` INT(11) NOT NULL AUTO_INCREMENT," + "`plot_id_x` INT(11) NOT NULL,"
@ -1361,7 +1355,7 @@ public class SQLManager implements AbstractDB {
@Override @Override
public PreparedStatement get() throws SQLException { public PreparedStatement get() throws SQLException {
return SQLManager.this.connection.prepareStatement( return SQLManager.this.getConnection().prepareStatement(
"DELETE FROM `" + SQLManager.this.prefix "DELETE FROM `" + SQLManager.this.prefix
+ "plot_settings` WHERE `plot_plot_id` = ?"); + "plot_settings` WHERE `plot_plot_id` = ?");
} }
@ -1381,7 +1375,7 @@ public class SQLManager implements AbstractDB {
@Override @Override
public PreparedStatement get() throws SQLException { public PreparedStatement get() throws SQLException {
return SQLManager.this.connection.prepareStatement( return SQLManager.this.getConnection().prepareStatement(
"DELETE FROM `" + SQLManager.this.prefix "DELETE FROM `" + SQLManager.this.prefix
+ "plot_helpers` WHERE `plot_plot_id` = ?"); + "plot_helpers` WHERE `plot_plot_id` = ?");
} }
@ -1401,7 +1395,7 @@ public class SQLManager implements AbstractDB {
@Override @Override
public PreparedStatement get() throws SQLException { public PreparedStatement get() throws SQLException {
return SQLManager.this.connection.prepareStatement( return SQLManager.this.getConnection().prepareStatement(
"DELETE FROM `" + SQLManager.this.prefix "DELETE FROM `" + SQLManager.this.prefix
+ "plot_trusted` WHERE `plot_plot_id` = ?"); + "plot_trusted` WHERE `plot_plot_id` = ?");
} }
@ -1421,7 +1415,7 @@ public class SQLManager implements AbstractDB {
@Override @Override
public PreparedStatement get() throws SQLException { public PreparedStatement get() throws SQLException {
return SQLManager.this.connection.prepareStatement( return SQLManager.this.getConnection().prepareStatement(
"DELETE FROM `" + SQLManager.this.prefix "DELETE FROM `" + SQLManager.this.prefix
+ "plot_denied` WHERE `plot_plot_id` = ?"); + "plot_denied` WHERE `plot_plot_id` = ?");
} }
@ -1439,7 +1433,7 @@ public class SQLManager implements AbstractDB {
@Override @Override
public PreparedStatement get() throws SQLException { public PreparedStatement get() throws SQLException {
return SQLManager.this.connection.prepareStatement( return SQLManager.this.getConnection().prepareStatement(
"DELETE FROM `" + SQLManager.this.prefix "DELETE FROM `" + SQLManager.this.prefix
+ "plot_comments` WHERE `world` = ? AND `hashcode` = ?"); + "plot_comments` WHERE `world` = ? AND `hashcode` = ?");
} }
@ -1459,7 +1453,7 @@ public class SQLManager implements AbstractDB {
@Override @Override
public PreparedStatement get() throws SQLException { public PreparedStatement get() throws SQLException {
return SQLManager.this.connection.prepareStatement( return SQLManager.this.getConnection().prepareStatement(
"DELETE FROM `" + SQLManager.this.prefix "DELETE FROM `" + SQLManager.this.prefix
+ "plot_rating` WHERE `plot_plot_id` = ?"); + "plot_rating` WHERE `plot_plot_id` = ?");
} }
@ -1487,7 +1481,7 @@ public class SQLManager implements AbstractDB {
@Override @Override
public PreparedStatement get() throws SQLException { public PreparedStatement get() throws SQLException {
return SQLManager.this.connection.prepareStatement( return SQLManager.this.getConnection().prepareStatement(
"DELETE FROM `" + SQLManager.this.prefix + "plot` WHERE `id` = ?"); "DELETE FROM `" + SQLManager.this.prefix + "plot` WHERE `id` = ?");
} }
}); });
@ -1509,7 +1503,7 @@ public class SQLManager implements AbstractDB {
@Override @Override
public PreparedStatement get() throws SQLException { public PreparedStatement get() throws SQLException {
return SQLManager.this.connection.prepareStatement( return SQLManager.this.getConnection().prepareStatement(
"INSERT INTO `" + SQLManager.this.prefix "INSERT INTO `" + SQLManager.this.prefix
+ "plot_settings`(`plot_plot_id`) VALUES(?)"); + "plot_settings`(`plot_plot_id`) VALUES(?)");
} }
@ -1527,7 +1521,7 @@ public class SQLManager implements AbstractDB {
return cluster.temp; return cluster.temp;
} }
int c_id; int c_id;
try (PreparedStatement stmt = this.connection.prepareStatement( try (PreparedStatement stmt = this.getConnection().prepareStatement(
"SELECT `id` FROM `" + this.prefix "SELECT `id` FROM `" + this.prefix
+ "cluster` WHERE `pos1_x` = ? AND `pos1_z` = ? AND `pos2_x` = ? AND `pos2_z` = ? AND `world` = ? ORDER BY `timestamp` ASC")) { + "cluster` WHERE `pos1_x` = ? AND `pos1_z` = ? AND `pos2_x` = ? AND `pos2_z` = ? AND `world` = ? ORDER BY `timestamp` ASC")) {
stmt.setInt(1, cluster.getP1().getX()); stmt.setInt(1, cluster.getP1().getX());
@ -1567,7 +1561,7 @@ public class SQLManager implements AbstractDB {
return plot.temp; return plot.temp;
} }
int id; int id;
try (PreparedStatement statement = this.connection.prepareStatement( try (PreparedStatement statement = this.getConnection().prepareStatement(
"SELECT `id` FROM `" + this.prefix "SELECT `id` FROM `" + this.prefix
+ "plot` WHERE `plot_id_x` = ? AND `plot_id_z` = ? AND world = ? ORDER BY `timestamp` ASC")) { + "plot` WHERE `plot_id_x` = ? AND `plot_id_z` = ? AND world = ? ORDER BY `timestamp` ASC")) {
statement.setInt(1, plot.getId().getX()); statement.setInt(1, plot.getId().getX());
@ -1598,13 +1592,13 @@ public class SQLManager implements AbstractDB {
public void updateTables(int[] oldVersion) { public void updateTables(int[] oldVersion) {
try { try {
if (this.mySQL && !PlotSquared.get().checkVersion(oldVersion, 3, 3, 2)) { if (this.mySQL && !PlotSquared.get().checkVersion(oldVersion, 3, 3, 2)) {
try (Statement stmt = this.connection.createStatement()) { try (Statement stmt = this.getConnection().createStatement()) {
stmt.executeUpdate( stmt.executeUpdate(
"ALTER TABLE `" + this.prefix + "plots` DROP INDEX `unique_alias`"); "ALTER TABLE `" + this.prefix + "plots` DROP INDEX `unique_alias`");
} catch (SQLException ignored) { } catch (SQLException ignored) {
} }
} }
DatabaseMetaData data = this.connection.getMetaData(); DatabaseMetaData data = this.getConnection().getMetaData();
ResultSet rs = ResultSet rs =
data.getColumns(null, null, this.prefix + "plot_comments", "plot_plot_id"); data.getColumns(null, null, this.prefix + "plot_comments", "plot_plot_id");
if (rs.next()) { if (rs.next()) {
@ -1612,7 +1606,7 @@ public class SQLManager implements AbstractDB {
rs = data.getColumns(null, null, this.prefix + "plot_comments", "hashcode"); rs = data.getColumns(null, null, this.prefix + "plot_comments", "hashcode");
if (!rs.next()) { if (!rs.next()) {
rs.close(); rs.close();
try (Statement statement = this.connection.createStatement()) { try (Statement statement = this.getConnection().createStatement()) {
statement.addBatch("DROP TABLE `" + this.prefix + "plot_comments`"); statement.addBatch("DROP TABLE `" + this.prefix + "plot_comments`");
if (Storage.MySQL.USE) { if (Storage.MySQL.USE) {
statement.addBatch( statement.addBatch(
@ -1633,7 +1627,7 @@ public class SQLManager implements AbstractDB {
} }
statement.executeBatch(); statement.executeBatch();
} catch (SQLException ignored) { } catch (SQLException ignored) {
try (Statement statement = this.connection.createStatement()) { try (Statement statement = this.getConnection().createStatement()) {
statement.addBatch("ALTER IGNORE TABLE `" + this.prefix statement.addBatch("ALTER IGNORE TABLE `" + this.prefix
+ "plot_comments` ADD `inbox` VARCHAR(11) DEFAULT `public`"); + "plot_comments` ADD `inbox` VARCHAR(11) DEFAULT `public`");
statement.addBatch("ALTER IGNORE TABLE `" + this.prefix statement.addBatch("ALTER IGNORE TABLE `" + this.prefix
@ -1647,7 +1641,7 @@ public class SQLManager implements AbstractDB {
rs.close(); rs.close();
rs = data.getColumns(null, null, this.prefix + "plot_denied", "plot_plot_id"); rs = data.getColumns(null, null, this.prefix + "plot_denied", "plot_plot_id");
if (rs.next()) { if (rs.next()) {
try (Statement statement = this.connection.createStatement()) { try (Statement statement = this.getConnection().createStatement()) {
statement.executeUpdate("DELETE FROM `" + this.prefix statement.executeUpdate("DELETE FROM `" + this.prefix
+ "plot_denied` WHERE `plot_plot_id` NOT IN (SELECT `id` FROM `" + "plot_denied` WHERE `plot_plot_id` NOT IN (SELECT `id` FROM `"
+ this.prefix + "plot`)"); + this.prefix + "plot`)");
@ -1656,7 +1650,7 @@ public class SQLManager implements AbstractDB {
} }
rs.close(); rs.close();
try (Statement statement = this.connection.createStatement()) { try (Statement statement = this.getConnection().createStatement()) {
for (String table : new String[]{"plot_denied", "plot_helpers", for (String table : new String[]{"plot_denied", "plot_helpers",
"plot_trusted"}) { "plot_trusted"}) {
ResultSet result = statement.executeQuery( ResultSet result = statement.executeQuery(
@ -1728,7 +1722,7 @@ public class SQLManager implements AbstractDB {
@Override @Override
public boolean convertFlags() { public boolean convertFlags() {
final Map<Integer, Map<String, String>> flagMap = new HashMap<>(); final Map<Integer, Map<String, String>> flagMap = new HashMap<>();
try (Statement statement = this.connection.createStatement()) { try (Statement statement = this.getConnection().createStatement()) {
try (ResultSet resultSet = statement try (ResultSet resultSet = statement
.executeQuery("SELECT * FROM `" + this.prefix + "plot_settings`")) { .executeQuery("SELECT * FROM `" + this.prefix + "plot_settings`")) {
while (resultSet.next()) { while (resultSet.next()) {
@ -1758,7 +1752,7 @@ public class SQLManager implements AbstractDB {
} }
LOGGER.info("Loaded {} plot flag collections...", flagMap.size()); LOGGER.info("Loaded {} plot flag collections...", flagMap.size());
LOGGER.info("Attempting to store these flags in the new table..."); LOGGER.info("Attempting to store these flags in the new table...");
try (final PreparedStatement preparedStatement = this.connection.prepareStatement( try (final PreparedStatement preparedStatement = this.getConnection().prepareStatement(
"INSERT INTO `" + SQLManager.this.prefix "INSERT INTO `" + SQLManager.this.prefix
+ "plot_flags`(`plot_id`, `flag`, `value`) VALUES(?, ?, ?)")) { + "plot_flags`(`plot_id`, `flag`, `value`) VALUES(?, ?, ?)")) {
@ -1843,7 +1837,7 @@ public class SQLManager implements AbstractDB {
/* /*
* Getting plots * Getting plots
*/ */
try (Statement statement = this.connection.createStatement()) { try (Statement statement = this.getConnection().createStatement()) {
int id; int id;
String o; String o;
UUID user; UUID user;
@ -2177,7 +2171,7 @@ public class SQLManager implements AbstractDB {
@Override @Override
public PreparedStatement get() throws SQLException { public PreparedStatement get() throws SQLException {
return SQLManager.this.connection.prepareStatement( return SQLManager.this.getConnection().prepareStatement(
"UPDATE `" + SQLManager.this.prefix "UPDATE `" + SQLManager.this.prefix
+ "plot_settings` SET `merged` = ? WHERE `plot_plot_id` = ?"); + "plot_settings` SET `merged` = ? WHERE `plot_plot_id` = ?");
} }
@ -2192,7 +2186,7 @@ public class SQLManager implements AbstractDB {
final int id2 = getId(plot2); final int id2 = getId(plot2);
final PlotId pos1 = plot1.getId(); final PlotId pos1 = plot1.getId();
final PlotId pos2 = plot2.getId(); final PlotId pos2 = plot2.getId();
try (final PreparedStatement preparedStatement = this.connection.prepareStatement( try (final PreparedStatement preparedStatement = this.getConnection().prepareStatement(
"UPDATE `" + SQLManager.this.prefix "UPDATE `" + SQLManager.this.prefix
+ "plot` SET `plot_id_x` = ?, `plot_id_z` = ? WHERE `id` = ?")) { + "plot` SET `plot_id_x` = ?, `plot_id_z` = ? WHERE `id` = ?")) {
preparedStatement.setInt(1, pos1.getX()); preparedStatement.setInt(1, pos1.getX());
@ -2227,7 +2221,7 @@ public class SQLManager implements AbstractDB {
@Override @Override
public PreparedStatement get() throws SQLException { public PreparedStatement get() throws SQLException {
return SQLManager.this.connection.prepareStatement( return SQLManager.this.getConnection().prepareStatement(
"UPDATE `" + SQLManager.this.prefix "UPDATE `" + SQLManager.this.prefix
+ "plot` SET `plot_id_x` = ?, `plot_id_z` = ?, `world` = ? WHERE `id` = ?"); + "plot` SET `plot_id_x` = ?, `plot_id_z` = ?, `world` = ? WHERE `id` = ?");
} }
@ -2258,7 +2252,7 @@ public class SQLManager implements AbstractDB {
+ "plot_flags`(`plot_id`, `flag`, `value`) VALUES(?, ?, ?) " + "plot_flags`(`plot_id`, `flag`, `value`) VALUES(?, ?, ?) "
+ "ON CONFLICT(`plot_id`,`flag`) DO UPDATE SET `value` = ?"; + "ON CONFLICT(`plot_id`,`flag`) DO UPDATE SET `value` = ?";
} }
return SQLManager.this.connection.prepareStatement(statement); return SQLManager.this.getConnection().prepareStatement(statement);
} }
}); });
} }
@ -2274,7 +2268,7 @@ public class SQLManager implements AbstractDB {
@Override @Override
public PreparedStatement get() throws SQLException { public PreparedStatement get() throws SQLException {
return SQLManager.this.connection.prepareStatement( return SQLManager.this.getConnection().prepareStatement(
"DELETE FROM `" + SQLManager.this.prefix "DELETE FROM `" + SQLManager.this.prefix
+ "plot_flags` WHERE `plot_id` = ? AND `flag` = ?"); + "plot_flags` WHERE `plot_id` = ? AND `flag` = ?");
} }
@ -2292,7 +2286,7 @@ public class SQLManager implements AbstractDB {
@Override @Override
public PreparedStatement get() throws SQLException { public PreparedStatement get() throws SQLException {
return SQLManager.this.connection.prepareStatement( return SQLManager.this.getConnection().prepareStatement(
"UPDATE `" + SQLManager.this.prefix "UPDATE `" + SQLManager.this.prefix
+ "plot_settings` SET `alias` = ? WHERE `plot_plot_id` = ?"); + "plot_settings` SET `alias` = ? WHERE `plot_plot_id` = ?");
} }
@ -2329,27 +2323,27 @@ public class SQLManager implements AbstractDB {
idstr.append(stmt_prefix).append(id); idstr.append(stmt_prefix).append(id);
stmt_prefix = " OR `plot_plot_id` = "; stmt_prefix = " OR `plot_plot_id` = ";
} }
PreparedStatement stmt = SQLManager.this.connection.prepareStatement( PreparedStatement stmt = SQLManager.this.getConnection().prepareStatement(
"DELETE FROM `" + SQLManager.this.prefix "DELETE FROM `" + SQLManager.this.prefix
+ "plot_helpers` WHERE `plot_plot_id` = " + idstr); + "plot_helpers` WHERE `plot_plot_id` = " + idstr);
stmt.executeUpdate(); stmt.executeUpdate();
stmt.close(); stmt.close();
stmt = SQLManager.this.connection.prepareStatement( stmt = SQLManager.this.getConnection().prepareStatement(
"DELETE FROM `" + SQLManager.this.prefix "DELETE FROM `" + SQLManager.this.prefix
+ "plot_denied` WHERE `plot_plot_id` = " + idstr); + "plot_denied` WHERE `plot_plot_id` = " + idstr);
stmt.executeUpdate(); stmt.executeUpdate();
stmt.close(); stmt.close();
stmt = SQLManager.this.connection.prepareStatement( stmt = SQLManager.this.getConnection().prepareStatement(
"DELETE FROM `" + SQLManager.this.prefix "DELETE FROM `" + SQLManager.this.prefix
+ "plot_settings` WHERE `plot_plot_id` = " + idstr); + "plot_settings` WHERE `plot_plot_id` = " + idstr);
stmt.executeUpdate(); stmt.executeUpdate();
stmt.close(); stmt.close();
stmt = SQLManager.this.connection.prepareStatement( stmt = SQLManager.this.getConnection().prepareStatement(
"DELETE FROM `" + SQLManager.this.prefix "DELETE FROM `" + SQLManager.this.prefix
+ "plot_trusted` WHERE `plot_plot_id` = " + idstr); + "plot_trusted` WHERE `plot_plot_id` = " + idstr);
stmt.executeUpdate(); stmt.executeUpdate();
stmt.close(); stmt.close();
stmt = SQLManager.this.connection.prepareStatement( stmt = SQLManager.this.getConnection().prepareStatement(
"DELETE FROM `" + SQLManager.this.prefix + "plot` WHERE `id` = " "DELETE FROM `" + SQLManager.this.prefix + "plot` WHERE `id` = "
+ idstr2); + idstr2);
stmt.executeUpdate(); stmt.executeUpdate();
@ -2368,7 +2362,7 @@ public class SQLManager implements AbstractDB {
@Override @Override
public void purge(final PlotArea area, final Set<PlotId> plots) { public void purge(final PlotArea area, final Set<PlotId> plots) {
addGlobalTask(() -> { addGlobalTask(() -> {
try (PreparedStatement stmt = SQLManager.this.connection.prepareStatement( try (PreparedStatement stmt = SQLManager.this.getConnection().prepareStatement(
"SELECT `id`, `plot_id_x`, `plot_id_z` FROM `" + SQLManager.this.prefix "SELECT `id`, `plot_id_x`, `plot_id_z` FROM `" + SQLManager.this.prefix
+ "plot` WHERE `world` = ?")) { + "plot` WHERE `world` = ?")) {
stmt.setString(1, area.toString()); stmt.setString(1, area.toString());
@ -2408,7 +2402,7 @@ public class SQLManager implements AbstractDB {
@Override @Override
public PreparedStatement get() throws SQLException { public PreparedStatement get() throws SQLException {
return SQLManager.this.connection.prepareStatement( return SQLManager.this.getConnection().prepareStatement(
"UPDATE `" + SQLManager.this.prefix "UPDATE `" + SQLManager.this.prefix
+ "plot_settings` SET `position` = ? WHERE `plot_plot_id` = ?"); + "plot_settings` SET `position` = ? WHERE `plot_plot_id` = ?");
} }
@ -2436,11 +2430,11 @@ public class SQLManager implements AbstractDB {
@Override @Override
public PreparedStatement get() throws SQLException { public PreparedStatement get() throws SQLException {
if (plot != null) { if (plot != null) {
return SQLManager.this.connection.prepareStatement( return SQLManager.this.getConnection().prepareStatement(
"DELETE FROM `" + SQLManager.this.prefix "DELETE FROM `" + SQLManager.this.prefix
+ "plot_comments` WHERE `world` = ? AND `hashcode` = ? AND `comment` = ? AND `inbox` = ? AND `sender` = ?"); + "plot_comments` WHERE `world` = ? AND `hashcode` = ? AND `comment` = ? AND `inbox` = ? AND `sender` = ?");
} }
return SQLManager.this.connection.prepareStatement( return SQLManager.this.getConnection().prepareStatement(
"DELETE FROM `" + SQLManager.this.prefix "DELETE FROM `" + SQLManager.this.prefix
+ "plot_comments` WHERE `comment` = ? AND `inbox` = ? AND `sender` = ?"); + "plot_comments` WHERE `comment` = ? AND `inbox` = ? AND `sender` = ?");
} }
@ -2464,11 +2458,11 @@ public class SQLManager implements AbstractDB {
@Override @Override
public PreparedStatement get() throws SQLException { public PreparedStatement get() throws SQLException {
if (plot != null) { if (plot != null) {
return SQLManager.this.connection.prepareStatement( return SQLManager.this.getConnection().prepareStatement(
"DELETE FROM `" + SQLManager.this.prefix "DELETE FROM `" + SQLManager.this.prefix
+ "plot_comments` WHERE `world` = ? AND `hashcode` = ? AND `inbox` = ?"); + "plot_comments` WHERE `world` = ? AND `hashcode` = ? AND `inbox` = ?");
} }
return SQLManager.this.connection.prepareStatement( return SQLManager.this.getConnection().prepareStatement(
"DELETE FROM `" + SQLManager.this.prefix + "plot_comments` `inbox` = ?"); "DELETE FROM `" + SQLManager.this.prefix + "plot_comments` `inbox` = ?");
} }
}); });
@ -2494,11 +2488,11 @@ public class SQLManager implements AbstractDB {
@Override @Override
public PreparedStatement get() throws SQLException { public PreparedStatement get() throws SQLException {
if (plot != null) { if (plot != null) {
return SQLManager.this.connection.prepareStatement( return SQLManager.this.getConnection().prepareStatement(
"SELECT * FROM `" + SQLManager.this.prefix "SELECT * FROM `" + SQLManager.this.prefix
+ "plot_comments` WHERE `world` = ? AND `hashcode` = ? AND `inbox` = ?"); + "plot_comments` WHERE `world` = ? AND `hashcode` = ? AND `inbox` = ?");
} }
return SQLManager.this.connection.prepareStatement( return SQLManager.this.getConnection().prepareStatement(
"SELECT * FROM `" + SQLManager.this.prefix "SELECT * FROM `" + SQLManager.this.prefix
+ "plot_comments` WHERE `inbox` = ?"); + "plot_comments` WHERE `inbox` = ?");
} }
@ -2549,7 +2543,7 @@ public class SQLManager implements AbstractDB {
@Override @Override
public PreparedStatement get() throws SQLException { public PreparedStatement get() throws SQLException {
return SQLManager.this.connection.prepareStatement( return SQLManager.this.getConnection().prepareStatement(
"INSERT INTO `" + SQLManager.this.prefix "INSERT INTO `" + SQLManager.this.prefix
+ "plot_comments` (`world`, `hashcode`, `comment`, `inbox`, `timestamp`, `sender`) VALUES(?,?,?,?,?,?)"); + "plot_comments` (`world`, `hashcode`, `comment`, `inbox`, `timestamp`, `sender`) VALUES(?,?,?,?,?,?)");
} }
@ -2567,7 +2561,7 @@ public class SQLManager implements AbstractDB {
@Override @Override
public PreparedStatement get() throws SQLException { public PreparedStatement get() throws SQLException {
return SQLManager.this.connection.prepareStatement( return SQLManager.this.getConnection().prepareStatement(
"DELETE FROM `" + SQLManager.this.prefix "DELETE FROM `" + SQLManager.this.prefix
+ "plot_helpers` WHERE `plot_plot_id` = ? AND `user_uuid` = ?"); + "plot_helpers` WHERE `plot_plot_id` = ? AND `user_uuid` = ?");
} }
@ -2585,7 +2579,7 @@ public class SQLManager implements AbstractDB {
@Override @Override
public PreparedStatement get() throws SQLException { public PreparedStatement get() throws SQLException {
return SQLManager.this.connection.prepareStatement( return SQLManager.this.getConnection().prepareStatement(
"DELETE FROM `" + SQLManager.this.prefix "DELETE FROM `" + SQLManager.this.prefix
+ "plot_trusted` WHERE `plot_plot_id` = ? AND `user_uuid` = ?"); + "plot_trusted` WHERE `plot_plot_id` = ? AND `user_uuid` = ?");
} }
@ -2603,7 +2597,7 @@ public class SQLManager implements AbstractDB {
@Override @Override
public PreparedStatement get() throws SQLException { public PreparedStatement get() throws SQLException {
return SQLManager.this.connection.prepareStatement( return SQLManager.this.getConnection().prepareStatement(
"INSERT INTO `" + SQLManager.this.prefix "INSERT INTO `" + SQLManager.this.prefix
+ "plot_helpers` (`plot_plot_id`, `user_uuid`) VALUES(?,?)"); + "plot_helpers` (`plot_plot_id`, `user_uuid`) VALUES(?,?)");
} }
@ -2621,7 +2615,7 @@ public class SQLManager implements AbstractDB {
@Override @Override
public PreparedStatement get() throws SQLException { public PreparedStatement get() throws SQLException {
return SQLManager.this.connection.prepareStatement( return SQLManager.this.getConnection().prepareStatement(
"INSERT INTO `" + SQLManager.this.prefix "INSERT INTO `" + SQLManager.this.prefix
+ "plot_trusted` (`plot_plot_id`, `user_uuid`) VALUES(?,?)"); + "plot_trusted` (`plot_plot_id`, `user_uuid`) VALUES(?,?)");
} }
@ -2639,7 +2633,7 @@ public class SQLManager implements AbstractDB {
@Override @Override
public PreparedStatement get() throws SQLException { public PreparedStatement get() throws SQLException {
return SQLManager.this.connection.prepareStatement( return SQLManager.this.getConnection().prepareStatement(
"DELETE FROM `" + SQLManager.this.prefix "DELETE FROM `" + SQLManager.this.prefix
+ "plot_denied` WHERE `plot_plot_id` = ? AND `user_uuid` = ?"); + "plot_denied` WHERE `plot_plot_id` = ? AND `user_uuid` = ?");
} }
@ -2657,7 +2651,7 @@ public class SQLManager implements AbstractDB {
@Override @Override
public PreparedStatement get() throws SQLException { public PreparedStatement get() throws SQLException {
return SQLManager.this.connection.prepareStatement( return SQLManager.this.getConnection().prepareStatement(
"INSERT INTO `" + SQLManager.this.prefix "INSERT INTO `" + SQLManager.this.prefix
+ "plot_denied` (`plot_plot_id`, `user_uuid`) VALUES(?,?)"); + "plot_denied` (`plot_plot_id`, `user_uuid`) VALUES(?,?)");
} }
@ -2667,7 +2661,7 @@ public class SQLManager implements AbstractDB {
@Override @Override
public HashMap<UUID, Integer> getRatings(Plot plot) { public HashMap<UUID, Integer> getRatings(Plot plot) {
HashMap<UUID, Integer> map = new HashMap<>(); HashMap<UUID, Integer> map = new HashMap<>();
try (PreparedStatement statement = this.connection.prepareStatement( try (PreparedStatement statement = this.getConnection().prepareStatement(
"SELECT `rating`, `player` FROM `" + this.prefix "SELECT `rating`, `player` FROM `" + this.prefix
+ "plot_rating` WHERE `plot_plot_id` = ? ")) { + "plot_rating` WHERE `plot_plot_id` = ? ")) {
statement.setInt(1, getId(plot)); statement.setInt(1, getId(plot));
@ -2697,7 +2691,7 @@ public class SQLManager implements AbstractDB {
@Override @Override
public PreparedStatement get() throws SQLException { public PreparedStatement get() throws SQLException {
return SQLManager.this.connection.prepareStatement( return SQLManager.this.getConnection().prepareStatement(
"INSERT INTO `" + SQLManager.this.prefix "INSERT INTO `" + SQLManager.this.prefix
+ "plot_rating` (`plot_plot_id`, `rating`, `player`) VALUES(?,?,?)"); + "plot_rating` (`plot_plot_id`, `rating`, `player`) VALUES(?,?,?)");
} }
@ -2715,7 +2709,7 @@ public class SQLManager implements AbstractDB {
@Override @Override
public PreparedStatement get() throws SQLException { public PreparedStatement get() throws SQLException {
return SQLManager.this.connection.prepareStatement( return SQLManager.this.getConnection().prepareStatement(
"DELETE FROM `" + SQLManager.this.prefix "DELETE FROM `" + SQLManager.this.prefix
+ "cluster_settings` WHERE `cluster_id` = ?"); + "cluster_settings` WHERE `cluster_id` = ?");
} }
@ -2728,7 +2722,7 @@ public class SQLManager implements AbstractDB {
@Override @Override
public PreparedStatement get() throws SQLException { public PreparedStatement get() throws SQLException {
return SQLManager.this.connection.prepareStatement( return SQLManager.this.getConnection().prepareStatement(
"DELETE FROM `" + SQLManager.this.prefix "DELETE FROM `" + SQLManager.this.prefix
+ "cluster_helpers` WHERE `cluster_id` = ?"); + "cluster_helpers` WHERE `cluster_id` = ?");
} }
@ -2741,7 +2735,7 @@ public class SQLManager implements AbstractDB {
@Override @Override
public PreparedStatement get() throws SQLException { public PreparedStatement get() throws SQLException {
return SQLManager.this.connection.prepareStatement( return SQLManager.this.getConnection().prepareStatement(
"DELETE FROM `" + SQLManager.this.prefix "DELETE FROM `" + SQLManager.this.prefix
+ "cluster_invited` WHERE `cluster_id` = ?"); + "cluster_invited` WHERE `cluster_id` = ?");
} }
@ -2754,7 +2748,7 @@ public class SQLManager implements AbstractDB {
@Override @Override
public PreparedStatement get() throws SQLException { public PreparedStatement get() throws SQLException {
return SQLManager.this.connection.prepareStatement( return SQLManager.this.getConnection().prepareStatement(
"DELETE FROM `" + SQLManager.this.prefix + "cluster` WHERE `id` = ?"); "DELETE FROM `" + SQLManager.this.prefix + "cluster` WHERE `id` = ?");
} }
}); });
@ -2782,11 +2776,11 @@ public class SQLManager implements AbstractDB {
@Override @Override
public PreparedStatement get() throws SQLException { public PreparedStatement get() throws SQLException {
if (replace) { if (replace) {
return SQLManager.this.connection.prepareStatement( return SQLManager.this.getConnection().prepareStatement(
"UPDATE `" + SQLManager.this.prefix "UPDATE `" + SQLManager.this.prefix
+ "player_meta` SET `value` = ? WHERE `uuid` = ? AND `key` = ?"); + "player_meta` SET `value` = ? WHERE `uuid` = ? AND `key` = ?");
} else { } else {
return SQLManager.this.connection.prepareStatement( return SQLManager.this.getConnection().prepareStatement(
"INSERT INTO `" + SQLManager.this.prefix "INSERT INTO `" + SQLManager.this.prefix
+ "player_meta`(`uuid`, `key`, `value`) VALUES(?, ? ,?)"); + "player_meta`(`uuid`, `key`, `value`) VALUES(?, ? ,?)");
} }
@ -2805,7 +2799,7 @@ public class SQLManager implements AbstractDB {
@Override @Override
public PreparedStatement get() throws SQLException { public PreparedStatement get() throws SQLException {
return SQLManager.this.connection.prepareStatement( return SQLManager.this.getConnection().prepareStatement(
"DELETE FROM `" + SQLManager.this.prefix "DELETE FROM `" + SQLManager.this.prefix
+ "player_meta` WHERE `uuid` = ? AND `key` = ?"); + "player_meta` WHERE `uuid` = ? AND `key` = ?");
} }
@ -2822,7 +2816,7 @@ public class SQLManager implements AbstractDB {
@Override @Override
public PreparedStatement get() throws SQLException { public PreparedStatement get() throws SQLException {
return SQLManager.this.connection.prepareStatement( return SQLManager.this.getConnection().prepareStatement(
"SELECT * FROM `" + SQLManager.this.prefix "SELECT * FROM `" + SQLManager.this.prefix
+ "player_meta` WHERE `uuid` = ? ORDER BY `meta_id` ASC"); + "player_meta` WHERE `uuid` = ? ORDER BY `meta_id` ASC");
} }
@ -2879,7 +2873,7 @@ public class SQLManager implements AbstractDB {
/* /*
* Getting clusters * Getting clusters
*/ */
try (Statement stmt = this.connection.createStatement()) { try (Statement stmt = this.getConnection().createStatement()) {
ResultSet resultSet = ResultSet resultSet =
stmt.executeQuery("SELECT * FROM `" + this.prefix + "cluster`"); stmt.executeQuery("SELECT * FROM `" + this.prefix + "cluster`");
PlotCluster cluster; PlotCluster cluster;
@ -3010,7 +3004,7 @@ public class SQLManager implements AbstractDB {
@Override @Override
public PreparedStatement get() throws SQLException { public PreparedStatement get() throws SQLException {
return SQLManager.this.connection.prepareStatement( return SQLManager.this.getConnection().prepareStatement(
"UPDATE `" + SQLManager.this.prefix "UPDATE `" + SQLManager.this.prefix
+ "cluster_settings` SET `alias` = ? WHERE `cluster_id` = ?"); + "cluster_settings` SET `alias` = ? WHERE `cluster_id` = ?");
} }
@ -3029,7 +3023,7 @@ public class SQLManager implements AbstractDB {
@Override @Override
public PreparedStatement get() throws SQLException { public PreparedStatement get() throws SQLException {
return SQLManager.this.connection.prepareStatement( return SQLManager.this.getConnection().prepareStatement(
"DELETE FROM `" + SQLManager.this.prefix "DELETE FROM `" + SQLManager.this.prefix
+ "cluster_helpers` WHERE `cluster_id` = ? AND `user_uuid` = ?"); + "cluster_helpers` WHERE `cluster_id` = ? AND `user_uuid` = ?");
} }
@ -3047,7 +3041,7 @@ public class SQLManager implements AbstractDB {
@Override @Override
public PreparedStatement get() throws SQLException { public PreparedStatement get() throws SQLException {
return SQLManager.this.connection.prepareStatement( return SQLManager.this.getConnection().prepareStatement(
"INSERT INTO `" + SQLManager.this.prefix "INSERT INTO `" + SQLManager.this.prefix
+ "cluster_helpers` (`cluster_id`, `user_uuid`) VALUES(?,?)"); + "cluster_helpers` (`cluster_id`, `user_uuid`) VALUES(?,?)");
} }
@ -3069,7 +3063,7 @@ public class SQLManager implements AbstractDB {
@Override @Override
public PreparedStatement get() throws SQLException { public PreparedStatement get() throws SQLException {
return SQLManager.this.connection.prepareStatement( return SQLManager.this.getConnection().prepareStatement(
SQLManager.this.CREATE_CLUSTER, SQLManager.this.CREATE_CLUSTER,
Statement.RETURN_GENERATED_KEYS Statement.RETURN_GENERATED_KEYS
); );
@ -3100,7 +3094,7 @@ public class SQLManager implements AbstractDB {
@Override @Override
public PreparedStatement get() throws SQLException { public PreparedStatement get() throws SQLException {
return SQLManager.this.connection.prepareStatement( return SQLManager.this.getConnection().prepareStatement(
"INSERT INTO `" + SQLManager.this.prefix "INSERT INTO `" + SQLManager.this.prefix
+ "cluster_settings`(`cluster_id`, `alias`) VALUES(?, ?)"); + "cluster_settings`(`cluster_id`, `alias`) VALUES(?, ?)");
} }
@ -3127,7 +3121,7 @@ public class SQLManager implements AbstractDB {
@Override @Override
public PreparedStatement get() throws SQLException { public PreparedStatement get() throws SQLException {
return SQLManager.this.connection.prepareStatement( return SQLManager.this.getConnection().prepareStatement(
"UPDATE `" + SQLManager.this.prefix "UPDATE `" + SQLManager.this.prefix
+ "cluster` SET `pos1_x` = ?, `pos1_z` = ?, `pos2_x` = ?, `pos2_z` = ? WHERE `id` = ?"); + "cluster` SET `pos1_x` = ?, `pos1_z` = ?, `pos2_x` = ?, `pos2_z` = ? WHERE `id` = ?");
} }
@ -3145,7 +3139,7 @@ public class SQLManager implements AbstractDB {
@Override @Override
public PreparedStatement get() throws SQLException { public PreparedStatement get() throws SQLException {
return SQLManager.this.connection.prepareStatement( return SQLManager.this.getConnection().prepareStatement(
"UPDATE `" + SQLManager.this.prefix "UPDATE `" + SQLManager.this.prefix
+ "cluster_settings` SET `position` = ? WHERE `cluster_id` = ?"); + "cluster_settings` SET `position` = ? WHERE `cluster_id` = ?");
} }
@ -3163,7 +3157,7 @@ public class SQLManager implements AbstractDB {
@Override @Override
public PreparedStatement get() throws SQLException { public PreparedStatement get() throws SQLException {
return SQLManager.this.connection.prepareStatement( return SQLManager.this.database.getConnection().prepareStatement(
"DELETE FROM `" + SQLManager.this.prefix "DELETE FROM `" + SQLManager.this.prefix
+ "cluster_invited` WHERE `cluster_id` = ? AND `user_uuid` = ?"); + "cluster_invited` WHERE `cluster_id` = ? AND `user_uuid` = ?");
} }
@ -3181,7 +3175,7 @@ public class SQLManager implements AbstractDB {
@Override @Override
public PreparedStatement get() throws SQLException { public PreparedStatement get() throws SQLException {
return SQLManager.this.connection.prepareStatement( return SQLManager.this.getConnection().prepareStatement(
"INSERT INTO `" + SQLManager.this.prefix "INSERT INTO `" + SQLManager.this.prefix
+ "cluster_invited` (`cluster_id`, `user_uuid`) VALUES(?,?)"); + "cluster_invited` (`cluster_id`, `user_uuid`) VALUES(?,?)");
} }
@ -3190,12 +3184,11 @@ public class SQLManager implements AbstractDB {
@Override @Override
public boolean deleteTables() { public boolean deleteTables() {
try (Statement stmt = this.connection.createStatement(); try (Statement stmt = this.getConnection().createStatement();
PreparedStatement statement = this.connection PreparedStatement statement = this.getConnection()
.prepareStatement("DROP TABLE `" + this.prefix + "plot`")) { .prepareStatement("DROP TABLE `" + this.prefix + "plot`")) {
close(); close();
this.closed = false; this.closed = false;
SQLManager.this.connection = this.database.forceConnection();
stmt.addBatch("DROP TABLE `" + this.prefix + "cluster_invited`"); stmt.addBatch("DROP TABLE `" + this.prefix + "cluster_invited`");
stmt.addBatch("DROP TABLE `" + this.prefix + "cluster_helpers`"); stmt.addBatch("DROP TABLE `" + this.prefix + "cluster_helpers`");
stmt.addBatch("DROP TABLE `" + this.prefix + "cluster`"); stmt.addBatch("DROP TABLE `" + this.prefix + "cluster`");
@ -3208,7 +3201,7 @@ public class SQLManager implements AbstractDB {
stmt.executeBatch(); stmt.executeBatch();
stmt.clearBatch(); stmt.clearBatch();
statement.executeUpdate(); statement.executeUpdate();
} catch (ClassNotFoundException | SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -3230,8 +3223,8 @@ public class SQLManager implements AbstractDB {
} }
} }
try { try {
if (this.connection.getAutoCommit()) { if (this.getConnection().getAutoCommit()) {
this.connection.setAutoCommit(false); this.getConnection().setAutoCommit(false);
} }
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
@ -3350,7 +3343,7 @@ public class SQLManager implements AbstractDB {
) { ) {
addGlobalTask(() -> { addGlobalTask(() -> {
if (min == null) { if (min == null) {
try (PreparedStatement stmt = SQLManager.this.connection.prepareStatement( try (PreparedStatement stmt = SQLManager.this.getConnection().prepareStatement(
"UPDATE `" + SQLManager.this.prefix "UPDATE `" + SQLManager.this.prefix
+ "plot` SET `world` = ? WHERE `world` = ?")) { + "plot` SET `world` = ? WHERE `world` = ?")) {
stmt.setString(1, newWorld); stmt.setString(1, newWorld);
@ -3359,7 +3352,7 @@ public class SQLManager implements AbstractDB {
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
try (PreparedStatement stmt = SQLManager.this.connection.prepareStatement( try (PreparedStatement stmt = SQLManager.this.getConnection().prepareStatement(
"UPDATE `" + SQLManager.this.prefix "UPDATE `" + SQLManager.this.prefix
+ "cluster` SET `world` = ? WHERE `world` = ?")) { + "cluster` SET `world` = ? WHERE `world` = ?")) {
stmt.setString(1, newWorld); stmt.setString(1, newWorld);
@ -3369,7 +3362,7 @@ public class SQLManager implements AbstractDB {
e.printStackTrace(); e.printStackTrace();
} }
} else { } else {
try (PreparedStatement stmt = SQLManager.this.connection.prepareStatement( try (PreparedStatement stmt = SQLManager.this.getConnection().prepareStatement(
"UPDATE `" + SQLManager.this.prefix "UPDATE `" + SQLManager.this.prefix
+ "plot` SET `world` = ? WHERE `world` = ? AND `plot_id_x` BETWEEN ? AND ? AND `plot_id_z` BETWEEN ? AND ?")) { + "plot` SET `world` = ? WHERE `world` = ? AND `plot_id_x` BETWEEN ? AND ? AND `plot_id_z` BETWEEN ? AND ?")) {
stmt.setString(1, newWorld); stmt.setString(1, newWorld);
@ -3382,7 +3375,7 @@ public class SQLManager implements AbstractDB {
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
try (PreparedStatement stmt = SQLManager.this.connection.prepareStatement( try (PreparedStatement stmt = SQLManager.this.getConnection().prepareStatement(
"UPDATE `" + SQLManager.this.prefix "UPDATE `" + SQLManager.this.prefix
+ "cluster` SET `world` = ? WHERE `world` = ? AND `pos1_x` <= ? AND `pos1_z` <= ? AND `pos2_x` >= ? AND `pos2_z` >= ?")) { + "cluster` SET `world` = ? WHERE `world` = ? AND `pos1_x` <= ? AND `pos1_z` <= ? AND `pos2_x` >= ? AND `pos2_z` >= ?")) {
stmt.setString(1, newWorld); stmt.setString(1, newWorld);
@ -3402,7 +3395,7 @@ public class SQLManager implements AbstractDB {
@Override @Override
public void replaceUUID(final UUID old, final UUID now) { public void replaceUUID(final UUID old, final UUID now) {
addGlobalTask(() -> { addGlobalTask(() -> {
try (Statement stmt = SQLManager.this.connection.createStatement()) { try (Statement stmt = SQLManager.this.getConnection().createStatement()) {
stmt.executeUpdate( stmt.executeUpdate(
"UPDATE `" + SQLManager.this.prefix + "cluster` SET `owner` = '" + now "UPDATE `" + SQLManager.this.prefix + "cluster` SET `owner` = '" + now
.toString() + "' WHERE `owner` = '" + old.toString() + '\''); .toString() + "' WHERE `owner` = '" + old.toString() + '\'');
@ -3431,7 +3424,7 @@ public class SQLManager implements AbstractDB {
public void close() { public void close() {
try { try {
this.closed = true; this.closed = true;
this.connection.close(); this.getConnection().close();
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }

View File

@ -19,13 +19,14 @@
package com.plotsquared.core.database; package com.plotsquared.core.database;
import com.plotsquared.core.PlotSquared; import com.plotsquared.core.PlotSquared;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Statement; import java.sql.Statement;
@ -36,9 +37,7 @@ import java.sql.Statement;
public class SQLite extends Database { public class SQLite extends Database {
private static final Logger LOGGER = LogManager.getLogger("PlotSquared/" + SQLite.class.getSimpleName()); private static final Logger LOGGER = LogManager.getLogger("PlotSquared/" + SQLite.class.getSimpleName());
private final HikariDataSource hikariDataSource;
private final String dbLocation;
private Connection connection;
/** /**
* Creates a new SQLite instance * Creates a new SQLite instance
@ -46,18 +45,10 @@ public class SQLite extends Database {
* @param dbLocation Location of the Database (Must end in .db) * @param dbLocation Location of the Database (Must end in .db)
*/ */
public SQLite(File dbLocation) { public SQLite(File dbLocation) {
this.dbLocation = dbLocation.getAbsolutePath();
}
@Override
public Connection openConnection() throws SQLException, ClassNotFoundException {
if (checkConnection()) {
return this.connection;
}
if (!PlotSquared.platform().getDirectory().exists()) { if (!PlotSquared.platform().getDirectory().exists()) {
PlotSquared.platform().getDirectory().mkdirs(); PlotSquared.platform().getDirectory().mkdirs();
} }
File file = new File(this.dbLocation); File file = dbLocation;
if (!file.exists()) { if (!file.exists()) {
try { try {
file.createNewFile(); file.createNewFile();
@ -65,56 +56,47 @@ public class SQLite extends Database {
LOGGER.error("Unable to create database"); LOGGER.error("Unable to create database");
} }
} }
Class.forName("org.sqlite.JDBC"); try {
this.connection = DriverManager.getConnection("jdbc:sqlite:" + this.dbLocation); Class.forName("org.sqlite.JDBC");
return this.connection; } catch (ClassNotFoundException e) {
} throw new RuntimeException(e);
}
@Override HikariConfig config = new HikariConfig();
public boolean checkConnection() throws SQLException { config.setJdbcUrl("jdbc:sqlite:" + dbLocation);
return (this.connection != null) && !this.connection.isClosed(); config.setDriverClassName("org.sqlite.JDBC");
this.hikariDataSource = new HikariDataSource(config);
} }
@Override @Override
public Connection getConnection() { public Connection getConnection() {
return this.connection; try {
return this.hikariDataSource.getConnection();
} catch (SQLException e) {
throw new RuntimeException(e);
}
} }
@Override @Override
public boolean closeConnection() throws SQLException { public boolean closeConnection() throws SQLException {
if (this.connection == null) { if (this.hikariDataSource == null) {
return false; return false;
} }
this.connection.close(); this.hikariDataSource.close();
this.connection = null;
return true; return true;
} }
@Override @Override
public ResultSet querySQL(String query) throws SQLException, ClassNotFoundException { public ResultSet querySQL(String query) throws SQLException {
if (checkConnection()) { try (Statement statement = this.getConnection().createStatement()) {
openConnection();
}
try (Statement statement = this.connection.createStatement()) {
return statement.executeQuery(query); return statement.executeQuery(query);
} }
} }
@Override @Override
public int updateSQL(String query) throws SQLException, ClassNotFoundException { public int updateSQL(String query) throws SQLException {
if (checkConnection()) { try (Statement statement = this.getConnection().createStatement()) {
openConnection();
}
try (Statement statement = this.connection.createStatement()) {
return statement.executeUpdate(query); return statement.executeUpdate(query);
} }
} }
@Override
public Connection forceConnection() throws SQLException, ClassNotFoundException {
Class.forName("org.sqlite.JDBC");
this.connection = DriverManager.getConnection("jdbc:sqlite:" + this.dbLocation);
return this.connection;
}
} }

View File

@ -31,6 +31,7 @@ paperlib = "1.0.8"
informative-annotations = "1.4" informative-annotations = "1.4"
vault = "1.7.1" vault = "1.7.1"
serverlib = "2.3.4" serverlib = "2.3.4"
hikaricp = "5.0.1"
# Gradle plugins # Gradle plugins
shadow = "8.1.1" shadow = "8.1.1"
@ -77,6 +78,7 @@ informativeAnnotations = { group = "com.intellectualsites.informative-annotation
paperlib = { group = "io.papermc", name = "paperlib", version.ref = "paperlib" } paperlib = { group = "io.papermc", name = "paperlib", version.ref = "paperlib" }
vault = { group = "com.github.MilkBowl", name = "VaultAPI", version.ref = "vault" } vault = { group = "com.github.MilkBowl", name = "VaultAPI", version.ref = "vault" }
serverlib = { group = "dev.notmyfault.serverlib", name = "ServerLib", version.ref = "serverlib" } serverlib = { group = "dev.notmyfault.serverlib", name = "ServerLib", version.ref = "serverlib" }
hikaricp = { group = "com.zaxxer", name = "HikariCP", version.ref = "hikaricp"}
[plugins] [plugins]
shadow = { id = "com.github.johnrengelman.shadow", version.ref = "shadow" } shadow = { id = "com.github.johnrengelman.shadow", version.ref = "shadow" }