2020-05-29 09:28:19 +00:00
#pragma warning disable CS1591
2016-11-20 09:46:07 +00:00
using System ;
using System.Collections.Generic ;
using System.Globalization ;
using System.IO ;
using System.Linq ;
using System.Text ;
2019-09-25 15:43:20 +00:00
using System.Text.Json ;
2016-11-20 09:46:07 +00:00
using System.Threading ;
2017-08-16 17:30:16 +00:00
using Emby.Server.Implementations.Playlists ;
2020-07-01 01:44:41 +00:00
using Jellyfin.Data.Enums ;
2019-10-20 14:08:40 +00:00
using MediaBrowser.Common.Extensions ;
2019-09-25 15:43:20 +00:00
using MediaBrowser.Common.Json ;
2019-01-13 19:20:16 +00:00
using MediaBrowser.Controller ;
2016-11-20 09:46:07 +00:00
using MediaBrowser.Controller.Channels ;
using MediaBrowser.Controller.Configuration ;
2019-01-13 19:20:16 +00:00
using MediaBrowser.Controller.Drawing ;
2016-11-20 09:46:07 +00:00
using MediaBrowser.Controller.Entities ;
using MediaBrowser.Controller.Entities.Audio ;
using MediaBrowser.Controller.Entities.Movies ;
using MediaBrowser.Controller.Entities.TV ;
using MediaBrowser.Controller.Extensions ;
2019-01-13 19:20:16 +00:00
using MediaBrowser.Controller.Library ;
2016-11-20 09:46:07 +00:00
using MediaBrowser.Controller.LiveTv ;
using MediaBrowser.Controller.Persistence ;
using MediaBrowser.Controller.Playlists ;
using MediaBrowser.Model.Dto ;
using MediaBrowser.Model.Entities ;
2019-03-14 21:31:51 +00:00
using MediaBrowser.Model.Globalization ;
2016-11-20 09:46:07 +00:00
using MediaBrowser.Model.LiveTv ;
using MediaBrowser.Model.Querying ;
2019-01-13 19:20:16 +00:00
using Microsoft.Extensions.Logging ;
using SQLitePCL.pretty ;
2016-11-20 09:46:07 +00:00
namespace Emby.Server.Implementations.Data
{
/// <summary>
2020-05-29 09:28:19 +00:00
/// Class SQLiteItemRepository.
2016-11-20 09:46:07 +00:00
/// </summary>
public class SqliteItemRepository : BaseSqliteRepository , IItemRepository
{
2019-07-01 15:59:01 +00:00
private const string ChaptersTableName = "Chapters2" ;
2016-11-20 09:46:07 +00:00
private readonly IServerConfigurationManager _config ;
2019-07-01 15:59:01 +00:00
private readonly IServerApplicationHost _appHost ;
2019-03-14 21:31:51 +00:00
private readonly ILocalizationManager _localization ;
2020-04-05 03:18:11 +00:00
// TODO: Remove this dependency. GetImageCacheTag() is the only method used and it can be converted to a static helper method
2020-04-04 21:12:24 +00:00
private readonly IImageProcessor _imageProcessor ;
2019-03-14 21:31:51 +00:00
2019-09-25 15:43:20 +00:00
private readonly TypeMapper _typeMapper ;
private readonly JsonSerializerOptions _jsonOptions ;
2019-11-07 16:20:29 +00:00
static SqliteItemRepository ( )
{
2019-11-05 19:53:46 +00:00
var queryPrefixText = new StringBuilder ( ) ;
queryPrefixText . Append ( "insert into mediaattachments (" ) ;
foreach ( var column in _mediaAttachmentSaveColumns )
{
2019-12-26 22:09:00 +00:00
queryPrefixText . Append ( column )
. Append ( ',' ) ;
2019-11-05 19:53:46 +00:00
}
2019-11-07 16:22:07 +00:00
2019-11-05 19:53:46 +00:00
queryPrefixText . Length - = 1 ;
queryPrefixText . Append ( ") values " ) ;
_mediaAttachmentInsertPrefix = queryPrefixText . ToString ( ) ;
}
2016-11-20 09:46:07 +00:00
/// <summary>
/// Initializes a new instance of the <see cref="SqliteItemRepository"/> class.
/// </summary>
2019-01-17 22:55:05 +00:00
public SqliteItemRepository (
IServerConfigurationManager config ,
IServerApplicationHost appHost ,
2019-09-25 15:43:20 +00:00
ILogger < SqliteItemRepository > logger ,
2020-04-04 21:12:24 +00:00
ILocalizationManager localization ,
IImageProcessor imageProcessor )
2019-09-25 15:43:20 +00:00
: base ( logger )
2016-11-20 09:46:07 +00:00
{
if ( config = = null )
{
2019-01-06 20:50:43 +00:00
throw new ArgumentNullException ( nameof ( config ) ) ;
2016-11-20 09:46:07 +00:00
}
2019-01-26 17:21:41 +00:00
2016-11-20 09:46:07 +00:00
_config = config ;
2019-09-25 15:43:20 +00:00
_appHost = appHost ;
2019-03-14 21:31:51 +00:00
_localization = localization ;
2020-04-04 21:12:24 +00:00
_imageProcessor = imageProcessor ;
2016-11-20 09:46:07 +00:00
2019-09-25 15:43:20 +00:00
_typeMapper = new TypeMapper ( ) ;
_jsonOptions = JsonDefaults . GetOptions ( ) ;
2016-11-20 09:46:07 +00:00
DbFilePath = Path . Combine ( _config . ApplicationPaths . DataPath , "library.db" ) ;
}
2019-07-01 15:59:01 +00:00
/// <inheritdoc />
public string Name = > "SQLite" ;
2016-11-20 09:46:07 +00:00
2019-07-01 15:59:01 +00:00
/// <inheritdoc />
protected override int? CacheSize = > 20000 ;
/// <inheritdoc />
2019-04-03 15:34:54 +00:00
protected override TempStoreMode TempStore = > TempStoreMode . Memory ;
2016-11-27 19:36:56 +00:00
2016-11-20 09:46:07 +00:00
/// <summary>
2020-06-15 22:37:52 +00:00
/// Opens the connection to the database.
2016-11-20 09:46:07 +00:00
/// </summary>
2018-09-12 17:26:21 +00:00
public void Initialize ( SqliteUserDataRepository userDataRepo , IUserManager userManager )
2016-11-20 09:46:07 +00:00
{
2019-08-18 20:05:06 +00:00
const string CreateMediaStreamsTableCommand
2018-09-12 17:26:21 +00:00
= "create table if not exists mediastreams (ItemId GUID, StreamIndex INT, StreamType TEXT, Codec TEXT, Language TEXT, ChannelLayout TEXT, Profile TEXT, AspectRatio TEXT, Path TEXT, IsInterlaced BIT, BitRate INT NULL, Channels INT NULL, SampleRate INT NULL, IsDefault BIT, IsForced BIT, IsExternal BIT, Height INT NULL, Width INT NULL, AverageFrameRate FLOAT NULL, RealFrameRate FLOAT NULL, Level FLOAT NULL, PixelFormat TEXT, BitDepth INT NULL, IsAnamorphic BIT NULL, RefFrames INT NULL, CodecTag TEXT NULL, Comment TEXT NULL, NalLengthSize TEXT NULL, IsAvc BIT NULL, Title TEXT NULL, TimeBase TEXT NULL, CodecTimeBase TEXT NULL, ColorPrimaries TEXT NULL, ColorSpace TEXT NULL, ColorTransfer TEXT NULL, PRIMARY KEY (ItemId, StreamIndex))" ;
2019-10-04 17:23:08 +00:00
const string CreateMediaAttachmentsTableCommand
= "create table if not exists mediaattachments (ItemId GUID, AttachmentIndex INT, Codec TEXT, CodecTag TEXT NULL, Comment TEXT NULL, Filename TEXT NULL, MIMEType TEXT NULL, PRIMARY KEY (ItemId, AttachmentIndex))" ;
2016-11-20 09:46:07 +00:00
2019-08-18 20:05:06 +00:00
string [ ] queries =
{
"PRAGMA locking_mode=EXCLUSIVE" ,
2016-11-20 09:46:07 +00:00
2019-08-18 20:05:06 +00:00
"create table if not exists TypedBaseItems (guid GUID primary key NOT NULL, type TEXT NOT NULL, data BLOB NULL, ParentId GUID NULL, Path TEXT NULL)" ,
2016-11-20 09:46:07 +00:00
2019-08-18 20:05:06 +00:00
"create table if not exists AncestorIds (ItemId GUID NOT NULL, AncestorId GUID NOT NULL, AncestorIdText TEXT NOT NULL, PRIMARY KEY (ItemId, AncestorId))" ,
"create index if not exists idx_AncestorIds1 on AncestorIds(AncestorId)" ,
"create index if not exists idx_AncestorIds5 on AncestorIds(AncestorIdText,ItemId)" ,
2016-11-20 09:46:07 +00:00
2019-08-18 20:05:06 +00:00
"create table if not exists ItemValues (ItemId GUID NOT NULL, Type INT NOT NULL, Value TEXT NOT NULL, CleanValue TEXT NOT NULL)" ,
2016-11-20 09:46:07 +00:00
2019-08-18 20:05:06 +00:00
"create table if not exists People (ItemId GUID, Name TEXT NOT NULL, Role TEXT, PersonType TEXT, SortOrder int, ListOrder int)" ,
2016-11-20 09:46:07 +00:00
2019-08-18 20:05:06 +00:00
"drop index if exists idxPeopleItemId" ,
"create index if not exists idxPeopleItemId1 on People(ItemId,ListOrder)" ,
"create index if not exists idxPeopleName on People(Name)" ,
2016-11-20 09:46:07 +00:00
2019-08-18 20:05:06 +00:00
"create table if not exists " + ChaptersTableName + " (ItemId GUID, ChapterIndex INT NOT NULL, StartPositionTicks BIGINT NOT NULL, Name TEXT, ImagePath TEXT, PRIMARY KEY (ItemId, ChapterIndex))" ,
2016-11-20 09:46:07 +00:00
2019-08-18 20:05:06 +00:00
CreateMediaStreamsTableCommand ,
2019-10-04 17:23:08 +00:00
CreateMediaAttachmentsTableCommand ,
2016-11-20 09:46:07 +00:00
2019-08-18 20:05:06 +00:00
"pragma shrink_memory"
} ;
string [ ] postQueries =
{
// obsolete
"drop index if exists idx_TypedBaseItems" ,
"drop index if exists idx_mediastreams" ,
"drop index if exists idx_mediastreams1" ,
"drop index if exists idx_" + ChaptersTableName ,
"drop index if exists idx_UserDataKeys1" ,
"drop index if exists idx_UserDataKeys2" ,
"drop index if exists idx_TypeTopParentId3" ,
"drop index if exists idx_TypeTopParentId2" ,
"drop index if exists idx_TypeTopParentId4" ,
"drop index if exists idx_Type" ,
"drop index if exists idx_TypeTopParentId" ,
"drop index if exists idx_GuidType" ,
"drop index if exists idx_TopParentId" ,
"drop index if exists idx_TypeTopParentId6" ,
"drop index if exists idx_ItemValues2" ,
"drop index if exists Idx_ProviderIds" ,
"drop index if exists idx_ItemValues3" ,
"drop index if exists idx_ItemValues4" ,
"drop index if exists idx_ItemValues5" ,
"drop index if exists idx_UserDataKeys3" ,
"drop table if exists UserDataKeys" ,
"drop table if exists ProviderIds" ,
"drop index if exists Idx_ProviderIds1" ,
"drop table if exists Images" ,
"drop index if exists idx_Images" ,
"drop index if exists idx_TypeSeriesPresentationUniqueKey" ,
"drop index if exists idx_SeriesPresentationUniqueKey" ,
"drop index if exists idx_TypeSeriesPresentationUniqueKey2" ,
"drop index if exists idx_AncestorIds3" ,
"drop index if exists idx_AncestorIds4" ,
"drop index if exists idx_AncestorIds2" ,
"create index if not exists idx_PathTypedBaseItems on TypedBaseItems(Path)" ,
"create index if not exists idx_ParentIdTypedBaseItems on TypedBaseItems(ParentId)" ,
"create index if not exists idx_PresentationUniqueKey on TypedBaseItems(PresentationUniqueKey)" ,
"create index if not exists idx_GuidTypeIsFolderIsVirtualItem on TypedBaseItems(Guid,Type,IsFolder,IsVirtualItem)" ,
"create index if not exists idx_CleanNameType on TypedBaseItems(CleanName,Type)" ,
// covering index
"create index if not exists idx_TopParentIdGuid on TypedBaseItems(TopParentId,Guid)" ,
// series
"create index if not exists idx_TypeSeriesPresentationUniqueKey1 on TypedBaseItems(Type,SeriesPresentationUniqueKey,PresentationUniqueKey,SortName)" ,
// series counts
// seriesdateplayed sort order
"create index if not exists idx_TypeSeriesPresentationUniqueKey3 on TypedBaseItems(SeriesPresentationUniqueKey,Type,IsFolder,IsVirtualItem)" ,
// live tv programs
"create index if not exists idx_TypeTopParentIdStartDate on TypedBaseItems(Type,TopParentId,StartDate)" ,
// covering index for getitemvalues
"create index if not exists idx_TypeTopParentIdGuid on TypedBaseItems(Type,TopParentId,Guid)" ,
// used by movie suggestions
"create index if not exists idx_TypeTopParentIdGroup on TypedBaseItems(Type,TopParentId,PresentationUniqueKey)" ,
"create index if not exists idx_TypeTopParentId5 on TypedBaseItems(TopParentId,IsVirtualItem)" ,
// latest items
"create index if not exists idx_TypeTopParentId9 on TypedBaseItems(TopParentId,Type,IsVirtualItem,PresentationUniqueKey,DateCreated)" ,
"create index if not exists idx_TypeTopParentId8 on TypedBaseItems(TopParentId,IsFolder,IsVirtualItem,PresentationUniqueKey,DateCreated)" ,
// resume
"create index if not exists idx_TypeTopParentId7 on TypedBaseItems(TopParentId,MediaType,IsVirtualItem,PresentationUniqueKey)" ,
// items by name
"create index if not exists idx_ItemValues6 on ItemValues(ItemId,Type,CleanValue)" ,
"create index if not exists idx_ItemValues7 on ItemValues(Type,CleanValue,ItemId)" ,
// Used to update inherited tags
"create index if not exists idx_ItemValues8 on ItemValues(Type, ItemId, Value)" ,
} ;
2016-11-20 09:46:07 +00:00
2019-08-18 20:05:06 +00:00
using ( var connection = GetConnection ( ) )
{
2016-11-22 05:55:34 +00:00
connection . RunQueries ( queries ) ;
connection . RunInTransaction ( db = >
{
var existingColumnNames = GetColumnNames ( db , "AncestorIds" ) ;
AddColumn ( db , "AncestorIds" , "AncestorIdText" , "Text" , existingColumnNames ) ;
existingColumnNames = GetColumnNames ( db , "TypedBaseItems" ) ;
AddColumn ( db , "TypedBaseItems" , "Path" , "Text" , existingColumnNames ) ;
AddColumn ( db , "TypedBaseItems" , "StartDate" , "DATETIME" , existingColumnNames ) ;
AddColumn ( db , "TypedBaseItems" , "EndDate" , "DATETIME" , existingColumnNames ) ;
AddColumn ( db , "TypedBaseItems" , "ChannelId" , "Text" , existingColumnNames ) ;
AddColumn ( db , "TypedBaseItems" , "IsMovie" , "BIT" , existingColumnNames ) ;
AddColumn ( db , "TypedBaseItems" , "CommunityRating" , "Float" , existingColumnNames ) ;
AddColumn ( db , "TypedBaseItems" , "CustomRating" , "Text" , existingColumnNames ) ;
AddColumn ( db , "TypedBaseItems" , "IndexNumber" , "INT" , existingColumnNames ) ;
AddColumn ( db , "TypedBaseItems" , "IsLocked" , "BIT" , existingColumnNames ) ;
AddColumn ( db , "TypedBaseItems" , "Name" , "Text" , existingColumnNames ) ;
AddColumn ( db , "TypedBaseItems" , "OfficialRating" , "Text" , existingColumnNames ) ;
AddColumn ( db , "TypedBaseItems" , "MediaType" , "Text" , existingColumnNames ) ;
AddColumn ( db , "TypedBaseItems" , "Overview" , "Text" , existingColumnNames ) ;
AddColumn ( db , "TypedBaseItems" , "ParentIndexNumber" , "INT" , existingColumnNames ) ;
AddColumn ( db , "TypedBaseItems" , "PremiereDate" , "DATETIME" , existingColumnNames ) ;
AddColumn ( db , "TypedBaseItems" , "ProductionYear" , "INT" , existingColumnNames ) ;
AddColumn ( db , "TypedBaseItems" , "ParentId" , "GUID" , existingColumnNames ) ;
AddColumn ( db , "TypedBaseItems" , "Genres" , "Text" , existingColumnNames ) ;
AddColumn ( db , "TypedBaseItems" , "SortName" , "Text" , existingColumnNames ) ;
2017-06-03 07:36:32 +00:00
AddColumn ( db , "TypedBaseItems" , "ForcedSortName" , "Text" , existingColumnNames ) ;
2016-11-22 05:55:34 +00:00
AddColumn ( db , "TypedBaseItems" , "RunTimeTicks" , "BIGINT" , existingColumnNames ) ;
AddColumn ( db , "TypedBaseItems" , "DateCreated" , "DATETIME" , existingColumnNames ) ;
AddColumn ( db , "TypedBaseItems" , "DateModified" , "DATETIME" , existingColumnNames ) ;
AddColumn ( db , "TypedBaseItems" , "IsSeries" , "BIT" , existingColumnNames ) ;
AddColumn ( db , "TypedBaseItems" , "EpisodeTitle" , "Text" , existingColumnNames ) ;
AddColumn ( db , "TypedBaseItems" , "IsRepeat" , "BIT" , existingColumnNames ) ;
AddColumn ( db , "TypedBaseItems" , "PreferredMetadataLanguage" , "Text" , existingColumnNames ) ;
AddColumn ( db , "TypedBaseItems" , "PreferredMetadataCountryCode" , "Text" , existingColumnNames ) ;
AddColumn ( db , "TypedBaseItems" , "DateLastRefreshed" , "DATETIME" , existingColumnNames ) ;
AddColumn ( db , "TypedBaseItems" , "DateLastSaved" , "DATETIME" , existingColumnNames ) ;
AddColumn ( db , "TypedBaseItems" , "IsInMixedFolder" , "BIT" , existingColumnNames ) ;
AddColumn ( db , "TypedBaseItems" , "LockedFields" , "Text" , existingColumnNames ) ;
AddColumn ( db , "TypedBaseItems" , "Studios" , "Text" , existingColumnNames ) ;
AddColumn ( db , "TypedBaseItems" , "Audio" , "Text" , existingColumnNames ) ;
AddColumn ( db , "TypedBaseItems" , "ExternalServiceId" , "Text" , existingColumnNames ) ;
AddColumn ( db , "TypedBaseItems" , "Tags" , "Text" , existingColumnNames ) ;
AddColumn ( db , "TypedBaseItems" , "IsFolder" , "BIT" , existingColumnNames ) ;
AddColumn ( db , "TypedBaseItems" , "InheritedParentalRatingValue" , "INT" , existingColumnNames ) ;
AddColumn ( db , "TypedBaseItems" , "UnratedType" , "Text" , existingColumnNames ) ;
AddColumn ( db , "TypedBaseItems" , "TopParentId" , "Text" , existingColumnNames ) ;
AddColumn ( db , "TypedBaseItems" , "TrailerTypes" , "Text" , existingColumnNames ) ;
AddColumn ( db , "TypedBaseItems" , "CriticRating" , "Float" , existingColumnNames ) ;
AddColumn ( db , "TypedBaseItems" , "CleanName" , "Text" , existingColumnNames ) ;
AddColumn ( db , "TypedBaseItems" , "PresentationUniqueKey" , "Text" , existingColumnNames ) ;
AddColumn ( db , "TypedBaseItems" , "OriginalTitle" , "Text" , existingColumnNames ) ;
AddColumn ( db , "TypedBaseItems" , "PrimaryVersionId" , "Text" , existingColumnNames ) ;
AddColumn ( db , "TypedBaseItems" , "DateLastMediaAdded" , "DATETIME" , existingColumnNames ) ;
AddColumn ( db , "TypedBaseItems" , "Album" , "Text" , existingColumnNames ) ;
AddColumn ( db , "TypedBaseItems" , "IsVirtualItem" , "BIT" , existingColumnNames ) ;
AddColumn ( db , "TypedBaseItems" , "SeriesName" , "Text" , existingColumnNames ) ;
AddColumn ( db , "TypedBaseItems" , "UserDataKey" , "Text" , existingColumnNames ) ;
AddColumn ( db , "TypedBaseItems" , "SeasonName" , "Text" , existingColumnNames ) ;
AddColumn ( db , "TypedBaseItems" , "SeasonId" , "GUID" , existingColumnNames ) ;
AddColumn ( db , "TypedBaseItems" , "SeriesId" , "GUID" , existingColumnNames ) ;
AddColumn ( db , "TypedBaseItems" , "ExternalSeriesId" , "Text" , existingColumnNames ) ;
AddColumn ( db , "TypedBaseItems" , "Tagline" , "Text" , existingColumnNames ) ;
AddColumn ( db , "TypedBaseItems" , "ProviderIds" , "Text" , existingColumnNames ) ;
AddColumn ( db , "TypedBaseItems" , "Images" , "Text" , existingColumnNames ) ;
AddColumn ( db , "TypedBaseItems" , "ProductionLocations" , "Text" , existingColumnNames ) ;
2018-09-12 17:26:21 +00:00
AddColumn ( db , "TypedBaseItems" , "ExtraIds" , "Text" , existingColumnNames ) ;
2016-11-22 05:55:34 +00:00
AddColumn ( db , "TypedBaseItems" , "TotalBitrate" , "INT" , existingColumnNames ) ;
AddColumn ( db , "TypedBaseItems" , "ExtraType" , "Text" , existingColumnNames ) ;
AddColumn ( db , "TypedBaseItems" , "Artists" , "Text" , existingColumnNames ) ;
AddColumn ( db , "TypedBaseItems" , "AlbumArtists" , "Text" , existingColumnNames ) ;
AddColumn ( db , "TypedBaseItems" , "ExternalId" , "Text" , existingColumnNames ) ;
2016-12-06 08:24:29 +00:00
AddColumn ( db , "TypedBaseItems" , "SeriesPresentationUniqueKey" , "Text" , existingColumnNames ) ;
2017-09-08 16:13:58 +00:00
AddColumn ( db , "TypedBaseItems" , "ShowId" , "Text" , existingColumnNames ) ;
2017-09-18 16:52:22 +00:00
AddColumn ( db , "TypedBaseItems" , "OwnerId" , "Text" , existingColumnNames ) ;
2018-09-12 17:26:21 +00:00
AddColumn ( db , "TypedBaseItems" , "Width" , "INT" , existingColumnNames ) ;
AddColumn ( db , "TypedBaseItems" , "Height" , "INT" , existingColumnNames ) ;
2019-01-26 08:07:13 +00:00
AddColumn ( db , "TypedBaseItems" , "Size" , "BIGINT" , existingColumnNames ) ;
2016-11-22 05:55:34 +00:00
existingColumnNames = GetColumnNames ( db , "ItemValues" ) ;
AddColumn ( db , "ItemValues" , "CleanValue" , "Text" , existingColumnNames ) ;
existingColumnNames = GetColumnNames ( db , ChaptersTableName ) ;
AddColumn ( db , ChaptersTableName , "ImageDateModified" , "DATETIME" , existingColumnNames ) ;
existingColumnNames = GetColumnNames ( db , "MediaStreams" ) ;
AddColumn ( db , "MediaStreams" , "IsAvc" , "BIT" , existingColumnNames ) ;
AddColumn ( db , "MediaStreams" , "TimeBase" , "TEXT" , existingColumnNames ) ;
AddColumn ( db , "MediaStreams" , "CodecTimeBase" , "TEXT" , existingColumnNames ) ;
AddColumn ( db , "MediaStreams" , "Title" , "TEXT" , existingColumnNames ) ;
AddColumn ( db , "MediaStreams" , "NalLengthSize" , "TEXT" , existingColumnNames ) ;
AddColumn ( db , "MediaStreams" , "Comment" , "TEXT" , existingColumnNames ) ;
AddColumn ( db , "MediaStreams" , "CodecTag" , "TEXT" , existingColumnNames ) ;
AddColumn ( db , "MediaStreams" , "PixelFormat" , "TEXT" , existingColumnNames ) ;
AddColumn ( db , "MediaStreams" , "BitDepth" , "INT" , existingColumnNames ) ;
AddColumn ( db , "MediaStreams" , "RefFrames" , "INT" , existingColumnNames ) ;
AddColumn ( db , "MediaStreams" , "KeyFrames" , "TEXT" , existingColumnNames ) ;
AddColumn ( db , "MediaStreams" , "IsAnamorphic" , "BIT" , existingColumnNames ) ;
2018-09-12 17:26:21 +00:00
AddColumn ( db , "MediaStreams" , "ColorPrimaries" , "TEXT" , existingColumnNames ) ;
AddColumn ( db , "MediaStreams" , "ColorSpace" , "TEXT" , existingColumnNames ) ;
AddColumn ( db , "MediaStreams" , "ColorTransfer" , "TEXT" , existingColumnNames ) ;
2016-11-28 19:26:48 +00:00
} , TransactionMode ) ;
2016-11-22 05:55:34 +00:00
connection . RunQueries ( postQueries ) ;
}
2016-11-20 09:46:07 +00:00
2019-04-02 20:15:18 +00:00
userDataRepo . Initialize ( userManager , WriteLock , WriteConnection ) ;
2016-11-20 09:46:07 +00:00
}
2019-01-26 17:21:41 +00:00
private static readonly string [ ] _retriveItemColumns =
2016-11-20 09:46:07 +00:00
{
"type" ,
"data" ,
"StartDate" ,
"EndDate" ,
"ChannelId" ,
"IsMovie" ,
"IsSeries" ,
"EpisodeTitle" ,
"IsRepeat" ,
"CommunityRating" ,
"CustomRating" ,
"IndexNumber" ,
"IsLocked" ,
"PreferredMetadataLanguage" ,
"PreferredMetadataCountryCode" ,
2018-09-12 17:26:21 +00:00
"Width" ,
"Height" ,
2016-11-20 09:46:07 +00:00
"DateLastRefreshed" ,
"Name" ,
"Path" ,
"PremiereDate" ,
"Overview" ,
"ParentIndexNumber" ,
"ProductionYear" ,
"OfficialRating" ,
2017-06-03 07:36:32 +00:00
"ForcedSortName" ,
2016-11-20 09:46:07 +00:00
"RunTimeTicks" ,
2019-01-26 08:07:13 +00:00
"Size" ,
2016-11-20 09:46:07 +00:00
"DateCreated" ,
"DateModified" ,
"guid" ,
"Genres" ,
"ParentId" ,
"Audio" ,
"ExternalServiceId" ,
"IsInMixedFolder" ,
"DateLastSaved" ,
"LockedFields" ,
"Studios" ,
"Tags" ,
"TrailerTypes" ,
"OriginalTitle" ,
"PrimaryVersionId" ,
"DateLastMediaAdded" ,
"Album" ,
"CriticRating" ,
"IsVirtualItem" ,
"SeriesName" ,
"SeasonName" ,
"SeasonId" ,
"SeriesId" ,
"PresentationUniqueKey" ,
"InheritedParentalRatingValue" ,
"ExternalSeriesId" ,
"Tagline" ,
"ProviderIds" ,
"Images" ,
"ProductionLocations" ,
2018-09-12 17:26:21 +00:00
"ExtraIds" ,
2016-11-20 09:46:07 +00:00
"TotalBitrate" ,
"ExtraType" ,
"Artists" ,
"AlbumArtists" ,
2016-12-06 08:24:29 +00:00
"ExternalId" ,
2017-09-08 16:13:58 +00:00
"SeriesPresentationUniqueKey" ,
2017-09-18 16:52:22 +00:00
"ShowId" ,
"OwnerId"
2016-11-20 09:46:07 +00:00
} ;
2019-01-26 17:21:41 +00:00
private static readonly string [ ] _mediaStreamSaveColumns =
2016-11-20 09:46:07 +00:00
{
"ItemId" ,
"StreamIndex" ,
"StreamType" ,
"Codec" ,
"Language" ,
"ChannelLayout" ,
"Profile" ,
"AspectRatio" ,
"Path" ,
"IsInterlaced" ,
"BitRate" ,
"Channels" ,
"SampleRate" ,
"IsDefault" ,
"IsForced" ,
"IsExternal" ,
"Height" ,
"Width" ,
"AverageFrameRate" ,
"RealFrameRate" ,
"Level" ,
"PixelFormat" ,
"BitDepth" ,
"IsAnamorphic" ,
"RefFrames" ,
"CodecTag" ,
"Comment" ,
"NalLengthSize" ,
"IsAvc" ,
"Title" ,
"TimeBase" ,
2018-09-12 17:26:21 +00:00
"CodecTimeBase" ,
"ColorPrimaries" ,
"ColorSpace" ,
"ColorTransfer"
2016-11-20 09:46:07 +00:00
} ;
2019-10-04 17:23:08 +00:00
private static readonly string [ ] _mediaAttachmentSaveColumns =
{
"ItemId" ,
"AttachmentIndex" ,
"Codec" ,
"CodecTag" ,
"Comment" ,
"Filename" ,
"MIMEType"
} ;
2019-12-26 22:09:00 +00:00
2019-11-05 19:53:46 +00:00
private static readonly string _mediaAttachmentInsertPrefix ;
2019-10-04 17:23:08 +00:00
2019-01-06 20:50:43 +00:00
private static string GetSaveItemCommandText ( )
2016-11-20 09:46:07 +00:00
{
2020-03-24 15:12:06 +00:00
var saveColumns = new [ ]
2016-11-20 09:46:07 +00:00
{
"guid" ,
"type" ,
"data" ,
"Path" ,
"StartDate" ,
"EndDate" ,
"ChannelId" ,
"IsMovie" ,
"IsSeries" ,
"EpisodeTitle" ,
"IsRepeat" ,
"CommunityRating" ,
"CustomRating" ,
"IndexNumber" ,
"IsLocked" ,
"Name" ,
"OfficialRating" ,
"MediaType" ,
"Overview" ,
"ParentIndexNumber" ,
"PremiereDate" ,
"ProductionYear" ,
"ParentId" ,
"Genres" ,
"InheritedParentalRatingValue" ,
"SortName" ,
2017-06-03 07:36:32 +00:00
"ForcedSortName" ,
2016-11-20 09:46:07 +00:00
"RunTimeTicks" ,
2019-01-26 08:07:13 +00:00
"Size" ,
2016-11-20 09:46:07 +00:00
"DateCreated" ,
"DateModified" ,
"PreferredMetadataLanguage" ,
"PreferredMetadataCountryCode" ,
2018-09-12 17:26:21 +00:00
"Width" ,
"Height" ,
2016-11-20 09:46:07 +00:00
"DateLastRefreshed" ,
"DateLastSaved" ,
"IsInMixedFolder" ,
"LockedFields" ,
"Studios" ,
"Audio" ,
"ExternalServiceId" ,
"Tags" ,
"IsFolder" ,
"UnratedType" ,
"TopParentId" ,
"TrailerTypes" ,
"CriticRating" ,
"CleanName" ,
"PresentationUniqueKey" ,
"OriginalTitle" ,
"PrimaryVersionId" ,
"DateLastMediaAdded" ,
"Album" ,
"IsVirtualItem" ,
"SeriesName" ,
"UserDataKey" ,
"SeasonName" ,
"SeasonId" ,
"SeriesId" ,
"ExternalSeriesId" ,
"Tagline" ,
"ProviderIds" ,
"Images" ,
"ProductionLocations" ,
2018-09-12 17:26:21 +00:00
"ExtraIds" ,
2016-11-20 09:46:07 +00:00
"TotalBitrate" ,
"ExtraType" ,
"Artists" ,
"AlbumArtists" ,
2016-12-06 08:24:29 +00:00
"ExternalId" ,
2017-09-08 16:13:58 +00:00
"SeriesPresentationUniqueKey" ,
2017-09-18 16:52:22 +00:00
"ShowId" ,
"OwnerId"
2016-11-20 09:46:07 +00:00
} ;
2019-01-26 17:21:41 +00:00
var saveItemCommandCommandText = "replace into TypedBaseItems (" + string . Join ( "," , saveColumns ) + ") values (" ;
2016-11-20 09:46:07 +00:00
2019-01-26 17:21:41 +00:00
for ( var i = 0 ; i < saveColumns . Length ; i + + )
2016-11-20 09:46:07 +00:00
{
2019-01-26 17:21:41 +00:00
if ( i ! = 0 )
2016-11-20 09:46:07 +00:00
{
saveItemCommandCommandText + = "," ;
}
2019-04-03 11:46:07 +00:00
2016-11-20 09:46:07 +00:00
saveItemCommandCommandText + = "@" + saveColumns [ i ] ;
}
2019-04-03 11:46:07 +00:00
return saveItemCommandCommandText + ")" ;
2016-11-20 09:46:07 +00:00
}
/// <summary>
2020-06-15 22:37:52 +00:00
/// Save a standard item in the repo.
2016-11-20 09:46:07 +00:00
/// </summary>
/// <param name="item">The item.</param>
/// <param name="cancellationToken">The cancellation token.</param>
2019-01-13 20:37:13 +00:00
/// <exception cref="ArgumentNullException">item</exception>
2017-08-27 00:32:33 +00:00
public void SaveItem ( BaseItem item , CancellationToken cancellationToken )
2016-11-20 09:46:07 +00:00
{
if ( item = = null )
{
2019-01-06 20:50:43 +00:00
throw new ArgumentNullException ( nameof ( item ) ) ;
2016-11-20 09:46:07 +00:00
}
2020-03-24 15:12:06 +00:00
SaveItems ( new [ ] { item } , cancellationToken ) ;
2016-11-20 09:46:07 +00:00
}
2017-10-22 06:22:43 +00:00
public void SaveImages ( BaseItem item )
{
if ( item = = null )
{
2019-01-06 20:50:43 +00:00
throw new ArgumentNullException ( nameof ( item ) ) ;
2017-10-22 06:22:43 +00:00
}
CheckDisposed ( ) ;
2019-02-26 17:30:13 +00:00
using ( var connection = GetConnection ( ) )
2017-10-22 06:22:43 +00:00
{
2019-04-03 11:46:07 +00:00
connection . RunInTransaction ( db = >
2017-10-22 06:22:43 +00:00
{
2019-04-03 11:46:07 +00:00
using ( var saveImagesStatement = base . PrepareStatement ( db , "Update TypedBaseItems set Images=@Images where guid=@Id" ) )
2017-10-22 06:22:43 +00:00
{
2019-09-25 11:24:39 +00:00
saveImagesStatement . TryBind ( "@Id" , item . Id . ToByteArray ( ) ) ;
2019-02-20 13:26:49 +00:00
saveImagesStatement . TryBind ( "@Images" , SerializeImages ( item ) ) ;
2017-10-22 06:22:43 +00:00
2019-02-20 13:26:49 +00:00
saveImagesStatement . MoveNext ( ) ;
}
2019-04-03 11:46:07 +00:00
} , TransactionMode ) ;
2017-10-22 06:22:43 +00:00
}
}
2016-11-20 09:46:07 +00:00
/// <summary>
/// Saves the items.
/// </summary>
/// <param name="items">The items.</param>
/// <param name="cancellationToken">The cancellation token.</param>
2019-01-13 20:37:13 +00:00
/// <exception cref="ArgumentNullException">
2016-11-20 09:46:07 +00:00
/// items
/// or
/// cancellationToken
/// </exception>
2019-02-08 23:57:58 +00:00
public void SaveItems ( IEnumerable < BaseItem > items , CancellationToken cancellationToken )
2016-11-20 09:46:07 +00:00
{
if ( items = = null )
{
2019-01-06 20:50:43 +00:00
throw new ArgumentNullException ( nameof ( items ) ) ;
2016-11-20 09:46:07 +00:00
}
cancellationToken . ThrowIfCancellationRequested ( ) ;
CheckDisposed ( ) ;
2019-02-09 00:09:22 +00:00
var tuples = new List < ( BaseItem , List < Guid > , BaseItem , string , List < string > ) > ( ) ;
2016-11-23 06:54:09 +00:00
foreach ( var item in items )
{
var ancestorIds = item . SupportsAncestors ?
item . GetAncestorIds ( ) . Distinct ( ) . ToList ( ) :
null ;
2016-12-11 05:12:00 +00:00
var topParent = item . GetTopParent ( ) ;
var userdataKey = item . GetUserDataKeys ( ) . FirstOrDefault ( ) ;
2017-08-06 23:01:00 +00:00
var inheritedTags = item . GetInheritedTags ( ) ;
2016-12-11 05:12:00 +00:00
2019-02-09 00:09:22 +00:00
tuples . Add ( ( item , ancestorIds , topParent , userdataKey , inheritedTags ) ) ;
2016-11-23 06:54:09 +00:00
}
2019-02-26 17:30:13 +00:00
using ( var connection = GetConnection ( ) )
2016-11-20 09:46:07 +00:00
{
2019-02-20 13:26:49 +00:00
connection . RunInTransaction ( db = >
2016-11-20 09:46:07 +00:00
{
2019-02-20 13:26:49 +00:00
SaveItemsInTranscation ( db , tuples ) ;
} , TransactionMode ) ;
2016-11-20 09:46:07 +00:00
}
}
2019-02-09 00:09:22 +00:00
private void SaveItemsInTranscation ( IDatabaseConnection db , IEnumerable < ( BaseItem , List < Guid > , BaseItem , string , List < string > ) > tuples )
2016-11-20 09:46:07 +00:00
{
2019-04-03 11:46:07 +00:00
var statements = PrepareAll ( db , new string [ ]
2016-11-20 09:46:07 +00:00
{
2016-11-25 17:36:00 +00:00
GetSaveItemCommandText ( ) ,
2018-09-12 17:26:21 +00:00
"delete from AncestorIds where ItemId=@ItemId"
2019-02-26 18:01:18 +00:00
} ) . ToList ( ) ;
2016-11-25 17:36:00 +00:00
using ( var saveItemStatement = statements [ 0 ] )
2019-01-26 18:46:30 +00:00
using ( var deleteAncestorsStatement = statements [ 1 ] )
2016-11-25 17:36:00 +00:00
{
2019-01-26 18:46:30 +00:00
var requiresReset = false ;
foreach ( var tuple in tuples )
2016-11-20 09:46:07 +00:00
{
2019-01-26 18:46:30 +00:00
if ( requiresReset )
2016-11-20 09:46:07 +00:00
{
2019-01-26 18:46:30 +00:00
saveItemStatement . Reset ( ) ;
}
2016-11-20 09:46:07 +00:00
2019-01-26 18:46:30 +00:00
var item = tuple . Item1 ;
var topParent = tuple . Item3 ;
var userDataKey = tuple . Item4 ;
2016-11-23 06:54:09 +00:00
2019-01-26 18:46:30 +00:00
SaveItem ( item , topParent , userDataKey , saveItemStatement ) ;
2016-11-20 09:46:07 +00:00
2019-01-26 18:46:30 +00:00
var inheritedTags = tuple . Item5 ;
2017-08-06 23:01:00 +00:00
2019-01-26 18:46:30 +00:00
if ( item . SupportsAncestors )
{
UpdateAncestors ( item . Id , tuple . Item2 , db , deleteAncestorsStatement ) ;
}
2016-11-20 09:46:07 +00:00
2019-01-26 18:46:30 +00:00
UpdateItemValues ( item . Id , GetItemValuesToSave ( item , inheritedTags ) , db ) ;
2016-11-20 09:46:07 +00:00
2019-01-26 18:46:30 +00:00
requiresReset = true ;
2016-11-20 09:46:07 +00:00
}
}
}
2018-09-12 17:26:21 +00:00
private string GetPathToSave ( string path )
{
if ( path = = null )
{
return null ;
}
return _appHost . ReverseVirtualPath ( path ) ;
}
private string RestorePath ( string path )
{
return _appHost . ExpandVirtualPath ( path ) ;
}
2016-12-11 05:12:00 +00:00
private void SaveItem ( BaseItem item , BaseItem topParent , string userDataKey , IStatement saveItemStatement )
2016-11-20 09:46:07 +00:00
{
2019-11-01 15:53:16 +00:00
Type type = item . GetType ( ) ;
2016-11-20 09:46:07 +00:00
saveItemStatement . TryBind ( "@guid" , item . Id ) ;
2019-11-01 15:53:16 +00:00
saveItemStatement . TryBind ( "@type" , type . FullName ) ;
2016-11-20 09:46:07 +00:00
2019-11-01 15:53:16 +00:00
if ( TypeRequiresDeserialization ( type ) )
2016-11-20 09:46:07 +00:00
{
2019-11-01 15:53:16 +00:00
saveItemStatement . TryBind ( "@data" , JsonSerializer . SerializeToUtf8Bytes ( item , type , _jsonOptions ) ) ;
2016-11-20 09:46:07 +00:00
}
else
{
saveItemStatement . TryBindNull ( "@data" ) ;
}
2018-09-12 17:26:21 +00:00
saveItemStatement . TryBind ( "@Path" , GetPathToSave ( item . Path ) ) ;
2016-11-20 09:46:07 +00:00
2019-01-26 17:21:41 +00:00
if ( item is IHasStartDate hasStartDate )
2016-11-20 09:46:07 +00:00
{
saveItemStatement . TryBind ( "@StartDate" , hasStartDate . StartDate ) ;
}
else
{
saveItemStatement . TryBindNull ( "@StartDate" ) ;
}
if ( item . EndDate . HasValue )
{
saveItemStatement . TryBind ( "@EndDate" , item . EndDate . Value ) ;
}
else
{
saveItemStatement . TryBindNull ( "@EndDate" ) ;
}
2019-02-28 22:22:57 +00:00
saveItemStatement . TryBind ( "@ChannelId" , item . ChannelId . Equals ( Guid . Empty ) ? null : item . ChannelId . ToString ( "N" , CultureInfo . InvariantCulture ) ) ;
2016-11-20 09:46:07 +00:00
2019-01-26 17:21:41 +00:00
if ( item is IHasProgramAttributes hasProgramAttributes )
2016-11-20 09:46:07 +00:00
{
saveItemStatement . TryBind ( "@IsMovie" , hasProgramAttributes . IsMovie ) ;
saveItemStatement . TryBind ( "@IsSeries" , hasProgramAttributes . IsSeries ) ;
saveItemStatement . TryBind ( "@EpisodeTitle" , hasProgramAttributes . EpisodeTitle ) ;
saveItemStatement . TryBind ( "@IsRepeat" , hasProgramAttributes . IsRepeat ) ;
}
else
{
saveItemStatement . TryBindNull ( "@IsMovie" ) ;
saveItemStatement . TryBindNull ( "@IsSeries" ) ;
saveItemStatement . TryBindNull ( "@EpisodeTitle" ) ;
saveItemStatement . TryBindNull ( "@IsRepeat" ) ;
}
saveItemStatement . TryBind ( "@CommunityRating" , item . CommunityRating ) ;
saveItemStatement . TryBind ( "@CustomRating" , item . CustomRating ) ;
saveItemStatement . TryBind ( "@IndexNumber" , item . IndexNumber ) ;
saveItemStatement . TryBind ( "@IsLocked" , item . IsLocked ) ;
saveItemStatement . TryBind ( "@Name" , item . Name ) ;
saveItemStatement . TryBind ( "@OfficialRating" , item . OfficialRating ) ;
saveItemStatement . TryBind ( "@MediaType" , item . MediaType ) ;
saveItemStatement . TryBind ( "@Overview" , item . Overview ) ;
saveItemStatement . TryBind ( "@ParentIndexNumber" , item . ParentIndexNumber ) ;
saveItemStatement . TryBind ( "@PremiereDate" , item . PremiereDate ) ;
saveItemStatement . TryBind ( "@ProductionYear" , item . ProductionYear ) ;
2017-09-18 16:52:22 +00:00
var parentId = item . ParentId ;
2018-09-12 17:26:21 +00:00
if ( parentId . Equals ( Guid . Empty ) )
2016-11-20 09:46:07 +00:00
{
saveItemStatement . TryBindNull ( "@ParentId" ) ;
}
else
{
2017-09-18 16:52:22 +00:00
saveItemStatement . TryBind ( "@ParentId" , parentId ) ;
2016-11-20 09:46:07 +00:00
}
2018-09-12 17:26:21 +00:00
if ( item . Genres . Length > 0 )
2016-11-20 09:46:07 +00:00
{
2018-09-12 17:26:21 +00:00
saveItemStatement . TryBind ( "@Genres" , string . Join ( "|" , item . Genres ) ) ;
2016-11-20 09:46:07 +00:00
}
else
{
saveItemStatement . TryBindNull ( "@Genres" ) ;
}
2016-12-11 05:12:00 +00:00
saveItemStatement . TryBind ( "@InheritedParentalRatingValue" , item . InheritedParentalRatingValue ) ;
2016-11-20 09:46:07 +00:00
saveItemStatement . TryBind ( "@SortName" , item . SortName ) ;
2017-06-03 07:36:32 +00:00
saveItemStatement . TryBind ( "@ForcedSortName" , item . ForcedSortName ) ;
2016-11-20 09:46:07 +00:00
saveItemStatement . TryBind ( "@RunTimeTicks" , item . RunTimeTicks ) ;
2019-01-26 08:07:13 +00:00
saveItemStatement . TryBind ( "@Size" , item . Size ) ;
2016-11-20 09:46:07 +00:00
saveItemStatement . TryBind ( "@DateCreated" , item . DateCreated ) ;
saveItemStatement . TryBind ( "@DateModified" , item . DateModified ) ;
saveItemStatement . TryBind ( "@PreferredMetadataLanguage" , item . PreferredMetadataLanguage ) ;
saveItemStatement . TryBind ( "@PreferredMetadataCountryCode" , item . PreferredMetadataCountryCode ) ;
2018-09-12 17:26:21 +00:00
if ( item . Width > 0 )
{
saveItemStatement . TryBind ( "@Width" , item . Width ) ;
}
else
{
saveItemStatement . TryBindNull ( "@Width" ) ;
}
2020-06-15 21:43:52 +00:00
2018-09-12 17:26:21 +00:00
if ( item . Height > 0 )
{
saveItemStatement . TryBind ( "@Height" , item . Height ) ;
}
else
{
saveItemStatement . TryBindNull ( "@Height" ) ;
}
2016-11-20 09:46:07 +00:00
2016-11-23 06:54:09 +00:00
if ( item . DateLastRefreshed ! = default ( DateTime ) )
2016-11-20 09:46:07 +00:00
{
saveItemStatement . TryBind ( "@DateLastRefreshed" , item . DateLastRefreshed ) ;
}
else
{
saveItemStatement . TryBindNull ( "@DateLastRefreshed" ) ;
}
2016-11-23 06:54:09 +00:00
if ( item . DateLastSaved ! = default ( DateTime ) )
2016-11-20 09:46:07 +00:00
{
2016-11-23 06:54:09 +00:00
saveItemStatement . TryBind ( "@DateLastSaved" , item . DateLastSaved ) ;
2016-11-20 09:46:07 +00:00
}
else
{
saveItemStatement . TryBindNull ( "@DateLastSaved" ) ;
}
saveItemStatement . TryBind ( "@IsInMixedFolder" , item . IsInMixedFolder ) ;
2017-08-09 19:56:38 +00:00
if ( item . LockedFields . Length > 0 )
2016-11-20 09:46:07 +00:00
{
2019-01-26 17:21:41 +00:00
saveItemStatement . TryBind ( "@LockedFields" , string . Join ( "|" , item . LockedFields ) ) ;
2016-11-20 09:46:07 +00:00
}
else
{
saveItemStatement . TryBindNull ( "@LockedFields" ) ;
}
2017-08-09 19:56:38 +00:00
if ( item . Studios . Length > 0 )
2016-11-20 09:46:07 +00:00
{
2019-01-26 17:21:41 +00:00
saveItemStatement . TryBind ( "@Studios" , string . Join ( "|" , item . Studios ) ) ;
2016-11-20 09:46:07 +00:00
}
else
{
saveItemStatement . TryBindNull ( "@Studios" ) ;
}
if ( item . Audio . HasValue )
{
saveItemStatement . TryBind ( "@Audio" , item . Audio . Value . ToString ( ) ) ;
}
else
{
saveItemStatement . TryBindNull ( "@Audio" ) ;
}
2019-01-26 17:21:41 +00:00
if ( item is LiveTvChannel liveTvChannel )
2018-09-12 17:26:21 +00:00
{
2019-01-26 17:21:41 +00:00
saveItemStatement . TryBind ( "@ExternalServiceId" , liveTvChannel . ServiceName ) ;
2018-09-12 17:26:21 +00:00
}
else
{
saveItemStatement . TryBindNull ( "@ExternalServiceId" ) ;
}
2016-11-20 09:46:07 +00:00
2017-08-09 19:56:38 +00:00
if ( item . Tags . Length > 0 )
2016-11-20 09:46:07 +00:00
{
2017-08-09 19:56:38 +00:00
saveItemStatement . TryBind ( "@Tags" , string . Join ( "|" , item . Tags ) ) ;
2016-11-20 09:46:07 +00:00
}
else
{
saveItemStatement . TryBindNull ( "@Tags" ) ;
}
saveItemStatement . TryBind ( "@IsFolder" , item . IsFolder ) ;
saveItemStatement . TryBind ( "@UnratedType" , item . GetBlockUnratedType ( ) . ToString ( ) ) ;
2019-01-26 17:21:41 +00:00
if ( topParent = = null )
2016-11-20 09:46:07 +00:00
{
2019-01-26 17:21:41 +00:00
saveItemStatement . TryBindNull ( "@TopParentId" ) ;
2016-11-20 09:46:07 +00:00
}
else
{
2019-02-28 22:22:57 +00:00
saveItemStatement . TryBind ( "@TopParentId" , topParent . Id . ToString ( "N" , CultureInfo . InvariantCulture ) ) ;
2016-11-20 09:46:07 +00:00
}
2019-01-26 17:21:41 +00:00
if ( item is Trailer trailer & & trailer . TrailerTypes . Length > 0 )
2016-11-20 09:46:07 +00:00
{
2019-01-26 17:21:41 +00:00
saveItemStatement . TryBind ( "@TrailerTypes" , string . Join ( "|" , trailer . TrailerTypes ) ) ;
2016-11-20 09:46:07 +00:00
}
else
{
saveItemStatement . TryBindNull ( "@TrailerTypes" ) ;
}
saveItemStatement . TryBind ( "@CriticRating" , item . CriticRating ) ;
if ( string . IsNullOrWhiteSpace ( item . Name ) )
{
saveItemStatement . TryBindNull ( "@CleanName" ) ;
}
else
{
saveItemStatement . TryBind ( "@CleanName" , GetCleanValue ( item . Name ) ) ;
}
2016-12-11 05:12:00 +00:00
saveItemStatement . TryBind ( "@PresentationUniqueKey" , item . PresentationUniqueKey ) ;
2016-11-20 09:46:07 +00:00
saveItemStatement . TryBind ( "@OriginalTitle" , item . OriginalTitle ) ;
2019-01-26 17:21:41 +00:00
if ( item is Video video )
2016-11-20 09:46:07 +00:00
{
saveItemStatement . TryBind ( "@PrimaryVersionId" , video . PrimaryVersionId ) ;
}
else
{
saveItemStatement . TryBindNull ( "@PrimaryVersionId" ) ;
}
2019-01-26 17:21:41 +00:00
if ( item is Folder folder & & folder . DateLastMediaAdded . HasValue )
2016-11-20 09:46:07 +00:00
{
saveItemStatement . TryBind ( "@DateLastMediaAdded" , folder . DateLastMediaAdded . Value ) ;
}
else
{
saveItemStatement . TryBindNull ( "@DateLastMediaAdded" ) ;
}
saveItemStatement . TryBind ( "@Album" , item . Album ) ;
saveItemStatement . TryBind ( "@IsVirtualItem" , item . IsVirtualItem ) ;
2019-01-26 17:21:41 +00:00
if ( item is IHasSeries hasSeriesName )
2016-11-20 09:46:07 +00:00
{
2018-09-12 17:26:21 +00:00
saveItemStatement . TryBind ( "@SeriesName" , hasSeriesName . SeriesName ) ;
2016-11-20 09:46:07 +00:00
}
else
{
saveItemStatement . TryBindNull ( "@SeriesName" ) ;
}
2020-06-15 21:43:52 +00:00
2016-12-11 05:12:00 +00:00
if ( string . IsNullOrWhiteSpace ( userDataKey ) )
{
saveItemStatement . TryBindNull ( "@UserDataKey" ) ;
}
else
{
saveItemStatement . TryBind ( "@UserDataKey" , userDataKey ) ;
}
2016-11-20 09:46:07 +00:00
2019-01-26 17:21:41 +00:00
if ( item is Episode episode )
2016-11-20 09:46:07 +00:00
{
2016-11-23 06:54:09 +00:00
saveItemStatement . TryBind ( "@SeasonName" , episode . SeasonName ) ;
2018-09-12 17:26:21 +00:00
2019-01-26 17:21:41 +00:00
var nullableSeasonId = episode . SeasonId = = Guid . Empty ? ( Guid ? ) null : episode . SeasonId ;
2018-09-12 17:26:21 +00:00
saveItemStatement . TryBind ( "@SeasonId" , nullableSeasonId ) ;
2016-11-20 09:46:07 +00:00
}
else
{
saveItemStatement . TryBindNull ( "@SeasonName" ) ;
saveItemStatement . TryBindNull ( "@SeasonId" ) ;
}
2019-01-26 17:21:41 +00:00
if ( item is IHasSeries hasSeries )
2016-11-20 09:46:07 +00:00
{
2018-09-12 17:26:21 +00:00
var nullableSeriesId = hasSeries . SeriesId . Equals ( Guid . Empty ) ? ( Guid ? ) null : hasSeries . SeriesId ;
saveItemStatement . TryBind ( "@SeriesId" , nullableSeriesId ) ;
2016-12-06 08:24:29 +00:00
saveItemStatement . TryBind ( "@SeriesPresentationUniqueKey" , hasSeries . SeriesPresentationUniqueKey ) ;
2016-11-20 09:46:07 +00:00
}
else
{
saveItemStatement . TryBindNull ( "@SeriesId" ) ;
2016-12-06 08:24:29 +00:00
saveItemStatement . TryBindNull ( "@SeriesPresentationUniqueKey" ) ;
2016-11-20 09:46:07 +00:00
}
saveItemStatement . TryBind ( "@ExternalSeriesId" , item . ExternalSeriesId ) ;
saveItemStatement . TryBind ( "@Tagline" , item . Tagline ) ;
saveItemStatement . TryBind ( "@ProviderIds" , SerializeProviderIds ( item ) ) ;
saveItemStatement . TryBind ( "@Images" , SerializeImages ( item ) ) ;
2017-08-09 19:56:38 +00:00
if ( item . ProductionLocations . Length > 0 )
2016-11-20 09:46:07 +00:00
{
2017-08-09 19:56:38 +00:00
saveItemStatement . TryBind ( "@ProductionLocations" , string . Join ( "|" , item . ProductionLocations ) ) ;
2016-11-20 09:46:07 +00:00
}
else
{
saveItemStatement . TryBindNull ( "@ProductionLocations" ) ;
}
2018-09-12 17:26:21 +00:00
if ( item . ExtraIds . Length > 0 )
2016-11-20 09:46:07 +00:00
{
2019-02-09 00:39:59 +00:00
saveItemStatement . TryBind ( "@ExtraIds" , string . Join ( "|" , item . ExtraIds ) ) ;
2016-11-20 09:46:07 +00:00
}
else
{
2018-09-12 17:26:21 +00:00
saveItemStatement . TryBindNull ( "@ExtraIds" ) ;
2016-11-20 09:46:07 +00:00
}
saveItemStatement . TryBind ( "@TotalBitrate" , item . TotalBitrate ) ;
if ( item . ExtraType . HasValue )
{
saveItemStatement . TryBind ( "@ExtraType" , item . ExtraType . Value . ToString ( ) ) ;
}
else
{
saveItemStatement . TryBindNull ( "@ExtraType" ) ;
}
string artists = null ;
2019-08-29 20:28:33 +00:00
if ( item is IHasArtist hasArtists & & hasArtists . Artists . Count > 0 )
2016-11-20 09:46:07 +00:00
{
2019-01-26 17:21:41 +00:00
artists = string . Join ( "|" , hasArtists . Artists ) ;
2016-11-20 09:46:07 +00:00
}
2020-06-15 21:43:52 +00:00
2016-11-20 09:46:07 +00:00
saveItemStatement . TryBind ( "@Artists" , artists ) ;
string albumArtists = null ;
2019-01-26 17:21:41 +00:00
if ( item is IHasAlbumArtist hasAlbumArtists
2019-08-29 20:28:33 +00:00
& & hasAlbumArtists . AlbumArtists . Count > 0 )
2016-11-20 09:46:07 +00:00
{
2019-01-26 17:21:41 +00:00
albumArtists = string . Join ( "|" , hasAlbumArtists . AlbumArtists ) ;
2016-11-20 09:46:07 +00:00
}
2019-04-03 11:46:07 +00:00
2016-11-20 09:46:07 +00:00
saveItemStatement . TryBind ( "@AlbumArtists" , albumArtists ) ;
saveItemStatement . TryBind ( "@ExternalId" , item . ExternalId ) ;
2019-01-26 17:21:41 +00:00
if ( item is LiveTvProgram program )
2017-09-08 16:13:58 +00:00
{
saveItemStatement . TryBind ( "@ShowId" , program . ShowId ) ;
}
else
{
saveItemStatement . TryBindNull ( "@ShowId" ) ;
}
2019-01-26 17:21:41 +00:00
Guid ownerId = item . OwnerId ;
if ( ownerId = = Guid . Empty )
2017-09-18 16:52:22 +00:00
{
2019-01-26 17:21:41 +00:00
saveItemStatement . TryBindNull ( "@OwnerId" ) ;
2017-09-18 16:52:22 +00:00
}
else
{
2019-01-26 17:21:41 +00:00
saveItemStatement . TryBind ( "@OwnerId" , ownerId ) ;
2017-09-18 16:52:22 +00:00
}
2016-11-20 09:46:07 +00:00
saveItemStatement . MoveNext ( ) ;
}
2019-01-06 20:50:43 +00:00
private static string SerializeProviderIds ( BaseItem item )
2016-11-20 09:46:07 +00:00
{
2019-01-26 17:21:41 +00:00
StringBuilder str = new StringBuilder ( ) ;
foreach ( var i in item . ProviderIds )
{
2019-01-26 18:46:30 +00:00
// Ideally we shouldn't need this IsNullOrWhiteSpace check,
// but we're seeing some cases of bad data slip through
2019-01-26 17:21:41 +00:00
if ( string . IsNullOrWhiteSpace ( i . Value ) )
{
continue ;
}
2019-04-03 11:46:07 +00:00
2019-01-26 17:21:41 +00:00
str . Append ( $"{i.Key}={i.Value}|" ) ;
}
2016-11-20 09:46:07 +00:00
2019-01-26 17:21:41 +00:00
if ( str . Length = = 0 )
2016-11-20 09:46:07 +00:00
{
return null ;
}
2019-04-03 11:46:07 +00:00
2019-01-26 17:21:41 +00:00
str . Length - = 1 ; // Remove last |
return str . ToString ( ) ;
2016-11-20 09:46:07 +00:00
}
2019-01-06 20:50:43 +00:00
private static void DeserializeProviderIds ( string value , BaseItem item )
2016-11-20 09:46:07 +00:00
{
if ( string . IsNullOrWhiteSpace ( value ) )
{
return ;
}
if ( item . ProviderIds . Count > 0 )
{
return ;
}
var parts = value . Split ( new [ ] { '|' } , StringSplitOptions . RemoveEmptyEntries ) ;
foreach ( var part in parts )
{
var idParts = part . Split ( '=' ) ;
if ( idParts . Length = = 2 )
{
item . SetProviderId ( idParts [ 0 ] , idParts [ 1 ] ) ;
}
}
}
private string SerializeImages ( BaseItem item )
{
2017-08-09 19:56:38 +00:00
var images = item . ImageInfos ;
2016-11-20 09:46:07 +00:00
2017-08-09 19:56:38 +00:00
if ( images . Length = = 0 )
2016-11-20 09:46:07 +00:00
{
return null ;
}
2019-04-03 11:46:07 +00:00
2019-01-26 17:21:41 +00:00
StringBuilder str = new StringBuilder ( ) ;
2019-01-26 18:46:30 +00:00
foreach ( var i in images )
2019-01-26 17:21:41 +00:00
{
if ( string . IsNullOrWhiteSpace ( i . Path ) )
{
continue ;
}
2020-06-15 21:43:52 +00:00
2020-07-22 12:34:51 +00:00
str . Append ( ToValueString ( i ) )
. Append ( '|' ) ;
2019-01-26 17:21:41 +00:00
}
2019-04-03 11:46:07 +00:00
2019-01-26 17:21:41 +00:00
str . Length - = 1 ; // Remove last |
return str . ToString ( ) ;
2016-11-20 09:46:07 +00:00
}
private void DeserializeImages ( string value , BaseItem item )
{
if ( string . IsNullOrWhiteSpace ( value ) )
{
return ;
}
2017-08-09 19:56:38 +00:00
if ( item . ImageInfos . Length > 0 )
2016-11-20 09:46:07 +00:00
{
return ;
}
var parts = value . Split ( new [ ] { '|' } , StringSplitOptions . RemoveEmptyEntries ) ;
2017-08-09 19:56:38 +00:00
var list = new List < ItemImageInfo > ( ) ;
2016-11-20 09:46:07 +00:00
foreach ( var part in parts )
{
2016-11-25 18:17:26 +00:00
var image = ItemImageInfoFromValueString ( part ) ;
if ( image ! = null )
{
2017-08-09 19:56:38 +00:00
list . Add ( image ) ;
2016-11-25 18:17:26 +00:00
}
2016-11-20 09:46:07 +00:00
}
2017-08-09 19:56:38 +00:00
2018-12-28 15:48:26 +00:00
item . ImageInfos = list . ToArray ( ) ;
2016-11-20 09:46:07 +00:00
}
public string ToValueString ( ItemImageInfo image )
{
2020-05-28 16:12:08 +00:00
const string Delimeter = "*" ;
2016-11-20 09:46:07 +00:00
2020-05-19 11:50:14 +00:00
var path = image . Path ? ? string . Empty ;
2020-05-20 22:43:19 +00:00
var hash = image . BlurHash ? ? string . Empty ;
2020-03-23 19:05:49 +00:00
2018-09-12 17:26:21 +00:00
return GetPathToSave ( path ) +
2020-05-28 16:12:08 +00:00
Delimeter +
2017-09-15 06:32:20 +00:00
image . DateModified . Ticks . ToString ( CultureInfo . InvariantCulture ) +
2020-05-28 16:12:08 +00:00
Delimeter +
2017-10-22 06:22:43 +00:00
image . Type +
2020-05-28 16:12:08 +00:00
Delimeter +
2017-10-22 06:22:43 +00:00
image . Width . ToString ( CultureInfo . InvariantCulture ) +
2020-05-28 16:12:08 +00:00
Delimeter +
2020-03-23 19:05:49 +00:00
image . Height . ToString ( CultureInfo . InvariantCulture ) +
2020-05-28 16:12:08 +00:00
Delimeter +
2020-03-23 19:05:49 +00:00
// Replace delimiters with other characters.
// This can be removed when we migrate to a proper DB.
hash . Replace ( '*' , '/' ) . Replace ( '|' , '\\' ) ;
2016-11-20 09:46:07 +00:00
}
public ItemImageInfo ItemImageInfoFromValueString ( string value )
{
var parts = value . Split ( new [ ] { '*' } , StringSplitOptions . None ) ;
2017-09-18 16:52:22 +00:00
if ( parts . Length < 3 )
2016-11-25 18:17:26 +00:00
{
return null ;
}
2016-11-20 09:46:07 +00:00
var image = new ItemImageInfo ( ) ;
2018-09-12 17:26:21 +00:00
image . Path = RestorePath ( parts [ 0 ] ) ;
2017-09-18 16:52:22 +00:00
2019-01-13 20:46:33 +00:00
if ( long . TryParse ( parts [ 1 ] , NumberStyles . Any , CultureInfo . InvariantCulture , out var ticks ) )
2017-09-18 16:52:22 +00:00
{
image . DateModified = new DateTime ( ticks , DateTimeKind . Utc ) ;
}
2019-01-13 20:46:33 +00:00
if ( Enum . TryParse ( parts [ 2 ] , true , out ImageType type ) )
2017-09-18 16:52:22 +00:00
{
image . Type = type ;
}
2016-11-20 09:46:07 +00:00
2017-10-22 06:22:43 +00:00
if ( parts . Length > = 5 )
{
2019-01-26 17:21:41 +00:00
if ( int . TryParse ( parts [ 3 ] , NumberStyles . Integer , CultureInfo . InvariantCulture , out var width )
& & int . TryParse ( parts [ 4 ] , NumberStyles . Integer , CultureInfo . InvariantCulture , out var height ) )
2017-10-22 06:22:43 +00:00
{
2019-01-26 17:21:41 +00:00
image . Width = width ;
image . Height = height ;
2017-10-22 06:22:43 +00:00
}
2020-03-23 19:05:49 +00:00
if ( parts . Length > = 6 )
{
2020-05-20 22:43:19 +00:00
image . BlurHash = parts [ 5 ] . Replace ( '/' , '*' ) . Replace ( '\\' , '|' ) ;
2020-03-23 19:05:49 +00:00
}
2017-10-22 06:22:43 +00:00
}
2016-11-20 09:46:07 +00:00
return image ;
}
/// <summary>
2020-06-15 22:37:52 +00:00
/// Internal retrieve from items or users table.
2016-11-20 09:46:07 +00:00
/// </summary>
/// <param name="id">The id.</param>
/// <returns>BaseItem.</returns>
2019-01-13 20:37:13 +00:00
/// <exception cref="ArgumentNullException">id</exception>
/// <exception cref="ArgumentException"></exception>
2016-11-20 09:46:07 +00:00
public BaseItem RetrieveItem ( Guid id )
{
2019-02-08 21:59:28 +00:00
if ( id = = Guid . Empty )
2016-11-20 09:46:07 +00:00
{
2019-10-25 10:47:20 +00:00
throw new ArgumentException ( "Guid can't be empty" , nameof ( id ) ) ;
2016-11-20 09:46:07 +00:00
}
CheckDisposed ( ) ;
2019-01-26 18:46:30 +00:00
2019-02-26 17:30:13 +00:00
using ( var connection = GetConnection ( true ) )
2016-11-20 09:46:07 +00:00
{
2019-04-02 20:15:18 +00:00
using ( var statement = PrepareStatement ( connection , "select " + string . Join ( "," , _retriveItemColumns ) + " from TypedBaseItems where guid = @guid" ) )
2016-11-20 09:46:07 +00:00
{
2019-02-20 13:26:49 +00:00
statement . TryBind ( "@guid" , id ) ;
2016-12-13 08:45:04 +00:00
2019-02-20 13:26:49 +00:00
foreach ( var row in statement . ExecuteQuery ( ) )
{
return GetItem ( row , new InternalItemsQuery ( ) ) ;
2016-12-13 15:44:34 +00:00
}
2016-11-20 09:46:07 +00:00
}
}
2019-02-20 13:26:49 +00:00
return null ;
2016-11-20 09:46:07 +00:00
}
private bool TypeRequiresDeserialization ( Type type )
{
if ( _config . Configuration . SkipDeserializationForBasicTypes )
{
if ( type = = typeof ( Channel ) )
{
return false ;
}
2019-01-26 17:21:41 +00:00
else if ( type = = typeof ( UserRootFolder ) )
2016-11-20 09:46:07 +00:00
{
return false ;
}
2017-08-16 03:40:36 +00:00
}
if ( type = = typeof ( Season ) )
{
return false ;
}
2019-01-26 17:21:41 +00:00
else if ( type = = typeof ( MusicArtist ) )
2017-08-16 03:40:36 +00:00
{
return false ;
2016-11-20 09:46:07 +00:00
}
2019-01-26 17:21:41 +00:00
else if ( type = = typeof ( Person ) )
2017-05-24 19:12:55 +00:00
{
return false ;
}
2019-01-26 17:21:41 +00:00
else if ( type = = typeof ( MusicGenre ) )
2017-05-13 19:32:10 +00:00
{
return false ;
}
2019-01-26 17:21:41 +00:00
else if ( type = = typeof ( Genre ) )
2017-05-13 19:32:10 +00:00
{
return false ;
}
2019-01-26 17:21:41 +00:00
else if ( type = = typeof ( Studio ) )
2017-05-13 19:32:10 +00:00
{
return false ;
}
2019-01-26 17:21:41 +00:00
else if ( type = = typeof ( PlaylistsFolder ) )
2017-05-13 19:32:10 +00:00
{
return false ;
}
2019-01-26 17:21:41 +00:00
else if ( type = = typeof ( PhotoAlbum ) )
2017-05-13 19:32:10 +00:00
{
return false ;
}
2019-01-26 17:21:41 +00:00
else if ( type = = typeof ( Year ) )
2016-11-20 09:46:07 +00:00
{
2017-05-06 19:45:23 +00:00
return false ;
}
2019-01-26 17:21:41 +00:00
else if ( type = = typeof ( Book ) )
2017-05-06 19:45:23 +00:00
{
return false ;
}
2019-01-26 17:21:41 +00:00
else if ( type = = typeof ( LiveTvProgram ) )
2017-05-06 19:45:23 +00:00
{
return false ;
}
2019-01-26 17:21:41 +00:00
else if ( type = = typeof ( AudioBook ) )
2017-05-06 19:45:23 +00:00
{
return false ;
}
2019-01-26 17:21:41 +00:00
else if ( type = = typeof ( Audio ) )
2016-11-20 09:46:07 +00:00
{
2017-08-11 06:29:49 +00:00
return false ;
}
2019-01-26 17:21:41 +00:00
else if ( type = = typeof ( MusicAlbum ) )
2017-08-11 06:29:49 +00:00
{
return false ;
2016-11-20 09:46:07 +00:00
}
return true ;
}
private BaseItem GetItem ( IReadOnlyList < IResultSetValue > reader , InternalItemsQuery query )
2017-05-23 16:43:24 +00:00
{
2018-09-12 17:26:21 +00:00
return GetItem ( reader , query , HasProgramAttributes ( query ) , HasEpisodeAttributes ( query ) , HasServiceName ( query ) , HasStartDate ( query ) , HasTrailerTypes ( query ) , HasArtistFields ( query ) , HasSeriesFields ( query ) ) ;
2017-05-23 16:43:24 +00:00
}
2018-09-12 17:26:21 +00:00
private BaseItem GetItem ( IReadOnlyList < IResultSetValue > reader , InternalItemsQuery query , bool enableProgramAttributes , bool hasEpisodeAttributes , bool hasServiceName , bool queryHasStartDate , bool hasTrailerTypes , bool hasArtistFields , bool hasSeriesFields )
2016-11-20 09:46:07 +00:00
{
var typeString = reader . GetString ( 0 ) ;
var type = _typeMapper . GetType ( typeString ) ;
if ( type = = null )
{
return null ;
}
BaseItem item = null ;
if ( TypeRequiresDeserialization ( type ) )
{
2019-08-18 15:22:45 +00:00
try
2016-11-20 09:46:07 +00:00
{
2019-09-25 15:43:20 +00:00
item = JsonSerializer . Deserialize ( reader [ 1 ] . ToBlob ( ) , type , _jsonOptions ) as BaseItem ;
2019-08-18 15:22:45 +00:00
}
2019-09-25 15:43:20 +00:00
catch ( JsonException ex )
2019-08-18 15:22:45 +00:00
{
2019-09-25 15:43:20 +00:00
Logger . LogError ( ex , "Error deserializing item with JSON: {Data}" , reader . GetString ( 1 ) ) ;
2016-11-20 09:46:07 +00:00
}
}
if ( item = = null )
{
try
{
item = Activator . CreateInstance ( type ) as BaseItem ;
}
catch
{
}
}
if ( item = = null )
{
return null ;
}
2017-05-23 16:43:24 +00:00
var index = 2 ;
if ( queryHasStartDate )
2016-11-20 09:46:07 +00:00
{
2017-05-23 16:43:24 +00:00
if ( ! reader . IsDBNull ( index ) )
2016-11-20 09:46:07 +00:00
{
2019-01-26 17:21:41 +00:00
if ( item is IHasStartDate hasStartDate )
2017-05-23 16:43:24 +00:00
{
hasStartDate . StartDate = reader [ index ] . ReadDateTime ( ) ;
}
2016-11-20 09:46:07 +00:00
}
2020-06-15 21:43:52 +00:00
2017-05-23 16:43:24 +00:00
index + + ;
2016-11-20 09:46:07 +00:00
}
2017-05-23 16:43:24 +00:00
if ( ! reader . IsDBNull ( index ) )
2016-11-20 09:46:07 +00:00
{
2018-09-12 17:26:21 +00:00
item . EndDate = reader [ index ] . TryReadDateTime ( ) ;
2016-11-20 09:46:07 +00:00
}
2020-06-15 21:43:52 +00:00
2017-05-23 16:43:24 +00:00
index + + ;
2016-11-20 09:46:07 +00:00
2017-05-23 16:43:24 +00:00
if ( ! reader . IsDBNull ( index ) )
2016-11-20 09:46:07 +00:00
{
2018-09-12 17:26:21 +00:00
item . ChannelId = new Guid ( reader . GetString ( index ) ) ;
2016-11-20 09:46:07 +00:00
}
2020-06-15 21:43:52 +00:00
2017-05-23 16:43:24 +00:00
index + + ;
2016-11-20 09:46:07 +00:00
2017-05-23 16:43:24 +00:00
if ( enableProgramAttributes )
2016-11-20 09:46:07 +00:00
{
2019-01-26 17:21:41 +00:00
if ( item is IHasProgramAttributes hasProgramAttributes )
2016-11-20 09:46:07 +00:00
{
2017-05-22 04:54:02 +00:00
if ( ! reader . IsDBNull ( index ) )
{
hasProgramAttributes . IsMovie = reader . GetBoolean ( index ) ;
}
2020-06-15 21:43:52 +00:00
2017-05-22 04:54:02 +00:00
index + + ;
2016-11-20 09:46:07 +00:00
2017-05-22 04:54:02 +00:00
if ( ! reader . IsDBNull ( index ) )
{
hasProgramAttributes . IsSeries = reader . GetBoolean ( index ) ;
}
2020-06-15 21:43:52 +00:00
2017-05-22 04:54:02 +00:00
index + + ;
2016-11-20 09:46:07 +00:00
2017-05-22 04:54:02 +00:00
if ( ! reader . IsDBNull ( index ) )
{
hasProgramAttributes . EpisodeTitle = reader . GetString ( index ) ;
}
2020-06-15 21:43:52 +00:00
2017-05-22 04:54:02 +00:00
index + + ;
2016-11-20 09:46:07 +00:00
2017-05-22 04:54:02 +00:00
if ( ! reader . IsDBNull ( index ) )
{
hasProgramAttributes . IsRepeat = reader . GetBoolean ( index ) ;
}
2020-06-15 21:43:52 +00:00
2017-05-22 04:54:02 +00:00
index + + ;
}
else
2016-11-20 09:46:07 +00:00
{
2018-09-12 17:26:21 +00:00
index + = 4 ;
2016-11-20 09:46:07 +00:00
}
}
if ( ! reader . IsDBNull ( index ) )
{
item . CommunityRating = reader . GetFloat ( index ) ;
}
2020-06-15 21:43:52 +00:00
2016-11-20 09:46:07 +00:00
index + + ;
2017-05-22 04:54:02 +00:00
if ( HasField ( query , ItemFields . CustomRating ) )
2016-11-20 09:46:07 +00:00
{
if ( ! reader . IsDBNull ( index ) )
{
item . CustomRating = reader . GetString ( index ) ;
}
2020-06-15 21:43:52 +00:00
2016-11-20 09:46:07 +00:00
index + + ;
}
if ( ! reader . IsDBNull ( index ) )
{
item . IndexNumber = reader . GetInt32 ( index ) ;
}
2020-06-15 21:43:52 +00:00
2016-11-20 09:46:07 +00:00
index + + ;
2017-05-22 04:54:02 +00:00
if ( HasField ( query , ItemFields . Settings ) )
2016-11-20 09:46:07 +00:00
{
if ( ! reader . IsDBNull ( index ) )
{
item . IsLocked = reader . GetBoolean ( index ) ;
}
2020-06-15 21:43:52 +00:00
2016-11-20 09:46:07 +00:00
index + + ;
if ( ! reader . IsDBNull ( index ) )
{
item . PreferredMetadataLanguage = reader . GetString ( index ) ;
}
2020-06-15 21:43:52 +00:00
2016-11-20 09:46:07 +00:00
index + + ;
if ( ! reader . IsDBNull ( index ) )
{
item . PreferredMetadataCountryCode = reader . GetString ( index ) ;
}
2020-06-15 21:43:52 +00:00
2016-11-20 09:46:07 +00:00
index + + ;
}
2018-09-12 17:26:21 +00:00
if ( HasField ( query , ItemFields . Width ) )
2016-11-20 09:46:07 +00:00
{
2018-09-12 17:26:21 +00:00
if ( ! reader . IsDBNull ( index ) )
{
item . Width = reader . GetInt32 ( index ) ;
}
2020-06-15 21:43:52 +00:00
2018-09-12 17:26:21 +00:00
index + + ;
2016-11-20 09:46:07 +00:00
}
2018-09-12 17:26:21 +00:00
if ( HasField ( query , ItemFields . Height ) )
2016-11-20 09:46:07 +00:00
{
2017-05-21 07:25:49 +00:00
if ( ! reader . IsDBNull ( index ) )
{
2018-09-12 17:26:21 +00:00
item . Height = reader . GetInt32 ( index ) ;
2017-05-21 07:25:49 +00:00
}
2020-06-15 21:43:52 +00:00
2017-05-21 07:25:49 +00:00
index + + ;
2016-11-20 09:46:07 +00:00
}
2017-05-22 04:54:02 +00:00
if ( HasField ( query , ItemFields . DateLastRefreshed ) )
2016-11-20 09:46:07 +00:00
{
2017-05-22 04:54:02 +00:00
if ( ! reader . IsDBNull ( index ) )
{
item . DateLastRefreshed = reader [ index ] . ReadDateTime ( ) ;
}
2020-06-15 21:43:52 +00:00
2017-05-22 04:54:02 +00:00
index + + ;
2016-11-20 09:46:07 +00:00
}
if ( ! reader . IsDBNull ( index ) )
{
item . Name = reader . GetString ( index ) ;
}
2020-06-15 21:43:52 +00:00
2016-11-20 09:46:07 +00:00
index + + ;
if ( ! reader . IsDBNull ( index ) )
{
2018-09-12 17:26:21 +00:00
item . Path = RestorePath ( reader . GetString ( index ) ) ;
2016-11-20 09:46:07 +00:00
}
2020-06-15 21:43:52 +00:00
2016-11-20 09:46:07 +00:00
index + + ;
if ( ! reader . IsDBNull ( index ) )
{
2018-09-12 17:26:21 +00:00
item . PremiereDate = reader [ index ] . TryReadDateTime ( ) ;
2016-11-20 09:46:07 +00:00
}
2020-06-15 21:43:52 +00:00
2016-11-20 09:46:07 +00:00
index + + ;
2017-05-22 04:54:02 +00:00
if ( HasField ( query , ItemFields . Overview ) )
2016-11-20 09:46:07 +00:00
{
if ( ! reader . IsDBNull ( index ) )
{
item . Overview = reader . GetString ( index ) ;
}
2020-06-15 21:43:52 +00:00
2016-11-20 09:46:07 +00:00
index + + ;
}
if ( ! reader . IsDBNull ( index ) )
{
item . ParentIndexNumber = reader . GetInt32 ( index ) ;
}
2020-06-15 21:43:52 +00:00
2016-11-20 09:46:07 +00:00
index + + ;
if ( ! reader . IsDBNull ( index ) )
{
item . ProductionYear = reader . GetInt32 ( index ) ;
}
2020-06-15 21:43:52 +00:00
2016-11-20 09:46:07 +00:00
index + + ;
if ( ! reader . IsDBNull ( index ) )
{
item . OfficialRating = reader . GetString ( index ) ;
}
2020-06-15 21:43:52 +00:00
2016-11-20 09:46:07 +00:00
index + + ;
2017-05-22 04:54:02 +00:00
if ( HasField ( query , ItemFields . SortName ) )
2016-11-20 09:46:07 +00:00
{
if ( ! reader . IsDBNull ( index ) )
{
2017-06-03 07:36:32 +00:00
item . ForcedSortName = reader . GetString ( index ) ;
2016-11-20 09:46:07 +00:00
}
2020-06-15 21:43:52 +00:00
2016-11-20 09:46:07 +00:00
index + + ;
}
if ( ! reader . IsDBNull ( index ) )
{
item . RunTimeTicks = reader . GetInt64 ( index ) ;
}
2020-06-15 21:43:52 +00:00
2016-11-20 09:46:07 +00:00
index + + ;
2019-01-26 08:07:13 +00:00
if ( ! reader . IsDBNull ( index ) )
{
item . Size = reader . GetInt64 ( index ) ;
}
2020-06-15 21:43:52 +00:00
2019-01-26 08:07:13 +00:00
index + + ;
2017-05-22 04:54:02 +00:00
if ( HasField ( query , ItemFields . DateCreated ) )
2016-11-20 09:46:07 +00:00
{
if ( ! reader . IsDBNull ( index ) )
{
item . DateCreated = reader [ index ] . ReadDateTime ( ) ;
}
2020-06-15 21:43:52 +00:00
2016-11-20 09:46:07 +00:00
index + + ;
}
if ( ! reader . IsDBNull ( index ) )
{
item . DateModified = reader [ index ] . ReadDateTime ( ) ;
}
2020-06-15 21:43:52 +00:00
2016-11-20 09:46:07 +00:00
index + + ;
item . Id = reader . GetGuid ( index ) ;
index + + ;
2017-05-22 04:54:02 +00:00
if ( HasField ( query , ItemFields . Genres ) )
2016-11-20 09:46:07 +00:00
{
if ( ! reader . IsDBNull ( index ) )
{
2018-09-12 17:26:21 +00:00
item . Genres = reader . GetString ( index ) . Split ( new [ ] { '|' } , StringSplitOptions . RemoveEmptyEntries ) ;
2016-11-20 09:46:07 +00:00
}
2020-06-15 21:43:52 +00:00
2016-11-20 09:46:07 +00:00
index + + ;
}
if ( ! reader . IsDBNull ( index ) )
{
item . ParentId = reader . GetGuid ( index ) ;
}
2020-06-15 21:43:52 +00:00
2016-11-20 09:46:07 +00:00
index + + ;
if ( ! reader . IsDBNull ( index ) )
{
2019-01-13 20:46:33 +00:00
if ( Enum . TryParse ( reader . GetString ( index ) , true , out ProgramAudio audio ) )
2017-07-25 18:32:03 +00:00
{
item . Audio = audio ;
}
2016-11-20 09:46:07 +00:00
}
2020-06-15 21:43:52 +00:00
2016-11-20 09:46:07 +00:00
index + + ;
// TODO: Even if not needed by apps, the server needs it internally
// But get this excluded from contexts where it is not needed
2018-09-12 17:26:21 +00:00
if ( hasServiceName )
2016-11-20 09:46:07 +00:00
{
2019-01-26 17:21:41 +00:00
if ( item is LiveTvChannel liveTvChannel )
2018-09-12 17:26:21 +00:00
{
if ( ! reader . IsDBNull ( index ) )
{
2019-01-26 17:21:41 +00:00
liveTvChannel . ServiceName = reader . GetString ( index ) ;
2018-09-12 17:26:21 +00:00
}
}
2020-06-15 21:43:52 +00:00
2018-09-12 17:26:21 +00:00
index + + ;
2016-11-20 09:46:07 +00:00
}
if ( ! reader . IsDBNull ( index ) )
{
item . IsInMixedFolder = reader . GetBoolean ( index ) ;
}
2020-06-15 21:43:52 +00:00
2016-11-20 09:46:07 +00:00
index + + ;
2017-05-22 04:54:02 +00:00
if ( HasField ( query , ItemFields . DateLastSaved ) )
2016-11-20 09:46:07 +00:00
{
2017-05-22 04:54:02 +00:00
if ( ! reader . IsDBNull ( index ) )
{
item . DateLastSaved = reader [ index ] . ReadDateTime ( ) ;
}
2020-06-15 21:43:52 +00:00
2017-05-22 04:54:02 +00:00
index + + ;
2016-11-20 09:46:07 +00:00
}
2017-05-22 04:54:02 +00:00
if ( HasField ( query , ItemFields . Settings ) )
2016-11-20 09:46:07 +00:00
{
if ( ! reader . IsDBNull ( index ) )
{
2020-06-09 22:12:53 +00:00
IEnumerable < MetadataField > GetLockedFields ( string s )
2019-01-26 17:21:41 +00:00
{
2020-03-24 15:12:06 +00:00
foreach ( var i in s . Split ( new [ ] { '|' } , StringSplitOptions . RemoveEmptyEntries ) )
2017-07-25 18:32:03 +00:00
{
2020-06-09 22:12:53 +00:00
if ( Enum . TryParse ( i , true , out MetadataField parsedValue ) )
2017-07-25 18:32:03 +00:00
{
2019-01-26 17:21:41 +00:00
yield return parsedValue ;
2017-07-25 18:32:03 +00:00
}
2019-01-26 17:21:41 +00:00
}
}
2020-06-15 21:43:52 +00:00
2019-01-26 17:21:41 +00:00
item . LockedFields = GetLockedFields ( reader . GetString ( index ) ) . ToArray ( ) ;
2016-11-20 09:46:07 +00:00
}
2020-06-15 21:43:52 +00:00
2016-11-20 09:46:07 +00:00
index + + ;
}
2017-05-22 04:54:02 +00:00
if ( HasField ( query , ItemFields . Studios ) )
2016-11-20 09:46:07 +00:00
{
if ( ! reader . IsDBNull ( index ) )
{
2017-08-09 19:56:38 +00:00
item . Studios = reader . GetString ( index ) . Split ( new [ ] { '|' } , StringSplitOptions . RemoveEmptyEntries ) ;
2016-11-20 09:46:07 +00:00
}
2020-06-15 21:43:52 +00:00
2016-11-20 09:46:07 +00:00
index + + ;
}
2017-05-22 04:54:02 +00:00
if ( HasField ( query , ItemFields . Tags ) )
2016-11-20 09:46:07 +00:00
{
if ( ! reader . IsDBNull ( index ) )
{
2017-08-09 19:56:38 +00:00
item . Tags = reader . GetString ( index ) . Split ( new [ ] { '|' } , StringSplitOptions . RemoveEmptyEntries ) ;
2016-11-20 09:46:07 +00:00
}
2020-06-15 21:43:52 +00:00
2016-11-20 09:46:07 +00:00
index + + ;
}
2017-05-23 16:43:24 +00:00
if ( hasTrailerTypes )
2016-11-20 09:46:07 +00:00
{
2019-01-26 17:21:41 +00:00
if ( item is Trailer trailer )
2016-11-20 09:46:07 +00:00
{
2017-05-23 16:43:24 +00:00
if ( ! reader . IsDBNull ( index ) )
{
2019-01-26 17:21:41 +00:00
IEnumerable < TrailerType > GetTrailerTypes ( string s )
{
foreach ( var i in s . Split ( new [ ] { '|' } , StringSplitOptions . RemoveEmptyEntries ) )
2017-07-25 18:32:03 +00:00
{
2019-01-13 20:46:33 +00:00
if ( Enum . TryParse ( i , true , out TrailerType parsedValue ) )
2017-09-15 06:32:20 +00:00
{
2019-01-26 17:21:41 +00:00
yield return parsedValue ;
2017-09-15 06:32:20 +00:00
}
2019-01-26 17:21:41 +00:00
}
}
2020-06-15 21:43:52 +00:00
2019-01-26 17:21:41 +00:00
trailer . TrailerTypes = GetTrailerTypes ( reader . GetString ( index ) ) . ToArray ( ) ;
2017-05-23 16:43:24 +00:00
}
2016-11-20 09:46:07 +00:00
}
2020-06-15 21:43:52 +00:00
2017-05-23 16:43:24 +00:00
index + + ;
2016-11-20 09:46:07 +00:00
}
2017-05-22 04:54:02 +00:00
if ( HasField ( query , ItemFields . OriginalTitle ) )
2016-11-20 09:46:07 +00:00
{
if ( ! reader . IsDBNull ( index ) )
{
item . OriginalTitle = reader . GetString ( index ) ;
}
2020-06-15 21:43:52 +00:00
2016-11-20 09:46:07 +00:00
index + + ;
}
2019-01-26 17:21:41 +00:00
if ( item is Video video )
2016-11-20 09:46:07 +00:00
{
if ( ! reader . IsDBNull ( index ) )
{
video . PrimaryVersionId = reader . GetString ( index ) ;
}
}
2020-06-15 21:43:52 +00:00
2016-11-20 09:46:07 +00:00
index + + ;
2017-05-22 04:54:02 +00:00
if ( HasField ( query , ItemFields . DateLastMediaAdded ) )
2016-11-20 09:46:07 +00:00
{
2019-01-26 17:21:41 +00:00
if ( item is Folder folder & & ! reader . IsDBNull ( index ) )
2016-11-20 09:46:07 +00:00
{
2018-09-12 17:26:21 +00:00
folder . DateLastMediaAdded = reader [ index ] . TryReadDateTime ( ) ;
2016-11-20 09:46:07 +00:00
}
2020-06-15 21:43:52 +00:00
2016-11-20 09:46:07 +00:00
index + + ;
}
if ( ! reader . IsDBNull ( index ) )
{
item . Album = reader . GetString ( index ) ;
}
2020-06-15 21:43:52 +00:00
2016-11-20 09:46:07 +00:00
index + + ;
if ( ! reader . IsDBNull ( index ) )
{
item . CriticRating = reader . GetFloat ( index ) ;
}
2020-06-15 21:43:52 +00:00
2016-11-20 09:46:07 +00:00
index + + ;
if ( ! reader . IsDBNull ( index ) )
{
item . IsVirtualItem = reader . GetBoolean ( index ) ;
}
2020-06-15 21:43:52 +00:00
2016-11-20 09:46:07 +00:00
index + + ;
2019-01-26 17:21:41 +00:00
if ( item is IHasSeries hasSeriesName )
2016-11-20 09:46:07 +00:00
{
2018-09-12 17:26:21 +00:00
if ( ! reader . IsDBNull ( index ) )
2016-11-20 09:46:07 +00:00
{
2018-09-12 17:26:21 +00:00
hasSeriesName . SeriesName = reader . GetString ( index ) ;
2016-11-20 09:46:07 +00:00
}
}
2020-06-15 21:43:52 +00:00
2018-09-12 17:26:21 +00:00
index + + ;
2016-11-20 09:46:07 +00:00
2017-05-23 16:43:24 +00:00
if ( hasEpisodeAttributes )
2016-11-20 09:46:07 +00:00
{
2019-01-26 17:21:41 +00:00
if ( item is Episode episode )
2016-11-20 09:46:07 +00:00
{
2017-05-23 16:43:24 +00:00
if ( ! reader . IsDBNull ( index ) )
{
episode . SeasonName = reader . GetString ( index ) ;
}
2020-06-15 21:43:52 +00:00
2017-05-23 16:43:24 +00:00
index + + ;
if ( ! reader . IsDBNull ( index ) )
{
episode . SeasonId = reader . GetGuid ( index ) ;
}
2016-11-20 09:46:07 +00:00
}
2017-05-23 16:43:24 +00:00
else
2016-11-20 09:46:07 +00:00
{
2017-05-23 16:43:24 +00:00
index + + ;
2016-11-20 09:46:07 +00:00
}
2020-06-15 21:43:52 +00:00
2016-11-20 09:46:07 +00:00
index + + ;
}
2018-09-12 17:26:21 +00:00
var hasSeries = item as IHasSeries ;
2017-05-23 16:56:01 +00:00
if ( hasSeriesFields )
2016-11-20 09:46:07 +00:00
{
2017-05-23 16:56:01 +00:00
if ( hasSeries ! = null )
2016-11-20 09:46:07 +00:00
{
2017-05-23 16:56:01 +00:00
if ( ! reader . IsDBNull ( index ) )
{
hasSeries . SeriesId = reader . GetGuid ( index ) ;
}
2016-11-20 09:46:07 +00:00
}
2020-06-15 21:43:52 +00:00
2017-05-23 16:56:01 +00:00
index + + ;
2016-11-20 09:46:07 +00:00
}
2017-05-22 04:54:02 +00:00
if ( HasField ( query , ItemFields . PresentationUniqueKey ) )
2016-11-20 09:46:07 +00:00
{
2017-05-21 07:25:49 +00:00
if ( ! reader . IsDBNull ( index ) )
{
item . PresentationUniqueKey = reader . GetString ( index ) ;
}
2020-06-15 21:43:52 +00:00
2017-05-21 07:25:49 +00:00
index + + ;
2016-11-20 09:46:07 +00:00
}
2017-05-22 04:54:02 +00:00
if ( HasField ( query , ItemFields . InheritedParentalRatingValue ) )
2016-11-20 09:46:07 +00:00
{
2017-05-21 07:25:49 +00:00
if ( ! reader . IsDBNull ( index ) )
{
item . InheritedParentalRatingValue = reader . GetInt32 ( index ) ;
}
2020-06-15 21:43:52 +00:00
2017-05-21 07:25:49 +00:00
index + + ;
2016-11-20 09:46:07 +00:00
}
2017-05-22 04:54:02 +00:00
if ( HasField ( query , ItemFields . ExternalSeriesId ) )
2016-11-20 09:46:07 +00:00
{
2017-05-21 07:25:49 +00:00
if ( ! reader . IsDBNull ( index ) )
{
item . ExternalSeriesId = reader . GetString ( index ) ;
}
2020-06-15 21:43:52 +00:00
2017-05-21 07:25:49 +00:00
index + + ;
2016-11-20 09:46:07 +00:00
}
2017-05-22 04:54:02 +00:00
if ( HasField ( query , ItemFields . Taglines ) )
2016-11-20 09:46:07 +00:00
{
if ( ! reader . IsDBNull ( index ) )
{
item . Tagline = reader . GetString ( index ) ;
}
2020-06-15 21:43:52 +00:00
2016-11-20 09:46:07 +00:00
index + + ;
}
if ( ! reader . IsDBNull ( index ) )
{
DeserializeProviderIds ( reader . GetString ( index ) , item ) ;
}
2020-06-15 21:43:52 +00:00
2016-11-20 09:46:07 +00:00
index + + ;
if ( query . DtoOptions . EnableImages )
{
if ( ! reader . IsDBNull ( index ) )
{
DeserializeImages ( reader . GetString ( index ) , item ) ;
}
2020-06-15 21:43:52 +00:00
2016-11-20 09:46:07 +00:00
index + + ;
}
2017-05-22 04:54:02 +00:00
if ( HasField ( query , ItemFields . ProductionLocations ) )
2016-11-20 09:46:07 +00:00
{
if ( ! reader . IsDBNull ( index ) )
{
2020-03-24 15:12:06 +00:00
item . ProductionLocations = reader . GetString ( index ) . Split ( new [ ] { '|' } , StringSplitOptions . RemoveEmptyEntries ) . ToArray ( ) ;
2016-11-20 09:46:07 +00:00
}
2020-06-15 21:43:52 +00:00
2016-11-20 09:46:07 +00:00
index + + ;
}
2018-09-12 17:26:21 +00:00
if ( HasField ( query , ItemFields . ExtraIds ) )
2016-11-20 09:46:07 +00:00
{
if ( ! reader . IsDBNull ( index ) )
{
2018-09-12 17:26:21 +00:00
item . ExtraIds = SplitToGuids ( reader . GetString ( index ) ) ;
2016-11-20 09:46:07 +00:00
}
2020-06-15 21:43:52 +00:00
2016-11-20 09:46:07 +00:00
index + + ;
}
if ( ! reader . IsDBNull ( index ) )
{
item . TotalBitrate = reader . GetInt32 ( index ) ;
}
2020-06-15 21:43:52 +00:00
2016-11-20 09:46:07 +00:00
index + + ;
if ( ! reader . IsDBNull ( index ) )
{
2019-01-13 20:46:33 +00:00
if ( Enum . TryParse ( reader . GetString ( index ) , true , out ExtraType extraType ) )
2017-07-25 18:32:03 +00:00
{
item . ExtraType = extraType ;
}
2016-11-20 09:46:07 +00:00
}
2020-06-15 21:43:52 +00:00
2016-11-20 09:46:07 +00:00
index + + ;
2017-05-23 16:43:24 +00:00
if ( hasArtistFields )
2016-11-20 09:46:07 +00:00
{
2019-01-26 17:21:41 +00:00
if ( item is IHasArtist hasArtists & & ! reader . IsDBNull ( index ) )
2017-05-23 16:43:24 +00:00
{
2017-08-24 19:52:19 +00:00
hasArtists . Artists = reader . GetString ( index ) . Split ( new [ ] { '|' } , StringSplitOptions . RemoveEmptyEntries ) ;
2017-05-23 16:43:24 +00:00
}
2020-06-15 21:43:52 +00:00
2017-05-23 16:43:24 +00:00
index + + ;
2016-11-20 09:46:07 +00:00
2019-01-26 17:21:41 +00:00
if ( item is IHasAlbumArtist hasAlbumArtists & & ! reader . IsDBNull ( index ) )
2017-05-23 16:43:24 +00:00
{
2017-08-10 18:01:31 +00:00
hasAlbumArtists . AlbumArtists = reader . GetString ( index ) . Split ( new [ ] { '|' } , StringSplitOptions . RemoveEmptyEntries ) ;
2017-05-23 16:43:24 +00:00
}
2020-06-15 21:43:52 +00:00
2017-05-23 16:43:24 +00:00
index + + ;
2016-11-20 09:46:07 +00:00
}
if ( ! reader . IsDBNull ( index ) )
{
item . ExternalId = reader . GetString ( index ) ;
}
2020-06-15 21:43:52 +00:00
2016-11-20 09:46:07 +00:00
index + + ;
2017-05-22 04:54:02 +00:00
if ( HasField ( query , ItemFields . SeriesPresentationUniqueKey ) )
2016-12-06 08:24:29 +00:00
{
2017-05-22 04:54:02 +00:00
if ( hasSeries ! = null )
2016-12-06 08:24:29 +00:00
{
2017-05-22 04:54:02 +00:00
if ( ! reader . IsDBNull ( index ) )
{
hasSeries . SeriesPresentationUniqueKey = reader . GetString ( index ) ;
}
2016-12-06 08:24:29 +00:00
}
2020-06-15 21:43:52 +00:00
2017-05-22 04:54:02 +00:00
index + + ;
2016-12-06 08:24:29 +00:00
}
2017-09-08 16:13:58 +00:00
if ( enableProgramAttributes )
{
2019-01-26 17:21:41 +00:00
if ( item is LiveTvProgram program & & ! reader . IsDBNull ( index ) )
2017-09-08 16:13:58 +00:00
{
2019-01-26 17:21:41 +00:00
program . ShowId = reader . GetString ( index ) ;
2017-09-08 16:13:58 +00:00
}
2020-06-15 21:43:52 +00:00
2019-01-26 17:21:41 +00:00
index + + ;
2017-09-08 16:13:58 +00:00
}
2017-09-18 16:52:22 +00:00
if ( ! reader . IsDBNull ( index ) )
{
item . OwnerId = reader . GetGuid ( index ) ;
}
2020-06-15 21:43:52 +00:00
2017-09-18 16:52:22 +00:00
index + + ;
2016-11-20 09:46:07 +00:00
return item ;
}
2019-01-06 20:50:43 +00:00
private static Guid [ ] SplitToGuids ( string value )
2017-08-09 19:56:38 +00:00
{
var ids = value . Split ( '|' ) ;
var result = new Guid [ ids . Length ] ;
for ( var i = 0 ; i < result . Length ; i + + )
{
result [ i ] = new Guid ( ids [ i ] ) ;
}
return result ;
}
2016-11-20 09:46:07 +00:00
/// <summary>
2020-06-15 22:37:52 +00:00
/// Gets chapters for an item.
2016-11-20 09:46:07 +00:00
/// </summary>
2019-01-06 20:50:43 +00:00
/// <param name="item">The item.</param>
2016-11-20 09:46:07 +00:00
/// <returns>IEnumerable{ChapterInfo}.</returns>
2019-01-13 20:37:13 +00:00
/// <exception cref="ArgumentNullException">id</exception>
2018-09-12 17:26:21 +00:00
public List < ChapterInfo > GetChapters ( BaseItem item )
2016-11-20 09:46:07 +00:00
{
CheckDisposed ( ) ;
2019-02-26 17:30:13 +00:00
using ( var connection = GetConnection ( true ) )
2016-11-20 09:46:07 +00:00
{
2019-07-01 15:59:01 +00:00
var chapters = new List < ChapterInfo > ( ) ;
2019-02-20 13:26:49 +00:00
2019-04-02 20:15:18 +00:00
using ( var statement = PrepareStatement ( connection , "select StartPositionTicks,Name,ImagePath,ImageDateModified from " + ChaptersTableName + " where ItemId = @ItemId order by ChapterIndex asc" ) )
2016-11-20 09:46:07 +00:00
{
2019-02-20 13:26:49 +00:00
statement . TryBind ( "@ItemId" , item . Id ) ;
2016-12-13 15:44:34 +00:00
2019-02-20 13:26:49 +00:00
foreach ( var row in statement . ExecuteQuery ( ) )
2016-11-20 09:46:07 +00:00
{
2019-07-01 15:59:01 +00:00
chapters . Add ( GetChapter ( row , item ) ) ;
2016-12-13 15:44:34 +00:00
}
2016-11-20 09:46:07 +00:00
}
2019-02-20 13:26:49 +00:00
2019-07-01 15:59:01 +00:00
return chapters ;
2016-11-20 09:46:07 +00:00
}
}
/// <summary>
2020-06-15 22:37:52 +00:00
/// Gets a single chapter for an item.
2016-11-20 09:46:07 +00:00
/// </summary>
2019-01-06 20:50:43 +00:00
/// <param name="item">The item.</param>
2016-11-20 09:46:07 +00:00
/// <param name="index">The index.</param>
/// <returns>ChapterInfo.</returns>
2019-01-13 20:37:13 +00:00
/// <exception cref="ArgumentNullException">id</exception>
2018-09-12 17:26:21 +00:00
public ChapterInfo GetChapter ( BaseItem item , int index )
2016-11-20 09:46:07 +00:00
{
CheckDisposed ( ) ;
2019-02-26 17:30:13 +00:00
using ( var connection = GetConnection ( true ) )
2016-11-20 09:46:07 +00:00
{
2019-04-02 20:15:18 +00:00
using ( var statement = PrepareStatement ( connection , "select StartPositionTicks,Name,ImagePath,ImageDateModified from " + ChaptersTableName + " where ItemId = @ItemId and ChapterIndex=@ChapterIndex" ) )
2016-11-20 09:46:07 +00:00
{
2019-02-20 13:26:49 +00:00
statement . TryBind ( "@ItemId" , item . Id ) ;
statement . TryBind ( "@ChapterIndex" , index ) ;
2016-11-21 03:52:58 +00:00
2019-02-20 13:26:49 +00:00
foreach ( var row in statement . ExecuteQuery ( ) )
{
return GetChapter ( row , item ) ;
2016-11-20 09:46:07 +00:00
}
}
}
2019-02-20 13:26:49 +00:00
2016-11-20 09:46:07 +00:00
return null ;
}
/// <summary>
/// Gets the chapter.
/// </summary>
/// <param name="reader">The reader.</param>
2020-05-29 09:28:19 +00:00
/// <param name="item">The item.</param>
2016-11-20 09:46:07 +00:00
/// <returns>ChapterInfo.</returns>
2018-09-12 17:26:21 +00:00
private ChapterInfo GetChapter ( IReadOnlyList < IResultSetValue > reader , BaseItem item )
2016-11-20 09:46:07 +00:00
{
var chapter = new ChapterInfo
{
StartPositionTicks = reader . GetInt64 ( 0 )
} ;
if ( ! reader . IsDBNull ( 1 ) )
{
chapter . Name = reader . GetString ( 1 ) ;
}
if ( ! reader . IsDBNull ( 2 ) )
{
chapter . ImagePath = reader . GetString ( 2 ) ;
2018-09-12 17:26:21 +00:00
if ( ! string . IsNullOrEmpty ( chapter . ImagePath ) )
{
2020-04-05 19:19:04 +00:00
try
{
2020-04-23 09:29:19 +00:00
chapter . ImageTag = _imageProcessor . GetImageCacheTag ( item , chapter ) ;
2020-04-05 19:19:04 +00:00
}
2020-04-15 09:14:54 +00:00
catch ( Exception ex )
2020-04-05 19:19:04 +00:00
{
2020-04-15 09:14:54 +00:00
Logger . LogError ( ex , "Failed to create image cache tag." ) ;
2020-04-05 19:19:04 +00:00
}
2018-09-12 17:26:21 +00:00
}
2016-11-20 09:46:07 +00:00
}
if ( ! reader . IsDBNull ( 3 ) )
{
chapter . ImageDateModified = reader [ 3 ] . ReadDateTime ( ) ;
}
return chapter ;
}
/// <summary>
/// Saves the chapters.
/// </summary>
2020-02-23 09:53:51 +00:00
public void SaveChapters ( Guid id , IReadOnlyList < ChapterInfo > chapters )
2016-11-20 09:46:07 +00:00
{
CheckDisposed ( ) ;
2018-09-12 17:26:21 +00:00
if ( id . Equals ( Guid . Empty ) )
2016-11-20 09:46:07 +00:00
{
2019-01-06 20:50:43 +00:00
throw new ArgumentNullException ( nameof ( id ) ) ;
2016-11-20 09:46:07 +00:00
}
if ( chapters = = null )
{
2019-01-06 20:50:43 +00:00
throw new ArgumentNullException ( nameof ( chapters ) ) ;
2016-11-20 09:46:07 +00:00
}
2019-09-25 11:24:39 +00:00
var idBlob = id . ToByteArray ( ) ;
2019-08-18 20:05:06 +00:00
2019-02-26 17:30:13 +00:00
using ( var connection = GetConnection ( ) )
2016-11-20 09:46:07 +00:00
{
2019-02-20 13:26:49 +00:00
connection . RunInTransaction ( db = >
2016-11-20 09:46:07 +00:00
{
2019-08-18 20:05:06 +00:00
// First delete chapters
db . Execute ( "delete from " + ChaptersTableName + " where ItemId=@ItemId" , idBlob ) ;
2016-11-21 03:52:58 +00:00
2019-02-20 13:26:49 +00:00
InsertChapters ( idBlob , chapters , db ) ;
} , TransactionMode ) ;
2018-09-12 17:26:21 +00:00
}
}
2016-11-20 09:46:07 +00:00
2020-02-23 09:53:51 +00:00
private void InsertChapters ( byte [ ] idBlob , IReadOnlyList < ChapterInfo > chapters , IDatabaseConnection db )
2018-09-12 17:26:21 +00:00
{
var startIndex = 0 ;
var limit = 100 ;
var chapterIndex = 0 ;
2016-11-20 09:46:07 +00:00
2020-02-23 09:53:51 +00:00
const string StartInsertText = "insert into " + ChaptersTableName + " (ItemId, ChapterIndex, StartPositionTicks, Name, ImagePath, ImageDateModified) values " ;
var insertText = new StringBuilder ( StartInsertText , 256 ) ;
2018-09-12 17:26:21 +00:00
while ( startIndex < chapters . Count )
{
var endIndex = Math . Min ( chapters . Count , startIndex + limit ) ;
for ( var i = startIndex ; i < endIndex ; i + + )
{
2019-01-26 17:21:41 +00:00
insertText . AppendFormat ( "(@ItemId, @ChapterIndex{0}, @StartPositionTicks{0}, @Name{0}, @ImagePath{0}, @ImageDateModified{0})," , i . ToString ( CultureInfo . InvariantCulture ) ) ;
2018-09-12 17:26:21 +00:00
}
2020-02-23 09:53:51 +00:00
2019-01-26 17:21:41 +00:00
insertText . Length - = 1 ; // Remove last ,
2018-09-12 17:26:21 +00:00
2019-04-02 20:15:18 +00:00
using ( var statement = PrepareStatement ( db , insertText . ToString ( ) ) )
2018-09-12 17:26:21 +00:00
{
statement . TryBind ( "@ItemId" , idBlob ) ;
for ( var i = startIndex ; i < endIndex ; i + + )
{
var index = i . ToString ( CultureInfo . InvariantCulture ) ;
var chapter = chapters [ i ] ;
statement . TryBind ( "@ChapterIndex" + index , chapterIndex ) ;
statement . TryBind ( "@StartPositionTicks" + index , chapter . StartPositionTicks ) ;
statement . TryBind ( "@Name" + index , chapter . Name ) ;
statement . TryBind ( "@ImagePath" + index , chapter . ImagePath ) ;
statement . TryBind ( "@ImageDateModified" + index , chapter . ImageDateModified ) ;
chapterIndex + + ;
}
statement . Reset ( ) ;
statement . MoveNext ( ) ;
2016-11-21 03:52:58 +00:00
}
2018-09-12 17:26:21 +00:00
startIndex + = limit ;
2020-02-23 09:53:51 +00:00
insertText . Length = StartInsertText . Length ;
2016-11-20 09:46:07 +00:00
}
}
2019-01-06 20:50:43 +00:00
private static bool EnableJoinUserData ( InternalItemsQuery query )
2016-11-20 09:46:07 +00:00
{
if ( query . User = = null )
{
return false ;
}
2019-02-08 21:59:28 +00:00
var sortingFields = new HashSet < string > ( query . OrderBy . Select ( i = > i . Item1 ) , StringComparer . OrdinalIgnoreCase ) ;
2016-11-20 09:46:07 +00:00
2019-02-08 21:59:28 +00:00
return sortingFields . Contains ( ItemSortBy . IsFavoriteOrLiked )
| | sortingFields . Contains ( ItemSortBy . IsPlayed )
| | sortingFields . Contains ( ItemSortBy . IsUnplayed )
| | sortingFields . Contains ( ItemSortBy . PlayCount )
| | sortingFields . Contains ( ItemSortBy . DatePlayed )
| | sortingFields . Contains ( ItemSortBy . SeriesDatePlayed )
2019-01-26 17:21:41 +00:00
| | query . IsFavoriteOrLiked . HasValue
| | query . IsFavorite . HasValue
| | query . IsResumable . HasValue
| | query . IsPlayed . HasValue
| | query . IsLiked . HasValue ;
2016-11-20 09:46:07 +00:00
}
2019-02-09 00:09:22 +00:00
private readonly ItemFields [ ] _allFields = Enum . GetNames ( typeof ( ItemFields ) )
2017-09-15 06:32:20 +00:00
. Select ( i = > ( ItemFields ) Enum . Parse ( typeof ( ItemFields ) , i , true ) )
2019-02-09 00:09:22 +00:00
. ToArray ( ) ;
2016-11-20 09:46:07 +00:00
2018-09-12 17:26:21 +00:00
private string [ ] GetColumnNamesFromField ( ItemFields field )
2016-11-20 09:46:07 +00:00
{
2019-01-26 17:21:41 +00:00
switch ( field )
2016-11-20 09:46:07 +00:00
{
2019-01-26 17:21:41 +00:00
case ItemFields . Settings :
return new [ ] { "IsLocked" , "PreferredMetadataCountryCode" , "PreferredMetadataLanguage" , "LockedFields" } ;
case ItemFields . ServiceName :
return new [ ] { "ExternalServiceId" } ;
case ItemFields . SortName :
return new [ ] { "ForcedSortName" } ;
case ItemFields . Taglines :
return new [ ] { "Tagline" } ;
case ItemFields . Tags :
return new [ ] { "Tags" } ;
case ItemFields . IsHD :
return Array . Empty < string > ( ) ;
default :
return new [ ] { field . ToString ( ) } ;
2018-09-12 17:26:21 +00:00
}
2016-11-20 09:46:07 +00:00
}
2017-05-22 04:54:02 +00:00
private bool HasField ( InternalItemsQuery query , ItemFields name )
{
switch ( name )
{
2018-09-12 17:26:21 +00:00
case ItemFields . Tags :
return query . DtoOptions . ContainsField ( name ) | | HasProgramAttributes ( query ) ;
2017-05-22 04:54:02 +00:00
case ItemFields . CustomRating :
case ItemFields . ProductionLocations :
case ItemFields . Settings :
case ItemFields . OriginalTitle :
case ItemFields . Taglines :
case ItemFields . SortName :
case ItemFields . Studios :
2018-09-12 17:26:21 +00:00
case ItemFields . ExtraIds :
2017-05-22 04:54:02 +00:00
case ItemFields . DateCreated :
case ItemFields . Overview :
case ItemFields . Genres :
case ItemFields . DateLastMediaAdded :
case ItemFields . PresentationUniqueKey :
case ItemFields . InheritedParentalRatingValue :
case ItemFields . ExternalSeriesId :
case ItemFields . SeriesPresentationUniqueKey :
case ItemFields . DateLastRefreshed :
case ItemFields . DateLastSaved :
2018-09-12 17:26:21 +00:00
return query . DtoOptions . ContainsField ( name ) ;
2017-05-22 04:54:02 +00:00
case ItemFields . ServiceName :
2018-09-12 17:26:21 +00:00
return HasServiceName ( query ) ;
2017-05-22 04:54:02 +00:00
default :
return true ;
}
}
2019-02-08 21:59:28 +00:00
private static readonly HashSet < string > _programExcludeParentTypes = new HashSet < string > ( StringComparer . OrdinalIgnoreCase )
2017-05-22 04:54:02 +00:00
{
2019-02-08 21:59:28 +00:00
"Series" ,
"Season" ,
"MusicAlbum" ,
"MusicArtist" ,
"PhotoAlbum"
} ;
2017-05-23 16:43:24 +00:00
2019-02-08 21:59:28 +00:00
private static readonly HashSet < string > _programTypes = new HashSet < string > ( StringComparer . OrdinalIgnoreCase )
{
"Program" ,
"TvChannel" ,
"LiveTvProgram" ,
"LiveTvTvChannel"
} ;
private bool HasProgramAttributes ( InternalItemsQuery query )
{
if ( _programExcludeParentTypes . Contains ( query . ParentType ) )
2017-05-23 16:43:24 +00:00
{
return false ;
}
2017-05-22 04:54:02 +00:00
if ( query . IncludeItemTypes . Length = = 0 )
{
return true ;
}
2019-02-08 21:59:28 +00:00
return query . IncludeItemTypes . Any ( x = > _programTypes . Contains ( x ) ) ;
2017-05-22 04:54:02 +00:00
}
2019-02-08 21:59:28 +00:00
private static readonly HashSet < string > _serviceTypes = new HashSet < string > ( StringComparer . OrdinalIgnoreCase )
2018-09-12 17:26:21 +00:00
{
2019-02-08 21:59:28 +00:00
"TvChannel" ,
"LiveTvTvChannel"
} ;
2018-09-12 17:26:21 +00:00
2019-02-08 21:59:28 +00:00
private bool HasServiceName ( InternalItemsQuery query )
{
if ( _programExcludeParentTypes . Contains ( query . ParentType ) )
2018-09-12 17:26:21 +00:00
{
return false ;
}
if ( query . IncludeItemTypes . Length = = 0 )
{
return true ;
}
2019-02-08 21:59:28 +00:00
return query . IncludeItemTypes . Any ( x = > _serviceTypes . Contains ( x ) ) ;
2018-09-12 17:26:21 +00:00
}
2019-02-08 21:59:28 +00:00
private static readonly HashSet < string > _startDateTypes = new HashSet < string > ( StringComparer . OrdinalIgnoreCase )
2017-05-23 16:43:24 +00:00
{
2019-02-08 21:59:28 +00:00
"Program" ,
"LiveTvProgram"
} ;
2017-05-23 16:43:24 +00:00
2019-02-08 21:59:28 +00:00
private bool HasStartDate ( InternalItemsQuery query )
{
if ( _programExcludeParentTypes . Contains ( query . ParentType ) )
2017-05-23 16:43:24 +00:00
{
return false ;
}
if ( query . IncludeItemTypes . Length = = 0 )
{
return true ;
}
2019-02-08 21:59:28 +00:00
return query . IncludeItemTypes . Any ( x = > _startDateTypes . Contains ( x ) ) ;
2017-05-23 16:43:24 +00:00
}
private bool HasEpisodeAttributes ( InternalItemsQuery query )
{
if ( query . IncludeItemTypes . Length = = 0 )
{
return true ;
}
2019-01-26 18:46:30 +00:00
return query . IncludeItemTypes . Contains ( "Episode" , StringComparer . OrdinalIgnoreCase ) ;
2017-05-23 16:43:24 +00:00
}
private bool HasTrailerTypes ( InternalItemsQuery query )
{
if ( query . IncludeItemTypes . Length = = 0 )
{
return true ;
}
2019-01-26 17:21:41 +00:00
return query . IncludeItemTypes . Contains ( "Trailer" , StringComparer . OrdinalIgnoreCase ) ;
2017-05-23 16:43:24 +00:00
}
2019-02-08 21:59:28 +00:00
private static readonly HashSet < string > _artistExcludeParentTypes = new HashSet < string > ( StringComparer . OrdinalIgnoreCase )
2017-05-23 16:43:24 +00:00
{
2019-02-08 21:59:28 +00:00
"Series" ,
"Season" ,
"PhotoAlbum"
} ;
2017-05-23 16:43:24 +00:00
2019-02-08 21:59:28 +00:00
private static readonly HashSet < string > _artistsTypes = new HashSet < string > ( StringComparer . OrdinalIgnoreCase )
{
"Audio" ,
"MusicAlbum" ,
"MusicVideo" ,
"AudioBook" ,
"AudioPodcast"
} ;
private bool HasArtistFields ( InternalItemsQuery query )
{
if ( _artistExcludeParentTypes . Contains ( query . ParentType ) )
2017-05-23 16:43:24 +00:00
{
return false ;
}
if ( query . IncludeItemTypes . Length = = 0 )
{
return true ;
}
2019-02-08 21:59:28 +00:00
return query . IncludeItemTypes . Any ( x = > _artistsTypes . Contains ( x ) ) ;
2017-05-23 16:43:24 +00:00
}
2019-02-08 21:59:28 +00:00
private static readonly HashSet < string > _seriesTypes = new HashSet < string > ( StringComparer . OrdinalIgnoreCase )
{
2019-03-02 03:28:25 +00:00
"Book" ,
2019-02-08 21:59:28 +00:00
"AudioBook" ,
2019-03-02 03:28:25 +00:00
"Episode" ,
"Season"
2019-02-08 21:59:28 +00:00
} ;
2017-05-23 16:56:01 +00:00
private bool HasSeriesFields ( InternalItemsQuery query )
{
2019-01-26 17:21:41 +00:00
if ( string . Equals ( query . ParentType , "PhotoAlbum" , StringComparison . OrdinalIgnoreCase ) )
2017-05-23 16:56:01 +00:00
{
return false ;
}
if ( query . IncludeItemTypes . Length = = 0 )
{
return true ;
}
2019-02-08 21:59:28 +00:00
return query . IncludeItemTypes . Any ( x = > _seriesTypes . Contains ( x ) ) ;
2017-05-23 16:56:01 +00:00
}
2019-02-08 23:48:09 +00:00
private List < string > GetFinalColumnsToSelect ( InternalItemsQuery query , IEnumerable < string > startColumns )
2016-11-20 09:46:07 +00:00
{
var list = startColumns . ToList ( ) ;
2019-02-09 00:09:22 +00:00
foreach ( var field in _allFields )
2016-11-20 09:46:07 +00:00
{
2017-05-22 04:54:02 +00:00
if ( ! HasField ( query , field ) )
2016-11-20 09:46:07 +00:00
{
2019-02-08 21:59:28 +00:00
foreach ( var fieldToRemove in GetColumnNamesFromField ( field ) )
2016-11-20 09:46:07 +00:00
{
list . Remove ( fieldToRemove ) ;
}
}
}
2017-05-22 04:54:02 +00:00
if ( ! HasProgramAttributes ( query ) )
{
list . Remove ( "IsMovie" ) ;
list . Remove ( "IsSeries" ) ;
list . Remove ( "EpisodeTitle" ) ;
list . Remove ( "IsRepeat" ) ;
2017-09-08 16:13:58 +00:00
list . Remove ( "ShowId" ) ;
2017-05-22 04:54:02 +00:00
}
2017-05-23 16:43:24 +00:00
if ( ! HasEpisodeAttributes ( query ) )
{
list . Remove ( "SeasonName" ) ;
list . Remove ( "SeasonId" ) ;
}
if ( ! HasStartDate ( query ) )
{
list . Remove ( "StartDate" ) ;
}
if ( ! HasTrailerTypes ( query ) )
{
list . Remove ( "TrailerTypes" ) ;
}
if ( ! HasArtistFields ( query ) )
{
list . Remove ( "AlbumArtists" ) ;
list . Remove ( "Artists" ) ;
}
2017-05-23 16:56:01 +00:00
if ( ! HasSeriesFields ( query ) )
{
list . Remove ( "SeriesId" ) ;
}
2017-05-23 16:43:24 +00:00
if ( ! HasEpisodeAttributes ( query ) )
{
list . Remove ( "SeasonName" ) ;
list . Remove ( "SeasonId" ) ;
}
2016-11-20 09:46:07 +00:00
if ( ! query . DtoOptions . EnableImages )
{
list . Remove ( "Images" ) ;
}
if ( EnableJoinUserData ( query ) )
{
2018-09-12 17:26:21 +00:00
list . Add ( "UserDatas.UserId" ) ;
list . Add ( "UserDatas.lastPlayedDate" ) ;
list . Add ( "UserDatas.playbackPositionTicks" ) ;
list . Add ( "UserDatas.playcount" ) ;
list . Add ( "UserDatas.isFavorite" ) ;
list . Add ( "UserDatas.played" ) ;
list . Add ( "UserDatas.rating" ) ;
2016-11-20 09:46:07 +00:00
}
if ( query . SimilarTo ! = null )
{
var item = query . SimilarTo ;
var builder = new StringBuilder ( ) ;
2020-07-22 12:34:51 +00:00
builder . Append ( '(' ) ;
2016-11-20 09:46:07 +00:00
2018-09-12 17:26:21 +00:00
if ( string . IsNullOrEmpty ( item . OfficialRating ) )
{
builder . Append ( "((OfficialRating is null) * 10)" ) ;
}
else
{
builder . Append ( "((OfficialRating=@ItemOfficialRating) * 10)" ) ;
}
2016-11-20 09:46:07 +00:00
2018-09-12 17:26:21 +00:00
if ( item . ProductionYear . HasValue )
{
builder . Append ( "+(Select Case When Abs(COALESCE(ProductionYear, 0) - @ItemProductionYear) < 10 Then 10 Else 0 End )" ) ;
builder . Append ( "+(Select Case When Abs(COALESCE(ProductionYear, 0) - @ItemProductionYear) < 5 Then 5 Else 0 End )" ) ;
}
2016-11-20 09:46:07 +00:00
2016-12-12 20:46:45 +00:00
//// genres, tags
2018-09-12 17:26:21 +00:00
builder . Append ( "+ ((Select count(CleanValue) from ItemValues where ItemId=Guid and CleanValue in (select CleanValue from itemvalues where ItemId=@SimilarItemId)) * 10)" ) ;
2016-11-20 09:46:07 +00:00
builder . Append ( ") as SimilarityScore" ) ;
list . Add ( builder . ToString ( ) ) ;
2019-02-08 21:59:28 +00:00
var oldLen = query . ExcludeItemIds . Length ;
var newLen = oldLen + item . ExtraIds . Length + 1 ;
var excludeIds = new Guid [ newLen ] ;
query . ExcludeItemIds . CopyTo ( excludeIds , 0 ) ;
excludeIds [ oldLen ] = item . Id ;
item . ExtraIds . CopyTo ( excludeIds , oldLen + 1 ) ;
2017-01-21 20:27:07 +00:00
2019-02-08 21:59:28 +00:00
query . ExcludeItemIds = excludeIds ;
2016-11-20 09:46:07 +00:00
query . ExcludeProviderIds = item . ProviderIds ;
}
2018-09-12 17:26:21 +00:00
if ( ! string . IsNullOrEmpty ( query . SearchTerm ) )
{
var builder = new StringBuilder ( ) ;
2020-07-22 12:34:51 +00:00
builder . Append ( '(' ) ;
2018-09-12 17:26:21 +00:00
builder . Append ( "((CleanName like @SearchTermStartsWith or (OriginalTitle not null and OriginalTitle like @SearchTermStartsWith)) * 10)" ) ;
if ( query . SearchTerm . Length > 1 )
{
builder . Append ( "+ ((CleanName like @SearchTermContains or (OriginalTitle not null and OriginalTitle like @SearchTermContains)) * 10)" ) ;
}
builder . Append ( ") as SearchScore" ) ;
list . Add ( builder . ToString ( ) ) ;
}
2019-02-08 23:48:09 +00:00
return list ;
2016-11-20 09:46:07 +00:00
}
2018-09-12 17:26:21 +00:00
private void BindSearchParams ( InternalItemsQuery query , IStatement statement )
{
var searchTerm = query . SearchTerm ;
if ( string . IsNullOrEmpty ( searchTerm ) )
{
return ;
}
searchTerm = FixUnicodeChars ( searchTerm ) ;
searchTerm = GetCleanValue ( searchTerm ) ;
var commandText = statement . SQL ;
if ( commandText . IndexOf ( "@SearchTermStartsWith" , StringComparison . OrdinalIgnoreCase ) ! = - 1 )
{
statement . TryBind ( "@SearchTermStartsWith" , searchTerm + "%" ) ;
}
2020-06-15 21:43:52 +00:00
2018-09-12 17:26:21 +00:00
if ( commandText . IndexOf ( "@SearchTermContains" , StringComparison . OrdinalIgnoreCase ) ! = - 1 )
{
statement . TryBind ( "@SearchTermContains" , "%" + searchTerm + "%" ) ;
}
}
2016-11-20 09:46:07 +00:00
private void BindSimilarParams ( InternalItemsQuery query , IStatement statement )
{
var item = query . SimilarTo ;
if ( item = = null )
{
return ;
}
2018-09-12 17:26:21 +00:00
var commandText = statement . SQL ;
if ( commandText . IndexOf ( "@ItemOfficialRating" , StringComparison . OrdinalIgnoreCase ) ! = - 1 )
{
statement . TryBind ( "@ItemOfficialRating" , item . OfficialRating ) ;
}
if ( commandText . IndexOf ( "@ItemProductionYear" , StringComparison . OrdinalIgnoreCase ) ! = - 1 )
{
statement . TryBind ( "@ItemProductionYear" , item . ProductionYear ? ? 0 ) ;
}
if ( commandText . IndexOf ( "@SimilarItemId" , StringComparison . OrdinalIgnoreCase ) ! = - 1 )
{
statement . TryBind ( "@SimilarItemId" , item . Id ) ;
}
2016-11-20 09:46:07 +00:00
}
private string GetJoinUserDataText ( InternalItemsQuery query )
{
if ( ! EnableJoinUserData ( query ) )
{
return string . Empty ;
}
2018-09-12 17:26:21 +00:00
return " left join UserDatas on UserDataKey=UserDatas.Key And (UserId=@UserId)" ;
2016-11-20 09:46:07 +00:00
}
2016-11-19 18:09:15 +00:00
2016-11-20 09:46:07 +00:00
private string GetGroupBy ( InternalItemsQuery query )
{
var groups = new List < string > ( ) ;
if ( EnableGroupByPresentationUniqueKey ( query ) )
{
groups . Add ( "PresentationUniqueKey" ) ;
}
2017-08-01 16:45:57 +00:00
if ( query . GroupBySeriesPresentationUniqueKey )
{
groups . Add ( "SeriesPresentationUniqueKey" ) ;
}
2016-11-20 09:46:07 +00:00
if ( groups . Count > 0 )
{
2019-01-26 17:21:41 +00:00
return " Group by " + string . Join ( "," , groups ) ;
2016-11-20 09:46:07 +00:00
}
return string . Empty ;
}
2016-11-19 18:09:15 +00:00
2016-11-20 09:46:07 +00:00
private string GetFromText ( string alias = "A" )
{
return " from TypedBaseItems " + alias ;
}
2016-11-19 18:09:15 +00:00
2016-12-12 19:40:27 +00:00
public int GetCount ( InternalItemsQuery query )
{
if ( query = = null )
{
2019-01-06 20:50:43 +00:00
throw new ArgumentNullException ( nameof ( query ) ) ;
2016-12-12 19:40:27 +00:00
}
CheckDisposed ( ) ;
var now = DateTime . UtcNow ;
// Hack for right now since we currently don't support filtering out these duplicates within a query
if ( query . Limit . HasValue & & query . EnableGroupByMetadataKey )
{
query . Limit = query . Limit . Value + 4 ;
}
2019-01-26 17:21:41 +00:00
var commandText = "select "
+ string . Join ( "," , GetFinalColumnsToSelect ( query , new [ ] { "count(distinct PresentationUniqueKey)" } ) )
+ GetFromText ( )
+ GetJoinUserDataText ( query ) ;
2016-12-12 19:40:27 +00:00
var whereClauses = GetWhereClauses ( query , null ) ;
2019-01-26 17:21:41 +00:00
if ( whereClauses . Count ! = 0 )
{
commandText + = " where " + string . Join ( " AND " , whereClauses ) ;
}
2016-12-12 19:40:27 +00:00
2019-08-18 20:05:06 +00:00
int count ;
2019-02-26 17:30:13 +00:00
using ( var connection = GetConnection ( true ) )
2016-12-12 19:40:27 +00:00
{
2019-04-02 20:15:18 +00:00
using ( var statement = PrepareStatement ( connection , commandText ) )
2016-12-12 19:40:27 +00:00
{
2019-02-20 13:26:49 +00:00
if ( EnableJoinUserData ( query ) )
2016-12-12 19:40:27 +00:00
{
2019-02-20 13:26:49 +00:00
statement . TryBind ( "@UserId" , query . User . InternalId ) ;
}
2016-12-12 19:40:27 +00:00
2019-02-20 13:26:49 +00:00
BindSimilarParams ( query , statement ) ;
BindSearchParams ( query , statement ) ;
2016-12-12 19:40:27 +00:00
2019-02-20 13:26:49 +00:00
// Running this again will bind the params
GetWhereClauses ( query , statement ) ;
2016-12-13 07:36:30 +00:00
2019-08-18 20:05:06 +00:00
count = statement . ExecuteQuery ( ) . SelectScalarInt ( ) . First ( ) ;
2016-12-12 19:40:27 +00:00
}
}
2019-08-18 20:05:06 +00:00
LogQueryTime ( "GetCount" , commandText , now ) ;
return count ;
2016-12-12 19:40:27 +00:00
}
2016-11-20 09:46:07 +00:00
public List < BaseItem > GetItemList ( InternalItemsQuery query )
{
if ( query = = null )
{
2019-01-06 20:50:43 +00:00
throw new ArgumentNullException ( nameof ( query ) ) ;
2016-11-20 09:46:07 +00:00
}
2016-11-19 18:09:15 +00:00
2016-11-20 09:46:07 +00:00
CheckDisposed ( ) ;
2016-11-19 18:09:15 +00:00
2016-11-20 09:46:07 +00:00
var now = DateTime . UtcNow ;
2016-11-19 18:09:15 +00:00
2016-11-20 09:46:07 +00:00
// Hack for right now since we currently don't support filtering out these duplicates within a query
if ( query . Limit . HasValue & & query . EnableGroupByMetadataKey )
{
query . Limit = query . Limit . Value + 4 ;
}
2016-11-19 18:09:15 +00:00
2019-01-26 17:21:41 +00:00
var commandText = "select "
+ string . Join ( "," , GetFinalColumnsToSelect ( query , _retriveItemColumns ) )
+ GetFromText ( )
+ GetJoinUserDataText ( query ) ;
2016-11-20 09:46:07 +00:00
var whereClauses = GetWhereClauses ( query , null ) ;
2019-01-26 17:21:41 +00:00
if ( whereClauses . Count ! = 0 )
{
commandText + = " where " + string . Join ( " AND " , whereClauses ) ;
}
2016-11-20 09:46:07 +00:00
2019-01-26 17:21:41 +00:00
commandText + = GetGroupBy ( query )
+ GetOrderByText ( query ) ;
2016-11-20 09:46:07 +00:00
if ( query . Limit . HasValue | | query . StartIndex . HasValue )
{
var offset = query . StartIndex ? ? 0 ;
if ( query . Limit . HasValue | | offset > 0 )
{
commandText + = " LIMIT " + ( query . Limit ? ? int . MaxValue ) . ToString ( CultureInfo . InvariantCulture ) ;
}
if ( offset > 0 )
{
commandText + = " OFFSET " + offset . ToString ( CultureInfo . InvariantCulture ) ;
}
}
2019-08-18 20:05:06 +00:00
var items = new List < BaseItem > ( ) ;
2019-02-26 17:30:13 +00:00
using ( var connection = GetConnection ( true ) )
2016-11-20 09:46:07 +00:00
{
2019-04-02 20:15:18 +00:00
using ( var statement = PrepareStatement ( connection , commandText ) )
2019-02-20 13:26:49 +00:00
{
if ( EnableJoinUserData ( query ) )
2017-01-01 22:35:22 +00:00
{
2019-02-20 13:26:49 +00:00
statement . TryBind ( "@UserId" , query . User . InternalId ) ;
}
2016-11-20 09:46:07 +00:00
2019-02-20 13:26:49 +00:00
BindSimilarParams ( query , statement ) ;
BindSearchParams ( query , statement ) ;
2016-11-20 09:46:07 +00:00
2019-02-20 13:26:49 +00:00
// Running this again will bind the params
GetWhereClauses ( query , statement ) ;
2016-11-20 09:46:07 +00:00
2019-02-20 13:26:49 +00:00
var hasEpisodeAttributes = HasEpisodeAttributes ( query ) ;
var hasServiceName = HasServiceName ( query ) ;
var hasProgramAttributes = HasProgramAttributes ( query ) ;
var hasStartDate = HasStartDate ( query ) ;
var hasTrailerTypes = HasTrailerTypes ( query ) ;
var hasArtistFields = HasArtistFields ( query ) ;
var hasSeriesFields = HasSeriesFields ( query ) ;
2017-05-23 16:43:24 +00:00
2019-02-20 13:26:49 +00:00
foreach ( var row in statement . ExecuteQuery ( ) )
{
var item = GetItem ( row , query , hasProgramAttributes , hasEpisodeAttributes , hasServiceName , hasStartDate , hasTrailerTypes , hasArtistFields , hasSeriesFields ) ;
if ( item ! = null )
2017-01-01 22:35:22 +00:00
{
2019-07-01 15:59:01 +00:00
items . Add ( item ) ;
2016-11-20 09:46:07 +00:00
}
2017-01-01 22:35:22 +00:00
}
2019-02-20 13:26:49 +00:00
}
2017-01-01 22:35:22 +00:00
2019-02-20 13:26:49 +00:00
// Hack for right now since we currently don't support filtering out these duplicates within a query
if ( query . EnableGroupByMetadataKey )
{
var limit = query . Limit ? ? int . MaxValue ;
limit - = 4 ;
var newList = new List < BaseItem > ( ) ;
2019-07-01 15:59:01 +00:00
foreach ( var item in items )
2017-01-01 22:35:22 +00:00
{
2019-02-20 13:26:49 +00:00
AddItem ( newList , item ) ;
2016-12-11 05:12:00 +00:00
2019-02-20 13:26:49 +00:00
if ( newList . Count > = limit )
2016-12-13 07:36:30 +00:00
{
2019-02-20 13:26:49 +00:00
break ;
2016-12-13 07:36:30 +00:00
}
2017-01-01 22:35:22 +00:00
}
2016-11-20 09:46:07 +00:00
2019-07-01 15:59:01 +00:00
items = newList ;
2016-12-13 07:36:30 +00:00
}
2019-08-18 20:05:06 +00:00
}
2019-02-20 13:26:49 +00:00
2019-08-18 20:05:06 +00:00
LogQueryTime ( "GetItemList" , commandText , now ) ;
2019-02-20 13:26:49 +00:00
2019-08-18 20:05:06 +00:00
return items ;
2016-11-20 09:46:07 +00:00
}
2018-09-12 17:26:21 +00:00
private string FixUnicodeChars ( string buffer )
{
2020-07-24 14:37:54 +00:00
if ( buffer . IndexOf ( ' \ u2013 ' , StringComparison . Ordinal ) > - 1 )
2020-06-20 08:35:29 +00:00
{
buffer = buffer . Replace ( ' \ u2013 ' , '-' ) ; // en dash
}
2020-07-24 14:37:54 +00:00
if ( buffer . IndexOf ( ' \ u2014 ' , StringComparison . Ordinal ) > - 1 )
2020-06-20 08:35:29 +00:00
{
buffer = buffer . Replace ( ' \ u2014 ' , '-' ) ; // em dash
}
2020-07-24 14:37:54 +00:00
if ( buffer . IndexOf ( ' \ u2015 ' , StringComparison . Ordinal ) > - 1 )
2020-06-20 08:35:29 +00:00
{
buffer = buffer . Replace ( ' \ u2015 ' , '-' ) ; // horizontal bar
}
2020-07-24 14:37:54 +00:00
if ( buffer . IndexOf ( ' \ u2017 ' , StringComparison . Ordinal ) > - 1 )
2020-06-20 08:35:29 +00:00
{
buffer = buffer . Replace ( ' \ u2017 ' , '_' ) ; // double low line
}
2020-07-24 14:37:54 +00:00
if ( buffer . IndexOf ( ' \ u2018 ' , StringComparison . Ordinal ) > - 1 )
2020-06-20 08:35:29 +00:00
{
buffer = buffer . Replace ( ' \ u2018 ' , '\'' ) ; // left single quotation mark
}
2020-07-24 14:37:54 +00:00
if ( buffer . IndexOf ( ' \ u2019 ' , StringComparison . Ordinal ) > - 1 )
2020-06-20 08:35:29 +00:00
{
buffer = buffer . Replace ( ' \ u2019 ' , '\'' ) ; // right single quotation mark
}
2020-07-24 14:37:54 +00:00
if ( buffer . IndexOf ( ' \ u201a ' , StringComparison . Ordinal ) > - 1 )
2020-06-20 08:35:29 +00:00
{
buffer = buffer . Replace ( ' \ u201a ' , ',' ) ; // single low-9 quotation mark
}
2020-07-24 14:37:54 +00:00
if ( buffer . IndexOf ( ' \ u201b ' , StringComparison . Ordinal ) > - 1 )
2020-06-20 08:35:29 +00:00
{
buffer = buffer . Replace ( ' \ u201b ' , '\'' ) ; // single high-reversed-9 quotation mark
}
2020-07-24 14:37:54 +00:00
if ( buffer . IndexOf ( ' \ u201c ' , StringComparison . Ordinal ) > - 1 )
2020-06-20 08:35:29 +00:00
{
buffer = buffer . Replace ( ' \ u201c ' , '\"' ) ; // left double quotation mark
}
2020-07-24 14:37:54 +00:00
if ( buffer . IndexOf ( ' \ u201d ' , StringComparison . Ordinal ) > - 1 )
2020-06-20 08:35:29 +00:00
{
buffer = buffer . Replace ( ' \ u201d ' , '\"' ) ; // right double quotation mark
}
2020-07-24 14:37:54 +00:00
if ( buffer . IndexOf ( ' \ u201e ' , StringComparison . Ordinal ) > - 1 )
2020-06-20 08:35:29 +00:00
{
buffer = buffer . Replace ( ' \ u201e ' , '\"' ) ; // double low-9 quotation mark
}
2020-07-24 14:37:54 +00:00
if ( buffer . IndexOf ( ' \ u2026 ' , StringComparison . Ordinal ) > - 1 )
2020-06-20 08:35:29 +00:00
{
2020-07-24 14:37:54 +00:00
buffer = buffer . Replace ( "\u2026" , "..." , StringComparison . Ordinal ) ; // horizontal ellipsis
2020-06-20 08:35:29 +00:00
}
2020-07-24 14:37:54 +00:00
if ( buffer . IndexOf ( ' \ u2032 ' , StringComparison . Ordinal ) > - 1 )
2020-06-20 08:35:29 +00:00
{
buffer = buffer . Replace ( ' \ u2032 ' , '\'' ) ; // prime
}
2020-07-24 14:37:54 +00:00
if ( buffer . IndexOf ( ' \ u2033 ' , StringComparison . Ordinal ) > - 1 )
2020-06-20 08:35:29 +00:00
{
buffer = buffer . Replace ( ' \ u2033 ' , '\"' ) ; // double prime
}
2020-07-24 14:37:54 +00:00
if ( buffer . IndexOf ( ' \ u0060 ' , StringComparison . Ordinal ) > - 1 )
2020-06-20 08:35:29 +00:00
{
buffer = buffer . Replace ( ' \ u0060 ' , '\'' ) ; // grave accent
}
2020-07-24 14:37:54 +00:00
if ( buffer . IndexOf ( ' \ u00B4 ' , StringComparison . Ordinal ) > - 1 )
2020-06-20 08:35:29 +00:00
{
buffer = buffer . Replace ( ' \ u00B4 ' , '\'' ) ; // acute accent
}
2018-09-12 17:26:21 +00:00
return buffer ;
}
2016-11-20 09:46:07 +00:00
private void AddItem ( List < BaseItem > items , BaseItem newItem )
{
for ( var i = 0 ; i < items . Count ; i + + )
{
var item = items [ i ] ;
2019-02-09 00:25:14 +00:00
foreach ( var providerId in newItem . ProviderIds )
2016-11-20 09:46:07 +00:00
{
2020-06-06 19:17:49 +00:00
if ( providerId . Key = = MetadataProvider . TmdbCollection . ToString ( ) )
2016-11-20 09:46:07 +00:00
{
continue ;
}
2019-02-08 21:59:28 +00:00
2016-11-20 09:46:07 +00:00
if ( item . GetProviderId ( providerId . Key ) = = providerId . Value )
{
if ( newItem . SourceType = = SourceType . Library )
{
items [ i ] = newItem ;
}
2020-06-15 21:43:52 +00:00
2016-11-20 09:46:07 +00:00
return ;
}
}
}
items . Add ( newItem ) ;
}
private void LogQueryTime ( string methodName , string commandText , DateTime startDate )
{
var elapsed = ( DateTime . UtcNow - startDate ) . TotalMilliseconds ;
#if DEBUG
2019-04-24 13:25:22 +00:00
const int SlowThreshold = 100 ;
#else
const int SlowThreshold = 10 ;
2016-11-20 09:46:07 +00:00
#endif
2019-04-24 13:25:22 +00:00
if ( elapsed > = SlowThreshold )
2016-11-20 09:46:07 +00:00
{
2019-10-08 20:17:15 +00:00
Logger . LogDebug (
2019-04-24 13:25:22 +00:00
"{Method} query time (slow): {ElapsedMs}ms. Query: {Query}" ,
2019-01-26 17:21:41 +00:00
methodName ,
elapsed ,
commandText ) ;
2016-11-20 09:46:07 +00:00
}
}
public QueryResult < BaseItem > GetItems ( InternalItemsQuery query )
{
if ( query = = null )
{
2019-01-06 20:50:43 +00:00
throw new ArgumentNullException ( nameof ( query ) ) ;
2016-11-20 09:46:07 +00:00
}
CheckDisposed ( ) ;
if ( ! query . EnableTotalRecordCount | | ( ! query . Limit . HasValue & & ( query . StartIndex ? ? 0 ) = = 0 ) )
{
var returnList = GetItemList ( query ) ;
return new QueryResult < BaseItem >
{
2019-09-02 06:19:29 +00:00
Items = returnList ,
2016-11-20 09:46:07 +00:00
TotalRecordCount = returnList . Count
} ;
}
var now = DateTime . UtcNow ;
// Hack for right now since we currently don't support filtering out these duplicates within a query
if ( query . Limit . HasValue & & query . EnableGroupByMetadataKey )
{
query . Limit = query . Limit . Value + 4 ;
}
2019-01-26 17:21:41 +00:00
var commandText = "select "
+ string . Join ( "," , GetFinalColumnsToSelect ( query , _retriveItemColumns ) )
+ GetFromText ( )
+ GetJoinUserDataText ( query ) ;
2016-11-20 09:46:07 +00:00
var whereClauses = GetWhereClauses ( query , null ) ;
var whereText = whereClauses . Count = = 0 ?
string . Empty :
2019-02-09 00:39:59 +00:00
" where " + string . Join ( " AND " , whereClauses ) ;
2016-11-20 09:46:07 +00:00
2019-01-26 17:21:41 +00:00
commandText + = whereText
+ GetGroupBy ( query )
+ GetOrderByText ( query ) ;
2016-11-20 09:46:07 +00:00
if ( query . Limit . HasValue | | query . StartIndex . HasValue )
{
var offset = query . StartIndex ? ? 0 ;
if ( query . Limit . HasValue | | offset > 0 )
{
commandText + = " LIMIT " + ( query . Limit ? ? int . MaxValue ) . ToString ( CultureInfo . InvariantCulture ) ;
}
if ( offset > 0 )
{
commandText + = " OFFSET " + offset . ToString ( CultureInfo . InvariantCulture ) ;
}
}
2016-11-25 17:36:00 +00:00
var isReturningZeroItems = query . Limit . HasValue & & query . Limit < = 0 ;
var statementTexts = new List < string > ( ) ;
if ( ! isReturningZeroItems )
{
statementTexts . Add ( commandText ) ;
}
2020-06-15 21:43:52 +00:00
2016-11-25 17:36:00 +00:00
if ( query . EnableTotalRecordCount )
{
commandText = string . Empty ;
if ( EnableGroupByPresentationUniqueKey ( query ) )
{
2018-09-12 17:26:21 +00:00
commandText + = " select " + string . Join ( "," , GetFinalColumnsToSelect ( query , new [ ] { "count (distinct PresentationUniqueKey)" } ) ) + GetFromText ( ) ;
2016-11-25 17:36:00 +00:00
}
2017-08-01 16:45:57 +00:00
else if ( query . GroupBySeriesPresentationUniqueKey )
{
2018-09-12 17:26:21 +00:00
commandText + = " select " + string . Join ( "," , GetFinalColumnsToSelect ( query , new [ ] { "count (distinct SeriesPresentationUniqueKey)" } ) ) + GetFromText ( ) ;
2017-08-01 16:45:57 +00:00
}
2016-11-25 17:36:00 +00:00
else
{
2018-09-12 17:26:21 +00:00
commandText + = " select " + string . Join ( "," , GetFinalColumnsToSelect ( query , new [ ] { "count (guid)" } ) ) + GetFromText ( ) ;
2016-11-25 17:36:00 +00:00
}
2019-01-26 17:21:41 +00:00
commandText + = GetJoinUserDataText ( query )
+ whereText ;
2016-11-25 17:36:00 +00:00
statementTexts . Add ( commandText ) ;
}
2019-08-18 20:05:06 +00:00
var list = new List < BaseItem > ( ) ;
var result = new QueryResult < BaseItem > ( ) ;
2019-02-26 17:30:13 +00:00
using ( var connection = GetConnection ( true ) )
2016-11-20 09:46:07 +00:00
{
2019-08-18 20:05:06 +00:00
connection . RunInTransaction ( db = >
2016-11-20 09:46:07 +00:00
{
2019-04-03 11:46:07 +00:00
var statements = PrepareAll ( db , statementTexts ) . ToList ( ) ;
2016-12-12 05:49:19 +00:00
2019-02-20 13:26:49 +00:00
if ( ! isReturningZeroItems )
{
using ( var statement = statements [ 0 ] )
2016-11-20 09:46:07 +00:00
{
2019-02-20 13:26:49 +00:00
if ( EnableJoinUserData ( query ) )
2016-11-21 03:52:58 +00:00
{
2019-02-20 13:26:49 +00:00
statement . TryBind ( "@UserId" , query . User . InternalId ) ;
}
2016-11-20 09:46:07 +00:00
2019-02-20 13:26:49 +00:00
BindSimilarParams ( query , statement ) ;
BindSearchParams ( query , statement ) ;
2016-11-20 09:46:07 +00:00
2019-10-20 14:08:40 +00:00
// Running this again will bind the params
GetWhereClauses ( query , statement ) ;
2016-11-20 09:46:07 +00:00
2019-02-20 13:26:49 +00:00
var hasEpisodeAttributes = HasEpisodeAttributes ( query ) ;
var hasServiceName = HasServiceName ( query ) ;
var hasProgramAttributes = HasProgramAttributes ( query ) ;
var hasStartDate = HasStartDate ( query ) ;
var hasTrailerTypes = HasTrailerTypes ( query ) ;
var hasArtistFields = HasArtistFields ( query ) ;
var hasSeriesFields = HasSeriesFields ( query ) ;
2017-05-23 16:43:24 +00:00
2019-02-20 13:26:49 +00:00
foreach ( var row in statement . ExecuteQuery ( ) )
{
var item = GetItem ( row , query , hasProgramAttributes , hasEpisodeAttributes , hasServiceName , hasStartDate , hasTrailerTypes , hasArtistFields , hasSeriesFields ) ;
if ( item ! = null )
2016-11-21 03:52:58 +00:00
{
2019-02-20 13:26:49 +00:00
list . Add ( item ) ;
2016-11-21 03:52:58 +00:00
}
2016-11-20 09:46:07 +00:00
}
2016-12-12 08:53:25 +00:00
}
2019-02-20 13:26:49 +00:00
}
2016-11-20 09:46:07 +00:00
2019-02-20 13:26:49 +00:00
if ( query . EnableTotalRecordCount )
{
using ( var statement = statements [ statements . Count - 1 ] )
2016-12-12 08:53:25 +00:00
{
2019-02-20 13:26:49 +00:00
if ( EnableJoinUserData ( query ) )
2016-11-25 17:36:00 +00:00
{
2019-02-20 13:26:49 +00:00
statement . TryBind ( "@UserId" , query . User . InternalId ) ;
}
2016-11-20 09:46:07 +00:00
2019-02-20 13:26:49 +00:00
BindSimilarParams ( query , statement ) ;
BindSearchParams ( query , statement ) ;
2016-11-20 09:46:07 +00:00
2020-03-24 15:12:06 +00:00
// Running this again will bind the params
GetWhereClauses ( query , statement ) ;
2016-11-20 09:46:07 +00:00
2019-02-20 13:26:49 +00:00
result . TotalRecordCount = statement . ExecuteQuery ( ) . SelectScalarInt ( ) . First ( ) ;
2016-11-25 17:36:00 +00:00
}
2019-02-20 13:26:49 +00:00
}
} , ReadTransactionMode ) ;
2016-11-20 09:46:07 +00:00
}
2019-08-18 20:05:06 +00:00
LogQueryTime ( "GetItems" , commandText , now ) ;
2019-09-02 06:19:29 +00:00
result . Items = list ;
2019-08-18 20:05:06 +00:00
return result ;
2016-11-20 09:46:07 +00:00
}
private string GetOrderByText ( InternalItemsQuery query )
{
2019-10-20 14:08:40 +00:00
var orderBy = query . OrderBy ;
2020-02-26 07:54:29 +00:00
bool hasSimilar = query . SimilarTo ! = null ;
bool hasSearch = ! string . IsNullOrEmpty ( query . SearchTerm ) ;
if ( hasSimilar | | hasSearch )
2016-11-20 09:46:07 +00:00
{
2020-02-26 07:54:29 +00:00
List < ( string , SortOrder ) > prepend = new List < ( string , SortOrder ) > ( 4 ) ;
if ( hasSearch )
2019-02-09 00:39:59 +00:00
{
2020-02-26 07:54:29 +00:00
prepend . Add ( ( "SearchScore" , SortOrder . Descending ) ) ;
prepend . Add ( ( ItemSortBy . SortName , SortOrder . Ascending ) ) ;
2019-02-09 00:39:59 +00:00
}
2020-03-24 15:12:06 +00:00
2020-02-26 07:54:29 +00:00
if ( hasSimilar )
2019-02-09 00:39:59 +00:00
{
2020-02-26 07:54:29 +00:00
prepend . Add ( ( "SimilarityScore" , SortOrder . Descending ) ) ;
prepend . Add ( ( ItemSortBy . Random , SortOrder . Ascending ) ) ;
}
2016-11-20 09:46:07 +00:00
2020-02-26 07:54:29 +00:00
var arr = new ( string , SortOrder ) [ prepend . Count + orderBy . Count ] ;
prepend . CopyTo ( arr , 0 ) ;
orderBy . CopyTo ( arr , prepend . Count ) ;
orderBy = query . OrderBy = arr ;
}
else if ( orderBy . Count = = 0 )
2016-11-20 09:46:07 +00:00
{
return string . Empty ;
}
return " ORDER BY " + string . Join ( "," , orderBy . Select ( i = >
{
var columnMap = MapOrderByField ( i . Item1 , query ) ;
2019-10-20 14:08:40 +00:00
var sortOrder = i . Item2 = = SortOrder . Ascending ? "ASC" : "DESC" ;
2016-11-20 09:46:07 +00:00
return columnMap . Item1 + " " + sortOrder ;
2019-01-26 17:21:41 +00:00
} ) ) ;
2016-11-20 09:46:07 +00:00
}
2019-02-09 00:39:59 +00:00
private ( string , bool ) MapOrderByField ( string name , InternalItemsQuery query )
2016-11-20 09:46:07 +00:00
{
if ( string . Equals ( name , ItemSortBy . AirTime , StringComparison . OrdinalIgnoreCase ) )
{
// TODO
2019-01-26 17:21:41 +00:00
return ( "SortName" , false ) ;
2016-11-20 09:46:07 +00:00
}
2019-01-26 17:21:41 +00:00
else if ( string . Equals ( name , ItemSortBy . Runtime , StringComparison . OrdinalIgnoreCase ) )
2016-11-20 09:46:07 +00:00
{
2019-01-26 17:21:41 +00:00
return ( "RuntimeTicks" , false ) ;
2016-11-20 09:46:07 +00:00
}
2019-01-26 17:21:41 +00:00
else if ( string . Equals ( name , ItemSortBy . Random , StringComparison . OrdinalIgnoreCase ) )
2016-11-20 09:46:07 +00:00
{
2019-01-26 17:21:41 +00:00
return ( "RANDOM()" , false ) ;
2016-11-20 09:46:07 +00:00
}
2019-01-26 17:21:41 +00:00
else if ( string . Equals ( name , ItemSortBy . DatePlayed , StringComparison . OrdinalIgnoreCase ) )
2016-11-20 09:46:07 +00:00
{
2017-08-01 16:45:57 +00:00
if ( query . GroupBySeriesPresentationUniqueKey )
{
2019-01-26 17:21:41 +00:00
return ( "MAX(LastPlayedDate)" , false ) ;
2017-08-01 16:45:57 +00:00
}
2019-01-26 17:21:41 +00:00
return ( "LastPlayedDate" , false ) ;
2016-11-20 09:46:07 +00:00
}
2019-01-26 17:21:41 +00:00
else if ( string . Equals ( name , ItemSortBy . PlayCount , StringComparison . OrdinalIgnoreCase ) )
2016-11-20 09:46:07 +00:00
{
2019-01-26 17:21:41 +00:00
return ( "PlayCount" , false ) ;
2016-11-20 09:46:07 +00:00
}
2019-01-26 17:21:41 +00:00
else if ( string . Equals ( name , ItemSortBy . IsFavoriteOrLiked , StringComparison . OrdinalIgnoreCase ) )
2016-11-20 09:46:07 +00:00
{
2019-01-26 17:21:41 +00:00
return ( "(Select Case When IsFavorite is null Then 0 Else IsFavorite End )" , true ) ;
2016-11-20 09:46:07 +00:00
}
2019-01-26 17:21:41 +00:00
else if ( string . Equals ( name , ItemSortBy . IsFolder , StringComparison . OrdinalIgnoreCase ) )
2016-11-20 09:46:07 +00:00
{
2019-01-26 17:21:41 +00:00
return ( "IsFolder" , true ) ;
2016-11-20 09:46:07 +00:00
}
2019-01-26 17:21:41 +00:00
else if ( string . Equals ( name , ItemSortBy . IsPlayed , StringComparison . OrdinalIgnoreCase ) )
2016-11-20 09:46:07 +00:00
{
2019-01-26 17:21:41 +00:00
return ( "played" , true ) ;
2016-11-20 09:46:07 +00:00
}
2019-01-26 17:21:41 +00:00
else if ( string . Equals ( name , ItemSortBy . IsUnplayed , StringComparison . OrdinalIgnoreCase ) )
2016-11-20 09:46:07 +00:00
{
2019-01-26 17:21:41 +00:00
return ( "played" , false ) ;
2016-11-20 09:46:07 +00:00
}
2019-01-26 17:21:41 +00:00
else if ( string . Equals ( name , ItemSortBy . DateLastContentAdded , StringComparison . OrdinalIgnoreCase ) )
2016-11-20 09:46:07 +00:00
{
2019-01-26 17:21:41 +00:00
return ( "DateLastMediaAdded" , false ) ;
2016-11-20 09:46:07 +00:00
}
2019-01-26 17:21:41 +00:00
else if ( string . Equals ( name , ItemSortBy . Artist , StringComparison . OrdinalIgnoreCase ) )
2016-11-20 09:46:07 +00:00
{
2019-01-26 17:21:41 +00:00
return ( "(select CleanValue from itemvalues where ItemId=Guid and Type=0 LIMIT 1)" , false ) ;
2016-11-20 09:46:07 +00:00
}
2019-01-26 17:21:41 +00:00
else if ( string . Equals ( name , ItemSortBy . AlbumArtist , StringComparison . OrdinalIgnoreCase ) )
2016-11-20 09:46:07 +00:00
{
2019-01-26 17:21:41 +00:00
return ( "(select CleanValue from itemvalues where ItemId=Guid and Type=1 LIMIT 1)" , false ) ;
2016-11-20 09:46:07 +00:00
}
2019-01-26 17:21:41 +00:00
else if ( string . Equals ( name , ItemSortBy . OfficialRating , StringComparison . OrdinalIgnoreCase ) )
2016-11-20 09:46:07 +00:00
{
2019-01-26 17:21:41 +00:00
return ( "InheritedParentalRatingValue" , false ) ;
2016-11-20 09:46:07 +00:00
}
2019-01-26 17:21:41 +00:00
else if ( string . Equals ( name , ItemSortBy . Studio , StringComparison . OrdinalIgnoreCase ) )
2016-11-20 09:46:07 +00:00
{
2019-01-26 17:21:41 +00:00
return ( "(select CleanValue from itemvalues where ItemId=Guid and Type=3 LIMIT 1)" , false ) ;
2016-11-20 09:46:07 +00:00
}
2019-01-26 17:21:41 +00:00
else if ( string . Equals ( name , ItemSortBy . SeriesDatePlayed , StringComparison . OrdinalIgnoreCase ) )
2016-11-20 09:46:07 +00:00
{
2019-01-26 17:21:41 +00:00
return ( "(Select MAX(LastPlayedDate) from TypedBaseItems B" + GetJoinUserDataText ( query ) + " where Played=1 and B.SeriesPresentationUniqueKey=A.PresentationUniqueKey)" , false ) ;
2016-11-20 09:46:07 +00:00
}
2019-01-26 17:21:41 +00:00
else if ( string . Equals ( name , ItemSortBy . SeriesSortName , StringComparison . OrdinalIgnoreCase ) )
2017-05-05 17:55:38 +00:00
{
2019-01-26 17:21:41 +00:00
return ( "SeriesName" , false ) ;
2017-05-05 17:55:38 +00:00
}
2016-11-20 09:46:07 +00:00
2019-01-26 17:21:41 +00:00
return ( name , false ) ;
2016-11-20 09:46:07 +00:00
}
public List < Guid > GetItemIdsList ( InternalItemsQuery query )
{
if ( query = = null )
{
2019-01-06 20:50:43 +00:00
throw new ArgumentNullException ( nameof ( query ) ) ;
2016-11-20 09:46:07 +00:00
}
CheckDisposed ( ) ;
var now = DateTime . UtcNow ;
2019-01-26 17:21:41 +00:00
var commandText = "select "
+ string . Join ( "," , GetFinalColumnsToSelect ( query , new [ ] { "guid" } ) )
+ GetFromText ( )
+ GetJoinUserDataText ( query ) ;
2016-11-20 09:46:07 +00:00
var whereClauses = GetWhereClauses ( query , null ) ;
2019-01-26 17:21:41 +00:00
if ( whereClauses . Count ! = 0 )
{
commandText + = " where " + string . Join ( " AND " , whereClauses ) ;
}
2016-11-20 09:46:07 +00:00
2019-01-26 17:21:41 +00:00
commandText + = GetGroupBy ( query )
+ GetOrderByText ( query ) ;
2016-11-20 09:46:07 +00:00
if ( query . Limit . HasValue | | query . StartIndex . HasValue )
{
var offset = query . StartIndex ? ? 0 ;
if ( query . Limit . HasValue | | offset > 0 )
{
commandText + = " LIMIT " + ( query . Limit ? ? int . MaxValue ) . ToString ( CultureInfo . InvariantCulture ) ;
}
if ( offset > 0 )
{
commandText + = " OFFSET " + offset . ToString ( CultureInfo . InvariantCulture ) ;
}
}
2019-08-18 20:05:06 +00:00
var list = new List < Guid > ( ) ;
2019-02-26 17:30:13 +00:00
using ( var connection = GetConnection ( true ) )
2016-11-20 09:46:07 +00:00
{
2019-04-02 20:15:18 +00:00
using ( var statement = PrepareStatement ( connection , commandText ) )
2019-02-20 13:26:49 +00:00
{
if ( EnableJoinUserData ( query ) )
2016-12-13 15:44:34 +00:00
{
2019-02-20 13:26:49 +00:00
statement . TryBind ( "@UserId" , query . User . InternalId ) ;
}
2016-11-20 09:46:07 +00:00
2019-02-20 13:26:49 +00:00
BindSimilarParams ( query , statement ) ;
BindSearchParams ( query , statement ) ;
2016-11-20 09:46:07 +00:00
2019-02-20 13:26:49 +00:00
// Running this again will bind the params
GetWhereClauses ( query , statement ) ;
2016-11-20 09:46:07 +00:00
2019-02-20 13:26:49 +00:00
foreach ( var row in statement . ExecuteQuery ( ) )
{
list . Add ( row [ 0 ] . ReadGuidFromBlob ( ) ) ;
2016-12-13 15:44:34 +00:00
}
2019-02-20 13:26:49 +00:00
}
2016-11-20 09:46:07 +00:00
}
2019-08-18 20:05:06 +00:00
LogQueryTime ( "GetItemList" , commandText , now ) ;
return list ;
2016-11-20 09:46:07 +00:00
}
public List < Tuple < Guid , string > > GetItemIdsWithPath ( InternalItemsQuery query )
{
if ( query = = null )
{
2019-01-06 20:50:43 +00:00
throw new ArgumentNullException ( nameof ( query ) ) ;
2016-11-20 09:46:07 +00:00
}
CheckDisposed ( ) ;
var now = DateTime . UtcNow ;
var commandText = "select " + string . Join ( "," , GetFinalColumnsToSelect ( query , new [ ] { "guid" , "path" } ) ) + GetFromText ( ) ;
var whereClauses = GetWhereClauses ( query , null ) ;
2019-01-26 17:21:41 +00:00
if ( whereClauses . Count ! = 0 )
{
commandText + = " where " + string . Join ( " AND " , whereClauses ) ;
}
2016-11-20 09:46:07 +00:00
2019-01-26 17:21:41 +00:00
commandText + = GetGroupBy ( query )
+ GetOrderByText ( query ) ;
2016-11-20 09:46:07 +00:00
if ( query . Limit . HasValue | | query . StartIndex . HasValue )
{
var offset = query . StartIndex ? ? 0 ;
if ( query . Limit . HasValue | | offset > 0 )
{
commandText + = " LIMIT " + ( query . Limit ? ? int . MaxValue ) . ToString ( CultureInfo . InvariantCulture ) ;
}
if ( offset > 0 )
{
commandText + = " OFFSET " + offset . ToString ( CultureInfo . InvariantCulture ) ;
}
}
2019-02-20 13:26:49 +00:00
var list = new List < Tuple < Guid , string > > ( ) ;
2019-02-26 17:30:13 +00:00
using ( var connection = GetConnection ( true ) )
2016-11-20 09:46:07 +00:00
{
2019-04-02 20:15:18 +00:00
using ( var statement = PrepareStatement ( connection , commandText ) )
2016-11-20 09:46:07 +00:00
{
2019-02-20 13:26:49 +00:00
if ( EnableJoinUserData ( query ) )
2016-11-20 09:46:07 +00:00
{
2019-02-20 13:26:49 +00:00
statement . TryBind ( "@UserId" , query . User . InternalId ) ;
}
2016-11-20 09:46:07 +00:00
2019-02-20 13:26:49 +00:00
// Running this again will bind the params
GetWhereClauses ( query , statement ) ;
2016-11-20 09:46:07 +00:00
2019-02-20 13:26:49 +00:00
foreach ( var row in statement . ExecuteQuery ( ) )
{
var id = row . GetGuid ( 0 ) ;
string path = null ;
2016-11-21 03:52:58 +00:00
2019-02-20 13:26:49 +00:00
if ( ! row . IsDBNull ( 1 ) )
{
path = row . GetString ( 1 ) ;
2016-11-20 09:46:07 +00:00
}
2019-08-18 20:05:06 +00:00
2019-02-20 13:26:49 +00:00
list . Add ( new Tuple < Guid , string > ( id , path ) ) ;
2016-11-20 09:46:07 +00:00
}
2016-12-11 05:12:00 +00:00
}
2019-02-20 13:26:49 +00:00
}
2016-11-20 09:46:07 +00:00
2019-02-20 13:26:49 +00:00
LogQueryTime ( "GetItemIdsWithPath" , commandText , now ) ;
2016-11-20 09:46:07 +00:00
2019-02-20 13:26:49 +00:00
return list ;
2016-11-20 09:46:07 +00:00
}
public QueryResult < Guid > GetItemIds ( InternalItemsQuery query )
{
if ( query = = null )
{
2019-01-06 20:50:43 +00:00
throw new ArgumentNullException ( nameof ( query ) ) ;
2016-11-20 09:46:07 +00:00
}
CheckDisposed ( ) ;
if ( ! query . EnableTotalRecordCount | | ( ! query . Limit . HasValue & & ( query . StartIndex ? ? 0 ) = = 0 ) )
{
var returnList = GetItemIdsList ( query ) ;
return new QueryResult < Guid >
{
2019-09-02 06:19:29 +00:00
Items = returnList ,
2016-11-20 09:46:07 +00:00
TotalRecordCount = returnList . Count
} ;
}
var now = DateTime . UtcNow ;
2019-01-26 17:21:41 +00:00
var commandText = "select "
+ string . Join ( "," , GetFinalColumnsToSelect ( query , new [ ] { "guid" } ) )
+ GetFromText ( )
+ GetJoinUserDataText ( query ) ;
2016-11-20 09:46:07 +00:00
var whereClauses = GetWhereClauses ( query , null ) ;
var whereText = whereClauses . Count = = 0 ?
string . Empty :
2019-02-09 00:39:59 +00:00
" where " + string . Join ( " AND " , whereClauses ) ;
2016-11-20 09:46:07 +00:00
2019-01-26 17:21:41 +00:00
commandText + = whereText
+ GetGroupBy ( query )
+ GetOrderByText ( query ) ;
2016-11-20 09:46:07 +00:00
if ( query . Limit . HasValue | | query . StartIndex . HasValue )
{
var offset = query . StartIndex ? ? 0 ;
if ( query . Limit . HasValue | | offset > 0 )
{
commandText + = " LIMIT " + ( query . Limit ? ? int . MaxValue ) . ToString ( CultureInfo . InvariantCulture ) ;
}
if ( offset > 0 )
{
commandText + = " OFFSET " + offset . ToString ( CultureInfo . InvariantCulture ) ;
}
}
2019-08-18 20:05:06 +00:00
2016-11-25 17:36:00 +00:00
var isReturningZeroItems = query . Limit . HasValue & & query . Limit < = 0 ;
var statementTexts = new List < string > ( ) ;
if ( ! isReturningZeroItems )
{
statementTexts . Add ( commandText ) ;
}
2020-06-15 21:43:52 +00:00
2016-11-25 17:36:00 +00:00
if ( query . EnableTotalRecordCount )
{
commandText = string . Empty ;
if ( EnableGroupByPresentationUniqueKey ( query ) )
{
2018-09-12 17:26:21 +00:00
commandText + = " select " + string . Join ( "," , GetFinalColumnsToSelect ( query , new [ ] { "count (distinct PresentationUniqueKey)" } ) ) + GetFromText ( ) ;
2016-11-25 17:36:00 +00:00
}
2017-08-01 16:45:57 +00:00
else if ( query . GroupBySeriesPresentationUniqueKey )
{
2018-09-12 17:26:21 +00:00
commandText + = " select " + string . Join ( "," , GetFinalColumnsToSelect ( query , new [ ] { "count (distinct SeriesPresentationUniqueKey)" } ) ) + GetFromText ( ) ;
2017-08-01 16:45:57 +00:00
}
2016-11-25 17:36:00 +00:00
else
{
2018-09-12 17:26:21 +00:00
commandText + = " select " + string . Join ( "," , GetFinalColumnsToSelect ( query , new [ ] { "count (guid)" } ) ) + GetFromText ( ) ;
2016-11-25 17:36:00 +00:00
}
2019-01-26 17:21:41 +00:00
commandText + = GetJoinUserDataText ( query )
+ whereText ;
2016-11-25 17:36:00 +00:00
statementTexts . Add ( commandText ) ;
}
2016-11-20 09:46:07 +00:00
2019-08-18 20:05:06 +00:00
var list = new List < Guid > ( ) ;
var result = new QueryResult < Guid > ( ) ;
2019-02-26 17:30:13 +00:00
using ( var connection = GetConnection ( true ) )
2016-11-20 09:46:07 +00:00
{
2019-08-18 20:05:06 +00:00
connection . RunInTransaction ( db = >
2016-11-20 09:46:07 +00:00
{
2019-04-03 11:46:07 +00:00
var statements = PrepareAll ( db , statementTexts ) . ToList ( ) ;
2016-12-12 05:49:19 +00:00
2019-02-20 13:26:49 +00:00
if ( ! isReturningZeroItems )
{
using ( var statement = statements [ 0 ] )
2016-11-21 03:52:58 +00:00
{
2019-02-20 13:26:49 +00:00
if ( EnableJoinUserData ( query ) )
2016-11-25 17:36:00 +00:00
{
2019-02-20 13:26:49 +00:00
statement . TryBind ( "@UserId" , query . User . InternalId ) ;
}
2016-11-20 09:46:07 +00:00
2019-02-20 13:26:49 +00:00
BindSimilarParams ( query , statement ) ;
BindSearchParams ( query , statement ) ;
2016-11-20 09:46:07 +00:00
2020-03-24 15:12:06 +00:00
// Running this again will bind the params
GetWhereClauses ( query , statement ) ;
2016-11-20 09:46:07 +00:00
2019-02-20 13:26:49 +00:00
foreach ( var row in statement . ExecuteQuery ( ) )
{
list . Add ( row [ 0 ] . ReadGuidFromBlob ( ) ) ;
2016-11-25 17:36:00 +00:00
}
2016-11-21 03:52:58 +00:00
}
2019-02-20 13:26:49 +00:00
}
2016-11-20 09:46:07 +00:00
2019-02-20 13:26:49 +00:00
if ( query . EnableTotalRecordCount )
{
using ( var statement = statements [ statements . Count - 1 ] )
2016-11-21 03:52:58 +00:00
{
2019-02-20 13:26:49 +00:00
if ( EnableJoinUserData ( query ) )
2016-11-25 17:36:00 +00:00
{
2019-02-20 13:26:49 +00:00
statement . TryBind ( "@UserId" , query . User . InternalId ) ;
}
2016-11-20 09:46:07 +00:00
2019-02-20 13:26:49 +00:00
BindSimilarParams ( query , statement ) ;
BindSearchParams ( query , statement ) ;
2016-11-20 09:46:07 +00:00
2020-03-24 15:12:06 +00:00
// Running this again will bind the params
GetWhereClauses ( query , statement ) ;
2016-11-20 09:46:07 +00:00
2019-02-20 13:26:49 +00:00
result . TotalRecordCount = statement . ExecuteQuery ( ) . SelectScalarInt ( ) . First ( ) ;
2016-11-25 17:36:00 +00:00
}
2019-02-20 13:26:49 +00:00
}
} , ReadTransactionMode ) ;
2016-11-20 09:46:07 +00:00
}
2019-08-18 20:05:06 +00:00
LogQueryTime ( "GetItemIds" , commandText , now ) ;
2019-09-02 06:19:29 +00:00
result . Items = list ;
2019-08-18 20:05:06 +00:00
return result ;
2016-11-20 09:46:07 +00:00
}
2017-03-19 18:59:05 +00:00
private bool IsAlphaNumeric ( string str )
{
if ( string . IsNullOrWhiteSpace ( str ) )
2019-01-26 17:21:41 +00:00
{
2017-03-19 18:59:05 +00:00
return false ;
2019-01-26 17:21:41 +00:00
}
2017-03-19 18:59:05 +00:00
for ( int i = 0 ; i < str . Length ; i + + )
{
2020-04-09 17:27:03 +00:00
if ( ! char . IsLetter ( str [ i ] ) & & ! char . IsNumber ( str [ i ] ) )
2019-01-26 17:21:41 +00:00
{
2017-03-19 18:59:05 +00:00
return false ;
2019-01-26 17:21:41 +00:00
}
2017-03-19 18:59:05 +00:00
}
return true ;
}
private bool IsValidType ( string value )
{
return IsAlphaNumeric ( value ) ;
}
private bool IsValidMediaType ( string value )
{
return IsAlphaNumeric ( value ) ;
}
private bool IsValidPersonType ( string value )
{
return IsAlphaNumeric ( value ) ;
}
2020-04-03 18:59:38 +00:00
private List < string > GetWhereClauses ( InternalItemsQuery query , IStatement statement )
2016-11-20 09:46:07 +00:00
{
2017-01-09 17:05:34 +00:00
if ( query . IsResumable ? ? false )
{
query . IsVirtualItem = false ;
}
2018-09-12 17:26:21 +00:00
var minWidth = query . MinWidth ;
var maxWidth = query . MaxWidth ;
2016-11-20 09:46:07 +00:00
if ( query . IsHD . HasValue )
{
2020-04-03 18:59:38 +00:00
const int Threshold = 1200 ;
2018-09-12 17:26:21 +00:00
if ( query . IsHD . Value )
{
2020-04-03 18:59:38 +00:00
minWidth = Threshold ;
2018-09-12 17:26:21 +00:00
}
else
{
2020-04-03 18:59:38 +00:00
maxWidth = Threshold - 1 ;
2018-09-12 17:26:21 +00:00
}
}
if ( query . Is4K . HasValue )
{
2020-04-03 18:59:38 +00:00
const int Threshold = 3800 ;
2018-09-12 17:26:21 +00:00
if ( query . Is4K . Value )
{
2020-04-03 18:59:38 +00:00
minWidth = Threshold ;
2018-09-12 17:26:21 +00:00
}
else
{
2020-04-03 18:59:38 +00:00
maxWidth = Threshold - 1 ;
2018-09-12 17:26:21 +00:00
}
}
2019-01-26 18:46:30 +00:00
var whereClauses = new List < string > ( ) ;
2018-09-12 17:26:21 +00:00
if ( minWidth . HasValue )
{
whereClauses . Add ( "Width>=@MinWidth" ) ;
2020-04-03 18:59:38 +00:00
statement ? . TryBind ( "@MinWidth" , minWidth ) ;
2018-09-12 17:26:21 +00:00
}
2020-04-03 18:59:38 +00:00
2018-09-12 17:26:21 +00:00
if ( query . MinHeight . HasValue )
{
whereClauses . Add ( "Height>=@MinHeight" ) ;
2020-04-03 18:59:38 +00:00
statement ? . TryBind ( "@MinHeight" , query . MinHeight ) ;
2018-09-12 17:26:21 +00:00
}
2020-04-03 18:59:38 +00:00
2018-09-12 17:26:21 +00:00
if ( maxWidth . HasValue )
{
whereClauses . Add ( "Width<=@MaxWidth" ) ;
2020-04-03 18:59:38 +00:00
statement ? . TryBind ( "@MaxWidth" , maxWidth ) ;
2018-09-12 17:26:21 +00:00
}
2020-04-03 18:59:38 +00:00
2018-09-12 17:26:21 +00:00
if ( query . MaxHeight . HasValue )
{
whereClauses . Add ( "Height<=@MaxHeight" ) ;
2020-04-03 18:59:38 +00:00
statement ? . TryBind ( "@MaxHeight" , query . MaxHeight ) ;
2016-11-20 09:46:07 +00:00
}
2018-09-12 17:26:21 +00:00
2016-11-20 09:46:07 +00:00
if ( query . IsLocked . HasValue )
{
whereClauses . Add ( "IsLocked=@IsLocked" ) ;
2020-04-03 18:59:38 +00:00
statement ? . TryBind ( "@IsLocked" , query . IsLocked ) ;
2016-11-20 09:46:07 +00:00
}
2018-09-12 17:26:21 +00:00
var tags = query . Tags . ToList ( ) ;
var excludeTags = query . ExcludeTags . ToList ( ) ;
2020-04-03 18:59:38 +00:00
if ( query . IsMovie = = true )
2016-11-20 09:46:07 +00:00
{
2020-04-03 18:59:38 +00:00
if ( query . IncludeItemTypes . Length = = 0
| | query . IncludeItemTypes . Contains ( nameof ( Movie ) )
| | query . IncludeItemTypes . Contains ( nameof ( Trailer ) ) )
2016-11-20 09:46:07 +00:00
{
2020-04-03 18:59:38 +00:00
whereClauses . Add ( "(IsMovie is null OR IsMovie=@IsMovie)" ) ;
2016-11-20 09:46:07 +00:00
}
2018-09-12 17:26:21 +00:00
else
2016-11-20 09:46:07 +00:00
{
2020-04-03 18:59:38 +00:00
whereClauses . Add ( "IsMovie=@IsMovie" ) ;
2016-11-20 09:46:07 +00:00
}
2018-09-12 17:26:21 +00:00
2020-04-03 18:59:38 +00:00
statement ? . TryBind ( "@IsMovie" , true ) ;
2016-11-20 09:46:07 +00:00
}
2018-09-12 17:26:21 +00:00
else if ( query . IsMovie . HasValue )
2016-11-20 09:46:07 +00:00
{
2018-09-12 17:26:21 +00:00
whereClauses . Add ( "IsMovie=@IsMovie" ) ;
2020-04-03 18:59:38 +00:00
statement ? . TryBind ( "@IsMovie" , query . IsMovie ) ;
2018-09-12 17:26:21 +00:00
}
2016-11-20 09:46:07 +00:00
2018-09-12 17:26:21 +00:00
if ( query . IsSeries . HasValue )
{
whereClauses . Add ( "IsSeries=@IsSeries" ) ;
2020-04-03 18:59:38 +00:00
statement ? . TryBind ( "@IsSeries" , query . IsSeries ) ;
2018-09-12 17:26:21 +00:00
}
2016-11-20 09:46:07 +00:00
2018-09-12 17:26:21 +00:00
if ( query . IsSports . HasValue )
{
if ( query . IsSports . Value )
{
tags . Add ( "Sports" ) ;
2016-11-20 09:46:07 +00:00
}
2018-09-12 17:26:21 +00:00
else
2016-11-20 09:46:07 +00:00
{
2018-09-12 17:26:21 +00:00
excludeTags . Add ( "Sports" ) ;
2016-11-20 09:46:07 +00:00
}
2018-09-12 17:26:21 +00:00
}
if ( query . IsNews . HasValue )
{
if ( query . IsNews . Value )
2016-11-20 09:46:07 +00:00
{
2018-09-12 17:26:21 +00:00
tags . Add ( "News" ) ;
2016-11-20 09:46:07 +00:00
}
2018-09-12 17:26:21 +00:00
else
2016-11-20 09:46:07 +00:00
{
2018-09-12 17:26:21 +00:00
excludeTags . Add ( "News" ) ;
2016-11-20 09:46:07 +00:00
}
2018-09-12 17:26:21 +00:00
}
if ( query . IsKids . HasValue )
{
if ( query . IsKids . Value )
2016-11-20 09:46:07 +00:00
{
2018-09-12 17:26:21 +00:00
tags . Add ( "Kids" ) ;
2016-11-20 09:46:07 +00:00
}
2018-09-12 17:26:21 +00:00
else
2016-11-20 09:46:07 +00:00
{
2018-09-12 17:26:21 +00:00
excludeTags . Add ( "Kids" ) ;
2016-11-20 09:46:07 +00:00
}
}
2017-01-07 08:04:54 +00:00
if ( query . SimilarTo ! = null & & query . MinSimilarityScore > 0 )
2016-11-20 09:46:07 +00:00
{
2017-01-07 08:04:54 +00:00
whereClauses . Add ( "SimilarityScore > " + ( query . MinSimilarityScore - 1 ) . ToString ( CultureInfo . InvariantCulture ) ) ;
2016-11-20 09:46:07 +00:00
}
2018-09-12 17:26:21 +00:00
if ( ! string . IsNullOrEmpty ( query . SearchTerm ) )
{
whereClauses . Add ( "SearchScore > 0" ) ;
}
2016-11-20 09:46:07 +00:00
if ( query . IsFolder . HasValue )
{
whereClauses . Add ( "IsFolder=@IsFolder" ) ;
2020-04-03 18:59:38 +00:00
statement ? . TryBind ( "@IsFolder" , query . IsFolder ) ;
2016-11-20 09:46:07 +00:00
}
var includeTypes = query . IncludeItemTypes . SelectMany ( MapIncludeItemTypes ) . ToArray ( ) ;
2018-09-12 17:26:21 +00:00
// Only specify excluded types if no included types are specified
if ( includeTypes . Length = = 0 )
2016-11-20 09:46:07 +00:00
{
2018-09-12 17:26:21 +00:00
var excludeTypes = query . ExcludeItemTypes . SelectMany ( MapIncludeItemTypes ) . ToArray ( ) ;
if ( excludeTypes . Length = = 1 )
{
whereClauses . Add ( "type<>@type" ) ;
2020-04-03 18:59:38 +00:00
statement ? . TryBind ( "@type" , excludeTypes [ 0 ] ) ;
2018-09-12 17:26:21 +00:00
}
else if ( excludeTypes . Length > 1 )
2016-11-20 09:46:07 +00:00
{
2019-01-26 17:21:41 +00:00
var inClause = string . Join ( "," , excludeTypes . Select ( i = > "'" + i + "'" ) ) ;
whereClauses . Add ( $"type not in ({inClause})" ) ;
2016-11-20 09:46:07 +00:00
}
}
2020-02-19 20:56:35 +00:00
else if ( includeTypes . Length = = 1 )
{
whereClauses . Add ( "type=@type" ) ;
2020-04-03 18:59:38 +00:00
statement ? . TryBind ( "@type" , includeTypes [ 0 ] ) ;
2020-02-19 20:56:35 +00:00
}
else if ( includeTypes . Length > 1 )
{
var inClause = string . Join ( "," , includeTypes . Select ( i = > "'" + i + "'" ) ) ;
whereClauses . Add ( $"type in ({inClause})" ) ;
}
2016-11-20 09:46:07 +00:00
if ( query . ChannelIds . Length = = 1 )
{
whereClauses . Add ( "ChannelId=@ChannelId" ) ;
2020-04-03 18:59:38 +00:00
statement ? . TryBind ( "@ChannelId" , query . ChannelIds [ 0 ] . ToString ( "N" , CultureInfo . InvariantCulture ) ) ;
2016-11-20 09:46:07 +00:00
}
2017-03-19 18:59:05 +00:00
else if ( query . ChannelIds . Length > 1 )
2016-11-20 09:46:07 +00:00
{
2019-02-28 22:22:57 +00:00
var inClause = string . Join ( "," , query . ChannelIds . Select ( i = > "'" + i . ToString ( "N" , CultureInfo . InvariantCulture ) + "'" ) ) ;
2019-01-26 17:21:41 +00:00
whereClauses . Add ( $"ChannelId in ({inClause})" ) ;
2016-11-20 09:46:07 +00:00
}
2018-09-12 17:26:21 +00:00
if ( ! query . ParentId . Equals ( Guid . Empty ) )
2016-11-20 09:46:07 +00:00
{
whereClauses . Add ( "ParentId=@ParentId" ) ;
2020-04-03 18:59:38 +00:00
statement ? . TryBind ( "@ParentId" , query . ParentId ) ;
2016-11-20 09:46:07 +00:00
}
if ( ! string . IsNullOrWhiteSpace ( query . Path ) )
{
whereClauses . Add ( "Path=@Path" ) ;
2020-04-03 18:59:38 +00:00
statement ? . TryBind ( "@Path" , GetPathToSave ( query . Path ) ) ;
2016-11-20 09:46:07 +00:00
}
if ( ! string . IsNullOrWhiteSpace ( query . PresentationUniqueKey ) )
{
whereClauses . Add ( "PresentationUniqueKey=@PresentationUniqueKey" ) ;
2020-04-03 18:59:38 +00:00
statement ? . TryBind ( "@PresentationUniqueKey" , query . PresentationUniqueKey ) ;
2016-11-20 09:46:07 +00:00
}
if ( query . MinCommunityRating . HasValue )
{
whereClauses . Add ( "CommunityRating>=@MinCommunityRating" ) ;
2020-04-03 18:59:38 +00:00
statement ? . TryBind ( "@MinCommunityRating" , query . MinCommunityRating . Value ) ;
2016-11-20 09:46:07 +00:00
}
if ( query . MinIndexNumber . HasValue )
{
whereClauses . Add ( "IndexNumber>=@MinIndexNumber" ) ;
2020-04-03 18:59:38 +00:00
statement ? . TryBind ( "@MinIndexNumber" , query . MinIndexNumber . Value ) ;
2016-11-20 09:46:07 +00:00
}
if ( query . MinDateCreated . HasValue )
{
whereClauses . Add ( "DateCreated>=@MinDateCreated" ) ;
2020-04-03 18:59:38 +00:00
statement ? . TryBind ( "@MinDateCreated" , query . MinDateCreated . Value ) ;
2016-11-20 09:46:07 +00:00
}
if ( query . MinDateLastSaved . HasValue )
{
2017-09-24 01:09:51 +00:00
whereClauses . Add ( "(DateLastSaved not null and DateLastSaved>=@MinDateLastSavedForUser)" ) ;
2020-04-03 18:59:38 +00:00
statement ? . TryBind ( "@MinDateLastSaved" , query . MinDateLastSaved . Value ) ;
2016-11-20 09:46:07 +00:00
}
2017-05-23 16:43:24 +00:00
if ( query . MinDateLastSavedForUser . HasValue )
{
2017-09-24 01:09:51 +00:00
whereClauses . Add ( "(DateLastSaved not null and DateLastSaved>=@MinDateLastSavedForUser)" ) ;
2020-04-03 18:59:38 +00:00
statement ? . TryBind ( "@MinDateLastSavedForUser" , query . MinDateLastSavedForUser . Value ) ;
2017-05-23 16:43:24 +00:00
}
2016-11-20 09:46:07 +00:00
if ( query . IndexNumber . HasValue )
{
whereClauses . Add ( "IndexNumber=@IndexNumber" ) ;
2020-04-03 18:59:38 +00:00
statement ? . TryBind ( "@IndexNumber" , query . IndexNumber . Value ) ;
2016-11-20 09:46:07 +00:00
}
2020-06-15 21:43:52 +00:00
2016-11-20 09:46:07 +00:00
if ( query . ParentIndexNumber . HasValue )
{
whereClauses . Add ( "ParentIndexNumber=@ParentIndexNumber" ) ;
2020-04-03 18:59:38 +00:00
statement ? . TryBind ( "@ParentIndexNumber" , query . ParentIndexNumber . Value ) ;
2016-11-20 09:46:07 +00:00
}
2020-06-15 21:43:52 +00:00
2016-11-20 09:46:07 +00:00
if ( query . ParentIndexNumberNotEquals . HasValue )
{
whereClauses . Add ( "(ParentIndexNumber<>@ParentIndexNumberNotEquals or ParentIndexNumber is null)" ) ;
2020-04-03 18:59:38 +00:00
statement ? . TryBind ( "@ParentIndexNumberNotEquals" , query . ParentIndexNumberNotEquals . Value ) ;
2016-11-20 09:46:07 +00:00
}
2018-09-12 17:26:21 +00:00
var minEndDate = query . MinEndDate ;
var maxEndDate = query . MaxEndDate ;
if ( query . HasAired . HasValue )
{
if ( query . HasAired . Value )
{
maxEndDate = DateTime . UtcNow ;
}
else
{
minEndDate = DateTime . UtcNow ;
}
}
if ( minEndDate . HasValue )
2016-11-20 09:46:07 +00:00
{
whereClauses . Add ( "EndDate>=@MinEndDate" ) ;
2020-04-03 18:59:38 +00:00
statement ? . TryBind ( "@MinEndDate" , minEndDate . Value ) ;
2016-11-20 09:46:07 +00:00
}
2018-09-12 17:26:21 +00:00
if ( maxEndDate . HasValue )
2016-11-20 09:46:07 +00:00
{
whereClauses . Add ( "EndDate<=@MaxEndDate" ) ;
2020-04-03 18:59:38 +00:00
statement ? . TryBind ( "@MaxEndDate" , maxEndDate . Value ) ;
2016-11-20 09:46:07 +00:00
}
if ( query . MinStartDate . HasValue )
{
whereClauses . Add ( "StartDate>=@MinStartDate" ) ;
2020-04-03 18:59:38 +00:00
statement ? . TryBind ( "@MinStartDate" , query . MinStartDate . Value ) ;
2016-11-20 09:46:07 +00:00
}
if ( query . MaxStartDate . HasValue )
{
whereClauses . Add ( "StartDate<=@MaxStartDate" ) ;
2020-04-03 18:59:38 +00:00
statement ? . TryBind ( "@MaxStartDate" , query . MaxStartDate . Value ) ;
2016-11-20 09:46:07 +00:00
}
if ( query . MinPremiereDate . HasValue )
{
whereClauses . Add ( "PremiereDate>=@MinPremiereDate" ) ;
2020-04-03 18:59:38 +00:00
statement ? . TryBind ( "@MinPremiereDate" , query . MinPremiereDate . Value ) ;
2016-11-20 09:46:07 +00:00
}
2020-04-03 18:59:38 +00:00
2016-11-20 09:46:07 +00:00
if ( query . MaxPremiereDate . HasValue )
{
whereClauses . Add ( "PremiereDate<=@MaxPremiereDate" ) ;
2020-04-03 18:59:38 +00:00
statement ? . TryBind ( "@MaxPremiereDate" , query . MaxPremiereDate . Value ) ;
2016-11-20 09:46:07 +00:00
}
2020-04-03 18:59:38 +00:00
var trailerTypes = query . TrailerTypes ;
int trailerTypesLen = trailerTypes . Length ;
if ( trailerTypesLen > 0 )
2016-11-20 09:46:07 +00:00
{
2020-04-09 17:27:03 +00:00
const string Or = " OR " ;
2020-04-03 18:59:38 +00:00
StringBuilder clause = new StringBuilder ( "(" , trailerTypesLen * 32 ) ;
for ( int i = 0 ; i < trailerTypesLen ; i + + )
2016-11-20 09:46:07 +00:00
{
2020-04-03 18:59:38 +00:00
var paramName = "@TrailerTypes" + i ;
clause . Append ( "TrailerTypes like " )
. Append ( paramName )
2020-04-09 17:27:03 +00:00
. Append ( Or ) ;
2020-04-03 18:59:38 +00:00
statement ? . TryBind ( paramName , "%" + trailerTypes [ i ] + "%" ) ;
2016-11-20 09:46:07 +00:00
}
2020-04-03 18:59:38 +00:00
// Remove last " OR "
2020-04-09 17:27:03 +00:00
clause . Length - = Or . Length ;
2020-04-03 18:59:38 +00:00
clause . Append ( ')' ) ;
whereClauses . Add ( clause . ToString ( ) ) ;
2016-11-20 09:46:07 +00:00
}
if ( query . IsAiring . HasValue )
{
if ( query . IsAiring . Value )
{
whereClauses . Add ( "StartDate<=@MaxStartDate" ) ;
2020-04-03 18:59:38 +00:00
statement ? . TryBind ( "@MaxStartDate" , DateTime . UtcNow ) ;
2016-11-20 09:46:07 +00:00
whereClauses . Add ( "EndDate>=@MinEndDate" ) ;
2020-04-03 18:59:38 +00:00
statement ? . TryBind ( "@MinEndDate" , DateTime . UtcNow ) ;
2016-11-20 09:46:07 +00:00
}
else
{
whereClauses . Add ( "(StartDate>@IsAiringDate OR EndDate < @IsAiringDate)" ) ;
2020-04-03 18:59:38 +00:00
statement ? . TryBind ( "@IsAiringDate" , DateTime . UtcNow ) ;
2016-11-20 09:46:07 +00:00
}
}
if ( query . PersonIds . Length > 0 )
{
2019-01-07 23:27:46 +00:00
// TODO: Should this query with CleanName ?
2016-11-20 20:20:01 +00:00
var clauses = new List < string > ( ) ;
var index = 0 ;
foreach ( var personId in query . PersonIds )
{
var paramName = "@PersonId" + index ;
2018-09-12 17:26:21 +00:00
clauses . Add ( "(guid in (select itemid from People where Name = (select Name from TypedBaseItems where guid=" + paramName + ")))" ) ;
2020-04-03 18:59:38 +00:00
statement ? . TryBind ( paramName , personId . ToByteArray ( ) ) ;
2016-11-20 20:20:01 +00:00
index + + ;
}
2020-04-03 18:59:38 +00:00
2019-01-26 17:21:41 +00:00
var clause = "(" + string . Join ( " OR " , clauses ) + ")" ;
2016-11-20 20:20:01 +00:00
whereClauses . Add ( clause ) ;
2016-11-20 09:46:07 +00:00
}
if ( ! string . IsNullOrWhiteSpace ( query . Person ) )
{
whereClauses . Add ( "Guid in (select ItemId from People where Name=@PersonName)" ) ;
2020-04-03 18:59:38 +00:00
statement ? . TryBind ( "@PersonName" , query . Person ) ;
2016-11-20 09:46:07 +00:00
}
if ( ! string . IsNullOrWhiteSpace ( query . MinSortName ) )
{
whereClauses . Add ( "SortName>=@MinSortName" ) ;
2020-04-03 18:59:38 +00:00
statement ? . TryBind ( "@MinSortName" , query . MinSortName ) ;
2016-11-20 09:46:07 +00:00
}
if ( ! string . IsNullOrWhiteSpace ( query . ExternalSeriesId ) )
{
whereClauses . Add ( "ExternalSeriesId=@ExternalSeriesId" ) ;
2020-04-03 18:59:38 +00:00
statement ? . TryBind ( "@ExternalSeriesId" , query . ExternalSeriesId ) ;
2016-11-20 09:46:07 +00:00
}
2016-11-27 00:40:15 +00:00
if ( ! string . IsNullOrWhiteSpace ( query . ExternalId ) )
{
whereClauses . Add ( "ExternalId=@ExternalId" ) ;
2020-04-03 18:59:38 +00:00
statement ? . TryBind ( "@ExternalId" , query . ExternalId ) ;
2016-11-27 00:40:15 +00:00
}
2016-11-20 09:46:07 +00:00
if ( ! string . IsNullOrWhiteSpace ( query . Name ) )
{
whereClauses . Add ( "CleanName=@Name" ) ;
2020-04-03 18:59:38 +00:00
statement ? . TryBind ( "@Name" , GetCleanValue ( query . Name ) ) ;
2016-11-20 09:46:07 +00:00
}
2018-09-12 17:26:21 +00:00
// These are the same, for now
var nameContains = query . NameContains ;
if ( ! string . IsNullOrWhiteSpace ( nameContains ) )
2016-11-20 09:46:07 +00:00
{
2018-09-12 17:26:21 +00:00
whereClauses . Add ( "(CleanName like @NameContains or OriginalTitle like @NameContains)" ) ;
2016-11-20 09:46:07 +00:00
if ( statement ! = null )
{
2018-09-12 17:26:21 +00:00
nameContains = FixUnicodeChars ( nameContains ) ;
statement . TryBind ( "@NameContains" , "%" + GetCleanValue ( nameContains ) + "%" ) ;
2016-11-20 09:46:07 +00:00
}
}
2018-09-12 17:26:21 +00:00
2016-11-20 09:46:07 +00:00
if ( ! string . IsNullOrWhiteSpace ( query . NameStartsWith ) )
{
whereClauses . Add ( "SortName like @NameStartsWith" ) ;
2020-04-03 18:59:38 +00:00
statement ? . TryBind ( "@NameStartsWith" , query . NameStartsWith + "%" ) ;
2016-11-20 09:46:07 +00:00
}
2020-04-03 18:59:38 +00:00
2016-11-20 09:46:07 +00:00
if ( ! string . IsNullOrWhiteSpace ( query . NameStartsWithOrGreater ) )
{
whereClauses . Add ( "SortName >= @NameStartsWithOrGreater" ) ;
// lowercase this because SortName is stored as lowercase
2020-04-03 18:59:38 +00:00
statement ? . TryBind ( "@NameStartsWithOrGreater" , query . NameStartsWithOrGreater . ToLowerInvariant ( ) ) ;
2016-11-20 09:46:07 +00:00
}
2020-04-03 18:59:38 +00:00
2016-11-20 09:46:07 +00:00
if ( ! string . IsNullOrWhiteSpace ( query . NameLessThan ) )
{
whereClauses . Add ( "SortName < @NameLessThan" ) ;
// lowercase this because SortName is stored as lowercase
2020-04-03 18:59:38 +00:00
statement ? . TryBind ( "@NameLessThan" , query . NameLessThan . ToLowerInvariant ( ) ) ;
2016-11-20 09:46:07 +00:00
}
if ( query . ImageTypes . Length > 0 )
{
foreach ( var requiredImage in query . ImageTypes )
{
whereClauses . Add ( "Images like '%" + requiredImage + "%'" ) ;
}
}
if ( query . IsLiked . HasValue )
{
if ( query . IsLiked . Value )
{
whereClauses . Add ( "rating>=@UserRating" ) ;
2020-04-03 18:59:38 +00:00
statement ? . TryBind ( "@UserRating" , UserItemData . MinLikeValue ) ;
2016-11-20 09:46:07 +00:00
}
else
{
whereClauses . Add ( "(rating is null or rating<@UserRating)" ) ;
2020-04-03 18:59:38 +00:00
statement ? . TryBind ( "@UserRating" , UserItemData . MinLikeValue ) ;
2016-11-20 09:46:07 +00:00
}
}
if ( query . IsFavoriteOrLiked . HasValue )
{
if ( query . IsFavoriteOrLiked . Value )
{
whereClauses . Add ( "IsFavorite=@IsFavoriteOrLiked" ) ;
}
else
{
whereClauses . Add ( "(IsFavorite is null or IsFavorite=@IsFavoriteOrLiked)" ) ;
}
2020-04-03 18:59:38 +00:00
statement ? . TryBind ( "@IsFavoriteOrLiked" , query . IsFavoriteOrLiked . Value ) ;
2016-11-20 09:46:07 +00:00
}
if ( query . IsFavorite . HasValue )
{
if ( query . IsFavorite . Value )
{
whereClauses . Add ( "IsFavorite=@IsFavorite" ) ;
}
else
{
whereClauses . Add ( "(IsFavorite is null or IsFavorite=@IsFavorite)" ) ;
}
2020-04-03 18:59:38 +00:00
statement ? . TryBind ( "@IsFavorite" , query . IsFavorite . Value ) ;
2016-11-20 09:46:07 +00:00
}
if ( EnableJoinUserData ( query ) )
{
if ( query . IsPlayed . HasValue )
{
2018-09-12 17:26:21 +00:00
// We should probably figure this out for all folders, but for right now, this is the only place where we need it
if ( query . IncludeItemTypes . Length = = 1 & & string . Equals ( query . IncludeItemTypes [ 0 ] , typeof ( Series ) . Name , StringComparison . OrdinalIgnoreCase ) )
2016-11-20 09:46:07 +00:00
{
2018-09-12 17:26:21 +00:00
if ( query . IsPlayed . Value )
{
whereClauses . Add ( "PresentationUniqueKey not in (select S.SeriesPresentationUniqueKey from TypedBaseitems S left join UserDatas UD on S.UserDataKey=UD.Key And UD.UserId=@UserId where Coalesce(UD.Played, 0)=0 and S.IsFolder=0 and S.IsVirtualItem=0 and S.SeriesPresentationUniqueKey not null)" ) ;
}
else
{
whereClauses . Add ( "PresentationUniqueKey in (select S.SeriesPresentationUniqueKey from TypedBaseitems S left join UserDatas UD on S.UserDataKey=UD.Key And UD.UserId=@UserId where Coalesce(UD.Played, 0)=0 and S.IsFolder=0 and S.IsVirtualItem=0 and S.SeriesPresentationUniqueKey not null)" ) ;
}
2016-11-20 09:46:07 +00:00
}
else
{
2018-09-12 17:26:21 +00:00
if ( query . IsPlayed . Value )
{
whereClauses . Add ( "(played=@IsPlayed)" ) ;
}
else
{
whereClauses . Add ( "(played is null or played=@IsPlayed)" ) ;
}
2020-04-03 18:59:38 +00:00
statement ? . TryBind ( "@IsPlayed" , query . IsPlayed . Value ) ;
2016-11-20 09:46:07 +00:00
}
}
}
if ( query . IsResumable . HasValue )
{
if ( query . IsResumable . Value )
{
whereClauses . Add ( "playbackPositionTicks > 0" ) ;
}
else
{
whereClauses . Add ( "(playbackPositionTicks is null or playbackPositionTicks = 0)" ) ;
}
}
2016-12-20 19:59:25 +00:00
if ( query . ArtistIds . Length > 0 )
2016-11-20 09:46:07 +00:00
{
var clauses = new List < string > ( ) ;
var index = 0 ;
2016-12-20 19:59:25 +00:00
foreach ( var artistId in query . ArtistIds )
2016-11-20 09:46:07 +00:00
{
2016-12-20 19:59:25 +00:00
var paramName = "@ArtistIds" + index ;
2018-09-12 17:26:21 +00:00
clauses . Add ( "(guid in (select itemid from itemvalues where CleanValue = (select CleanName from TypedBaseItems where guid=" + paramName + ") and Type<=1))" ) ;
if ( statement ! = null )
{
2019-09-25 11:24:39 +00:00
statement . TryBind ( paramName , artistId . ToByteArray ( ) ) ;
2018-09-12 17:26:21 +00:00
}
2020-06-15 21:43:52 +00:00
2018-09-12 17:26:21 +00:00
index + + ;
}
2020-04-03 18:59:38 +00:00
2019-01-26 17:21:41 +00:00
var clause = "(" + string . Join ( " OR " , clauses ) + ")" ;
2018-09-12 17:26:21 +00:00
whereClauses . Add ( clause ) ;
}
if ( query . AlbumArtistIds . Length > 0 )
{
var clauses = new List < string > ( ) ;
var index = 0 ;
foreach ( var artistId in query . AlbumArtistIds )
{
var paramName = "@ArtistIds" + index ;
clauses . Add ( "(guid in (select itemid from itemvalues where CleanValue = (select CleanName from TypedBaseItems where guid=" + paramName + ") and Type=1))" ) ;
if ( statement ! = null )
{
2019-09-25 11:24:39 +00:00
statement . TryBind ( paramName , artistId . ToByteArray ( ) ) ;
2018-09-12 17:26:21 +00:00
}
2020-06-15 21:43:52 +00:00
2018-09-12 17:26:21 +00:00
index + + ;
}
2020-04-03 18:59:38 +00:00
2019-01-26 17:21:41 +00:00
var clause = "(" + string . Join ( " OR " , clauses ) + ")" ;
2018-09-12 17:26:21 +00:00
whereClauses . Add ( clause ) ;
}
if ( query . ContributingArtistIds . Length > 0 )
{
var clauses = new List < string > ( ) ;
var index = 0 ;
foreach ( var artistId in query . ContributingArtistIds )
{
var paramName = "@ArtistIds" + index ;
clauses . Add ( "((select CleanName from TypedBaseItems where guid=" + paramName + ") in (select CleanValue from itemvalues where ItemId=Guid and Type=0) AND (select CleanName from TypedBaseItems where guid=" + paramName + ") not in (select CleanValue from itemvalues where ItemId=Guid and Type=1))" ) ;
2016-11-20 09:46:07 +00:00
if ( statement ! = null )
{
2019-09-25 11:24:39 +00:00
statement . TryBind ( paramName , artistId . ToByteArray ( ) ) ;
2016-11-20 09:46:07 +00:00
}
2020-06-15 21:43:52 +00:00
2016-11-20 09:46:07 +00:00
index + + ;
}
2020-06-15 21:43:52 +00:00
2019-01-26 17:21:41 +00:00
var clause = "(" + string . Join ( " OR " , clauses ) + ")" ;
2016-11-20 09:46:07 +00:00
whereClauses . Add ( clause ) ;
}
2017-02-18 08:32:17 +00:00
if ( query . AlbumIds . Length > 0 )
{
var clauses = new List < string > ( ) ;
var index = 0 ;
foreach ( var albumId in query . AlbumIds )
{
var paramName = "@AlbumIds" + index ;
clauses . Add ( "Album in (select Name from typedbaseitems where guid=" + paramName + ")" ) ;
if ( statement ! = null )
{
2019-09-25 11:24:39 +00:00
statement . TryBind ( paramName , albumId . ToByteArray ( ) ) ;
2017-02-18 08:32:17 +00:00
}
2020-06-15 21:43:52 +00:00
2017-02-18 08:32:17 +00:00
index + + ;
}
2020-06-15 21:43:52 +00:00
2019-01-26 17:21:41 +00:00
var clause = "(" + string . Join ( " OR " , clauses ) + ")" ;
2017-02-18 08:32:17 +00:00
whereClauses . Add ( clause ) ;
}
2016-11-20 09:46:07 +00:00
if ( query . ExcludeArtistIds . Length > 0 )
{
var clauses = new List < string > ( ) ;
var index = 0 ;
foreach ( var artistId in query . ExcludeArtistIds )
{
2016-11-20 20:20:01 +00:00
var paramName = "@ExcludeArtistId" + index ;
2018-09-12 17:26:21 +00:00
clauses . Add ( "(guid not in (select itemid from itemvalues where CleanValue = (select CleanName from TypedBaseItems where guid=" + paramName + ") and Type<=1))" ) ;
2016-11-20 20:20:01 +00:00
if ( statement ! = null )
2016-11-20 09:46:07 +00:00
{
2019-09-25 11:24:39 +00:00
statement . TryBind ( paramName , artistId . ToByteArray ( ) ) ;
2016-11-20 09:46:07 +00:00
}
2020-06-15 21:43:52 +00:00
2016-11-20 20:20:01 +00:00
index + + ;
2016-11-20 09:46:07 +00:00
}
2020-06-15 21:43:52 +00:00
2019-01-26 17:21:41 +00:00
var clause = "(" + string . Join ( " OR " , clauses ) + ")" ;
2016-11-20 09:46:07 +00:00
whereClauses . Add ( clause ) ;
}
if ( query . GenreIds . Length > 0 )
{
2016-11-20 20:20:01 +00:00
var clauses = new List < string > ( ) ;
var index = 0 ;
foreach ( var genreId in query . GenreIds )
{
var paramName = "@GenreId" + index ;
2018-09-12 17:26:21 +00:00
clauses . Add ( "(guid in (select itemid from itemvalues where CleanValue = (select CleanName from TypedBaseItems where guid=" + paramName + ") and Type=2))" ) ;
2016-11-20 20:20:01 +00:00
if ( statement ! = null )
{
2019-09-25 11:24:39 +00:00
statement . TryBind ( paramName , genreId . ToByteArray ( ) ) ;
2016-11-20 20:20:01 +00:00
}
2020-06-15 21:43:52 +00:00
2016-11-20 20:20:01 +00:00
index + + ;
}
2020-06-15 21:43:52 +00:00
2019-01-26 17:21:41 +00:00
var clause = "(" + string . Join ( " OR " , clauses ) + ")" ;
2016-11-20 20:20:01 +00:00
whereClauses . Add ( clause ) ;
2016-11-20 09:46:07 +00:00
}
if ( query . Genres . Length > 0 )
{
var clauses = new List < string > ( ) ;
var index = 0 ;
foreach ( var item in query . Genres )
{
clauses . Add ( "@Genre" + index + " in (select CleanValue from itemvalues where ItemId=Guid and Type=2)" ) ;
if ( statement ! = null )
{
statement . TryBind ( "@Genre" + index , GetCleanValue ( item ) ) ;
}
2020-06-15 21:43:52 +00:00
2016-11-20 09:46:07 +00:00
index + + ;
}
2020-06-15 21:43:52 +00:00
2019-01-26 17:21:41 +00:00
var clause = "(" + string . Join ( " OR " , clauses ) + ")" ;
2016-11-20 09:46:07 +00:00
whereClauses . Add ( clause ) ;
}
2018-09-12 17:26:21 +00:00
if ( tags . Count > 0 )
2016-11-20 09:46:07 +00:00
{
var clauses = new List < string > ( ) ;
var index = 0 ;
2018-09-12 17:26:21 +00:00
foreach ( var item in tags )
2016-11-20 09:46:07 +00:00
{
clauses . Add ( "@Tag" + index + " in (select CleanValue from itemvalues where ItemId=Guid and Type=4)" ) ;
if ( statement ! = null )
{
statement . TryBind ( "@Tag" + index , GetCleanValue ( item ) ) ;
}
2020-06-15 21:43:52 +00:00
2016-11-20 09:46:07 +00:00
index + + ;
}
2020-06-15 21:43:52 +00:00
2019-01-26 17:21:41 +00:00
var clause = "(" + string . Join ( " OR " , clauses ) + ")" ;
2016-11-20 09:46:07 +00:00
whereClauses . Add ( clause ) ;
}
2018-09-12 17:26:21 +00:00
if ( excludeTags . Count > 0 )
{
var clauses = new List < string > ( ) ;
var index = 0 ;
foreach ( var item in excludeTags )
{
clauses . Add ( "@ExcludeTag" + index + " not in (select CleanValue from itemvalues where ItemId=Guid and Type=4)" ) ;
if ( statement ! = null )
{
statement . TryBind ( "@ExcludeTag" + index , GetCleanValue ( item ) ) ;
}
2020-06-15 21:43:52 +00:00
2018-09-12 17:26:21 +00:00
index + + ;
}
2020-06-15 21:43:52 +00:00
2019-01-26 17:21:41 +00:00
var clause = "(" + string . Join ( " OR " , clauses ) + ")" ;
2018-09-12 17:26:21 +00:00
whereClauses . Add ( clause ) ;
}
2016-11-20 09:46:07 +00:00
if ( query . StudioIds . Length > 0 )
{
2016-11-20 20:20:01 +00:00
var clauses = new List < string > ( ) ;
var index = 0 ;
foreach ( var studioId in query . StudioIds )
{
var paramName = "@StudioId" + index ;
2018-09-12 17:26:21 +00:00
clauses . Add ( "(guid in (select itemid from itemvalues where CleanValue = (select CleanName from TypedBaseItems where guid=" + paramName + ") and Type=3))" ) ;
2016-11-20 20:20:01 +00:00
if ( statement ! = null )
{
2019-09-25 11:24:39 +00:00
statement . TryBind ( paramName , studioId . ToByteArray ( ) ) ;
2016-11-20 20:20:01 +00:00
}
2020-06-15 21:43:52 +00:00
2016-11-20 20:20:01 +00:00
index + + ;
}
2020-06-15 21:43:52 +00:00
2019-01-26 17:21:41 +00:00
var clause = "(" + string . Join ( " OR " , clauses ) + ")" ;
2016-11-20 20:20:01 +00:00
whereClauses . Add ( clause ) ;
2016-11-20 09:46:07 +00:00
}
if ( query . OfficialRatings . Length > 0 )
{
var clauses = new List < string > ( ) ;
var index = 0 ;
foreach ( var item in query . OfficialRatings )
{
clauses . Add ( "OfficialRating=@OfficialRating" + index ) ;
if ( statement ! = null )
{
statement . TryBind ( "@OfficialRating" + index , item ) ;
}
2020-06-15 21:43:52 +00:00
2016-11-20 09:46:07 +00:00
index + + ;
}
2020-06-15 21:43:52 +00:00
2019-01-26 17:21:41 +00:00
var clause = "(" + string . Join ( " OR " , clauses ) + ")" ;
2016-11-20 09:46:07 +00:00
whereClauses . Add ( clause ) ;
}
if ( query . MinParentalRating . HasValue )
{
2017-11-29 20:50:18 +00:00
whereClauses . Add ( "InheritedParentalRatingValue>=@MinParentalRating" ) ;
2016-11-20 09:46:07 +00:00
if ( statement ! = null )
{
statement . TryBind ( "@MinParentalRating" , query . MinParentalRating . Value ) ;
}
}
if ( query . MaxParentalRating . HasValue )
{
whereClauses . Add ( "InheritedParentalRatingValue<=@MaxParentalRating" ) ;
if ( statement ! = null )
{
statement . TryBind ( "@MaxParentalRating" , query . MaxParentalRating . Value ) ;
}
}
if ( query . HasParentalRating . HasValue )
{
if ( query . HasParentalRating . Value )
{
whereClauses . Add ( "InheritedParentalRatingValue > 0" ) ;
}
else
{
whereClauses . Add ( "InheritedParentalRatingValue = 0" ) ;
}
}
2018-09-12 17:26:21 +00:00
if ( query . HasOfficialRating . HasValue )
{
if ( query . HasOfficialRating . Value )
{
whereClauses . Add ( "(OfficialRating not null AND OfficialRating<>'')" ) ;
}
else
{
whereClauses . Add ( "(OfficialRating is null OR OfficialRating='')" ) ;
}
}
2016-11-20 09:46:07 +00:00
if ( query . HasOverview . HasValue )
{
if ( query . HasOverview . Value )
{
whereClauses . Add ( "(Overview not null AND Overview<>'')" ) ;
}
else
{
whereClauses . Add ( "(Overview is null OR Overview='')" ) ;
}
}
2018-09-12 17:26:21 +00:00
if ( query . HasOwnerId . HasValue )
{
if ( query . HasOwnerId . Value )
{
whereClauses . Add ( "OwnerId not null" ) ;
}
else
{
whereClauses . Add ( "OwnerId is null" ) ;
}
}
2017-09-10 03:18:23 +00:00
if ( ! string . IsNullOrWhiteSpace ( query . HasNoAudioTrackWithLanguage ) )
{
whereClauses . Add ( "((select language from MediaStreams where MediaStreams.ItemId=A.Guid and MediaStreams.StreamType='Audio' and MediaStreams.Language=@HasNoAudioTrackWithLanguage limit 1) is null)" ) ;
if ( statement ! = null )
{
statement . TryBind ( "@HasNoAudioTrackWithLanguage" , query . HasNoAudioTrackWithLanguage ) ;
}
}
if ( ! string . IsNullOrWhiteSpace ( query . HasNoInternalSubtitleTrackWithLanguage ) )
{
whereClauses . Add ( "((select language from MediaStreams where MediaStreams.ItemId=A.Guid and MediaStreams.StreamType='Subtitle' and MediaStreams.IsExternal=0 and MediaStreams.Language=@HasNoInternalSubtitleTrackWithLanguage limit 1) is null)" ) ;
if ( statement ! = null )
{
statement . TryBind ( "@HasNoInternalSubtitleTrackWithLanguage" , query . HasNoInternalSubtitleTrackWithLanguage ) ;
}
}
if ( ! string . IsNullOrWhiteSpace ( query . HasNoExternalSubtitleTrackWithLanguage ) )
{
whereClauses . Add ( "((select language from MediaStreams where MediaStreams.ItemId=A.Guid and MediaStreams.StreamType='Subtitle' and MediaStreams.IsExternal=1 and MediaStreams.Language=@HasNoExternalSubtitleTrackWithLanguage limit 1) is null)" ) ;
if ( statement ! = null )
{
statement . TryBind ( "@HasNoExternalSubtitleTrackWithLanguage" , query . HasNoExternalSubtitleTrackWithLanguage ) ;
}
}
if ( ! string . IsNullOrWhiteSpace ( query . HasNoSubtitleTrackWithLanguage ) )
{
whereClauses . Add ( "((select language from MediaStreams where MediaStreams.ItemId=A.Guid and MediaStreams.StreamType='Subtitle' and MediaStreams.Language=@HasNoSubtitleTrackWithLanguage limit 1) is null)" ) ;
if ( statement ! = null )
{
statement . TryBind ( "@HasNoSubtitleTrackWithLanguage" , query . HasNoSubtitleTrackWithLanguage ) ;
}
}
2018-09-12 17:26:21 +00:00
if ( query . HasSubtitles . HasValue )
{
if ( query . HasSubtitles . Value )
{
whereClauses . Add ( "((select type from MediaStreams where MediaStreams.ItemId=A.Guid and MediaStreams.StreamType='Subtitle' limit 1) not null)" ) ;
}
else
{
whereClauses . Add ( "((select type from MediaStreams where MediaStreams.ItemId=A.Guid and MediaStreams.StreamType='Subtitle' limit 1) is null)" ) ;
}
}
2017-09-10 03:18:23 +00:00
if ( query . HasChapterImages . HasValue )
{
if ( query . HasChapterImages . Value )
{
whereClauses . Add ( "((select imagepath from Chapters2 where Chapters2.ItemId=A.Guid and imagepath not null limit 1) not null)" ) ;
}
else
{
whereClauses . Add ( "((select imagepath from Chapters2 where Chapters2.ItemId=A.Guid and imagepath not null limit 1) is null)" ) ;
}
}
2017-06-24 18:32:22 +00:00
if ( query . HasDeadParentId . HasValue & & query . HasDeadParentId . Value )
2016-11-20 09:46:07 +00:00
{
2017-06-24 18:32:22 +00:00
whereClauses . Add ( "ParentId NOT NULL AND ParentId NOT IN (select guid from TypedBaseItems)" ) ;
2016-11-20 09:46:07 +00:00
}
2018-09-12 17:26:21 +00:00
if ( query . IsDeadArtist . HasValue & & query . IsDeadArtist . Value )
{
whereClauses . Add ( "CleanName not in (Select CleanValue From ItemValues where Type in (0,1))" ) ;
}
if ( query . IsDeadStudio . HasValue & & query . IsDeadStudio . Value )
{
whereClauses . Add ( "CleanName not in (Select CleanValue From ItemValues where Type = 3)" ) ;
}
if ( query . IsDeadPerson . HasValue & & query . IsDeadPerson . Value )
{
whereClauses . Add ( "Name not in (Select Name From People)" ) ;
}
2016-11-20 09:46:07 +00:00
if ( query . Years . Length = = 1 )
{
whereClauses . Add ( "ProductionYear=@Years" ) ;
if ( statement ! = null )
{
statement . TryBind ( "@Years" , query . Years [ 0 ] . ToString ( ) ) ;
}
}
else if ( query . Years . Length > 1 )
{
2019-02-09 00:39:59 +00:00
var val = string . Join ( "," , query . Years ) ;
2016-11-20 09:46:07 +00:00
whereClauses . Add ( "ProductionYear in (" + val + ")" ) ;
}
2017-08-13 02:09:07 +00:00
var isVirtualItem = query . IsVirtualItem ? ? query . IsMissing ;
if ( isVirtualItem . HasValue )
2016-11-20 09:46:07 +00:00
{
whereClauses . Add ( "IsVirtualItem=@IsVirtualItem" ) ;
if ( statement ! = null )
{
2017-08-13 02:09:07 +00:00
statement . TryBind ( "@IsVirtualItem" , isVirtualItem . Value ) ;
2016-11-20 09:46:07 +00:00
}
}
2020-06-15 21:43:52 +00:00
2016-11-20 09:46:07 +00:00
if ( query . IsSpecialSeason . HasValue )
{
if ( query . IsSpecialSeason . Value )
{
whereClauses . Add ( "IndexNumber = 0" ) ;
}
else
{
whereClauses . Add ( "IndexNumber <> 0" ) ;
}
}
2020-06-15 21:43:52 +00:00
2016-11-20 09:46:07 +00:00
if ( query . IsUnaired . HasValue )
{
if ( query . IsUnaired . Value )
{
whereClauses . Add ( "PremiereDate >= DATETIME('now')" ) ;
}
else
{
whereClauses . Add ( "PremiereDate < DATETIME('now')" ) ;
}
}
2020-06-15 21:43:52 +00:00
2017-03-19 18:59:05 +00:00
var queryMediaTypes = query . MediaTypes . Where ( IsValidMediaType ) . ToArray ( ) ;
if ( queryMediaTypes . Length = = 1 )
2016-11-20 09:46:07 +00:00
{
whereClauses . Add ( "MediaType=@MediaTypes" ) ;
if ( statement ! = null )
{
2017-03-19 18:59:05 +00:00
statement . TryBind ( "@MediaTypes" , queryMediaTypes [ 0 ] ) ;
2016-11-20 09:46:07 +00:00
}
}
2017-03-19 18:59:05 +00:00
else if ( queryMediaTypes . Length > 1 )
2016-11-20 09:46:07 +00:00
{
2019-01-26 17:21:41 +00:00
var val = string . Join ( "," , queryMediaTypes . Select ( i = > "'" + i + "'" ) ) ;
2016-11-20 09:46:07 +00:00
whereClauses . Add ( "MediaType in (" + val + ")" ) ;
}
2020-06-15 21:43:52 +00:00
2016-11-20 09:46:07 +00:00
if ( query . ItemIds . Length > 0 )
{
var includeIds = new List < string > ( ) ;
var index = 0 ;
foreach ( var id in query . ItemIds )
{
includeIds . Add ( "Guid = @IncludeId" + index ) ;
if ( statement ! = null )
{
2018-09-12 17:26:21 +00:00
statement . TryBind ( "@IncludeId" + index , id ) ;
2016-11-20 09:46:07 +00:00
}
2020-06-15 21:43:52 +00:00
2016-11-20 09:46:07 +00:00
index + + ;
}
2019-01-26 17:21:41 +00:00
whereClauses . Add ( "(" + string . Join ( " OR " , includeIds ) + ")" ) ;
2016-11-20 09:46:07 +00:00
}
2020-06-15 21:43:52 +00:00
2016-11-20 09:46:07 +00:00
if ( query . ExcludeItemIds . Length > 0 )
{
var excludeIds = new List < string > ( ) ;
var index = 0 ;
foreach ( var id in query . ExcludeItemIds )
{
excludeIds . Add ( "Guid <> @ExcludeId" + index ) ;
if ( statement ! = null )
{
2018-09-12 17:26:21 +00:00
statement . TryBind ( "@ExcludeId" + index , id ) ;
2016-11-20 09:46:07 +00:00
}
2020-06-15 21:43:52 +00:00
2016-11-20 09:46:07 +00:00
index + + ;
}
2019-01-26 17:21:41 +00:00
whereClauses . Add ( string . Join ( " AND " , excludeIds ) ) ;
2016-11-20 09:46:07 +00:00
}
if ( query . ExcludeProviderIds . Count > 0 )
{
var excludeIds = new List < string > ( ) ;
var index = 0 ;
foreach ( var pair in query . ExcludeProviderIds )
{
2020-06-06 19:17:49 +00:00
if ( string . Equals ( pair . Key , MetadataProvider . TmdbCollection . ToString ( ) , StringComparison . OrdinalIgnoreCase ) )
2016-11-20 09:46:07 +00:00
{
continue ;
}
var paramName = "@ExcludeProviderId" + index ;
2017-01-20 18:30:22 +00:00
excludeIds . Add ( "(ProviderIds is null or ProviderIds not like " + paramName + ")" ) ;
2016-11-20 09:46:07 +00:00
if ( statement ! = null )
{
statement . TryBind ( paramName , "%" + pair . Key + "=" + pair . Value + "%" ) ;
}
2020-06-15 21:43:52 +00:00
2016-11-20 09:46:07 +00:00
index + + ;
break ;
}
2018-09-12 17:26:21 +00:00
if ( excludeIds . Count > 0 )
{
2019-01-26 17:21:41 +00:00
whereClauses . Add ( string . Join ( " AND " , excludeIds ) ) ;
2018-09-12 17:26:21 +00:00
}
}
if ( query . HasAnyProviderId . Count > 0 )
{
var hasProviderIds = new List < string > ( ) ;
var index = 0 ;
foreach ( var pair in query . HasAnyProviderId )
{
2020-06-06 19:17:49 +00:00
if ( string . Equals ( pair . Key , MetadataProvider . TmdbCollection . ToString ( ) , StringComparison . OrdinalIgnoreCase ) )
2018-09-12 17:26:21 +00:00
{
continue ;
}
2019-01-22 12:20:36 +00:00
// TODO this seems to be an idea for a better schema where ProviderIds are their own table
// buut this is not implemented
2020-06-14 09:11:11 +00:00
// hasProviderIds.Add("(COALESCE((select value from ProviderIds where ItemId=Guid and Name = '" + pair.Key + "'), '') <> " + paramName + ")");
2019-01-22 12:20:36 +00:00
// TODO this is a really BAD way to do it since the pair:
// Tmdb, 1234 matches Tmdb=1234 but also Tmdb=1234567
// and maybe even NotTmdb=1234.
// this is a placeholder for this specific pair to correlate it in the bigger query
var paramName = "@HasAnyProviderId" + index ;
// this is a search for the placeholder
2018-09-12 17:26:21 +00:00
hasProviderIds . Add ( "ProviderIds like " + paramName + "" ) ;
2019-01-22 12:20:36 +00:00
// this replaces the placeholder with a value, here: %key=val%
2018-09-12 17:26:21 +00:00
if ( statement ! = null )
{
statement . TryBind ( paramName , "%" + pair . Key + "=" + pair . Value + "%" ) ;
}
2020-06-15 21:43:52 +00:00
2018-09-12 17:26:21 +00:00
index + + ;
break ;
}
if ( hasProviderIds . Count > 0 )
{
2019-01-26 17:21:41 +00:00
whereClauses . Add ( "(" + string . Join ( " OR " , hasProviderIds ) + ")" ) ;
2018-09-12 17:26:21 +00:00
}
2016-11-20 09:46:07 +00:00
}
if ( query . HasImdbId . HasValue )
{
whereClauses . Add ( "ProviderIds like '%imdb=%'" ) ;
}
if ( query . HasTmdbId . HasValue )
{
whereClauses . Add ( "ProviderIds like '%tmdb=%'" ) ;
}
if ( query . HasTvdbId . HasValue )
{
whereClauses . Add ( "ProviderIds like '%tvdb=%'" ) ;
}
2017-07-25 18:32:03 +00:00
var includedItemByNameTypes = GetItemByNameTypesInQuery ( query ) . SelectMany ( MapIncludeItemTypes ) . ToList ( ) ;
var enableItemsByName = ( query . IncludeItemsByName ? ? false ) & & includedItemByNameTypes . Count > 0 ;
2016-11-20 09:46:07 +00:00
2018-09-12 17:26:21 +00:00
var queryTopParentIds = query . TopParentIds ;
2017-03-19 18:59:05 +00:00
if ( queryTopParentIds . Length = = 1 )
2016-11-20 09:46:07 +00:00
{
2017-07-25 18:32:03 +00:00
if ( enableItemsByName & & includedItemByNameTypes . Count = = 1 )
2016-11-20 09:46:07 +00:00
{
2017-07-25 18:32:03 +00:00
whereClauses . Add ( "(TopParentId=@TopParentId or Type=@IncludedItemByNameType)" ) ;
2016-11-20 09:46:07 +00:00
if ( statement ! = null )
{
2017-07-25 18:32:03 +00:00
statement . TryBind ( "@IncludedItemByNameType" , includedItemByNameTypes [ 0 ] ) ;
2016-11-20 09:46:07 +00:00
}
}
2017-07-25 18:32:03 +00:00
else if ( enableItemsByName & & includedItemByNameTypes . Count > 1 )
{
2019-01-26 17:21:41 +00:00
var itemByNameTypeVal = string . Join ( "," , includedItemByNameTypes . Select ( i = > "'" + i + "'" ) ) ;
2017-07-25 18:32:03 +00:00
whereClauses . Add ( "(TopParentId=@TopParentId or Type in (" + itemByNameTypeVal + "))" ) ;
}
2016-11-20 09:46:07 +00:00
else
{
whereClauses . Add ( "(TopParentId=@TopParentId)" ) ;
}
2020-06-15 21:43:52 +00:00
2016-11-20 09:46:07 +00:00
if ( statement ! = null )
{
2019-02-28 22:22:57 +00:00
statement . TryBind ( "@TopParentId" , queryTopParentIds [ 0 ] . ToString ( "N" , CultureInfo . InvariantCulture ) ) ;
2016-11-20 09:46:07 +00:00
}
}
2017-03-19 18:59:05 +00:00
else if ( queryTopParentIds . Length > 1 )
2016-11-20 09:46:07 +00:00
{
2019-02-28 22:22:57 +00:00
var val = string . Join ( "," , queryTopParentIds . Select ( i = > "'" + i . ToString ( "N" , CultureInfo . InvariantCulture ) + "'" ) ) ;
2016-11-20 09:46:07 +00:00
2017-07-25 18:32:03 +00:00
if ( enableItemsByName & & includedItemByNameTypes . Count = = 1 )
2016-11-20 09:46:07 +00:00
{
2017-07-25 18:32:03 +00:00
whereClauses . Add ( "(Type=@IncludedItemByNameType or TopParentId in (" + val + "))" ) ;
2016-11-20 09:46:07 +00:00
if ( statement ! = null )
{
2017-07-25 18:32:03 +00:00
statement . TryBind ( "@IncludedItemByNameType" , includedItemByNameTypes [ 0 ] ) ;
2016-11-20 09:46:07 +00:00
}
}
2017-07-25 18:32:03 +00:00
else if ( enableItemsByName & & includedItemByNameTypes . Count > 1 )
{
2019-01-26 17:21:41 +00:00
var itemByNameTypeVal = string . Join ( "," , includedItemByNameTypes . Select ( i = > "'" + i + "'" ) ) ;
2017-07-25 18:32:03 +00:00
whereClauses . Add ( "(Type in (" + itemByNameTypeVal + ") or TopParentId in (" + val + "))" ) ;
}
2016-11-20 09:46:07 +00:00
else
{
2019-01-26 17:21:41 +00:00
whereClauses . Add ( "TopParentId in (" + val + ")" ) ;
2016-11-20 09:46:07 +00:00
}
}
if ( query . AncestorIds . Length = = 1 )
{
whereClauses . Add ( "Guid in (select itemId from AncestorIds where AncestorId=@AncestorId)" ) ;
if ( statement ! = null )
{
2018-09-12 17:26:21 +00:00
statement . TryBind ( "@AncestorId" , query . AncestorIds [ 0 ] ) ;
2016-11-20 09:46:07 +00:00
}
}
2020-06-15 21:43:52 +00:00
2016-11-20 09:46:07 +00:00
if ( query . AncestorIds . Length > 1 )
{
2019-02-28 22:22:57 +00:00
var inClause = string . Join ( "," , query . AncestorIds . Select ( i = > "'" + i . ToString ( "N" , CultureInfo . InvariantCulture ) + "'" ) ) ;
2016-11-20 09:46:07 +00:00
whereClauses . Add ( string . Format ( "Guid in (select itemId from AncestorIds where AncestorIdText in ({0}))" , inClause ) ) ;
}
2020-06-15 21:43:52 +00:00
2016-11-20 09:46:07 +00:00
if ( ! string . IsNullOrWhiteSpace ( query . AncestorWithPresentationUniqueKey ) )
{
var inClause = "select guid from TypedBaseItems where PresentationUniqueKey=@AncestorWithPresentationUniqueKey" ;
whereClauses . Add ( string . Format ( "Guid in (select itemId from AncestorIds where AncestorId in ({0}))" , inClause ) ) ;
if ( statement ! = null )
{
statement . TryBind ( "@AncestorWithPresentationUniqueKey" , query . AncestorWithPresentationUniqueKey ) ;
2016-12-06 08:24:29 +00:00
}
}
if ( ! string . IsNullOrWhiteSpace ( query . SeriesPresentationUniqueKey ) )
{
whereClauses . Add ( "SeriesPresentationUniqueKey=@SeriesPresentationUniqueKey" ) ;
if ( statement ! = null )
{
statement . TryBind ( "@SeriesPresentationUniqueKey" , query . SeriesPresentationUniqueKey ) ;
2016-11-20 09:46:07 +00:00
}
}
if ( query . BlockUnratedItems . Length = = 1 )
{
whereClauses . Add ( "(InheritedParentalRatingValue > 0 or UnratedType <> @UnratedType)" ) ;
if ( statement ! = null )
{
statement . TryBind ( "@UnratedType" , query . BlockUnratedItems [ 0 ] . ToString ( ) ) ;
}
}
2020-06-15 21:43:52 +00:00
2016-11-20 09:46:07 +00:00
if ( query . BlockUnratedItems . Length > 1 )
{
2020-07-24 14:37:54 +00:00
var inClause = string . Join ( ',' , query . BlockUnratedItems . Select ( i = > "'" + i . ToString ( ) + "'" ) ) ;
whereClauses . Add (
string . Format (
CultureInfo . InvariantCulture ,
"(InheritedParentalRatingValue > 0 or UnratedType not in ({0}))" ,
inClause ) ) ;
2016-11-20 09:46:07 +00:00
}
2017-08-06 23:01:00 +00:00
if ( query . ExcludeInheritedTags . Length > 0 )
2016-11-20 09:46:07 +00:00
{
2019-12-13 10:29:38 +00:00
var paramName = "@ExcludeInheritedTags" ;
if ( statement = = null )
{
2019-12-14 17:48:30 +00:00
int index = 0 ;
2020-07-24 14:37:54 +00:00
string excludedTags = string . Join ( ',' , query . ExcludeInheritedTags . Select ( t = > paramName + index + + ) ) ;
2019-12-14 17:48:30 +00:00
whereClauses . Add ( "((select CleanValue from itemvalues where ItemId=Guid and Type=6 and cleanvalue in (" + excludedTags + ")) is null)" ) ;
2019-12-13 10:29:38 +00:00
}
else
{
for ( int index = 0 ; index < query . ExcludeInheritedTags . Length ; index + + )
{
2019-12-13 13:45:04 +00:00
statement . TryBind ( paramName + index , GetCleanValue ( query . ExcludeInheritedTags [ index ] ) ) ;
2019-12-13 10:29:38 +00:00
}
}
2016-11-20 09:46:07 +00:00
}
2018-09-12 17:26:21 +00:00
if ( query . SeriesStatuses . Length > 0 )
{
var statuses = new List < string > ( ) ;
foreach ( var seriesStatus in query . SeriesStatuses )
{
statuses . Add ( "data like '%" + seriesStatus + "%'" ) ;
}
2019-01-26 17:21:41 +00:00
whereClauses . Add ( "(" + string . Join ( " OR " , statuses ) + ")" ) ;
2018-09-12 17:26:21 +00:00
}
if ( query . BoxSetLibraryFolders . Length > 0 )
{
var folderIdQueries = new List < string > ( ) ;
foreach ( var folderId in query . BoxSetLibraryFolders )
{
2019-02-28 22:22:57 +00:00
folderIdQueries . Add ( "data like '%" + folderId . ToString ( "N" , CultureInfo . InvariantCulture ) + "%'" ) ;
2018-09-12 17:26:21 +00:00
}
2019-01-26 17:21:41 +00:00
whereClauses . Add ( "(" + string . Join ( " OR " , folderIdQueries ) + ")" ) ;
2018-09-12 17:26:21 +00:00
}
if ( query . VideoTypes . Length > 0 )
{
var videoTypes = new List < string > ( ) ;
foreach ( var videoType in query . VideoTypes )
{
videoTypes . Add ( "data like '%\"VideoType\":\"" + videoType . ToString ( ) + "\"%'" ) ;
}
2019-01-26 17:21:41 +00:00
whereClauses . Add ( "(" + string . Join ( " OR " , videoTypes ) + ")" ) ;
2018-09-12 17:26:21 +00:00
}
if ( query . Is3D . HasValue )
{
if ( query . Is3D . Value )
{
whereClauses . Add ( "data like '%Video3DFormat%'" ) ;
}
else
{
whereClauses . Add ( "data not like '%Video3DFormat%'" ) ;
}
}
if ( query . IsPlaceHolder . HasValue )
{
if ( query . IsPlaceHolder . Value )
{
whereClauses . Add ( "data like '%\"IsPlaceHolder\":true%'" ) ;
}
else
{
whereClauses . Add ( "(data is null or data not like '%\"IsPlaceHolder\":true%')" ) ;
}
}
if ( query . HasSpecialFeature . HasValue )
{
if ( query . HasSpecialFeature . Value )
{
whereClauses . Add ( "ExtraIds not null" ) ;
}
else
{
whereClauses . Add ( "ExtraIds is null" ) ;
}
}
if ( query . HasTrailer . HasValue )
{
if ( query . HasTrailer . Value )
{
whereClauses . Add ( "ExtraIds not null" ) ;
}
else
{
whereClauses . Add ( "ExtraIds is null" ) ;
}
}
if ( query . HasThemeSong . HasValue )
{
if ( query . HasThemeSong . Value )
{
whereClauses . Add ( "ExtraIds not null" ) ;
}
else
{
whereClauses . Add ( "ExtraIds is null" ) ;
}
}
if ( query . HasThemeVideo . HasValue )
{
if ( query . HasThemeVideo . Value )
{
whereClauses . Add ( "ExtraIds not null" ) ;
}
else
{
whereClauses . Add ( "ExtraIds is null" ) ;
}
}
2016-11-20 09:46:07 +00:00
return whereClauses ;
}
2017-07-25 18:32:03 +00:00
private List < string > GetItemByNameTypesInQuery ( InternalItemsQuery query )
{
var list = new List < string > ( ) ;
if ( IsTypeInQuery ( typeof ( Person ) . Name , query ) )
{
list . Add ( typeof ( Person ) . Name ) ;
}
2020-04-03 18:59:38 +00:00
2017-07-25 18:32:03 +00:00
if ( IsTypeInQuery ( typeof ( Genre ) . Name , query ) )
{
list . Add ( typeof ( Genre ) . Name ) ;
}
2020-04-03 18:59:38 +00:00
2017-07-25 18:32:03 +00:00
if ( IsTypeInQuery ( typeof ( MusicGenre ) . Name , query ) )
{
list . Add ( typeof ( MusicGenre ) . Name ) ;
}
2020-04-03 18:59:38 +00:00
2017-07-25 18:32:03 +00:00
if ( IsTypeInQuery ( typeof ( MusicArtist ) . Name , query ) )
{
list . Add ( typeof ( MusicArtist ) . Name ) ;
}
2020-04-03 18:59:38 +00:00
2017-07-25 18:32:03 +00:00
if ( IsTypeInQuery ( typeof ( Studio ) . Name , query ) )
{
list . Add ( typeof ( Studio ) . Name ) ;
}
return list ;
}
private bool IsTypeInQuery ( string type , InternalItemsQuery query )
{
if ( query . ExcludeItemTypes . Contains ( type , StringComparer . OrdinalIgnoreCase ) )
{
return false ;
}
return query . IncludeItemTypes . Length = = 0 | | query . IncludeItemTypes . Contains ( type , StringComparer . OrdinalIgnoreCase ) ;
}
2016-11-20 09:46:07 +00:00
private string GetCleanValue ( string value )
{
if ( string . IsNullOrWhiteSpace ( value ) )
{
return value ;
}
2019-01-27 11:03:43 +00:00
return value . RemoveDiacritics ( ) . ToLowerInvariant ( ) ;
2016-11-20 09:46:07 +00:00
}
private bool EnableGroupByPresentationUniqueKey ( InternalItemsQuery query )
{
if ( ! query . GroupByPresentationUniqueKey )
{
return false ;
}
2017-08-01 16:45:57 +00:00
if ( query . GroupBySeriesPresentationUniqueKey )
{
return false ;
}
2016-11-20 09:46:07 +00:00
if ( ! string . IsNullOrWhiteSpace ( query . PresentationUniqueKey ) )
{
return false ;
}
if ( query . User = = null )
{
return false ;
}
if ( query . IncludeItemTypes . Length = = 0 )
{
return true ;
}
2019-01-26 17:21:41 +00:00
var types = new [ ]
{
2016-11-20 09:46:07 +00:00
typeof ( Episode ) . Name ,
2019-01-26 17:21:41 +00:00
typeof ( Video ) . Name ,
typeof ( Movie ) . Name ,
typeof ( MusicVideo ) . Name ,
typeof ( Series ) . Name ,
typeof ( Season ) . Name
} ;
2016-11-20 09:46:07 +00:00
if ( types . Any ( i = > query . IncludeItemTypes . Contains ( i , StringComparer . OrdinalIgnoreCase ) ) )
{
return true ;
}
return false ;
}
2020-04-03 18:59:38 +00:00
private static readonly Type [ ] _knownTypes =
2016-11-20 09:46:07 +00:00
{
typeof ( LiveTvProgram ) ,
typeof ( LiveTvChannel ) ,
typeof ( Series ) ,
typeof ( Audio ) ,
typeof ( MusicAlbum ) ,
typeof ( MusicArtist ) ,
typeof ( MusicGenre ) ,
typeof ( MusicVideo ) ,
typeof ( Movie ) ,
typeof ( Playlist ) ,
2016-12-12 05:49:19 +00:00
typeof ( AudioBook ) ,
2016-11-20 09:46:07 +00:00
typeof ( Trailer ) ,
typeof ( BoxSet ) ,
typeof ( Episode ) ,
typeof ( Season ) ,
typeof ( Series ) ,
typeof ( Book ) ,
typeof ( CollectionFolder ) ,
typeof ( Folder ) ,
typeof ( Genre ) ,
typeof ( Person ) ,
typeof ( Photo ) ,
typeof ( PhotoAlbum ) ,
typeof ( Studio ) ,
typeof ( UserRootFolder ) ,
typeof ( UserView ) ,
typeof ( Video ) ,
typeof ( Year ) ,
typeof ( Channel ) ,
typeof ( AggregateFolder )
} ;
2017-08-27 00:32:33 +00:00
public void UpdateInheritedValues ( CancellationToken cancellationToken )
2016-11-20 09:46:07 +00:00
{
2017-09-15 06:32:20 +00:00
UpdateInheritedTags ( cancellationToken ) ;
}
private void UpdateInheritedTags ( CancellationToken cancellationToken )
{
2019-08-18 20:05:06 +00:00
string sql = string . Join (
";" ,
new string [ ]
2017-09-15 06:32:20 +00:00
{
2019-08-18 20:05:06 +00:00
"delete from itemvalues where type = 6" ,
2017-09-15 06:32:20 +00:00
2019-08-18 20:05:06 +00:00
"insert into itemvalues (ItemId, Type, Value, CleanValue) select ItemId, 6, Value, CleanValue from ItemValues where Type=4" ,
2017-09-15 06:32:20 +00:00
2019-08-18 20:05:06 +00:00
@ "insert into itemvalues (ItemId, Type, Value, CleanValue) select AncestorIds.itemid, 6, ItemValues.Value, ItemValues.CleanValue
2017-09-15 06:32:20 +00:00
FROM AncestorIds
LEFT JOIN ItemValues ON ( AncestorIds . AncestorId = ItemValues . ItemId )
where AncestorIdText not null and ItemValues . Value not null and ItemValues . Type = 4 "
2019-08-18 20:05:06 +00:00
} ) ;
2017-09-15 06:32:20 +00:00
2019-08-18 20:05:06 +00:00
using ( var connection = GetConnection ( ) )
{
connection . RunInTransaction ( db = >
{
connection . ExecuteAll ( sql ) ;
2019-02-20 13:26:49 +00:00
} , TransactionMode ) ;
2017-09-15 06:32:20 +00:00
}
2016-11-20 09:46:07 +00:00
}
private static Dictionary < string , string [ ] > GetTypeMapDictionary ( )
{
var dict = new Dictionary < string , string [ ] > ( StringComparer . OrdinalIgnoreCase ) ;
2020-04-03 18:59:38 +00:00
foreach ( var t in _knownTypes )
2016-11-20 09:46:07 +00:00
{
dict [ t . Name ] = new [ ] { t . FullName } ;
}
dict [ "Program" ] = new [ ] { typeof ( LiveTvProgram ) . FullName } ;
dict [ "TvChannel" ] = new [ ] { typeof ( LiveTvChannel ) . FullName } ;
return dict ;
}
// Not crazy about having this all the way down here, but at least it's in one place
2020-04-03 18:59:38 +00:00
private readonly Dictionary < string , string [ ] > _types = GetTypeMapDictionary ( ) ;
2016-11-20 09:46:07 +00:00
2020-02-19 20:56:35 +00:00
private string [ ] MapIncludeItemTypes ( string value )
2016-11-20 09:46:07 +00:00
{
2019-01-17 17:47:41 +00:00
if ( _types . TryGetValue ( value , out string [ ] result ) )
2016-11-20 09:46:07 +00:00
{
return result ;
}
2019-02-08 21:59:28 +00:00
if ( IsValidType ( value ) )
{
return new [ ] { value } ;
}
return Array . Empty < string > ( ) ;
2016-11-20 09:46:07 +00:00
}
2020-04-03 18:59:38 +00:00
public void DeleteItem ( Guid id )
2016-11-20 09:46:07 +00:00
{
2019-01-26 17:21:41 +00:00
if ( id = = Guid . Empty )
2016-11-20 09:46:07 +00:00
{
2019-01-06 20:50:43 +00:00
throw new ArgumentNullException ( nameof ( id ) ) ;
2016-11-20 09:46:07 +00:00
}
CheckDisposed ( ) ;
2019-02-26 17:30:13 +00:00
using ( var connection = GetConnection ( ) )
2016-11-20 09:46:07 +00:00
{
2019-02-20 13:26:49 +00:00
connection . RunInTransaction ( db = >
2016-11-20 09:46:07 +00:00
{
2019-09-25 11:24:39 +00:00
var idBlob = id . ToByteArray ( ) ;
2018-09-12 17:26:21 +00:00
2019-08-18 20:05:06 +00:00
// Delete people
ExecuteWithSingleParam ( db , "delete from People where ItemId=@Id" , idBlob ) ;
2016-11-20 09:46:07 +00:00
2019-08-18 20:05:06 +00:00
// Delete chapters
ExecuteWithSingleParam ( db , "delete from " + ChaptersTableName + " where ItemId=@Id" , idBlob ) ;
2016-11-20 09:46:07 +00:00
2019-08-18 20:05:06 +00:00
// Delete media streams
ExecuteWithSingleParam ( db , "delete from mediastreams where ItemId=@Id" , idBlob ) ;
2016-11-20 09:46:07 +00:00
2019-08-18 20:05:06 +00:00
// Delete ancestors
ExecuteWithSingleParam ( db , "delete from AncestorIds where ItemId=@Id" , idBlob ) ;
2016-11-20 09:46:07 +00:00
2019-08-18 20:05:06 +00:00
// Delete item values
ExecuteWithSingleParam ( db , "delete from ItemValues where ItemId=@Id" , idBlob ) ;
2016-11-20 09:46:07 +00:00
2019-08-18 20:05:06 +00:00
// Delete the item
ExecuteWithSingleParam ( db , "delete from TypedBaseItems where guid=@Id" , idBlob ) ;
2019-02-20 13:26:49 +00:00
} , TransactionMode ) ;
2016-11-20 09:46:07 +00:00
}
}
2020-04-03 18:59:38 +00:00
private void ExecuteWithSingleParam ( IDatabaseConnection db , string query , ReadOnlySpan < byte > value )
2016-11-20 09:46:07 +00:00
{
2016-12-11 05:12:00 +00:00
using ( var statement = PrepareStatement ( db , query ) )
2016-11-20 09:46:07 +00:00
{
statement . TryBind ( "@Id" , value ) ;
statement . MoveNext ( ) ;
}
}
public List < string > GetPeopleNames ( InternalPeopleQuery query )
{
if ( query = = null )
{
2019-01-06 20:50:43 +00:00
throw new ArgumentNullException ( nameof ( query ) ) ;
2016-11-20 09:46:07 +00:00
}
CheckDisposed ( ) ;
var commandText = "select Distinct Name from People" ;
var whereClauses = GetPeopleWhereClauses ( query , null ) ;
2019-01-26 17:21:41 +00:00
if ( whereClauses . Count ! = 0 )
2016-11-20 09:46:07 +00:00
{
2019-01-26 17:21:41 +00:00
commandText + = " where " + string . Join ( " AND " , whereClauses ) ;
2016-11-20 09:46:07 +00:00
}
commandText + = " order by ListOrder" ;
2020-04-03 15:30:01 +00:00
if ( query . Limit > 0 )
{
2020-04-03 21:13:04 +00:00
commandText + = " LIMIT " + query . Limit ;
2020-04-03 15:30:01 +00:00
}
2019-02-26 17:30:13 +00:00
using ( var connection = GetConnection ( true ) )
2016-11-20 09:46:07 +00:00
{
2019-02-20 13:26:49 +00:00
var list = new List < string > ( ) ;
2019-04-02 20:15:18 +00:00
using ( var statement = PrepareStatement ( connection , commandText ) )
2016-11-20 09:46:07 +00:00
{
2019-02-20 13:26:49 +00:00
// Run this again to bind the params
GetPeopleWhereClauses ( query , statement ) ;
2016-12-12 05:49:19 +00:00
2019-02-20 13:26:49 +00:00
foreach ( var row in statement . ExecuteQuery ( ) )
{
list . Add ( row . GetString ( 0 ) ) ;
2016-12-13 15:44:34 +00:00
}
2016-11-20 09:46:07 +00:00
}
2019-08-18 20:05:06 +00:00
2019-02-20 13:26:49 +00:00
return list ;
2016-11-20 09:46:07 +00:00
}
}
public List < PersonInfo > GetPeople ( InternalPeopleQuery query )
{
if ( query = = null )
{
2019-01-06 20:50:43 +00:00
throw new ArgumentNullException ( nameof ( query ) ) ;
2016-11-20 09:46:07 +00:00
}
CheckDisposed ( ) ;
var commandText = "select ItemId, Name, Role, PersonType, SortOrder from People" ;
var whereClauses = GetPeopleWhereClauses ( query , null ) ;
2019-01-26 17:21:41 +00:00
if ( whereClauses . Count ! = 0 )
2016-11-20 09:46:07 +00:00
{
2019-01-26 17:21:41 +00:00
commandText + = " where " + string . Join ( " AND " , whereClauses ) ;
2016-11-20 09:46:07 +00:00
}
commandText + = " order by ListOrder" ;
2020-04-03 15:30:01 +00:00
if ( query . Limit > 0 )
{
2020-04-03 20:41:14 +00:00
commandText + = " LIMIT " + query . Limit ;
2020-04-03 15:30:01 +00:00
}
2019-02-26 17:30:13 +00:00
using ( var connection = GetConnection ( true ) )
2016-11-20 09:46:07 +00:00
{
2019-02-20 13:26:49 +00:00
var list = new List < PersonInfo > ( ) ;
2019-04-02 20:15:18 +00:00
using ( var statement = PrepareStatement ( connection , commandText ) )
2016-11-20 09:46:07 +00:00
{
2019-02-20 13:26:49 +00:00
// Run this again to bind the params
GetPeopleWhereClauses ( query , statement ) ;
2016-12-13 15:44:34 +00:00
2019-02-20 13:26:49 +00:00
foreach ( var row in statement . ExecuteQuery ( ) )
2016-11-20 09:46:07 +00:00
{
2019-02-20 13:26:49 +00:00
list . Add ( GetPerson ( row ) ) ;
2016-12-13 15:44:34 +00:00
}
2016-11-20 09:46:07 +00:00
}
2019-02-20 13:26:49 +00:00
return list ;
2016-11-20 09:46:07 +00:00
}
}
private List < string > GetPeopleWhereClauses ( InternalPeopleQuery query , IStatement statement )
{
var whereClauses = new List < string > ( ) ;
2018-09-12 17:26:21 +00:00
if ( ! query . ItemId . Equals ( Guid . Empty ) )
2016-11-20 09:46:07 +00:00
{
whereClauses . Add ( "ItemId=@ItemId" ) ;
if ( statement ! = null )
{
2019-09-25 11:24:39 +00:00
statement . TryBind ( "@ItemId" , query . ItemId . ToByteArray ( ) ) ;
2016-11-20 09:46:07 +00:00
}
}
2020-06-15 21:43:52 +00:00
2018-09-12 17:26:21 +00:00
if ( ! query . AppearsInItemId . Equals ( Guid . Empty ) )
2016-11-20 09:46:07 +00:00
{
whereClauses . Add ( "Name in (Select Name from People where ItemId=@AppearsInItemId)" ) ;
if ( statement ! = null )
{
2019-09-25 11:24:39 +00:00
statement . TryBind ( "@AppearsInItemId" , query . AppearsInItemId . ToByteArray ( ) ) ;
2016-11-20 09:46:07 +00:00
}
}
2020-06-15 21:43:52 +00:00
2017-03-19 18:59:05 +00:00
var queryPersonTypes = query . PersonTypes . Where ( IsValidPersonType ) . ToList ( ) ;
if ( queryPersonTypes . Count = = 1 )
2016-11-20 09:46:07 +00:00
{
whereClauses . Add ( "PersonType=@PersonType" ) ;
if ( statement ! = null )
{
2017-03-19 18:59:05 +00:00
statement . TryBind ( "@PersonType" , queryPersonTypes [ 0 ] ) ;
2016-11-20 09:46:07 +00:00
}
}
2017-03-19 18:59:05 +00:00
else if ( queryPersonTypes . Count > 1 )
2016-11-20 09:46:07 +00:00
{
2019-01-26 17:21:41 +00:00
var val = string . Join ( "," , queryPersonTypes . Select ( i = > "'" + i + "'" ) ) ;
2016-11-20 09:46:07 +00:00
whereClauses . Add ( "PersonType in (" + val + ")" ) ;
}
2020-06-15 21:43:52 +00:00
2017-03-19 18:59:05 +00:00
var queryExcludePersonTypes = query . ExcludePersonTypes . Where ( IsValidPersonType ) . ToList ( ) ;
if ( queryExcludePersonTypes . Count = = 1 )
2016-11-20 09:46:07 +00:00
{
whereClauses . Add ( "PersonType<>@PersonType" ) ;
if ( statement ! = null )
{
2017-03-19 18:59:05 +00:00
statement . TryBind ( "@PersonType" , queryExcludePersonTypes [ 0 ] ) ;
2016-11-20 09:46:07 +00:00
}
}
2017-03-19 18:59:05 +00:00
else if ( queryExcludePersonTypes . Count > 1 )
2016-11-20 09:46:07 +00:00
{
2019-01-26 17:21:41 +00:00
var val = string . Join ( "," , queryExcludePersonTypes . Select ( i = > "'" + i + "'" ) ) ;
2016-11-20 09:46:07 +00:00
whereClauses . Add ( "PersonType not in (" + val + ")" ) ;
}
2020-06-15 21:43:52 +00:00
2016-11-20 09:46:07 +00:00
if ( query . MaxListOrder . HasValue )
{
whereClauses . Add ( "ListOrder<=@MaxListOrder" ) ;
if ( statement ! = null )
{
statement . TryBind ( "@MaxListOrder" , query . MaxListOrder . Value ) ;
}
}
2020-06-15 21:43:52 +00:00
2016-11-20 09:46:07 +00:00
if ( ! string . IsNullOrWhiteSpace ( query . NameContains ) )
{
whereClauses . Add ( "Name like @NameContains" ) ;
if ( statement ! = null )
{
statement . TryBind ( "@NameContains" , "%" + query . NameContains + "%" ) ;
}
}
return whereClauses ;
}
2018-09-12 17:26:21 +00:00
private void UpdateAncestors ( Guid itemId , List < Guid > ancestorIds , IDatabaseConnection db , IStatement deleteAncestorsStatement )
2016-11-20 09:46:07 +00:00
{
2018-09-12 17:26:21 +00:00
if ( itemId . Equals ( Guid . Empty ) )
2016-11-20 09:46:07 +00:00
{
2019-01-06 20:50:43 +00:00
throw new ArgumentNullException ( nameof ( itemId ) ) ;
2016-11-20 09:46:07 +00:00
}
if ( ancestorIds = = null )
{
2019-01-06 20:50:43 +00:00
throw new ArgumentNullException ( nameof ( ancestorIds ) ) ;
2016-11-20 09:46:07 +00:00
}
CheckDisposed ( ) ;
2019-09-25 11:24:39 +00:00
var itemIdBlob = itemId . ToByteArray ( ) ;
2018-09-12 17:26:21 +00:00
2019-01-07 23:27:46 +00:00
// First delete
2016-11-20 09:46:07 +00:00
deleteAncestorsStatement . Reset ( ) ;
2018-09-12 17:26:21 +00:00
deleteAncestorsStatement . TryBind ( "@ItemId" , itemIdBlob ) ;
2016-11-20 09:46:07 +00:00
deleteAncestorsStatement . MoveNext ( ) ;
2018-09-12 17:26:21 +00:00
if ( ancestorIds . Count = = 0 )
{
return ;
}
var insertText = new StringBuilder ( "insert into AncestorIds (ItemId, AncestorId, AncestorIdText) values " ) ;
for ( var i = 0 ; i < ancestorIds . Count ; i + + )
{
if ( i > 0 )
{
2020-07-22 12:34:51 +00:00
insertText . Append ( ',' ) ;
2018-09-12 17:26:21 +00:00
}
insertText . AppendFormat ( "(@ItemId, @AncestorId{0}, @AncestorIdText{0})" , i . ToString ( CultureInfo . InvariantCulture ) ) ;
}
2019-04-02 20:15:18 +00:00
using ( var statement = PrepareStatement ( db , insertText . ToString ( ) ) )
2016-11-20 09:46:07 +00:00
{
2018-09-12 17:26:21 +00:00
statement . TryBind ( "@ItemId" , itemIdBlob ) ;
for ( var i = 0 ; i < ancestorIds . Count ; i + + )
{
var index = i . ToString ( CultureInfo . InvariantCulture ) ;
var ancestorId = ancestorIds [ i ] ;
2019-09-25 11:24:39 +00:00
statement . TryBind ( "@AncestorId" + index , ancestorId . ToByteArray ( ) ) ;
2019-02-28 22:22:57 +00:00
statement . TryBind ( "@AncestorIdText" + index , ancestorId . ToString ( "N" , CultureInfo . InvariantCulture ) ) ;
2018-09-12 17:26:21 +00:00
}
statement . Reset ( ) ;
statement . MoveNext ( ) ;
2016-11-20 09:46:07 +00:00
}
}
2019-02-08 23:48:09 +00:00
public QueryResult < ( BaseItem , ItemCounts ) > GetAllArtists ( InternalItemsQuery query )
2016-11-20 09:46:07 +00:00
{
return GetItemValues ( query , new [ ] { 0 , 1 } , typeof ( MusicArtist ) . FullName ) ;
}
2019-02-08 23:48:09 +00:00
public QueryResult < ( BaseItem , ItemCounts ) > GetArtists ( InternalItemsQuery query )
2016-11-20 09:46:07 +00:00
{
return GetItemValues ( query , new [ ] { 0 } , typeof ( MusicArtist ) . FullName ) ;
}
2019-02-08 23:48:09 +00:00
public QueryResult < ( BaseItem , ItemCounts ) > GetAlbumArtists ( InternalItemsQuery query )
2016-11-20 09:46:07 +00:00
{
return GetItemValues ( query , new [ ] { 1 } , typeof ( MusicArtist ) . FullName ) ;
}
2019-02-08 23:48:09 +00:00
public QueryResult < ( BaseItem , ItemCounts ) > GetStudios ( InternalItemsQuery query )
2016-11-20 09:46:07 +00:00
{
return GetItemValues ( query , new [ ] { 3 } , typeof ( Studio ) . FullName ) ;
}
2019-02-08 23:48:09 +00:00
public QueryResult < ( BaseItem , ItemCounts ) > GetGenres ( InternalItemsQuery query )
2016-11-20 09:46:07 +00:00
{
return GetItemValues ( query , new [ ] { 2 } , typeof ( Genre ) . FullName ) ;
}
2019-02-08 23:48:09 +00:00
public QueryResult < ( BaseItem , ItemCounts ) > GetMusicGenres ( InternalItemsQuery query )
2016-11-20 09:46:07 +00:00
{
return GetItemValues ( query , new [ ] { 2 } , typeof ( MusicGenre ) . FullName ) ;
}
public List < string > GetStudioNames ( )
{
return GetItemValueNames ( new [ ] { 3 } , new List < string > ( ) , new List < string > ( ) ) ;
}
public List < string > GetAllArtistNames ( )
{
return GetItemValueNames ( new [ ] { 0 , 1 } , new List < string > ( ) , new List < string > ( ) ) ;
}
public List < string > GetMusicGenreNames ( )
{
return GetItemValueNames ( new [ ] { 2 } , new List < string > { "Audio" , "MusicVideo" , "MusicAlbum" , "MusicArtist" } , new List < string > ( ) ) ;
}
public List < string > GetGenreNames ( )
{
2019-01-28 17:49:25 +00:00
return GetItemValueNames ( new [ ] { 2 } , new List < string > ( ) , new List < string > { "Audio" , "MusicVideo" , "MusicAlbum" , "MusicArtist" } ) ;
2016-11-20 09:46:07 +00:00
}
private List < string > GetItemValueNames ( int [ ] itemValueTypes , List < string > withItemTypes , List < string > excludeItemTypes )
{
CheckDisposed ( ) ;
withItemTypes = withItemTypes . SelectMany ( MapIncludeItemTypes ) . ToList ( ) ;
excludeItemTypes = excludeItemTypes . SelectMany ( MapIncludeItemTypes ) . ToList ( ) ;
var now = DateTime . UtcNow ;
var typeClause = itemValueTypes . Length = = 1 ?
( "Type=" + itemValueTypes [ 0 ] . ToString ( CultureInfo . InvariantCulture ) ) :
2019-01-26 17:21:41 +00:00
( "Type in (" + string . Join ( "," , itemValueTypes . Select ( i = > i . ToString ( CultureInfo . InvariantCulture ) ) ) + ")" ) ;
2016-11-20 09:46:07 +00:00
var commandText = "Select Value From ItemValues where " + typeClause ;
if ( withItemTypes . Count > 0 )
{
2019-01-26 17:21:41 +00:00
var typeString = string . Join ( "," , withItemTypes . Select ( i = > "'" + i + "'" ) ) ;
2016-11-20 09:46:07 +00:00
commandText + = " AND ItemId In (select guid from typedbaseitems where type in (" + typeString + "))" ;
}
2020-06-15 21:43:52 +00:00
2016-11-20 09:46:07 +00:00
if ( excludeItemTypes . Count > 0 )
{
2019-01-26 17:21:41 +00:00
var typeString = string . Join ( "," , excludeItemTypes . Select ( i = > "'" + i + "'" ) ) ;
2016-11-20 09:46:07 +00:00
commandText + = " AND ItemId not In (select guid from typedbaseitems where type in (" + typeString + "))" ;
}
commandText + = " Group By CleanValue" ;
2019-08-18 20:05:06 +00:00
var list = new List < string > ( ) ;
2019-02-26 17:30:13 +00:00
using ( var connection = GetConnection ( true ) )
2016-11-20 09:46:07 +00:00
{
2019-04-02 20:15:18 +00:00
using ( var statement = PrepareStatement ( connection , commandText ) )
2019-02-20 13:26:49 +00:00
{
foreach ( var row in statement . ExecuteQuery ( ) )
2016-12-13 15:44:34 +00:00
{
2019-02-20 13:26:49 +00:00
if ( ! row . IsDBNull ( 0 ) )
2016-11-21 03:52:58 +00:00
{
2019-02-20 13:26:49 +00:00
list . Add ( row . GetString ( 0 ) ) ;
2016-11-21 03:52:58 +00:00
}
2016-12-13 15:44:34 +00:00
}
2019-02-20 13:26:49 +00:00
}
2016-11-20 09:46:07 +00:00
}
2019-08-18 20:05:06 +00:00
LogQueryTime ( "GetItemValueNames" , commandText , now ) ;
return list ;
2016-11-20 09:46:07 +00:00
}
2019-02-08 23:48:09 +00:00
private QueryResult < ( BaseItem , ItemCounts ) > GetItemValues ( InternalItemsQuery query , int [ ] itemValueTypes , string returnType )
2016-11-20 09:46:07 +00:00
{
if ( query = = null )
{
2019-01-06 20:50:43 +00:00
throw new ArgumentNullException ( nameof ( query ) ) ;
2016-11-20 09:46:07 +00:00
}
if ( ! query . Limit . HasValue )
{
query . EnableTotalRecordCount = false ;
}
CheckDisposed ( ) ;
var now = DateTime . UtcNow ;
var typeClause = itemValueTypes . Length = = 1 ?
( "Type=" + itemValueTypes [ 0 ] . ToString ( CultureInfo . InvariantCulture ) ) :
2019-02-08 23:48:09 +00:00
( "Type in (" + string . Join ( "," , itemValueTypes . Select ( i = > i . ToString ( CultureInfo . InvariantCulture ) ) ) + ")" ) ;
2016-11-20 09:46:07 +00:00
2016-11-20 20:20:01 +00:00
InternalItemsQuery typeSubQuery = null ;
2019-01-26 17:21:41 +00:00
Dictionary < string , string > itemCountColumns = null ;
2016-11-20 20:20:01 +00:00
2017-08-09 19:56:38 +00:00
var typesToCount = query . IncludeItemTypes ;
2016-11-20 20:20:01 +00:00
2017-08-09 19:56:38 +00:00
if ( typesToCount . Length > 0 )
2016-11-20 20:20:01 +00:00
{
var itemCountColumnQuery = "select group_concat(type, '|')" + GetFromText ( "B" ) ;
typeSubQuery = new InternalItemsQuery ( query . User )
{
ExcludeItemTypes = query . ExcludeItemTypes ,
IncludeItemTypes = query . IncludeItemTypes ,
MediaTypes = query . MediaTypes ,
AncestorIds = query . AncestorIds ,
ExcludeItemIds = query . ExcludeItemIds ,
ItemIds = query . ItemIds ,
TopParentIds = query . TopParentIds ,
ParentId = query . ParentId ,
IsPlayed = query . IsPlayed
} ;
2017-02-26 21:47:52 +00:00
var whereClauses = GetWhereClauses ( typeSubQuery , null ) ;
2016-11-20 20:20:01 +00:00
whereClauses . Add ( "guid in (select ItemId from ItemValues where ItemValues.CleanValue=A.CleanName AND " + typeClause + ")" ) ;
2019-02-08 23:48:09 +00:00
itemCountColumnQuery + = " where " + string . Join ( " AND " , whereClauses ) ;
2016-11-20 20:20:01 +00:00
2019-01-26 17:21:41 +00:00
itemCountColumns = new Dictionary < string , string > ( )
{
{ "itemTypes" , "(" + itemCountColumnQuery + ") as itemTypes" }
} ;
2016-11-20 20:20:01 +00:00
}
2019-01-26 17:21:41 +00:00
List < string > columns = _retriveItemColumns . ToList ( ) ;
if ( itemCountColumns ! = null )
{
columns . AddRange ( itemCountColumns . Values ) ;
}
2016-11-20 20:20:01 +00:00
2018-09-12 17:26:21 +00:00
// do this first before calling GetFinalColumnsToSelect, otherwise ExcludeItemIds will be set by SimilarTo
2016-11-20 20:20:01 +00:00
var innerQuery = new InternalItemsQuery ( query . User )
{
ExcludeItemTypes = query . ExcludeItemTypes ,
IncludeItemTypes = query . IncludeItemTypes ,
MediaTypes = query . MediaTypes ,
AncestorIds = query . AncestorIds ,
ItemIds = query . ItemIds ,
TopParentIds = query . TopParentIds ,
ParentId = query . ParentId ,
2017-12-03 22:15:21 +00:00
IsPlayed = query . IsPlayed ,
IsAiring = query . IsAiring ,
IsMovie = query . IsMovie ,
IsSports = query . IsSports ,
IsKids = query . IsKids ,
IsNews = query . IsNews ,
IsSeries = query . IsSeries
2016-11-20 20:20:01 +00:00
} ;
2019-02-08 23:48:09 +00:00
columns = GetFinalColumnsToSelect ( query , columns ) ;
2018-09-12 17:26:21 +00:00
2019-01-26 17:21:41 +00:00
var commandText = "select "
+ string . Join ( "," , columns )
+ GetFromText ( )
+ GetJoinUserDataText ( query ) ;
2018-09-12 17:26:21 +00:00
2016-11-20 20:20:01 +00:00
var innerWhereClauses = GetWhereClauses ( innerQuery , null ) ;
var innerWhereText = innerWhereClauses . Count = = 0 ?
string . Empty :
2019-01-26 17:21:41 +00:00
" where " + string . Join ( " AND " , innerWhereClauses ) ;
2016-11-20 20:20:01 +00:00
2019-01-26 17:21:41 +00:00
var whereText = " where Type=@SelectType And CleanName In (Select CleanValue from ItemValues where " + typeClause + " AND ItemId in (select guid from TypedBaseItems" + innerWhereText + "))" ;
2016-11-20 20:20:01 +00:00
var outerQuery = new InternalItemsQuery ( query . User )
{
IsFavorite = query . IsFavorite ,
IsFavoriteOrLiked = query . IsFavoriteOrLiked ,
IsLiked = query . IsLiked ,
IsLocked = query . IsLocked ,
NameLessThan = query . NameLessThan ,
NameStartsWith = query . NameStartsWith ,
NameStartsWithOrGreater = query . NameStartsWithOrGreater ,
Tags = query . Tags ,
OfficialRatings = query . OfficialRatings ,
GenreIds = query . GenreIds ,
Genres = query . Genres ,
2017-11-01 19:50:16 +00:00
Years = query . Years ,
2018-09-12 17:26:21 +00:00
NameContains = query . NameContains ,
SearchTerm = query . SearchTerm ,
SimilarTo = query . SimilarTo ,
ExcludeItemIds = query . ExcludeItemIds
2016-11-20 20:20:01 +00:00
} ;
var outerWhereClauses = GetWhereClauses ( outerQuery , null ) ;
2019-01-26 17:21:41 +00:00
if ( outerWhereClauses . Count ! = 0 )
{
whereText + = " AND " + string . Join ( " AND " , outerWhereClauses ) ;
}
2016-11-20 20:20:01 +00:00
2019-01-26 17:21:41 +00:00
commandText + = whereText + " group by PresentationUniqueKey" ;
2016-11-20 20:20:01 +00:00
2018-09-12 17:26:21 +00:00
if ( query . SimilarTo ! = null | | ! string . IsNullOrEmpty ( query . SearchTerm ) )
{
commandText + = GetOrderByText ( query ) ;
}
else
{
commandText + = " order by SortName" ;
}
2016-11-20 20:20:01 +00:00
if ( query . Limit . HasValue | | query . StartIndex . HasValue )
{
var offset = query . StartIndex ? ? 0 ;
if ( query . Limit . HasValue | | offset > 0 )
{
commandText + = " LIMIT " + ( query . Limit ? ? int . MaxValue ) . ToString ( CultureInfo . InvariantCulture ) ;
}
if ( offset > 0 )
{
commandText + = " OFFSET " + offset . ToString ( CultureInfo . InvariantCulture ) ;
}
}
var isReturningZeroItems = query . Limit . HasValue & & query . Limit < = 0 ;
2016-11-25 17:36:00 +00:00
var statementTexts = new List < string > ( ) ;
if ( ! isReturningZeroItems )
{
statementTexts . Add ( commandText ) ;
}
2019-08-18 20:05:06 +00:00
2016-11-25 17:36:00 +00:00
if ( query . EnableTotalRecordCount )
{
2019-01-26 17:21:41 +00:00
var countText = "select "
+ string . Join ( "," , GetFinalColumnsToSelect ( query , new [ ] { "count (distinct PresentationUniqueKey)" } ) )
+ GetFromText ( )
+ GetJoinUserDataText ( query )
+ whereText ;
2016-11-25 17:36:00 +00:00
statementTexts . Add ( countText ) ;
}
2019-08-18 20:05:06 +00:00
var list = new List < ( BaseItem , ItemCounts ) > ( ) ;
var result = new QueryResult < ( BaseItem , ItemCounts ) > ( ) ;
2019-02-26 17:30:13 +00:00
using ( var connection = GetConnection ( true ) )
2016-11-20 09:46:07 +00:00
{
2019-08-18 20:05:06 +00:00
connection . RunInTransaction (
db = >
2019-02-20 13:26:49 +00:00
{
2019-08-18 20:05:06 +00:00
var statements = PrepareAll ( db , statementTexts ) . ToList ( ) ;
if ( ! isReturningZeroItems )
2016-11-20 20:20:01 +00:00
{
2019-08-18 20:05:06 +00:00
using ( var statement = statements [ 0 ] )
2016-11-21 03:52:58 +00:00
{
2019-08-18 20:05:06 +00:00
statement . TryBind ( "@SelectType" , returnType ) ;
if ( EnableJoinUserData ( query ) )
{
statement . TryBind ( "@UserId" , query . User . InternalId ) ;
}
2016-11-20 20:20:01 +00:00
2019-08-18 20:05:06 +00:00
if ( typeSubQuery ! = null )
{
GetWhereClauses ( typeSubQuery , null ) ;
}
2019-02-20 13:26:49 +00:00
2019-08-18 20:05:06 +00:00
BindSimilarParams ( query , statement ) ;
BindSearchParams ( query , statement ) ;
GetWhereClauses ( innerQuery , statement ) ;
GetWhereClauses ( outerQuery , statement ) ;
2019-02-20 13:26:49 +00:00
2019-08-18 20:05:06 +00:00
var hasEpisodeAttributes = HasEpisodeAttributes ( query ) ;
var hasProgramAttributes = HasProgramAttributes ( query ) ;
var hasServiceName = HasServiceName ( query ) ;
var hasStartDate = HasStartDate ( query ) ;
var hasTrailerTypes = HasTrailerTypes ( query ) ;
var hasArtistFields = HasArtistFields ( query ) ;
var hasSeriesFields = HasSeriesFields ( query ) ;
foreach ( var row in statement . ExecuteQuery ( ) )
2016-11-21 03:52:58 +00:00
{
2019-08-18 20:05:06 +00:00
var item = GetItem ( row , query , hasProgramAttributes , hasEpisodeAttributes , hasServiceName , hasStartDate , hasTrailerTypes , hasArtistFields , hasSeriesFields ) ;
if ( item ! = null )
{
var countStartColumn = columns . Count - 1 ;
2016-11-20 20:20:01 +00:00
2019-08-18 20:05:06 +00:00
list . Add ( ( item , GetItemCounts ( row , countStartColumn , typesToCount ) ) ) ;
}
2016-11-21 03:52:58 +00:00
}
2016-12-12 05:49:19 +00:00
}
2016-11-21 03:52:58 +00:00
}
2016-11-20 20:20:01 +00:00
2019-08-18 20:05:06 +00:00
if ( query . EnableTotalRecordCount )
2019-02-20 13:26:49 +00:00
{
2019-08-18 20:05:06 +00:00
commandText = "select "
+ string . Join ( "," , GetFinalColumnsToSelect ( query , new [ ] { "count (distinct PresentationUniqueKey)" } ) )
+ GetFromText ( )
+ GetJoinUserDataText ( query )
+ whereText ;
2016-11-20 20:20:01 +00:00
2019-08-18 20:05:06 +00:00
using ( var statement = statements [ statements . Count - 1 ] )
2019-02-20 13:26:49 +00:00
{
2019-08-18 20:05:06 +00:00
statement . TryBind ( "@SelectType" , returnType ) ;
if ( EnableJoinUserData ( query ) )
{
statement . TryBind ( "@UserId" , query . User . InternalId ) ;
}
2016-11-20 20:20:01 +00:00
2019-08-18 20:05:06 +00:00
if ( typeSubQuery ! = null )
{
GetWhereClauses ( typeSubQuery , null ) ;
}
2020-04-03 18:59:38 +00:00
2019-08-18 20:05:06 +00:00
BindSimilarParams ( query , statement ) ;
BindSearchParams ( query , statement ) ;
GetWhereClauses ( innerQuery , statement ) ;
GetWhereClauses ( outerQuery , statement ) ;
2016-11-20 20:20:01 +00:00
2019-08-18 20:05:06 +00:00
result . TotalRecordCount = statement . ExecuteQuery ( ) . SelectScalarInt ( ) . First ( ) ;
}
2016-11-21 03:52:58 +00:00
}
2019-08-18 20:05:06 +00:00
} ,
ReadTransactionMode ) ;
}
2016-12-13 07:36:30 +00:00
2019-08-18 20:05:06 +00:00
LogQueryTime ( "GetItemValues" , commandText , now ) ;
2016-12-13 07:36:30 +00:00
2019-08-18 20:05:06 +00:00
if ( result . TotalRecordCount = = 0 )
{
result . TotalRecordCount = list . Count ;
2016-11-20 20:20:01 +00:00
}
2019-08-18 20:05:06 +00:00
2019-09-02 06:19:29 +00:00
result . Items = list ;
2019-08-18 20:05:06 +00:00
return result ;
2016-11-20 09:46:07 +00:00
}
2017-08-09 19:56:38 +00:00
private ItemCounts GetItemCounts ( IReadOnlyList < IResultSetValue > reader , int countStartColumn , string [ ] typesToCount )
2016-11-20 09:46:07 +00:00
{
var counts = new ItemCounts ( ) ;
2017-08-09 19:56:38 +00:00
if ( typesToCount . Length = = 0 )
2016-11-20 09:46:07 +00:00
{
return counts ;
}
var typeString = reader . IsDBNull ( countStartColumn ) ? null : reader . GetString ( countStartColumn ) ;
if ( string . IsNullOrWhiteSpace ( typeString ) )
{
return counts ;
}
var allTypes = typeString . Split ( new [ ] { '|' } , StringSplitOptions . RemoveEmptyEntries )
2020-04-03 18:59:38 +00:00
. ToLookup ( x = > x ) ;
2016-11-20 09:46:07 +00:00
foreach ( var type in allTypes )
{
2019-01-26 17:21:41 +00:00
var value = type . Count ( ) ;
2016-11-20 09:46:07 +00:00
var typeName = type . Key ;
if ( string . Equals ( typeName , typeof ( Series ) . FullName , StringComparison . OrdinalIgnoreCase ) )
{
counts . SeriesCount = value ;
}
else if ( string . Equals ( typeName , typeof ( Episode ) . FullName , StringComparison . OrdinalIgnoreCase ) )
{
counts . EpisodeCount = value ;
}
else if ( string . Equals ( typeName , typeof ( Movie ) . FullName , StringComparison . OrdinalIgnoreCase ) )
{
counts . MovieCount = value ;
}
else if ( string . Equals ( typeName , typeof ( MusicAlbum ) . FullName , StringComparison . OrdinalIgnoreCase ) )
{
counts . AlbumCount = value ;
}
else if ( string . Equals ( typeName , typeof ( MusicArtist ) . FullName , StringComparison . OrdinalIgnoreCase ) )
{
counts . ArtistCount = value ;
}
else if ( string . Equals ( typeName , typeof ( Audio ) . FullName , StringComparison . OrdinalIgnoreCase ) )
{
counts . SongCount = value ;
}
else if ( string . Equals ( typeName , typeof ( Trailer ) . FullName , StringComparison . OrdinalIgnoreCase ) )
{
counts . TrailerCount = value ;
}
2019-02-28 22:22:57 +00:00
2016-11-20 09:46:07 +00:00
counts . ItemCount + = value ;
}
return counts ;
}
2020-02-19 20:56:35 +00:00
private List < ( int , string ) > GetItemValuesToSave ( BaseItem item , List < string > inheritedTags )
2016-11-20 09:46:07 +00:00
{
2020-02-19 20:56:35 +00:00
var list = new List < ( int , string ) > ( ) ;
2016-11-20 09:46:07 +00:00
2019-01-26 17:21:41 +00:00
if ( item is IHasArtist hasArtist )
2016-11-20 09:46:07 +00:00
{
2020-02-19 20:56:35 +00:00
list . AddRange ( hasArtist . Artists . Select ( i = > ( 0 , i ) ) ) ;
2016-11-20 09:46:07 +00:00
}
2019-01-26 17:21:41 +00:00
if ( item is IHasAlbumArtist hasAlbumArtist )
2016-11-20 09:46:07 +00:00
{
2020-02-19 20:56:35 +00:00
list . AddRange ( hasAlbumArtist . AlbumArtists . Select ( i = > ( 1 , i ) ) ) ;
2016-11-20 09:46:07 +00:00
}
2020-02-19 20:56:35 +00:00
list . AddRange ( item . Genres . Select ( i = > ( 2 , i ) ) ) ;
list . AddRange ( item . Studios . Select ( i = > ( 3 , i ) ) ) ;
list . AddRange ( item . Tags . Select ( i = > ( 4 , i ) ) ) ;
2016-11-20 09:46:07 +00:00
2017-08-06 23:01:00 +00:00
// keywords was 5
2020-02-19 20:56:35 +00:00
list . AddRange ( inheritedTags . Select ( i = > ( 6 , i ) ) ) ;
2017-08-06 23:01:00 +00:00
2016-11-20 09:46:07 +00:00
return list ;
}
2020-02-19 20:56:35 +00:00
private void UpdateItemValues ( Guid itemId , List < ( int , string ) > values , IDatabaseConnection db )
2016-11-20 09:46:07 +00:00
{
2018-09-12 17:26:21 +00:00
if ( itemId . Equals ( Guid . Empty ) )
2016-11-20 09:46:07 +00:00
{
2019-01-06 20:50:43 +00:00
throw new ArgumentNullException ( nameof ( itemId ) ) ;
2016-11-20 09:46:07 +00:00
}
if ( values = = null )
{
2019-01-06 20:50:43 +00:00
throw new ArgumentNullException ( nameof ( values ) ) ;
2016-11-20 09:46:07 +00:00
}
CheckDisposed ( ) ;
2019-09-25 11:24:39 +00:00
var guidBlob = itemId . ToByteArray ( ) ;
2017-08-11 06:29:49 +00:00
2019-01-07 23:27:46 +00:00
// First delete
2017-08-11 06:29:49 +00:00
db . Execute ( "delete from ItemValues where ItemId=@Id" , guidBlob ) ;
2016-11-20 09:46:07 +00:00
2018-09-12 17:26:21 +00:00
InsertItemValues ( guidBlob , values , db ) ;
}
2020-02-19 20:56:35 +00:00
private void InsertItemValues ( byte [ ] idBlob , List < ( int , string ) > values , IDatabaseConnection db )
2018-09-12 17:26:21 +00:00
{
2020-04-03 18:59:38 +00:00
const int Limit = 100 ;
2018-09-12 17:26:21 +00:00
var startIndex = 0 ;
while ( startIndex < values . Count )
2016-11-20 09:46:07 +00:00
{
2018-09-12 17:26:21 +00:00
var insertText = new StringBuilder ( "insert into ItemValues (ItemId, Type, Value, CleanValue) values " ) ;
2020-04-03 18:59:38 +00:00
var endIndex = Math . Min ( values . Count , startIndex + Limit ) ;
2016-12-24 07:41:53 +00:00
2018-09-12 17:26:21 +00:00
for ( var i = startIndex ; i < endIndex ; i + + )
{
2019-09-25 11:24:39 +00:00
insertText . AppendFormat (
CultureInfo . InvariantCulture ,
2020-04-03 18:59:38 +00:00
"(@ItemId, @Type{0}, @Value{0}, @CleanValue{0})," ,
2019-09-25 11:24:39 +00:00
i ) ;
2018-09-12 17:26:21 +00:00
}
2016-11-20 09:46:07 +00:00
2020-04-03 18:59:38 +00:00
// Remove last comma
insertText . Length - - ;
2019-04-02 20:15:18 +00:00
using ( var statement = PrepareStatement ( db , insertText . ToString ( ) ) )
2018-09-12 17:26:21 +00:00
{
statement . TryBind ( "@ItemId" , idBlob ) ;
2016-11-20 09:46:07 +00:00
2018-09-12 17:26:21 +00:00
for ( var i = startIndex ; i < endIndex ; i + + )
2016-11-20 09:46:07 +00:00
{
2018-09-12 17:26:21 +00:00
var index = i . ToString ( CultureInfo . InvariantCulture ) ;
var currentValueInfo = values [ i ] ;
var itemValue = currentValueInfo . Item2 ;
// Don't save if invalid
if ( string . IsNullOrWhiteSpace ( itemValue ) )
{
continue ;
}
statement . TryBind ( "@Type" + index , currentValueInfo . Item1 ) ;
statement . TryBind ( "@Value" + index , itemValue ) ;
statement . TryBind ( "@CleanValue" + index , GetCleanValue ( itemValue ) ) ;
2016-11-20 09:46:07 +00:00
}
2018-09-12 17:26:21 +00:00
statement . Reset ( ) ;
2016-11-20 09:46:07 +00:00
statement . MoveNext ( ) ;
}
2018-09-12 17:26:21 +00:00
2020-04-03 18:59:38 +00:00
startIndex + = Limit ;
2016-11-20 09:46:07 +00:00
}
}
2017-08-27 00:32:33 +00:00
public void UpdatePeople ( Guid itemId , List < PersonInfo > people )
2016-11-20 09:46:07 +00:00
{
2018-09-12 17:26:21 +00:00
if ( itemId . Equals ( Guid . Empty ) )
2016-11-20 09:46:07 +00:00
{
2019-01-06 20:50:43 +00:00
throw new ArgumentNullException ( nameof ( itemId ) ) ;
2016-11-20 09:46:07 +00:00
}
if ( people = = null )
{
2019-01-06 20:50:43 +00:00
throw new ArgumentNullException ( nameof ( people ) ) ;
2016-11-20 09:46:07 +00:00
}
CheckDisposed ( ) ;
2019-02-26 17:30:13 +00:00
using ( var connection = GetConnection ( ) )
2016-11-20 09:46:07 +00:00
{
2019-02-20 13:26:49 +00:00
connection . RunInTransaction ( db = >
2016-12-11 05:12:00 +00:00
{
2019-09-25 11:24:39 +00:00
var itemIdBlob = itemId . ToByteArray ( ) ;
2018-09-12 17:26:21 +00:00
2019-08-18 20:05:06 +00:00
// First delete chapters
db . Execute ( "delete from People where ItemId=@ItemId" , itemIdBlob ) ;
2018-09-12 17:26:21 +00:00
2019-02-20 13:26:49 +00:00
InsertPeople ( itemIdBlob , people , db ) ;
} , TransactionMode ) ;
2018-09-12 17:26:21 +00:00
}
}
private void InsertPeople ( byte [ ] idBlob , List < PersonInfo > people , IDatabaseConnection db )
{
2020-04-03 18:59:38 +00:00
const int Limit = 100 ;
2018-09-12 17:26:21 +00:00
var startIndex = 0 ;
var listIndex = 0 ;
while ( startIndex < people . Count )
{
var insertText = new StringBuilder ( "insert into People (ItemId, Name, Role, PersonType, SortOrder, ListOrder) values " ) ;
2020-04-03 18:59:38 +00:00
var endIndex = Math . Min ( people . Count , startIndex + Limit ) ;
2018-09-12 17:26:21 +00:00
for ( var i = startIndex ; i < endIndex ; i + + )
{
2020-04-03 18:59:38 +00:00
insertText . AppendFormat ( "(@ItemId, @Name{0}, @Role{0}, @PersonType{0}, @SortOrder{0}, @ListOrder{0})," , i . ToString ( CultureInfo . InvariantCulture ) ) ;
2018-09-12 17:26:21 +00:00
}
2016-11-20 09:46:07 +00:00
2020-04-03 18:59:38 +00:00
// Remove last comma
insertText . Length - - ;
2019-04-02 20:15:18 +00:00
using ( var statement = PrepareStatement ( db , insertText . ToString ( ) ) )
2018-09-12 17:26:21 +00:00
{
statement . TryBind ( "@ItemId" , idBlob ) ;
2016-11-20 09:46:07 +00:00
2018-09-12 17:26:21 +00:00
for ( var i = startIndex ; i < endIndex ; i + + )
2016-11-20 09:46:07 +00:00
{
2018-09-12 17:26:21 +00:00
var index = i . ToString ( CultureInfo . InvariantCulture ) ;
2016-11-20 09:46:07 +00:00
2018-09-12 17:26:21 +00:00
var person = people [ i ] ;
2016-11-20 09:46:07 +00:00
2018-09-12 17:26:21 +00:00
statement . TryBind ( "@Name" + index , person . Name ) ;
statement . TryBind ( "@Role" + index , person . Role ) ;
statement . TryBind ( "@PersonType" + index , person . Type ) ;
statement . TryBind ( "@SortOrder" + index , person . SortOrder ) ;
statement . TryBind ( "@ListOrder" + index , listIndex ) ;
listIndex + + ;
2016-11-20 09:46:07 +00:00
}
2018-09-12 17:26:21 +00:00
statement . Reset ( ) ;
statement . MoveNext ( ) ;
2016-11-20 09:46:07 +00:00
}
2018-09-12 17:26:21 +00:00
2020-04-03 18:59:38 +00:00
startIndex + = Limit ;
2016-11-20 09:46:07 +00:00
}
}
private PersonInfo GetPerson ( IReadOnlyList < IResultSetValue > reader )
{
2020-04-03 18:59:38 +00:00
var item = new PersonInfo
{
ItemId = reader . GetGuid ( 0 ) ,
Name = reader . GetString ( 1 )
} ;
2016-11-20 09:46:07 +00:00
if ( ! reader . IsDBNull ( 2 ) )
{
item . Role = reader . GetString ( 2 ) ;
}
if ( ! reader . IsDBNull ( 3 ) )
{
item . Type = reader . GetString ( 3 ) ;
}
if ( ! reader . IsDBNull ( 4 ) )
{
item . SortOrder = reader . GetInt32 ( 4 ) ;
}
return item ;
}
2017-08-05 19:02:33 +00:00
public List < MediaStream > GetMediaStreams ( MediaStreamQuery query )
2016-11-20 09:46:07 +00:00
{
CheckDisposed ( ) ;
if ( query = = null )
{
2019-01-06 20:50:43 +00:00
throw new ArgumentNullException ( nameof ( query ) ) ;
2016-11-20 09:46:07 +00:00
}
2019-01-26 17:21:41 +00:00
var cmdText = "select "
+ string . Join ( "," , _mediaStreamSaveColumns )
+ " from mediastreams where"
+ " ItemId=@ItemId" ;
2016-11-20 09:46:07 +00:00
if ( query . Type . HasValue )
{
cmdText + = " AND StreamType=@StreamType" ;
}
if ( query . Index . HasValue )
{
cmdText + = " AND StreamIndex=@StreamIndex" ;
}
cmdText + = " order by StreamIndex ASC" ;
2019-02-26 17:30:13 +00:00
using ( var connection = GetConnection ( true ) )
2016-11-20 09:46:07 +00:00
{
2019-02-20 13:26:49 +00:00
var list = new List < MediaStream > ( ) ;
2019-04-02 20:15:18 +00:00
using ( var statement = PrepareStatement ( connection , cmdText ) )
2016-11-20 09:46:07 +00:00
{
2019-09-25 11:24:39 +00:00
statement . TryBind ( "@ItemId" , query . ItemId . ToByteArray ( ) ) ;
2016-12-13 15:44:34 +00:00
2019-02-20 13:26:49 +00:00
if ( query . Type . HasValue )
2016-11-20 09:46:07 +00:00
{
2019-02-20 13:26:49 +00:00
statement . TryBind ( "@StreamType" , query . Type . Value . ToString ( ) ) ;
}
2016-12-13 07:36:30 +00:00
2019-02-20 13:26:49 +00:00
if ( query . Index . HasValue )
{
statement . TryBind ( "@StreamIndex" , query . Index . Value ) ;
2016-12-13 15:44:34 +00:00
}
2016-12-13 07:36:30 +00:00
2019-02-20 13:26:49 +00:00
foreach ( var row in statement . ExecuteQuery ( ) )
{
list . Add ( GetMediaStream ( row ) ) ;
}
2016-11-20 09:46:07 +00:00
}
2019-02-20 13:26:49 +00:00
return list ;
2016-11-20 09:46:07 +00:00
}
}
2017-08-27 00:32:33 +00:00
public void SaveMediaStreams ( Guid id , List < MediaStream > streams , CancellationToken cancellationToken )
2016-11-20 09:46:07 +00:00
{
CheckDisposed ( ) ;
2019-01-26 17:21:41 +00:00
if ( id = = Guid . Empty )
2016-11-20 09:46:07 +00:00
{
2019-01-06 20:50:43 +00:00
throw new ArgumentNullException ( nameof ( id ) ) ;
2016-11-20 09:46:07 +00:00
}
if ( streams = = null )
{
2019-01-06 20:50:43 +00:00
throw new ArgumentNullException ( nameof ( streams ) ) ;
2016-11-20 09:46:07 +00:00
}
cancellationToken . ThrowIfCancellationRequested ( ) ;
2019-02-26 17:30:13 +00:00
using ( var connection = GetConnection ( ) )
2016-11-20 09:46:07 +00:00
{
2019-02-20 13:26:49 +00:00
connection . RunInTransaction ( db = >
2016-12-11 05:12:00 +00:00
{
2019-09-25 11:24:39 +00:00
var itemIdBlob = id . ToByteArray ( ) ;
2018-09-12 17:26:21 +00:00
2019-08-18 20:05:06 +00:00
// First delete chapters
db . Execute ( "delete from mediastreams where ItemId=@ItemId" , itemIdBlob ) ;
2018-09-12 17:26:21 +00:00
2019-02-20 13:26:49 +00:00
InsertMediaStreams ( itemIdBlob , streams , db ) ;
} , TransactionMode ) ;
2018-09-12 17:26:21 +00:00
}
}
private void InsertMediaStreams ( byte [ ] idBlob , List < MediaStream > streams , IDatabaseConnection db )
{
2020-04-03 18:59:38 +00:00
const int Limit = 10 ;
2018-09-12 17:26:21 +00:00
var startIndex = 0 ;
while ( startIndex < streams . Count )
{
2020-04-03 18:59:38 +00:00
var insertText = new StringBuilder ( "insert into mediastreams (" ) ;
foreach ( var column in _mediaStreamSaveColumns )
{
insertText . Append ( column ) . Append ( ',' ) ;
}
// Remove last comma
insertText . Length - - ;
insertText . Append ( ") values " ) ;
2018-09-12 17:26:21 +00:00
2020-04-03 18:59:38 +00:00
var endIndex = Math . Min ( streams . Count , startIndex + Limit ) ;
2018-09-12 17:26:21 +00:00
for ( var i = startIndex ; i < endIndex ; i + + )
{
2019-01-26 17:21:41 +00:00
if ( i ! = startIndex )
2018-09-12 17:26:21 +00:00
{
2020-04-03 18:59:38 +00:00
insertText . Append ( ',' ) ;
2018-09-12 17:26:21 +00:00
}
var index = i . ToString ( CultureInfo . InvariantCulture ) ;
2019-01-26 17:21:41 +00:00
insertText . Append ( "(@ItemId, " ) ;
2018-09-12 17:26:21 +00:00
2019-01-26 17:21:41 +00:00
foreach ( var column in _mediaStreamSaveColumns . Skip ( 1 ) )
{
2020-04-03 18:59:38 +00:00
insertText . Append ( '@' ) . Append ( column ) . Append ( index ) . Append ( ',' ) ;
2019-01-26 17:21:41 +00:00
}
2020-04-03 18:59:38 +00:00
2019-01-26 17:21:41 +00:00
insertText . Length - = 1 ; // Remove the last comma
2018-09-12 17:26:21 +00:00
2020-04-03 18:59:38 +00:00
insertText . Append ( ')' ) ;
2018-09-12 17:26:21 +00:00
}
2019-04-02 20:15:18 +00:00
using ( var statement = PrepareStatement ( db , insertText . ToString ( ) ) )
2018-09-12 17:26:21 +00:00
{
statement . TryBind ( "@ItemId" , idBlob ) ;
2016-11-20 09:46:07 +00:00
2018-09-12 17:26:21 +00:00
for ( var i = startIndex ; i < endIndex ; i + + )
2016-11-20 09:46:07 +00:00
{
2018-09-12 17:26:21 +00:00
var index = i . ToString ( CultureInfo . InvariantCulture ) ;
var stream = streams [ i ] ;
statement . TryBind ( "@StreamIndex" + index , stream . Index ) ;
statement . TryBind ( "@StreamType" + index , stream . Type . ToString ( ) ) ;
statement . TryBind ( "@Codec" + index , stream . Codec ) ;
statement . TryBind ( "@Language" + index , stream . Language ) ;
statement . TryBind ( "@ChannelLayout" + index , stream . ChannelLayout ) ;
statement . TryBind ( "@Profile" + index , stream . Profile ) ;
statement . TryBind ( "@AspectRatio" + index , stream . AspectRatio ) ;
statement . TryBind ( "@Path" + index , GetPathToSave ( stream . Path ) ) ;
statement . TryBind ( "@IsInterlaced" + index , stream . IsInterlaced ) ;
statement . TryBind ( "@BitRate" + index , stream . BitRate ) ;
statement . TryBind ( "@Channels" + index , stream . Channels ) ;
statement . TryBind ( "@SampleRate" + index , stream . SampleRate ) ;
statement . TryBind ( "@IsDefault" + index , stream . IsDefault ) ;
statement . TryBind ( "@IsForced" + index , stream . IsForced ) ;
statement . TryBind ( "@IsExternal" + index , stream . IsExternal ) ;
// Yes these are backwards due to a mistake
statement . TryBind ( "@Width" + index , stream . Height ) ;
statement . TryBind ( "@Height" + index , stream . Width ) ;
statement . TryBind ( "@AverageFrameRate" + index , stream . AverageFrameRate ) ;
statement . TryBind ( "@RealFrameRate" + index , stream . RealFrameRate ) ;
statement . TryBind ( "@Level" + index , stream . Level ) ;
statement . TryBind ( "@PixelFormat" + index , stream . PixelFormat ) ;
statement . TryBind ( "@BitDepth" + index , stream . BitDepth ) ;
statement . TryBind ( "@IsExternal" + index , stream . IsExternal ) ;
statement . TryBind ( "@RefFrames" + index , stream . RefFrames ) ;
statement . TryBind ( "@CodecTag" + index , stream . CodecTag ) ;
statement . TryBind ( "@Comment" + index , stream . Comment ) ;
statement . TryBind ( "@NalLengthSize" + index , stream . NalLengthSize ) ;
statement . TryBind ( "@IsAvc" + index , stream . IsAVC ) ;
statement . TryBind ( "@Title" + index , stream . Title ) ;
statement . TryBind ( "@TimeBase" + index , stream . TimeBase ) ;
statement . TryBind ( "@CodecTimeBase" + index , stream . CodecTimeBase ) ;
statement . TryBind ( "@ColorPrimaries" + index , stream . ColorPrimaries ) ;
statement . TryBind ( "@ColorSpace" + index , stream . ColorSpace ) ;
statement . TryBind ( "@ColorTransfer" + index , stream . ColorTransfer ) ;
2016-11-20 09:46:07 +00:00
}
2018-09-12 17:26:21 +00:00
statement . Reset ( ) ;
statement . MoveNext ( ) ;
2016-11-20 09:46:07 +00:00
}
2018-09-12 17:26:21 +00:00
2020-04-03 18:59:38 +00:00
startIndex + = Limit ;
2016-11-20 09:46:07 +00:00
}
}
2018-09-12 17:26:21 +00:00
2016-11-20 09:46:07 +00:00
/// <summary>
/// Gets the chapter.
/// </summary>
/// <param name="reader">The reader.</param>
/// <returns>ChapterInfo.</returns>
private MediaStream GetMediaStream ( IReadOnlyList < IResultSetValue > reader )
{
var item = new MediaStream
{
Index = reader [ 1 ] . ToInt ( )
} ;
2020-04-03 18:59:38 +00:00
item . Type = Enum . Parse < MediaStreamType > ( reader [ 2 ] . ToString ( ) , true ) ;
2016-11-20 09:46:07 +00:00
if ( reader [ 3 ] . SQLiteType ! = SQLiteType . Null )
{
item . Codec = reader [ 3 ] . ToString ( ) ;
}
if ( reader [ 4 ] . SQLiteType ! = SQLiteType . Null )
{
item . Language = reader [ 4 ] . ToString ( ) ;
}
if ( reader [ 5 ] . SQLiteType ! = SQLiteType . Null )
{
item . ChannelLayout = reader [ 5 ] . ToString ( ) ;
}
if ( reader [ 6 ] . SQLiteType ! = SQLiteType . Null )
{
item . Profile = reader [ 6 ] . ToString ( ) ;
}
if ( reader [ 7 ] . SQLiteType ! = SQLiteType . Null )
{
item . AspectRatio = reader [ 7 ] . ToString ( ) ;
}
if ( reader [ 8 ] . SQLiteType ! = SQLiteType . Null )
{
2018-09-12 17:26:21 +00:00
item . Path = RestorePath ( reader [ 8 ] . ToString ( ) ) ;
2016-11-20 09:46:07 +00:00
}
item . IsInterlaced = reader . GetBoolean ( 9 ) ;
if ( reader [ 10 ] . SQLiteType ! = SQLiteType . Null )
{
item . BitRate = reader . GetInt32 ( 10 ) ;
}
if ( reader [ 11 ] . SQLiteType ! = SQLiteType . Null )
{
item . Channels = reader . GetInt32 ( 11 ) ;
}
if ( reader [ 12 ] . SQLiteType ! = SQLiteType . Null )
{
item . SampleRate = reader . GetInt32 ( 12 ) ;
}
item . IsDefault = reader . GetBoolean ( 13 ) ;
item . IsForced = reader . GetBoolean ( 14 ) ;
item . IsExternal = reader . GetBoolean ( 15 ) ;
if ( reader [ 16 ] . SQLiteType ! = SQLiteType . Null )
{
item . Width = reader . GetInt32 ( 16 ) ;
}
if ( reader [ 17 ] . SQLiteType ! = SQLiteType . Null )
{
item . Height = reader . GetInt32 ( 17 ) ;
}
if ( reader [ 18 ] . SQLiteType ! = SQLiteType . Null )
{
item . AverageFrameRate = reader . GetFloat ( 18 ) ;
}
if ( reader [ 19 ] . SQLiteType ! = SQLiteType . Null )
{
item . RealFrameRate = reader . GetFloat ( 19 ) ;
}
if ( reader [ 20 ] . SQLiteType ! = SQLiteType . Null )
{
item . Level = reader . GetFloat ( 20 ) ;
}
if ( reader [ 21 ] . SQLiteType ! = SQLiteType . Null )
{
item . PixelFormat = reader [ 21 ] . ToString ( ) ;
}
if ( reader [ 22 ] . SQLiteType ! = SQLiteType . Null )
{
item . BitDepth = reader . GetInt32 ( 22 ) ;
}
if ( reader [ 23 ] . SQLiteType ! = SQLiteType . Null )
{
item . IsAnamorphic = reader . GetBoolean ( 23 ) ;
}
if ( reader [ 24 ] . SQLiteType ! = SQLiteType . Null )
{
item . RefFrames = reader . GetInt32 ( 24 ) ;
}
if ( reader [ 25 ] . SQLiteType ! = SQLiteType . Null )
{
item . CodecTag = reader . GetString ( 25 ) ;
}
if ( reader [ 26 ] . SQLiteType ! = SQLiteType . Null )
{
item . Comment = reader . GetString ( 26 ) ;
}
if ( reader [ 27 ] . SQLiteType ! = SQLiteType . Null )
{
item . NalLengthSize = reader . GetString ( 27 ) ;
}
if ( reader [ 28 ] . SQLiteType ! = SQLiteType . Null )
{
item . IsAVC = reader [ 28 ] . ToBool ( ) ;
}
if ( reader [ 29 ] . SQLiteType ! = SQLiteType . Null )
{
item . Title = reader [ 29 ] . ToString ( ) ;
}
if ( reader [ 30 ] . SQLiteType ! = SQLiteType . Null )
{
item . TimeBase = reader [ 30 ] . ToString ( ) ;
}
if ( reader [ 31 ] . SQLiteType ! = SQLiteType . Null )
{
item . CodecTimeBase = reader [ 31 ] . ToString ( ) ;
}
2018-09-12 17:26:21 +00:00
if ( reader [ 32 ] . SQLiteType ! = SQLiteType . Null )
{
item . ColorPrimaries = reader [ 32 ] . ToString ( ) ;
}
if ( reader [ 33 ] . SQLiteType ! = SQLiteType . Null )
{
item . ColorSpace = reader [ 33 ] . ToString ( ) ;
}
if ( reader [ 34 ] . SQLiteType ! = SQLiteType . Null )
{
item . ColorTransfer = reader [ 34 ] . ToString ( ) ;
}
2020-03-24 15:12:06 +00:00
if ( item . Type = = MediaStreamType . Subtitle )
{
2019-03-14 21:31:51 +00:00
item . localizedUndefined = _localization . GetLocalizedString ( "Undefined" ) ;
item . localizedDefault = _localization . GetLocalizedString ( "Default" ) ;
item . localizedForced = _localization . GetLocalizedString ( "Forced" ) ;
}
2016-11-20 09:46:07 +00:00
return item ;
}
2019-10-04 17:23:08 +00:00
public List < MediaAttachment > GetMediaAttachments ( MediaAttachmentQuery query )
{
CheckDisposed ( ) ;
if ( query = = null )
{
throw new ArgumentNullException ( nameof ( query ) ) ;
}
var cmdText = "select "
+ string . Join ( "," , _mediaAttachmentSaveColumns )
+ " from mediaattachments where"
+ " ItemId=@ItemId" ;
if ( query . Index . HasValue )
{
2019-11-04 13:48:27 +00:00
cmdText + = " AND AttachmentIndex=@AttachmentIndex" ;
2019-10-04 17:23:08 +00:00
}
cmdText + = " order by AttachmentIndex ASC" ;
2019-11-04 15:28:33 +00:00
var list = new List < MediaAttachment > ( ) ;
2019-11-06 15:43:14 +00:00
using ( var connection = GetConnection ( true ) )
2019-11-04 15:28:33 +00:00
using ( var statement = PrepareStatement ( connection , cmdText ) )
2019-10-04 17:23:08 +00:00
{
2019-11-04 15:28:33 +00:00
statement . TryBind ( "@ItemId" , query . ItemId . ToByteArray ( ) ) ;
2019-10-04 17:23:08 +00:00
2019-11-04 15:28:33 +00:00
if ( query . Index . HasValue )
2019-10-04 17:23:08 +00:00
{
2019-11-04 15:28:33 +00:00
statement . TryBind ( "@AttachmentIndex" , query . Index . Value ) ;
2019-10-04 17:23:08 +00:00
}
2019-11-05 13:19:07 +00:00
foreach ( var row in statement . ExecuteQuery ( ) )
{
2019-11-04 15:28:33 +00:00
list . Add ( GetMediaAttachment ( row ) ) ;
}
2019-10-04 17:23:08 +00:00
}
2019-11-04 15:28:33 +00:00
return list ;
2019-10-04 17:23:08 +00:00
}
2019-12-26 22:09:00 +00:00
public void SaveMediaAttachments (
Guid id ,
2019-12-26 22:20:31 +00:00
IReadOnlyList < MediaAttachment > attachments ,
2019-12-26 22:09:00 +00:00
CancellationToken cancellationToken )
2019-10-04 17:23:08 +00:00
{
CheckDisposed ( ) ;
if ( id = = Guid . Empty )
{
2019-11-05 12:48:32 +00:00
throw new ArgumentException ( nameof ( id ) ) ;
2019-10-04 17:23:08 +00:00
}
if ( attachments = = null )
{
throw new ArgumentNullException ( nameof ( attachments ) ) ;
}
2019-11-04 15:31:32 +00:00
cancellationToken . ThrowIfCancellationRequested ( ) ;
2019-11-06 15:43:14 +00:00
using ( var connection = GetConnection ( ) )
2019-10-04 17:23:08 +00:00
{
2019-11-06 15:43:14 +00:00
connection . RunInTransaction ( db = >
{
var itemIdBlob = id . ToByteArray ( ) ;
2019-10-04 17:23:08 +00:00
2019-11-06 15:43:14 +00:00
db . Execute ( "delete from mediaattachments where ItemId=@ItemId" , itemIdBlob ) ;
2019-10-04 17:23:08 +00:00
2019-11-07 13:38:36 +00:00
InsertMediaAttachments ( itemIdBlob , attachments , db , cancellationToken ) ;
2019-11-06 15:43:14 +00:00
} , TransactionMode ) ;
}
2019-10-04 17:23:08 +00:00
}
2019-12-26 22:09:00 +00:00
private void InsertMediaAttachments (
byte [ ] idBlob ,
2019-12-26 22:20:31 +00:00
IReadOnlyList < MediaAttachment > attachments ,
2019-12-26 22:09:00 +00:00
IDatabaseConnection db ,
CancellationToken cancellationToken )
2019-10-04 17:23:08 +00:00
{
2019-12-26 22:09:00 +00:00
const int InsertAtOnce = 10 ;
2019-10-04 17:23:08 +00:00
2019-12-26 22:09:00 +00:00
for ( var startIndex = 0 ; startIndex < attachments . Count ; startIndex + = InsertAtOnce )
2019-10-04 17:23:08 +00:00
{
2019-11-05 19:53:46 +00:00
var insertText = new StringBuilder ( _mediaAttachmentInsertPrefix ) ;
2019-10-04 17:23:08 +00:00
2019-12-26 22:09:00 +00:00
var endIndex = Math . Min ( attachments . Count , startIndex + InsertAtOnce ) ;
2019-10-04 17:23:08 +00:00
for ( var i = startIndex ; i < endIndex ; i + + )
{
var index = i . ToString ( CultureInfo . InvariantCulture ) ;
insertText . Append ( "(@ItemId, " ) ;
foreach ( var column in _mediaAttachmentSaveColumns . Skip ( 1 ) )
{
2020-07-22 12:34:51 +00:00
insertText . Append ( '@' )
. Append ( column )
. Append ( index )
. Append ( ',' ) ;
2019-10-04 17:23:08 +00:00
}
2019-11-05 13:13:22 +00:00
2019-10-04 17:23:08 +00:00
insertText . Length - = 1 ;
2019-12-26 22:09:00 +00:00
insertText . Append ( ")," ) ;
2019-10-04 17:23:08 +00:00
}
2019-12-26 22:09:00 +00:00
insertText . Length - - ;
2019-11-07 13:38:36 +00:00
cancellationToken . ThrowIfCancellationRequested ( ) ;
2019-10-04 17:23:08 +00:00
using ( var statement = PrepareStatement ( db , insertText . ToString ( ) ) )
{
statement . TryBind ( "@ItemId" , idBlob ) ;
for ( var i = startIndex ; i < endIndex ; i + + )
{
var index = i . ToString ( CultureInfo . InvariantCulture ) ;
var attachment = attachments [ i ] ;
statement . TryBind ( "@AttachmentIndex" + index , attachment . Index ) ;
statement . TryBind ( "@Codec" + index , attachment . Codec ) ;
statement . TryBind ( "@CodecTag" + index , attachment . CodecTag ) ;
statement . TryBind ( "@Comment" + index , attachment . Comment ) ;
2020-03-26 14:40:28 +00:00
statement . TryBind ( "@Filename" + index , attachment . FileName ) ;
statement . TryBind ( "@MIMEType" + index , attachment . MimeType ) ;
2019-10-04 17:23:08 +00:00
}
statement . Reset ( ) ;
statement . MoveNext ( ) ;
}
}
}
/// <summary>
/// Gets the attachment.
/// </summary>
/// <param name="reader">The reader.</param>
2020-06-19 10:21:49 +00:00
/// <returns>MediaAttachment.</returns>
2019-10-04 17:23:08 +00:00
private MediaAttachment GetMediaAttachment ( IReadOnlyList < IResultSetValue > reader )
{
var item = new MediaAttachment
{
Index = reader [ 1 ] . ToInt ( )
} ;
if ( reader [ 2 ] . SQLiteType ! = SQLiteType . Null )
{
item . Codec = reader [ 2 ] . ToString ( ) ;
}
if ( reader [ 2 ] . SQLiteType ! = SQLiteType . Null )
{
item . CodecTag = reader [ 3 ] . ToString ( ) ;
}
if ( reader [ 4 ] . SQLiteType ! = SQLiteType . Null )
{
item . Comment = reader [ 4 ] . ToString ( ) ;
}
if ( reader [ 6 ] . SQLiteType ! = SQLiteType . Null )
{
2020-01-08 01:52:48 +00:00
item . FileName = reader [ 5 ] . ToString ( ) ;
2019-10-04 17:23:08 +00:00
}
if ( reader [ 6 ] . SQLiteType ! = SQLiteType . Null )
{
2020-01-08 01:52:48 +00:00
item . MimeType = reader [ 6 ] . ToString ( ) ;
2019-10-04 17:23:08 +00:00
}
return item ;
}
2016-11-20 09:46:07 +00:00
}
2018-12-27 23:27:57 +00:00
}