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 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);
}

View File

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

View File

@ -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);
}

View File

@ -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;

View File

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