2013-03-26 08:51:56 +03:00
|
|
|
using System;
|
|
|
|
using Marr.Data.Converters;
|
|
|
|
using Marr.Data.Mapping;
|
2013-05-12 18:18:17 +03:00
|
|
|
using NzbDrone.Common.Serializer;
|
2013-03-26 08:51:56 +03:00
|
|
|
|
|
|
|
namespace NzbDrone.Core.Datastore.Converters
|
|
|
|
{
|
2013-09-21 09:38:27 +03:00
|
|
|
|
|
|
|
public class ProviderSettingConverter : EmbeddedDocumentConverter
|
2013-03-26 08:51:56 +03:00
|
|
|
{
|
2013-09-21 09:38:27 +03:00
|
|
|
public override object FromDB(ConverterContext context)
|
|
|
|
{
|
|
|
|
if (context.DbValue == DBNull.Value)
|
|
|
|
{
|
|
|
|
return DBNull.Value;
|
|
|
|
}
|
2013-04-18 02:32:06 +03:00
|
|
|
|
2013-09-21 09:38:27 +03:00
|
|
|
var stringValue = (string)context.DbValue;
|
|
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(stringValue))
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
var ordinal = context.DataRecord.GetOrdinal("ConfigContract");
|
|
|
|
|
|
|
|
var implementation = context.DataRecord.GetString(ordinal);
|
|
|
|
|
|
|
|
var impType = Type.GetType(implementation, true, true);
|
|
|
|
|
|
|
|
return Json.Deserialize(stringValue, impType);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public class EmbeddedDocumentConverter : IConverter
|
|
|
|
{
|
|
|
|
public virtual object FromDB(ConverterContext context)
|
2013-03-26 08:51:56 +03:00
|
|
|
{
|
2013-09-21 09:38:27 +03:00
|
|
|
if (context.DbValue == DBNull.Value)
|
2013-03-26 08:51:56 +03:00
|
|
|
{
|
|
|
|
return DBNull.Value;
|
|
|
|
}
|
|
|
|
|
2013-09-21 09:38:27 +03:00
|
|
|
var stringValue = (string)context.DbValue;
|
2013-03-26 08:51:56 +03:00
|
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(stringValue))
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2013-09-21 09:38:27 +03:00
|
|
|
return Json.Deserialize(stringValue, context.ColumnMap.FieldType);
|
|
|
|
}
|
|
|
|
|
|
|
|
public object FromDB(ColumnMap map, object dbValue)
|
|
|
|
{
|
|
|
|
return FromDB(new ConverterContext { ColumnMap = map, DbValue = dbValue });
|
2013-03-26 08:51:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public object ToDB(object clrValue)
|
|
|
|
{
|
|
|
|
if (clrValue == null) return null;
|
|
|
|
|
2013-05-30 00:29:51 +03:00
|
|
|
return clrValue.ToJson();
|
2013-03-26 08:51:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public Type DbType
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return typeof(string);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|