mirror of
https://github.com/GeorgH93/Minepacks.git
synced 2025-01-03 18:47:43 +01:00
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
This commit is contained in:
parent
d82bea094f
commit
da9043100f
@ -17,14 +17,13 @@
|
|||||||
|
|
||||||
package at.pcgamingfreaks.Minepacks.Bukkit.Database;
|
package at.pcgamingfreaks.Minepacks.Bukkit.Database;
|
||||||
|
|
||||||
|
import at.pcgamingfreaks.Database.DBTools;
|
||||||
import at.pcgamingfreaks.Minepacks.Bukkit.Minepacks;
|
import at.pcgamingfreaks.Minepacks.Bukkit.Minepacks;
|
||||||
|
|
||||||
import com.zaxxer.hikari.HikariConfig;
|
import com.zaxxer.hikari.HikariConfig;
|
||||||
|
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.sql.ResultSet;
|
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.sql.Statement;
|
|
||||||
|
|
||||||
public class MySQL extends SQL
|
public class MySQL extends SQL
|
||||||
{
|
{
|
||||||
@ -56,60 +55,17 @@ protected void updateQuerysForDialect()
|
|||||||
@Override
|
@Override
|
||||||
protected void checkDB()
|
protected void checkDB()
|
||||||
{
|
{
|
||||||
try(Connection connection = getConnection(); Statement stmt = connection.createStatement())
|
try(Connection connection = getConnection())
|
||||||
{
|
{
|
||||||
ResultSet res;
|
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," +
|
||||||
if(useUUIDs)
|
"\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" +
|
||||||
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 + "`));");
|
"{FieldBPLastUpdate} TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n" +
|
||||||
res = stmt.executeQuery("SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = '" + tablePlayers + "' AND COLUMN_NAME = '" + fieldPlayerUUID + "';");
|
"PRIMARY KEY ({FieldBPOwner}),\nCONSTRAINT fk_{TableBackpacks}_{TablePlayers}_{FieldBPOwner} FOREIGN KEY ({FieldBPOwner}) " +
|
||||||
if(!res.next())
|
"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" +
|
||||||
stmt.execute("ALTER TABLE `" + tablePlayers + "` ADD COLUMN `" + fieldPlayerUUID + "` CHAR(36) UNIQUE;");
|
"CONSTRAINT fk_{TableCooldowns}_{TablePlayers}_{FieldCDPlayer} FOREIGN KEY ({FieldCDPlayer}) " +
|
||||||
}
|
"REFERENCES {TablePlayers} ({FieldPlayerID}) ON DELETE CASCADE ON UPDATE CASCADE\n);"));
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
catch (SQLException e)
|
catch (SQLException e)
|
||||||
{
|
{
|
||||||
|
@ -37,12 +37,13 @@
|
|||||||
|
|
||||||
public abstract class SQL extends Database
|
public abstract class SQL extends Database
|
||||||
{
|
{
|
||||||
//TODO test sync cooldown
|
//TODO load cooldown
|
||||||
private HikariDataSource dataSource;
|
private HikariDataSource dataSource;
|
||||||
|
|
||||||
protected String tablePlayers, tableBackpacks, tableCooldowns; // Table Names
|
protected String tablePlayers, tableBackpacks, tableCooldowns; // Table Names
|
||||||
protected String fieldPlayerName, fieldPlayerID, fieldPlayerUUID, fieldBpOwner, fieldBpIts, fieldBpVersion, fieldBpLastUpdate, fieldCdPlayer, fieldCdTime; // Table Fields
|
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 queryUpdatePlayerAdd, queryGetPlayerID, queryInsertBp, queryUpdateBp, queryGetBP, queryDeleteOldBackpacks, queryGetUnsetOrInvalidUUIDs, queryFixUUIDs, querySyncCooldown; // DB Querys
|
||||||
|
protected String queryDeleteOldCooldowns, queryGetCooldown; // DB Querys
|
||||||
protected boolean updatePlayer, syncCooldown;
|
protected boolean updatePlayer, syncCooldown;
|
||||||
|
|
||||||
public SQL(Minepacks mp)
|
public SQL(Minepacks mp)
|
||||||
@ -67,12 +68,21 @@ public SQL(Minepacks mp)
|
|||||||
// Delete old backpacks
|
// Delete old backpacks
|
||||||
if(maxAge > 0)
|
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)
|
catch(SQLException e)
|
||||||
{
|
{
|
||||||
@ -203,12 +213,7 @@ protected final void buildQuerys()
|
|||||||
querySyncCooldown = "INSERT INTO {TableCooldowns} ({FieldCDPlayer},{FieldCDTime}) SELECT {FieldPlayerID},? FROM {TablePlayers} WHERE {FieldName}=?;";
|
querySyncCooldown = "INSERT INTO {TableCooldowns} ({FieldCDPlayer},{FieldCDTime}) SELECT {FieldPlayerID},? FROM {TablePlayers} WHERE {FieldName}=?;";
|
||||||
}
|
}
|
||||||
queryInsertBp = "INSERT INTO {TableBackpacks} ({FieldBPOwner},{FieldBPITS},{FieldBPVersion}) VALUES (?,?,?);";
|
queryInsertBp = "INSERT INTO {TableBackpacks} ({FieldBPOwner},{FieldBPITS},{FieldBPVersion}) VALUES (?,?,?);";
|
||||||
queryUpdateBp = "UPDATE {TableBackpacks} SET {FieldBPITS}=?,{FieldBPVersion}=?";
|
queryUpdateBp = "UPDATE {TableBackpacks} SET {FieldBPITS}=?,{FieldBPVersion}=?,{FieldBPLastUpdate}={NOW} WHERE {FieldBPOwner}=?;";
|
||||||
if(maxAge > 0)
|
|
||||||
{
|
|
||||||
queryUpdateBp += ",{FieldBPLastUpdate}={NOW}";
|
|
||||||
}
|
|
||||||
queryUpdateBp += " WHERE {FieldBPOwner}=?;";
|
|
||||||
queryDeleteOldBackpacks = "DELETE FROM {TableBackpacks} WHERE {FieldBPLastUpdate} < DATE('now', '-{VarMaxAge} days')";
|
queryDeleteOldBackpacks = "DELETE FROM {TableBackpacks} WHERE {FieldBPLastUpdate} < DATE('now', '-{VarMaxAge} days')";
|
||||||
if(useUUIDSeparators)
|
if(useUUIDSeparators)
|
||||||
{
|
{
|
||||||
@ -219,6 +224,7 @@ protected final void buildQuerys()
|
|||||||
queryGetUnsetOrInvalidUUIDs = "SELECT {FieldPlayerID},{FieldName},{FieldUUID} FROM {TablePlayers} WHERE {FieldUUID} IS NULL OR {FieldUUID} LIKE '%-%';";
|
queryGetUnsetOrInvalidUUIDs = "SELECT {FieldPlayerID},{FieldName},{FieldUUID} FROM {TablePlayers} WHERE {FieldUUID} IS NULL OR {FieldUUID} LIKE '%-%';";
|
||||||
}
|
}
|
||||||
queryFixUUIDs = "UPDATE {TablePlayers} SET {FieldUUID}=? WHERE {FieldPlayerID}=?;";
|
queryFixUUIDs = "UPDATE {TablePlayers} SET {FieldUUID}=? WHERE {FieldPlayerID}=?;";
|
||||||
|
queryDeleteOldCooldowns = "DELETE FROM {TableCooldowns} WHERE {FieldCDTime}<?;";
|
||||||
|
|
||||||
updateQuerysForDialect();
|
updateQuerysForDialect();
|
||||||
|
|
||||||
@ -231,12 +237,13 @@ protected void setTableAndFieldNames()
|
|||||||
queryUpdatePlayerAdd = replacePlaceholders(queryUpdatePlayerAdd);
|
queryUpdatePlayerAdd = replacePlaceholders(queryUpdatePlayerAdd);
|
||||||
queryGetPlayerID = replacePlaceholders(queryGetPlayerID);
|
queryGetPlayerID = replacePlaceholders(queryGetPlayerID);
|
||||||
queryGetBP = replacePlaceholders(queryGetBP);
|
queryGetBP = replacePlaceholders(queryGetBP);
|
||||||
queryInsertBp = replacePlaceholders(queryInsertBp);
|
queryInsertBp = replacePlaceholders(queryInsertBp);
|
||||||
queryUpdateBp = replacePlaceholders(queryUpdateBp);
|
queryUpdateBp = replacePlaceholders(queryUpdateBp);
|
||||||
queryFixUUIDs = replacePlaceholders(queryFixUUIDs);
|
queryFixUUIDs = replacePlaceholders(queryFixUUIDs);
|
||||||
queryDeleteOldBackpacks = replacePlaceholders(queryDeleteOldBackpacks.replaceAll("\\{VarMaxAge}", maxAge + ""));
|
queryDeleteOldBackpacks = replacePlaceholders(queryDeleteOldBackpacks.replaceAll("\\{VarMaxAge}", maxAge + ""));
|
||||||
queryGetUnsetOrInvalidUUIDs = replacePlaceholders(queryGetUnsetOrInvalidUUIDs);
|
queryGetUnsetOrInvalidUUIDs = replacePlaceholders(queryGetUnsetOrInvalidUUIDs);
|
||||||
querySyncCooldown = replacePlaceholders(querySyncCooldown);
|
querySyncCooldown = replacePlaceholders(querySyncCooldown);
|
||||||
|
queryDeleteOldCooldowns = replacePlaceholders(queryDeleteOldCooldowns);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected abstract void updateQuerysForDialect();
|
protected abstract void updateQuerysForDialect();
|
||||||
|
@ -59,6 +59,7 @@ protected void loadSettings()
|
|||||||
// Set fixed settings
|
// Set fixed settings
|
||||||
useUUIDSeparators = false;
|
useUUIDSeparators = false;
|
||||||
updatePlayer = true;
|
updatePlayer = true;
|
||||||
|
syncCooldown = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -119,16 +120,13 @@ protected void checkDB()
|
|||||||
stmt.execute("ALTER TABLE `backpacks` ADD COLUMN `version` INT DEFAULT 0;");
|
stmt.execute("ALTER TABLE `backpacks` ADD COLUMN `version` INT DEFAULT 0;");
|
||||||
}
|
}
|
||||||
catch(SQLException ignored) {}
|
catch(SQLException ignored) {}
|
||||||
if(maxAge > 0)
|
try
|
||||||
{
|
{
|
||||||
try
|
ResultSet rs = stmt.executeQuery("SELECT DATE('now');");
|
||||||
{
|
rs.next();
|
||||||
ResultSet rs = stmt.executeQuery("SELECT DATE('now');");
|
stmt.execute("ALTER TABLE `backpacks` ADD COLUMN `lastupdate` DATE DEFAULT '" + rs.getString(1) + "';");
|
||||||
rs.next();
|
|
||||||
stmt.execute("ALTER TABLE `backpacks` ADD COLUMN `lastupdate` DATE DEFAULT '" + rs.getString(1) + "';");
|
|
||||||
}
|
|
||||||
catch(SQLException ignored) {}
|
|
||||||
}
|
}
|
||||||
|
catch(SQLException ignored) {}
|
||||||
}
|
}
|
||||||
catch(SQLException e)
|
catch(SQLException e)
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user