You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Prowlarr/NzbDrone.Core/Datastore/CustomeMapper.cs

75 lines
2.7 KiB

14 years ago
using System;
using System.Reflection;
using NzbDrone.Core.Qualities;
14 years ago
using PetaPoco;
namespace NzbDrone.Core.Datastore
{
public class CustomeMapper : DefaultMapper
{
public override Func<object, object> GetToDbConverter(Type sourceType)
14 years ago
{
if (sourceType == typeof(Quality))
{
return delegate(object s)
{
var source = (Quality)s;
return source.Id;
};
}
return base.GetToDbConverter(sourceType);
}
14 years ago
public override Func<object, object> GetFromDbConverter(Type destinationType, Type sourceType)
{
if ((sourceType == typeof(Int32) || sourceType == typeof(Int64)) && destinationType.IsGenericType && destinationType.GetGenericTypeDefinition() == typeof(Nullable<>))
14 years ago
{
// If it is NULLABLE, then get the underlying type. eg if "Nullable<int>" then this will return just "int"
Type genericArgument = destinationType.GetGenericArguments()[0];
if (genericArgument == typeof(DayOfWeek))
14 years ago
{
return delegate(object s)
{
int value;
Int32.TryParse(s.ToString(), out value);
return (DayOfWeek?)value;
14 years ago
};
}
return delegate(object s)
{
int value;
Int32.TryParse(s.ToString(), out value);
return value;
};
14 years ago
}
if ((sourceType == typeof(Int32) || sourceType == typeof(Int64)) && destinationType == typeof(Quality))
{
return delegate(object s)
{
int value;
Int32.TryParse(s.ToString(), out value);
var quality = (Quality)value;
return quality;
};
}
return base.GetFromDbConverter(destinationType, sourceType);
}
public override Func<object, object> GetFromDbConverter(PropertyInfo propertyInfo, Type sourceType)
{
//Only needed if using dynamic as the return type from DB, not implemented currently as it has no use right now
//if (propertyInfo == null)
// return null;
if (propertyInfo == null) return base.GetFromDbConverter(propertyInfo, sourceType);
return GetFromDbConverter(propertyInfo.PropertyType, sourceType);
14 years ago
}
}
14 years ago
}