// 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; /// /// The current version of this store. Used for migrations (see ). /// The initial version is 1. /// protected override int StoreVersion => 2; 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; } } } /// /// 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 to 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; } private void cleanupPendingDeletions() { Connection.RunInTransaction(() => { foreach (var b in QueryAndPopulate(b => b.DeletePending && !b.Protected)) Connection.Delete(b, true); }); } } }