2013-03-23 17:08:23 -07:00
|
|
|
using System;
|
2013-03-25 22:51:56 -07:00
|
|
|
using System.Data.SQLite;
|
2013-03-24 20:51:32 -07:00
|
|
|
using Marr.Data;
|
2013-04-01 16:55:09 -07:00
|
|
|
using Marr.Data.Reflection;
|
2013-06-27 18:03:04 -07:00
|
|
|
using NzbDrone.Common.Composition;
|
2013-03-24 23:13:53 -07:00
|
|
|
using NzbDrone.Core.Datastore.Migration.Framework;
|
2013-06-27 18:03:04 -07:00
|
|
|
using NzbDrone.Core.Instrumentation;
|
2013-09-13 23:36:07 -07:00
|
|
|
using NzbDrone.Core.Messaging.Events;
|
2013-03-24 20:51:32 -07:00
|
|
|
|
2013-03-23 17:08:23 -07:00
|
|
|
|
|
|
|
namespace NzbDrone.Core.Datastore
|
|
|
|
{
|
|
|
|
public interface IDbFactory
|
|
|
|
{
|
2013-06-27 18:03:04 -07:00
|
|
|
IDatabase Create(MigrationType migrationType = MigrationType.Main);
|
2013-03-23 17:08:23 -07:00
|
|
|
}
|
|
|
|
|
2013-07-04 20:56:27 -07:00
|
|
|
|
2013-03-23 17:08:23 -07:00
|
|
|
public class DbFactory : IDbFactory
|
|
|
|
{
|
2013-03-24 23:13:53 -07:00
|
|
|
private readonly IMigrationController _migrationController;
|
2013-07-04 20:56:27 -07:00
|
|
|
private readonly IConnectionStringFactory _connectionStringFactory;
|
2013-03-23 17:08:23 -07:00
|
|
|
|
2013-03-30 15:14:33 -07:00
|
|
|
static DbFactory()
|
2013-03-23 17:08:23 -07:00
|
|
|
{
|
2013-06-02 20:15:56 -07:00
|
|
|
MapRepository.Instance.ReflectionStrategy = new SimpleReflectionStrategy();
|
2013-03-24 23:13:53 -07:00
|
|
|
TableMapping.Map();
|
2013-03-30 15:14:33 -07:00
|
|
|
}
|
|
|
|
|
2013-06-27 18:03:04 -07:00
|
|
|
public static void RegisterDatabase(IContainer container)
|
|
|
|
{
|
2013-09-04 18:03:41 -07:00
|
|
|
container.Resolve<IDbFactory>().Create();
|
|
|
|
|
2013-06-27 18:03:04 -07:00
|
|
|
container.Register(c => c.Resolve<IDbFactory>().Create());
|
|
|
|
|
2013-09-04 18:03:41 -07:00
|
|
|
container.Resolve<IDbFactory>().Create(MigrationType.Log);
|
|
|
|
|
2013-06-27 18:03:04 -07:00
|
|
|
container.Register<ILogRepository>(c =>
|
|
|
|
{
|
|
|
|
var db = c.Resolve<IDbFactory>().Create(MigrationType.Log);
|
2013-09-13 23:36:07 -07:00
|
|
|
return new LogRepository(db, c.Resolve<IEventAggregator>());
|
2013-06-27 18:03:04 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-07-04 20:56:27 -07:00
|
|
|
public DbFactory(IMigrationController migrationController, IConnectionStringFactory connectionStringFactory)
|
2013-03-30 15:14:33 -07:00
|
|
|
{
|
2013-03-24 23:13:53 -07:00
|
|
|
_migrationController = migrationController;
|
2013-07-04 20:56:27 -07:00
|
|
|
_connectionStringFactory = connectionStringFactory;
|
2013-03-24 23:13:53 -07:00
|
|
|
}
|
2013-03-23 21:56:59 -07:00
|
|
|
|
2013-06-27 18:03:04 -07:00
|
|
|
public IDatabase Create(MigrationType migrationType = MigrationType.Main)
|
2013-03-24 23:13:53 -07:00
|
|
|
{
|
2013-07-04 20:56:27 -07:00
|
|
|
string connectionString;
|
|
|
|
|
2013-06-27 18:03:04 -07:00
|
|
|
|
|
|
|
switch (migrationType)
|
|
|
|
{
|
|
|
|
case MigrationType.Main:
|
|
|
|
{
|
2013-07-04 20:56:27 -07:00
|
|
|
connectionString = _connectionStringFactory.MainDbConnectionString;
|
2013-06-27 18:03:04 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MigrationType.Log:
|
|
|
|
{
|
2013-07-04 20:56:27 -07:00
|
|
|
connectionString = _connectionStringFactory.LogDbConnectionString;
|
2013-06-27 18:03:04 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
throw new ArgumentException("Invalid MigrationType");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-03-23 17:08:23 -07:00
|
|
|
|
2013-03-24 23:13:53 -07:00
|
|
|
_migrationController.MigrateToLatest(connectionString, migrationType);
|
2013-05-11 13:06:57 -07:00
|
|
|
|
|
|
|
return new Database(() =>
|
|
|
|
{
|
|
|
|
var dataMapper = new DataMapper(SQLiteFactory.Instance, connectionString)
|
|
|
|
{
|
|
|
|
SqlMode = SqlModes.Text,
|
|
|
|
};
|
|
|
|
|
|
|
|
return dataMapper;
|
|
|
|
});
|
2013-03-24 20:51:32 -07:00
|
|
|
}
|
2013-03-23 17:08:23 -07:00
|
|
|
}
|
|
|
|
}
|