mirror of
https://github.com/Radarr/Radarr.git
synced 2024-11-05 18:42:42 +01:00
4bb4faf626
Still need to remove System.Data.Sqlite, prefer an option in OrmLite to pluralize table names.
48 lines
1.3 KiB
C#
48 lines
1.3 KiB
C#
using System;
|
|
using System.Data;
|
|
using ServiceStack.OrmLite;
|
|
using ServiceStack.OrmLite.Sqlite;
|
|
|
|
namespace NzbDrone.Core.Datastore
|
|
{
|
|
public interface IDbFactory
|
|
{
|
|
IDbConnection Create(string dbPath = null);
|
|
}
|
|
|
|
public class DbFactory : IDbFactory
|
|
{
|
|
private const string MemoryConnectionString = "Data Source=:memory:;Version=3;New=True;";
|
|
|
|
static DbFactory()
|
|
{
|
|
OrmLiteConfig.DialectProvider = new SqliteOrmLiteDialectProvider();
|
|
}
|
|
|
|
public IDbConnection Create(string dbPath = null)
|
|
{
|
|
var connectionString = MemoryConnectionString;
|
|
|
|
if (!string.IsNullOrWhiteSpace(dbPath))
|
|
{
|
|
connectionString = GetConnectionString(dbPath);
|
|
}
|
|
|
|
MigrationHelper.MigrateToLatest(connectionString, MigrationType.Main);
|
|
|
|
OrmLiteConfig.DialectProvider = new SqliteOrmLiteDialectProvider();
|
|
var dbFactory = new OrmLiteConnectionFactory(connectionString);
|
|
var connection = dbFactory.Open();
|
|
|
|
Migration.CreateTables(connection);
|
|
|
|
return connection;
|
|
}
|
|
|
|
private string GetConnectionString(string dbPath)
|
|
{
|
|
return String.Format("Data Source={0};Version=3;", dbPath);
|
|
}
|
|
}
|
|
}
|