1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-22 07:27:25 +08:00
osu-lazer/osu.Game/Database/DatabaseBackedStore.cs
Dean Herbert 5f53426a9a *Database -> *Store
Welcome back BeatmapManager
2017-07-27 16:56:41 +09:00

93 lines
2.9 KiB
C#

// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using osu.Framework.Logging;
using osu.Framework.Platform;
using SQLite.Net;
using SQLiteNetExtensions.Extensions;
namespace osu.Game.Database
{
public abstract class DatabaseBackedStore
{
protected readonly Storage Storage;
protected readonly SQLiteConnection Connection;
protected DatabaseBackedStore(SQLiteConnection connection, Storage storage = null)
{
Storage = storage;
Connection = connection;
try
{
Prepare();
}
catch (Exception e)
{
Logger.Error(e, $@"Failed to initialise the {GetType()}! Trying again with a clean database...");
Prepare(true);
}
}
/// <summary>
/// Prepare this database for use.
/// </summary>
protected abstract void Prepare(bool reset = false);
/// <summary>
/// Reset this database to a default state. Undo all changes to database and storage backings.
/// </summary>
public void Reset() => Prepare(true);
public TableQuery<T> Query<T>(Expression<Func<T, bool>> filter = null) where T : class
{
checkType(typeof(T));
var query = Connection.Table<T>();
if (filter != null)
query = query.Where(filter);
return query;
}
/// <summary>
/// Query and populate results.
/// </summary>
/// <param name="filter">An optional filter to refine results.</param>
/// <returns></returns>
public List<T> QueryAndPopulate<T>(Expression<Func<T, bool>> filter = null)
where T : class
{
checkType(typeof(T));
return Connection.GetAllWithChildren(filter, true);
}
/// <summary>
/// Populate a database-backed item.
/// </summary>
/// <param name="item"></param>
/// <param name="recursive">Whether population should recurse beyond a single level.</param>
public void Populate<T>(T item, bool recursive = true)
{
checkType(item.GetType());
Connection.GetChildren(item, recursive);
}
private void checkType(Type type)
{
if (!ValidTypes.Contains(type))
throw new InvalidOperationException($"The requested operation specified a type of {type}, which is invalid for this {nameof(DatabaseBackedStore)}.");
}
protected abstract Type[] ValidTypes { get; }
}
}