2013-03-26 07:50:18 +00:00
|
|
|
|
using System;
|
|
|
|
|
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;
|
2013-04-21 21:04:09 +00:00
|
|
|
|
using NzbDrone.Common.Reflection;
|
2013-04-20 21:23:17 +00:00
|
|
|
|
using NzbDrone.Core.Tv;
|
2013-03-26 07:50:18 +00:00
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Datastore
|
|
|
|
|
{
|
|
|
|
|
public static class MappingExtensions
|
|
|
|
|
{
|
2013-04-20 21:23:17 +00:00
|
|
|
|
|
|
|
|
|
public static ColumnMapBuilder<T> MapResultSet<T>(this FluentMappings.MappingsFluentEntity<T> mapBuilder) where T : ResultSet, new()
|
|
|
|
|
{
|
|
|
|
|
return mapBuilder
|
|
|
|
|
.Columns
|
|
|
|
|
.AutoMapPropertiesWhere(IsMappableProperty);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static ColumnMapBuilder<T> RegisterModel<T>(this FluentMappings.MappingsFluentEntity<T> mapBuilder, string tableName = null) where T : ModelBase, new()
|
2013-03-26 07:50:18 +00:00
|
|
|
|
{
|
|
|
|
|
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-21 21:04:09 +00:00
|
|
|
|
if (propertyInfo.PropertyType.IsSimpleType() || 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
|
|
|
|
}
|
|
|
|
|
|
2013-03-26 22:17:13 +00:00
|
|
|
|
|
2013-03-26 07:50:18 +00:00
|
|
|
|
}
|
|
|
|
|
}
|