From aa7e2422e39d45a9bfa2028560fa377e21d517a9 Mon Sep 17 00:00:00 2001 From: GeorgH93 Date: Thu, 25 Mar 2021 18:44:45 +0100 Subject: [PATCH] Refactor SQL database startup error handling --- .../Bukkit/Database/Backend/MySQL.java | 2 +- .../Bukkit/Database/Backend/SQL.java | 30 +++++-------------- .../Bukkit/Database/Backend/SQLite.java | 2 +- .../Minepacks/Bukkit/Database/Database.java | 1 + .../Database/Migration/ToSQLMigration.java | 25 +++++++++++----- 5 files changed, 27 insertions(+), 33 deletions(-) diff --git a/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/Backend/MySQL.java b/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/Backend/MySQL.java index a34e56f..ecf9e7c 100644 --- a/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/Backend/MySQL.java +++ b/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/Backend/MySQL.java @@ -30,7 +30,7 @@ public class MySQL extends SQL { - public MySQL(@NotNull Minepacks plugin, @Nullable ConnectionProvider connectionProvider) + public MySQL(@NotNull Minepacks plugin, @Nullable ConnectionProvider connectionProvider) throws SQLException { super(plugin, (connectionProvider == null) ? new MySQLConnectionProvider(plugin.getLogger(), plugin.getDescription().getName(), plugin.getConfiguration()) : connectionProvider); } diff --git a/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/Backend/SQL.java b/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/Backend/SQL.java index 92aca02..350254c 100644 --- a/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/Backend/SQL.java +++ b/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/Backend/SQL.java @@ -49,7 +49,7 @@ public abstract class SQL extends DatabaseBackend @Language("SQL") protected String queryDeleteOldCooldowns, querySyncCooldown; // DB Querys protected boolean syncCooldown; - public SQL(@NotNull Minepacks plugin, @NotNull ConnectionProvider connectionProvider) + public SQL(@NotNull Minepacks plugin, @NotNull ConnectionProvider connectionProvider) throws SQLException { super(plugin); @@ -62,28 +62,10 @@ public SQL(@NotNull Minepacks plugin, @NotNull ConnectionProvider connectionProv checkUUIDs(); // Check if there are user accounts without UUID // Delete old backpacks - if(maxAge > 0) + try(Connection connection = getConnection()) { - try(Connection connection = getConnection(); Statement statement = connection.createStatement()) - { - 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) - { - e.printStackTrace(); - } + DBTools.runStatementWithoutException(connection, queryDeleteOldBackpacks); + if(syncCooldown) DBTools.runStatementWithoutException(connection, queryDeleteOldCooldowns, System.currentTimeMillis()); } } @@ -229,11 +211,13 @@ protected void setTableAndFieldNames() protected String replacePlaceholders(@Language("SQL") String query) { - return query.replaceAll("(\\{\\w+})", "`$1`").replaceAll("`(\\{\\w+})`_(\\w+)", "`$1_$2`").replaceAll("fk_`(\\{\\w+})`_`(\\{\\w+})`_`(\\{\\w+})`", "`fk_$1_$2_$3`") // Fix name formatting + query = query.replaceAll("(\\{\\w+})", "`$1`").replaceAll("`(\\{\\w+})`_(\\w+)", "`$1_$2`").replaceAll("fk_`(\\{\\w+})`_`(\\{\\w+})`_`(\\{\\w+})`", "`fk_$1_$2_$3`") // Fix name formatting .replaceAll("\\{TablePlayers}", tablePlayers).replaceAll("\\{FieldName}", fieldPlayerName).replaceAll("\\{FieldUUID}", fieldPlayerUUID).replaceAll("\\{FieldPlayerID}", fieldPlayerID) // Players .replaceAll("\\{TableBackpacks}", tableBackpacks).replaceAll("\\{FieldBPOwner}", fieldBpOwner).replaceAll("\\{FieldBPITS}", fieldBpIts) // Backpacks .replaceAll("\\{FieldBPVersion}", fieldBpVersion).replaceAll("\\{FieldBPLastUpdate}", fieldBpLastUpdate) // Backpacks .replaceAll("\\{TableCooldowns}", tableCooldowns).replaceAll("\\{FieldCDPlayer}", fieldCdPlayer).replaceAll("\\{FieldCDTime}", fieldCdTime); // Cooldowns + if(query.matches(".*\\{\\w+}.*")) plugin.getLogger().warning("Found unresolved placeholder in query:\n" + query); + return query; } protected void runStatementAsync(final @NotNull @Language("SQL") String query, final Object... args) diff --git a/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/Backend/SQLite.java b/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/Backend/SQLite.java index e1343e8..9a2ae26 100644 --- a/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/Backend/SQLite.java +++ b/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/Backend/SQLite.java @@ -42,7 +42,7 @@ public static String getDbFile(final @NotNull Minepacks plugin) } //TODO add cooldown sync table - public SQLite(final @NotNull Minepacks plugin, final @Nullable ConnectionProvider connectionProvider) + public SQLite(final @NotNull Minepacks plugin, final @Nullable ConnectionProvider connectionProvider) throws SQLException { super(plugin, (connectionProvider == null) ? new SQLiteConnectionProvider(plugin.getLogger(), plugin.getDescription().getName(), getDbFile(plugin)) : connectionProvider); } diff --git a/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/Database.java b/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/Database.java index 1a02839..7f6d3d7 100644 --- a/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/Database.java +++ b/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/Database.java @@ -123,6 +123,7 @@ public boolean available() catch(IllegalStateException ignored) {} catch(Exception e) { + plugin.getLogger().severe("Failed to create database backend!"); e.printStackTrace(); } return null; diff --git a/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/Migration/ToSQLMigration.java b/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/Migration/ToSQLMigration.java index 7650036..176ed2c 100644 --- a/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/Migration/ToSQLMigration.java +++ b/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/Migration/ToSQLMigration.java @@ -56,16 +56,25 @@ protected ToSQLMigration(@NotNull Minepacks plugin, @NotNull DatabaseBackend old super(plugin, oldDb); ConnectionProvider connectionProvider = (global) ? Database.getGlobalConnectionProvider(plugin.getLogger()) : null; - switch(dbType) + SQL newDb = null; + try { - case MYSQL: newDb = new MySQL(plugin, connectionProvider); break; - case SQLITE: - final File dbFile = new File(SQLite.getDbFile(plugin)); - if(dbFile.exists()) dbFile.renameTo(new File(SQLite.getDbFile(plugin) + ".old_" + System.currentTimeMillis())); - newDb = new SQLite(plugin, connectionProvider); - break; - default: newDb = null; + switch(dbType) + { + case MYSQL: newDb = new MySQL(plugin, connectionProvider); break; + case SQLITE: + final File dbFile = new File(SQLite.getDbFile(plugin)); + if(dbFile.exists()) dbFile.renameTo(new File(SQLite.getDbFile(plugin) + ".old_" + System.currentTimeMillis())); + newDb = new SQLite(plugin, connectionProvider); + break; + } } + catch(Exception e) + { + e.printStackTrace(); + } + + this.newDb = newDb; } protected @Language("SQL") String replacePlaceholders(SQL database, @Language("SQL") String query) throws Exception