2013-03-26 08:50:18 +01:00
|
|
|
|
using System;
|
2013-06-09 22:50:57 +02:00
|
|
|
|
using System.Collections.Generic;
|
2013-03-26 08:50:18 +01:00
|
|
|
|
using System.Reflection;
|
2013-04-01 02:20:01 +02:00
|
|
|
|
using Marr.Data;
|
2013-03-26 08:50:18 +01:00
|
|
|
|
using Marr.Data.Mapping;
|
2013-04-21 23:04:09 +02:00
|
|
|
|
using NzbDrone.Common.Reflection;
|
2013-04-20 23:23:17 +02:00
|
|
|
|
using NzbDrone.Core.Tv;
|
2013-03-26 08:50:18 +01:00
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Datastore
|
|
|
|
|
{
|
|
|
|
|
public static class MappingExtensions
|
|
|
|
|
{
|
2013-04-20 23:23:17 +02: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 08:50:18 +01:00
|
|
|
|
{
|
|
|
|
|
return mapBuilder.Table.MapTable(tableName)
|
|
|
|
|
.Columns
|
|
|
|
|
.AutoMapPropertiesWhere(IsMappableProperty)
|
2013-05-10 17:19:29 +02:00
|
|
|
|
.PrefixAltNames(String.Format("{0}_", typeof(T).Name))
|
2013-03-26 08:50:18 +01:00
|
|
|
|
.For(c => c.Id)
|
|
|
|
|
.SetPrimaryKey()
|
|
|
|
|
.SetReturnValue()
|
|
|
|
|
.SetAutoIncrement();
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-09 22:50:57 +02:00
|
|
|
|
public static RelationshipBuilder<T> AutoMapChildModels<T>(this ColumnMapBuilder<T> mapBuilder)
|
|
|
|
|
{
|
|
|
|
|
return mapBuilder.Relationships.AutoMapPropertiesWhere(m =>
|
|
|
|
|
m.MemberType == MemberTypes.Property &&
|
|
|
|
|
typeof(ModelBase).IsAssignableFrom(((PropertyInfo) m).PropertyType));
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-26 08:50:18 +01:00
|
|
|
|
public static bool IsMappableProperty(MemberInfo memberInfo)
|
|
|
|
|
{
|
|
|
|
|
var propertyInfo = memberInfo as PropertyInfo;
|
|
|
|
|
|
|
|
|
|
if (propertyInfo == null) return false;
|
|
|
|
|
|
2013-04-01 02:20:01 +02:00
|
|
|
|
|
|
|
|
|
if (!propertyInfo.IsReadable() || !propertyInfo.IsWritable())
|
2013-03-26 08:50:18 +01:00
|
|
|
|
{
|
2013-04-01 02:20:01 +02:00
|
|
|
|
return false;
|
2013-03-26 08:50:18 +01:00
|
|
|
|
}
|
|
|
|
|
|
2013-04-21 23:04:09 +02:00
|
|
|
|
if (propertyInfo.PropertyType.IsSimpleType() || MapRepository.Instance.TypeConverters.ContainsKey(propertyInfo.PropertyType))
|
2013-03-26 23:17:13 +01:00
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-01 02:20:01 +02:00
|
|
|
|
return false;
|
2013-03-26 08:50:18 +01:00
|
|
|
|
}
|
|
|
|
|
|
2013-03-26 23:17:13 +01:00
|
|
|
|
|
2013-03-26 08:50:18 +01:00
|
|
|
|
}
|
|
|
|
|
}
|