mirror of
https://github.com/Readarr/Readarr.git
synced 2026-04-24 22:35:39 -04:00
Swap to dapper with lazyload
This commit is contained in:
@@ -2,7 +2,6 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Data.SQLite;
|
||||
using System.Linq;
|
||||
using Marr.Data;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using NUnit.Framework;
|
||||
@@ -112,7 +111,7 @@ namespace NzbDrone.Core.Test.Framework
|
||||
Mocker.SetConstant<IConnectionStringFactory>(Mocker.Resolve<ConnectionStringFactory>());
|
||||
Mocker.SetConstant<IMigrationController>(Mocker.Resolve<MigrationController>());
|
||||
|
||||
MapRepository.Instance.EnableTraceLogging = true;
|
||||
SqlBuilderExtensions.LogSql = true;
|
||||
}
|
||||
|
||||
[SetUp]
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Data.Common;
|
||||
using System.Linq;
|
||||
using NzbDrone.Common.Serializer;
|
||||
using NzbDrone.Core.Datastore;
|
||||
@@ -13,32 +12,20 @@ namespace NzbDrone.Core.Test.Framework
|
||||
List<Dictionary<string, object>> Query(string sql);
|
||||
List<T> Query<T>(string sql)
|
||||
where T : new();
|
||||
T QueryScalar<T>(string sql);
|
||||
}
|
||||
|
||||
public class DirectDataMapper : IDirectDataMapper
|
||||
{
|
||||
private readonly DbProviderFactory _providerFactory;
|
||||
private readonly string _connectionString;
|
||||
private readonly IDatabase _database;
|
||||
|
||||
public DirectDataMapper(IDatabase database)
|
||||
{
|
||||
var dataMapper = database.GetDataMapper();
|
||||
_providerFactory = dataMapper.ProviderFactory;
|
||||
_connectionString = dataMapper.ConnectionString;
|
||||
}
|
||||
|
||||
private DbConnection OpenConnection()
|
||||
{
|
||||
var connection = _providerFactory.CreateConnection();
|
||||
connection.ConnectionString = _connectionString;
|
||||
connection.Open();
|
||||
return connection;
|
||||
_database = database;
|
||||
}
|
||||
|
||||
public DataTable GetDataTable(string sql)
|
||||
{
|
||||
using (var connection = OpenConnection())
|
||||
using (var connection = _database.OpenConnection())
|
||||
{
|
||||
using (var cmd = connection.CreateCommand())
|
||||
{
|
||||
@@ -65,13 +52,6 @@ namespace NzbDrone.Core.Test.Framework
|
||||
return dataTable.Rows.Cast<DataRow>().Select(MapToObject<T>).ToList();
|
||||
}
|
||||
|
||||
public T QueryScalar<T>(string sql)
|
||||
{
|
||||
var dataTable = GetDataTable(sql);
|
||||
|
||||
return dataTable.Rows.Cast<DataRow>().Select(d => MapValue(d, 0, typeof(T))).Cast<T>().FirstOrDefault();
|
||||
}
|
||||
|
||||
protected Dictionary<string, object> MapToDictionary(DataRow dataRow)
|
||||
{
|
||||
var item = new Dictionary<string, object>();
|
||||
@@ -118,28 +98,24 @@ namespace NzbDrone.Core.Test.Framework
|
||||
propertyType = propertyType.GetGenericArguments()[0];
|
||||
}
|
||||
|
||||
object value = MapValue(dataRow, i, propertyType);
|
||||
object value;
|
||||
if (dataRow.ItemArray[i] == DBNull.Value)
|
||||
{
|
||||
value = null;
|
||||
}
|
||||
else if (dataRow.Table.Columns[i].DataType == typeof(string) && propertyType != typeof(string))
|
||||
{
|
||||
value = Json.Deserialize((string)dataRow.ItemArray[i], propertyType);
|
||||
}
|
||||
else
|
||||
{
|
||||
value = Convert.ChangeType(dataRow.ItemArray[i], propertyType);
|
||||
}
|
||||
|
||||
propertyInfo.SetValue(item, value, null);
|
||||
}
|
||||
|
||||
return item;
|
||||
}
|
||||
|
||||
private object MapValue(DataRow dataRow, int i, Type targetType)
|
||||
{
|
||||
if (dataRow.ItemArray[i] == DBNull.Value)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
else if (dataRow.Table.Columns[i].DataType == typeof(string) && targetType != typeof(string))
|
||||
{
|
||||
return Json.Deserialize((string)dataRow.ItemArray[i], targetType);
|
||||
}
|
||||
else
|
||||
{
|
||||
return Convert.ChangeType(dataRow.ItemArray[i], targetType);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using Moq;
|
||||
using NzbDrone.Core.Datastore;
|
||||
@@ -21,6 +22,7 @@ namespace NzbDrone.Core.Test.Framework
|
||||
void Delete<T>(T childModel)
|
||||
where T : ModelBase, new();
|
||||
IDirectDataMapper GetDirectDataMapper();
|
||||
IDbConnection OpenConnection();
|
||||
}
|
||||
|
||||
public class TestDatabase : ITestDatabase
|
||||
@@ -74,5 +76,10 @@ namespace NzbDrone.Core.Test.Framework
|
||||
{
|
||||
return new DirectDataMapper(_dbConnection);
|
||||
}
|
||||
|
||||
public IDbConnection OpenConnection()
|
||||
{
|
||||
return _dbConnection.OpenConnection();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user