Cleanup SQLite database backend

This commit is contained in:
GeorgH93 2021-03-19 19:52:08 +01:00
parent 77163441a7
commit 8caad77c93
No known key found for this signature in database
GPG Key ID: D1630D37F9E4B3C8
2 changed files with 35 additions and 36 deletions

View File

@ -41,7 +41,10 @@ public abstract class SQL extends DatabaseBackend
private final ConnectionProvider dataSource;
protected String tablePlayers = "backpack_players", tableBackpacks = "backpacks", tableCooldowns = "backpack_cooldowns"; // Table Names
protected String fieldPlayerName, fieldPlayerID, fieldPlayerUUID, fieldBpOwner, fieldBpIts, fieldBpVersion, fieldBpLastUpdate, fieldCdPlayer, fieldCdTime; // Table Fields
protected String fieldPlayerName = "name", fieldPlayerID = "id", fieldPlayerUUID = "uuid"; // Table players
protected String fieldBpOwner = "owner", fieldBpIts = "its", fieldBpVersion = "version", fieldBpLastUpdate = "lastupdate"; // Table Backpack
protected String fieldCdPlayer = "id", fieldCdTime = "time"; // Table Fields
@Language("SQL") protected String queryUpdatePlayerAdd, queryInsertBp, queryUpdateBp, queryGetPlayer, queryGetBP, queryDeleteOldBackpacks, queryGetUnsetOrInvalidUUIDs, queryFixUUIDs; // DB Querys
@Language("SQL") protected String queryDeleteOldCooldowns, querySyncCooldown, queryGetCooldown; // DB Querys
protected boolean syncCooldown;
@ -90,15 +93,15 @@ protected void loadSettings()
tablePlayers = plugin.getConfiguration().getDBTable("User", tablePlayers);
tableBackpacks = plugin.getConfiguration().getDBTable("Backpack", tableBackpacks);
tableCooldowns = plugin.getConfiguration().getDBTable("Cooldown", tableCooldowns);
fieldPlayerID = plugin.getConfiguration().getDBFields("User.Player_ID", "id");
fieldPlayerName = plugin.getConfiguration().getDBFields("User.Name", "name");
fieldPlayerUUID = plugin.getConfiguration().getDBFields("User.UUID", "uuid");
fieldBpOwner = plugin.getConfiguration().getDBFields("Backpack.Owner_ID", "owner");
fieldBpIts = plugin.getConfiguration().getDBFields("Backpack.ItemStacks", "its");
fieldBpVersion = plugin.getConfiguration().getDBFields("Backpack.Version", "version");
fieldBpLastUpdate = plugin.getConfiguration().getDBFields("Backpack.LastUpdate", "lastUpdate");
fieldCdPlayer = plugin.getConfiguration().getDBFields("Cooldown.Player_ID", "id");
fieldCdTime = plugin.getConfiguration().getDBFields("Cooldown.Time", "time");
fieldPlayerID = plugin.getConfiguration().getDBFields("User.Player_ID", fieldPlayerID);
fieldPlayerName = plugin.getConfiguration().getDBFields("User.Name", fieldPlayerName);
fieldPlayerUUID = plugin.getConfiguration().getDBFields("User.UUID", fieldPlayerUUID);
fieldBpOwner = plugin.getConfiguration().getDBFields("Backpack.Owner_ID", fieldBpOwner);
fieldBpIts = plugin.getConfiguration().getDBFields("Backpack.ItemStacks", fieldBpIts);
fieldBpVersion = plugin.getConfiguration().getDBFields("Backpack.Version", fieldBpVersion);
fieldBpLastUpdate = plugin.getConfiguration().getDBFields("Backpack.LastUpdate", fieldBpLastUpdate);
fieldCdPlayer = plugin.getConfiguration().getDBFields("Cooldown.Player_ID", fieldCdPlayer);
fieldCdTime = plugin.getConfiguration().getDBFields("Cooldown.Time", fieldCdTime);
syncCooldown = plugin.getConfiguration().isCommandCooldownSyncEnabled();
}

View File

@ -24,6 +24,7 @@
import at.pcgamingfreaks.Minepacks.Bukkit.Minepacks;
import at.pcgamingfreaks.Version;
import org.intellij.lang.annotations.Language;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@ -51,18 +52,9 @@ protected void loadSettings()
{
// Set table and field names to fixed values to prevent users from destroying old databases.
fieldPlayerID = "player_id";
fieldPlayerName = "name";
fieldPlayerUUID = "uuid";
fieldBpOwner = "owner";
//noinspection SpellCheckingInspection
fieldBpIts = "itemstacks";
fieldBpVersion = "version";
fieldBpLastUpdate = "lastupdate";
tablePlayers = "backpack_players";
tableBackpacks = "backpacks";
tableCooldowns = "backpack_cooldowns";
fieldCdPlayer = "player_id";
fieldCdTime = "time";
// Set fixed settings
useUUIDSeparators = false;
syncCooldown = false;
@ -77,6 +69,15 @@ protected void updateQueriesForDialect()
queryUpdatePlayerAdd = "INSERT OR IGNORE INTO {TablePlayers} ({FieldName},{FieldUUID}) VALUES (?,?);";
}
private void tryQuery(final @NotNull Statement statement, final @NotNull @Language("SQL") String query)
{
try
{
statement.execute(query);
}
catch(SQLException ignored) {}
}
@SuppressWarnings("SqlResolve")
@Override
protected void checkDB()
@ -84,24 +85,15 @@ protected void checkDB()
try(Connection connection = getConnection(); Statement stmt = connection.createStatement())
{
Version dbVersion = getDatabaseVersion(stmt);
if(dbVersion.olderThan(new Version("3.0-PRE-ALPHA-SNAPSHOT")))
{
tryQuery(stmt, "ALTER TABLE `backpack_players` ADD COLUMN `uuid` CHAR(32);");
tryQuery(stmt, "ALTER TABLE `backpacks` ADD COLUMN `version` INT DEFAULT 0;");
tryQuery(stmt, "ALTER TABLE `backpacks` ADD COLUMN `lastupdate` DATE DEFAULT '2020-09-24';");
}
stmt.execute("CREATE TABLE IF NOT EXISTS `backpack_players` (`player_id` INTEGER PRIMARY KEY AUTOINCREMENT, `name` CHAR(16) NOT NULL , `uuid` CHAR(32) UNIQUE);");
try
{
stmt.execute("ALTER TABLE `backpack_players` ADD COLUMN `uuid` CHAR(32);");
}
catch(SQLException ignored) {}
stmt.execute("CREATE TABLE IF NOT EXISTS `backpack_players` (`player_id` INT UNSIGNED PRIMARY KEY AUTOINCREMENT, `name` CHAR(16) NOT NULL , `uuid` CHAR(32) UNIQUE);");
stmt.execute("CREATE TABLE IF NOT EXISTS `backpacks` (`owner` INT UNSIGNED PRIMARY KEY, `itemstacks` BLOB, `version` INT DEFAULT 0, `lastupdate` DATE DEFAULT CURRENT_DATE);");
try
{
stmt.execute("ALTER TABLE `backpacks` ADD COLUMN `version` INT DEFAULT 0;");
}
catch(SQLException ignored) {}
try
{
stmt.execute("ALTER TABLE `backpacks` ADD COLUMN `lastupdate` DATE DEFAULT '2020-09-24';");
}
catch(SQLException ignored) {}
DBTools.runStatement(connection, "INSERT OR REPLACE INTO `minepacks_metadata` (`key`, `value`) VALUES ('db_version',?);", plugin.getDescription().getVersion());
}
@ -118,7 +110,11 @@ protected void checkDB()
{
if(rs.next()) return new Version(rs.getString("value"));
}
return new Version("0");
try(ResultSet rs = stmt.executeQuery("SELECT name FROM sqlite_master WHERE type='table' AND name='backpack_players';"))
{ // Check if players table exists
if(rs.next()) return new Version(2);
}
return plugin.getVersion();
}
@Override