Sonarr/NzbDrone.Core/Datastore/ConnectionFactory.cs

160 lines
5.3 KiB
C#
Raw Normal View History

2013-01-19 19:42:06 +00:00
using System;
using System.Collections.Generic;
using System.Configuration;
2013-02-16 19:23:31 +00:00
using System.Data.Common;
using System.Reflection;
2013-01-19 19:42:06 +00: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 19:23:31 +00:00
private static readonly DbProviderFactory _factory;
2013-01-19 19:42:06 +00:00
static ConnectionFactory()
{
Database.Mapper = new CustomeMapper();
2013-02-16 23:29:21 +00:00
if (EnvironmentProvider.IsMono) return;
2013-02-16 21:10:20 +00:00
2013-01-19 19:42:06 +00:00
var dataSet = (System.Data.DataSet)ConfigurationManager.GetSection("system.data");
dataSet.Tables[0].Rows.Add("Microsoft SQL Server Compact Data Provider 4.0"
2013-02-16 23:29:21 +00:00
, "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:36:29 +00:00
2013-02-16 23:29:21 +00:00
_factory = SqlCeProxy.GetSqlCeProviderFactory();
2013-01-19 19:42:06 +00:00
}
2013-02-16 19:23:31 +00:00
2013-01-19 19:42:06 +00:00
public ConnectionFactory(EnvironmentProvider environmentProvider)
{
_environmentProvider = environmentProvider;
}
public String MainConnectionString
{
get
{
2013-02-04 01:27:34 +00:00
return GetConnectionString(_environmentProvider.GetSqlCeMainDbPath());
2013-01-19 19:42:06 +00:00
}
}
public String LogConnectionString
{
get
{
2013-02-04 01:27:34 +00:00
return GetConnectionString(_environmentProvider.GetSqlCeLogDbPath());
2013-01-19 19:42:06 +00: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 19:23:31 +00:00
2013-01-19 19:42:06 +00:00
public static IDatabase GetPetaPocoDb(string connectionString, Boolean profiled = true)
{
2013-02-16 23:29:21 +00:00
if (EnvironmentProvider.IsMono)
{
throw new NotSupportedException("SqlCe is not supported in mono");
}
2013-02-16 21:10:20 +00:00
2013-01-19 19:42:06 +00:00
lock (initilized)
{
if (!initilized.Contains(connectionString))
{
2013-02-16 19:23:31 +00:00
//VerifyDatabase(connectionString);
2013-01-19 19:42:06 +00:00
MigrationsHelper.Run(connectionString, true);
initilized.Add(connectionString);
}
}
2013-02-16 19:23:31 +00:00
var db = new Database(connectionString, _factory, Database.DBType.SqlServerCE)
2013-01-19 19:42:06 +00:00
{
KeepConnectionAlive = true,
ForceDateTimesToUtc = false,
};
return db;
}
2013-02-16 19:23:31 +00:00
/*private static void VerifyDatabase(string connectionString)
2013-01-19 19:42:06 +00: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 01:00:20 +00:00
if (e.Message.Contains("file sharing violation"))
{
logger.WarnException("file is in use. skipping.", e);
return;
}
2013-01-19 19:42:06 +00:00
logger.WarnException(
2013-01-25 17:54:45 +00:00
"Safe recovery failed. will attempts a more aggressive strategy. might cause loss of data.",
2013-01-19 19:42:06 +00: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 19:23:31 +00:00
}*/
2013-01-19 19:42:06 +00:00
}
}