2013-03-26 07:50:18 +00:00
|
|
|
|
using System;
|
2013-03-26 22:17:13 +00:00
|
|
|
|
using System.Collections.Generic;
|
2013-03-26 07:50:18 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Reflection;
|
2013-04-01 00:20:01 +00:00
|
|
|
|
using Marr.Data;
|
2013-03-26 07:50:18 +00:00
|
|
|
|
using Marr.Data.Mapping;
|
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Datastore
|
|
|
|
|
{
|
|
|
|
|
public static class MappingExtensions
|
|
|
|
|
{
|
|
|
|
|
public static ColumnMapBuilder<T> RegisterModel<T>(this FluentMappings.MappingsFluentEntity<T> mapBuilder, string tableName) where T : ModelBase
|
|
|
|
|
{
|
|
|
|
|
return mapBuilder.Table.MapTable(tableName)
|
|
|
|
|
.Columns
|
|
|
|
|
.AutoMapPropertiesWhere(IsMappableProperty)
|
|
|
|
|
.For(c => c.Id)
|
|
|
|
|
.SetPrimaryKey()
|
|
|
|
|
.SetReturnValue()
|
|
|
|
|
.SetAutoIncrement();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool IsMappableProperty(MemberInfo memberInfo)
|
|
|
|
|
{
|
|
|
|
|
var propertyInfo = memberInfo as PropertyInfo;
|
|
|
|
|
|
|
|
|
|
if (propertyInfo == null) return false;
|
|
|
|
|
|
2013-04-01 00:20:01 +00:00
|
|
|
|
|
|
|
|
|
if (!propertyInfo.IsReadable() || !propertyInfo.IsWritable())
|
2013-03-26 07:50:18 +00:00
|
|
|
|
{
|
2013-04-01 00:20:01 +00:00
|
|
|
|
return false;
|
2013-03-26 07:50:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-04-01 00:20:01 +00:00
|
|
|
|
if (IsSimpleType(propertyInfo.PropertyType) || MapRepository.Instance.TypeConverters.ContainsKey(propertyInfo.PropertyType))
|
2013-03-26 22:17:13 +00:00
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-01 00:20:01 +00:00
|
|
|
|
return false;
|
2013-03-26 07:50:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool IsSimpleType(Type type)
|
|
|
|
|
{
|
|
|
|
|
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
|
|
|
|
|
{
|
|
|
|
|
type = type.GetGenericArguments()[0];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return type.IsPrimitive
|
|
|
|
|
|| type.IsEnum
|
|
|
|
|
|| type == typeof(string)
|
|
|
|
|
|| type == typeof(DateTime)
|
|
|
|
|
|| type == typeof(Decimal);
|
|
|
|
|
}
|
2013-03-26 22:17:13 +00:00
|
|
|
|
|
|
|
|
|
private static bool IsReadable(this PropertyInfo propertyInfo)
|
|
|
|
|
{
|
|
|
|
|
return propertyInfo.CanRead && propertyInfo.GetGetMethod(false) != null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static bool IsWritable(this PropertyInfo propertyInfo)
|
|
|
|
|
{
|
|
|
|
|
return propertyInfo.CanWrite && propertyInfo.GetSetMethod(false) != null;
|
|
|
|
|
}
|
2013-03-26 07:50:18 +00:00
|
|
|
|
}
|
|
|
|
|
}
|