2013-03-24 00:08:23 +00:00
|
|
|
|
using System;
|
2013-03-26 05:51:56 +00:00
|
|
|
|
using System.Data.SQLite;
|
2013-03-25 03:51:32 +00:00
|
|
|
|
using Marr.Data;
|
2013-04-01 23:55:09 +00:00
|
|
|
|
using Marr.Data.Reflection;
|
2013-05-11 20:06:57 +00:00
|
|
|
|
using NzbDrone.Common.Composition;
|
2013-03-25 06:13:53 +00:00
|
|
|
|
using NzbDrone.Core.Datastore.Migration.Framework;
|
2013-03-25 03:51:32 +00:00
|
|
|
|
|
2013-03-24 00:08:23 +00:00
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Datastore
|
|
|
|
|
{
|
|
|
|
|
public interface IDbFactory
|
|
|
|
|
{
|
2013-03-25 06:13:53 +00:00
|
|
|
|
IDatabase Create(string dbPath, MigrationType migrationType = MigrationType.Main);
|
2013-03-24 00:08:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-05-11 20:06:57 +00:00
|
|
|
|
[Singleton]
|
2013-03-24 00:08:23 +00:00
|
|
|
|
public class DbFactory : IDbFactory
|
|
|
|
|
{
|
2013-03-25 06:13:53 +00:00
|
|
|
|
private readonly IMigrationController _migrationController;
|
2013-03-24 00:08:23 +00:00
|
|
|
|
|
2013-03-30 22:14:33 +00:00
|
|
|
|
static DbFactory()
|
2013-03-24 00:08:23 +00:00
|
|
|
|
{
|
2013-03-25 06:13:53 +00:00
|
|
|
|
TableMapping.Map();
|
2013-03-30 22:14:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public DbFactory(IMigrationController migrationController)
|
|
|
|
|
{
|
2013-03-25 06:13:53 +00:00
|
|
|
|
_migrationController = migrationController;
|
|
|
|
|
}
|
2013-03-24 04:56:59 +00:00
|
|
|
|
|
2013-03-25 06:13:53 +00:00
|
|
|
|
public IDatabase Create(string dbPath, MigrationType migrationType = MigrationType.Main)
|
|
|
|
|
{
|
|
|
|
|
var connectionString = GetConnectionString(dbPath);
|
2013-03-24 00:08:23 +00:00
|
|
|
|
|
2013-03-25 06:13:53 +00:00
|
|
|
|
_migrationController.MigrateToLatest(connectionString, migrationType);
|
2013-05-11 20:06:57 +00:00
|
|
|
|
|
2013-04-01 23:55:09 +00:00
|
|
|
|
|
|
|
|
|
MapRepository.Instance.ReflectionStrategy = new SimpleReflectionStrategy();
|
|
|
|
|
|
2013-05-11 20:06:57 +00:00
|
|
|
|
return new Database(() =>
|
|
|
|
|
{
|
|
|
|
|
var dataMapper = new DataMapper(SQLiteFactory.Instance, connectionString)
|
|
|
|
|
{
|
|
|
|
|
SqlMode = SqlModes.Text,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return dataMapper;
|
|
|
|
|
});
|
2013-03-25 03:51:32 +00:00
|
|
|
|
}
|
2013-03-25 04:36:24 +00:00
|
|
|
|
|
2013-03-24 00:08:23 +00:00
|
|
|
|
private string GetConnectionString(string dbPath)
|
|
|
|
|
{
|
2013-05-23 02:14:30 +00:00
|
|
|
|
var connectionBuilder = new SQLiteConnectionStringBuilder();
|
|
|
|
|
|
|
|
|
|
connectionBuilder.DataSource = dbPath;
|
|
|
|
|
connectionBuilder.CacheSize = (int)-10.Megabytes();
|
|
|
|
|
connectionBuilder.DateTimeKind = DateTimeKind.Utc;
|
2013-05-24 01:41:32 +00:00
|
|
|
|
connectionBuilder.JournalMode = SQLiteJournalModeEnum.Wal;
|
|
|
|
|
connectionBuilder.Pooling = true;
|
2013-05-23 02:14:30 +00:00
|
|
|
|
|
|
|
|
|
return connectionBuilder.ConnectionString;
|
2013-03-24 00:08:23 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|