Refactor SQL database startup error handling

This commit is contained in:
GeorgH93 2021-03-25 18:44:45 +01:00
parent 2ea150075d
commit aa7e2422e3
No known key found for this signature in database
GPG Key ID: D1630D37F9E4B3C8
5 changed files with 27 additions and 33 deletions

View File

@ -30,7 +30,7 @@
public class MySQL extends SQL 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); super(plugin, (connectionProvider == null) ? new MySQLConnectionProvider(plugin.getLogger(), plugin.getDescription().getName(), plugin.getConfiguration()) : connectionProvider);
} }

View File

@ -49,7 +49,7 @@ public abstract class SQL extends DatabaseBackend
@Language("SQL") protected String queryDeleteOldCooldowns, querySyncCooldown; // DB Querys @Language("SQL") protected String queryDeleteOldCooldowns, querySyncCooldown; // DB Querys
protected boolean syncCooldown; protected boolean syncCooldown;
public SQL(@NotNull Minepacks plugin, @NotNull ConnectionProvider connectionProvider) public SQL(@NotNull Minepacks plugin, @NotNull ConnectionProvider connectionProvider) throws SQLException
{ {
super(plugin); super(plugin);
@ -62,28 +62,10 @@ public SQL(@NotNull Minepacks plugin, @NotNull ConnectionProvider connectionProv
checkUUIDs(); // Check if there are user accounts without UUID checkUUIDs(); // Check if there are user accounts without UUID
// Delete old backpacks // Delete old backpacks
if(maxAge > 0) try(Connection connection = getConnection())
{ {
try(Connection connection = getConnection(); Statement statement = connection.createStatement()) DBTools.runStatementWithoutException(connection, queryDeleteOldBackpacks);
{ if(syncCooldown) DBTools.runStatementWithoutException(connection, queryDeleteOldCooldowns, System.currentTimeMillis());
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();
}
} }
} }
@ -229,11 +211,13 @@ protected void setTableAndFieldNames()
protected String replacePlaceholders(@Language("SQL") String query) 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("\\{TablePlayers}", tablePlayers).replaceAll("\\{FieldName}", fieldPlayerName).replaceAll("\\{FieldUUID}", fieldPlayerUUID).replaceAll("\\{FieldPlayerID}", fieldPlayerID) // Players
.replaceAll("\\{TableBackpacks}", tableBackpacks).replaceAll("\\{FieldBPOwner}", fieldBpOwner).replaceAll("\\{FieldBPITS}", fieldBpIts) // Backpacks .replaceAll("\\{TableBackpacks}", tableBackpacks).replaceAll("\\{FieldBPOwner}", fieldBpOwner).replaceAll("\\{FieldBPITS}", fieldBpIts) // Backpacks
.replaceAll("\\{FieldBPVersion}", fieldBpVersion).replaceAll("\\{FieldBPLastUpdate}", fieldBpLastUpdate) // Backpacks .replaceAll("\\{FieldBPVersion}", fieldBpVersion).replaceAll("\\{FieldBPLastUpdate}", fieldBpLastUpdate) // Backpacks
.replaceAll("\\{TableCooldowns}", tableCooldowns).replaceAll("\\{FieldCDPlayer}", fieldCdPlayer).replaceAll("\\{FieldCDTime}", fieldCdTime); // Cooldowns .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) protected void runStatementAsync(final @NotNull @Language("SQL") String query, final Object... args)

View File

@ -42,7 +42,7 @@ public static String getDbFile(final @NotNull Minepacks plugin)
} }
//TODO add cooldown sync table //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); super(plugin, (connectionProvider == null) ? new SQLiteConnectionProvider(plugin.getLogger(), plugin.getDescription().getName(), getDbFile(plugin)) : connectionProvider);
} }

View File

@ -123,6 +123,7 @@ public boolean available()
catch(IllegalStateException ignored) {} catch(IllegalStateException ignored) {}
catch(Exception e) catch(Exception e)
{ {
plugin.getLogger().severe("Failed to create database backend!");
e.printStackTrace(); e.printStackTrace();
} }
return null; return null;

View File

@ -56,16 +56,25 @@ protected ToSQLMigration(@NotNull Minepacks plugin, @NotNull DatabaseBackend old
super(plugin, oldDb); super(plugin, oldDb);
ConnectionProvider connectionProvider = (global) ? Database.getGlobalConnectionProvider(plugin.getLogger()) : null; ConnectionProvider connectionProvider = (global) ? Database.getGlobalConnectionProvider(plugin.getLogger()) : null;
switch(dbType) SQL newDb = null;
try
{ {
case MYSQL: newDb = new MySQL(plugin, connectionProvider); break; switch(dbType)
case SQLITE: {
final File dbFile = new File(SQLite.getDbFile(plugin)); case MYSQL: newDb = new MySQL(plugin, connectionProvider); break;
if(dbFile.exists()) dbFile.renameTo(new File(SQLite.getDbFile(plugin) + ".old_" + System.currentTimeMillis())); case SQLITE:
newDb = new SQLite(plugin, connectionProvider); final File dbFile = new File(SQLite.getDbFile(plugin));
break; if(dbFile.exists()) dbFile.renameTo(new File(SQLite.getDbFile(plugin) + ".old_" + System.currentTimeMillis()));
default: newDb = null; 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 protected @Language("SQL") String replacePlaceholders(SQL database, @Language("SQL") String query) throws Exception