2013-03-24 01:08:23 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.Data;
|
2013-03-25 04:51:32 +01:00
|
|
|
|
using Marr.Data;
|
|
|
|
|
using Mono.Data.Sqlite;
|
|
|
|
|
|
2013-03-24 01:08:23 +01:00
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Datastore
|
|
|
|
|
{
|
|
|
|
|
public interface IDbFactory
|
|
|
|
|
{
|
2013-03-25 04:51:32 +01:00
|
|
|
|
IDatabase Create(string dbPath = null);
|
2013-03-24 01:08:23 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class DbFactory : IDbFactory
|
|
|
|
|
{
|
2013-03-24 05:56:59 +01:00
|
|
|
|
private const string MemoryConnectionString = "Data Source=:memory:;Version=3;New=True;";
|
2013-03-24 01:08:23 +01:00
|
|
|
|
|
2013-03-25 04:51:32 +01:00
|
|
|
|
public IDatabase Create(string dbPath = null)
|
2013-03-24 01:08:23 +01:00
|
|
|
|
{
|
2013-03-24 05:56:59 +01:00
|
|
|
|
var connectionString = MemoryConnectionString;
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(dbPath))
|
2013-03-24 01:08:23 +01:00
|
|
|
|
{
|
2013-03-24 05:56:59 +01:00
|
|
|
|
connectionString = GetConnectionString(dbPath);
|
2013-03-24 01:08:23 +01:00
|
|
|
|
}
|
|
|
|
|
|
2013-03-25 05:36:24 +01:00
|
|
|
|
MigrationHelper.MigrateToLatest(connectionString, MigrationType.Main);
|
2013-03-25 04:51:32 +01:00
|
|
|
|
var dataMapper = new DataMapper(SqliteFactory.Instance, connectionString);
|
|
|
|
|
return new Database(dataMapper);
|
|
|
|
|
}
|
2013-03-25 05:36:24 +01:00
|
|
|
|
|
2013-03-24 11:09:32 +01:00
|
|
|
|
|
2013-03-24 01:08:23 +01:00
|
|
|
|
|
|
|
|
|
private string GetConnectionString(string dbPath)
|
|
|
|
|
{
|
|
|
|
|
return String.Format("Data Source={0};Version=3;", dbPath);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|