Add removing of coordinates and add optional migration of coordinates database table to include autoincrement id property

This commit is contained in:
SemvdH
2026-02-25 22:52:17 +01:00
parent 09236a360b
commit f780457092
14 changed files with 292 additions and 35 deletions

View File

@@ -28,7 +28,7 @@ public enum DatabaseManager {
if (this.initialized) {
return true;
}
this.app = app;
if (!setupDatabase()) {
@@ -58,6 +58,12 @@ public enum DatabaseManager {
return true;
}
/**
* Registers a listener that will be called when the database is
* initialized. This can be used to initialize the tables for the database.
*
* @param listener the listener to register
*/
public void registerDatabaseInitializeListener(DatabaseInitializeListener listener) {
this.databaseInitializeListeners.add(listener);
this.app.getLogger().log(Level.INFO, "Registered database initialize listener: {0}", listener.getClass().getName());
@@ -93,7 +99,7 @@ public enum DatabaseManager {
this.connection = DriverManager.getConnection(url);
} catch (SQLException e) {
this.app.getLogger().log(Level.SEVERE, "Could not connect to database file {0}: {1}",
new Object[] { dbFile, e.getMessage() });
new Object[]{dbFile, e.getMessage()});
}
this.app.getLogger().log(Level.INFO, "Connected to SQLite database: {0}", dbFile.getName());
@@ -102,6 +108,37 @@ public enum DatabaseManager {
return true;
}
/**
* Creates a backup of the current database file in the plugin data folder.
* The backup file will be named database_backup_TIMESTAMP.db
*
* @return the File object of the backup, or null if failed
*/
public File createBackupDatabase() {
if (this.app == null) {
return null;
}
File dbFile = new File(app.getDataFolder(), "database.db");
if (!dbFile.exists()) {
return null;
}
String backupName = "database_backup_" + System.currentTimeMillis() + ".db";
File backupFile = new File(app.getDataFolder(), backupName);
try (
java.io.FileInputStream in = new java.io.FileInputStream(dbFile); java.io.FileOutputStream out = new java.io.FileOutputStream(backupFile)) {
byte[] buffer = new byte[8192];
int bytesRead;
while ((bytesRead = in.read(buffer)) > 0) {
out.write(buffer, 0, bytesRead);
}
this.app.getLogger().log(Level.INFO, "Database backup created: {0}", backupFile.getName());
return backupFile;
} catch (Exception e) {
this.app.getLogger().log(Level.SEVERE, "Failed to create database backup: {0}", e.getMessage());
return null;
}
}
/**
* initializes the tables for the database.
*/