1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 23:47:24 +08:00
osu-lazer/osu.Game/Database/OsuDbContext.cs

124 lines
4.7 KiB
C#
Raw Normal View History

2017-10-15 23:53:59 +08:00
// 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;
2017-10-14 14:16:08 +08:00
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using osu.Framework.Logging;
2017-10-05 03:52:12 +08:00
using osu.Game.Beatmaps;
using osu.Game.Input.Bindings;
using osu.Game.IO;
using osu.Game.Rulesets;
2017-10-14 14:16:08 +08:00
using LogLevel = Microsoft.Extensions.Logging.LogLevel;
2017-10-05 03:52:12 +08:00
namespace osu.Game.Database
{
public class OsuDbContext : DbContext
{
2017-10-15 23:53:59 +08:00
public DbSet<BeatmapInfo> BeatmapInfo { get; set; }
public DbSet<BeatmapSetInfo> BeatmapSetInfo { get; set; }
public DbSet<DatabasedKeyBinding> DatabasedKeyBinding { get; set; }
public DbSet<FileInfo> FileInfo { get; set; }
public DbSet<RulesetInfo> RulesetInfo { get; set; }
2017-10-05 03:52:12 +08:00
private readonly string connectionString;
2017-10-16 10:06:19 +08:00
static OsuDbContext()
2017-10-05 03:52:12 +08:00
{
2017-10-16 10:06:19 +08:00
// required to initialise native SQLite libraries on some platforms.
SQLitePCL.Batteries_V2.Init();
2017-10-05 03:52:12 +08:00
}
2017-10-17 16:52:02 +08:00
/// <summary>
/// Create a new in-memory OsuDbContext instance.
/// </summary>
public OsuDbContext() : this("DataSource=:memory:")
{
// required for tooling (see https://wildermuth.com/2017/07/06/Program-cs-in-ASP-NET-Core-2-0).
}
2017-10-16 10:06:19 +08:00
/// <summary>
/// Create a new OsuDbContext instance.
/// </summary>
2017-10-17 16:52:02 +08:00
/// <param name="connectionString">A valid SQLite connection string.</param>
public OsuDbContext(string connectionString)
2017-10-05 03:52:12 +08:00
{
this.connectionString = connectionString;
2017-10-16 10:06:19 +08:00
Database.SetCommandTimeout(new TimeSpan(TimeSpan.TicksPerSecond * 10));
var connection = Database.GetDbConnection();
connection.Open();
using (var cmd = connection.CreateCommand())
{
cmd.CommandText = "PRAGMA journal_mode=WAL;";
cmd.ExecuteNonQuery();
}
2017-10-05 03:52:12 +08:00
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
base.OnConfiguring(optionsBuilder);
optionsBuilder.UseSqlite(connectionString);
2017-10-14 14:16:08 +08:00
optionsBuilder.UseLoggerFactory(new OsuDbLoggerFactory());
2017-10-05 03:52:12 +08:00
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<BeatmapInfo>().HasIndex(b => b.MD5Hash);
modelBuilder.Entity<BeatmapSetInfo>().HasIndex(b => b.DeletePending);
modelBuilder.Entity<DatabasedKeyBinding>().HasIndex(b => b.Variant);
modelBuilder.Entity<DatabasedKeyBinding>().HasIndex(b => b.IntAction);
modelBuilder.Entity<FileInfo>().HasIndex(b => b.Hash).IsUnique();
modelBuilder.Entity<FileInfo>().HasIndex(b => b.ReferenceCount);
modelBuilder.Entity<RulesetInfo>().HasIndex(b => b.Name).IsUnique();
modelBuilder.Entity<RulesetInfo>().HasIndex(b => b.InstantiationInfo).IsUnique();
modelBuilder.Entity<RulesetInfo>().HasIndex(b => b.Available);
2017-10-17 17:26:28 +08:00
modelBuilder.Entity<BeatmapMetadata>().HasOne(m => m.BeatmapSetInfo).WithOne(s => s.Metadata).OnDelete(DeleteBehavior.Cascade);
modelBuilder.Entity<BeatmapMetadata>().HasOne(m => m.BeatmapInfo).WithOne(b => b.Metadata).OnDelete(DeleteBehavior.Cascade);
2017-10-05 03:52:12 +08:00
}
2017-10-14 14:16:08 +08:00
private class OsuDbLoggerFactory : ILoggerFactory
{
2017-10-15 23:53:59 +08:00
#region Disposal
2017-10-14 14:16:08 +08:00
public void Dispose()
{
}
2017-10-15 23:53:59 +08:00
#endregion
2017-10-14 14:16:08 +08:00
public ILogger CreateLogger(string categoryName) => new OsuDbLogger();
2017-10-16 11:51:46 +08:00
public void AddProvider(ILoggerProvider provider)
{
2017-10-17 16:52:02 +08:00
// no-op. called by tooling.
2017-10-16 11:51:46 +08:00
}
2017-10-14 14:16:08 +08:00
private class OsuDbLoggerProvider : ILoggerProvider
{
2017-10-15 23:53:59 +08:00
#region Disposal
2017-10-14 14:16:08 +08:00
public void Dispose()
{
}
2017-10-15 23:53:59 +08:00
#endregion
2017-10-14 14:16:08 +08:00
public ILogger CreateLogger(string categoryName) => new OsuDbLogger();
}
private class OsuDbLogger : ILogger
{
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
=> Logger.Log(formatter(state, exception), LoggingTarget.Database, Framework.Logging.LogLevel.Debug);
public bool IsEnabled(LogLevel logLevel) => true;
public IDisposable BeginScope<TState>(TState state) => null;
}
}
2017-10-05 03:52:12 +08:00
}
}