1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-11-01 00:12:30 +01:00
Sonarr/NzbDrone.Core/Datastore/CustomeMapper.cs

75 lines
2.7 KiB
C#
Raw Normal View History

2011-06-15 04:31:41 +02:00
using System;
2011-07-03 01:12:20 +02:00
using System.Reflection;
2012-10-14 02:36:16 +02:00
using NzbDrone.Core.Repository.Quality;
2011-06-15 04:31:41 +02:00
using PetaPoco;
namespace NzbDrone.Core.Datastore
{
public class CustomeMapper : DefaultMapper
{
2012-10-14 02:36:16 +02:00
public override Func<object, object> GetToDbConverter(Type sourceType)
2011-06-15 04:31:41 +02:00
{
2012-10-14 02:36:16 +02:00
if (sourceType == typeof(QualityTypes))
{
return delegate(object s)
{
var source = (QualityTypes)s;
return source.Id;
};
}
return base.GetToDbConverter(sourceType);
}
2011-06-15 04:31:41 +02:00
2012-10-14 02:36:16 +02:00
public override Func<object, object> GetFromDbConverter(Type destinationType, Type sourceType)
{
2011-07-03 01:12:20 +02:00
if ((sourceType == typeof(Int32) || sourceType == typeof(Int64)) && destinationType.IsGenericType && destinationType.GetGenericTypeDefinition() == typeof(Nullable<>))
2011-06-15 04:31:41 +02:00
{
// If it is NULLABLE, then get the underlying type. eg if "Nullable<int>" then this will return just "int"
2011-07-03 01:12:20 +02:00
Type genericArgument = destinationType.GetGenericArguments()[0];
2011-06-16 08:33:01 +02:00
if (genericArgument == typeof(DayOfWeek))
2011-06-15 04:31:41 +02:00
{
return delegate(object s)
{
int value;
Int32.TryParse(s.ToString(), out value);
2011-06-18 10:29:38 +02:00
return (DayOfWeek?)value;
2011-06-15 04:31:41 +02:00
};
}
2011-06-16 08:33:01 +02:00
2011-06-18 10:29:38 +02:00
return delegate(object s)
{
int value;
Int32.TryParse(s.ToString(), out value);
return value;
};
2011-06-15 04:31:41 +02:00
}
2012-10-14 02:36:16 +02:00
if ((sourceType == typeof(Int32) || sourceType == typeof(Int64)) && destinationType == typeof(QualityTypes))
{
return delegate(object s)
{
int value;
Int32.TryParse(s.ToString(), out value);
var quality = (QualityTypes)value;
return quality;
};
}
2011-07-03 01:12:20 +02:00
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);
2011-07-03 01:12:20 +02:00
return GetFromDbConverter(propertyInfo.PropertyType, sourceType);
2011-06-15 04:31:41 +02:00
}
}
2011-07-03 01:12:20 +02:00
2011-06-15 04:31:41 +02:00
}