2013-03-24 01:08:23 +01:00
|
|
|
|
using System;
|
2013-03-26 06:51:56 +01:00
|
|
|
|
using System.Data.SQLite;
|
2013-03-25 04:51:32 +01:00
|
|
|
|
using Marr.Data;
|
2013-04-02 01:55:09 +02:00
|
|
|
|
using Marr.Data.Reflection;
|
2013-06-28 03:03:04 +02:00
|
|
|
|
using NzbDrone.Common.Composition;
|
|
|
|
|
using NzbDrone.Common.EnvironmentInfo;
|
|
|
|
|
using NzbDrone.Common.Messaging;
|
2013-03-25 07:13:53 +01:00
|
|
|
|
using NzbDrone.Core.Datastore.Migration.Framework;
|
2013-06-28 03:03:04 +02:00
|
|
|
|
using NzbDrone.Common;
|
|
|
|
|
using NzbDrone.Core.Instrumentation;
|
2013-03-25 04:51:32 +01:00
|
|
|
|
|
2013-03-24 01:08:23 +01:00
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Datastore
|
|
|
|
|
{
|
|
|
|
|
public interface IDbFactory
|
|
|
|
|
{
|
2013-06-28 03:03:04 +02:00
|
|
|
|
IDatabase Create(MigrationType migrationType = MigrationType.Main);
|
2013-03-24 01:08:23 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class DbFactory : IDbFactory
|
|
|
|
|
{
|
2013-03-25 07:13:53 +01:00
|
|
|
|
private readonly IMigrationController _migrationController;
|
2013-06-28 03:03:04 +02:00
|
|
|
|
private readonly IAppDirectoryInfo _appDirectoryInfo;
|
2013-03-24 01:08:23 +01:00
|
|
|
|
|
2013-03-30 23:14:33 +01:00
|
|
|
|
static DbFactory()
|
2013-03-24 01:08:23 +01:00
|
|
|
|
{
|
2013-06-03 05:15:56 +02:00
|
|
|
|
MapRepository.Instance.ReflectionStrategy = new SimpleReflectionStrategy();
|
2013-03-25 07:13:53 +01:00
|
|
|
|
TableMapping.Map();
|
2013-03-30 23:14:33 +01:00
|
|
|
|
}
|
|
|
|
|
|
2013-06-28 03:03:04 +02:00
|
|
|
|
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, IAppDirectoryInfo appDirectoryInfo)
|
2013-03-30 23:14:33 +01:00
|
|
|
|
{
|
2013-03-25 07:13:53 +01:00
|
|
|
|
_migrationController = migrationController;
|
2013-06-28 03:03:04 +02:00
|
|
|
|
_appDirectoryInfo = appDirectoryInfo;
|
2013-03-25 07:13:53 +01:00
|
|
|
|
}
|
2013-03-24 05:56:59 +01:00
|
|
|
|
|
2013-06-28 03:03:04 +02:00
|
|
|
|
public IDatabase Create(MigrationType migrationType = MigrationType.Main)
|
2013-03-25 07:13:53 +01:00
|
|
|
|
{
|
2013-06-28 03:03:04 +02:00
|
|
|
|
string dbPath;
|
|
|
|
|
|
|
|
|
|
switch (migrationType)
|
|
|
|
|
{
|
|
|
|
|
case MigrationType.Main:
|
|
|
|
|
{
|
|
|
|
|
dbPath = _appDirectoryInfo.GetNzbDroneDatabase();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case MigrationType.Log:
|
|
|
|
|
{
|
|
|
|
|
dbPath = _appDirectoryInfo.GetLogDatabase();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("Invalid MigrationType");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2013-03-25 07:13:53 +01:00
|
|
|
|
var connectionString = GetConnectionString(dbPath);
|
2013-03-24 01:08:23 +01:00
|
|
|
|
|
2013-03-25 07:13:53 +01:00
|
|
|
|
_migrationController.MigrateToLatest(connectionString, migrationType);
|
2013-05-11 22:06:57 +02:00
|
|
|
|
|
|
|
|
|
return new Database(() =>
|
|
|
|
|
{
|
|
|
|
|
var dataMapper = new DataMapper(SQLiteFactory.Instance, connectionString)
|
|
|
|
|
{
|
|
|
|
|
SqlMode = SqlModes.Text,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return dataMapper;
|
|
|
|
|
});
|
2013-03-25 04:51:32 +01:00
|
|
|
|
}
|
2013-03-25 05:36:24 +01:00
|
|
|
|
|
2013-03-24 01:08:23 +01:00
|
|
|
|
private string GetConnectionString(string dbPath)
|
|
|
|
|
{
|
2013-05-23 04:14:30 +02:00
|
|
|
|
var connectionBuilder = new SQLiteConnectionStringBuilder();
|
|
|
|
|
|
|
|
|
|
connectionBuilder.DataSource = dbPath;
|
|
|
|
|
connectionBuilder.CacheSize = (int)-10.Megabytes();
|
|
|
|
|
connectionBuilder.DateTimeKind = DateTimeKind.Utc;
|
2013-05-24 03:41:32 +02:00
|
|
|
|
connectionBuilder.JournalMode = SQLiteJournalModeEnum.Wal;
|
2013-05-23 04:14:30 +02:00
|
|
|
|
|
|
|
|
|
return connectionBuilder.ConnectionString;
|
2013-03-24 01:08:23 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|