2011-05-23 23:29:14 +00:00
|
|
|
|
using System;
|
2011-11-23 06:15:02 +00:00
|
|
|
|
using System.Configuration;
|
2011-11-23 05:58:26 +00:00
|
|
|
|
using System.Data.Common;
|
|
|
|
|
using System.Data.SqlServerCe;
|
2011-10-29 04:54:33 +00:00
|
|
|
|
using NzbDrone.Common;
|
2011-11-23 05:58:26 +00:00
|
|
|
|
using NzbDrone.Core.Instrumentation;
|
2011-06-15 02:31:41 +00:00
|
|
|
|
using PetaPoco;
|
2011-05-23 23:29:14 +00:00
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Datastore
|
|
|
|
|
{
|
2011-11-03 05:04:14 +00:00
|
|
|
|
public class Connection
|
2011-05-23 23:29:14 +00:00
|
|
|
|
{
|
2012-03-07 02:59:43 +00:00
|
|
|
|
private readonly EnvironmentProvider _environmentProvider;
|
2011-05-23 23:29:14 +00:00
|
|
|
|
|
2011-12-02 07:07:18 +00:00
|
|
|
|
static Connection()
|
2011-11-23 06:15:02 +00:00
|
|
|
|
{
|
2011-12-02 07:07:18 +00:00
|
|
|
|
Database.Mapper = new CustomeMapper();
|
2011-11-23 06:15:02 +00:00
|
|
|
|
|
2011-12-02 07:07:18 +00:00
|
|
|
|
var dataSet = ConfigurationManager.GetSection("system.data") as System.Data.DataSet;
|
|
|
|
|
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");
|
2011-11-23 06:15:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-03-07 02:59:43 +00:00
|
|
|
|
public Connection(EnvironmentProvider environmentProvider)
|
2011-05-23 23:29:14 +00:00
|
|
|
|
{
|
2012-03-07 02:59:43 +00:00
|
|
|
|
_environmentProvider = environmentProvider;
|
2011-05-23 23:29:14 +00:00
|
|
|
|
}
|
2011-12-02 07:07:18 +00:00
|
|
|
|
|
2011-11-03 05:04:14 +00:00
|
|
|
|
public String MainConnectionString
|
2011-05-23 23:29:14 +00:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2012-08-14 18:51:25 +00:00
|
|
|
|
return GetConnectionString(_environmentProvider.GetNzbDroneDbFile());
|
2011-05-23 23:29:14 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-03 05:04:14 +00:00
|
|
|
|
public String LogConnectionString
|
2011-05-23 23:29:14 +00:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2012-03-07 02:59:43 +00:00
|
|
|
|
return GetConnectionString(_environmentProvider.GetLogDbFileDbFile());
|
2011-05-23 23:29:14 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-03 05:04:14 +00:00
|
|
|
|
public static string GetConnectionString(string path)
|
|
|
|
|
{
|
2012-08-14 18:51:25 +00:00
|
|
|
|
return String.Format("Data Source=\"{0}\"; Max Database Size = 512;", path);
|
2011-11-03 05:04:14 +00:00
|
|
|
|
}
|
2011-11-13 07:27:16 +00:00
|
|
|
|
|
2011-11-03 05:04:14 +00:00
|
|
|
|
public IDatabase GetMainPetaPocoDb(Boolean profiled = true)
|
|
|
|
|
{
|
|
|
|
|
return GetPetaPocoDb(MainConnectionString, profiled);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IDatabase GetLogPetaPocoDb(Boolean profiled = true)
|
|
|
|
|
{
|
|
|
|
|
return GetPetaPocoDb(LogConnectionString, profiled);
|
|
|
|
|
}
|
2011-06-18 02:51:53 +00:00
|
|
|
|
|
2011-11-23 05:58:26 +00:00
|
|
|
|
public LogDbContext GetLogEfContext()
|
|
|
|
|
{
|
|
|
|
|
return GetLogDbContext(LogConnectionString);
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-18 02:51:53 +00:00
|
|
|
|
public static IDatabase GetPetaPocoDb(string connectionString, Boolean profiled = true)
|
2011-06-15 02:31:41 +00:00
|
|
|
|
{
|
2011-06-17 06:58:50 +00:00
|
|
|
|
MigrationsHelper.Run(connectionString, true);
|
2011-06-23 06:56:17 +00:00
|
|
|
|
|
2011-11-23 05:58:26 +00:00
|
|
|
|
var factory = new DbProviderFactory
|
2011-08-28 17:43:33 +00:00
|
|
|
|
{
|
|
|
|
|
IsProfiled = profiled
|
|
|
|
|
};
|
2011-06-17 03:36:52 +00:00
|
|
|
|
|
2011-08-28 17:43:33 +00:00
|
|
|
|
var db = new Database(connectionString, factory, Database.DBType.SqlServerCE)
|
|
|
|
|
{
|
|
|
|
|
KeepConnectionAlive = true,
|
|
|
|
|
ForceDateTimesToUtc = false,
|
|
|
|
|
};
|
2011-06-15 02:31:41 +00:00
|
|
|
|
|
|
|
|
|
return db;
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-23 05:58:26 +00:00
|
|
|
|
public static LogDbContext GetLogDbContext(string connectionString)
|
|
|
|
|
{
|
|
|
|
|
MigrationsHelper.Run(connectionString, true);
|
|
|
|
|
DbConnection connection = new SqlCeConnection(connectionString);
|
|
|
|
|
return new LogDbContext(connection);
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-23 23:29:14 +00:00
|
|
|
|
}
|
|
|
|
|
}
|