2013-03-24 00:08:23 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Data;
|
|
|
|
|
using ServiceStack.OrmLite;
|
2013-03-24 04:56:59 +00:00
|
|
|
|
using ServiceStack.OrmLite.Sqlite;
|
2013-03-24 00:08:23 +00:00
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Datastore
|
|
|
|
|
{
|
|
|
|
|
public interface IDbFactory
|
|
|
|
|
{
|
|
|
|
|
IDbConnection Create(string dbPath = null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class DbFactory : IDbFactory
|
|
|
|
|
{
|
2013-03-24 04:56:59 +00:00
|
|
|
|
private const string MemoryConnectionString = "Data Source=:memory:;Version=3;New=True;";
|
2013-03-24 00:08:23 +00:00
|
|
|
|
|
2013-03-24 04:56:59 +00:00
|
|
|
|
static DbFactory()
|
2013-03-24 00:08:23 +00:00
|
|
|
|
{
|
2013-03-24 04:56:59 +00:00
|
|
|
|
OrmLiteConfig.DialectProvider = new SqliteOrmLiteDialectProvider();
|
2013-03-24 00:08:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IDbConnection Create(string dbPath = null)
|
|
|
|
|
{
|
2013-03-24 04:56:59 +00:00
|
|
|
|
var connectionString = MemoryConnectionString;
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(dbPath))
|
2013-03-24 00:08:23 +00:00
|
|
|
|
{
|
2013-03-24 04:56:59 +00:00
|
|
|
|
connectionString = GetConnectionString(dbPath);
|
2013-03-24 00:08:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-03-25 04:36:24 +00:00
|
|
|
|
MigrationHelper.MigrateToLatest(connectionString, MigrationType.Main);
|
|
|
|
|
|
2013-03-24 05:50:51 +00:00
|
|
|
|
OrmLiteConfig.DialectProvider = new SqliteOrmLiteDialectProvider();
|
2013-03-24 04:56:59 +00:00
|
|
|
|
var dbFactory = new OrmLiteConnectionFactory(connectionString);
|
2013-03-24 10:09:32 +00:00
|
|
|
|
var connection = dbFactory.Open();
|
|
|
|
|
|
|
|
|
|
Migration.CreateTables(connection);
|
|
|
|
|
|
|
|
|
|
return connection;
|
2013-03-24 00:08:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string GetConnectionString(string dbPath)
|
|
|
|
|
{
|
|
|
|
|
return String.Format("Data Source={0};Version=3;", dbPath);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|