You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Lidarr/NzbDrone.Core/Datastore/DbFactory.cs

64 lines
1.8 KiB

using System;
using System.Data.SQLite;
using Marr.Data;
using Marr.Data.Reflection;
12 years ago
using NzbDrone.Common.Composition;
using NzbDrone.Core.Datastore.Migration.Framework;
namespace NzbDrone.Core.Datastore
{
public interface IDbFactory
{
IDatabase Create(string dbPath, MigrationType migrationType = MigrationType.Main);
}
12 years ago
[Singleton]
public class DbFactory : IDbFactory
{
private readonly IMigrationController _migrationController;
static DbFactory()
{
TableMapping.Map();
}
public DbFactory(IMigrationController migrationController)
{
_migrationController = migrationController;
}
public IDatabase Create(string dbPath, MigrationType migrationType = MigrationType.Main)
{
var connectionString = GetConnectionString(dbPath);
_migrationController.MigrateToLatest(connectionString, migrationType);
12 years ago
MapRepository.Instance.ReflectionStrategy = new SimpleReflectionStrategy();
12 years ago
return new Database(() =>
{
var dataMapper = new DataMapper(SQLiteFactory.Instance, connectionString)
{
SqlMode = SqlModes.Text,
};
return dataMapper;
});
}
private string GetConnectionString(string dbPath)
{
var connectionBuilder = new SQLiteConnectionStringBuilder();
connectionBuilder.DataSource = dbPath;
connectionBuilder.CacheSize = (int)-10.Megabytes();
connectionBuilder.DateTimeKind = DateTimeKind.Utc;
connectionBuilder.JournalMode = SQLiteJournalModeEnum.Wal;
return connectionBuilder.ConnectionString;
}
}
}