using System; using System.Collections.Generic; using System.Linq; using Dapper; using NzbDrone.Common.Reflection; using NzbDrone.Core.Authentication; using NzbDrone.Core.AutoTagging.Specifications; using NzbDrone.Core.Blocklisting; using NzbDrone.Core.Configuration; using NzbDrone.Core.CustomFilters; using NzbDrone.Core.CustomFormats; using NzbDrone.Core.DataAugmentation.Scene; using NzbDrone.Core.Datastore.Converters; using NzbDrone.Core.Download; using NzbDrone.Core.Download.History; using NzbDrone.Core.Download.Pending; using NzbDrone.Core.Extras.Metadata; using NzbDrone.Core.Extras.Metadata.Files; using NzbDrone.Core.Extras.Others; using NzbDrone.Core.Extras.Subtitles; using NzbDrone.Core.History; using NzbDrone.Core.ImportLists; using NzbDrone.Core.ImportLists.Exclusions; using NzbDrone.Core.Indexers; using NzbDrone.Core.Instrumentation; using NzbDrone.Core.Jobs; using NzbDrone.Core.Languages; using NzbDrone.Core.MediaFiles; using NzbDrone.Core.Messaging.Commands; using NzbDrone.Core.Notifications; using NzbDrone.Core.Organizer; using NzbDrone.Core.Parser.Model; using NzbDrone.Core.Profiles; using NzbDrone.Core.Profiles.Delay; using NzbDrone.Core.Profiles.Qualities; using NzbDrone.Core.Profiles.Releases; using NzbDrone.Core.Qualities; using NzbDrone.Core.RemotePathMappings; using NzbDrone.Core.RootFolders; using NzbDrone.Core.Tags; using NzbDrone.Core.ThingiProvider; using NzbDrone.Core.Tv; using NzbDrone.Core.Update.History; using static Dapper.SqlMapper; namespace NzbDrone.Core.Datastore { public static class TableMapping { static TableMapping() { Mapper = new TableMapper(); } public static TableMapper Mapper { get; private set; } public static void Map() { RegisterMappers(); Mapper.Entity("Config").RegisterModel(); Mapper.Entity("RootFolders").RegisterModel() .Ignore(r => r.Accessible) .Ignore(r => r.FreeSpace) .Ignore(r => r.TotalSpace); Mapper.Entity("ScheduledTasks").RegisterModel() .Ignore(i => i.Priority); Mapper.Entity("Indexers").RegisterModel() .Ignore(x => x.ImplementationName) .Ignore(i => i.Enable) .Ignore(i => i.Protocol) .Ignore(i => i.SupportsRss) .Ignore(i => i.SupportsSearch); Mapper.Entity("ImportLists").RegisterModel() .Ignore(x => x.ImplementationName) .Ignore(i => i.ListType) .Ignore(i => i.MinRefreshInterval) .Ignore(i => i.Enable); Mapper.Entity("ImportListItems").RegisterModel() .Ignore(i => i.ImportList) .Ignore(i => i.Seasons); Mapper.Entity("Notifications").RegisterModel() .Ignore(x => x.ImplementationName) .Ignore(i => i.SupportsOnGrab) .Ignore(i => i.SupportsOnDownload) .Ignore(i => i.SupportsOnUpgrade) .Ignore(i => i.SupportsOnRename) .Ignore(i => i.SupportsOnSeriesAdd) .Ignore(i => i.SupportsOnSeriesDelete) .Ignore(i => i.SupportsOnEpisodeFileDelete) .Ignore(i => i.SupportsOnEpisodeFileDeleteForUpgrade) .Ignore(i => i.SupportsOnHealthIssue) .Ignore(i => i.SupportsOnHealthRestored) .Ignore(i => i.SupportsOnApplicationUpdate) .Ignore(i => i.SupportsOnManualInteractionRequired); Mapper.Entity("Metadata").RegisterModel() .Ignore(x => x.ImplementationName) .Ignore(d => d.Tags); Mapper.Entity("DownloadClients").RegisterModel() .Ignore(x => x.ImplementationName) .Ignore(d => d.Protocol); Mapper.Entity("SceneMappings").RegisterModel(); Mapper.Entity("History").RegisterModel(); Mapper.Entity("Series").RegisterModel() .Ignore(s => s.RootFolderPath) .HasOne(s => s.QualityProfile, s => s.QualityProfileId); Mapper.Entity("EpisodeFiles").RegisterModel() .HasOne(f => f.Series, f => f.SeriesId) .LazyLoad(x => x.Episodes, (db, parent) => db.Query(new SqlBuilder(db.DatabaseType).Where(c => c.EpisodeFileId == parent.Id)).ToList(), t => t.Id > 0) .Ignore(f => f.Path); Mapper.Entity("Episodes").RegisterModel() .Ignore(e => e.SeriesTitle) .Ignore(e => e.Series) .Ignore(e => e.HasFile) .Ignore(e => e.AbsoluteEpisodeNumberAdded) .HasOne(s => s.EpisodeFile, s => s.EpisodeFileId); Mapper.Entity("QualityDefinitions").RegisterModel() .Ignore(d => d.GroupName) .Ignore(d => d.Weight); Mapper.Entity("CustomFormats").RegisterModel(); Mapper.Entity("QualityProfiles").RegisterModel(); Mapper.Entity("Logs").RegisterModel(); Mapper.Entity("NamingConfig").RegisterModel(); Mapper.Entity("Blocklist").RegisterModel(); Mapper.Entity("MetadataFiles").RegisterModel(); Mapper.Entity("SubtitleFiles").RegisterModel(); Mapper.Entity("ExtraFiles").RegisterModel(); Mapper.Entity("PendingReleases").RegisterModel() .Ignore(e => e.RemoteEpisode); Mapper.Entity("RemotePathMappings").RegisterModel(); Mapper.Entity("Tags").RegisterModel(); Mapper.Entity("ReleaseProfiles").RegisterModel(); Mapper.Entity("DelayProfiles").RegisterModel(); Mapper.Entity("Users").RegisterModel(); Mapper.Entity("Commands").RegisterModel() .Ignore(c => c.Message); Mapper.Entity("IndexerStatus").RegisterModel(); Mapper.Entity("DownloadClientStatus").RegisterModel(); Mapper.Entity("ImportListStatus").RegisterModel(); Mapper.Entity("NotificationStatus").RegisterModel(); Mapper.Entity("CustomFilters").RegisterModel(); Mapper.Entity("DownloadHistory").RegisterModel(); Mapper.Entity("UpdateHistory").RegisterModel(); Mapper.Entity("ImportListExclusions").RegisterModel(); Mapper.Entity("AutoTagging").RegisterModel(); } private static void RegisterMappers() { RegisterEmbeddedConverter(); RegisterProviderSettingConverter(); SqlMapper.RemoveTypeMap(typeof(DateTime)); SqlMapper.AddTypeHandler(new DapperUtcConverter()); SqlMapper.AddTypeHandler(new DapperQualityIntConverter()); SqlMapper.AddTypeHandler(new EmbeddedDocumentConverter>(new QualityIntConverter())); SqlMapper.AddTypeHandler(new EmbeddedDocumentConverter>(new CustomFormatIntConverter())); SqlMapper.AddTypeHandler(new EmbeddedDocumentConverter>(new CustomFormatSpecificationListConverter())); SqlMapper.AddTypeHandler(new EmbeddedDocumentConverter>(new AutoTaggingSpecificationConverter())); SqlMapper.AddTypeHandler(new EmbeddedDocumentConverter(new QualityIntConverter())); SqlMapper.AddTypeHandler(new EmbeddedDocumentConverter>()); SqlMapper.AddTypeHandler(new EmbeddedDocumentConverter>()); SqlMapper.AddTypeHandler(new EmbeddedDocumentConverter>()); SqlMapper.AddTypeHandler(new EmbeddedDocumentConverter>>()); SqlMapper.AddTypeHandler(new EmbeddedDocumentConverter>()); SqlMapper.AddTypeHandler(new DapperLanguageIntConverter()); SqlMapper.AddTypeHandler(new EmbeddedDocumentConverter>(new LanguageIntConverter())); SqlMapper.AddTypeHandler(new StringListConverter>()); SqlMapper.AddTypeHandler(new EmbeddedDocumentConverter(new QualityIntConverter(), new LanguageIntConverter())); SqlMapper.AddTypeHandler(new EmbeddedDocumentConverter()); SqlMapper.AddTypeHandler(new EmbeddedDocumentConverter()); SqlMapper.AddTypeHandler(new EmbeddedDocumentConverter>()); SqlMapper.AddTypeHandler(new OsPathConverter()); SqlMapper.RemoveTypeMap(typeof(Guid)); SqlMapper.RemoveTypeMap(typeof(Guid?)); SqlMapper.AddTypeHandler(new GuidConverter()); SqlMapper.AddTypeHandler(new CommandConverter()); SqlMapper.AddTypeHandler(new SystemVersionConverter()); } private static void RegisterProviderSettingConverter() { var settingTypes = typeof(IProviderConfig).Assembly.ImplementationsOf() .Where(x => !x.ContainsGenericParameters); var providerSettingConverter = new ProviderSettingConverter(); foreach (var embeddedType in settingTypes) { SqlMapper.AddTypeHandler(embeddedType, providerSettingConverter); } } private static void RegisterEmbeddedConverter() { var embeddedTypes = typeof(IEmbeddedDocument).Assembly.ImplementationsOf(); var embeddedConverterDefinition = typeof(EmbeddedDocumentConverter<>).GetGenericTypeDefinition(); var genericListDefinition = typeof(List<>).GetGenericTypeDefinition(); foreach (var embeddedType in embeddedTypes) { var embeddedListType = genericListDefinition.MakeGenericType(embeddedType); RegisterEmbeddedConverter(embeddedType, embeddedConverterDefinition); RegisterEmbeddedConverter(embeddedListType, embeddedConverterDefinition); } } private static void RegisterEmbeddedConverter(Type embeddedType, Type embeddedConverterDefinition) { var embeddedConverterType = embeddedConverterDefinition.MakeGenericType(embeddedType); var converter = (ITypeHandler)Activator.CreateInstance(embeddedConverterType); SqlMapper.AddTypeHandler(embeddedType, converter); } } }