2013-07-05 03:56:27 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Data.SQLite;
|
2022-01-26 00:08:27 +00:00
|
|
|
|
using Npgsql;
|
2013-07-05 03:56:27 +00:00
|
|
|
|
using NzbDrone.Common.EnvironmentInfo;
|
2014-12-02 06:26:25 +00:00
|
|
|
|
using NzbDrone.Common.Extensions;
|
2022-01-26 00:08:27 +00:00
|
|
|
|
using NzbDrone.Core.Configuration;
|
2013-07-05 03:56:27 +00:00
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Datastore
|
|
|
|
|
{
|
|
|
|
|
public interface IConnectionStringFactory
|
|
|
|
|
{
|
|
|
|
|
string MainDbConnectionString { get; }
|
|
|
|
|
string LogDbConnectionString { get; }
|
2016-07-20 01:57:36 +00:00
|
|
|
|
string GetDatabasePath(string connectionString);
|
2013-07-05 03:56:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class ConnectionStringFactory : IConnectionStringFactory
|
|
|
|
|
{
|
2022-01-26 00:08:27 +00:00
|
|
|
|
private readonly IConfigFileProvider _configFileProvider;
|
|
|
|
|
|
|
|
|
|
public ConnectionStringFactory(IAppFolderInfo appFolderInfo, IConfigFileProvider configFileProvider)
|
2013-07-05 03:56:27 +00:00
|
|
|
|
{
|
2022-01-26 00:08:27 +00:00
|
|
|
|
_configFileProvider = configFileProvider;
|
|
|
|
|
|
|
|
|
|
MainDbConnectionString = _configFileProvider.PostgresHost.IsNotNullOrWhiteSpace() ? GetPostgresConnectionString(_configFileProvider.PostgresMainDb) :
|
|
|
|
|
GetConnectionString(appFolderInfo.GetDatabase());
|
|
|
|
|
|
|
|
|
|
LogDbConnectionString = _configFileProvider.PostgresHost.IsNotNullOrWhiteSpace() ? GetPostgresConnectionString(_configFileProvider.PostgresLogDb) :
|
|
|
|
|
GetConnectionString(appFolderInfo.GetLogDatabase());
|
2013-07-05 03:56:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string MainDbConnectionString { get; private set; }
|
|
|
|
|
public string LogDbConnectionString { get; private set; }
|
|
|
|
|
|
2016-07-20 01:57:36 +00:00
|
|
|
|
public string GetDatabasePath(string connectionString)
|
|
|
|
|
{
|
|
|
|
|
var connectionBuilder = new SQLiteConnectionStringBuilder(connectionString);
|
|
|
|
|
|
|
|
|
|
return connectionBuilder.DataSource;
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-05 03:56:27 +00:00
|
|
|
|
private static string GetConnectionString(string dbPath)
|
|
|
|
|
{
|
|
|
|
|
var connectionBuilder = new SQLiteConnectionStringBuilder();
|
|
|
|
|
|
|
|
|
|
connectionBuilder.DataSource = dbPath;
|
2020-01-03 12:49:24 +00:00
|
|
|
|
connectionBuilder.CacheSize = -10000;
|
2013-07-05 03:56:27 +00:00
|
|
|
|
connectionBuilder.DateTimeKind = DateTimeKind.Utc;
|
2015-01-06 01:09:40 +00:00
|
|
|
|
connectionBuilder.JournalMode = OsInfo.IsOsx ? SQLiteJournalModeEnum.Truncate : SQLiteJournalModeEnum.Wal;
|
2014-10-16 02:10:25 +00:00
|
|
|
|
connectionBuilder.Pooling = true;
|
2015-01-20 18:17:10 +00:00
|
|
|
|
connectionBuilder.Version = 3;
|
2020-01-03 12:49:24 +00:00
|
|
|
|
|
2015-02-15 21:55:17 +00:00
|
|
|
|
if (OsInfo.IsOsx)
|
|
|
|
|
{
|
|
|
|
|
connectionBuilder.Add("Full FSync", true);
|
|
|
|
|
}
|
2013-07-05 03:56:27 +00:00
|
|
|
|
|
|
|
|
|
return connectionBuilder.ConnectionString;
|
|
|
|
|
}
|
2022-01-26 00:08:27 +00:00
|
|
|
|
|
|
|
|
|
private string GetPostgresConnectionString(string dbName)
|
|
|
|
|
{
|
|
|
|
|
var connectionBuilder = new NpgsqlConnectionStringBuilder();
|
|
|
|
|
|
|
|
|
|
connectionBuilder.Database = dbName;
|
|
|
|
|
connectionBuilder.Host = _configFileProvider.PostgresHost;
|
|
|
|
|
connectionBuilder.Username = _configFileProvider.PostgresUser;
|
|
|
|
|
connectionBuilder.Password = _configFileProvider.PostgresPassword;
|
|
|
|
|
connectionBuilder.Port = _configFileProvider.PostgresPort;
|
|
|
|
|
connectionBuilder.Enlist = false;
|
|
|
|
|
|
|
|
|
|
return connectionBuilder.ConnectionString;
|
|
|
|
|
}
|
2013-07-05 03:56:27 +00:00
|
|
|
|
}
|
2020-01-03 12:49:24 +00:00
|
|
|
|
}
|