2013-03-25 06:13:53 +00:00
|
|
|
|
using System;
|
2013-03-26 05:51:56 +00:00
|
|
|
|
using System.Reflection;
|
2013-03-25 06:13:53 +00:00
|
|
|
|
using Marr.Data;
|
|
|
|
|
using Marr.Data.Mapping;
|
|
|
|
|
using NzbDrone.Core.Configuration;
|
|
|
|
|
using NzbDrone.Core.Datastore.Converters;
|
|
|
|
|
using NzbDrone.Core.ExternalNotification;
|
|
|
|
|
using NzbDrone.Core.Indexers;
|
|
|
|
|
using NzbDrone.Core.Instrumentation;
|
|
|
|
|
using NzbDrone.Core.Jobs;
|
|
|
|
|
using NzbDrone.Core.MediaFiles;
|
|
|
|
|
using NzbDrone.Core.Qualities;
|
|
|
|
|
using NzbDrone.Core.ReferenceData;
|
|
|
|
|
using NzbDrone.Core.RootFolders;
|
|
|
|
|
using NzbDrone.Core.Tv;
|
|
|
|
|
using BooleanIntConverter = NzbDrone.Core.Datastore.Converters.BooleanIntConverter;
|
2013-03-26 05:51:56 +00:00
|
|
|
|
using System.Linq;
|
2013-03-25 06:13:53 +00:00
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Datastore
|
|
|
|
|
{
|
|
|
|
|
public static class TableMapping
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
private static readonly FluentMappings Mapper = new FluentMappings(true);
|
|
|
|
|
|
|
|
|
|
public static void Map()
|
|
|
|
|
{
|
|
|
|
|
RegisterMappers();
|
|
|
|
|
|
|
|
|
|
Mapper.Entity<Config>().RegisterModel("Config");
|
|
|
|
|
Mapper.Entity<RootFolder>().RegisterModel("RootFolders");
|
|
|
|
|
|
|
|
|
|
Mapper.Entity<Indexer>().RegisterModel("IndexerDefinitions");
|
|
|
|
|
Mapper.Entity<NewznabDefinition>().RegisterModel("NewznabDefinitions");
|
|
|
|
|
Mapper.Entity<JobDefinition>().RegisterModel("JobDefinitions");
|
|
|
|
|
Mapper.Entity<ExternalNotificationDefinition>().RegisterModel("ExternalNotificationDefinitions");
|
|
|
|
|
|
|
|
|
|
Mapper.Entity<SceneMapping>().RegisterModel("SceneMappings");
|
|
|
|
|
|
2013-03-26 05:51:56 +00:00
|
|
|
|
Mapper.Entity<History.History>().RegisterModel("History")
|
|
|
|
|
.Relationships.AutoMapComplexTypeProperties<ILazyLoaded>()
|
|
|
|
|
.For(c => c.Episode)
|
|
|
|
|
.LazyLoad((db, history) => db.Query<Episode>().Where(ep => ep.Id == history.Id).ToList());
|
|
|
|
|
|
|
|
|
|
Mapper.Entity<Series>().RegisterModel("Series")
|
|
|
|
|
.Relationships.AutoMapComplexTypeProperties<ILazyLoaded>()
|
|
|
|
|
.For(c => c.Covers)
|
|
|
|
|
.LazyLoad((db, series) => db.Query<MediaCover.MediaCover>().Where(cover => cover.SeriesId == series.Id).ToList());
|
|
|
|
|
|
2013-03-25 06:13:53 +00:00
|
|
|
|
|
|
|
|
|
Mapper.Entity<Season>().RegisterModel("Seasons");
|
|
|
|
|
Mapper.Entity<Episode>().RegisterModel("Episodes");
|
|
|
|
|
Mapper.Entity<EpisodeFile>().RegisterModel("EpisodeFiles");
|
2013-03-26 05:51:56 +00:00
|
|
|
|
Mapper.Entity<MediaCover.MediaCover>().RegisterModel("MediaCovers");
|
2013-03-25 06:13:53 +00:00
|
|
|
|
|
|
|
|
|
Mapper.Entity<QualityProfile>().RegisterModel("QualityProfiles");
|
|
|
|
|
Mapper.Entity<QualitySize>().RegisterModel("QualitySizes");
|
|
|
|
|
|
|
|
|
|
Mapper.Entity<Log>().RegisterModel("Logs");
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void RegisterMappers()
|
|
|
|
|
{
|
|
|
|
|
MapRepository.Instance.RegisterTypeConverter(typeof(Int32), new Int32Converter());
|
|
|
|
|
MapRepository.Instance.RegisterTypeConverter(typeof(Boolean), new BooleanIntConverter());
|
2013-03-26 05:51:56 +00:00
|
|
|
|
MapRepository.Instance.RegisterTypeConverter(typeof(Enum), new EnumIntConverter());
|
|
|
|
|
MapRepository.Instance.RegisterTypeConverter(typeof(QualityModel), new EmbeddedDocumentConverter());
|
2013-03-25 06:13:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2013-03-26 05:51:56 +00:00
|
|
|
|
private static ColumnMapBuilder<T> RegisterModel<T>(this FluentMappings.MappingsFluentEntity<T> mapBuilder, string tableName) where T : ModelBase
|
2013-03-25 06:13:53 +00:00
|
|
|
|
{
|
2013-03-26 05:51:56 +00:00
|
|
|
|
return mapBuilder.Table.MapTable(tableName)
|
|
|
|
|
.Columns
|
|
|
|
|
.AutoMapPropertiesWhere(IsMappableProperty)
|
|
|
|
|
.For(c => c.Id)
|
|
|
|
|
.SetPrimaryKey()
|
|
|
|
|
.SetReturnValue()
|
|
|
|
|
.SetAutoIncrement();
|
2013-03-25 06:13:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-03-26 05:51:56 +00:00
|
|
|
|
private static bool IsMappableProperty(MemberInfo memberInfo)
|
|
|
|
|
{
|
|
|
|
|
var propertyInfo = memberInfo as PropertyInfo;
|
|
|
|
|
|
|
|
|
|
if (propertyInfo == null) return false;
|
|
|
|
|
|
|
|
|
|
if (propertyInfo.PropertyType.GetInterfaces().Any(i => i == typeof(IEmbeddedDocument)))
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return propertyInfo.CanRead && propertyInfo.CanWrite && IsSimpleType(propertyInfo.PropertyType);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private 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-25 06:13:53 +00:00
|
|
|
|
}
|
|
|
|
|
}
|