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:
GeorgH93 2017-01-30 08:00:43 +01:00
parent d82bea094f
commit da9043100f
3 changed files with 38 additions and 77 deletions

View File

@ -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)
{

View File

@ -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}<?;";
updateQuerysForDialect();
@ -231,12 +237,13 @@ protected void setTableAndFieldNames()
queryUpdatePlayerAdd = replacePlaceholders(queryUpdatePlayerAdd);
queryGetPlayerID = replacePlaceholders(queryGetPlayerID);
queryGetBP = replacePlaceholders(queryGetBP);
queryInsertBp = replacePlaceholders(queryInsertBp);
queryUpdateBp = replacePlaceholders(queryUpdateBp);
queryInsertBp = replacePlaceholders(queryInsertBp);
queryUpdateBp = replacePlaceholders(queryUpdateBp);
queryFixUUIDs = replacePlaceholders(queryFixUUIDs);
queryDeleteOldBackpacks = replacePlaceholders(queryDeleteOldBackpacks.replaceAll("\\{VarMaxAge}", maxAge + ""));
queryGetUnsetOrInvalidUUIDs = replacePlaceholders(queryGetUnsetOrInvalidUUIDs);
querySyncCooldown = replacePlaceholders(querySyncCooldown);
queryDeleteOldCooldowns = replacePlaceholders(queryDeleteOldCooldowns);
}
protected abstract void updateQuerysForDialect();

View File

@ -59,6 +59,7 @@ protected void loadSettings()
// Set fixed settings
useUUIDSeparators = false;
updatePlayer = true;
syncCooldown = false;
}
@Override
@ -119,16 +120,13 @@ protected void checkDB()
stmt.execute("ALTER TABLE `backpacks` ADD COLUMN `version` INT DEFAULT 0;");
}
catch(SQLException ignored) {}
if(maxAge > 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)
{