diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/uuid/SQLiteUUIDService.java b/Bukkit/src/main/java/com/plotsquared/bukkit/uuid/SQLiteUUIDService.java index 9193b4f0a..12a4cbbd4 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/uuid/SQLiteUUIDService.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/uuid/SQLiteUUIDService.java @@ -45,11 +45,6 @@ public class SQLiteUUIDService implements UUIDService, Consumer getGlobalTasks() { @@ -392,8 +386,8 @@ public class SQLManager implements AbstractDB { public boolean sendBatch() { try { if (!getGlobalTasks().isEmpty()) { - if (this.connection.getAutoCommit()) { - this.connection.setAutoCommit(false); + if (this.getConnection().getAutoCommit()) { + this.getConnection().setAutoCommit(false); } Runnable task = getGlobalTasks().remove(); if (task != null) { @@ -414,8 +408,8 @@ public class SQLManager implements AbstractDB { int count = -1; if (!this.plotTasks.isEmpty()) { count = Math.max(count, 0); - if (this.connection.getAutoCommit()) { - this.connection.setAutoCommit(false); + if (this.getConnection().getAutoCommit()) { + this.getConnection().setAutoCommit(false); } String method = null; PreparedStatement statement = null; @@ -469,8 +463,8 @@ public class SQLManager implements AbstractDB { } if (!this.playerTasks.isEmpty()) { count = Math.max(count, 0); - if (this.connection.getAutoCommit()) { - this.connection.setAutoCommit(false); + if (this.getConnection().getAutoCommit()) { + this.getConnection().setAutoCommit(false); } String method = null; PreparedStatement statement = null; @@ -514,8 +508,8 @@ public class SQLManager implements AbstractDB { } if (!this.clusterTasks.isEmpty()) { count = Math.max(count, 0); - if (this.connection.getAutoCommit()) { - this.connection.setAutoCommit(false); + if (this.getConnection().getAutoCommit()) { + this.getConnection().setAutoCommit(false); } String method = null; PreparedStatement statement = null; @@ -563,8 +557,8 @@ public class SQLManager implements AbstractDB { return true; } if (count != -1) { - if (!this.connection.getAutoCommit()) { - this.connection.setAutoCommit(true); + if (!this.getConnection().getAutoCommit()) { + this.getConnection().setAutoCommit(true); } } if (!this.clusterTasks.isEmpty()) { @@ -585,7 +579,7 @@ public class SQLManager implements AbstractDB { } public Connection getConnection() { - return this.connection; + return this.database.getConnection(); } /** @@ -607,7 +601,7 @@ public class SQLManager implements AbstractDB { @Override 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 denied = new ArrayList<>(); // Populating structures - try (PreparedStatement stmt = SQLManager.this.connection + try (PreparedStatement stmt = SQLManager.this.getConnection() .prepareStatement(SQLManager.this.GET_ALL_PLOTS); ResultSet result = stmt.executeQuery()) { while (result.next()) { @@ -663,7 +657,7 @@ public class SQLManager implements AbstractDB { () -> createTiers(trusted, "trusted", () -> createTiers(denied, "denied", () -> { try { - SQLManager.this.connection.commit(); + SQLManager.this.getConnection().commit(); } catch (SQLException e) { e.printStackTrace(); } @@ -677,7 +671,7 @@ public class SQLManager implements AbstractDB { } catch (SQLException e) { LOGGER.warn("Failed to set all flags and member tiers for plots", e); try { - SQLManager.this.connection.commit(); + SQLManager.this.getConnection().commit(); } catch (SQLException e1) { e1.printStackTrace(); } @@ -686,7 +680,7 @@ public class SQLManager implements AbstractDB { } catch (Exception e) { LOGGER.warn("Warning! Failed to set all helper for plots", e); try { - SQLManager.this.connection.commit(); + SQLManager.this.getConnection().commit(); } catch (SQLException e1) { e1.printStackTrace(); } @@ -747,7 +741,7 @@ public class SQLManager implements AbstractDB { } public void createFlags(Map ids, List plots, Runnable whenDone) { - try (final PreparedStatement preparedStatement = this.connection.prepareStatement( + try (final PreparedStatement preparedStatement = this.getConnection().prepareStatement( "INSERT INTO `" + SQLManager.this.prefix + "plot_flags`(`plot_id`, `flag`, `value`) VALUES(?, ?, ?)")) { for (final Plot plot : plots) { @@ -872,13 +866,13 @@ public class SQLManager implements AbstractDB { if (last == -1) { last = 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) { preparedStmt.executeBatch(); preparedStmt.close(); statement = mod.getCreateMySQL(subList.size()); - preparedStmt = this.connection.prepareStatement(statement); + preparedStmt = this.getConnection().prepareStatement(statement); } for (int i = 0; i < subList.size(); i++) { count++; @@ -914,13 +908,13 @@ public class SQLManager implements AbstractDB { if (last == -1) { last = 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) { preparedStmt.executeBatch(); preparedStmt.clearParameters(); statement = mod.getCreateSQLite(subList.size()); - preparedStmt = this.connection.prepareStatement(statement); + preparedStmt = this.getConnection().prepareStatement(statement); } for (int i = 0; i < subList.size(); i++) { count++; @@ -937,7 +931,7 @@ public class SQLManager implements AbstractDB { e.printStackTrace(); LOGGER.error("2: | {}", objList.get(0).getClass().getCanonicalName()); LOGGER.error("Could not bulk save!"); - try (PreparedStatement preparedStmt = this.connection + try (PreparedStatement preparedStmt = this.getConnection() .prepareStatement(mod.getCreateSQL())) { for (T obj : objList) { mod.setSQL(preparedStmt, obj); @@ -955,7 +949,7 @@ public class SQLManager implements AbstractDB { } public void createSettings(final ArrayList 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`" + "(`plot_plot_id`,`biome`,`rain`,`custom_time`,`time`,`deny_entry`,`alias`,`merged`,`position`) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)")) { @@ -1083,7 +1077,7 @@ public class SQLManager implements AbstractDB { @Override public PreparedStatement get() throws SQLException { - return SQLManager.this.connection.prepareStatement( + return SQLManager.this.getConnection().prepareStatement( SQLManager.this.CREATE_PLOT_SAFE, Statement.RETURN_GENERATED_KEYS ); @@ -1110,7 +1104,7 @@ public class SQLManager implements AbstractDB { @Override public PreparedStatement get() throws SQLException { - return SQLManager.this.connection.prepareStatement( + return SQLManager.this.getConnection().prepareStatement( "INSERT INTO `" + SQLManager.this.prefix + "plot_settings`(`plot_plot_id`) VALUES(?)"); } @@ -1134,9 +1128,9 @@ public class SQLManager implements AbstractDB { return; } try { - if (!this.connection.getAutoCommit()) { - this.connection.commit(); - this.connection.setAutoCommit(true); + if (!this.getConnection().getAutoCommit()) { + this.getConnection().commit(); + this.getConnection().setAutoCommit(true); } } catch (SQLException e) { e.printStackTrace(); @@ -1157,7 +1151,7 @@ public class SQLManager implements AbstractDB { @Override public PreparedStatement get() throws SQLException { - return SQLManager.this.connection + return SQLManager.this.getConnection() .prepareStatement(SQLManager.this.CREATE_PLOT, Statement.RETURN_GENERATED_KEYS); } @@ -1183,7 +1177,7 @@ public class SQLManager implements AbstractDB { @Override public PreparedStatement get() throws SQLException { - return SQLManager.this.connection.prepareStatement( + return SQLManager.this.getConnection().prepareStatement( "INSERT INTO `" + SQLManager.this.prefix + "plot_settings`(`plot_plot_id`) VALUES(?)"); } @@ -1201,7 +1195,7 @@ public class SQLManager implements AbstractDB { String[] tables = new String[]{"plot", "plot_denied", "plot_helpers", "plot_comments", "plot_trusted", "plot_rating", "plot_settings", "cluster", "player_meta", "plot_flags"}; - DatabaseMetaData meta = this.connection.getMetaData(); + DatabaseMetaData meta = this.getConnection().getMetaData(); int create = 0; for (String s : tables) { ResultSet set = meta.getTables(null, null, this.prefix + s, new String[]{"TABLE"}); @@ -1215,7 +1209,7 @@ public class SQLManager implements AbstractDB { return; } boolean addConstraint = create == tables.length; - try (Statement stmt = this.connection.createStatement()) { + try (Statement stmt = this.getConnection().createStatement()) { if (this.mySQL) { stmt.addBatch("CREATE TABLE IF NOT EXISTS `" + this.prefix + "plot` (" + "`id` INT(11) NOT NULL AUTO_INCREMENT," + "`plot_id_x` INT(11) NOT NULL," @@ -1361,7 +1355,7 @@ public class SQLManager implements AbstractDB { @Override public PreparedStatement get() throws SQLException { - return SQLManager.this.connection.prepareStatement( + return SQLManager.this.getConnection().prepareStatement( "DELETE FROM `" + SQLManager.this.prefix + "plot_settings` WHERE `plot_plot_id` = ?"); } @@ -1381,7 +1375,7 @@ public class SQLManager implements AbstractDB { @Override public PreparedStatement get() throws SQLException { - return SQLManager.this.connection.prepareStatement( + return SQLManager.this.getConnection().prepareStatement( "DELETE FROM `" + SQLManager.this.prefix + "plot_helpers` WHERE `plot_plot_id` = ?"); } @@ -1401,7 +1395,7 @@ public class SQLManager implements AbstractDB { @Override public PreparedStatement get() throws SQLException { - return SQLManager.this.connection.prepareStatement( + return SQLManager.this.getConnection().prepareStatement( "DELETE FROM `" + SQLManager.this.prefix + "plot_trusted` WHERE `plot_plot_id` = ?"); } @@ -1421,7 +1415,7 @@ public class SQLManager implements AbstractDB { @Override public PreparedStatement get() throws SQLException { - return SQLManager.this.connection.prepareStatement( + return SQLManager.this.getConnection().prepareStatement( "DELETE FROM `" + SQLManager.this.prefix + "plot_denied` WHERE `plot_plot_id` = ?"); } @@ -1439,7 +1433,7 @@ public class SQLManager implements AbstractDB { @Override public PreparedStatement get() throws SQLException { - return SQLManager.this.connection.prepareStatement( + return SQLManager.this.getConnection().prepareStatement( "DELETE FROM `" + SQLManager.this.prefix + "plot_comments` WHERE `world` = ? AND `hashcode` = ?"); } @@ -1459,7 +1453,7 @@ public class SQLManager implements AbstractDB { @Override public PreparedStatement get() throws SQLException { - return SQLManager.this.connection.prepareStatement( + return SQLManager.this.getConnection().prepareStatement( "DELETE FROM `" + SQLManager.this.prefix + "plot_rating` WHERE `plot_plot_id` = ?"); } @@ -1487,7 +1481,7 @@ public class SQLManager implements AbstractDB { @Override public PreparedStatement get() throws SQLException { - return SQLManager.this.connection.prepareStatement( + return SQLManager.this.getConnection().prepareStatement( "DELETE FROM `" + SQLManager.this.prefix + "plot` WHERE `id` = ?"); } }); @@ -1509,7 +1503,7 @@ public class SQLManager implements AbstractDB { @Override public PreparedStatement get() throws SQLException { - return SQLManager.this.connection.prepareStatement( + return SQLManager.this.getConnection().prepareStatement( "INSERT INTO `" + SQLManager.this.prefix + "plot_settings`(`plot_plot_id`) VALUES(?)"); } @@ -1527,7 +1521,7 @@ public class SQLManager implements AbstractDB { return cluster.temp; } int c_id; - try (PreparedStatement stmt = this.connection.prepareStatement( + try (PreparedStatement stmt = this.getConnection().prepareStatement( "SELECT `id` FROM `" + this.prefix + "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()); @@ -1567,7 +1561,7 @@ public class SQLManager implements AbstractDB { return plot.temp; } int id; - try (PreparedStatement statement = this.connection.prepareStatement( + try (PreparedStatement statement = this.getConnection().prepareStatement( "SELECT `id` FROM `" + this.prefix + "plot` WHERE `plot_id_x` = ? AND `plot_id_z` = ? AND world = ? ORDER BY `timestamp` ASC")) { statement.setInt(1, plot.getId().getX()); @@ -1598,13 +1592,13 @@ public class SQLManager implements AbstractDB { public void updateTables(int[] oldVersion) { try { if (this.mySQL && !PlotSquared.get().checkVersion(oldVersion, 3, 3, 2)) { - try (Statement stmt = this.connection.createStatement()) { + try (Statement stmt = this.getConnection().createStatement()) { stmt.executeUpdate( "ALTER TABLE `" + this.prefix + "plots` DROP INDEX `unique_alias`"); } catch (SQLException ignored) { } } - DatabaseMetaData data = this.connection.getMetaData(); + DatabaseMetaData data = this.getConnection().getMetaData(); ResultSet rs = data.getColumns(null, null, this.prefix + "plot_comments", "plot_plot_id"); if (rs.next()) { @@ -1612,7 +1606,7 @@ public class SQLManager implements AbstractDB { rs = data.getColumns(null, null, this.prefix + "plot_comments", "hashcode"); if (!rs.next()) { rs.close(); - try (Statement statement = this.connection.createStatement()) { + try (Statement statement = this.getConnection().createStatement()) { statement.addBatch("DROP TABLE `" + this.prefix + "plot_comments`"); if (Storage.MySQL.USE) { statement.addBatch( @@ -1633,7 +1627,7 @@ public class SQLManager implements AbstractDB { } statement.executeBatch(); } catch (SQLException ignored) { - try (Statement statement = this.connection.createStatement()) { + try (Statement statement = this.getConnection().createStatement()) { statement.addBatch("ALTER IGNORE TABLE `" + this.prefix + "plot_comments` ADD `inbox` VARCHAR(11) DEFAULT `public`"); statement.addBatch("ALTER IGNORE TABLE `" + this.prefix @@ -1647,7 +1641,7 @@ public class SQLManager implements AbstractDB { rs.close(); rs = data.getColumns(null, null, this.prefix + "plot_denied", "plot_plot_id"); if (rs.next()) { - try (Statement statement = this.connection.createStatement()) { + try (Statement statement = this.getConnection().createStatement()) { statement.executeUpdate("DELETE FROM `" + this.prefix + "plot_denied` WHERE `plot_plot_id` NOT IN (SELECT `id` FROM `" + this.prefix + "plot`)"); @@ -1656,7 +1650,7 @@ public class SQLManager implements AbstractDB { } rs.close(); - try (Statement statement = this.connection.createStatement()) { + try (Statement statement = this.getConnection().createStatement()) { for (String table : new String[]{"plot_denied", "plot_helpers", "plot_trusted"}) { ResultSet result = statement.executeQuery( @@ -1728,7 +1722,7 @@ public class SQLManager implements AbstractDB { @Override public boolean convertFlags() { final Map> flagMap = new HashMap<>(); - try (Statement statement = this.connection.createStatement()) { + try (Statement statement = this.getConnection().createStatement()) { try (ResultSet resultSet = statement .executeQuery("SELECT * FROM `" + this.prefix + "plot_settings`")) { while (resultSet.next()) { @@ -1758,7 +1752,7 @@ public class SQLManager implements AbstractDB { } LOGGER.info("Loaded {} plot flag collections...", flagMap.size()); 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 + "plot_flags`(`plot_id`, `flag`, `value`) VALUES(?, ?, ?)")) { @@ -1843,7 +1837,7 @@ public class SQLManager implements AbstractDB { /* * Getting plots */ - try (Statement statement = this.connection.createStatement()) { + try (Statement statement = this.getConnection().createStatement()) { int id; String o; UUID user; @@ -2177,7 +2171,7 @@ public class SQLManager implements AbstractDB { @Override public PreparedStatement get() throws SQLException { - return SQLManager.this.connection.prepareStatement( + return SQLManager.this.getConnection().prepareStatement( "UPDATE `" + SQLManager.this.prefix + "plot_settings` SET `merged` = ? WHERE `plot_plot_id` = ?"); } @@ -2192,7 +2186,7 @@ public class SQLManager implements AbstractDB { final int id2 = getId(plot2); final PlotId pos1 = plot1.getId(); final PlotId pos2 = plot2.getId(); - try (final PreparedStatement preparedStatement = this.connection.prepareStatement( + try (final PreparedStatement preparedStatement = this.getConnection().prepareStatement( "UPDATE `" + SQLManager.this.prefix + "plot` SET `plot_id_x` = ?, `plot_id_z` = ? WHERE `id` = ?")) { preparedStatement.setInt(1, pos1.getX()); @@ -2227,7 +2221,7 @@ public class SQLManager implements AbstractDB { @Override public PreparedStatement get() throws SQLException { - return SQLManager.this.connection.prepareStatement( + return SQLManager.this.getConnection().prepareStatement( "UPDATE `" + SQLManager.this.prefix + "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(?, ?, ?) " + "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 public PreparedStatement get() throws SQLException { - return SQLManager.this.connection.prepareStatement( + return SQLManager.this.getConnection().prepareStatement( "DELETE FROM `" + SQLManager.this.prefix + "plot_flags` WHERE `plot_id` = ? AND `flag` = ?"); } @@ -2292,7 +2286,7 @@ public class SQLManager implements AbstractDB { @Override public PreparedStatement get() throws SQLException { - return SQLManager.this.connection.prepareStatement( + return SQLManager.this.getConnection().prepareStatement( "UPDATE `" + SQLManager.this.prefix + "plot_settings` SET `alias` = ? WHERE `plot_plot_id` = ?"); } @@ -2329,27 +2323,27 @@ public class SQLManager implements AbstractDB { idstr.append(stmt_prefix).append(id); stmt_prefix = " OR `plot_plot_id` = "; } - PreparedStatement stmt = SQLManager.this.connection.prepareStatement( + PreparedStatement stmt = SQLManager.this.getConnection().prepareStatement( "DELETE FROM `" + SQLManager.this.prefix + "plot_helpers` WHERE `plot_plot_id` = " + idstr); stmt.executeUpdate(); stmt.close(); - stmt = SQLManager.this.connection.prepareStatement( + stmt = SQLManager.this.getConnection().prepareStatement( "DELETE FROM `" + SQLManager.this.prefix + "plot_denied` WHERE `plot_plot_id` = " + idstr); stmt.executeUpdate(); stmt.close(); - stmt = SQLManager.this.connection.prepareStatement( + stmt = SQLManager.this.getConnection().prepareStatement( "DELETE FROM `" + SQLManager.this.prefix + "plot_settings` WHERE `plot_plot_id` = " + idstr); stmt.executeUpdate(); stmt.close(); - stmt = SQLManager.this.connection.prepareStatement( + stmt = SQLManager.this.getConnection().prepareStatement( "DELETE FROM `" + SQLManager.this.prefix + "plot_trusted` WHERE `plot_plot_id` = " + idstr); stmt.executeUpdate(); stmt.close(); - stmt = SQLManager.this.connection.prepareStatement( + stmt = SQLManager.this.getConnection().prepareStatement( "DELETE FROM `" + SQLManager.this.prefix + "plot` WHERE `id` = " + idstr2); stmt.executeUpdate(); @@ -2368,7 +2362,7 @@ public class SQLManager implements AbstractDB { @Override public void purge(final PlotArea area, final Set plots) { 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 + "plot` WHERE `world` = ?")) { stmt.setString(1, area.toString()); @@ -2408,7 +2402,7 @@ public class SQLManager implements AbstractDB { @Override public PreparedStatement get() throws SQLException { - return SQLManager.this.connection.prepareStatement( + return SQLManager.this.getConnection().prepareStatement( "UPDATE `" + SQLManager.this.prefix + "plot_settings` SET `position` = ? WHERE `plot_plot_id` = ?"); } @@ -2436,11 +2430,11 @@ public class SQLManager implements AbstractDB { @Override public PreparedStatement get() throws SQLException { if (plot != null) { - return SQLManager.this.connection.prepareStatement( + return SQLManager.this.getConnection().prepareStatement( "DELETE FROM `" + SQLManager.this.prefix + "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 + "plot_comments` WHERE `comment` = ? AND `inbox` = ? AND `sender` = ?"); } @@ -2464,11 +2458,11 @@ public class SQLManager implements AbstractDB { @Override public PreparedStatement get() throws SQLException { if (plot != null) { - return SQLManager.this.connection.prepareStatement( + return SQLManager.this.getConnection().prepareStatement( "DELETE FROM `" + SQLManager.this.prefix + "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` = ?"); } }); @@ -2494,11 +2488,11 @@ public class SQLManager implements AbstractDB { @Override public PreparedStatement get() throws SQLException { if (plot != null) { - return SQLManager.this.connection.prepareStatement( + return SQLManager.this.getConnection().prepareStatement( "SELECT * FROM `" + SQLManager.this.prefix + "plot_comments` WHERE `world` = ? AND `hashcode` = ? AND `inbox` = ?"); } - return SQLManager.this.connection.prepareStatement( + return SQLManager.this.getConnection().prepareStatement( "SELECT * FROM `" + SQLManager.this.prefix + "plot_comments` WHERE `inbox` = ?"); } @@ -2549,7 +2543,7 @@ public class SQLManager implements AbstractDB { @Override public PreparedStatement get() throws SQLException { - return SQLManager.this.connection.prepareStatement( + return SQLManager.this.getConnection().prepareStatement( "INSERT INTO `" + SQLManager.this.prefix + "plot_comments` (`world`, `hashcode`, `comment`, `inbox`, `timestamp`, `sender`) VALUES(?,?,?,?,?,?)"); } @@ -2567,7 +2561,7 @@ public class SQLManager implements AbstractDB { @Override public PreparedStatement get() throws SQLException { - return SQLManager.this.connection.prepareStatement( + return SQLManager.this.getConnection().prepareStatement( "DELETE FROM `" + SQLManager.this.prefix + "plot_helpers` WHERE `plot_plot_id` = ? AND `user_uuid` = ?"); } @@ -2585,7 +2579,7 @@ public class SQLManager implements AbstractDB { @Override public PreparedStatement get() throws SQLException { - return SQLManager.this.connection.prepareStatement( + return SQLManager.this.getConnection().prepareStatement( "DELETE FROM `" + SQLManager.this.prefix + "plot_trusted` WHERE `plot_plot_id` = ? AND `user_uuid` = ?"); } @@ -2603,7 +2597,7 @@ public class SQLManager implements AbstractDB { @Override public PreparedStatement get() throws SQLException { - return SQLManager.this.connection.prepareStatement( + return SQLManager.this.getConnection().prepareStatement( "INSERT INTO `" + SQLManager.this.prefix + "plot_helpers` (`plot_plot_id`, `user_uuid`) VALUES(?,?)"); } @@ -2621,7 +2615,7 @@ public class SQLManager implements AbstractDB { @Override public PreparedStatement get() throws SQLException { - return SQLManager.this.connection.prepareStatement( + return SQLManager.this.getConnection().prepareStatement( "INSERT INTO `" + SQLManager.this.prefix + "plot_trusted` (`plot_plot_id`, `user_uuid`) VALUES(?,?)"); } @@ -2639,7 +2633,7 @@ public class SQLManager implements AbstractDB { @Override public PreparedStatement get() throws SQLException { - return SQLManager.this.connection.prepareStatement( + return SQLManager.this.getConnection().prepareStatement( "DELETE FROM `" + SQLManager.this.prefix + "plot_denied` WHERE `plot_plot_id` = ? AND `user_uuid` = ?"); } @@ -2657,7 +2651,7 @@ public class SQLManager implements AbstractDB { @Override public PreparedStatement get() throws SQLException { - return SQLManager.this.connection.prepareStatement( + return SQLManager.this.getConnection().prepareStatement( "INSERT INTO `" + SQLManager.this.prefix + "plot_denied` (`plot_plot_id`, `user_uuid`) VALUES(?,?)"); } @@ -2667,7 +2661,7 @@ public class SQLManager implements AbstractDB { @Override public HashMap getRatings(Plot plot) { HashMap map = new HashMap<>(); - try (PreparedStatement statement = this.connection.prepareStatement( + try (PreparedStatement statement = this.getConnection().prepareStatement( "SELECT `rating`, `player` FROM `" + this.prefix + "plot_rating` WHERE `plot_plot_id` = ? ")) { statement.setInt(1, getId(plot)); @@ -2697,7 +2691,7 @@ public class SQLManager implements AbstractDB { @Override public PreparedStatement get() throws SQLException { - return SQLManager.this.connection.prepareStatement( + return SQLManager.this.getConnection().prepareStatement( "INSERT INTO `" + SQLManager.this.prefix + "plot_rating` (`plot_plot_id`, `rating`, `player`) VALUES(?,?,?)"); } @@ -2715,7 +2709,7 @@ public class SQLManager implements AbstractDB { @Override public PreparedStatement get() throws SQLException { - return SQLManager.this.connection.prepareStatement( + return SQLManager.this.getConnection().prepareStatement( "DELETE FROM `" + SQLManager.this.prefix + "cluster_settings` WHERE `cluster_id` = ?"); } @@ -2728,7 +2722,7 @@ public class SQLManager implements AbstractDB { @Override public PreparedStatement get() throws SQLException { - return SQLManager.this.connection.prepareStatement( + return SQLManager.this.getConnection().prepareStatement( "DELETE FROM `" + SQLManager.this.prefix + "cluster_helpers` WHERE `cluster_id` = ?"); } @@ -2741,7 +2735,7 @@ public class SQLManager implements AbstractDB { @Override public PreparedStatement get() throws SQLException { - return SQLManager.this.connection.prepareStatement( + return SQLManager.this.getConnection().prepareStatement( "DELETE FROM `" + SQLManager.this.prefix + "cluster_invited` WHERE `cluster_id` = ?"); } @@ -2754,7 +2748,7 @@ public class SQLManager implements AbstractDB { @Override public PreparedStatement get() throws SQLException { - return SQLManager.this.connection.prepareStatement( + return SQLManager.this.getConnection().prepareStatement( "DELETE FROM `" + SQLManager.this.prefix + "cluster` WHERE `id` = ?"); } }); @@ -2782,11 +2776,11 @@ public class SQLManager implements AbstractDB { @Override public PreparedStatement get() throws SQLException { if (replace) { - return SQLManager.this.connection.prepareStatement( + return SQLManager.this.getConnection().prepareStatement( "UPDATE `" + SQLManager.this.prefix + "player_meta` SET `value` = ? WHERE `uuid` = ? AND `key` = ?"); } else { - return SQLManager.this.connection.prepareStatement( + return SQLManager.this.getConnection().prepareStatement( "INSERT INTO `" + SQLManager.this.prefix + "player_meta`(`uuid`, `key`, `value`) VALUES(?, ? ,?)"); } @@ -2805,7 +2799,7 @@ public class SQLManager implements AbstractDB { @Override public PreparedStatement get() throws SQLException { - return SQLManager.this.connection.prepareStatement( + return SQLManager.this.getConnection().prepareStatement( "DELETE FROM `" + SQLManager.this.prefix + "player_meta` WHERE `uuid` = ? AND `key` = ?"); } @@ -2822,7 +2816,7 @@ public class SQLManager implements AbstractDB { @Override public PreparedStatement get() throws SQLException { - return SQLManager.this.connection.prepareStatement( + return SQLManager.this.getConnection().prepareStatement( "SELECT * FROM `" + SQLManager.this.prefix + "player_meta` WHERE `uuid` = ? ORDER BY `meta_id` ASC"); } @@ -2879,7 +2873,7 @@ public class SQLManager implements AbstractDB { /* * Getting clusters */ - try (Statement stmt = this.connection.createStatement()) { + try (Statement stmt = this.getConnection().createStatement()) { ResultSet resultSet = stmt.executeQuery("SELECT * FROM `" + this.prefix + "cluster`"); PlotCluster cluster; @@ -3010,7 +3004,7 @@ public class SQLManager implements AbstractDB { @Override public PreparedStatement get() throws SQLException { - return SQLManager.this.connection.prepareStatement( + return SQLManager.this.getConnection().prepareStatement( "UPDATE `" + SQLManager.this.prefix + "cluster_settings` SET `alias` = ? WHERE `cluster_id` = ?"); } @@ -3029,7 +3023,7 @@ public class SQLManager implements AbstractDB { @Override public PreparedStatement get() throws SQLException { - return SQLManager.this.connection.prepareStatement( + return SQLManager.this.getConnection().prepareStatement( "DELETE FROM `" + SQLManager.this.prefix + "cluster_helpers` WHERE `cluster_id` = ? AND `user_uuid` = ?"); } @@ -3047,7 +3041,7 @@ public class SQLManager implements AbstractDB { @Override public PreparedStatement get() throws SQLException { - return SQLManager.this.connection.prepareStatement( + return SQLManager.this.getConnection().prepareStatement( "INSERT INTO `" + SQLManager.this.prefix + "cluster_helpers` (`cluster_id`, `user_uuid`) VALUES(?,?)"); } @@ -3069,7 +3063,7 @@ public class SQLManager implements AbstractDB { @Override public PreparedStatement get() throws SQLException { - return SQLManager.this.connection.prepareStatement( + return SQLManager.this.getConnection().prepareStatement( SQLManager.this.CREATE_CLUSTER, Statement.RETURN_GENERATED_KEYS ); @@ -3100,7 +3094,7 @@ public class SQLManager implements AbstractDB { @Override public PreparedStatement get() throws SQLException { - return SQLManager.this.connection.prepareStatement( + return SQLManager.this.getConnection().prepareStatement( "INSERT INTO `" + SQLManager.this.prefix + "cluster_settings`(`cluster_id`, `alias`) VALUES(?, ?)"); } @@ -3127,7 +3121,7 @@ public class SQLManager implements AbstractDB { @Override public PreparedStatement get() throws SQLException { - return SQLManager.this.connection.prepareStatement( + return SQLManager.this.getConnection().prepareStatement( "UPDATE `" + SQLManager.this.prefix + "cluster` SET `pos1_x` = ?, `pos1_z` = ?, `pos2_x` = ?, `pos2_z` = ? WHERE `id` = ?"); } @@ -3145,7 +3139,7 @@ public class SQLManager implements AbstractDB { @Override public PreparedStatement get() throws SQLException { - return SQLManager.this.connection.prepareStatement( + return SQLManager.this.getConnection().prepareStatement( "UPDATE `" + SQLManager.this.prefix + "cluster_settings` SET `position` = ? WHERE `cluster_id` = ?"); } @@ -3163,7 +3157,7 @@ public class SQLManager implements AbstractDB { @Override public PreparedStatement get() throws SQLException { - return SQLManager.this.connection.prepareStatement( + return SQLManager.this.database.getConnection().prepareStatement( "DELETE FROM `" + SQLManager.this.prefix + "cluster_invited` WHERE `cluster_id` = ? AND `user_uuid` = ?"); } @@ -3181,7 +3175,7 @@ public class SQLManager implements AbstractDB { @Override public PreparedStatement get() throws SQLException { - return SQLManager.this.connection.prepareStatement( + return SQLManager.this.getConnection().prepareStatement( "INSERT INTO `" + SQLManager.this.prefix + "cluster_invited` (`cluster_id`, `user_uuid`) VALUES(?,?)"); } @@ -3190,12 +3184,11 @@ public class SQLManager implements AbstractDB { @Override public boolean deleteTables() { - try (Statement stmt = this.connection.createStatement(); - PreparedStatement statement = this.connection + try (Statement stmt = this.getConnection().createStatement(); + PreparedStatement statement = this.getConnection() .prepareStatement("DROP TABLE `" + this.prefix + "plot`")) { close(); this.closed = false; - SQLManager.this.connection = this.database.forceConnection(); stmt.addBatch("DROP TABLE `" + this.prefix + "cluster_invited`"); stmt.addBatch("DROP TABLE `" + this.prefix + "cluster_helpers`"); stmt.addBatch("DROP TABLE `" + this.prefix + "cluster`"); @@ -3208,7 +3201,7 @@ public class SQLManager implements AbstractDB { stmt.executeBatch(); stmt.clearBatch(); statement.executeUpdate(); - } catch (ClassNotFoundException | SQLException e) { + } catch (SQLException e) { e.printStackTrace(); } @@ -3230,8 +3223,8 @@ public class SQLManager implements AbstractDB { } } try { - if (this.connection.getAutoCommit()) { - this.connection.setAutoCommit(false); + if (this.getConnection().getAutoCommit()) { + this.getConnection().setAutoCommit(false); } } catch (SQLException e) { e.printStackTrace(); @@ -3350,7 +3343,7 @@ public class SQLManager implements AbstractDB { ) { addGlobalTask(() -> { if (min == null) { - try (PreparedStatement stmt = SQLManager.this.connection.prepareStatement( + try (PreparedStatement stmt = SQLManager.this.getConnection().prepareStatement( "UPDATE `" + SQLManager.this.prefix + "plot` SET `world` = ? WHERE `world` = ?")) { stmt.setString(1, newWorld); @@ -3359,7 +3352,7 @@ public class SQLManager implements AbstractDB { } catch (SQLException e) { e.printStackTrace(); } - try (PreparedStatement stmt = SQLManager.this.connection.prepareStatement( + try (PreparedStatement stmt = SQLManager.this.getConnection().prepareStatement( "UPDATE `" + SQLManager.this.prefix + "cluster` SET `world` = ? WHERE `world` = ?")) { stmt.setString(1, newWorld); @@ -3369,7 +3362,7 @@ public class SQLManager implements AbstractDB { e.printStackTrace(); } } else { - try (PreparedStatement stmt = SQLManager.this.connection.prepareStatement( + try (PreparedStatement stmt = SQLManager.this.getConnection().prepareStatement( "UPDATE `" + SQLManager.this.prefix + "plot` SET `world` = ? WHERE `world` = ? AND `plot_id_x` BETWEEN ? AND ? AND `plot_id_z` BETWEEN ? AND ?")) { stmt.setString(1, newWorld); @@ -3382,7 +3375,7 @@ public class SQLManager implements AbstractDB { } catch (SQLException e) { e.printStackTrace(); } - try (PreparedStatement stmt = SQLManager.this.connection.prepareStatement( + try (PreparedStatement stmt = SQLManager.this.getConnection().prepareStatement( "UPDATE `" + SQLManager.this.prefix + "cluster` SET `world` = ? WHERE `world` = ? AND `pos1_x` <= ? AND `pos1_z` <= ? AND `pos2_x` >= ? AND `pos2_z` >= ?")) { stmt.setString(1, newWorld); @@ -3402,7 +3395,7 @@ public class SQLManager implements AbstractDB { @Override public void replaceUUID(final UUID old, final UUID now) { addGlobalTask(() -> { - try (Statement stmt = SQLManager.this.connection.createStatement()) { + try (Statement stmt = SQLManager.this.getConnection().createStatement()) { stmt.executeUpdate( "UPDATE `" + SQLManager.this.prefix + "cluster` SET `owner` = '" + now .toString() + "' WHERE `owner` = '" + old.toString() + '\''); @@ -3431,7 +3424,7 @@ public class SQLManager implements AbstractDB { public void close() { try { this.closed = true; - this.connection.close(); + this.getConnection().close(); } catch (SQLException e) { e.printStackTrace(); } diff --git a/Core/src/main/java/com/plotsquared/core/database/SQLite.java b/Core/src/main/java/com/plotsquared/core/database/SQLite.java index c34be4c59..67d634d7b 100644 --- a/Core/src/main/java/com/plotsquared/core/database/SQLite.java +++ b/Core/src/main/java/com/plotsquared/core/database/SQLite.java @@ -19,13 +19,14 @@ package com.plotsquared.core.database; 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.Logger; import java.io.File; import java.io.IOException; import java.sql.Connection; -import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; @@ -36,9 +37,7 @@ import java.sql.Statement; public class SQLite extends Database { private static final Logger LOGGER = LogManager.getLogger("PlotSquared/" + SQLite.class.getSimpleName()); - - private final String dbLocation; - private Connection connection; + private final HikariDataSource hikariDataSource; /** * Creates a new SQLite instance @@ -46,18 +45,10 @@ public class SQLite extends Database { * @param dbLocation Location of the Database (Must end in .db) */ 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()) { PlotSquared.platform().getDirectory().mkdirs(); } - File file = new File(this.dbLocation); + File file = dbLocation; if (!file.exists()) { try { file.createNewFile(); @@ -65,56 +56,47 @@ public class SQLite extends Database { LOGGER.error("Unable to create database"); } } - Class.forName("org.sqlite.JDBC"); - this.connection = DriverManager.getConnection("jdbc:sqlite:" + this.dbLocation); - return this.connection; - } - - @Override - public boolean checkConnection() throws SQLException { - return (this.connection != null) && !this.connection.isClosed(); + try { + Class.forName("org.sqlite.JDBC"); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } + HikariConfig config = new HikariConfig(); + config.setJdbcUrl("jdbc:sqlite:" + dbLocation); + config.setDriverClassName("org.sqlite.JDBC"); + this.hikariDataSource = new HikariDataSource(config); } @Override public Connection getConnection() { - return this.connection; + try { + return this.hikariDataSource.getConnection(); + } catch (SQLException e) { + throw new RuntimeException(e); + } } @Override public boolean closeConnection() throws SQLException { - if (this.connection == null) { + if (this.hikariDataSource == null) { return false; } - this.connection.close(); - this.connection = null; + this.hikariDataSource.close(); return true; } @Override - public ResultSet querySQL(String query) throws SQLException, ClassNotFoundException { - if (checkConnection()) { - openConnection(); - } - try (Statement statement = this.connection.createStatement()) { + public ResultSet querySQL(String query) throws SQLException { + try (Statement statement = this.getConnection().createStatement()) { return statement.executeQuery(query); } } @Override - public int updateSQL(String query) throws SQLException, ClassNotFoundException { - if (checkConnection()) { - openConnection(); - } - try (Statement statement = this.connection.createStatement()) { + public int updateSQL(String query) throws SQLException { + try (Statement statement = this.getConnection().createStatement()) { 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; - } - } diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 5af6d937b..d7a3afa22 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -31,6 +31,7 @@ paperlib = "1.0.8" informative-annotations = "1.4" vault = "1.7.1" serverlib = "2.3.4" +hikaricp = "5.0.1" # Gradle plugins shadow = "8.1.1" @@ -77,6 +78,7 @@ informativeAnnotations = { group = "com.intellectualsites.informative-annotation paperlib = { group = "io.papermc", name = "paperlib", version.ref = "paperlib" } vault = { group = "com.github.MilkBowl", name = "VaultAPI", version.ref = "vault" } serverlib = { group = "dev.notmyfault.serverlib", name = "ServerLib", version.ref = "serverlib" } +hikaricp = { group = "com.zaxxer", name = "HikariCP", version.ref = "hikaricp"} [plugins] shadow = { id = "com.github.johnrengelman.shadow", version.ref = "shadow" }