mirror of
https://github.com/Sonarr/Sonarr.git
synced 2024-12-16 11:37:58 +02:00
86 lines
2.5 KiB
C#
86 lines
2.5 KiB
C#
using System;
|
|
using System.Data.SQLite;
|
|
using Marr.Data;
|
|
using Marr.Data.Reflection;
|
|
using NzbDrone.Common.Composition;
|
|
using NzbDrone.Common.Messaging;
|
|
using NzbDrone.Core.Datastore.Migration.Framework;
|
|
using NzbDrone.Core.Instrumentation;
|
|
|
|
|
|
namespace NzbDrone.Core.Datastore
|
|
{
|
|
public interface IDbFactory
|
|
{
|
|
IDatabase Create(MigrationType migrationType = MigrationType.Main);
|
|
}
|
|
|
|
|
|
public class DbFactory : IDbFactory
|
|
{
|
|
private readonly IMigrationController _migrationController;
|
|
private readonly IConnectionStringFactory _connectionStringFactory;
|
|
|
|
static DbFactory()
|
|
{
|
|
MapRepository.Instance.ReflectionStrategy = new SimpleReflectionStrategy();
|
|
TableMapping.Map();
|
|
}
|
|
|
|
public static void RegisterDatabase(IContainer container)
|
|
{
|
|
container.Register(c => c.Resolve<IDbFactory>().Create());
|
|
|
|
container.Register<ILogRepository>(c =>
|
|
{
|
|
var db = c.Resolve<IDbFactory>().Create(MigrationType.Log);
|
|
return new LogRepository(db, c.Resolve<IMessageAggregator>());
|
|
});
|
|
}
|
|
|
|
public DbFactory(IMigrationController migrationController, IConnectionStringFactory connectionStringFactory)
|
|
{
|
|
_migrationController = migrationController;
|
|
_connectionStringFactory = connectionStringFactory;
|
|
}
|
|
|
|
public IDatabase Create(MigrationType migrationType = MigrationType.Main)
|
|
{
|
|
string connectionString;
|
|
|
|
|
|
switch (migrationType)
|
|
{
|
|
case MigrationType.Main:
|
|
{
|
|
connectionString = _connectionStringFactory.MainDbConnectionString;
|
|
break;
|
|
}
|
|
case MigrationType.Log:
|
|
{
|
|
connectionString = _connectionStringFactory.LogDbConnectionString;
|
|
break;
|
|
}
|
|
default:
|
|
{
|
|
throw new ArgumentException("Invalid MigrationType");
|
|
}
|
|
}
|
|
|
|
|
|
|
|
_migrationController.MigrateToLatest(connectionString, migrationType);
|
|
|
|
return new Database(() =>
|
|
{
|
|
var dataMapper = new DataMapper(SQLiteFactory.Instance, connectionString)
|
|
{
|
|
SqlMode = SqlModes.Text,
|
|
};
|
|
|
|
return dataMapper;
|
|
});
|
|
}
|
|
}
|
|
}
|