From da9043100f4c57319ccf7bd3ec0df0b3583fd793 Mon Sep 17 00:00:00 2001 From: GeorgH93 Date: Mon, 30 Jan 2017 08:00:43 +0100 Subject: [PATCH] Switch MySQL table creation to DBTools Allways store the last modify date for backpack Disable cooldown sync for SQLite Add cooldown sync table for MySQL --- .../Minepacks/Bukkit/Database/MySQL.java | 66 ++++--------------- .../Minepacks/Bukkit/Database/SQL.java | 35 ++++++---- .../Minepacks/Bukkit/Database/SQLite.java | 14 ++-- 3 files changed, 38 insertions(+), 77 deletions(-) diff --git a/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/MySQL.java b/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/MySQL.java index 5e9d6a8..3b71758 100644 --- a/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/MySQL.java +++ b/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/MySQL.java @@ -17,14 +17,13 @@ package at.pcgamingfreaks.Minepacks.Bukkit.Database; +import at.pcgamingfreaks.Database.DBTools; import at.pcgamingfreaks.Minepacks.Bukkit.Minepacks; import com.zaxxer.hikari.HikariConfig; import java.sql.Connection; -import java.sql.ResultSet; import java.sql.SQLException; -import java.sql.Statement; public class MySQL extends SQL { @@ -56,60 +55,17 @@ protected void updateQuerysForDialect() @Override protected void checkDB() { - try(Connection connection = getConnection(); Statement stmt = connection.createStatement()) + try(Connection connection = getConnection()) { - ResultSet res; - if(useUUIDs) - { - stmt.execute("CREATE TABLE IF NOT EXISTS `" + tablePlayers + "` (`" + fieldPlayerID + "` INT UNSIGNED NOT NULL AUTO_INCREMENT,`" + fieldPlayerName + "` CHAR(16) NOT NULL,`" + fieldPlayerUUID + "` CHAR(36) UNIQUE, PRIMARY KEY (`" + fieldPlayerID + "`));"); - res = stmt.executeQuery("SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = '" + tablePlayers + "' AND COLUMN_NAME = '" + fieldPlayerUUID + "';"); - if(!res.next()) - { - stmt.execute("ALTER TABLE `" + tablePlayers + "` ADD COLUMN `" + fieldPlayerUUID + "` CHAR(36) UNIQUE;"); - } - res.close(); - res = stmt.executeQuery("SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = '" + tablePlayers + "' AND COLUMN_NAME = '" + fieldPlayerName + "' AND COLUMN_KEY='UNI';"); - if(res.next()) - { - stmt.execute("ALTER TABLE `" + tablePlayers + "` DROP INDEX `" + fieldPlayerName + "_UNIQUE`;"); - } - res.close(); - if(useUUIDSeparators) - { - res = stmt.executeQuery("SELECT CHARACTER_MAXIMUM_LENGTH FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = '" + tablePlayers + "' AND COLUMN_NAME = '" + fieldPlayerUUID + "';"); - if(res.next() && res.getInt(1) < 36) - { - stmt.execute("ALTER TABLE `" + tablePlayers + "` MODIFY `" + fieldPlayerUUID + "` CHAR(36) UNIQUE;"); - } - res.close(); - } - } - else - { - stmt.execute("CREATE TABLE IF NOT EXISTS `" + tablePlayers + "` (`" + fieldPlayerID + "` INT UNSIGNED NOT NULL AUTO_INCREMENT,`" + fieldPlayerName + "` CHAR(16) NOT NULL, PRIMARY KEY (`" + fieldPlayerID + "`));"); - res = stmt.executeQuery("SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = '" + tablePlayers + "' AND COLUMN_NAME = '" + fieldPlayerName + "' AND COLUMN_KEY='UNI';"); - if(!res.next()) - { - stmt.execute("ALTER TABLE `" + tablePlayers + "` ADD UNIQUE INDEX `" + fieldPlayerName + "_UNIQUE` (`" + fieldPlayerName + "` ASC);"); - } - res.close(); - } - stmt.execute("CREATE TABLE IF NOT EXISTS `" + tableBackpacks + "` (`" + fieldBpOwner + "` INT UNSIGNED NOT NULL, `" + fieldBpIts + "` BLOB, `" - + fieldBpVersion + "` INT DEFAULT 0, " + ((maxAge > 0) ? "`" + fieldBpLastUpdate + "` TIMESTAMP DEFAULT CURRENT_TIMESTAMP, " : "") + "PRIMARY KEY (`" + fieldBpOwner + "`));"); - res = stmt.executeQuery("SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = '" + tableBackpacks + "' AND COLUMN_NAME = '" + fieldBpVersion + "';"); - if(!res.next()) - { - stmt.execute("ALTER TABLE `" + tableBackpacks + "` ADD COLUMN `" + fieldBpVersion + "` INT DEFAULT 0;"); - } - if(maxAge > 0) - { - res = stmt.executeQuery("SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = '" + tableBackpacks + "' AND COLUMN_NAME = '" + fieldBpLastUpdate + "';"); - if(!res.next()) - { - stmt.execute("ALTER TABLE `" + tableBackpacks + "` ADD COLUMN `" + fieldBpLastUpdate + "` TIMESTAMP DEFAULT CURRENT_TIMESTAMP;"); - } - res.close(); - } + DBTools.updateDB(connection, replacePlaceholders("CREATE TABLE {TablePlayers} (\n{FieldPlayerID} INT UNSIGNED NOT NULL AUTO_INCREMENT,\n{FieldName} CHAR(16) NOT NULL,\n{FieldUUID} CHAR(36) DEFAULT NULL," + + "\nPRIMARY KEY ({FieldPlayerID}),\nUNIQUE INDEX {FieldUUID}_UNIQUE ({FieldUUID})\n);")); + DBTools.updateDB(connection, replacePlaceholders("CREATE TABLE {TableBackpacks} (\n{FieldBPOwner} INT UNSIGNED NOT NULL,\n{FieldBPITS} BLOB,\n{FieldBPVersion} INT DEFAULT 0,\n" + + "{FieldBPLastUpdate} TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n" + + "PRIMARY KEY ({FieldBPOwner}),\nCONSTRAINT fk_{TableBackpacks}_{TablePlayers}_{FieldBPOwner} FOREIGN KEY ({FieldBPOwner}) " + + "REFERENCES {TablePlayers} ({FieldPlayerID}) ON DELETE CASCADE ON UPDATE CASCADE\n);")); + DBTools.updateDB(connection, replacePlaceholders("CREATE TABLE {TableCooldowns} (\n{FieldCDPlayer} INT UNSIGNED,\n{FieldCDTime} LONG NOT NULL\nPRIMARY KEY({FieldCDPlayer}),\n" + + "CONSTRAINT fk_{TableCooldowns}_{TablePlayers}_{FieldCDPlayer} FOREIGN KEY ({FieldCDPlayer}) " + + "REFERENCES {TablePlayers} ({FieldPlayerID}) ON DELETE CASCADE ON UPDATE CASCADE\n);")); } catch (SQLException e) { diff --git a/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/SQL.java b/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/SQL.java index 6e11260..8a7f4ae 100644 --- a/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/SQL.java +++ b/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/SQL.java @@ -37,12 +37,13 @@ public abstract class SQL extends Database { - //TODO test sync cooldown + //TODO load cooldown private HikariDataSource dataSource; protected String tablePlayers, tableBackpacks, tableCooldowns; // Table Names protected String fieldPlayerName, fieldPlayerID, fieldPlayerUUID, fieldBpOwner, fieldBpIts, fieldBpVersion, fieldBpLastUpdate, fieldCdPlayer, fieldCdTime; // Table Fields protected String queryUpdatePlayerAdd, queryGetPlayerID, queryInsertBp, queryUpdateBp, queryGetBP, queryDeleteOldBackpacks, queryGetUnsetOrInvalidUUIDs, queryFixUUIDs, querySyncCooldown; // DB Querys + protected String queryDeleteOldCooldowns, queryGetCooldown; // DB Querys protected boolean updatePlayer, syncCooldown; public SQL(Minepacks mp) @@ -67,12 +68,21 @@ public SQL(Minepacks mp) // Delete old backpacks if(maxAge > 0) { - try + try(Connection connection = getConnection(); Statement statement = connection.createStatement()) { - try(Connection connection = getConnection(); Statement statement = connection.createStatement()) - { - statement.execute(queryDeleteOldBackpacks); - } + statement.execute(queryDeleteOldBackpacks); + } + catch(SQLException e) + { + e.printStackTrace(); + } + } + // Delete old cooldowns + if(syncCooldown) + { + try(Connection connection = getConnection()) + { + DBTools.runStatement(connection, queryDeleteOldCooldowns, System.currentTimeMillis()); } catch(SQLException e) { @@ -203,12 +213,7 @@ protected final void buildQuerys() querySyncCooldown = "INSERT INTO {TableCooldowns} ({FieldCDPlayer},{FieldCDTime}) SELECT {FieldPlayerID},? FROM {TablePlayers} WHERE {FieldName}=?;"; } queryInsertBp = "INSERT INTO {TableBackpacks} ({FieldBPOwner},{FieldBPITS},{FieldBPVersion}) VALUES (?,?,?);"; - queryUpdateBp = "UPDATE {TableBackpacks} SET {FieldBPITS}=?,{FieldBPVersion}=?"; - if(maxAge > 0) - { - queryUpdateBp += ",{FieldBPLastUpdate}={NOW}"; - } - queryUpdateBp += " WHERE {FieldBPOwner}=?;"; + queryUpdateBp = "UPDATE {TableBackpacks} SET {FieldBPITS}=?,{FieldBPVersion}=?,{FieldBPLastUpdate}={NOW} WHERE {FieldBPOwner}=?;"; queryDeleteOldBackpacks = "DELETE FROM {TableBackpacks} WHERE {FieldBPLastUpdate} < DATE('now', '-{VarMaxAge} days')"; if(useUUIDSeparators) { @@ -219,6 +224,7 @@ protected final void buildQuerys() queryGetUnsetOrInvalidUUIDs = "SELECT {FieldPlayerID},{FieldName},{FieldUUID} FROM {TablePlayers} WHERE {FieldUUID} IS NULL OR {FieldUUID} LIKE '%-%';"; } queryFixUUIDs = "UPDATE {TablePlayers} SET {FieldUUID}=? WHERE {FieldPlayerID}=?;"; + queryDeleteOldCooldowns = "DELETE FROM {TableCooldowns} WHERE {FieldCDTime} 0) + try { - try - { - ResultSet rs = stmt.executeQuery("SELECT DATE('now');"); - rs.next(); - stmt.execute("ALTER TABLE `backpacks` ADD COLUMN `lastupdate` DATE DEFAULT '" + rs.getString(1) + "';"); - } - catch(SQLException ignored) {} + ResultSet rs = stmt.executeQuery("SELECT DATE('now');"); + rs.next(); + stmt.execute("ALTER TABLE `backpacks` ADD COLUMN `lastupdate` DATE DEFAULT '" + rs.getString(1) + "';"); } + catch(SQLException ignored) {} } catch(SQLException e) {