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