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