delhome/delwarp all rewrite; add events for bulk-deletes, close #265

This commit is contained in:
William committed 2022-11-22 21:58:23 +00:00
1 parent 2402f12536
commit 834fdbfc0e
17 files changed
+379 -51

No files matched your search

@@ -443,6 +443,14 @@ public abstract class Database {
*/
public abstract CompletableFuture<Void> deleteHome(@NotNull UUID uuid);
/**
* Deletes all {@link Home}s of a {@link User} from the home table on the database.
*
* @param user The {@link User} to delete all homes of
* @return A future returning an integer; the number of deleted homes
*/
public abstract CompletableFuture<Integer> deleteAllHomes(@NotNull User user);
/**
* Deletes a {@link Warp} by the given unique id from the warp table on the database.
*
@@ -451,6 +459,13 @@ public abstract class Database {
*/
public abstract CompletableFuture<Void> deleteWarp(@NotNull UUID uuid);
/**
* Deletes all {@link Warp}s set on the database table
*
* @return A future returning an integer; the number of deleted warps
*/
public abstract CompletableFuture<Integer> deleteAllWarps();
/**
* Close any remaining connection to the database source
*/
@@ -1011,6 +1011,32 @@ public class MySqlDatabase extends Database {
});
}
@Override
public CompletableFuture<Integer> deleteAllHomes(@NotNull User user) {
return CompletableFuture.supplyAsync(() -> {
try (Connection connection = getConnection()) {
try (PreparedStatement statement = connection.prepareStatement(formatStatementTables("""
DELETE FROM `%positions_table%`
WHERE `%positions_table%`.`id` IN (
SELECT `position_id`
FROM `%saved_positions_table%`
WHERE `%saved_positions_table%`.`id` IN (
SELECT `saved_position_id`
FROM `%homes_table%`
WHERE `owner_uuid`=?
)
);"""))) {
statement.setString(1, user.uuid.toString());
return statement.executeUpdate();
}
} catch (SQLException e) {
getLogger().log(Level.SEVERE, "Failed to delete all homes for " + user.username + " from the database", e);
}
return 0;
});
}
@Override
public CompletableFuture<Void> deleteWarp(@NotNull UUID uuid) {
return CompletableFuture.runAsync(() -> {
@@ -1036,6 +1062,29 @@ public class MySqlDatabase extends Database {
});
}
@Override
public CompletableFuture<Integer> deleteAllWarps() {
return CompletableFuture.supplyAsync(() -> {
try (Connection connection = getConnection()) {
try (PreparedStatement statement = connection.prepareStatement(formatStatementTables("""
DELETE FROM `%positions_table%`
WHERE `%positions_table%`.`id` IN (
SELECT `position_id`
FROM `%saved_positions_table%`
WHERE `%saved_positions_table%`.`id` IN (
SELECT `saved_position_id`
FROM `%warps_table%`
)
);"""))) {
return statement.executeUpdate();
}
} catch (SQLException e) {
getLogger().log(Level.SEVERE, "Failed to delete all warps from the database", e);
}
return 0;
});
}
@Override
public void terminate() {
if (dataSource != null) {
@@ -1011,6 +1011,32 @@ public class SqLiteDatabase extends Database {
});
}
@Override
public CompletableFuture<Integer> deleteAllHomes(@NotNull User user) {
return CompletableFuture.supplyAsync(() -> {
try (Connection connection = getConnection()) {
try (PreparedStatement statement = connection.prepareStatement(formatStatementTables("""
DELETE FROM `%positions_table%`
WHERE `%positions_table%`.`id` IN (
SELECT `position_id`
FROM `%saved_positions_table%`
WHERE `%saved_positions_table%`.`id` IN (
SELECT `saved_position_id`
FROM `%homes_table%`
WHERE `owner_uuid`=?
)
);"""))) {
statement.setString(1, user.uuid.toString());
return statement.executeUpdate();
}
} catch (SQLException e) {
getLogger().log(Level.SEVERE, "Failed to delete all homes for " + user.username + " from the database", e);
}
return 0;
});
}
@Override
public CompletableFuture<Void> deleteWarp(@NotNull UUID uuid) {
return CompletableFuture.runAsync(() -> {
@@ -1036,6 +1062,29 @@ public class SqLiteDatabase extends Database {
});
}
@Override
public CompletableFuture<Integer> deleteAllWarps() {
return CompletableFuture.supplyAsync(() -> {
try (Connection connection = getConnection()) {
try (PreparedStatement statement = connection.prepareStatement(formatStatementTables("""
DELETE FROM `%positions_table%`
WHERE `%positions_table%`.`id` IN (
SELECT `position_id`
FROM `%saved_positions_table%`
WHERE `%saved_positions_table%`.`id` IN (
SELECT `saved_position_id`
FROM `%warps_table%`
)
);"""))) {
return statement.executeUpdate();
}
} catch (SQLException e) {
getLogger().log(Level.SEVERE, "Failed to delete all warps from the database", e);
}
return 0;
});
}
@Override
public void terminate() {
try {