Deleting beatmaps/groups now also deletes them from the cache.

Signed-off-by: Jeffrey Han <itdelatrisu@gmail.com>
This commit is contained in:
Jeffrey Han 2015-03-04 21:45:44 -05:00
parent 3216c4d1ac
commit 4959510b5d
2 changed files with 32 additions and 9 deletions

View File

@ -19,6 +19,7 @@
package itdelatrisu.opsu; package itdelatrisu.opsu;
import itdelatrisu.opsu.audio.MusicController; import itdelatrisu.opsu.audio.MusicController;
import itdelatrisu.opsu.db.OsuDB;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
@ -180,6 +181,9 @@ public class OsuGroupList {
} }
} }
// remove entry from cache
OsuDB.delete(dir.getName());
// delete the associated directory // delete the associated directory
try { try {
Utils.deleteToTrash(dir); Utils.deleteToTrash(dir);
@ -224,9 +228,13 @@ public class OsuGroupList {
if (node.next != null) if (node.next != null)
node.next.prev = node.prev; node.next.prev = node.prev;
// remove entry from cache
File file = osu.getFile();
OsuDB.delete(file.getParentFile().getName(), file.getName());
// delete the associated file // delete the associated file
try { try {
Utils.deleteToTrash(osu.getFile()); Utils.deleteToTrash(file);
} catch (IOException e) { } catch (IOException e) {
ErrorHandler.error("Could not delete song.", e, true); ErrorHandler.error("Could not delete song.", e, true);
} }

View File

@ -48,7 +48,7 @@ public class OsuDB {
private static Connection connection; private static Connection connection;
/** Query statements. */ /** Query statements. */
private static PreparedStatement insertStmt, selectStmt, lastModStmt, deleteStmt; private static PreparedStatement insertStmt, selectStmt, lastModStmt, deleteMapStmt, deleteGroupStmt;
// This class should not be instantiated. // This class should not be instantiated.
private OsuDB() {} private OsuDB() {}
@ -80,7 +80,8 @@ public class OsuDB {
); );
lastModStmt = connection.prepareStatement("SELECT dir, file, lastModified FROM beatmaps"); lastModStmt = connection.prepareStatement("SELECT dir, file, lastModified FROM beatmaps");
selectStmt = connection.prepareStatement("SELECT * FROM beatmaps WHERE dir = ? AND file = ?"); selectStmt = connection.prepareStatement("SELECT * FROM beatmaps WHERE dir = ? AND file = ?");
deleteStmt = connection.prepareStatement("DELETE FROM beatmaps WHERE dir = ? AND file = ?"); deleteMapStmt = connection.prepareStatement("DELETE FROM beatmaps WHERE dir = ? AND file = ?");
deleteGroupStmt = connection.prepareStatement("DELETE FROM beatmaps WHERE dir = ?");
} catch (SQLException e) { } catch (SQLException e) {
ErrorHandler.error("Failed to prepare beatmap statements.", e, true); ErrorHandler.error("Failed to prepare beatmap statements.", e, true);
} }
@ -319,17 +320,30 @@ public class OsuDB {
} }
/** /**
* Deletes the entry from the database. * Deletes the beatmap entry from the database.
* @param dir the directory * @param dir the directory
* @param file the file * @param file the file
*/ */
public static void delete(String dir, String file) { public static void delete(String dir, String file) {
try { try {
deleteStmt.setString(1, dir); deleteMapStmt.setString(1, dir);
deleteStmt.setString(2, file); deleteMapStmt.setString(2, file);
deleteStmt.executeUpdate(); deleteMapStmt.executeUpdate();
} catch (SQLException e) { } catch (SQLException e) {
ErrorHandler.error("Failed to delete entry from database.", e, true); ErrorHandler.error("Failed to delete beatmap entry from database.", e, true);
}
}
/**
* Deletes the beatmap group entry from the database.
* @param dir the directory
*/
public static void delete(String dir) {
try {
deleteGroupStmt.setString(1, dir);
deleteGroupStmt.executeUpdate();
} catch (SQLException e) {
ErrorHandler.error("Failed to delete beatmap group entry from database.", e, true);
} }
} }
@ -342,7 +356,8 @@ public class OsuDB {
insertStmt.close(); insertStmt.close();
lastModStmt.close(); lastModStmt.close();
selectStmt.close(); selectStmt.close();
deleteStmt.close(); deleteMapStmt.close();
deleteGroupStmt.close();
connection.close(); connection.close();
} catch (SQLException e) { } catch (SQLException e) {
ErrorHandler.error("Failed to close beatmap database.", e, true); ErrorHandler.error("Failed to close beatmap database.", e, true);