2013-03-24 01:08:23 +01:00
|
|
|
|
using System;
|
2013-03-25 04:51:32 +01:00
|
|
|
|
using Marr.Data;
|
|
|
|
|
using Mono.Data.Sqlite;
|
2013-03-25 07:13:53 +01:00
|
|
|
|
using NzbDrone.Core.Datastore.Migration.Framework;
|
2013-03-25 04:51:32 +01:00
|
|
|
|
|
2013-03-24 01:08:23 +01:00
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Datastore
|
|
|
|
|
{
|
|
|
|
|
public interface IDbFactory
|
|
|
|
|
{
|
2013-03-25 07:13:53 +01:00
|
|
|
|
IDatabase Create(string dbPath, 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-03-24 01:08:23 +01:00
|
|
|
|
|
2013-03-25 07:13:53 +01:00
|
|
|
|
public DbFactory(IMigrationController migrationController)
|
2013-03-24 01:08:23 +01:00
|
|
|
|
{
|
2013-03-25 07:13:53 +01:00
|
|
|
|
TableMapping.Map();
|
|
|
|
|
_migrationController = migrationController;
|
|
|
|
|
}
|
2013-03-24 05:56:59 +01:00
|
|
|
|
|
2013-03-25 07:13:53 +01:00
|
|
|
|
public IDatabase Create(string dbPath, MigrationType migrationType = MigrationType.Main)
|
|
|
|
|
{
|
|
|
|
|
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-03-25 08:36:22 +01:00
|
|
|
|
var dataMapper = new DataMapper(SqliteFactory.Instance, connectionString)
|
|
|
|
|
{
|
|
|
|
|
SqlMode = SqlModes.Text,
|
|
|
|
|
};
|
2013-03-25 04:51:32 +01:00
|
|
|
|
return new Database(dataMapper);
|
|
|
|
|
}
|
2013-03-25 05:36:24 +01:00
|
|
|
|
|
2013-03-24 01:08:23 +01:00
|
|
|
|
private string GetConnectionString(string dbPath)
|
|
|
|
|
{
|
|
|
|
|
return String.Format("Data Source={0};Version=3;", dbPath);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|