// Copyright (c) 2007-2017 ppy Pty Ltd . // Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE using System; using osu.Game.Database; using SQLite.Net; using SQLiteNetExtensions.Extensions; namespace osu.Game.Beatmaps { /// /// Handles the storage and retrieval of Beatmaps/BeatmapSets to the database backing /// public class BeatmapStore : DatabaseBackedStore { public event Action BeatmapSetAdded; public event Action BeatmapSetRemoved; public event Action BeatmapHidden; public event Action BeatmapRestored; /// /// The current version of this store. Used for migrations (see ). /// The initial version is 1. /// protected override int StoreVersion => 4; public BeatmapStore(SQLiteConnection connection) : base(connection) { } protected override Type[] ValidTypes => new[] { typeof(BeatmapSetInfo), typeof(BeatmapInfo), typeof(BeatmapMetadata), typeof(BeatmapDifficulty), }; protected override void Prepare(bool reset = false) { if (reset) { Connection.DropTable(); Connection.DropTable(); Connection.DropTable(); Connection.DropTable(); Connection.DropTable(); } Connection.CreateTable(); Connection.CreateTable(); Connection.CreateTable(); Connection.CreateTable(); Connection.CreateTable(); } protected override void StartupTasks() { base.StartupTasks(); cleanupPendingDeletions(); } /// /// Perform migrations between two store versions. /// /// The current store version. This will be zero on a fresh database initialisation. /// The target version which we are migrating to (equal to the current ). protected override void PerformMigration(int currentVersion, int targetVersion) { base.PerformMigration(currentVersion, targetVersion); while (currentVersion++ < targetVersion) { switch (currentVersion) { case 1: case 2: // cannot migrate; breaking underlying changes. Reset(); break; case 3: // Added MD5Hash column to BeatmapInfo Connection.MigrateTable(); break; case 4: // Added Hidden column to BeatmapInfo Connection.MigrateTable(); break; } } } /// /// Add a to the database. /// /// The beatmap to add. public void Add(BeatmapSetInfo beatmapSet) { Connection.RunInTransaction(() => { Connection.InsertOrReplaceWithChildren(beatmapSet, true); }); BeatmapSetAdded?.Invoke(beatmapSet); } /// /// Delete a from the database. /// /// The beatmap to delete. /// Whether the beatmap's was changed. public bool Delete(BeatmapSetInfo beatmapSet) { if (beatmapSet.DeletePending) return false; beatmapSet.DeletePending = true; Connection.Update(beatmapSet); BeatmapSetRemoved?.Invoke(beatmapSet); return true; } /// /// Restore a previously deleted . /// /// The beatmap to restore. /// Whether the beatmap's was changed. public bool Undelete(BeatmapSetInfo beatmapSet) { if (!beatmapSet.DeletePending) return false; beatmapSet.DeletePending = false; Connection.Update(beatmapSet); BeatmapSetAdded?.Invoke(beatmapSet); return true; } /// /// Hide a in the database. /// /// The beatmap to hide. /// Whether the beatmap's was changed. public bool Hide(BeatmapInfo beatmap) { if (beatmap.Hidden) return false; beatmap.Hidden = true; Connection.Update(beatmap); BeatmapHidden?.Invoke(beatmap); return true; } /// /// Restore a previously hidden . /// /// The beatmap to restore. /// Whether the beatmap's was changed. public bool Restore(BeatmapInfo beatmap) { if (!beatmap.Hidden) return false; beatmap.Hidden = false; Connection.Update(beatmap); BeatmapRestored?.Invoke(beatmap); return true; } private void cleanupPendingDeletions() { Connection.RunInTransaction(() => { foreach (var b in QueryAndPopulate(b => b.DeletePending && !b.Protected)) Connection.Delete(b, true); }); } } }