2021-05-20 19:28:18 +00:00
#nullable disable
2020-05-29 09:28:19 +00:00
#pragma warning disable CS1591
2016-11-20 09:46:07 +00:00
using System ;
2021-04-29 14:23:29 +00:00
using System.Buffers.Text ;
2016-11-20 09:46:07 +00:00
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 ;
2021-06-12 09:22:26 +00:00
using Diacritics.Extensions ;
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 ;
2021-06-19 16:02:33 +00:00
using Jellyfin.Extensions ;
using Jellyfin.Extensions.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 ;
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
{
2021-05-23 22:30:41 +00:00
private const string FromText = " from TypedBaseItems A" ;
2019-07-01 15:59:01 +00:00
private const string ChaptersTableName = "Chapters2" ;
2016-11-20 09:46:07 +00:00
2021-10-02 17:03:04 +00:00
private const string SaveItemCommandText =
@ "replace into TypedBaseItems
( 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 , ForcedSortName , RunTimeTicks , Size , DateCreated , DateModified , PreferredMetadataLanguage , PreferredMetadataCountryCode , Width , Height , 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 , ExtraIds , TotalBitrate , ExtraType , Artists , AlbumArtists , ExternalId , SeriesPresentationUniqueKey , ShowId , OwnerId )
values ( @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 , @ForcedSortName , @RunTimeTicks , @Size , @DateCreated , @DateModified , @PreferredMetadataLanguage , @PreferredMetadataCountryCode , @Width , @Height , @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 , @ExtraIds , @TotalBitrate , @ExtraType , @Artists , @AlbumArtists , @ExternalId , @SeriesPresentationUniqueKey , @ShowId , @OwnerId ) ";
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 ;
2021-10-02 17:03:04 +00:00
private readonly ItemFields [ ] _allItemFields = Enum . GetValues < ItemFields > ( ) ;
2021-12-16 00:57:39 +00:00
private static readonly string [ ] _retrieveItemColumns =
2021-10-02 17:03:04 +00:00
{
"type" ,
"data" ,
"StartDate" ,
"EndDate" ,
"ChannelId" ,
"IsMovie" ,
"IsSeries" ,
"EpisodeTitle" ,
"IsRepeat" ,
"CommunityRating" ,
"CustomRating" ,
"IndexNumber" ,
"IsLocked" ,
"PreferredMetadataLanguage" ,
"PreferredMetadataCountryCode" ,
"Width" ,
"Height" ,
"DateLastRefreshed" ,
"Name" ,
"Path" ,
"PremiereDate" ,
"Overview" ,
"ParentIndexNumber" ,
"ProductionYear" ,
"OfficialRating" ,
"ForcedSortName" ,
"RunTimeTicks" ,
"Size" ,
"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" ,
"ExtraIds" ,
"TotalBitrate" ,
"ExtraType" ,
"Artists" ,
"AlbumArtists" ,
"ExternalId" ,
"SeriesPresentationUniqueKey" ,
"ShowId" ,
"OwnerId"
} ;
2021-12-16 00:57:39 +00:00
private static readonly string _retrieveItemColumnsSelectQuery = $"select {string.Join(',', _retrieveItemColumns)} from TypedBaseItems where guid = @guid" ;
2021-10-02 17:03:04 +00:00
private static readonly string [ ] _mediaStreamSaveColumns =
{
"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" ,
"CodecTimeBase" ,
"ColorPrimaries" ,
"ColorSpace" ,
"ColorTransfer"
} ;
private static readonly string _mediaStreamSaveColumnsInsertQuery =
$"insert into mediastreams ({string.Join(',', _mediaStreamSaveColumns)}) values " ;
private static readonly string _mediaStreamSaveColumnsSelectQuery =
$"select {string.Join(',', _mediaStreamSaveColumns)} from mediastreams where ItemId=@ItemId" ;
private static readonly string [ ] _mediaAttachmentSaveColumns =
{
"ItemId" ,
"AttachmentIndex" ,
"Codec" ,
"CodecTag" ,
"Comment" ,
"Filename" ,
"MIMEType"
} ;
private static readonly string _mediaAttachmentSaveColumnsSelectQuery =
$"select {string.Join(',', _mediaAttachmentSaveColumns)} from mediaattachments where ItemId=@ItemId" ;
private static readonly string _mediaAttachmentInsertPrefix ;
2021-12-14 22:10:40 +00:00
private static readonly BaseItemKind [ ] _programTypes = new [ ]
2021-10-02 17:03:04 +00:00
{
2021-12-12 02:31:30 +00:00
BaseItemKind . Program ,
BaseItemKind . TvChannel ,
BaseItemKind . LiveTvProgram ,
BaseItemKind . LiveTvChannel
2021-10-02 17:03:04 +00:00
} ;
2021-12-14 22:10:40 +00:00
private static readonly BaseItemKind [ ] _programExcludeParentTypes = new [ ]
2021-10-02 17:03:04 +00:00
{
2021-12-12 02:31:30 +00:00
BaseItemKind . Series ,
BaseItemKind . Season ,
BaseItemKind . MusicAlbum ,
BaseItemKind . MusicArtist ,
BaseItemKind . PhotoAlbum
2021-10-02 17:03:04 +00:00
} ;
2021-12-14 22:10:40 +00:00
private static readonly BaseItemKind [ ] _serviceTypes = new [ ]
2021-10-02 17:03:04 +00:00
{
2021-12-12 02:31:30 +00:00
BaseItemKind . TvChannel ,
BaseItemKind . LiveTvChannel
2021-10-02 17:03:04 +00:00
} ;
2021-12-14 22:10:40 +00:00
private static readonly BaseItemKind [ ] _startDateTypes = new [ ]
2021-10-02 17:03:04 +00:00
{
2021-12-12 02:31:30 +00:00
BaseItemKind . Program ,
BaseItemKind . LiveTvProgram
2021-10-02 17:03:04 +00:00
} ;
2021-12-14 22:10:40 +00:00
private static readonly BaseItemKind [ ] _seriesTypes = new [ ]
2021-10-02 17:03:04 +00:00
{
2021-12-12 02:31:30 +00:00
BaseItemKind . Book ,
BaseItemKind . AudioBook ,
BaseItemKind . Episode ,
BaseItemKind . Season
2021-10-02 17:03:04 +00:00
} ;
2021-12-14 22:10:40 +00:00
private static readonly BaseItemKind [ ] _artistExcludeParentTypes = new [ ]
2021-10-02 17:03:04 +00:00
{
2021-12-12 02:31:30 +00:00
BaseItemKind . Series ,
BaseItemKind . Season ,
BaseItemKind . PhotoAlbum
2021-10-02 17:03:04 +00:00
} ;
2021-12-14 22:10:40 +00:00
private static readonly BaseItemKind [ ] _artistsTypes = new [ ]
2021-10-02 17:03:04 +00:00
{
2021-12-12 02:31:30 +00:00
BaseItemKind . Audio ,
BaseItemKind . MusicAlbum ,
BaseItemKind . MusicVideo ,
BaseItemKind . AudioBook
2021-10-02 17:03:04 +00:00
} ;
private static readonly Type [ ] _knownTypes =
{
typeof ( LiveTvProgram ) ,
typeof ( LiveTvChannel ) ,
typeof ( Series ) ,
typeof ( Audio ) ,
typeof ( MusicAlbum ) ,
typeof ( MusicArtist ) ,
typeof ( MusicGenre ) ,
typeof ( MusicVideo ) ,
typeof ( Movie ) ,
typeof ( Playlist ) ,
typeof ( AudioBook ) ,
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 )
} ;
private readonly Dictionary < string , string > _types = GetTypeMapDictionary ( ) ;
2021-12-16 00:57:39 +00:00
private static readonly Dictionary < BaseItemKind , string > _baseItemKindNames = new ( )
{
{ BaseItemKind . AggregateFolder , typeof ( AggregateFolder ) . FullName } ,
{ BaseItemKind . Audio , typeof ( Audio ) . FullName } ,
{ BaseItemKind . AudioBook , typeof ( AudioBook ) . FullName } ,
{ BaseItemKind . BasePluginFolder , typeof ( BasePluginFolder ) . FullName } ,
{ BaseItemKind . Book , typeof ( Book ) . FullName } ,
{ BaseItemKind . BoxSet , typeof ( BoxSet ) . FullName } ,
{ BaseItemKind . Channel , typeof ( Channel ) . FullName } ,
{ BaseItemKind . CollectionFolder , typeof ( CollectionFolder ) . FullName } ,
{ BaseItemKind . Episode , typeof ( Episode ) . FullName } ,
{ BaseItemKind . Folder , typeof ( Folder ) . FullName } ,
{ BaseItemKind . Genre , typeof ( Genre ) . FullName } ,
{ BaseItemKind . Movie , typeof ( Movie ) . FullName } ,
{ BaseItemKind . LiveTvChannel , typeof ( LiveTvChannel ) . FullName } ,
{ BaseItemKind . LiveTvProgram , typeof ( LiveTvProgram ) . FullName } ,
{ BaseItemKind . MusicAlbum , typeof ( MusicAlbum ) . FullName } ,
{ BaseItemKind . MusicArtist , typeof ( MusicArtist ) . FullName } ,
{ BaseItemKind . MusicGenre , typeof ( MusicGenre ) . FullName } ,
{ BaseItemKind . MusicVideo , typeof ( MusicVideo ) . FullName } ,
{ BaseItemKind . Person , typeof ( Person ) . FullName } ,
{ BaseItemKind . Photo , typeof ( Photo ) . FullName } ,
{ BaseItemKind . PhotoAlbum , typeof ( PhotoAlbum ) . FullName } ,
{ BaseItemKind . Playlist , typeof ( Playlist ) . FullName } ,
{ BaseItemKind . PlaylistsFolder , typeof ( PlaylistsFolder ) . FullName } ,
{ BaseItemKind . Season , typeof ( Season ) . FullName } ,
{ BaseItemKind . Series , typeof ( Series ) . FullName } ,
{ BaseItemKind . Studio , typeof ( Studio ) . FullName } ,
{ BaseItemKind . Trailer , typeof ( Trailer ) . FullName } ,
{ BaseItemKind . TvChannel , typeof ( LiveTvChannel ) . FullName } ,
{ BaseItemKind . TvProgram , typeof ( LiveTvProgram ) . FullName } ,
{ BaseItemKind . UserRootFolder , typeof ( UserRootFolder ) . FullName } ,
{ BaseItemKind . UserView , typeof ( UserView ) . FullName } ,
{ BaseItemKind . Video , typeof ( Video ) . FullName } ,
{ BaseItemKind . Year , typeof ( Year ) . FullName }
} ;
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>
2021-08-28 22:32:50 +00:00
/// <param name="config">Instance of the <see cref="IServerConfigurationManager"/> interface.</param>
/// <param name="appHost">Instance of the <see cref="IServerApplicationHost"/> interface.</param>
/// <param name="logger">Instance of the <see cref="ILogger{SqliteItemRepository}"/> interface.</param>
/// <param name="localization">Instance of the <see cref="ILocalizationManager"/> interface.</param>
/// <param name="imageProcessor">Instance of the <see cref="IImageProcessor"/> interface.</param>
/// <exception cref="ArgumentNullException">config is null.</exception>
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 ( ) ;
2021-03-09 04:57:38 +00:00
_jsonOptions = JsonDefaults . Options ;
2019-09-25 15:43:20 +00:00
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>
2021-10-02 17:03:04 +00:00
/// <param name="userDataRepo">The user data repository.</param>
/// <param name="userManager">The user manager.</param>
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" ,
2021-10-02 17:03:04 +00:00
"drop index if exists idx_" + ChaptersTableName ,
2019-08-18 20:05:06 +00:00
"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 ) ;
2020-10-12 18:05:11 +00:00
connection . RunInTransaction (
db = >
2016-11-22 05:55:34 +00:00
{
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
}
/// <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>
2021-10-02 17:03:04 +00:00
/// <exception cref="ArgumentNullException"><paramref name="item"/> is <c>null</c>.</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
{
2020-10-12 18:05:11 +00:00
connection . RunInTransaction (
db = >
2017-10-22 06:22:43 +00:00
{
2021-10-02 17:03:04 +00:00
using ( var saveImagesStatement = 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 ( ) ) ;
2021-05-01 22:26:30 +00:00
saveImagesStatement . TryBind ( "@Images" , SerializeImages ( item . ImageInfos ) ) ;
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">
2021-10-02 17:03:04 +00:00
/// <paramref name="items"/> or <paramref name="cancellationToken"/> is <c>null</c>.
2016-11-20 09:46:07 +00:00
/// </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
{
2020-10-12 18:05:11 +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
{
2020-08-02 21:34:28 +00:00
SaveItemCommandText ,
2018-09-12 17:26:21 +00:00
"delete from AncestorIds where ItemId=@ItemId"
2020-09-09 11:38:27 +00:00
} ) ;
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
{
2021-02-12 23:39:18 +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
{
2021-02-12 23:39:18 +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
{
2021-02-12 23:39:18 +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
{
2021-02-12 23:39:18 +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
{
2021-02-12 23:39:18 +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 ) ;
2021-05-02 10:45:02 +00:00
saveItemStatement . TryBind ( "@ProviderIds" , SerializeProviderIds ( item . ProviderIds ) ) ;
2021-05-01 22:26:30 +00:00
saveItemStatement . TryBind ( "@Images" , SerializeImages ( item . ImageInfos ) ) ;
2016-11-20 09:46:07 +00:00
2017-08-09 19:56:38 +00:00
if ( item . ProductionLocations . Length > 0 )
2016-11-20 09:46:07 +00:00
{
2021-02-12 23:39:18 +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
{
2021-02-12 23:39:18 +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
{
2021-02-12 23:39:18 +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
{
2021-02-12 23:39:18 +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 ( ) ;
}
2021-05-02 10:45:02 +00:00
internal static string SerializeProviderIds ( Dictionary < string , string > providerIds )
2016-11-20 09:46:07 +00:00
{
2019-01-26 17:21:41 +00:00
StringBuilder str = new StringBuilder ( ) ;
2021-05-02 10:45:02 +00:00
foreach ( var i in providerIds )
2019-01-26 17:21:41 +00:00
{
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
2020-08-03 18:32:45 +00:00
str . Append ( i . Key )
. Append ( '=' )
. Append ( i . Value )
. Append ( '|' ) ;
2019-01-26 17:21:41 +00:00
}
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
}
2021-05-02 10:45:02 +00:00
internal static void DeserializeProviderIds ( string value , IHasProviderIds item )
2016-11-20 09:46:07 +00:00
{
if ( string . IsNullOrWhiteSpace ( value ) )
{
return ;
}
2021-04-30 13:09:36 +00:00
foreach ( var part in value . SpanSplit ( '|' ) )
2016-11-20 09:46:07 +00:00
{
2021-04-30 13:09:36 +00:00
var providerDelimiterIndex = part . IndexOf ( '=' ) ;
if ( providerDelimiterIndex ! = - 1 & & providerDelimiterIndex = = part . LastIndexOf ( '=' ) )
2016-11-20 09:46:07 +00:00
{
2021-04-30 21:35:29 +00:00
item . SetProviderId ( part . Slice ( 0 , providerDelimiterIndex ) . ToString ( ) , part . Slice ( providerDelimiterIndex + 1 ) . ToString ( ) ) ;
2016-11-20 09:46:07 +00:00
}
}
}
2021-05-01 22:26:30 +00:00
internal string SerializeImages ( ItemImageInfo [ ] images )
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-08-03 18:32:45 +00:00
AppendItemImageInfo ( str , i ) ;
str . 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
}
2021-05-01 22:26:30 +00:00
internal ItemImageInfo [ ] DeserializeImages ( string value )
2016-11-20 09:46:07 +00:00
{
if ( string . IsNullOrWhiteSpace ( value ) )
{
2021-05-01 22:26:30 +00:00
return Array . Empty < ItemImageInfo > ( ) ;
2016-11-20 09:46:07 +00:00
}
2021-05-23 22:30:41 +00:00
// TODO The following is an ugly performance optimization, but it's extremely unlikely that the data in the database would be malformed
var valueSpan = value . AsSpan ( ) ;
2021-06-11 22:56:09 +00:00
var count = valueSpan . Count ( '|' ) + 1 ;
2021-05-23 22:30:41 +00:00
var position = 0 ;
var result = new ItemImageInfo [ count ] ;
foreach ( var part in valueSpan . Split ( '|' ) )
2016-11-20 09:46:07 +00:00
{
2016-11-25 18:17:26 +00:00
var image = ItemImageInfoFromValueString ( part ) ;
if ( image ! = null )
{
2021-05-23 22:30:41 +00:00
result [ position + + ] = 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
2021-05-23 22:30:41 +00:00
if ( position = = count )
{
return result ;
}
if ( position = = 0 )
{
return Array . Empty < ItemImageInfo > ( ) ;
}
// Extremely unlikely, but somehow one or more of the image strings were malformed. Cut the array.
2021-06-05 18:16:04 +00:00
return result [ . . position ] ;
2016-11-20 09:46:07 +00:00
}
2021-05-01 22:26:30 +00:00
private void AppendItemImageInfo ( StringBuilder bldr , ItemImageInfo image )
2016-11-20 09:46:07 +00:00
{
2020-08-03 18:42:01 +00:00
const char Delimiter = '*' ;
2016-11-20 09:46:07 +00:00
2020-05-19 11:50:14 +00:00
var path = image . Path ? ? string . Empty ;
2020-03-23 19:05:49 +00:00
2020-08-03 18:32:45 +00:00
bldr . Append ( GetPathToSave ( path ) )
2020-08-03 18:42:01 +00:00
. Append ( Delimiter )
2020-08-03 18:32:45 +00:00
. Append ( image . DateModified . Ticks )
2020-08-03 18:42:01 +00:00
. Append ( Delimiter )
2020-08-03 18:32:45 +00:00
. Append ( image . Type )
2020-08-03 18:42:01 +00:00
. Append ( Delimiter )
2020-08-03 18:32:45 +00:00
. Append ( image . Width )
2020-08-03 18:42:01 +00:00
. Append ( Delimiter )
2021-05-01 22:26:30 +00:00
. Append ( image . Height ) ;
var hash = image . BlurHash ;
if ( ! string . IsNullOrEmpty ( hash ) )
{
bldr . Append ( Delimiter )
// Replace delimiters with other characters.
// This can be removed when we migrate to a proper DB.
. Append ( hash . Replace ( '*' , '/' ) . Replace ( '|' , '\\' ) ) ;
}
2016-11-20 09:46:07 +00:00
}
2021-05-03 21:51:45 +00:00
internal ItemImageInfo ItemImageInfoFromValueString ( ReadOnlySpan < char > value )
2016-11-20 09:46:07 +00:00
{
2021-04-30 13:09:36 +00:00
var nextSegment = value . IndexOf ( '*' ) ;
if ( nextSegment = = - 1 )
{
return null ;
}
2016-11-20 09:46:07 +00:00
2021-04-30 13:09:36 +00:00
ReadOnlySpan < char > path = value [ . . nextSegment ] ;
value = value [ ( nextSegment + 1 ) . . ] ;
nextSegment = value . IndexOf ( '*' ) ;
if ( nextSegment = = - 1 )
2016-11-25 18:17:26 +00:00
{
return null ;
}
2021-04-30 13:09:36 +00:00
ReadOnlySpan < char > dateModified = value [ . . nextSegment ] ;
value = value [ ( nextSegment + 1 ) . . ] ;
nextSegment = value . IndexOf ( '*' ) ;
if ( nextSegment = = - 1 )
{
2021-05-03 21:51:45 +00:00
nextSegment = value . Length ;
2021-04-30 13:09:36 +00:00
}
ReadOnlySpan < char > imageType = value [ . . nextSegment ] ;
2016-11-20 09:46:07 +00:00
2021-04-30 13:09:36 +00:00
var image = new ItemImageInfo
{
Path = RestorePath ( path . ToString ( ) )
} ;
2017-09-18 16:52:22 +00:00
2021-09-02 19:28:00 +00:00
if ( long . TryParse ( dateModified , NumberStyles . Any , CultureInfo . InvariantCulture , out var ticks )
& & ticks > = DateTime . MinValue . Ticks
& & ticks < = DateTime . MaxValue . Ticks )
2017-09-18 16:52:22 +00:00
{
image . DateModified = new DateTime ( ticks , DateTimeKind . Utc ) ;
}
2021-09-02 19:28:00 +00:00
else
{
return null ;
}
2017-09-18 16:52:22 +00:00
2021-05-22 22:20:19 +00:00
if ( Enum . TryParse ( imageType , true , out ImageType type ) )
2017-09-18 16:52:22 +00:00
{
image . Type = type ;
}
2021-09-02 19:28:00 +00:00
else
{
return null ;
}
2016-11-20 09:46:07 +00:00
2021-04-30 13:09:36 +00:00
// Optional parameters: width*height*blurhash
if ( nextSegment + 1 < value . Length - 1 )
2017-10-22 06:22:43 +00:00
{
2021-04-30 13:09:36 +00:00
value = value [ ( nextSegment + 1 ) . . ] ;
nextSegment = value . IndexOf ( '*' ) ;
2021-05-03 21:51:45 +00:00
if ( nextSegment = = - 1 | | nextSegment = = value . Length )
{
return image ;
}
2021-04-30 13:09:36 +00:00
ReadOnlySpan < char > widthSpan = value [ . . nextSegment ] ;
value = value [ ( nextSegment + 1 ) . . ] ;
nextSegment = value . IndexOf ( '*' ) ;
if ( nextSegment = = - 1 )
{
2021-05-03 21:51:45 +00:00
nextSegment = value . Length ;
2021-04-30 13:09:36 +00:00
}
ReadOnlySpan < char > heightSpan = value [ . . nextSegment ] ;
if ( int . TryParse ( widthSpan , NumberStyles . Integer , CultureInfo . InvariantCulture , out var width )
& & int . TryParse ( heightSpan , 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
2021-04-30 13:09:36 +00:00
if ( nextSegment < value . Length - 1 )
2020-03-23 19:05:49 +00:00
{
2021-05-03 21:51:45 +00:00
value = value [ ( nextSegment + 1 ) . . ] ;
2021-04-30 13:09:36 +00:00
var length = value . Length ;
Span < char > blurHashSpan = stackalloc char [ length ] ;
for ( int i = 0 ; i < length ; i + + )
{
var c = value [ i ] ;
blurHashSpan [ i ] = c switch
{
'/' = > '*' ,
'\\' = > '|' ,
_ = > c
} ;
}
image . BlurHash = new string ( blurHashSpan ) ;
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>
2021-10-02 17:03:04 +00:00
/// <exception cref="ArgumentNullException"><paramref name="id"/> is <c>null</c>.</exception>
/// <exception cref="ArgumentException"><paramr name="id"/> is <seealso cref="Guid.Empty"/>.</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
{
2021-12-16 00:57:39 +00:00
using ( var statement = PrepareStatement ( connection , _retrieveItemColumnsSelectQuery ) )
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 ;
}
2021-05-21 17:35:00 +00:00
private BaseItem GetItem ( IReadOnlyList < ResultSetValue > 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
}
2021-05-21 17:35:00 +00:00
private BaseItem GetItem ( IReadOnlyList < ResultSetValue > 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
{
2021-05-16 12:49:11 +00:00
if ( item is IHasStartDate hasStartDate & & reader . TryReadDateTime ( index , out var startDate ) )
2016-11-20 09:46:07 +00:00
{
2021-05-19 17:52:45 +00:00
hasStartDate . StartDate = startDate ;
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
}
2021-05-16 12:49:11 +00:00
if ( reader . TryReadDateTime ( index + + , out var endDate ) )
2016-11-20 09:46:07 +00:00
{
2021-05-16 12:49:11 +00:00
item . EndDate = endDate ;
2016-11-20 09:46:07 +00:00
}
2020-06-15 21:43:52 +00:00
2021-05-16 12:49:11 +00:00
var channelId = reader [ index ] ;
2021-05-19 06:51:46 +00:00
if ( ! channelId . IsDbNull ( ) )
2016-11-20 09:46:07 +00:00
{
2021-05-16 12:49:11 +00:00
if ( ! Utf8Parser . TryParse ( channelId . ToBlob ( ) , out Guid value , out _ , standardFormat : 'N' ) )
2021-04-29 14:23:29 +00:00
{
var str = reader . GetString ( index ) ;
Logger . LogWarning ( "{ChannelId} isn't in the expected format" , str ) ;
value = new Guid ( str ) ;
}
item . ChannelId = value ;
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
{
2021-05-16 12:49:11 +00:00
if ( reader . TryGetBoolean ( index + + , out var isMovie ) )
2017-05-22 04:54:02 +00:00
{
2021-05-19 06:51:46 +00:00
hasProgramAttributes . IsMovie = isMovie ;
2017-05-22 04:54:02 +00:00
}
2020-06-15 21:43:52 +00:00
2021-05-16 12:49:11 +00:00
if ( reader . TryGetBoolean ( index + + , out var isSeries ) )
2017-05-22 04:54:02 +00:00
{
2021-05-19 06:51:46 +00:00
hasProgramAttributes . IsSeries = isSeries ;
2017-05-22 04:54:02 +00:00
}
2020-06-15 21:43:52 +00:00
2021-05-16 12:49:11 +00:00
if ( reader . TryGetString ( index + + , out var episodeTitle ) )
2017-05-22 04:54:02 +00:00
{
2021-05-16 12:49:11 +00:00
hasProgramAttributes . EpisodeTitle = episodeTitle ;
2017-05-22 04:54:02 +00:00
}
2020-06-15 21:43:52 +00:00
2021-05-16 12:49:11 +00:00
if ( reader . TryGetBoolean ( index + + , out var isRepeat ) )
2017-05-22 04:54:02 +00:00
{
2021-05-19 06:51:46 +00:00
hasProgramAttributes . IsRepeat = isRepeat ;
2017-05-22 04:54:02 +00:00
}
}
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
}
}
2021-05-16 17:05:56 +00:00
if ( reader . TryGetSingle ( index + + , out var communityRating ) )
2016-11-20 09:46:07 +00:00
{
2021-05-16 12:49:11 +00:00
item . CommunityRating = communityRating ;
2016-11-20 09:46:07 +00:00
}
2020-06-15 21:43:52 +00:00
2017-05-22 04:54:02 +00:00
if ( HasField ( query , ItemFields . CustomRating ) )
2016-11-20 09:46:07 +00:00
{
2021-05-16 12:49:11 +00:00
if ( reader . TryGetString ( index + + , out var customRating ) )
2016-11-20 09:46:07 +00:00
{
2021-05-16 12:49:11 +00:00
item . CustomRating = customRating ;
2016-11-20 09:46:07 +00:00
}
}
2021-05-16 17:05:56 +00:00
if ( reader . TryGetInt32 ( index + + , out var indexNumber ) )
2016-11-20 09:46:07 +00:00
{
2021-05-16 12:49:11 +00:00
item . IndexNumber = indexNumber ;
2016-11-20 09:46:07 +00:00
}
2020-06-15 21:43:52 +00:00
2017-05-22 04:54:02 +00:00
if ( HasField ( query , ItemFields . Settings ) )
2016-11-20 09:46:07 +00:00
{
2021-05-16 12:49:11 +00:00
if ( reader . TryGetBoolean ( index + + , out var isLocked ) )
2016-11-20 09:46:07 +00:00
{
2021-05-19 06:51:46 +00:00
item . IsLocked = isLocked ;
2016-11-20 09:46:07 +00:00
}
2020-06-15 21:43:52 +00:00
2021-05-16 12:49:11 +00:00
if ( reader . TryGetString ( index + + , out var preferredMetadataLanguage ) )
2016-11-20 09:46:07 +00:00
{
2021-05-16 12:49:11 +00:00
item . PreferredMetadataLanguage = preferredMetadataLanguage ;
2016-11-20 09:46:07 +00:00
}
2020-06-15 21:43:52 +00:00
2021-05-16 12:49:11 +00:00
if ( reader . TryGetString ( index + + , out var preferredMetadataCountryCode ) )
2016-11-20 09:46:07 +00:00
{
2021-05-16 12:49:11 +00:00
item . PreferredMetadataCountryCode = preferredMetadataCountryCode ;
2016-11-20 09:46:07 +00:00
}
}
2018-09-12 17:26:21 +00:00
if ( HasField ( query , ItemFields . Width ) )
2016-11-20 09:46:07 +00:00
{
2021-05-16 17:05:56 +00:00
if ( reader . TryGetInt32 ( index + + , out var width ) )
2018-09-12 17:26:21 +00:00
{
2021-05-19 06:51:46 +00:00
item . Width = width ;
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 ( HasField ( query , ItemFields . Height ) )
2016-11-20 09:46:07 +00:00
{
2021-05-16 17:05:56 +00:00
if ( reader . TryGetInt32 ( index + + , out var height ) )
2017-05-21 07:25:49 +00:00
{
2021-05-19 06:51:46 +00:00
item . Height = height ;
2017-05-21 07:25:49 +00:00
}
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
{
2021-05-16 12:49:11 +00:00
if ( reader . TryReadDateTime ( index + + , out var dateLastRefreshed ) )
2017-05-22 04:54:02 +00:00
{
2021-05-19 17:52:45 +00:00
item . DateLastRefreshed = dateLastRefreshed ;
2017-05-22 04:54:02 +00:00
}
2016-11-20 09:46:07 +00:00
}
2021-05-16 12:49:11 +00:00
if ( reader . TryGetString ( index + + , out var name ) )
2016-11-20 09:46:07 +00:00
{
2021-05-16 12:49:11 +00:00
item . Name = name ;
2016-11-20 09:46:07 +00:00
}
2020-06-15 21:43:52 +00:00
2021-05-16 12:49:11 +00:00
if ( reader . TryGetString ( index + + , out var restorePath ) )
2016-11-20 09:46:07 +00:00
{
2021-05-16 12:49:11 +00:00
item . Path = RestorePath ( restorePath ) ;
2016-11-20 09:46:07 +00:00
}
2020-06-15 21:43:52 +00:00
2021-05-16 12:49:11 +00:00
if ( reader . TryReadDateTime ( index + + , out var premiereDate ) )
2016-11-20 09:46:07 +00:00
{
2021-05-16 12:49:11 +00:00
item . PremiereDate = premiereDate ;
2016-11-20 09:46:07 +00:00
}
2020-06-15 21:43:52 +00:00
2017-05-22 04:54:02 +00:00
if ( HasField ( query , ItemFields . Overview ) )
2016-11-20 09:46:07 +00:00
{
2021-05-16 12:49:11 +00:00
if ( reader . TryGetString ( index + + , out var overview ) )
2016-11-20 09:46:07 +00:00
{
2021-05-16 12:49:11 +00:00
item . Overview = overview ;
2016-11-20 09:46:07 +00:00
}
}
2021-05-16 17:05:56 +00:00
if ( reader . TryGetInt32 ( index + + , out var parentIndexNumber ) )
2016-11-20 09:46:07 +00:00
{
2021-05-16 12:49:11 +00:00
item . ParentIndexNumber = parentIndexNumber ;
2016-11-20 09:46:07 +00:00
}
2020-06-15 21:43:52 +00:00
2021-05-16 17:05:56 +00:00
if ( reader . TryGetInt32 ( index + + , out var productionYear ) )
2016-11-20 09:46:07 +00:00
{
2021-05-16 12:49:11 +00:00
item . ProductionYear = productionYear ;
2016-11-20 09:46:07 +00:00
}
2020-06-15 21:43:52 +00:00
2021-05-16 12:49:11 +00:00
if ( reader . TryGetString ( index + + , out var officialRating ) )
2016-11-20 09:46:07 +00:00
{
2021-05-16 12:49:11 +00:00
item . OfficialRating = officialRating ;
2016-11-20 09:46:07 +00:00
}
2020-06-15 21:43:52 +00:00
2017-05-22 04:54:02 +00:00
if ( HasField ( query , ItemFields . SortName ) )
2016-11-20 09:46:07 +00:00
{
2021-05-16 12:49:11 +00:00
if ( reader . TryGetString ( index + + , out var forcedSortName ) )
2016-11-20 09:46:07 +00:00
{
2021-05-16 12:49:11 +00:00
item . ForcedSortName = forcedSortName ;
2016-11-20 09:46:07 +00:00
}
}
2021-05-16 17:05:56 +00:00
if ( reader . TryGetInt64 ( index + + , out var runTimeTicks ) )
2016-11-20 09:46:07 +00:00
{
2021-05-16 12:49:11 +00:00
item . RunTimeTicks = runTimeTicks ;
2016-11-20 09:46:07 +00:00
}
2020-06-15 21:43:52 +00:00
2021-05-16 17:05:56 +00:00
if ( reader . TryGetInt64 ( index + + , out var size ) )
2019-01-26 08:07:13 +00:00
{
2021-05-16 12:49:11 +00:00
item . Size = size ;
2019-01-26 08:07:13 +00:00
}
2020-06-15 21:43:52 +00:00
2017-05-22 04:54:02 +00:00
if ( HasField ( query , ItemFields . DateCreated ) )
2016-11-20 09:46:07 +00:00
{
2021-05-16 12:49:11 +00:00
if ( reader . TryReadDateTime ( index + + , out var dateCreated ) )
2016-11-20 09:46:07 +00:00
{
2021-05-19 17:52:45 +00:00
item . DateCreated = dateCreated ;
2016-11-20 09:46:07 +00:00
}
}
2021-05-16 12:49:11 +00:00
if ( reader . TryReadDateTime ( index + + , out var dateModified ) )
2016-11-20 09:46:07 +00:00
{
2021-05-19 17:52:45 +00:00
item . DateModified = dateModified ;
2016-11-20 09:46:07 +00:00
}
2020-06-15 21:43:52 +00:00
2021-05-16 12:49:11 +00:00
item . Id = reader . GetGuid ( index + + ) ;
2016-11-20 09:46:07 +00:00
2017-05-22 04:54:02 +00:00
if ( HasField ( query , ItemFields . Genres ) )
2016-11-20 09:46:07 +00:00
{
2021-05-16 12:49:11 +00:00
if ( reader . TryGetString ( index + + , out var genres ) )
2016-11-20 09:46:07 +00:00
{
2021-05-16 12:49:11 +00:00
item . Genres = genres . Split ( '|' , StringSplitOptions . RemoveEmptyEntries ) ;
2016-11-20 09:46:07 +00:00
}
}
2021-05-16 12:49:11 +00:00
if ( reader . TryGetGuid ( index + + , out var parentId ) )
2016-11-20 09:46:07 +00:00
{
2021-05-19 17:52:45 +00:00
item . ParentId = parentId ;
2016-11-20 09:46:07 +00:00
}
2020-06-15 21:43:52 +00:00
2021-05-16 12:49:11 +00:00
if ( reader . TryGetString ( index + + , out var audioString ) )
2016-11-20 09:46:07 +00:00
{
2021-05-16 12:49:11 +00:00
if ( Enum . TryParse ( audioString , 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
// 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
{
2021-05-16 12:49:11 +00:00
if ( reader . TryGetString ( index , out var serviceName ) )
2018-09-12 17:26:21 +00:00
{
2021-05-16 12:49:11 +00:00
liveTvChannel . ServiceName = serviceName ;
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
}
2021-05-16 12:49:11 +00:00
if ( reader . TryGetBoolean ( index + + , out var isInMixedFolder ) )
2016-11-20 09:46:07 +00:00
{
2021-05-19 06:51:46 +00:00
item . IsInMixedFolder = isInMixedFolder ;
2016-11-20 09:46:07 +00:00
}
2020-06-15 21:43:52 +00:00
2017-05-22 04:54:02 +00:00
if ( HasField ( query , ItemFields . DateLastSaved ) )
2016-11-20 09:46:07 +00:00
{
2021-05-16 12:49:11 +00:00
if ( reader . TryReadDateTime ( index + + , out var dateLastSaved ) )
2017-05-22 04:54:02 +00:00
{
2021-05-19 17:52:45 +00:00
item . DateLastSaved = dateLastSaved ;
2017-05-22 04:54:02 +00:00
}
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
{
2021-05-16 12:49:11 +00:00
if ( reader . TryGetString ( index + + , out var lockedFields ) )
2016-11-20 09:46:07 +00:00
{
2021-05-22 22:20:19 +00:00
List < MetadataField > fields = null ;
2021-09-24 18:15:10 +00:00
foreach ( var i in lockedFields . AsSpan ( ) . Split ( '|' ) )
2019-01-26 17:21:41 +00:00
{
2021-05-22 22:20:19 +00:00
if ( Enum . TryParse ( i , true , out MetadataField parsedValue ) )
2017-07-25 18:32:03 +00:00
{
2021-05-22 22:20:19 +00:00
( fields ? ? = new List < MetadataField > ( ) ) . Add ( parsedValue ) ;
2019-01-26 17:21:41 +00:00
}
}
2020-06-15 21:43:52 +00:00
2021-05-22 22:20:19 +00:00
item . LockedFields = fields ? . ToArray ( ) ? ? Array . Empty < MetadataField > ( ) ;
2016-11-20 09:46:07 +00:00
}
}
2017-05-22 04:54:02 +00:00
if ( HasField ( query , ItemFields . Studios ) )
2016-11-20 09:46:07 +00:00
{
2021-05-16 12:49:11 +00:00
if ( reader . TryGetString ( index + + , out var studios ) )
2016-11-20 09:46:07 +00:00
{
2021-05-16 12:49:11 +00:00
item . Studios = studios . Split ( '|' , StringSplitOptions . RemoveEmptyEntries ) ;
2016-11-20 09:46:07 +00:00
}
}
2017-05-22 04:54:02 +00:00
if ( HasField ( query , ItemFields . Tags ) )
2016-11-20 09:46:07 +00:00
{
2021-05-16 12:49:11 +00:00
if ( reader . TryGetString ( index + + , out var tags ) )
2016-11-20 09:46:07 +00:00
{
2021-05-16 12:49:11 +00:00
item . Tags = tags . Split ( '|' , StringSplitOptions . RemoveEmptyEntries ) ;
2016-11-20 09:46:07 +00:00
}
}
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
{
2021-05-16 12:49:11 +00:00
if ( reader . TryGetString ( index , out var trailerTypes ) )
2017-05-23 16:43:24 +00:00
{
2021-05-22 22:20:19 +00:00
List < TrailerType > types = null ;
2021-09-24 18:15:10 +00:00
foreach ( var i in trailerTypes . AsSpan ( ) . Split ( '|' ) )
2019-01-26 17:21:41 +00:00
{
2021-05-22 22:20:19 +00:00
if ( Enum . TryParse ( i , true , out TrailerType parsedValue ) )
2017-07-25 18:32:03 +00:00
{
2021-05-22 22:20:19 +00:00
( types ? ? = new List < TrailerType > ( ) ) . Add ( parsedValue ) ;
2019-01-26 17:21:41 +00:00
}
}
2020-06-15 21:43:52 +00:00
2021-09-24 18:15:10 +00:00
trailer . TrailerTypes = types ? . ToArray ( ) ? ? Array . Empty < TrailerType > ( ) ;
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
{
2021-05-16 12:49:11 +00:00
if ( reader . TryGetString ( index + + , out var originalTitle ) )
2016-11-20 09:46:07 +00:00
{
2021-05-16 12:49:11 +00:00
item . OriginalTitle = originalTitle ;
2016-11-20 09:46:07 +00:00
}
}
2019-01-26 17:21:41 +00:00
if ( item is Video video )
2016-11-20 09:46:07 +00:00
{
2021-05-16 12:49:11 +00:00
if ( reader . TryGetString ( index , out var primaryVersionId ) )
2016-11-20 09:46:07 +00:00
{
2021-05-16 12:49:11 +00:00
video . PrimaryVersionId = primaryVersionId ;
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 . DateLastMediaAdded ) )
2016-11-20 09:46:07 +00:00
{
2021-05-16 12:49:11 +00:00
if ( item is Folder folder & & reader . TryReadDateTime ( index , out var dateLastMediaAdded ) )
2016-11-20 09:46:07 +00:00
{
2021-05-16 12:49:11 +00:00
folder . DateLastMediaAdded = dateLastMediaAdded ;
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 + + ;
}
2021-05-16 12:49:11 +00:00
if ( reader . TryGetString ( index + + , out var album ) )
2016-11-20 09:46:07 +00:00
{
2021-05-16 12:49:11 +00:00
item . Album = album ;
2016-11-20 09:46:07 +00:00
}
2020-06-15 21:43:52 +00:00
2021-05-16 17:05:56 +00:00
if ( reader . TryGetSingle ( index + + , out var criticRating ) )
2016-11-20 09:46:07 +00:00
{
2021-05-16 12:49:11 +00:00
item . CriticRating = criticRating ;
2016-11-20 09:46:07 +00:00
}
2020-06-15 21:43:52 +00:00
2021-05-16 12:49:11 +00:00
if ( reader . TryGetBoolean ( index + + , out var isVirtualItem ) )
2016-11-20 09:46:07 +00:00
{
2021-05-19 06:51:46 +00:00
item . IsVirtualItem = isVirtualItem ;
2016-11-20 09:46:07 +00:00
}
2020-06-15 21:43:52 +00:00
2019-01-26 17:21:41 +00:00
if ( item is IHasSeries hasSeriesName )
2016-11-20 09:46:07 +00:00
{
2021-05-16 12:49:11 +00:00
if ( reader . TryGetString ( index , out var seriesName ) )
2016-11-20 09:46:07 +00:00
{
2021-05-16 12:49:11 +00:00
hasSeriesName . SeriesName = seriesName ;
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
{
2021-05-16 12:49:11 +00:00
if ( reader . TryGetString ( index , out var seasonName ) )
2017-05-23 16:43:24 +00:00
{
2021-05-16 12:49:11 +00:00
episode . SeasonName = seasonName ;
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 + + ;
2021-05-16 12:49:11 +00:00
if ( reader . TryGetGuid ( index , out var seasonId ) )
2017-05-23 16:43:24 +00:00
{
2021-05-19 17:52:45 +00:00
episode . SeasonId = seasonId ;
2017-05-23 16:43:24 +00:00
}
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
{
2021-05-16 12:49:11 +00:00
if ( reader . TryGetGuid ( index , out var seriesId ) )
2017-05-23 16:56:01 +00:00
{
2021-05-19 17:52:45 +00:00
hasSeries . SeriesId = seriesId ;
2017-05-23 16:56:01 +00:00
}
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
{
2021-05-16 12:49:11 +00:00
if ( reader . TryGetString ( index + + , out var presentationUniqueKey ) )
2017-05-21 07:25:49 +00:00
{
2021-05-16 12:49:11 +00:00
item . PresentationUniqueKey = presentationUniqueKey ;
2017-05-21 07:25:49 +00:00
}
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
{
2021-05-16 17:05:56 +00:00
if ( reader . TryGetInt32 ( index + + , out var parentalRating ) )
2017-05-21 07:25:49 +00:00
{
2021-05-19 06:51:46 +00:00
item . InheritedParentalRatingValue = parentalRating ;
2017-05-21 07:25:49 +00:00
}
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
{
2021-05-16 12:49:11 +00:00
if ( reader . TryGetString ( index + + , out var externalSeriesId ) )
2017-05-21 07:25:49 +00:00
{
2021-05-16 12:49:11 +00:00
item . ExternalSeriesId = externalSeriesId ;
2017-05-21 07:25:49 +00:00
}
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
{
2021-05-16 12:49:11 +00:00
if ( reader . TryGetString ( index + + , out var tagLine ) )
2016-11-20 09:46:07 +00:00
{
2021-05-16 12:49:11 +00:00
item . Tagline = tagLine ;
2016-11-20 09:46:07 +00:00
}
}
2021-05-16 12:49:11 +00:00
if ( item . ProviderIds . Count = = 0 & & reader . TryGetString ( index , out var providerIds ) )
2016-11-20 09:46:07 +00:00
{
2021-05-16 12:49:11 +00:00
DeserializeProviderIds ( providerIds , item ) ;
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 ( query . DtoOptions . EnableImages )
{
2021-05-16 12:49:11 +00:00
if ( item . ImageInfos . Length = = 0 & & reader . TryGetString ( index , out var imageInfos ) )
2016-11-20 09:46:07 +00:00
{
2021-05-16 12:49:11 +00:00
item . ImageInfos = DeserializeImages ( imageInfos ) ;
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 . ProductionLocations ) )
2016-11-20 09:46:07 +00:00
{
2021-05-16 12:49:11 +00:00
if ( reader . TryGetString ( index + + , out var productionLocations ) )
2016-11-20 09:46:07 +00:00
{
2021-05-16 12:49:11 +00:00
item . ProductionLocations = productionLocations . Split ( '|' , StringSplitOptions . RemoveEmptyEntries ) ;
2016-11-20 09:46:07 +00:00
}
}
2018-09-12 17:26:21 +00:00
if ( HasField ( query , ItemFields . ExtraIds ) )
2016-11-20 09:46:07 +00:00
{
2021-05-16 12:49:11 +00:00
if ( reader . TryGetString ( index + + , out var extraIds ) )
2016-11-20 09:46:07 +00:00
{
2021-05-16 12:49:11 +00:00
item . ExtraIds = SplitToGuids ( extraIds ) ;
2016-11-20 09:46:07 +00:00
}
}
2021-05-16 17:05:56 +00:00
if ( reader . TryGetInt32 ( index + + , out var totalBitrate ) )
2016-11-20 09:46:07 +00:00
{
2021-05-16 12:49:11 +00:00
item . TotalBitrate = totalBitrate ;
2016-11-20 09:46:07 +00:00
}
2020-06-15 21:43:52 +00:00
2021-05-16 12:49:11 +00:00
if ( reader . TryGetString ( index + + , out var extraTypeString ) )
2016-11-20 09:46:07 +00:00
{
2021-05-16 12:49:11 +00:00
if ( Enum . TryParse ( extraTypeString , 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
2017-05-23 16:43:24 +00:00
if ( hasArtistFields )
2016-11-20 09:46:07 +00:00
{
2021-05-16 12:49:11 +00:00
if ( item is IHasArtist hasArtists & & reader . TryGetString ( index , out var artists ) )
2017-05-23 16:43:24 +00:00
{
2021-05-16 12:49:11 +00:00
hasArtists . Artists = artists . Split ( '|' , 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
2021-05-16 12:49:11 +00:00
if ( item is IHasAlbumArtist hasAlbumArtists & & reader . TryGetString ( index , out var albumArtists ) )
2017-05-23 16:43:24 +00:00
{
2021-05-16 12:49:11 +00:00
hasAlbumArtists . AlbumArtists = albumArtists . Split ( '|' , 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
}
2021-05-16 12:49:11 +00:00
if ( reader . TryGetString ( index + + , out var externalId ) )
2016-11-20 09:46:07 +00:00
{
2021-05-16 12:49:11 +00:00
item . ExternalId = externalId ;
2016-11-20 09:46:07 +00:00
}
2020-06-15 21:43:52 +00:00
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
{
2021-05-16 12:49:11 +00:00
if ( reader . TryGetString ( index , out var seriesPresentationUniqueKey ) )
2017-05-22 04:54:02 +00:00
{
2021-05-16 12:49:11 +00:00
hasSeries . SeriesPresentationUniqueKey = seriesPresentationUniqueKey ;
2017-05-22 04:54:02 +00:00
}
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 )
{
2021-05-16 12:49:11 +00:00
if ( item is LiveTvProgram program & & reader . TryGetString ( index , out var showId ) )
2017-09-08 16:13:58 +00:00
{
2021-05-16 12:49:11 +00:00
program . ShowId = showId ;
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
}
2021-05-16 12:49:11 +00:00
if ( reader . TryGetGuid ( index , out var ownerId ) )
2017-09-18 16:52:22 +00:00
{
2021-05-19 17:52:45 +00:00
item . OwnerId = ownerId ;
2017-09-18 16:52:22 +00:00
}
2020-06-15 21:43:52 +00:00
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 ;
}
2021-09-03 16:46:34 +00:00
/// <inheritdoc />
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
}
}
2021-09-03 16:46:34 +00:00
/// <inheritdoc />
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>
2021-05-21 17:35:00 +00:00
private ChapterInfo GetChapter ( IReadOnlyList < ResultSetValue > reader , BaseItem item )
2016-11-20 09:46:07 +00:00
{
var chapter = new ChapterInfo
{
StartPositionTicks = reader . GetInt64 ( 0 )
} ;
2021-05-16 12:49:11 +00:00
if ( reader . TryGetString ( 1 , out var chapterName ) )
2016-11-20 09:46:07 +00:00
{
2021-05-16 12:49:11 +00:00
chapter . Name = chapterName ;
2016-11-20 09:46:07 +00:00
}
2021-05-16 12:49:11 +00:00
if ( reader . TryGetString ( 2 , out var imagePath ) )
2016-11-20 09:46:07 +00:00
{
2021-05-16 12:49:11 +00:00
chapter . ImagePath = imagePath ;
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
}
2021-05-16 12:49:11 +00:00
if ( reader . TryReadDateTime ( 3 , out var imageDateModified ) )
2016-11-20 09:46:07 +00:00
{
2021-05-19 17:52:45 +00:00
chapter . ImageDateModified = imageDateModified ;
2016-11-20 09:46:07 +00:00
}
return chapter ;
}
/// <summary>
/// Saves the chapters.
/// </summary>
2021-10-02 17:03:04 +00:00
/// <param name="id">The item id.</param>
/// <param name="chapters">The chapters.</param>
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
{
2020-10-12 18:05:11 +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 + + )
{
2021-09-03 16:46:34 +00:00
insertText . AppendFormat ( CultureInfo . InvariantCulture , "(@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
}
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 bool HasProgramAttributes ( InternalItemsQuery query )
{
2021-12-12 02:31:30 +00:00
if ( query . ParentType ! = null & & _programExcludeParentTypes . Contains ( query . ParentType . Value ) )
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 bool HasServiceName ( InternalItemsQuery query )
{
2021-12-12 02:31:30 +00:00
if ( query . ParentType ! = null & & _programExcludeParentTypes . Contains ( query . ParentType . Value ) )
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 bool HasStartDate ( InternalItemsQuery query )
{
2021-12-12 02:31:30 +00:00
if ( query . ParentType ! = null & & _programExcludeParentTypes . Contains ( query . ParentType . Value ) )
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 ;
}
2021-12-12 02:31:30 +00:00
return query . IncludeItemTypes . Contains ( BaseItemKind . Episode ) ;
2017-05-23 16:43:24 +00:00
}
private bool HasTrailerTypes ( InternalItemsQuery query )
{
if ( query . IncludeItemTypes . Length = = 0 )
{
return true ;
}
2021-12-12 02:31:30 +00:00
return query . IncludeItemTypes . Contains ( BaseItemKind . Trailer ) ;
2017-05-23 16:43:24 +00:00
}
2019-02-08 21:59:28 +00:00
private bool HasArtistFields ( InternalItemsQuery query )
{
2021-12-12 02:31:30 +00:00
if ( query . ParentType ! = null & & _artistExcludeParentTypes . Contains ( query . ParentType . Value ) )
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
}
2017-05-23 16:56:01 +00:00
private bool HasSeriesFields ( InternalItemsQuery query )
{
2021-12-12 02:31:30 +00:00
if ( query . ParentType = = BaseItemKind . PhotoAlbum )
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
}
2021-05-31 11:55:54 +00:00
private void SetFinalColumnsToSelect ( InternalItemsQuery query , List < string > columns )
2016-11-20 09:46:07 +00:00
{
2021-10-02 17:03:04 +00:00
foreach ( var field in _allItemFields )
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
{
2021-04-30 13:09:36 +00:00
switch ( field )
2016-11-20 09:46:07 +00:00
{
2021-04-30 13:09:36 +00:00
case ItemFields . Settings :
2021-05-23 22:30:41 +00:00
columns . Remove ( "IsLocked" ) ;
columns . Remove ( "PreferredMetadataCountryCode" ) ;
columns . Remove ( "PreferredMetadataLanguage" ) ;
columns . Remove ( "LockedFields" ) ;
2021-04-30 13:09:36 +00:00
break ;
case ItemFields . ServiceName :
2021-05-23 22:30:41 +00:00
columns . Remove ( "ExternalServiceId" ) ;
2021-04-30 13:09:36 +00:00
break ;
case ItemFields . SortName :
2021-05-23 22:30:41 +00:00
columns . Remove ( "ForcedSortName" ) ;
2021-04-30 13:09:36 +00:00
break ;
case ItemFields . Taglines :
2021-05-23 22:30:41 +00:00
columns . Remove ( "Tagline" ) ;
2021-04-30 13:09:36 +00:00
break ;
case ItemFields . Tags :
2021-05-23 22:30:41 +00:00
columns . Remove ( "Tags" ) ;
2021-04-30 13:09:36 +00:00
break ;
case ItemFields . IsHD :
// do nothing
break ;
default :
2021-05-23 22:30:41 +00:00
columns . Remove ( field . ToString ( ) ) ;
2021-04-30 13:09:36 +00:00
break ;
2016-11-20 09:46:07 +00:00
}
}
}
2017-05-22 04:54:02 +00:00
if ( ! HasProgramAttributes ( query ) )
{
2021-05-23 22:30:41 +00:00
columns . Remove ( "IsMovie" ) ;
columns . Remove ( "IsSeries" ) ;
columns . Remove ( "EpisodeTitle" ) ;
columns . Remove ( "IsRepeat" ) ;
columns . Remove ( "ShowId" ) ;
2017-05-22 04:54:02 +00:00
}
2017-05-23 16:43:24 +00:00
if ( ! HasEpisodeAttributes ( query ) )
{
2021-05-23 22:30:41 +00:00
columns . Remove ( "SeasonName" ) ;
columns . Remove ( "SeasonId" ) ;
2017-05-23 16:43:24 +00:00
}
if ( ! HasStartDate ( query ) )
{
2021-05-23 22:30:41 +00:00
columns . Remove ( "StartDate" ) ;
2017-05-23 16:43:24 +00:00
}
if ( ! HasTrailerTypes ( query ) )
{
2021-05-23 22:30:41 +00:00
columns . Remove ( "TrailerTypes" ) ;
2017-05-23 16:43:24 +00:00
}
if ( ! HasArtistFields ( query ) )
{
2021-05-23 22:30:41 +00:00
columns . Remove ( "AlbumArtists" ) ;
columns . Remove ( "Artists" ) ;
2017-05-23 16:43:24 +00:00
}
2017-05-23 16:56:01 +00:00
if ( ! HasSeriesFields ( query ) )
{
2021-05-23 22:30:41 +00:00
columns . Remove ( "SeriesId" ) ;
2017-05-23 16:56:01 +00:00
}
2017-05-23 16:43:24 +00:00
if ( ! HasEpisodeAttributes ( query ) )
{
2021-05-23 22:30:41 +00:00
columns . Remove ( "SeasonName" ) ;
columns . Remove ( "SeasonId" ) ;
2017-05-23 16:43:24 +00:00
}
2016-11-20 09:46:07 +00:00
if ( ! query . DtoOptions . EnableImages )
{
2021-05-23 22:30:41 +00:00
columns . Remove ( "Images" ) ;
2016-11-20 09:46:07 +00:00
}
if ( EnableJoinUserData ( query ) )
{
2021-05-23 22:30:41 +00:00
columns . Add ( "UserDatas.UserId" ) ;
columns . Add ( "UserDatas.lastPlayedDate" ) ;
columns . Add ( "UserDatas.playbackPositionTicks" ) ;
columns . Add ( "UserDatas.playcount" ) ;
columns . Add ( "UserDatas.isFavorite" ) ;
columns . Add ( "UserDatas.played" ) ;
columns . 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 ) )
{
2020-11-09 19:15:15 +00:00
builder . Append ( "(OfficialRating is null * 10)" ) ;
2018-09-12 17:26:21 +00:00
}
else
{
2020-11-09 19:15:15 +00:00
builder . Append ( "(OfficialRating=@ItemOfficialRating * 10)" ) ;
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 ( 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
2020-11-09 19:15:15 +00:00
// genres, tags, studios, person, year?
builder . Append ( "+ (Select count(1) * 10 from ItemValues where ItemId=Guid and CleanValue in (select CleanValue from itemvalues where ItemId=@SimilarItemId))" ) ;
if ( item is MusicArtist )
{
// Match albums where the artist is AlbumArtist against other albums.
// It is assumed that similar albums => similar artists.
builder . Append (
@ "+ (WITH artistValues AS (
SELECT DISTINCT albumValues . CleanValue
FROM ItemValues albumValues
INNER JOIN ItemValues artistAlbums ON albumValues . ItemId = artistAlbums . ItemId
INNER JOIN TypedBaseItems artistItem ON artistAlbums . CleanValue = artistItem . CleanName AND artistAlbums . TYPE = 1 AND artistItem . Guid = @SimilarItemId
) , similarArtist AS (
SELECT albumValues . ItemId
FROM ItemValues albumValues
INNER JOIN ItemValues artistAlbums ON albumValues . ItemId = artistAlbums . ItemId
INNER JOIN TypedBaseItems artistItem ON artistAlbums . CleanValue = artistItem . CleanName AND artistAlbums . TYPE = 1 AND artistItem . Guid = A . Guid
) SELECT COUNT ( DISTINCT ( CleanValue ) ) * 10 FROM ItemValues WHERE ItemId IN ( SELECT ItemId FROM similarArtist ) AND CleanValue IN ( SELECT CleanValue FROM artistValues ) ) ");
}
2016-11-20 09:46:07 +00:00
builder . Append ( ") as SimilarityScore" ) ;
2021-05-23 22:30:41 +00:00
columns . Add ( builder . ToString ( ) ) ;
2016-11-20 09:46:07 +00:00
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" ) ;
2021-05-23 22:30:41 +00:00
columns . Add ( builder . ToString ( ) ) ;
2018-09-12 17:26:21 +00:00
}
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 )
{
2021-05-23 22:30:41 +00:00
var enableGroupByPresentationUniqueKey = EnableGroupByPresentationUniqueKey ( query ) ;
if ( enableGroupByPresentationUniqueKey & & query . GroupBySeriesPresentationUniqueKey )
2016-11-20 09:46:07 +00:00
{
2021-05-23 22:30:41 +00:00
return " Group by PresentationUniqueKey, SeriesPresentationUniqueKey" ;
2016-11-20 09:46:07 +00:00
}
2021-05-23 22:30:41 +00:00
if ( enableGroupByPresentationUniqueKey )
2017-08-01 16:45:57 +00:00
{
2021-05-23 22:30:41 +00:00
return " Group by PresentationUniqueKey" ;
2017-08-01 16:45:57 +00:00
}
2021-05-23 22:30:41 +00:00
if ( query . GroupBySeriesPresentationUniqueKey )
2016-11-20 09:46:07 +00:00
{
2021-05-23 22:30:41 +00:00
return " Group by SeriesPresentationUniqueKey" ;
2016-11-20 09:46:07 +00:00
}
return string . Empty ;
}
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 ;
}
2021-05-31 11:55:54 +00:00
var columns = new List < string > { "count(distinct PresentationUniqueKey)" } ;
SetFinalColumnsToSelect ( query , columns ) ;
var commandTextBuilder = new StringBuilder ( "select " , 256 )
. AppendJoin ( ',' , columns )
2021-05-23 22:30:41 +00:00
. Append ( FromText )
. Append ( 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 )
{
2021-05-23 22:30:41 +00:00
commandTextBuilder . Append ( " where " )
. AppendJoin ( " AND " , whereClauses ) ;
2019-01-26 17:21:41 +00:00
}
2016-12-12 19:40:27 +00:00
2021-05-23 22:30:41 +00:00
var commandText = commandTextBuilder . ToString ( ) ;
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
2021-12-16 00:57:39 +00:00
var columns = _retrieveItemColumns . ToList ( ) ;
2021-05-31 11:55:54 +00:00
SetFinalColumnsToSelect ( query , columns ) ;
var commandTextBuilder = new StringBuilder ( "select " , 1024 )
. AppendJoin ( ',' , columns )
2021-05-23 22:30:41 +00:00
. Append ( FromText )
. Append ( 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 )
{
2021-05-23 22:30:41 +00:00
commandTextBuilder . Append ( " where " )
. AppendJoin ( " AND " , whereClauses ) ;
2019-01-26 17:21:41 +00:00
}
2016-11-20 09:46:07 +00:00
2021-05-23 22:30:41 +00:00
commandTextBuilder . Append ( GetGroupBy ( query ) )
. Append ( 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 )
{
2021-05-23 22:30:41 +00:00
commandTextBuilder . Append ( " LIMIT " )
2021-05-31 11:55:54 +00:00
. Append ( query . Limit ? ? int . MaxValue ) ;
2016-11-20 09:46:07 +00:00
}
if ( offset > 0 )
{
2021-05-23 22:30:41 +00:00
commandTextBuilder . Append ( " OFFSET " )
2021-05-31 11:55:54 +00:00
. Append ( offset ) ;
2016-11-20 09:46:07 +00:00
}
}
2021-05-23 22:30:41 +00:00
var commandText = commandTextBuilder . ToString ( ) ;
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 )
{
2021-04-22 13:28:24 +00:00
buffer = buffer . Replace ( ' \ u2013 ' , '-' ) ; // en dash
buffer = buffer . Replace ( ' \ u2014 ' , '-' ) ; // em dash
buffer = buffer . Replace ( ' \ u2015 ' , '-' ) ; // horizontal bar
buffer = buffer . Replace ( ' \ u2017 ' , '_' ) ; // double low line
buffer = buffer . Replace ( ' \ u2018 ' , '\'' ) ; // left single quotation mark
buffer = buffer . Replace ( ' \ u2019 ' , '\'' ) ; // right single quotation mark
buffer = buffer . Replace ( ' \ u201a ' , ',' ) ; // single low-9 quotation mark
buffer = buffer . Replace ( ' \ u201b ' , '\'' ) ; // single high-reversed-9 quotation mark
buffer = buffer . Replace ( ' \ u201c ' , '\"' ) ; // left double quotation mark
buffer = buffer . Replace ( ' \ u201d ' , '\"' ) ; // right double quotation mark
buffer = buffer . Replace ( ' \ u201e ' , '\"' ) ; // double low-9 quotation mark
buffer = buffer . Replace ( "\u2026" , "..." , StringComparison . Ordinal ) ; // horizontal ellipsis
buffer = buffer . Replace ( ' \ u2032 ' , '\'' ) ; // prime
buffer = buffer . Replace ( ' \ u2033 ' , '\"' ) ; // double prime
buffer = buffer . Replace ( ' \ u0060 ' , '\'' ) ; // grave accent
return buffer . Replace ( ' \ u00B4 ' , '\'' ) ; // acute accent
2018-09-12 17:26:21 +00:00
}
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 ;
}
2021-12-16 00:57:39 +00:00
var columns = _retrieveItemColumns . ToList ( ) ;
2021-05-31 11:55:54 +00:00
SetFinalColumnsToSelect ( query , columns ) ;
var commandTextBuilder = new StringBuilder ( "select " , 512 )
. AppendJoin ( ',' , columns )
2021-05-23 22:30:41 +00:00
. Append ( FromText )
. Append ( GetJoinUserDataText ( query ) ) ;
2016-11-20 09:46:07 +00:00
var whereClauses = GetWhereClauses ( query , null ) ;
var whereText = whereClauses . Count = = 0 ?
string . Empty :
2021-05-23 22:30:41 +00:00
string . Join ( " AND " , whereClauses ) ;
2016-11-20 09:46:07 +00:00
2021-05-23 22:30:41 +00:00
if ( ! string . IsNullOrEmpty ( whereText ) )
{
commandTextBuilder . Append ( " where " )
. Append ( whereText ) ;
}
commandTextBuilder . Append ( GetGroupBy ( query ) )
. Append ( 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 )
{
2021-05-23 22:30:41 +00:00
commandTextBuilder . Append ( " LIMIT " )
2021-05-31 11:55:54 +00:00
. Append ( query . Limit ? ? int . MaxValue ) ;
2016-11-20 09:46:07 +00:00
}
if ( offset > 0 )
{
2021-05-23 22:30:41 +00:00
commandTextBuilder . Append ( " OFFSET " )
2021-05-31 11:55:54 +00:00
. Append ( offset ) ;
2016-11-20 09:46:07 +00:00
}
}
2016-11-25 17:36:00 +00:00
var isReturningZeroItems = query . Limit . HasValue & & query . Limit < = 0 ;
2021-05-23 22:30:41 +00:00
var itemQuery = string . Empty ;
var totalRecordCountQuery = string . Empty ;
2016-11-25 17:36:00 +00:00
if ( ! isReturningZeroItems )
{
2021-05-23 22:30:41 +00:00
itemQuery = commandTextBuilder . ToString ( ) ;
2016-11-25 17:36:00 +00:00
}
2020-06-15 21:43:52 +00:00
2016-11-25 17:36:00 +00:00
if ( query . EnableTotalRecordCount )
{
2021-05-23 22:30:41 +00:00
commandTextBuilder . Clear ( ) ;
commandTextBuilder . Append ( " select " ) ;
2016-11-25 17:36:00 +00:00
2021-05-31 11:55:54 +00:00
List < string > columnsToSelect ;
2016-11-25 17:36:00 +00:00
if ( EnableGroupByPresentationUniqueKey ( query ) )
{
2021-05-31 11:55:54 +00:00
columnsToSelect = new List < string > { "count (distinct PresentationUniqueKey)" } ;
2016-11-25 17:36:00 +00:00
}
2017-08-01 16:45:57 +00:00
else if ( query . GroupBySeriesPresentationUniqueKey )
{
2021-05-31 11:55:54 +00:00
columnsToSelect = new List < string > { "count (distinct SeriesPresentationUniqueKey)" } ;
2017-08-01 16:45:57 +00:00
}
2016-11-25 17:36:00 +00:00
else
{
2021-05-31 11:55:54 +00:00
columnsToSelect = new List < string > { "count (guid)" } ;
2016-11-25 17:36:00 +00:00
}
2021-05-31 11:55:54 +00:00
SetFinalColumnsToSelect ( query , columnsToSelect ) ;
commandTextBuilder . AppendJoin ( ',' , columnsToSelect )
. Append ( FromText )
2021-05-23 22:30:41 +00:00
. Append ( GetJoinUserDataText ( query ) ) ;
if ( ! string . IsNullOrEmpty ( whereText ) )
{
commandTextBuilder . Append ( " where " )
. Append ( whereText ) ;
}
totalRecordCountQuery = commandTextBuilder . ToString ( ) ;
2016-11-25 17:36:00 +00:00
}
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
{
2020-10-12 18:05:11 +00:00
connection . RunInTransaction (
db = >
2016-11-20 09:46:07 +00:00
{
2021-05-23 22:30:41 +00:00
var itemQueryStatement = PrepareStatement ( db , itemQuery ) ;
var totalRecordCountQueryStatement = PrepareStatement ( db , totalRecordCountQuery ) ;
2016-12-12 05:49:19 +00:00
2019-02-20 13:26:49 +00:00
if ( ! isReturningZeroItems )
{
2021-05-23 22:30:41 +00:00
using ( var statement = itemQueryStatement )
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
}
2021-05-23 22:30:41 +00:00
LogQueryTime ( "GetItems.ItemQuery" , itemQuery , now ) ;
2019-02-20 13:26:49 +00:00
}
2016-11-20 09:46:07 +00:00
2021-05-23 22:30:41 +00:00
now = DateTime . UtcNow ;
2019-02-20 13:26:49 +00:00
if ( query . EnableTotalRecordCount )
{
2021-05-23 22:30:41 +00:00
using ( var statement = totalRecordCountQueryStatement )
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
}
2021-05-23 22:30:41 +00:00
LogQueryTime ( "GetItems.TotalRecordCount" , totalRecordCountQuery , now ) ;
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
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 ;
}
2021-02-12 23:39:18 +00:00
return " ORDER BY " + string . Join ( ',' , orderBy . Select ( i = >
2016-11-20 09:46:07 +00:00
{
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 ;
2021-05-31 11:55:54 +00:00
var columns = new List < string > { "guid" } ;
SetFinalColumnsToSelect ( query , columns ) ;
var commandTextBuilder = new StringBuilder ( "select " , 256 )
. AppendJoin ( ',' , columns )
2021-05-23 22:30:41 +00:00
. Append ( FromText )
. Append ( 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 )
{
2021-05-23 22:30:41 +00:00
commandTextBuilder . Append ( " where " )
. AppendJoin ( " AND " , whereClauses ) ;
2019-01-26 17:21:41 +00:00
}
2016-11-20 09:46:07 +00:00
2021-05-23 22:30:41 +00:00
commandTextBuilder . Append ( GetGroupBy ( query ) )
. Append ( 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 )
{
2021-05-23 22:30:41 +00:00
commandTextBuilder . Append ( " LIMIT " )
2021-05-31 11:55:54 +00:00
. Append ( query . Limit ? ? int . MaxValue ) ;
2016-11-20 09:46:07 +00:00
}
if ( offset > 0 )
{
2021-05-23 22:30:41 +00:00
commandTextBuilder . Append ( " OFFSET " )
2021-05-31 11:55:54 +00:00
. Append ( offset ) ;
2016-11-20 09:46:07 +00:00
}
}
2021-05-23 22:30:41 +00:00
var commandText = commandTextBuilder . ToString ( ) ;
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 ;
2021-05-31 11:55:54 +00:00
var columns = new List < string > { "guid" , "path" } ;
SetFinalColumnsToSelect ( query , columns ) ;
var commandText = "select " + string . Join ( ',' , columns ) + FromText ;
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-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 ) ;
2016-11-21 03:52:58 +00:00
2021-05-16 12:49:11 +00:00
row . TryGetString ( 1 , out var path ) ;
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 ;
2021-05-31 11:55:54 +00:00
var columns = new List < string > { "guid" } ;
SetFinalColumnsToSelect ( query , columns ) ;
2019-01-26 17:21:41 +00:00
var commandText = "select "
2021-05-31 11:55:54 +00:00
+ string . Join ( ',' , columns )
2021-05-23 22:30:41 +00:00
+ FromText
2019-01-26 17:21:41 +00:00
+ 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 ;
2021-05-31 11:55:54 +00:00
List < string > columnsToSelect ;
2016-11-25 17:36:00 +00:00
if ( EnableGroupByPresentationUniqueKey ( query ) )
{
2021-05-31 11:55:54 +00:00
columnsToSelect = new List < string > { "count (distinct PresentationUniqueKey)" } ;
2016-11-25 17:36:00 +00:00
}
2017-08-01 16:45:57 +00:00
else if ( query . GroupBySeriesPresentationUniqueKey )
{
2021-05-31 11:55:54 +00:00
columnsToSelect = new List < string > { "count (distinct SeriesPresentationUniqueKey)" } ;
2017-08-01 16:45:57 +00:00
}
2016-11-25 17:36:00 +00:00
else
{
2021-05-31 11:55:54 +00:00
columnsToSelect = new List < string > { "count (guid)" } ;
2016-11-25 17:36:00 +00:00
}
2021-05-31 11:55:54 +00:00
SetFinalColumnsToSelect ( query , columnsToSelect ) ;
commandText + = " select " + string . Join ( ',' , columnsToSelect ) + FromText ;
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
{
2020-10-12 18:05:11 +00:00
connection . RunInTransaction (
db = >
2016-11-20 09:46:07 +00:00
{
2020-09-09 11:38:27 +00:00
var statements = PrepareAll ( db , statementTexts ) ;
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 )
{
2020-09-09 12:22:27 +00:00
using ( var statement = statements [ statements . Length - 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
2021-12-12 02:31:30 +00:00
| | query . IncludeItemTypes . Contains ( BaseItemKind . Movie )
| | query . IncludeItemTypes . Contains ( BaseItemKind . 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
}
2021-12-12 02:31:30 +00:00
var includeTypes = query . IncludeItemTypes ;
2018-09-12 17:26:21 +00:00
// Only specify excluded types if no included types are specified
2021-12-12 02:31:30 +00:00
if ( query . IncludeItemTypes . Length = = 0 )
2016-11-20 09:46:07 +00:00
{
2021-12-12 02:31:30 +00:00
var excludeTypes = query . ExcludeItemTypes ;
2018-09-12 17:26:21 +00:00
if ( excludeTypes . Length = = 1 )
{
2021-12-16 00:57:39 +00:00
if ( _baseItemKindNames . TryGetValue ( excludeTypes [ 0 ] , out var excludeTypeName ) )
{
whereClauses . Add ( "type<>@type" ) ;
statement ? . TryBind ( "@type" , excludeTypeName ) ;
}
2021-12-16 14:54:22 +00:00
else
{
Logger . LogWarning ( "Undefined BaseItemKind to Type mapping: {BaseItemKind}" , excludeTypes [ 0 ] ) ;
}
2018-09-12 17:26:21 +00:00
}
else if ( excludeTypes . Length > 1 )
2016-11-20 09:46:07 +00:00
{
2021-12-16 14:54:22 +00:00
var whereBuilder = new StringBuilder ( ) ;
foreach ( var excludeType in excludeTypes )
{
if ( _baseItemKindNames . TryGetValue ( excludeType , out var baseItemKindName ) )
{
whereBuilder
. Append ( '\'' )
. Append ( baseItemKindName )
. Append ( "'," ) ;
}
else
{
Logger . LogWarning ( "Undefined BaseItemKind to Type mapping: {BaseItemKind}" , excludeType ) ;
}
}
// Remove trailing comma.
whereBuilder . Length - - ;
whereClauses . Add ( $"type not in ({whereBuilder})" ) ;
2016-11-20 09:46:07 +00:00
}
}
2020-02-19 20:56:35 +00:00
else if ( includeTypes . Length = = 1 )
{
2021-12-16 00:57:39 +00:00
if ( _baseItemKindNames . TryGetValue ( includeTypes [ 0 ] , out var includeTypeName ) )
{
whereClauses . Add ( "type=@type" ) ;
statement ? . TryBind ( "@type" , includeTypeName ) ;
}
2021-12-16 14:54:22 +00:00
else
{
Logger . LogWarning ( "Undefined BaseItemKind to Type mapping: {BaseItemKind}" , includeTypes [ 0 ] ) ;
}
2020-02-19 20:56:35 +00:00
}
else if ( includeTypes . Length > 1 )
{
2021-12-16 14:54:22 +00:00
var whereBuilder = new StringBuilder ( ) ;
foreach ( var includeType in includeTypes )
{
if ( _baseItemKindNames . TryGetValue ( includeType , out var baseItemKindName ) )
{
whereBuilder
. Append ( '\'' )
. Append ( baseItemKindName )
. Append ( "'," ) ;
}
else
{
Logger . LogWarning ( "Undefined BaseItemKind to Type mapping: {BaseItemKind}" , includeType ) ;
}
}
// Remove trailing comma.
whereBuilder . Length - - ;
whereClauses . Add ( $"type in ({whereBuilder})" ) ;
2020-02-19 20:56:35 +00:00
}
2016-11-20 09:46:07 +00:00
2020-11-17 03:29:46 +00:00
if ( query . ChannelIds . Count = = 1 )
2016-11-20 09:46:07 +00:00
{
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
}
2020-11-17 03:29:46 +00:00
else if ( query . ChannelIds . Count > 1 )
2016-11-20 09:46:07 +00:00
{
2021-02-12 23:39:18 +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-09-09 11:38:27 +00:00
StringBuilder clauseBuilder = new StringBuilder ( ) ;
const string Or = " OR " ;
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-09-09 11:38:27 +00:00
clauseBuilder . Append ( '(' ) ;
2020-04-03 18:59:38 +00:00
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 ;
2020-09-09 11:38:27 +00:00
clauseBuilder . Append ( "TrailerTypes like " )
2020-04-03 18:59:38 +00:00
. 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-09-09 11:38:27 +00:00
clauseBuilder . Length - = Or . Length ;
clauseBuilder . Append ( ')' ) ;
whereClauses . Add ( clauseBuilder . ToString ( ) ) ;
2020-04-03 18:59:38 +00:00
2020-09-09 11:38:27 +00:00
clauseBuilder . Length = 0 ;
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
}
}
2020-09-09 11:38:27 +00:00
int personIdsLen = query . PersonIds . Length ;
if ( personIdsLen > 0 )
2016-11-20 09:46:07 +00:00
{
2019-01-07 23:27:46 +00:00
// TODO: Should this query with CleanName ?
2016-11-20 20:20:01 +00:00
2020-09-09 11:38:27 +00:00
clauseBuilder . Append ( '(' ) ;
Span < byte > idBytes = stackalloc byte [ 16 ] ;
for ( int i = 0 ; i < personIdsLen ; i + + )
2016-11-20 20:20:01 +00:00
{
2020-09-09 11:38:27 +00:00
string paramName = "@PersonId" + i ;
clauseBuilder . Append ( "(guid in (select itemid from People where Name = (select Name from TypedBaseItems where guid=" )
. Append ( paramName )
. Append ( "))) OR " ) ;
2016-11-20 20:20:01 +00:00
2020-09-09 11:38:27 +00:00
if ( statement ! = null )
{
query . PersonIds [ i ] . TryWriteBytes ( idBytes ) ;
statement . TryBind ( paramName , idBytes ) ;
}
2016-11-20 20:20:01 +00:00
}
2020-04-03 18:59:38 +00:00
2020-09-09 11:38:27 +00:00
// Remove last " OR "
clauseBuilder . Length - = Or . Length ;
clauseBuilder . Append ( ')' ) ;
whereClauses . Add ( clauseBuilder . ToString ( ) ) ;
clauseBuilder . Length = 0 ;
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
2021-12-12 02:31:30 +00:00
if ( query . IncludeItemTypes . Length = = 1 & & query . IncludeItemTypes [ 0 ] = = BaseItemKind . Series )
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 ) ;
}
2020-11-17 03:29:46 +00:00
if ( query . GenreIds . Count > 0 )
2016-11-20 09:46:07 +00:00
{
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
}
2020-11-17 03:29:46 +00:00
if ( query . Genres . Count > 0 )
2016-11-20 09:46:07 +00:00
{
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 )
{
2020-08-20 10:16:24 +00:00
statement . TryBind ( "@Years" , query . Years [ 0 ] . ToString ( CultureInfo . InvariantCulture ) ) ;
2016-11-20 09:46:07 +00:00
}
}
else if ( query . Years . Length > 1 )
{
2021-02-12 23:39:18 +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
{
2021-02-12 23:39:18 +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
}
2021-05-18 10:23:26 +00:00
if ( query . ExcludeProviderIds ! = null & & query . ExcludeProviderIds . Count > 0 )
2016-11-20 09:46:07 +00:00
{
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
}
}
2021-05-18 10:23:26 +00:00
if ( query . HasAnyProviderId ! = null & & query . HasAnyProviderId . Count > 0 )
2018-09-12 17:26:21 +00:00
{
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
2021-02-12 23:39:18 +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
}
2020-11-27 22:12:16 +00:00
if ( query . HasImdbId . HasValue )
2016-11-20 09:46:07 +00:00
{
2020-11-27 22:12:16 +00:00
whereClauses . Add ( GetProviderIdClause ( query . HasImdbId . Value , "imdb" ) ) ;
2016-11-20 09:46:07 +00:00
}
2020-11-27 22:12:16 +00:00
if ( query . HasTmdbId . HasValue )
2016-11-20 09:46:07 +00:00
{
2020-11-27 22:12:16 +00:00
whereClauses . Add ( GetProviderIdClause ( query . HasTmdbId . Value , "tmdb" ) ) ;
2016-11-20 09:46:07 +00:00
}
2020-11-27 22:12:16 +00:00
if ( query . HasTvdbId . HasValue )
2016-11-20 09:46:07 +00:00
{
2020-11-27 22:12:16 +00:00
whereClauses . Add ( GetProviderIdClause ( query . HasTvdbId . Value , "tvdb" ) ) ;
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
2021-05-23 22:30:41 +00:00
if ( queryTopParentIds . Length > 0 )
2016-11-20 09:46:07 +00:00
{
2021-05-23 22:30:41 +00:00
var includedItemByNameTypes = GetItemByNameTypesInQuery ( query ) ;
var enableItemsByName = ( query . IncludeItemsByName ? ? false ) & & includedItemByNameTypes . Count > 0 ;
if ( queryTopParentIds . Length = = 1 )
2016-11-20 09:46:07 +00:00
{
2021-05-23 22:30:41 +00:00
if ( enableItemsByName & & includedItemByNameTypes . Count = = 1 )
2016-11-20 09:46:07 +00:00
{
2021-05-23 22:30:41 +00:00
whereClauses . Add ( "(TopParentId=@TopParentId or Type=@IncludedItemByNameType)" ) ;
statement ? . TryBind ( "@IncludedItemByNameType" , includedItemByNameTypes [ 0 ] ) ;
}
else if ( enableItemsByName & & includedItemByNameTypes . Count > 1 )
{
var itemByNameTypeVal = string . Join ( ',' , includedItemByNameTypes . Select ( i = > "'" + i + "'" ) ) ;
whereClauses . Add ( "(TopParentId=@TopParentId or Type in (" + itemByNameTypeVal + "))" ) ;
}
else
{
whereClauses . Add ( "(TopParentId=@TopParentId)" ) ;
2016-11-20 09:46:07 +00:00
}
2020-06-15 21:43:52 +00:00
2021-05-23 22:30:41 +00:00
statement ? . TryBind ( "@TopParentId" , queryTopParentIds [ 0 ] . ToString ( "N" , CultureInfo . InvariantCulture ) ) ;
2016-11-20 09:46:07 +00:00
}
2021-05-23 22:30:41 +00:00
else if ( queryTopParentIds . Length > 1 )
2016-11-20 09:46:07 +00:00
{
2021-05-23 22:30:41 +00:00
var val = string . Join ( ',' , queryTopParentIds . Select ( i = > "'" + i . ToString ( "N" , CultureInfo . InvariantCulture ) + "'" ) ) ;
if ( enableItemsByName & & includedItemByNameTypes . Count = = 1 )
2016-11-20 09:46:07 +00:00
{
2021-05-23 22:30:41 +00:00
whereClauses . Add ( "(Type=@IncludedItemByNameType or TopParentId in (" + val + "))" ) ;
statement ? . TryBind ( "@IncludedItemByNameType" , includedItemByNameTypes [ 0 ] ) ;
}
else if ( enableItemsByName & & includedItemByNameTypes . Count > 1 )
{
var itemByNameTypeVal = string . Join ( ',' , includedItemByNameTypes . Select ( i = > "'" + i + "'" ) ) ;
whereClauses . Add ( "(Type in (" + itemByNameTypeVal + ") or TopParentId in (" + val + "))" ) ;
}
else
{
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 )
{
2021-02-12 23:39:18 +00:00
var inClause = string . Join ( ',' , query . AncestorIds . Select ( i = > "'" + i . ToString ( "N" , CultureInfo . InvariantCulture ) + "'" ) ) ;
2020-08-07 17:26:28 +00:00
whereClauses . Add ( string . Format ( CultureInfo . InvariantCulture , "Guid in (select itemId from AncestorIds where AncestorIdText in ({0}))" , inClause ) ) ;
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 ( ! string . IsNullOrWhiteSpace ( query . AncestorWithPresentationUniqueKey ) )
{
var inClause = "select guid from TypedBaseItems where PresentationUniqueKey=@AncestorWithPresentationUniqueKey" ;
2020-08-07 17:26:28 +00:00
whereClauses . Add ( string . Format ( CultureInfo . InvariantCulture , "Guid in (select itemId from AncestorIds where AncestorId in ({0}))" , inClause ) ) ;
2016-11-20 09:46:07 +00:00
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 ;
}
2020-11-27 22:12:16 +00:00
/// <summary>
/// Formats a where clause for the specified provider.
/// </summary>
/// <param name="includeResults">Whether or not to include items with this provider's ids.</param>
/// <param name="provider">Provider name.</param>
/// <returns>Formatted SQL clause.</returns>
private string GetProviderIdClause ( bool includeResults , string provider )
{
return string . Format (
CultureInfo . InvariantCulture ,
"ProviderIds {0} like '%{1}=%'" ,
includeResults ? string . Empty : "not" ,
provider ) ;
}
2017-07-25 18:32:03 +00:00
private List < string > GetItemByNameTypesInQuery ( InternalItemsQuery query )
{
var list = new List < string > ( ) ;
2021-12-12 02:31:30 +00:00
if ( IsTypeInQuery ( BaseItemKind . Person , query ) )
2017-07-25 18:32:03 +00:00
{
2021-04-22 13:28:24 +00:00
list . Add ( typeof ( Person ) . FullName ) ;
2017-07-25 18:32:03 +00:00
}
2020-04-03 18:59:38 +00:00
2021-12-12 02:31:30 +00:00
if ( IsTypeInQuery ( BaseItemKind . Genre , query ) )
2017-07-25 18:32:03 +00:00
{
2021-04-22 13:28:24 +00:00
list . Add ( typeof ( Genre ) . FullName ) ;
2017-07-25 18:32:03 +00:00
}
2020-04-03 18:59:38 +00:00
2021-12-12 02:31:30 +00:00
if ( IsTypeInQuery ( BaseItemKind . MusicGenre , query ) )
2017-07-25 18:32:03 +00:00
{
2021-04-22 13:28:24 +00:00
list . Add ( typeof ( MusicGenre ) . FullName ) ;
2017-07-25 18:32:03 +00:00
}
2020-04-03 18:59:38 +00:00
2021-12-12 02:31:30 +00:00
if ( IsTypeInQuery ( BaseItemKind . MusicArtist , query ) )
2017-07-25 18:32:03 +00:00
{
2021-04-22 13:28:24 +00:00
list . Add ( typeof ( MusicArtist ) . FullName ) ;
2017-07-25 18:32:03 +00:00
}
2020-04-03 18:59:38 +00:00
2021-12-12 02:31:30 +00:00
if ( IsTypeInQuery ( BaseItemKind . Studio , query ) )
2017-07-25 18:32:03 +00:00
{
2021-04-22 13:28:24 +00:00
list . Add ( typeof ( Studio ) . FullName ) ;
2017-07-25 18:32:03 +00:00
}
return list ;
}
2021-12-12 02:31:30 +00:00
private bool IsTypeInQuery ( BaseItemKind type , InternalItemsQuery query )
2017-07-25 18:32:03 +00:00
{
2021-12-12 02:31:30 +00:00
if ( query . ExcludeItemTypes . Contains ( type ) )
2017-07-25 18:32:03 +00:00
{
return false ;
}
2021-12-12 02:31:30 +00:00
return query . IncludeItemTypes . Length = = 0 | | query . IncludeItemTypes . Contains ( type ) ;
2017-07-25 18:32:03 +00:00
}
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 ;
}
2021-12-12 02:31:30 +00:00
if ( query . IncludeItemTypes . Contains ( BaseItemKind . Episode )
| | query . IncludeItemTypes . Contains ( BaseItemKind . Video )
| | query . IncludeItemTypes . Contains ( BaseItemKind . Movie )
| | query . IncludeItemTypes . Contains ( BaseItemKind . MusicVideo )
| | query . IncludeItemTypes . Contains ( BaseItemKind . Series )
| | query . IncludeItemTypes . Contains ( BaseItemKind . Season ) )
2016-11-20 09:46:07 +00:00
{
return true ;
}
return false ;
}
2021-04-22 13:28:24 +00:00
public void UpdateInheritedValues ( )
2017-09-15 06:32:20 +00:00
{
2019-08-18 20:05:06 +00:00
string sql = string . Join (
2021-04-22 13:28:24 +00:00
';' ,
2019-08-18 20:05:06 +00:00
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 ( ) )
{
2020-10-12 18:05:11 +00:00
connection . RunInTransaction (
db = >
2019-08-18 20:05:06 +00:00
{
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
}
2021-04-22 13:28:24 +00:00
private static Dictionary < string , string > GetTypeMapDictionary ( )
2016-11-20 09:46:07 +00:00
{
2021-04-22 13:28:24 +00:00
var dict = new Dictionary < string , string > ( StringComparer . OrdinalIgnoreCase ) ;
2016-11-20 09:46:07 +00:00
2020-04-03 18:59:38 +00:00
foreach ( var t in _knownTypes )
2016-11-20 09:46:07 +00:00
{
2021-08-28 22:32:50 +00:00
dict [ t . Name ] = t . FullName ;
2016-11-20 09:46:07 +00:00
}
2021-04-22 13:28:24 +00:00
dict [ "Program" ] = typeof ( LiveTvProgram ) . FullName ;
dict [ "TvChannel" ] = typeof ( LiveTvChannel ) . FullName ;
2016-11-20 09:46:07 +00:00
return dict ;
}
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
{
2020-10-12 18:05:11 +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 ( ) ;
2020-11-05 11:27:22 +00:00
var commandText = new StringBuilder ( "select Distinct p.Name from People p" ) ;
2016-11-20 09:46:07 +00:00
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
{
2020-11-05 11:27:22 +00:00
commandText . Append ( " where " ) . Append ( string . Join ( " AND " , whereClauses ) ) ;
2016-11-20 09:46:07 +00:00
}
2020-11-05 11:27:22 +00:00
commandText . Append ( " order by ListOrder" ) ;
2016-11-20 09:46:07 +00:00
2020-04-03 15:30:01 +00:00
if ( query . Limit > 0 )
{
2020-11-05 11:27:22 +00:00
commandText . Append ( " LIMIT " ) . Append ( 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 > ( ) ;
2020-11-05 11:27:22 +00:00
using ( var statement = PrepareStatement ( connection , commandText . ToString ( ) ) )
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 ( ) ;
2020-11-09 19:15:15 +00:00
var commandText = "select ItemId, Name, Role, PersonType, SortOrder from People p" ;
2016-11-20 09:46:07 +00:00
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 > ( ) ;
2020-12-04 15:00:55 +00:00
if ( query . User ! = null & & query . IsFavorite . HasValue )
{
whereClauses . Add ( @ "p.Name IN (
SELECT Name FROM TypedBaseItems WHERE UserDataKey IN (
SELECT key FROM UserDatas WHERE isFavorite = @IsFavorite AND userId = @UserId )
AND Type = @InternalPersonType ) ");
statement ? . TryBind ( "@IsFavorite" , query . IsFavorite . Value ) ;
statement ? . TryBind ( "@InternalPersonType" , typeof ( Person ) . FullName ) ;
}
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" ) ;
2020-11-05 11:27:22 +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
{
2020-11-05 11:27:22 +00:00
whereClauses . Add ( "p.Name in (Select Name from People where ItemId=@AppearsInItemId)" ) ;
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" ) ;
2020-11-05 11:27:22 +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
{
2021-02-12 23:39:18 +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" ) ;
2020-11-05 11:27:22 +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
{
2021-02-12 23:39:18 +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" ) ;
2020-11-05 11:27:22 +00:00
statement ? . TryBind ( "@MaxListOrder" , query . MaxListOrder . 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 ( ! string . IsNullOrWhiteSpace ( query . NameContains ) )
{
2020-11-05 11:27:22 +00:00
whereClauses . Add ( "p.Name like @NameContains" ) ;
statement ? . TryBind ( "@NameContains" , "%" + query . NameContains + "%" ) ;
}
if ( query . User ! = null )
{
statement ? . TryBind ( "@UserId" , query . User . InternalId ) ;
2016-11-20 09:46:07 +00:00
}
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 ( ) ;
2020-09-09 12:22:27 +00:00
Span < byte > itemIdBlob = stackalloc byte [ 16 ] ;
2020-09-09 11:38:27 +00:00
itemId . TryWriteBytes ( itemIdBlob ) ;
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 + + )
{
2020-08-20 10:16:24 +00:00
insertText . AppendFormat (
CultureInfo . InvariantCulture ,
2020-09-09 11:38:27 +00:00
"(@ItemId, @AncestorId{0}, @AncestorIdText{0})," ,
2020-08-20 10:16:24 +00:00
i . ToString ( CultureInfo . InvariantCulture ) ) ;
2018-09-12 17:26:21 +00:00
}
2020-09-09 11:38:27 +00:00
// Remove last ,
insertText . Length - - ;
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 ] ;
2020-09-09 11:38:27 +00:00
ancestorId . TryWriteBytes ( itemIdBlob ) ;
2018-09-12 17:26:21 +00:00
2020-09-09 11:38:27 +00:00
statement . TryBind ( "@AncestorId" + index , itemIdBlob ) ;
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 ( )
{
2021-04-22 13:28:24 +00:00
return GetItemValueNames ( new [ ] { 3 } , Array . Empty < string > ( ) , Array . Empty < string > ( ) ) ;
2016-11-20 09:46:07 +00:00
}
public List < string > GetAllArtistNames ( )
{
2021-04-22 13:28:24 +00:00
return GetItemValueNames ( new [ ] { 0 , 1 } , Array . Empty < string > ( ) , Array . Empty < string > ( ) ) ;
2016-11-20 09:46:07 +00:00
}
public List < string > GetMusicGenreNames ( )
{
2021-04-22 13:28:24 +00:00
return GetItemValueNames (
new [ ] { 2 } ,
new string [ ]
{
typeof ( Audio ) . FullName ,
typeof ( MusicVideo ) . FullName ,
typeof ( MusicAlbum ) . FullName ,
typeof ( MusicArtist ) . FullName
} ,
Array . Empty < string > ( ) ) ;
2016-11-20 09:46:07 +00:00
}
public List < string > GetGenreNames ( )
{
2021-04-22 13:28:24 +00:00
return GetItemValueNames (
new [ ] { 2 } ,
Array . Empty < string > ( ) ,
new string [ ]
{
typeof ( Audio ) . FullName ,
typeof ( MusicVideo ) . FullName ,
typeof ( MusicAlbum ) . FullName ,
typeof ( MusicArtist ) . FullName
} ) ;
2016-11-20 09:46:07 +00:00
}
2021-04-22 13:28:24 +00:00
private List < string > GetItemValueNames ( int [ ] itemValueTypes , IReadOnlyList < string > withItemTypes , IReadOnlyList < string > excludeItemTypes )
2016-11-20 09:46:07 +00:00
{
CheckDisposed ( ) ;
var now = DateTime . UtcNow ;
2021-05-31 11:55:54 +00:00
var stringBuilder = new StringBuilder ( "Select Value From ItemValues where Type" , 128 ) ;
2021-05-23 22:30:41 +00:00
if ( itemValueTypes . Length = = 1 )
{
stringBuilder . Append ( '=' )
2021-05-31 11:55:54 +00:00
. Append ( itemValueTypes [ 0 ] ) ;
2021-05-23 22:30:41 +00:00
}
else
{
stringBuilder . Append ( " in (" )
2021-05-31 11:55:54 +00:00
. AppendJoin ( ',' , itemValueTypes )
2021-05-23 22:30:41 +00:00
. Append ( ')' ) ;
}
2016-11-20 09:46:07 +00:00
if ( withItemTypes . Count > 0 )
{
2021-05-23 22:30:41 +00:00
stringBuilder . Append ( " AND ItemId In (select guid from typedbaseitems where type in (" )
2021-05-31 11:55:54 +00:00
. AppendJoinInSingleQuotes ( ',' , withItemTypes )
2021-05-23 22:30:41 +00:00
. Append ( "))" ) ;
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 ( excludeItemTypes . Count > 0 )
{
2021-05-23 22:30:41 +00:00
stringBuilder . Append ( " AND ItemId not In (select guid from typedbaseitems where type in (" )
2021-05-31 11:55:54 +00:00
. AppendJoinInSingleQuotes ( ',' , excludeItemTypes )
2021-05-23 22:30:41 +00:00
. Append ( "))" ) ;
2016-11-20 09:46:07 +00:00
}
2021-05-23 22:30:41 +00:00
stringBuilder . Append ( " Group By CleanValue" ) ;
var commandText = stringBuilder . ToString ( ) ;
2016-11-20 09:46:07 +00:00
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 ) )
2021-05-23 22:30:41 +00:00
using ( var statement = PrepareStatement ( connection , commandText ) )
2016-11-20 09:46:07 +00:00
{
2021-05-23 22:30:41 +00:00
foreach ( var row in statement . ExecuteQuery ( ) )
2019-02-20 13:26:49 +00:00
{
2021-05-23 22:30:41 +00:00
if ( row . TryGetString ( 0 , out var result ) )
2016-12-13 15:44:34 +00:00
{
2021-05-23 22:30:41 +00:00
list . Add ( result ) ;
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 ?
2021-05-31 11:55:54 +00:00
( "Type=" + itemValueTypes [ 0 ] ) :
( "Type in (" + string . Join ( ',' , itemValueTypes ) + ")" ) ;
2016-11-20 09:46:07 +00:00
2016-11-20 20:20:01 +00:00
InternalItemsQuery typeSubQuery = null ;
2021-05-23 22:30:41 +00:00
string itemCountColumns = null ;
2016-11-20 20:20:01 +00:00
2021-05-31 11:55:54 +00:00
var stringBuilder = new StringBuilder ( 1024 ) ;
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
{
2021-05-23 22:30:41 +00:00
stringBuilder . Append ( "(select group_concat(type, '|') from TypedBaseItems B" ) ;
2016-11-20 20:20:01 +00:00
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
2021-05-23 22:30:41 +00:00
stringBuilder . Append ( " where " )
. AppendJoin ( " AND " , whereClauses )
2021-05-31 11:55:54 +00:00
. Append ( " AND " )
2021-05-23 22:30:41 +00:00
. Append ( "guid in (select ItemId from ItemValues where ItemValues.CleanValue=A.CleanName AND " )
. Append ( typeClause )
. Append ( ")) as itemTypes" ) ;
2016-11-20 20:20:01 +00:00
2021-05-23 22:30:41 +00:00
itemCountColumns = stringBuilder . ToString ( ) ;
stringBuilder . Clear ( ) ;
2016-11-20 20:20:01 +00:00
}
2021-12-16 00:57:39 +00:00
List < string > columns = _retrieveItemColumns . ToList ( ) ;
2021-05-23 22:30:41 +00:00
// Unfortunately we need to add it to columns to ensure the order of the columns in the select
if ( ! string . IsNullOrEmpty ( itemCountColumns ) )
2019-01-26 17:21:41 +00:00
{
2021-05-23 22:30:41 +00:00
columns . Add ( itemCountColumns ) ;
2019-01-26 17:21:41 +00:00
}
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
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
} ;
2021-05-31 11:55:54 +00:00
SetFinalColumnsToSelect ( query , columns ) ;
2018-09-12 17:26:21 +00:00
2016-11-20 20:20:01 +00:00
var innerWhereClauses = GetWhereClauses ( innerQuery , null ) ;
2021-05-23 22:30:41 +00:00
stringBuilder . Append ( " where Type=@SelectType And CleanName In (Select CleanValue from ItemValues where " )
. Append ( typeClause )
. Append ( " AND ItemId in (select guid from TypedBaseItems" ) ;
if ( innerWhereClauses . Count > 0 )
{
stringBuilder . Append ( " where " )
. AppendJoin ( " AND " , innerWhereClauses ) ;
}
2016-11-20 20:20:01 +00:00
2021-05-23 22:30:41 +00:00
stringBuilder . Append ( "))" ) ;
2016-11-20 20:20:01 +00:00
var outerQuery = new InternalItemsQuery ( query . User )
{
2021-04-09 09:32:19 +00:00
IsPlayed = query . IsPlayed ,
2016-11-20 20:20:01 +00:00
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 ,
2020-11-05 11:27:22 +00:00
StudioIds = query . StudioIds ,
2016-11-20 20:20:01 +00:00
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 )
{
2021-05-23 22:30:41 +00:00
stringBuilder . Append ( " AND " )
. AppendJoin ( " AND " , outerWhereClauses ) ;
2019-01-26 17:21:41 +00:00
}
2016-11-20 20:20:01 +00:00
2021-05-23 22:30:41 +00:00
var whereText = stringBuilder . ToString ( ) ;
stringBuilder . Clear ( ) ;
stringBuilder . Append ( "select " )
. AppendJoin ( ',' , columns )
. Append ( FromText )
. Append ( GetJoinUserDataText ( query ) )
. Append ( whereText )
. Append ( " group by PresentationUniqueKey" ) ;
2016-11-20 20:20:01 +00:00
2021-04-29 00:33:30 +00:00
if ( query . OrderBy . Count ! = 0
| | query . SimilarTo ! = null
| | ! string . IsNullOrEmpty ( query . SearchTerm ) )
2018-09-12 17:26:21 +00:00
{
2021-05-23 22:30:41 +00:00
stringBuilder . Append ( GetOrderByText ( query ) ) ;
2018-09-12 17:26:21 +00:00
}
else
{
2021-05-23 22:30:41 +00:00
stringBuilder . Append ( " order by SortName" ) ;
2018-09-12 17:26:21 +00:00
}
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 )
{
2021-05-23 22:30:41 +00:00
stringBuilder . Append ( " LIMIT " )
2021-05-31 11:55:54 +00:00
. Append ( query . Limit ? ? int . MaxValue ) ;
2016-11-20 20:20:01 +00:00
}
if ( offset > 0 )
{
2021-05-23 22:30:41 +00:00
stringBuilder . Append ( " OFFSET " )
2021-05-31 11:55:54 +00:00
. Append ( offset ) ;
2016-11-20 20:20:01 +00:00
}
}
var isReturningZeroItems = query . Limit . HasValue & & query . Limit < = 0 ;
2021-05-23 22:30:41 +00:00
string commandText = string . Empty ;
2016-11-25 17:36:00 +00:00
if ( ! isReturningZeroItems )
{
2021-05-23 22:30:41 +00:00
commandText = stringBuilder . ToString ( ) ;
2016-11-25 17:36:00 +00:00
}
2019-08-18 20:05:06 +00:00
2021-05-23 22:30:41 +00:00
string countText = string . Empty ;
2016-11-25 17:36:00 +00:00
if ( query . EnableTotalRecordCount )
{
2021-05-23 22:30:41 +00:00
stringBuilder . Clear ( ) ;
2021-05-31 11:55:54 +00:00
var columnsToSelect = new List < string > { "count (distinct PresentationUniqueKey)" } ;
SetFinalColumnsToSelect ( query , columnsToSelect ) ;
2021-05-23 22:30:41 +00:00
stringBuilder . Append ( "select " )
2021-05-31 11:55:54 +00:00
. AppendJoin ( ',' , columnsToSelect )
2021-05-23 22:30:41 +00:00
. Append ( FromText )
. Append ( GetJoinUserDataText ( query ) )
. Append ( whereText ) ;
2016-11-25 17:36:00 +00:00
2021-05-23 22:30:41 +00:00
countText = stringBuilder . ToString ( ) ;
2016-11-25 17:36:00 +00:00
}
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
if ( ! isReturningZeroItems )
2016-11-20 20:20:01 +00:00
{
2021-05-23 22:30:41 +00:00
using ( var statement = PrepareStatement ( db , commandText ) )
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
{
2021-05-23 22:30:41 +00:00
using ( var statement = PrepareStatement ( db , countText ) )
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
}
2021-12-12 02:31:30 +00:00
private static ItemCounts GetItemCounts ( IReadOnlyList < ResultSetValue > reader , int countStartColumn , BaseItemKind [ ] 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 ;
}
2021-05-16 12:49:11 +00:00
if ( ! reader . TryGetString ( countStartColumn , out var typeString ) )
2016-11-20 09:46:07 +00:00
{
return counts ;
}
2021-05-16 12:49:11 +00:00
foreach ( var typeName in typeString . AsSpan ( ) . Split ( '|' ) )
2016-11-20 09:46:07 +00:00
{
2021-05-16 12:49:11 +00:00
if ( typeName . Equals ( typeof ( Series ) . FullName , StringComparison . OrdinalIgnoreCase ) )
2016-11-20 09:46:07 +00:00
{
2021-05-16 12:49:11 +00:00
counts . SeriesCount + + ;
2016-11-20 09:46:07 +00:00
}
2021-05-16 12:49:11 +00:00
else if ( typeName . Equals ( typeof ( Episode ) . FullName , StringComparison . OrdinalIgnoreCase ) )
2016-11-20 09:46:07 +00:00
{
2021-05-16 12:49:11 +00:00
counts . EpisodeCount + + ;
2016-11-20 09:46:07 +00:00
}
2021-05-16 12:49:11 +00:00
else if ( typeName . Equals ( typeof ( Movie ) . FullName , StringComparison . OrdinalIgnoreCase ) )
2016-11-20 09:46:07 +00:00
{
2021-05-16 12:49:11 +00:00
counts . MovieCount + + ;
2016-11-20 09:46:07 +00:00
}
2021-05-16 12:49:11 +00:00
else if ( typeName . Equals ( typeof ( MusicAlbum ) . FullName , StringComparison . OrdinalIgnoreCase ) )
2016-11-20 09:46:07 +00:00
{
2021-05-16 12:49:11 +00:00
counts . AlbumCount + + ;
2016-11-20 09:46:07 +00:00
}
2021-05-16 12:49:11 +00:00
else if ( typeName . Equals ( typeof ( MusicArtist ) . FullName , StringComparison . OrdinalIgnoreCase ) )
2016-11-20 09:46:07 +00:00
{
2021-05-16 12:49:11 +00:00
counts . ArtistCount + + ;
2016-11-20 09:46:07 +00:00
}
2021-05-16 12:49:11 +00:00
else if ( typeName . Equals ( typeof ( Audio ) . FullName , StringComparison . OrdinalIgnoreCase ) )
2016-11-20 09:46:07 +00:00
{
2021-05-16 12:49:11 +00:00
counts . SongCount + + ;
2016-11-20 09:46:07 +00:00
}
2021-05-16 12:49:11 +00:00
else if ( typeName . Equals ( typeof ( Trailer ) . FullName , StringComparison . OrdinalIgnoreCase ) )
2016-11-20 09:46:07 +00:00
{
2021-05-16 12:49:11 +00:00
counts . TrailerCount + + ;
2016-11-20 09:46:07 +00:00
}
2019-02-28 22:22:57 +00:00
2021-05-16 12:49:11 +00:00
counts . ItemCount + + ;
2016-11-20 09:46:07 +00:00
}
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 ;
2020-08-03 18:32:45 +00:00
const string StartInsertText = "insert into ItemValues (ItemId, Type, Value, CleanValue) values " ;
var insertText = new StringBuilder ( StartInsertText ) ;
2018-09-12 17:26:21 +00:00
while ( startIndex < values . Count )
2016-11-20 09:46:07 +00:00
{
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 ;
2020-08-03 18:32:45 +00:00
insertText . Length = StartInsertText . Length ;
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
{
2020-10-12 18:05:11 +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 ;
2020-08-03 18:32:45 +00:00
const string StartInsertText = "insert into People (ItemId, Name, Role, PersonType, SortOrder, ListOrder) values " ;
var insertText = new StringBuilder ( StartInsertText ) ;
2018-09-12 17:26:21 +00:00
while ( startIndex < people . Count )
{
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 + + )
{
2021-04-22 13:28:24 +00:00
insertText . AppendFormat (
CultureInfo . InvariantCulture ,
"(@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 ;
2020-08-03 18:32:45 +00:00
insertText . Length = StartInsertText . Length ;
2016-11-20 09:46:07 +00:00
}
}
2021-05-21 17:35:00 +00:00
private PersonInfo GetPerson ( IReadOnlyList < ResultSetValue > reader )
2016-11-20 09:46:07 +00:00
{
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
2021-05-16 12:49:11 +00:00
if ( reader . TryGetString ( 2 , out var role ) )
2016-11-20 09:46:07 +00:00
{
2021-05-16 12:49:11 +00:00
item . Role = role ;
2016-11-20 09:46:07 +00:00
}
2021-05-16 12:49:11 +00:00
if ( reader . TryGetString ( 3 , out var type ) )
2016-11-20 09:46:07 +00:00
{
2021-05-16 12:49:11 +00:00
item . Type = type ;
2016-11-20 09:46:07 +00:00
}
2021-05-16 17:05:56 +00:00
if ( reader . TryGetInt32 ( 4 , out var sortOrder ) )
2016-11-20 09:46:07 +00:00
{
2021-05-16 12:49:11 +00:00
item . SortOrder = sortOrder ;
2016-11-20 09:46:07 +00:00
}
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
}
2020-08-02 21:30:34 +00:00
var cmdText = _mediaStreamSaveColumnsSelectQuery ;
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
{
2020-10-12 18:05:11 +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 ;
2020-08-03 18:32:45 +00:00
var insertText = new StringBuilder ( _mediaStreamSaveColumnsInsertQuery ) ;
2018-09-12 17:26:21 +00:00
while ( startIndex < streams . Count )
{
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 ;
2020-08-03 18:32:45 +00:00
insertText . Length = _mediaStreamSaveColumnsInsertQuery . Length ;
2016-11-20 09:46:07 +00:00
}
}
/// <summary>
/// Gets the chapter.
/// </summary>
/// <param name="reader">The reader.</param>
/// <returns>ChapterInfo.</returns>
2021-05-21 17:35:00 +00:00
private MediaStream GetMediaStream ( IReadOnlyList < ResultSetValue > reader )
2016-11-20 09:46:07 +00:00
{
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
2021-05-16 12:49:11 +00:00
if ( reader . TryGetString ( 3 , out var codec ) )
2016-11-20 09:46:07 +00:00
{
2021-05-16 12:49:11 +00:00
item . Codec = codec ;
2016-11-20 09:46:07 +00:00
}
2021-05-16 12:49:11 +00:00
if ( reader . TryGetString ( 4 , out var language ) )
2016-11-20 09:46:07 +00:00
{
2021-05-16 12:49:11 +00:00
item . Language = language ;
2016-11-20 09:46:07 +00:00
}
2021-05-16 12:49:11 +00:00
if ( reader . TryGetString ( 5 , out var channelLayout ) )
2016-11-20 09:46:07 +00:00
{
2021-05-16 12:49:11 +00:00
item . ChannelLayout = channelLayout ;
2016-11-20 09:46:07 +00:00
}
2021-05-16 12:49:11 +00:00
if ( reader . TryGetString ( 6 , out var profile ) )
2016-11-20 09:46:07 +00:00
{
2021-05-16 12:49:11 +00:00
item . Profile = profile ;
2016-11-20 09:46:07 +00:00
}
2021-05-16 12:49:11 +00:00
if ( reader . TryGetString ( 7 , out var aspectRatio ) )
2016-11-20 09:46:07 +00:00
{
2021-05-16 12:49:11 +00:00
item . AspectRatio = aspectRatio ;
2016-11-20 09:46:07 +00:00
}
2021-05-16 12:49:11 +00:00
if ( reader . TryGetString ( 8 , out var path ) )
2016-11-20 09:46:07 +00:00
{
2021-05-16 12:49:11 +00:00
item . Path = RestorePath ( path ) ;
2016-11-20 09:46:07 +00:00
}
item . IsInterlaced = reader . GetBoolean ( 9 ) ;
2021-05-16 17:05:56 +00:00
if ( reader . TryGetInt32 ( 10 , out var bitrate ) )
2016-11-20 09:46:07 +00:00
{
2021-05-16 12:49:11 +00:00
item . BitRate = bitrate ;
2016-11-20 09:46:07 +00:00
}
2021-05-16 17:05:56 +00:00
if ( reader . TryGetInt32 ( 11 , out var channels ) )
2016-11-20 09:46:07 +00:00
{
2021-05-16 12:49:11 +00:00
item . Channels = channels ;
2016-11-20 09:46:07 +00:00
}
2021-05-16 17:05:56 +00:00
if ( reader . TryGetInt32 ( 12 , out var sampleRate ) )
2016-11-20 09:46:07 +00:00
{
2021-05-16 12:49:11 +00:00
item . SampleRate = sampleRate ;
2016-11-20 09:46:07 +00:00
}
item . IsDefault = reader . GetBoolean ( 13 ) ;
item . IsForced = reader . GetBoolean ( 14 ) ;
item . IsExternal = reader . GetBoolean ( 15 ) ;
2021-05-16 17:05:56 +00:00
if ( reader . TryGetInt32 ( 16 , out var width ) )
2016-11-20 09:46:07 +00:00
{
2021-05-16 12:49:11 +00:00
item . Width = width ;
2016-11-20 09:46:07 +00:00
}
2021-05-16 17:05:56 +00:00
if ( reader . TryGetInt32 ( 17 , out var height ) )
2016-11-20 09:46:07 +00:00
{
2021-05-16 12:49:11 +00:00
item . Height = height ;
2016-11-20 09:46:07 +00:00
}
2021-05-16 17:05:56 +00:00
if ( reader . TryGetSingle ( 18 , out var averageFrameRate ) )
2016-11-20 09:46:07 +00:00
{
2021-05-16 12:49:11 +00:00
item . AverageFrameRate = averageFrameRate ;
2016-11-20 09:46:07 +00:00
}
2021-05-16 17:05:56 +00:00
if ( reader . TryGetSingle ( 19 , out var realFrameRate ) )
2016-11-20 09:46:07 +00:00
{
2021-05-16 12:49:11 +00:00
item . RealFrameRate = realFrameRate ;
2016-11-20 09:46:07 +00:00
}
2021-05-16 17:05:56 +00:00
if ( reader . TryGetSingle ( 20 , out var level ) )
2016-11-20 09:46:07 +00:00
{
2021-05-16 12:49:11 +00:00
item . Level = level ;
2016-11-20 09:46:07 +00:00
}
2021-05-16 12:49:11 +00:00
if ( reader . TryGetString ( 21 , out var pixelFormat ) )
2016-11-20 09:46:07 +00:00
{
2021-05-16 12:49:11 +00:00
item . PixelFormat = pixelFormat ;
2016-11-20 09:46:07 +00:00
}
2021-05-16 17:05:56 +00:00
if ( reader . TryGetInt32 ( 22 , out var bitDepth ) )
2016-11-20 09:46:07 +00:00
{
2021-05-16 12:49:11 +00:00
item . BitDepth = bitDepth ;
2016-11-20 09:46:07 +00:00
}
2021-05-16 12:49:11 +00:00
if ( reader . TryGetBoolean ( 23 , out var isAnamorphic ) )
2016-11-20 09:46:07 +00:00
{
2021-05-16 12:49:11 +00:00
item . IsAnamorphic = isAnamorphic ;
2016-11-20 09:46:07 +00:00
}
2021-05-16 17:05:56 +00:00
if ( reader . TryGetInt32 ( 24 , out var refFrames ) )
2016-11-20 09:46:07 +00:00
{
2021-05-16 12:49:11 +00:00
item . RefFrames = refFrames ;
2016-11-20 09:46:07 +00:00
}
2021-05-16 12:49:11 +00:00
if ( reader . TryGetString ( 25 , out var codecTag ) )
2016-11-20 09:46:07 +00:00
{
2021-05-16 12:49:11 +00:00
item . CodecTag = codecTag ;
2016-11-20 09:46:07 +00:00
}
2021-05-16 12:49:11 +00:00
if ( reader . TryGetString ( 26 , out var comment ) )
2016-11-20 09:46:07 +00:00
{
2021-05-16 12:49:11 +00:00
item . Comment = comment ;
2016-11-20 09:46:07 +00:00
}
2021-05-16 12:49:11 +00:00
if ( reader . TryGetString ( 27 , out var nalLengthSize ) )
2016-11-20 09:46:07 +00:00
{
2021-05-16 12:49:11 +00:00
item . NalLengthSize = nalLengthSize ;
2016-11-20 09:46:07 +00:00
}
2021-05-16 12:49:11 +00:00
if ( reader . TryGetBoolean ( 28 , out var isAVC ) )
2016-11-20 09:46:07 +00:00
{
2021-05-16 12:49:11 +00:00
item . IsAVC = isAVC ;
2016-11-20 09:46:07 +00:00
}
2021-05-16 12:49:11 +00:00
if ( reader . TryGetString ( 29 , out var title ) )
2016-11-20 09:46:07 +00:00
{
2021-05-16 12:49:11 +00:00
item . Title = title ;
2016-11-20 09:46:07 +00:00
}
2021-05-16 12:49:11 +00:00
if ( reader . TryGetString ( 30 , out var timeBase ) )
2016-11-20 09:46:07 +00:00
{
2021-05-16 12:49:11 +00:00
item . TimeBase = timeBase ;
2016-11-20 09:46:07 +00:00
}
2021-05-16 12:49:11 +00:00
if ( reader . TryGetString ( 31 , out var codecTimeBase ) )
2016-11-20 09:46:07 +00:00
{
2021-05-16 12:49:11 +00:00
item . CodecTimeBase = codecTimeBase ;
2016-11-20 09:46:07 +00:00
}
2021-05-16 12:49:11 +00:00
if ( reader . TryGetString ( 32 , out var colorPrimaries ) )
2018-09-12 17:26:21 +00:00
{
2021-05-16 12:49:11 +00:00
item . ColorPrimaries = colorPrimaries ;
2018-09-12 17:26:21 +00:00
}
2021-05-16 12:49:11 +00:00
if ( reader . TryGetString ( 33 , out var colorSpace ) )
2018-09-12 17:26:21 +00:00
{
2021-05-16 12:49:11 +00:00
item . ColorSpace = colorSpace ;
2018-09-12 17:26:21 +00:00
}
2021-05-16 12:49:11 +00:00
if ( reader . TryGetString ( 34 , out var colorTransfer ) )
2018-09-12 17:26:21 +00:00
{
2021-05-16 12:49:11 +00:00
item . ColorTransfer = colorTransfer ;
2018-09-12 17:26:21 +00:00
}
2020-03-24 15:12:06 +00:00
if ( item . Type = = MediaStreamType . Subtitle )
{
2021-02-20 22:13:04 +00:00
item . LocalizedUndefined = _localization . GetLocalizedString ( "Undefined" ) ;
item . LocalizedDefault = _localization . GetLocalizedString ( "Default" ) ;
item . LocalizedForced = _localization . GetLocalizedString ( "Forced" ) ;
2019-03-14 21:31:51 +00:00
}
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 ) ) ;
}
2020-08-02 21:30:34 +00:00
var cmdText = _mediaAttachmentSaveColumnsSelectQuery ;
2019-10-04 17:23:08 +00:00
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 )
{
2021-04-22 13:28:24 +00:00
throw new ArgumentException ( "Guid can't be empty." , 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
{
2020-10-12 18:05:11 +00:00
connection . RunInTransaction (
db = >
2019-11-06 15:43:14 +00:00
{
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
2020-08-03 18:32:45 +00:00
var insertText = new StringBuilder ( _mediaAttachmentInsertPrefix ) ;
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-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 ( ) ;
}
2020-08-03 18:32:45 +00:00
insertText . Length = _mediaAttachmentInsertPrefix . Length ;
2019-10-04 17:23:08 +00:00
}
}
/// <summary>
/// Gets the attachment.
/// </summary>
/// <param name="reader">The reader.</param>
2020-06-19 10:21:49 +00:00
/// <returns>MediaAttachment.</returns>
2021-05-21 17:35:00 +00:00
private MediaAttachment GetMediaAttachment ( IReadOnlyList < ResultSetValue > reader )
2019-10-04 17:23:08 +00:00
{
var item = new MediaAttachment
{
Index = reader [ 1 ] . ToInt ( )
} ;
2021-05-16 12:49:11 +00:00
if ( reader . TryGetString ( 2 , out var codec ) )
2019-10-04 17:23:08 +00:00
{
2021-05-16 12:49:11 +00:00
item . Codec = codec ;
2019-10-04 17:23:08 +00:00
}
2021-05-16 12:49:11 +00:00
if ( reader . TryGetString ( 3 , out var codecTag ) )
2019-10-04 17:23:08 +00:00
{
2021-05-16 12:49:11 +00:00
item . CodecTag = codecTag ;
2019-10-04 17:23:08 +00:00
}
2021-05-16 12:49:11 +00:00
if ( reader . TryGetString ( 4 , out var comment ) )
2019-10-04 17:23:08 +00:00
{
2021-05-16 12:49:11 +00:00
item . Comment = comment ;
2019-10-04 17:23:08 +00:00
}
2021-05-16 12:49:11 +00:00
if ( reader . TryGetString ( 5 , out var fileName ) )
2019-10-04 17:23:08 +00:00
{
2021-05-16 12:49:11 +00:00
item . FileName = fileName ;
2019-10-04 17:23:08 +00:00
}
2021-05-16 12:49:11 +00:00
if ( reader . TryGetString ( 6 , out var mimeType ) )
2019-10-04 17:23:08 +00:00
{
2021-05-16 12:49:11 +00:00
item . MimeType = mimeType ;
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
}