1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-12-14 11:23:42 +02:00
Sonarr/NzbDrone.Core/Datastore/ConnectionFactory.cs

167 lines
5.4 KiB
C#
Raw Normal View History

2013-01-19 22:42:06 +03:00
using System;
using System.Collections.Generic;
using System.Configuration;
2013-02-16 22:23:31 +03:00
using System.Data.Common;
2013-02-17 00:10:20 +03:00
using System.Data.OleDb;
using System.Data.SqlClient;
2013-02-16 22:23:31 +03:00
using System.Reflection;
2013-01-19 22:42:06 +03:00
using NLog;
using NzbDrone.Common;
using PetaPoco;
namespace NzbDrone.Core.Datastore
{
public class ConnectionFactory
{
private readonly EnvironmentProvider _environmentProvider;
private static readonly Logger logger = LogManager.GetLogger("ConnectionFactory");
2013-02-16 22:23:31 +03:00
private static readonly DbProviderFactory _factory;
2013-01-19 22:42:06 +03:00
static ConnectionFactory()
{
Database.Mapper = new CustomeMapper();
2013-02-17 00:10:20 +03:00
#if __MonoCS__
#else
2013-01-19 22:42:06 +03:00
var dataSet = (System.Data.DataSet)ConfigurationManager.GetSection("system.data");
dataSet.Tables[0].Rows.Add("Microsoft SQL Server Compact Data Provider 4.0"
, "System.Data.SqlServerCe.4.0"
, ".NET Framework Data Provider for Microsoft SQL Server Compact"
, "System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=4.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91");
2013-02-16 22:23:31 +03:00
var proxyType = Assembly.Load("NzbDrone.SqlCe").GetExportedTypes()[0];
var instance = Activator.CreateInstance(proxyType);
var factoryMethod = proxyType.GetMethod("GetSqlCeProviderFactory");
_factory = (DbProviderFactory)factoryMethod.Invoke(instance, null);
2013-02-17 00:10:20 +03:00
#endif
2013-02-16 22:23:31 +03:00
2013-01-19 22:42:06 +03:00
}
2013-02-16 22:23:31 +03:00
2013-01-19 22:42:06 +03:00
public ConnectionFactory(EnvironmentProvider environmentProvider)
{
_environmentProvider = environmentProvider;
}
public String MainConnectionString
{
get
{
2013-02-04 04:27:34 +03:00
return GetConnectionString(_environmentProvider.GetSqlCeMainDbPath());
2013-01-19 22:42:06 +03:00
}
}
public String LogConnectionString
{
get
{
2013-02-04 04:27:34 +03:00
return GetConnectionString(_environmentProvider.GetSqlCeLogDbPath());
2013-01-19 22:42:06 +03:00
}
}
public static string GetConnectionString(string path)
{
return String.Format("Data Source=\"{0}\"; Max Database Size = 512;", path);
}
public IDatabase GetMainPetaPocoDb(Boolean profiled = true)
{
return GetPetaPocoDb(MainConnectionString, profiled);
}
public IDatabase GetLogPetaPocoDb(Boolean profiled = true)
{
return GetPetaPocoDb(LogConnectionString, profiled);
}
static readonly HashSet<String> initilized = new HashSet<string>();
2013-02-16 22:23:31 +03:00
2013-01-19 22:42:06 +03:00
public static IDatabase GetPetaPocoDb(string connectionString, Boolean profiled = true)
{
2013-02-17 00:10:20 +03:00
#if __MonoCS__
throw new NotSupportedException("SqlCe is not supported in mono");
#endif
2013-01-19 22:42:06 +03:00
lock (initilized)
{
if (!initilized.Contains(connectionString))
{
2013-02-16 22:23:31 +03:00
//VerifyDatabase(connectionString);
2013-01-19 22:42:06 +03:00
MigrationsHelper.Run(connectionString, true);
initilized.Add(connectionString);
}
}
2013-02-16 22:23:31 +03:00
var db = new Database(connectionString, _factory, Database.DBType.SqlServerCE)
2013-01-19 22:42:06 +03:00
{
KeepConnectionAlive = true,
ForceDateTimesToUtc = false,
};
return db;
}
2013-02-16 22:23:31 +03:00
/*private static void VerifyDatabase(string connectionString)
2013-01-19 22:42:06 +03:00
{
logger.Debug("Verifying database {0}", connectionString);
var sqlConnection = new SqlCeConnection(connectionString);
if (!File.Exists(sqlConnection.Database))
{
logger.Debug("database file doesn't exist. {0}", sqlConnection.Database);
return;
}
using (var sqlEngine = new SqlCeEngine(connectionString))
{
if (sqlEngine.Verify(VerifyOption.Default))
{
logger.Debug("Database integrity verified.");
}
else
{
logger.Error("Database verification failed.");
RepairDatabase(connectionString);
}
}
}
private static void RepairDatabase(string connectionString)
{
logger.Info("Attempting to repair database: {0}", connectionString);
using (var sqlEngine = new SqlCeEngine(connectionString))
{
try
{
sqlEngine.Repair(connectionString, RepairOption.RecoverAllOrFail);
logger.Info("Recovery was successful without any data loss {0}", connectionString);
}
catch (SqlCeException e)
{
2013-01-20 04:00:20 +03:00
if (e.Message.Contains("file sharing violation"))
{
logger.WarnException("file is in use. skipping.", e);
return;
}
2013-01-19 22:42:06 +03:00
logger.WarnException(
2013-01-25 20:54:45 +03:00
"Safe recovery failed. will attempts a more aggressive strategy. might cause loss of data.",
2013-01-19 22:42:06 +03:00
e);
sqlEngine.Repair(connectionString, RepairOption.DeleteCorruptedRows);
logger.Warn("Database was recovered. some data might have been lost");
//TODO: do db cleanup to avoid broken relationships.
}
}
2013-02-16 22:23:31 +03:00
}*/
2013-01-19 22:42:06 +03:00
}
}