You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
5423 lines
201 KiB
5423 lines
201 KiB
12 years ago
|
using System;
|
||
|
using System.Collections.Generic;
|
||
|
using System.Data;
|
||
10 years ago
|
using System.Globalization;
|
||
12 years ago
|
using System.IO;
|
||
|
using System.Linq;
|
||
10 years ago
|
using System.Runtime.Serialization;
|
||
9 years ago
|
using System.Text;
|
||
12 years ago
|
using System.Threading;
|
||
|
using System.Threading.Tasks;
|
||
9 years ago
|
using MediaBrowser.Controller.Channels;
|
||
8 years ago
|
using MediaBrowser.Controller.Collections;
|
||
9 years ago
|
using MediaBrowser.Controller.Configuration;
|
||
8 years ago
|
using MediaBrowser.Controller.Entities;
|
||
|
using MediaBrowser.Controller.Entities.Audio;
|
||
|
using MediaBrowser.Controller.Entities.Movies;
|
||
|
using MediaBrowser.Controller.Entities.TV;
|
||
8 years ago
|
using MediaBrowser.Controller.Extensions;
|
||
8 years ago
|
using MediaBrowser.Controller.LiveTv;
|
||
|
using MediaBrowser.Controller.Persistence;
|
||
9 years ago
|
using MediaBrowser.Controller.Playlists;
|
||
9 years ago
|
using MediaBrowser.Model.Dto;
|
||
8 years ago
|
using MediaBrowser.Model.Entities;
|
||
8 years ago
|
using MediaBrowser.Model.IO;
|
||
9 years ago
|
using MediaBrowser.Model.LiveTv;
|
||
8 years ago
|
using MediaBrowser.Model.Logging;
|
||
|
using MediaBrowser.Model.Querying;
|
||
|
using MediaBrowser.Model.Serialization;
|
||
8 years ago
|
using MediaBrowser.Server.Implementations.Devices;
|
||
8 years ago
|
using MediaBrowser.Server.Implementations.Playlists;
|
||
12 years ago
|
|
||
8 years ago
|
namespace Emby.Server.Core.Data
|
||
12 years ago
|
{
|
||
|
/// <summary>
|
||
|
/// Class SQLiteItemRepository
|
||
|
/// </summary>
|
||
9 years ago
|
public class SqliteItemRepository : BaseSqliteRepository, IItemRepository
|
||
12 years ago
|
{
|
||
9 years ago
|
private IDbConnection _connection;
|
||
|
|
||
11 years ago
|
private readonly TypeMapper _typeMapper = new TypeMapper();
|
||
11 years ago
|
|
||
12 years ago
|
/// <summary>
|
||
|
/// Gets the name of the repository
|
||
|
/// </summary>
|
||
|
/// <value>The name.</value>
|
||
|
public string Name
|
||
|
{
|
||
|
get
|
||
|
{
|
||
12 years ago
|
return "SQLite";
|
||
12 years ago
|
}
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// Gets the json serializer.
|
||
|
/// </summary>
|
||
|
/// <value>The json serializer.</value>
|
||
|
private readonly IJsonSerializer _jsonSerializer;
|
||
|
|
||
|
/// <summary>
|
||
|
/// The _app paths
|
||
|
/// </summary>
|
||
9 years ago
|
private readonly IServerConfigurationManager _config;
|
||
12 years ago
|
|
||
9 years ago
|
/// <summary>
|
||
|
/// The _save item command
|
||
|
/// </summary>
|
||
|
private IDbCommand _saveItemCommand;
|
||
|
|
||
12 years ago
|
private readonly string _criticReviewsPath;
|
||
|
|
||
9 years ago
|
private IDbCommand _deleteItemCommand;
|
||
9 years ago
|
|
||
9 years ago
|
private IDbCommand _deletePeopleCommand;
|
||
|
private IDbCommand _savePersonCommand;
|
||
|
|
||
|
private IDbCommand _deleteChaptersCommand;
|
||
|
private IDbCommand _saveChapterCommand;
|
||
|
|
||
|
private IDbCommand _deleteStreamsCommand;
|
||
|
private IDbCommand _saveStreamCommand;
|
||
|
|
||
|
private IDbCommand _deleteAncestorsCommand;
|
||
|
private IDbCommand _saveAncestorCommand;
|
||
|
|
||
|
private IDbCommand _deleteItemValuesCommand;
|
||
|
private IDbCommand _saveItemValuesCommand;
|
||
|
|
||
|
private IDbCommand _deleteProviderIdsCommand;
|
||
|
private IDbCommand _saveProviderIdsCommand;
|
||
|
|
||
|
private IDbCommand _deleteImagesCommand;
|
||
|
private IDbCommand _saveImagesCommand;
|
||
|
|
||
|
private IDbCommand _updateInheritedTagsCommand;
|
||
|
|
||
9 years ago
|
public const int LatestSchemaVersion = 109;
|
||
8 years ago
|
private readonly IMemoryStreamFactory _memoryStreamProvider;
|
||
10 years ago
|
|
||
12 years ago
|
/// <summary>
|
||
|
/// Initializes a new instance of the <see cref="SqliteItemRepository"/> class.
|
||
|
/// </summary>
|
||
8 years ago
|
public SqliteItemRepository(IServerConfigurationManager config, IJsonSerializer jsonSerializer, ILogManager logManager, IDbConnector connector, IMemoryStreamFactory memoryStreamProvider)
|
||
9 years ago
|
: base(logManager, connector)
|
||
12 years ago
|
{
|
||
9 years ago
|
if (config == null)
|
||
12 years ago
|
{
|
||
9 years ago
|
throw new ArgumentNullException("config");
|
||
12 years ago
|
}
|
||
|
if (jsonSerializer == null)
|
||
|
{
|
||
|
throw new ArgumentNullException("jsonSerializer");
|
||
|
}
|
||
|
|
||
9 years ago
|
_config = config;
|
||
12 years ago
|
_jsonSerializer = jsonSerializer;
|
||
8 years ago
|
_memoryStreamProvider = memoryStreamProvider;
|
||
12 years ago
|
|
||
9 years ago
|
_criticReviewsPath = Path.Combine(_config.ApplicationPaths.DataPath, "critic-reviews");
|
||
9 years ago
|
DbFilePath = Path.Combine(_config.ApplicationPaths.DataPath, "library.db");
|
||
|
}
|
||
|
|
||
9 years ago
|
private const string ChaptersTableName = "Chapters2";
|
||
|
|
||
9 years ago
|
protected override async Task<IDbConnection> CreateConnection(bool isReadOnly = false)
|
||
|
{
|
||
9 years ago
|
var cacheSize = _config.Configuration.SqliteCacheSize;
|
||
9 years ago
|
if (cacheSize <= 0)
|
||
|
{
|
||
8 years ago
|
cacheSize = Math.Min(Environment.ProcessorCount * 50000, 100000);
|
||
9 years ago
|
}
|
||
|
|
||
|
var connection = await DbConnector.Connect(DbFilePath, false, false, 0 - cacheSize).ConfigureAwait(false);
|
||
9 years ago
|
|
||
9 years ago
|
connection.RunQueries(new[]
|
||
|
{
|
||
9 years ago
|
"pragma temp_store = memory",
|
||
9 years ago
|
"pragma default_temp_store = memory",
|
||
|
"PRAGMA locking_mode=EXCLUSIVE"
|
||
9 years ago
|
|
||
9 years ago
|
}, Logger);
|
||
9 years ago
|
|
||
|
return connection;
|
||
12 years ago
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// Opens the connection to the database
|
||
|
/// </summary>
|
||
|
/// <returns>Task.</returns>
|
||
9 years ago
|
public async Task Initialize(SqliteUserDataRepository userDataRepo)
|
||
12 years ago
|
{
|
||
9 years ago
|
_connection = await CreateConnection(false).ConfigureAwait(false);
|
||
9 years ago
|
|
||
9 years ago
|
var createMediaStreamsTableCommand
|
||
|
= "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, PRIMARY KEY (ItemId, StreamIndex))";
|
||
9 years ago
|
|
||
9 years ago
|
string[] queries = {
|
||
12 years ago
|
|
||
9 years ago
|
"create table if not exists TypedBaseItems (guid GUID primary key, type TEXT, data BLOB, ParentId GUID, Path TEXT)",
|
||
12 years ago
|
|
||
9 years ago
|
"create table if not exists AncestorIds (ItemId GUID, AncestorId GUID, AncestorIdText TEXT, PRIMARY KEY (ItemId, AncestorId))",
|
||
|
"create index if not exists idx_AncestorIds1 on AncestorIds(AncestorId)",
|
||
|
"create index if not exists idx_AncestorIds2 on AncestorIds(AncestorIdText)",
|
||
9 years ago
|
|
||
9 years ago
|
"create table if not exists ItemValues (ItemId GUID, Type INT, Value TEXT, CleanValue TEXT)",
|
||
9 years ago
|
|
||
9 years ago
|
"create table if not exists ProviderIds (ItemId GUID, Name TEXT, Value TEXT, PRIMARY KEY (ItemId, Name))",
|
||
9 years ago
|
// covering index
|
||
|
"create index if not exists Idx_ProviderIds1 on ProviderIds(ItemId,Name,Value)",
|
||
9 years ago
|
|
||
9 years ago
|
"create table if not exists Images (ItemId GUID NOT NULL, Path TEXT NOT NULL, ImageType INT NOT NULL, DateModified DATETIME, IsPlaceHolder BIT NOT NULL, SortOrder INT)",
|
||
|
"create index if not exists idx_Images on Images(ItemId)",
|
||
|
|
||
10 years ago
|
"create table if not exists People (ItemId GUID, Name TEXT NOT NULL, Role TEXT, PersonType TEXT, SortOrder int, ListOrder int)",
|
||
9 years ago
|
|
||
|
"drop index if exists idxPeopleItemId",
|
||
9 years ago
|
"create index if not exists idxPeopleItemId1 on People(ItemId,ListOrder)",
|
||
9 years ago
|
"create index if not exists idxPeopleName on People(Name)",
|
||
10 years ago
|
|
||
9 years ago
|
"create table if not exists "+ChaptersTableName+" (ItemId GUID, ChapterIndex INT, StartPositionTicks BIGINT, Name TEXT, ImagePath TEXT, PRIMARY KEY (ItemId, ChapterIndex))",
|
||
|
|
||
9 years ago
|
createMediaStreamsTableCommand,
|
||
9 years ago
|
|
||
9 years ago
|
"create index if not exists idx_mediastreams1 on mediastreams(ItemId)",
|
||
9 years ago
|
|
||
8 years ago
|
//"drop table if exists UserDataKeys"
|
||
|
|
||
12 years ago
|
};
|
||
|
|
||
9 years ago
|
_connection.RunQueries(queries, Logger);
|
||
|
|
||
|
_connection.AddColumn(Logger, "AncestorIds", "AncestorIdText", "Text");
|
||
|
|
||
|
_connection.AddColumn(Logger, "TypedBaseItems", "Path", "Text");
|
||
|
_connection.AddColumn(Logger, "TypedBaseItems", "StartDate", "DATETIME");
|
||
|
_connection.AddColumn(Logger, "TypedBaseItems", "EndDate", "DATETIME");
|
||
|
_connection.AddColumn(Logger, "TypedBaseItems", "ChannelId", "Text");
|
||
|
_connection.AddColumn(Logger, "TypedBaseItems", "IsMovie", "BIT");
|
||
|
_connection.AddColumn(Logger, "TypedBaseItems", "IsSports", "BIT");
|
||
|
_connection.AddColumn(Logger, "TypedBaseItems", "IsKids", "BIT");
|
||
|
_connection.AddColumn(Logger, "TypedBaseItems", "CommunityRating", "Float");
|
||
|
_connection.AddColumn(Logger, "TypedBaseItems", "CustomRating", "Text");
|
||
|
_connection.AddColumn(Logger, "TypedBaseItems", "IndexNumber", "INT");
|
||
|
_connection.AddColumn(Logger, "TypedBaseItems", "IsLocked", "BIT");
|
||
|
_connection.AddColumn(Logger, "TypedBaseItems", "Name", "Text");
|
||
|
_connection.AddColumn(Logger, "TypedBaseItems", "OfficialRating", "Text");
|
||
|
|
||
|
_connection.AddColumn(Logger, "TypedBaseItems", "MediaType", "Text");
|
||
|
_connection.AddColumn(Logger, "TypedBaseItems", "Overview", "Text");
|
||
|
_connection.AddColumn(Logger, "TypedBaseItems", "ParentIndexNumber", "INT");
|
||
|
_connection.AddColumn(Logger, "TypedBaseItems", "PremiereDate", "DATETIME");
|
||
|
_connection.AddColumn(Logger, "TypedBaseItems", "ProductionYear", "INT");
|
||
|
_connection.AddColumn(Logger, "TypedBaseItems", "ParentId", "GUID");
|
||
|
_connection.AddColumn(Logger, "TypedBaseItems", "Genres", "Text");
|
||
|
_connection.AddColumn(Logger, "TypedBaseItems", "SchemaVersion", "INT");
|
||
|
_connection.AddColumn(Logger, "TypedBaseItems", "SortName", "Text");
|
||
|
_connection.AddColumn(Logger, "TypedBaseItems", "RunTimeTicks", "BIGINT");
|
||
|
|
||
|
_connection.AddColumn(Logger, "TypedBaseItems", "OfficialRatingDescription", "Text");
|
||
|
_connection.AddColumn(Logger, "TypedBaseItems", "HomePageUrl", "Text");
|
||
|
_connection.AddColumn(Logger, "TypedBaseItems", "VoteCount", "INT");
|
||
|
_connection.AddColumn(Logger, "TypedBaseItems", "DisplayMediaType", "Text");
|
||
|
_connection.AddColumn(Logger, "TypedBaseItems", "DateCreated", "DATETIME");
|
||
|
_connection.AddColumn(Logger, "TypedBaseItems", "DateModified", "DATETIME");
|
||
|
|
||
|
_connection.AddColumn(Logger, "TypedBaseItems", "ForcedSortName", "Text");
|
||
|
_connection.AddColumn(Logger, "TypedBaseItems", "IsOffline", "BIT");
|
||
|
_connection.AddColumn(Logger, "TypedBaseItems", "LocationType", "Text");
|
||
|
|
||
|
_connection.AddColumn(Logger, "TypedBaseItems", "IsSeries", "BIT");
|
||
|
_connection.AddColumn(Logger, "TypedBaseItems", "IsLive", "BIT");
|
||
|
_connection.AddColumn(Logger, "TypedBaseItems", "IsNews", "BIT");
|
||
|
_connection.AddColumn(Logger, "TypedBaseItems", "IsPremiere", "BIT");
|
||
|
|
||
|
_connection.AddColumn(Logger, "TypedBaseItems", "EpisodeTitle", "Text");
|
||
|
_connection.AddColumn(Logger, "TypedBaseItems", "IsRepeat", "BIT");
|
||
|
|
||
|
_connection.AddColumn(Logger, "TypedBaseItems", "PreferredMetadataLanguage", "Text");
|
||
|
_connection.AddColumn(Logger, "TypedBaseItems", "PreferredMetadataCountryCode", "Text");
|
||
|
_connection.AddColumn(Logger, "TypedBaseItems", "IsHD", "BIT");
|
||
|
_connection.AddColumn(Logger, "TypedBaseItems", "ExternalEtag", "Text");
|
||
|
_connection.AddColumn(Logger, "TypedBaseItems", "DateLastRefreshed", "DATETIME");
|
||
|
|
||
|
_connection.AddColumn(Logger, "TypedBaseItems", "DateLastSaved", "DATETIME");
|
||
|
_connection.AddColumn(Logger, "TypedBaseItems", "IsInMixedFolder", "BIT");
|
||
|
_connection.AddColumn(Logger, "TypedBaseItems", "LockedFields", "Text");
|
||
|
_connection.AddColumn(Logger, "TypedBaseItems", "Studios", "Text");
|
||
|
_connection.AddColumn(Logger, "TypedBaseItems", "Audio", "Text");
|
||
|
_connection.AddColumn(Logger, "TypedBaseItems", "ExternalServiceId", "Text");
|
||
|
_connection.AddColumn(Logger, "TypedBaseItems", "Tags", "Text");
|
||
|
_connection.AddColumn(Logger, "TypedBaseItems", "IsFolder", "BIT");
|
||
|
_connection.AddColumn(Logger, "TypedBaseItems", "InheritedParentalRatingValue", "INT");
|
||
|
_connection.AddColumn(Logger, "TypedBaseItems", "UnratedType", "Text");
|
||
|
_connection.AddColumn(Logger, "TypedBaseItems", "TopParentId", "Text");
|
||
|
_connection.AddColumn(Logger, "TypedBaseItems", "IsItemByName", "BIT");
|
||
|
_connection.AddColumn(Logger, "TypedBaseItems", "SourceType", "Text");
|
||
|
_connection.AddColumn(Logger, "TypedBaseItems", "TrailerTypes", "Text");
|
||
|
_connection.AddColumn(Logger, "TypedBaseItems", "CriticRating", "Float");
|
||
|
_connection.AddColumn(Logger, "TypedBaseItems", "CriticRatingSummary", "Text");
|
||
|
_connection.AddColumn(Logger, "TypedBaseItems", "InheritedTags", "Text");
|
||
|
_connection.AddColumn(Logger, "TypedBaseItems", "CleanName", "Text");
|
||
|
_connection.AddColumn(Logger, "TypedBaseItems", "PresentationUniqueKey", "Text");
|
||
|
_connection.AddColumn(Logger, "TypedBaseItems", "SlugName", "Text");
|
||
|
_connection.AddColumn(Logger, "TypedBaseItems", "OriginalTitle", "Text");
|
||
|
_connection.AddColumn(Logger, "TypedBaseItems", "PrimaryVersionId", "Text");
|
||
|
_connection.AddColumn(Logger, "TypedBaseItems", "DateLastMediaAdded", "DATETIME");
|
||
|
_connection.AddColumn(Logger, "TypedBaseItems", "Album", "Text");
|
||
|
_connection.AddColumn(Logger, "TypedBaseItems", "IsVirtualItem", "BIT");
|
||
|
_connection.AddColumn(Logger, "TypedBaseItems", "SeriesName", "Text");
|
||
9 years ago
|
_connection.AddColumn(Logger, "TypedBaseItems", "UserDataKey", "Text");
|
||
9 years ago
|
_connection.AddColumn(Logger, "TypedBaseItems", "SeasonName", "Text");
|
||
9 years ago
|
_connection.AddColumn(Logger, "TypedBaseItems", "SeasonId", "GUID");
|
||
9 years ago
|
_connection.AddColumn(Logger, "TypedBaseItems", "SeriesId", "GUID");
|
||
9 years ago
|
_connection.AddColumn(Logger, "TypedBaseItems", "SeriesSortName", "Text");
|
||
8 years ago
|
_connection.AddColumn(Logger, "TypedBaseItems", "ExternalSeriesId", "Text");
|
||
8 years ago
|
_connection.AddColumn(Logger, "TypedBaseItems", "ShortOverview", "Text");
|
||
|
_connection.AddColumn(Logger, "TypedBaseItems", "Tagline", "Text");
|
||
|
_connection.AddColumn(Logger, "TypedBaseItems", "Keywords", "Text");
|
||
|
_connection.AddColumn(Logger, "TypedBaseItems", "ProviderIds", "Text");
|
||
|
_connection.AddColumn(Logger, "TypedBaseItems", "Images", "Text");
|
||
8 years ago
|
_connection.AddColumn(Logger, "TypedBaseItems", "ProductionLocations", "Text");
|
||
|
_connection.AddColumn(Logger, "TypedBaseItems", "ThemeSongIds", "Text");
|
||
|
_connection.AddColumn(Logger, "TypedBaseItems", "ThemeVideoIds", "Text");
|
||
8 years ago
|
_connection.AddColumn(Logger, "TypedBaseItems", "TotalBitrate", "INT");
|
||
|
_connection.AddColumn(Logger, "TypedBaseItems", "ExtraType", "Text");
|
||
|
_connection.AddColumn(Logger, "TypedBaseItems", "Artists", "Text");
|
||
|
_connection.AddColumn(Logger, "TypedBaseItems", "AlbumArtists", "Text");
|
||
9 years ago
|
|
||
9 years ago
|
_connection.AddColumn(Logger, "ItemValues", "CleanValue", "Text");
|
||
9 years ago
|
|
||
9 years ago
|
_connection.AddColumn(Logger, ChaptersTableName, "ImageDateModified", "DATETIME");
|
||
|
|
||
9 years ago
|
string[] postQueries =
|
||
9 years ago
|
|
||
9 years ago
|
{
|
||
9 years ago
|
// obsolete
|
||
|
"drop index if exists idx_TypedBaseItems",
|
||
|
"drop index if exists idx_mediastreams",
|
||
|
"drop index if exists idx_"+ChaptersTableName,
|
||
|
"drop index if exists idx_UserDataKeys1",
|
||
|
"drop index if exists idx_UserDataKeys2",
|
||
|
"drop index if exists idx_TypeTopParentId3",
|
||
|
"drop index if exists idx_TypeTopParentId2",
|
||
|
"drop index if exists idx_TypeTopParentId4",
|
||
|
"drop index if exists idx_Type",
|
||
|
"drop index if exists idx_TypeTopParentId",
|
||
|
"drop index if exists idx_GuidType",
|
||
|
"drop index if exists idx_TopParentId",
|
||
|
"drop index if exists idx_TypeTopParentId6",
|
||
|
"drop index if exists idx_ItemValues2",
|
||
|
"drop index if exists Idx_ProviderIds",
|
||
9 years ago
|
"drop index if exists idx_ItemValues3",
|
||
|
"drop index if exists idx_ItemValues4",
|
||
|
"drop index if exists idx_ItemValues5",
|
||
8 years ago
|
"drop index if exists idx_UserDataKeys3",
|
||
|
"drop table if exists UserDataKeys",
|
||
9 years ago
|
|
||
9 years ago
|
"create index if not exists idx_PathTypedBaseItems on TypedBaseItems(Path)",
|
||
|
"create index if not exists idx_ParentIdTypedBaseItems on TypedBaseItems(ParentId)",
|
||
|
|
||
9 years ago
|
"create index if not exists idx_PresentationUniqueKey on TypedBaseItems(PresentationUniqueKey)",
|
||
9 years ago
|
"create index if not exists idx_GuidTypeIsFolderIsVirtualItem on TypedBaseItems(Guid,Type,IsFolder,IsVirtualItem)",
|
||
|
//"create index if not exists idx_GuidMediaTypeIsFolderIsVirtualItem on TypedBaseItems(Guid,MediaType,IsFolder,IsVirtualItem)",
|
||
9 years ago
|
"create index if not exists idx_CleanNameType on TypedBaseItems(CleanName,Type)",
|
||
9 years ago
|
|
||
|
// covering index
|
||
|
"create index if not exists idx_TopParentIdGuid on TypedBaseItems(TopParentId,Guid)",
|
||
9 years ago
|
|
||
9 years ago
|
// live tv programs
|
||
|
"create index if not exists idx_TypeTopParentIdStartDate on TypedBaseItems(Type,TopParentId,StartDate)",
|
||
|
|
||
9 years ago
|
// covering index for getitemvalues
|
||
|
"create index if not exists idx_TypeTopParentIdGuid on TypedBaseItems(Type,TopParentId,Guid)",
|
||
|
|
||
9 years ago
|
// used by movie suggestions
|
||
|
"create index if not exists idx_TypeTopParentIdGroup on TypedBaseItems(Type,TopParentId,PresentationUniqueKey)",
|
||
9 years ago
|
"create index if not exists idx_TypeTopParentId5 on TypedBaseItems(TopParentId,IsVirtualItem)",
|
||
9 years ago
|
|
||
|
// latest items
|
||
|
"create index if not exists idx_TypeTopParentId9 on TypedBaseItems(TopParentId,Type,IsVirtualItem,PresentationUniqueKey,DateCreated)",
|
||
9 years ago
|
"create index if not exists idx_TypeTopParentId8 on TypedBaseItems(TopParentId,IsFolder,IsVirtualItem,PresentationUniqueKey,DateCreated)",
|
||
|
|
||
|
// resume
|
||
9 years ago
|
"create index if not exists idx_TypeTopParentId7 on TypedBaseItems(TopParentId,MediaType,IsVirtualItem,PresentationUniqueKey)",
|
||
9 years ago
|
|
||
|
// items by name
|
||
9 years ago
|
"create index if not exists idx_ItemValues6 on ItemValues(ItemId,Type,CleanValue)",
|
||
8 years ago
|
"create index if not exists idx_ItemValues7 on ItemValues(Type,CleanValue,ItemId)"
|
||
9 years ago
|
};
|
||
9 years ago
|
|
||
9 years ago
|
_connection.RunQueries(postQueries, Logger);
|
||
9 years ago
|
|
||
9 years ago
|
PrepareStatements();
|
||
11 years ago
|
|
||
9 years ago
|
new MediaStreamColumns(_connection, Logger).AddColumns();
|
||
9 years ago
|
|
||
9 years ago
|
DataExtensions.Attach(_connection, Path.Combine(_config.ApplicationPaths.DataPath, "userdata_v2.db"), "UserDataDb");
|
||
9 years ago
|
await userDataRepo.Initialize(_connection, WriteLock).ConfigureAwait(false);
|
||
9 years ago
|
//await Vacuum(_connection).ConfigureAwait(false);
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
private readonly string[] _retriveItemColumns =
|
||
10 years ago
|
{
|
||
|
"type",
|
||
|
"data",
|
||
9 years ago
|
"StartDate",
|
||
9 years ago
|
"EndDate",
|
||
|
"IsOffline",
|
||
|
"ChannelId",
|
||
|
"IsMovie",
|
||
|
"IsSports",
|
||
9 years ago
|
"IsKids",
|
||
9 years ago
|
"IsSeries",
|
||
|
"IsLive",
|
||
|
"IsNews",
|
||
|
"IsPremiere",
|
||
9 years ago
|
"EpisodeTitle",
|
||
|
"IsRepeat",
|
||
9 years ago
|
"CommunityRating",
|
||
|
"CustomRating",
|
||
|
"IndexNumber",
|
||
9 years ago
|
"IsLocked",
|
||
|
"PreferredMetadataLanguage",
|
||
9 years ago
|
"PreferredMetadataCountryCode",
|
||
|
"IsHD",
|
||
|
"ExternalEtag",
|
||
9 years ago
|
"DateLastRefreshed",
|
||
|
"Name",
|
||
|
"Path",
|
||
|
"PremiereDate",
|
||
|
"Overview",
|
||
|
"ParentIndexNumber",
|
||
|
"ProductionYear",
|
||
|
"OfficialRating",
|
||
|
"OfficialRatingDescription",
|
||
|
"HomePageUrl",
|
||
|
"DisplayMediaType",
|
||
|
"ForcedSortName",
|
||
|
"RunTimeTicks",
|
||
|
"VoteCount",
|
||
|
"DateCreated",
|
||
|
"DateModified",
|
||
|
"guid",
|
||
|
"Genres",
|
||
|
"ParentId",
|
||
|
"Audio",
|
||
9 years ago
|
"ExternalServiceId",
|
||
|
"IsInMixedFolder",
|
||
|
"DateLastSaved",
|
||
|
"LockedFields",
|
||
|
"Studios",
|
||
9 years ago
|
"Tags",
|
||
9 years ago
|
"SourceType",
|
||
9 years ago
|
"TrailerTypes",
|
||
9 years ago
|
"OriginalTitle",
|
||
9 years ago
|
"PrimaryVersionId",
|
||
|
"DateLastMediaAdded",
|
||
9 years ago
|
"Album",
|
||
|
"CriticRating",
|
||
9 years ago
|
"CriticRatingSummary",
|
||
9 years ago
|
"IsVirtualItem",
|
||
|
"SeriesName",
|
||
9 years ago
|
"SeasonName",
|
||
9 years ago
|
"SeasonId",
|
||
9 years ago
|
"SeriesId",
|
||
9 years ago
|
"SeriesSortName",
|
||
8 years ago
|
"PresentationUniqueKey",
|
||
8 years ago
|
"InheritedParentalRatingValue",
|
||
8 years ago
|
"InheritedTags",
|
||
8 years ago
|
"ExternalSeriesId",
|
||
|
"ShortOverview",
|
||
|
"Tagline",
|
||
|
"Keywords",
|
||
|
"ProviderIds",
|
||
8 years ago
|
"Images",
|
||
|
"ProductionLocations",
|
||
|
"ThemeSongIds",
|
||
8 years ago
|
"ThemeVideoIds",
|
||
|
"TotalBitrate",
|
||
|
"ExtraType",
|
||
|
"Artists",
|
||
|
"AlbumArtists"
|
||
10 years ago
|
};
|
||
|
|
||
9 years ago
|
private 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",
|
||
9 years ago
|
"CodecTag",
|
||
9 years ago
|
"Comment",
|
||
9 years ago
|
"NalLengthSize",
|
||
9 years ago
|
"IsAvc",
|
||
9 years ago
|
"Title",
|
||
|
"TimeBase",
|
||
|
"CodecTimeBase"
|
||
9 years ago
|
};
|
||
|
|
||
9 years ago
|
/// <summary>
|
||
|
/// Prepares the statements.
|
||
|
/// </summary>
|
||
|
private void PrepareStatements()
|
||
|
{
|
||
|
var saveColumns = new List<string>
|
||
|
{
|
||
|
"guid",
|
||
|
"type",
|
||
|
"data",
|
||
|
"Path",
|
||
|
"StartDate",
|
||
|
"EndDate",
|
||
|
"ChannelId",
|
||
|
"IsKids",
|
||
|
"IsMovie",
|
||
|
"IsSports",
|
||
|
"IsSeries",
|
||
|
"IsLive",
|
||
|
"IsNews",
|
||
|
"IsPremiere",
|
||
|
"EpisodeTitle",
|
||
|
"IsRepeat",
|
||
|
"CommunityRating",
|
||
|
"CustomRating",
|
||
|
"IndexNumber",
|
||
|
"IsLocked",
|
||
|
"Name",
|
||
|
"OfficialRating",
|
||
|
"MediaType",
|
||
|
"Overview",
|
||
|
"ParentIndexNumber",
|
||
|
"PremiereDate",
|
||
|
"ProductionYear",
|
||
|
"ParentId",
|
||
|
"Genres",
|
||
|
"InheritedParentalRatingValue",
|
||
|
"SchemaVersion",
|
||
|
"SortName",
|
||
|
"RunTimeTicks",
|
||
|
"OfficialRatingDescription",
|
||
|
"HomePageUrl",
|
||
|
"VoteCount",
|
||
|
"DisplayMediaType",
|
||
|
"DateCreated",
|
||
|
"DateModified",
|
||
|
"ForcedSortName",
|
||
|
"IsOffline",
|
||
|
"LocationType",
|
||
|
"PreferredMetadataLanguage",
|
||
|
"PreferredMetadataCountryCode",
|
||
|
"IsHD",
|
||
|
"ExternalEtag",
|
||
|
"DateLastRefreshed",
|
||
|
"DateLastSaved",
|
||
|
"IsInMixedFolder",
|
||
|
"LockedFields",
|
||
|
"Studios",
|
||
|
"Audio",
|
||
|
"ExternalServiceId",
|
||
|
"Tags",
|
||
|
"IsFolder",
|
||
|
"UnratedType",
|
||
|
"TopParentId",
|
||
|
"IsItemByName",
|
||
|
"SourceType",
|
||
|
"TrailerTypes",
|
||
|
"CriticRating",
|
||
|
"CriticRatingSummary",
|
||
|
"InheritedTags",
|
||
|
"CleanName",
|
||
|
"PresentationUniqueKey",
|
||
|
"SlugName",
|
||
|
"OriginalTitle",
|
||
|
"PrimaryVersionId",
|
||
|
"DateLastMediaAdded",
|
||
|
"Album",
|
||
|
"IsVirtualItem",
|
||
9 years ago
|
"SeriesName",
|
||
9 years ago
|
"UserDataKey",
|
||
9 years ago
|
"SeasonName",
|
||
9 years ago
|
"SeasonId",
|
||
9 years ago
|
"SeriesId",
|
||
8 years ago
|
"SeriesSortName",
|
||
8 years ago
|
"ExternalSeriesId",
|
||
|
"ShortOverview",
|
||
|
"Tagline",
|
||
|
"Keywords",
|
||
|
"ProviderIds",
|
||
8 years ago
|
"Images",
|
||
|
"ProductionLocations",
|
||
|
"ThemeSongIds",
|
||
8 years ago
|
"ThemeVideoIds",
|
||
|
"TotalBitrate",
|
||
|
"ExtraType",
|
||
|
"Artists",
|
||
|
"AlbumArtists"
|
||
9 years ago
|
};
|
||
|
_saveItemCommand = _connection.CreateCommand();
|
||
|
_saveItemCommand.CommandText = "replace into TypedBaseItems (" + string.Join(",", saveColumns.ToArray()) + ") values (";
|
||
|
|
||
|
for (var i = 1; i <= saveColumns.Count; i++)
|
||
|
{
|
||
|
if (i > 1)
|
||
|
{
|
||
|
_saveItemCommand.CommandText += ",";
|
||
|
}
|
||
|
_saveItemCommand.CommandText += "@" + i.ToString(CultureInfo.InvariantCulture);
|
||
|
|
||
|
_saveItemCommand.Parameters.Add(_saveItemCommand, "@" + i.ToString(CultureInfo.InvariantCulture));
|
||
|
}
|
||
|
_saveItemCommand.CommandText += ")";
|
||
|
|
||
|
_deleteItemCommand = _connection.CreateCommand();
|
||
|
_deleteItemCommand.CommandText = "delete from TypedBaseItems where guid=@Id";
|
||
|
_deleteItemCommand.Parameters.Add(_deleteItemCommand, "@Id");
|
||
|
|
||
|
// People
|
||
|
_deletePeopleCommand = _connection.CreateCommand();
|
||
|
_deletePeopleCommand.CommandText = "delete from People where ItemId=@Id";
|
||
|
_deletePeopleCommand.Parameters.Add(_deletePeopleCommand, "@Id");
|
||
|
|
||
|
_savePersonCommand = _connection.CreateCommand();
|
||
|
_savePersonCommand.CommandText = "insert into People (ItemId, Name, Role, PersonType, SortOrder, ListOrder) values (@ItemId, @Name, @Role, @PersonType, @SortOrder, @ListOrder)";
|
||
|
_savePersonCommand.Parameters.Add(_savePersonCommand, "@ItemId");
|
||
|
_savePersonCommand.Parameters.Add(_savePersonCommand, "@Name");
|
||
|
_savePersonCommand.Parameters.Add(_savePersonCommand, "@Role");
|
||
|
_savePersonCommand.Parameters.Add(_savePersonCommand, "@PersonType");
|
||
|
_savePersonCommand.Parameters.Add(_savePersonCommand, "@SortOrder");
|
||
|
_savePersonCommand.Parameters.Add(_savePersonCommand, "@ListOrder");
|
||
|
|
||
|
// Ancestors
|
||
|
_deleteAncestorsCommand = _connection.CreateCommand();
|
||
|
_deleteAncestorsCommand.CommandText = "delete from AncestorIds where ItemId=@Id";
|
||
|
_deleteAncestorsCommand.Parameters.Add(_deleteAncestorsCommand, "@Id");
|
||
|
|
||
|
_saveAncestorCommand = _connection.CreateCommand();
|
||
|
_saveAncestorCommand.CommandText = "insert into AncestorIds (ItemId, AncestorId, AncestorIdText) values (@ItemId, @AncestorId, @AncestorIdText)";
|
||
|
_saveAncestorCommand.Parameters.Add(_saveAncestorCommand, "@ItemId");
|
||
|
_saveAncestorCommand.Parameters.Add(_saveAncestorCommand, "@AncestorId");
|
||
|
_saveAncestorCommand.Parameters.Add(_saveAncestorCommand, "@AncestorIdText");
|
||
|
|
||
|
// Chapters
|
||
|
_deleteChaptersCommand = _connection.CreateCommand();
|
||
|
_deleteChaptersCommand.CommandText = "delete from " + ChaptersTableName + " where ItemId=@ItemId";
|
||
|
_deleteChaptersCommand.Parameters.Add(_deleteChaptersCommand, "@ItemId");
|
||
|
|
||
|
_saveChapterCommand = _connection.CreateCommand();
|
||
|
_saveChapterCommand.CommandText = "replace into " + ChaptersTableName + " (ItemId, ChapterIndex, StartPositionTicks, Name, ImagePath) values (@ItemId, @ChapterIndex, @StartPositionTicks, @Name, @ImagePath)";
|
||
|
|
||
|
_saveChapterCommand.Parameters.Add(_saveChapterCommand, "@ItemId");
|
||
|
_saveChapterCommand.Parameters.Add(_saveChapterCommand, "@ChapterIndex");
|
||
|
_saveChapterCommand.Parameters.Add(_saveChapterCommand, "@StartPositionTicks");
|
||
|
_saveChapterCommand.Parameters.Add(_saveChapterCommand, "@Name");
|
||
|
_saveChapterCommand.Parameters.Add(_saveChapterCommand, "@ImagePath");
|
||
9 years ago
|
_saveChapterCommand.Parameters.Add(_saveChapterCommand, "@ImageDateModified");
|
||
9 years ago
|
|
||
|
// MediaStreams
|
||
|
_deleteStreamsCommand = _connection.CreateCommand();
|
||
|
_deleteStreamsCommand.CommandText = "delete from mediastreams where ItemId=@ItemId";
|
||
|
_deleteStreamsCommand.Parameters.Add(_deleteStreamsCommand, "@ItemId");
|
||
|
|
||
|
_saveStreamCommand = _connection.CreateCommand();
|
||
|
|
||
|
_saveStreamCommand.CommandText = string.Format("replace into mediastreams ({0}) values ({1})",
|
||
|
string.Join(",", _mediaStreamSaveColumns),
|
||
|
string.Join(",", _mediaStreamSaveColumns.Select(i => "@" + i).ToArray()));
|
||
|
|
||
|
foreach (var col in _mediaStreamSaveColumns)
|
||
|
{
|
||
|
_saveStreamCommand.Parameters.Add(_saveStreamCommand, "@" + col);
|
||
|
}
|
||
|
|
||
|
_updateInheritedTagsCommand = _connection.CreateCommand();
|
||
|
_updateInheritedTagsCommand.CommandText = "Update TypedBaseItems set InheritedTags=@InheritedTags where Guid=@Guid";
|
||
|
_updateInheritedTagsCommand.Parameters.Add(_updateInheritedTagsCommand, "@Guid");
|
||
|
_updateInheritedTagsCommand.Parameters.Add(_updateInheritedTagsCommand, "@InheritedTags");
|
||
|
|
||
|
// item values
|
||
|
_deleteItemValuesCommand = _connection.CreateCommand();
|
||
|
_deleteItemValuesCommand.CommandText = "delete from ItemValues where ItemId=@Id";
|
||
|
_deleteItemValuesCommand.Parameters.Add(_deleteItemValuesCommand, "@Id");
|
||
|
|
||
|
_saveItemValuesCommand = _connection.CreateCommand();
|
||
9 years ago
|
_saveItemValuesCommand.CommandText = "insert into ItemValues (ItemId, Type, Value, CleanValue) values (@ItemId, @Type, @Value, @CleanValue)";
|
||
9 years ago
|
_saveItemValuesCommand.Parameters.Add(_saveItemValuesCommand, "@ItemId");
|
||
|
_saveItemValuesCommand.Parameters.Add(_saveItemValuesCommand, "@Type");
|
||
|
_saveItemValuesCommand.Parameters.Add(_saveItemValuesCommand, "@Value");
|
||
9 years ago
|
_saveItemValuesCommand.Parameters.Add(_saveItemValuesCommand, "@CleanValue");
|
||
9 years ago
|
|
||
|
// provider ids
|
||
|
_deleteProviderIdsCommand = _connection.CreateCommand();
|
||
|
_deleteProviderIdsCommand.CommandText = "delete from ProviderIds where ItemId=@Id";
|
||
|
_deleteProviderIdsCommand.Parameters.Add(_deleteProviderIdsCommand, "@Id");
|
||
|
|
||
|
_saveProviderIdsCommand = _connection.CreateCommand();
|
||
|
_saveProviderIdsCommand.CommandText = "insert into ProviderIds (ItemId, Name, Value) values (@ItemId, @Name, @Value)";
|
||
|
_saveProviderIdsCommand.Parameters.Add(_saveProviderIdsCommand, "@ItemId");
|
||
|
_saveProviderIdsCommand.Parameters.Add(_saveProviderIdsCommand, "@Name");
|
||
|
_saveProviderIdsCommand.Parameters.Add(_saveProviderIdsCommand, "@Value");
|
||
|
|
||
|
// images
|
||
|
_deleteImagesCommand = _connection.CreateCommand();
|
||
|
_deleteImagesCommand.CommandText = "delete from Images where ItemId=@Id";
|
||
|
_deleteImagesCommand.Parameters.Add(_deleteImagesCommand, "@Id");
|
||
|
|
||
|
_saveImagesCommand = _connection.CreateCommand();
|
||
|
_saveImagesCommand.CommandText = "insert into Images (ItemId, ImageType, Path, DateModified, IsPlaceHolder, SortOrder) values (@ItemId, @ImageType, @Path, @DateModified, @IsPlaceHolder, @SortOrder)";
|
||
|
_saveImagesCommand.Parameters.Add(_saveImagesCommand, "@ItemId");
|
||
|
_saveImagesCommand.Parameters.Add(_saveImagesCommand, "@ImageType");
|
||
|
_saveImagesCommand.Parameters.Add(_saveImagesCommand, "@Path");
|
||
|
_saveImagesCommand.Parameters.Add(_saveImagesCommand, "@DateModified");
|
||
|
_saveImagesCommand.Parameters.Add(_saveImagesCommand, "@IsPlaceHolder");
|
||
|
_saveImagesCommand.Parameters.Add(_saveImagesCommand, "@SortOrder");
|
||
|
}
|
||
|
|
||
12 years ago
|
/// <summary>
|
||
|
/// Save a standard item in the repo
|
||
|
/// </summary>
|
||
|
/// <param name="item">The item.</param>
|
||
|
/// <param name="cancellationToken">The cancellation token.</param>
|
||
|
/// <returns>Task.</returns>
|
||
|
/// <exception cref="System.ArgumentNullException">item</exception>
|
||
|
public Task SaveItem(BaseItem item, CancellationToken cancellationToken)
|
||
|
{
|
||
|
if (item == null)
|
||
|
{
|
||
|
throw new ArgumentNullException("item");
|
||
|
}
|
||
|
|
||
|
return SaveItems(new[] { item }, cancellationToken);
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// Saves the items.
|
||
|
/// </summary>
|
||
|
/// <param name="items">The items.</param>
|
||
|
/// <param name="cancellationToken">The cancellation token.</param>
|
||
|
/// <returns>Task.</returns>
|
||
|
/// <exception cref="System.ArgumentNullException">
|
||
|
/// items
|
||
|
/// or
|
||
|
/// cancellationToken
|
||
|
/// </exception>
|
||
|
public async Task SaveItems(IEnumerable<BaseItem> items, CancellationToken cancellationToken)
|
||
|
{
|
||
|
if (items == null)
|
||
|
{
|
||
|
throw new ArgumentNullException("items");
|
||
|
}
|
||
|
|
||
|
cancellationToken.ThrowIfCancellationRequested();
|
||
|
|
||
10 years ago
|
CheckDisposed();
|
||
10 years ago
|
|
||
9 years ago
|
await WriteLock.WaitAsync(cancellationToken).ConfigureAwait(false);
|
||
|
|
||
|
IDbTransaction transaction = null;
|
||
|
|
||
|
try
|
||
12 years ago
|
{
|
||
9 years ago
|
transaction = _connection.BeginTransaction();
|
||
12 years ago
|
|
||
9 years ago
|
foreach (var item in items)
|
||
12 years ago
|
{
|
||
9 years ago
|
cancellationToken.ThrowIfCancellationRequested();
|
||
|
|
||
|
var index = 0;
|
||
9 years ago
|
|
||
9 years ago
|
_saveItemCommand.GetParameter(index++).Value = item.Id;
|
||
|
_saveItemCommand.GetParameter(index++).Value = item.GetType().FullName;
|
||
8 years ago
|
|
||
|
if (TypeRequiresDeserialization(item.GetType()))
|
||
|
{
|
||
|
_saveItemCommand.GetParameter(index++).Value = _jsonSerializer.SerializeToBytes(item, _memoryStreamProvider);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
_saveItemCommand.GetParameter(index++).Value = null;
|
||
|
}
|
||
9 years ago
|
|
||
|
_saveItemCommand.GetParameter(index++).Value = item.Path;
|
||
|
|
||
|
var hasStartDate = item as IHasStartDate;
|
||
|
if (hasStartDate != null)
|
||
9 years ago
|
{
|
||
9 years ago
|
_saveItemCommand.GetParameter(index++).Value = hasStartDate.StartDate;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
_saveItemCommand.GetParameter(index++).Value = null;
|
||
|
}
|
||
9 years ago
|
|
||
9 years ago
|
_saveItemCommand.GetParameter(index++).Value = item.EndDate;
|
||
|
_saveItemCommand.GetParameter(index++).Value = item.ChannelId;
|
||
9 years ago
|
|
||
9 years ago
|
var hasProgramAttributes = item as IHasProgramAttributes;
|
||
|
if (hasProgramAttributes != null)
|
||
|
{
|
||
|
_saveItemCommand.GetParameter(index++).Value = hasProgramAttributes.IsKids;
|
||
|
_saveItemCommand.GetParameter(index++).Value = hasProgramAttributes.IsMovie;
|
||
|
_saveItemCommand.GetParameter(index++).Value = hasProgramAttributes.IsSports;
|
||
|
_saveItemCommand.GetParameter(index++).Value = hasProgramAttributes.IsSeries;
|
||
|
_saveItemCommand.GetParameter(index++).Value = hasProgramAttributes.IsLive;
|
||
|
_saveItemCommand.GetParameter(index++).Value = hasProgramAttributes.IsNews;
|
||
|
_saveItemCommand.GetParameter(index++).Value = hasProgramAttributes.IsPremiere;
|
||
|
_saveItemCommand.GetParameter(index++).Value = hasProgramAttributes.EpisodeTitle;
|
||
|
_saveItemCommand.GetParameter(index++).Value = hasProgramAttributes.IsRepeat;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
_saveItemCommand.GetParameter(index++).Value = null;
|
||
|
_saveItemCommand.GetParameter(index++).Value = null;
|
||
|
_saveItemCommand.GetParameter(index++).Value = null;
|
||
|
_saveItemCommand.GetParameter(index++).Value = null;
|
||
|
_saveItemCommand.GetParameter(index++).Value = null;
|
||
|
_saveItemCommand.GetParameter(index++).Value = null;
|
||
|
_saveItemCommand.GetParameter(index++).Value = null;
|
||
|
_saveItemCommand.GetParameter(index++).Value = null;
|
||
|
_saveItemCommand.GetParameter(index++).Value = null;
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
_saveItemCommand.GetParameter(index++).Value = item.CommunityRating;
|
||
|
_saveItemCommand.GetParameter(index++).Value = item.CustomRating;
|
||
|
|
||
|
_saveItemCommand.GetParameter(index++).Value = item.IndexNumber;
|
||
|
_saveItemCommand.GetParameter(index++).Value = item.IsLocked;
|
||
|
|
||
|
_saveItemCommand.GetParameter(index++).Value = item.Name;
|
||
|
_saveItemCommand.GetParameter(index++).Value = item.OfficialRating;
|
||
|
|
||
|
_saveItemCommand.GetParameter(index++).Value = item.MediaType;
|
||
|
_saveItemCommand.GetParameter(index++).Value = item.Overview;
|
||
|
_saveItemCommand.GetParameter(index++).Value = item.ParentIndexNumber;
|
||
|
_saveItemCommand.GetParameter(index++).Value = item.PremiereDate;
|
||
|
_saveItemCommand.GetParameter(index++).Value = item.ProductionYear;
|
||
|
|
||
|
if (item.ParentId == Guid.Empty)
|
||
|
{
|
||
|
_saveItemCommand.GetParameter(index++).Value = null;
|
||
|
}
|
||
|
else
|
||
9 years ago
|
{
|
||
9 years ago
|
_saveItemCommand.GetParameter(index++).Value = item.ParentId;
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
9 years ago
|
_saveItemCommand.GetParameter(index++).Value = string.Join("|", item.Genres.ToArray());
|
||
|
_saveItemCommand.GetParameter(index++).Value = item.GetInheritedParentalRatingValue() ?? 0;
|
||
|
|
||
|
_saveItemCommand.GetParameter(index++).Value = LatestSchemaVersion;
|
||
|
_saveItemCommand.GetParameter(index++).Value = item.SortName;
|
||
|
_saveItemCommand.GetParameter(index++).Value = item.RunTimeTicks;
|
||
|
|
||
|
_saveItemCommand.GetParameter(index++).Value = item.OfficialRatingDescription;
|
||
|
_saveItemCommand.GetParameter(index++).Value = item.HomePageUrl;
|
||
|
_saveItemCommand.GetParameter(index++).Value = item.VoteCount;
|
||
|
_saveItemCommand.GetParameter(index++).Value = item.DisplayMediaType;
|
||
|
_saveItemCommand.GetParameter(index++).Value = item.DateCreated;
|
||
|
_saveItemCommand.GetParameter(index++).Value = item.DateModified;
|
||
|
|
||
|
_saveItemCommand.GetParameter(index++).Value = item.ForcedSortName;
|
||
|
_saveItemCommand.GetParameter(index++).Value = item.IsOffline;
|
||
|
_saveItemCommand.GetParameter(index++).Value = item.LocationType.ToString();
|
||
|
|
||
|
_saveItemCommand.GetParameter(index++).Value = item.PreferredMetadataLanguage;
|
||
|
_saveItemCommand.GetParameter(index++).Value = item.PreferredMetadataCountryCode;
|
||
|
_saveItemCommand.GetParameter(index++).Value = item.IsHD;
|
||
|
_saveItemCommand.GetParameter(index++).Value = item.ExternalEtag;
|
||
|
|
||
|
if (item.DateLastRefreshed == default(DateTime))
|
||
|
{
|
||
|
_saveItemCommand.GetParameter(index++).Value = null;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
_saveItemCommand.GetParameter(index++).Value = item.DateLastRefreshed;
|
||
|
}
|
||
12 years ago
|
|
||
9 years ago
|
if (item.DateLastSaved == default(DateTime))
|
||
9 years ago
|
{
|
||
9 years ago
|
_saveItemCommand.GetParameter(index++).Value = null;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
_saveItemCommand.GetParameter(index++).Value = item.DateLastSaved;
|
||
9 years ago
|
}
|
||
12 years ago
|
|
||
9 years ago
|
_saveItemCommand.GetParameter(index++).Value = item.IsInMixedFolder;
|
||
|
_saveItemCommand.GetParameter(index++).Value = string.Join("|", item.LockedFields.Select(i => i.ToString()).ToArray());
|
||
|
_saveItemCommand.GetParameter(index++).Value = string.Join("|", item.Studios.ToArray());
|
||
|
|
||
|
if (item.Audio.HasValue)
|
||
|
{
|
||
|
_saveItemCommand.GetParameter(index++).Value = item.Audio.Value.ToString();
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
_saveItemCommand.GetParameter(index++).Value = null;
|
||
|
}
|
||
|
|
||
|
_saveItemCommand.GetParameter(index++).Value = item.ServiceName;
|
||
|
|
||
|
if (item.Tags.Count > 0)
|
||
|
{
|
||
|
_saveItemCommand.GetParameter(index++).Value = string.Join("|", item.Tags.ToArray());
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
_saveItemCommand.GetParameter(index++).Value = null;
|
||
|
}
|
||
|
|
||
|
_saveItemCommand.GetParameter(index++).Value = item.IsFolder;
|
||
|
|
||
|
_saveItemCommand.GetParameter(index++).Value = item.GetBlockUnratedType().ToString();
|
||
|
|
||
|
var topParent = item.GetTopParent();
|
||
|
if (topParent != null)
|
||
|
{
|
||
|
//Logger.Debug("Item {0} has top parent {1}", item.Id, topParent.Id);
|
||
|
_saveItemCommand.GetParameter(index++).Value = topParent.Id.ToString("N");
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
//Logger.Debug("Item {0} has null top parent", item.Id);
|
||
|
_saveItemCommand.GetParameter(index++).Value = null;
|
||
|
}
|
||
|
|
||
|
var isByName = false;
|
||
|
var byName = item as IItemByName;
|
||
|
if (byName != null)
|
||
|
{
|
||
|
var dualAccess = item as IHasDualAccess;
|
||
|
isByName = dualAccess == null || dualAccess.IsAccessedByName;
|
||
|
}
|
||
|
_saveItemCommand.GetParameter(index++).Value = isByName;
|
||
|
|
||
|
_saveItemCommand.GetParameter(index++).Value = item.SourceType.ToString();
|
||
|
|
||
|
var trailer = item as Trailer;
|
||
|
if (trailer != null && trailer.TrailerTypes.Count > 0)
|
||
|
{
|
||
|
_saveItemCommand.GetParameter(index++).Value = string.Join("|", trailer.TrailerTypes.Select(i => i.ToString()).ToArray());
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
_saveItemCommand.GetParameter(index++).Value = null;
|
||
|
}
|
||
|
|
||
|
_saveItemCommand.GetParameter(index++).Value = item.CriticRating;
|
||
|
_saveItemCommand.GetParameter(index++).Value = item.CriticRatingSummary;
|
||
|
|
||
|
var inheritedTags = item.GetInheritedTags();
|
||
|
if (inheritedTags.Count > 0)
|
||
9 years ago
|
{
|
||
9 years ago
|
_saveItemCommand.GetParameter(index++).Value = string.Join("|", inheritedTags.ToArray());
|
||
9 years ago
|
}
|
||
9 years ago
|
else
|
||
|
{
|
||
|
_saveItemCommand.GetParameter(index++).Value = null;
|
||
|
}
|
||
|
|
||
|
if (string.IsNullOrWhiteSpace(item.Name))
|
||
|
{
|
||
|
_saveItemCommand.GetParameter(index++).Value = null;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
9 years ago
|
_saveItemCommand.GetParameter(index++).Value = GetCleanValue(item.Name);
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
_saveItemCommand.GetParameter(index++).Value = item.GetPresentationUniqueKey();
|
||
9 years ago
|
_saveItemCommand.GetParameter(index++).Value = item.SlugName;
|
||
|
_saveItemCommand.GetParameter(index++).Value = item.OriginalTitle;
|
||
|
|
||
|
var video = item as Video;
|
||
|
if (video != null)
|
||
|
{
|
||
|
_saveItemCommand.GetParameter(index++).Value = video.PrimaryVersionId;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
_saveItemCommand.GetParameter(index++).Value = null;
|
||
|
}
|
||
|
|
||
|
var folder = item as Folder;
|
||
|
if (folder != null && folder.DateLastMediaAdded.HasValue)
|
||
|
{
|
||
|
_saveItemCommand.GetParameter(index++).Value = folder.DateLastMediaAdded.Value;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
_saveItemCommand.GetParameter(index++).Value = null;
|
||
|
}
|
||
|
|
||
|
_saveItemCommand.GetParameter(index++).Value = item.Album;
|
||
|
|
||
8 years ago
|
_saveItemCommand.GetParameter(index++).Value = item.IsVirtualItem;
|
||
9 years ago
|
|
||
|
var hasSeries = item as IHasSeries;
|
||
|
if (hasSeries != null)
|
||
|
{
|
||
9 years ago
|
_saveItemCommand.GetParameter(index++).Value = hasSeries.FindSeriesName();
|
||
9 years ago
|
}
|
||
|
else
|
||
|
{
|
||
|
_saveItemCommand.GetParameter(index++).Value = null;
|
||
|
}
|
||
|
|
||
9 years ago
|
_saveItemCommand.GetParameter(index++).Value = item.GetUserDataKeys().FirstOrDefault();
|
||
|
|
||
9 years ago
|
var episode = item as Episode;
|
||
|
if (episode != null)
|
||
|
{
|
||
|
_saveItemCommand.GetParameter(index++).Value = episode.FindSeasonName();
|
||
9 years ago
|
_saveItemCommand.GetParameter(index++).Value = episode.FindSeasonId();
|
||
9 years ago
|
}
|
||
|
else
|
||
|
{
|
||
|
_saveItemCommand.GetParameter(index++).Value = null;
|
||
9 years ago
|
_saveItemCommand.GetParameter(index++).Value = null;
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
if (hasSeries != null)
|
||
|
{
|
||
|
_saveItemCommand.GetParameter(index++).Value = hasSeries.FindSeriesId();
|
||
9 years ago
|
_saveItemCommand.GetParameter(index++).Value = hasSeries.FindSeriesSortName();
|
||
9 years ago
|
}
|
||
|
else
|
||
|
{
|
||
|
_saveItemCommand.GetParameter(index++).Value = null;
|
||
9 years ago
|
_saveItemCommand.GetParameter(index++).Value = null;
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
_saveItemCommand.GetParameter(index++).Value = item.ExternalSeriesId;
|
||
8 years ago
|
_saveItemCommand.GetParameter(index++).Value = item.ShortOverview;
|
||
|
_saveItemCommand.GetParameter(index++).Value = item.Tagline;
|
||
8 years ago
|
|
||
|
if (item.Keywords.Count > 0)
|
||
|
{
|
||
|
_saveItemCommand.GetParameter(index++).Value = string.Join("|", item.Keywords.ToArray());
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
_saveItemCommand.GetParameter(index++).Value = null;
|
||
|
}
|
||
|
|
||
8 years ago
|
_saveItemCommand.GetParameter(index++).Value = SerializeProviderIds(item);
|
||
|
_saveItemCommand.GetParameter(index++).Value = SerializeImages(item);
|
||
8 years ago
|
|
||
8 years ago
|
if (item.ProductionLocations.Count > 0)
|
||
|
{
|
||
|
_saveItemCommand.GetParameter(index++).Value = string.Join("|", item.ProductionLocations.ToArray());
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
_saveItemCommand.GetParameter(index++).Value = null;
|
||
|
}
|
||
|
|
||
|
if (item.ThemeSongIds.Count > 0)
|
||
|
{
|
||
|
_saveItemCommand.GetParameter(index++).Value = string.Join("|", item.ThemeSongIds.Select(i => i.ToString("N")).ToArray());
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
_saveItemCommand.GetParameter(index++).Value = null;
|
||
|
}
|
||
|
|
||
|
if (item.ThemeVideoIds.Count > 0)
|
||
|
{
|
||
|
_saveItemCommand.GetParameter(index++).Value = string.Join("|", item.ThemeVideoIds.Select(i => i.ToString("N")).ToArray());
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
_saveItemCommand.GetParameter(index++).Value = null;
|
||
|
}
|
||
|
|
||
8 years ago
|
_saveItemCommand.GetParameter(index++).Value = item.TotalBitrate;
|
||
|
_saveItemCommand.GetParameter(index++).Value = item.ExtraType;
|
||
|
|
||
|
var hasArtists = item as IHasArtist;
|
||
|
if (hasArtists != null)
|
||
|
{
|
||
|
if (hasArtists.Artists.Count > 0)
|
||
|
{
|
||
|
_saveItemCommand.GetParameter(index++).Value = string.Join("|", hasArtists.Artists.ToArray());
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
_saveItemCommand.GetParameter(index++).Value = null;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
var hasAlbumArtists = item as IHasAlbumArtist;
|
||
|
if (hasAlbumArtists != null)
|
||
|
{
|
||
|
if (hasAlbumArtists.AlbumArtists.Count > 0)
|
||
|
{
|
||
|
_saveItemCommand.GetParameter(index++).Value = string.Join("|", hasAlbumArtists.AlbumArtists.ToArray());
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
_saveItemCommand.GetParameter(index++).Value = null;
|
||
|
}
|
||
|
}
|
||
|
|
||
9 years ago
|
_saveItemCommand.Transaction = transaction;
|
||
|
|
||
|
_saveItemCommand.ExecuteNonQuery();
|
||
|
|
||
|
if (item.SupportsAncestors)
|
||
|
{
|
||
|
UpdateAncestors(item.Id, item.GetAncestorIds().Distinct().ToList(), transaction);
|
||
|
}
|
||
|
|
||
|
UpdateImages(item.Id, item.ImageInfos, transaction);
|
||
|
UpdateProviderIds(item.Id, item.ProviderIds, transaction);
|
||
9 years ago
|
UpdateItemValues(item.Id, GetItemValuesToSave(item), transaction);
|
||
9 years ago
|
}
|
||
|
|
||
|
transaction.Commit();
|
||
|
}
|
||
|
catch (OperationCanceledException)
|
||
|
{
|
||
|
if (transaction != null)
|
||
|
{
|
||
|
transaction.Rollback();
|
||
|
}
|
||
|
|
||
|
throw;
|
||
|
}
|
||
|
catch (Exception e)
|
||
|
{
|
||
|
Logger.ErrorException("Failed to save items:", e);
|
||
|
|
||
|
if (transaction != null)
|
||
|
{
|
||
|
transaction.Rollback();
|
||
|
}
|
||
|
|
||
|
throw;
|
||
|
}
|
||
|
finally
|
||
|
{
|
||
|
if (transaction != null)
|
||
|
{
|
||
|
transaction.Dispose();
|
||
12 years ago
|
}
|
||
9 years ago
|
|
||
|
WriteLock.Release();
|
||
12 years ago
|
}
|
||
|
}
|
||
10 years ago
|
|
||
8 years ago
|
private string SerializeProviderIds(BaseItem item)
|
||
|
{
|
||
8 years ago
|
// Ideally we shouldn't need this IsNullOrWhiteSpace check but we're seeing some cases of bad data slip through
|
||
|
var ids = item.ProviderIds
|
||
|
.Where(i => !string.IsNullOrWhiteSpace(i.Value))
|
||
|
.ToList();
|
||
8 years ago
|
|
||
|
if (ids.Count == 0)
|
||
|
{
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
return string.Join("|", ids.Select(i => i.Key + "=" + i.Value).ToArray());
|
||
|
}
|
||
|
|
||
|
private void DeserializeProviderIds(string value, BaseItem item)
|
||
|
{
|
||
|
if (string.IsNullOrWhiteSpace(value))
|
||
|
{
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
if (item.ProviderIds.Count > 0)
|
||
|
{
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
var parts = value.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
|
||
|
|
||
|
foreach (var part in parts)
|
||
|
{
|
||
|
var idParts = part.Split('=');
|
||
|
|
||
8 years ago
|
if (idParts.Length == 2)
|
||
|
{
|
||
|
item.SetProviderId(idParts[0], idParts[1]);
|
||
|
}
|
||
8 years ago
|
}
|
||
|
}
|
||
|
|
||
|
private string SerializeImages(BaseItem item)
|
||
|
{
|
||
|
var images = item.ImageInfos.ToList();
|
||
|
|
||
|
if (images.Count == 0)
|
||
|
{
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
return string.Join("|", images.Select(ToValueString).ToArray());
|
||
|
}
|
||
|
|
||
|
private void DeserializeImages(string value, BaseItem item)
|
||
|
{
|
||
|
if (string.IsNullOrWhiteSpace(value))
|
||
|
{
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
if (item.ImageInfos.Count > 0)
|
||
|
{
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
var parts = value.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
|
||
|
|
||
|
foreach (var part in parts)
|
||
|
{
|
||
|
item.ImageInfos.Add(ItemImageInfoFromValueString(part));
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public string ToValueString(ItemImageInfo image)
|
||
|
{
|
||
|
var delimeter = "*";
|
||
|
|
||
|
return (image.Path ?? string.Empty) +
|
||
|
delimeter +
|
||
|
image.DateModified.Ticks.ToString(CultureInfo.InvariantCulture) +
|
||
|
delimeter +
|
||
|
image.Type +
|
||
|
delimeter +
|
||
|
image.IsPlaceholder;
|
||
|
}
|
||
|
|
||
|
public ItemImageInfo ItemImageInfoFromValueString(string value)
|
||
|
{
|
||
8 years ago
|
var parts = value.Split(new[] { '*' }, StringSplitOptions.None);
|
||
8 years ago
|
|
||
|
var image = new ItemImageInfo();
|
||
|
|
||
|
image.Path = parts[0];
|
||
|
image.DateModified = new DateTime(long.Parse(parts[1], CultureInfo.InvariantCulture), DateTimeKind.Utc);
|
||
|
image.Type = (ImageType)Enum.Parse(typeof(ImageType), parts[2], true);
|
||
|
image.IsPlaceholder = string.Equals(parts[3], true.ToString(), StringComparison.OrdinalIgnoreCase);
|
||
|
|
||
|
return image;
|
||
|
}
|
||
|
|
||
12 years ago
|
/// <summary>
|
||
|
/// Internal retrieve from items or users table
|
||
|
/// </summary>
|
||
|
/// <param name="id">The id.</param>
|
||
|
/// <returns>BaseItem.</returns>
|
||
|
/// <exception cref="System.ArgumentNullException">id</exception>
|
||
|
/// <exception cref="System.ArgumentException"></exception>
|
||
12 years ago
|
public BaseItem RetrieveItem(Guid id)
|
||
12 years ago
|
{
|
||
|
if (id == Guid.Empty)
|
||
|
{
|
||
|
throw new ArgumentNullException("id");
|
||
|
}
|
||
|
|
||
10 years ago
|
CheckDisposed();
|
||
10 years ago
|
|
||
9 years ago
|
using (var cmd = _connection.CreateCommand())
|
||
12 years ago
|
{
|
||
9 years ago
|
cmd.CommandText = "select " + string.Join(",", _retriveItemColumns) + " from TypedBaseItems where guid = @guid";
|
||
|
cmd.Parameters.Add(cmd, "@guid", DbType.Guid).Value = id;
|
||
9 years ago
|
|
||
9 years ago
|
using (var reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess | CommandBehavior.SingleResult | CommandBehavior.SingleRow))
|
||
|
{
|
||
|
if (reader.Read())
|
||
12 years ago
|
{
|
||
9 years ago
|
return GetItem(reader);
|
||
11 years ago
|
}
|
||
|
}
|
||
9 years ago
|
return null;
|
||
11 years ago
|
}
|
||
|
}
|
||
12 years ago
|
|
||
11 years ago
|
private BaseItem GetItem(IDataReader reader)
|
||
8 years ago
|
{
|
||
|
return GetItem(reader, new InternalItemsQuery());
|
||
|
}
|
||
|
|
||
|
private bool TypeRequiresDeserialization(Type type)
|
||
|
{
|
||
|
if (_config.Configuration.SkipDeserializationForBasicTypes)
|
||
|
{
|
||
|
if (type == typeof(MusicGenre))
|
||
|
{
|
||
|
return false;
|
||
|
}
|
||
|
if (type == typeof(GameGenre))
|
||
|
{
|
||
|
return false;
|
||
|
}
|
||
|
if (type == typeof(Genre))
|
||
|
{
|
||
|
return false;
|
||
|
}
|
||
|
if (type == typeof(Studio))
|
||
|
{
|
||
|
return false;
|
||
|
}
|
||
|
if (type == typeof(Year))
|
||
|
{
|
||
|
return false;
|
||
|
}
|
||
|
if (type == typeof(Book))
|
||
|
{
|
||
|
return false;
|
||
|
}
|
||
8 years ago
|
if (type == typeof(Person))
|
||
|
{
|
||
|
return false;
|
||
|
}
|
||
|
if (type == typeof(RecordingGroup))
|
||
|
{
|
||
|
return false;
|
||
|
}
|
||
|
if (type == typeof(Channel))
|
||
|
{
|
||
|
return false;
|
||
|
}
|
||
|
if (type == typeof(ManualCollectionsFolder))
|
||
|
{
|
||
|
return false;
|
||
|
}
|
||
|
if (type == typeof(CameraUploadsFolder))
|
||
|
{
|
||
|
return false;
|
||
|
}
|
||
|
if (type == typeof(PlaylistsFolder))
|
||
|
{
|
||
|
return false;
|
||
|
}
|
||
|
if (type == typeof(UserRootFolder))
|
||
|
{
|
||
|
return false;
|
||
|
}
|
||
|
if (type == typeof(PhotoAlbum))
|
||
|
{
|
||
|
return false;
|
||
|
}
|
||
|
if (type == typeof(Season))
|
||
|
{
|
||
|
return false;
|
||
|
}
|
||
|
if (type == typeof(MusicArtist))
|
||
|
{
|
||
|
return false;
|
||
|
}
|
||
8 years ago
|
}
|
||
|
if (_config.Configuration.SkipDeserializationForPrograms)
|
||
|
{
|
||
|
if (type == typeof(LiveTvProgram))
|
||
|
{
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
8 years ago
|
if (_config.Configuration.SkipDeserializationForAudio)
|
||
|
{
|
||
|
if (type == typeof(Audio))
|
||
|
{
|
||
|
return false;
|
||
|
}
|
||
|
if (type == typeof(LiveTvAudioRecording))
|
||
|
{
|
||
|
return false;
|
||
|
}
|
||
|
if (type == typeof(AudioPodcast))
|
||
|
{
|
||
|
return false;
|
||
|
}
|
||
|
if (type == typeof(MusicAlbum))
|
||
|
{
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
8 years ago
|
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
private BaseItem GetItem(IDataReader reader, InternalItemsQuery query)
|
||
11 years ago
|
{
|
||
|
var typeString = reader.GetString(0);
|
||
12 years ago
|
|
||
11 years ago
|
var type = _typeMapper.GetType(typeString);
|
||
12 years ago
|
|
||
11 years ago
|
if (type == null)
|
||
|
{
|
||
9 years ago
|
//Logger.Debug("Unknown type {0}", typeString);
|
||
12 years ago
|
|
||
12 years ago
|
return null;
|
||
|
}
|
||
11 years ago
|
|
||
9 years ago
|
BaseItem item = null;
|
||
10 years ago
|
|
||
8 years ago
|
if (TypeRequiresDeserialization(type))
|
||
11 years ago
|
{
|
||
8 years ago
|
using (var stream = reader.GetMemoryStream(1, _memoryStreamProvider))
|
||
9 years ago
|
{
|
||
|
try
|
||
|
{
|
||
8 years ago
|
item = _jsonSerializer.DeserializeFromStream(stream, type) as BaseItem;
|
||
9 years ago
|
}
|
||
8 years ago
|
catch (SerializationException ex)
|
||
10 years ago
|
{
|
||
8 years ago
|
Logger.ErrorException("Error deserializing item", ex);
|
||
10 years ago
|
}
|
||
10 years ago
|
}
|
||
8 years ago
|
}
|
||
9 years ago
|
|
||
8 years ago
|
if (item == null)
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
item = Activator.CreateInstance(type) as BaseItem;
|
||
|
}
|
||
|
catch
|
||
10 years ago
|
{
|
||
|
}
|
||
11 years ago
|
}
|
||
10 years ago
|
|
||
8 years ago
|
if (item == null)
|
||
|
{
|
||
|
return null;
|
||
|
}
|
||
|
|
||
10 years ago
|
if (!reader.IsDBNull(2))
|
||
|
{
|
||
9 years ago
|
var hasStartDate = item as IHasStartDate;
|
||
|
if (hasStartDate != null)
|
||
|
{
|
||
|
hasStartDate.StartDate = reader.GetDateTime(2).ToUniversalTime();
|
||
|
}
|
||
9 years ago
|
}
|
||
|
|
||
|
if (!reader.IsDBNull(3))
|
||
|
{
|
||
9 years ago
|
item.EndDate = reader.GetDateTime(3).ToUniversalTime();
|
||
9 years ago
|
}
|
||
|
|
||
|
if (!reader.IsDBNull(4))
|
||
|
{
|
||
9 years ago
|
item.IsOffline = reader.GetBoolean(4);
|
||
|
}
|
||
|
|
||
|
if (!reader.IsDBNull(5))
|
||
|
{
|
||
|
item.ChannelId = reader.GetString(5);
|
||
9 years ago
|
}
|
||
|
|
||
|
var hasProgramAttributes = item as IHasProgramAttributes;
|
||
|
if (hasProgramAttributes != null)
|
||
|
{
|
||
9 years ago
|
if (!reader.IsDBNull(6))
|
||
9 years ago
|
{
|
||
9 years ago
|
hasProgramAttributes.IsMovie = reader.GetBoolean(6);
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
if (!reader.IsDBNull(7))
|
||
9 years ago
|
{
|
||
9 years ago
|
hasProgramAttributes.IsSports = reader.GetBoolean(7);
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
if (!reader.IsDBNull(8))
|
||
9 years ago
|
{
|
||
9 years ago
|
hasProgramAttributes.IsKids = reader.GetBoolean(8);
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
|
if (!reader.IsDBNull(9))
|
||
|
{
|
||
|
hasProgramAttributes.IsSeries = reader.GetBoolean(9);
|
||
|
}
|
||
|
|
||
|
if (!reader.IsDBNull(10))
|
||
|
{
|
||
|
hasProgramAttributes.IsLive = reader.GetBoolean(10);
|
||
|
}
|
||
|
|
||
|
if (!reader.IsDBNull(11))
|
||
|
{
|
||
|
hasProgramAttributes.IsNews = reader.GetBoolean(11);
|
||
|
}
|
||
|
|
||
|
if (!reader.IsDBNull(12))
|
||
|
{
|
||
|
hasProgramAttributes.IsPremiere = reader.GetBoolean(12);
|
||
|
}
|
||
9 years ago
|
|
||
|
if (!reader.IsDBNull(13))
|
||
|
{
|
||
|
hasProgramAttributes.EpisodeTitle = reader.GetString(13);
|
||
|
}
|
||
|
|
||
|
if (!reader.IsDBNull(14))
|
||
|
{
|
||
|
hasProgramAttributes.IsRepeat = reader.GetBoolean(14);
|
||
|
}
|
||
10 years ago
|
}
|
||
|
|
||
8 years ago
|
var index = 15;
|
||
9 years ago
|
|
||
8 years ago
|
if (!reader.IsDBNull(index))
|
||
9 years ago
|
{
|
||
8 years ago
|
item.CommunityRating = reader.GetFloat(index);
|
||
9 years ago
|
}
|
||
8 years ago
|
index++;
|
||
9 years ago
|
|
||
8 years ago
|
if (query.HasField(ItemFields.CustomRating))
|
||
9 years ago
|
{
|
||
8 years ago
|
if (!reader.IsDBNull(index))
|
||
|
{
|
||
|
item.CustomRating = reader.GetString(index);
|
||
|
}
|
||
|
index++;
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
if (!reader.IsDBNull(index))
|
||
9 years ago
|
{
|
||
8 years ago
|
item.IndexNumber = reader.GetInt32(index);
|
||
9 years ago
|
}
|
||
8 years ago
|
index++;
|
||
9 years ago
|
|
||
8 years ago
|
if (query.HasField(ItemFields.Settings))
|
||
9 years ago
|
{
|
||
8 years ago
|
if (!reader.IsDBNull(index))
|
||
|
{
|
||
|
item.IsLocked = reader.GetBoolean(index);
|
||
|
}
|
||
|
index++;
|
||
9 years ago
|
|
||
8 years ago
|
if (!reader.IsDBNull(index))
|
||
|
{
|
||
|
item.PreferredMetadataLanguage = reader.GetString(index);
|
||
|
}
|
||
|
index++;
|
||
|
|
||
|
if (!reader.IsDBNull(index))
|
||
|
{
|
||
|
item.PreferredMetadataCountryCode = reader.GetString(index);
|
||
|
}
|
||
|
index++;
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
if (!reader.IsDBNull(index))
|
||
9 years ago
|
{
|
||
8 years ago
|
item.IsHD = reader.GetBoolean(index);
|
||
9 years ago
|
}
|
||
8 years ago
|
index++;
|
||
9 years ago
|
|
||
8 years ago
|
if (!reader.IsDBNull(index))
|
||
9 years ago
|
{
|
||
8 years ago
|
item.ExternalEtag = reader.GetString(index);
|
||
9 years ago
|
}
|
||
8 years ago
|
index++;
|
||
9 years ago
|
|
||
8 years ago
|
if (!reader.IsDBNull(index))
|
||
9 years ago
|
{
|
||
8 years ago
|
item.DateLastRefreshed = reader.GetDateTime(index).ToUniversalTime();
|
||
9 years ago
|
}
|
||
8 years ago
|
index++;
|
||
9 years ago
|
|
||
8 years ago
|
if (!reader.IsDBNull(index))
|
||
9 years ago
|
{
|
||
8 years ago
|
item.Name = reader.GetString(index);
|
||
9 years ago
|
}
|
||
8 years ago
|
index++;
|
||
9 years ago
|
|
||
8 years ago
|
if (!reader.IsDBNull(index))
|
||
9 years ago
|
{
|
||
8 years ago
|
item.Path = reader.GetString(index);
|
||
9 years ago
|
}
|
||
8 years ago
|
index++;
|
||
9 years ago
|
|
||
8 years ago
|
if (!reader.IsDBNull(index))
|
||
9 years ago
|
{
|
||
8 years ago
|
item.PremiereDate = reader.GetDateTime(index).ToUniversalTime();
|
||
9 years ago
|
}
|
||
8 years ago
|
index++;
|
||
9 years ago
|
|
||
8 years ago
|
if (query.HasField(ItemFields.Overview))
|
||
9 years ago
|
{
|
||
8 years ago
|
if (!reader.IsDBNull(index))
|
||
|
{
|
||
|
item.Overview = reader.GetString(index);
|
||
|
}
|
||
|
index++;
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
if (!reader.IsDBNull(index))
|
||
9 years ago
|
{
|
||
8 years ago
|
item.ParentIndexNumber = reader.GetInt32(index);
|
||
9 years ago
|
}
|
||
8 years ago
|
index++;
|
||
9 years ago
|
|
||
8 years ago
|
if (!reader.IsDBNull(index))
|
||
9 years ago
|
{
|
||
8 years ago
|
item.ProductionYear = reader.GetInt32(index);
|
||
9 years ago
|
}
|
||
8 years ago
|
index++;
|
||
9 years ago
|
|
||
8 years ago
|
if (!reader.IsDBNull(index))
|
||
9 years ago
|
{
|
||
8 years ago
|
item.OfficialRating = reader.GetString(index);
|
||
9 years ago
|
}
|
||
8 years ago
|
index++;
|
||
9 years ago
|
|
||
8 years ago
|
if (query.HasField(ItemFields.OfficialRatingDescription))
|
||
9 years ago
|
{
|
||
8 years ago
|
if (!reader.IsDBNull(index))
|
||
|
{
|
||
|
item.OfficialRatingDescription = reader.GetString(index);
|
||
|
}
|
||
|
index++;
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
if (query.HasField(ItemFields.HomePageUrl))
|
||
9 years ago
|
{
|
||
8 years ago
|
if (!reader.IsDBNull(index))
|
||
|
{
|
||
|
item.HomePageUrl = reader.GetString(index);
|
||
|
}
|
||
|
index++;
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
if (query.HasField(ItemFields.DisplayMediaType))
|
||
9 years ago
|
{
|
||
8 years ago
|
if (!reader.IsDBNull(index))
|
||
|
{
|
||
|
item.DisplayMediaType = reader.GetString(index);
|
||
|
}
|
||
|
index++;
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
if (query.HasField(ItemFields.SortName))
|
||
9 years ago
|
{
|
||
8 years ago
|
if (!reader.IsDBNull(index))
|
||
|
{
|
||
|
item.ForcedSortName = reader.GetString(index);
|
||
|
}
|
||
|
index++;
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
if (!reader.IsDBNull(index))
|
||
9 years ago
|
{
|
||
8 years ago
|
item.RunTimeTicks = reader.GetInt64(index);
|
||
9 years ago
|
}
|
||
8 years ago
|
index++;
|
||
9 years ago
|
|
||
8 years ago
|
if (query.HasField(ItemFields.VoteCount))
|
||
9 years ago
|
{
|
||
8 years ago
|
if (!reader.IsDBNull(index))
|
||
|
{
|
||
|
item.VoteCount = reader.GetInt32(index);
|
||
|
}
|
||
|
index++;
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
if (query.HasField(ItemFields.DateCreated))
|
||
9 years ago
|
{
|
||
8 years ago
|
if (!reader.IsDBNull(index))
|
||
|
{
|
||
|
item.DateCreated = reader.GetDateTime(index).ToUniversalTime();
|
||
|
}
|
||
|
index++;
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
if (!reader.IsDBNull(index))
|
||
9 years ago
|
{
|
||
8 years ago
|
item.DateModified = reader.GetDateTime(index).ToUniversalTime();
|
||
9 years ago
|
}
|
||
8 years ago
|
index++;
|
||
9 years ago
|
|
||
8 years ago
|
item.Id = reader.GetGuid(index);
|
||
|
index++;
|
||
9 years ago
|
|
||
8 years ago
|
if (query.HasField(ItemFields.Genres))
|
||
9 years ago
|
{
|
||
8 years ago
|
if (!reader.IsDBNull(index))
|
||
|
{
|
||
|
item.Genres = reader.GetString(index).Split('|').Where(i => !string.IsNullOrWhiteSpace(i)).ToList();
|
||
|
}
|
||
|
index++;
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
if (!reader.IsDBNull(index))
|
||
9 years ago
|
{
|
||
8 years ago
|
item.ParentId = reader.GetGuid(index);
|
||
9 years ago
|
}
|
||
8 years ago
|
index++;
|
||
9 years ago
|
|
||
8 years ago
|
if (!reader.IsDBNull(index))
|
||
9 years ago
|
{
|
||
8 years ago
|
item.Audio = (ProgramAudio)Enum.Parse(typeof(ProgramAudio), reader.GetString(index), true);
|
||
9 years ago
|
}
|
||
8 years ago
|
index++;
|
||
9 years ago
|
|
||
8 years ago
|
// TODO: Even if not needed by apps, the server needs it internally
|
||
|
// But get this excluded from contexts where it is not needed
|
||
|
if (!reader.IsDBNull(index))
|
||
9 years ago
|
{
|
||
8 years ago
|
item.ServiceName = reader.GetString(index);
|
||
9 years ago
|
}
|
||
8 years ago
|
index++;
|
||
9 years ago
|
|
||
8 years ago
|
if (!reader.IsDBNull(index))
|
||
9 years ago
|
{
|
||
8 years ago
|
item.IsInMixedFolder = reader.GetBoolean(index);
|
||
9 years ago
|
}
|
||
8 years ago
|
index++;
|
||
9 years ago
|
|
||
8 years ago
|
if (!reader.IsDBNull(index))
|
||
9 years ago
|
{
|
||
8 years ago
|
item.DateLastSaved = reader.GetDateTime(index).ToUniversalTime();
|
||
9 years ago
|
}
|
||
8 years ago
|
index++;
|
||
9 years ago
|
|
||
8 years ago
|
if (query.HasField(ItemFields.Settings))
|
||
9 years ago
|
{
|
||
8 years ago
|
if (!reader.IsDBNull(index))
|
||
|
{
|
||
|
item.LockedFields = reader.GetString(index).Split('|').Where(i => !string.IsNullOrWhiteSpace(i)).Select(i => (MetadataFields)Enum.Parse(typeof(MetadataFields), i, true)).ToList();
|
||
|
}
|
||
|
index++;
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
if (query.HasField(ItemFields.Studios))
|
||
9 years ago
|
{
|
||
8 years ago
|
if (!reader.IsDBNull(index))
|
||
|
{
|
||
|
item.Studios = reader.GetString(index).Split('|').Where(i => !string.IsNullOrWhiteSpace(i)).ToList();
|
||
|
}
|
||
|
index++;
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
if (query.HasField(ItemFields.Tags))
|
||
9 years ago
|
{
|
||
8 years ago
|
if (!reader.IsDBNull(index))
|
||
|
{
|
||
|
item.Tags = reader.GetString(index).Split('|').Where(i => !string.IsNullOrWhiteSpace(i)).ToList();
|
||
|
}
|
||
|
index++;
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
if (!reader.IsDBNull(index))
|
||
9 years ago
|
{
|
||
8 years ago
|
item.SourceType = (SourceType)Enum.Parse(typeof(SourceType), reader.GetString(index), true);
|
||
9 years ago
|
}
|
||
8 years ago
|
index++;
|
||
9 years ago
|
|
||
9 years ago
|
var trailer = item as Trailer;
|
||
|
if (trailer != null)
|
||
|
{
|
||
8 years ago
|
if (!reader.IsDBNull(index))
|
||
9 years ago
|
{
|
||
8 years ago
|
trailer.TrailerTypes = reader.GetString(index).Split('|').Where(i => !string.IsNullOrWhiteSpace(i)).Select(i => (TrailerType)Enum.Parse(typeof(TrailerType), i, true)).ToList();
|
||
9 years ago
|
}
|
||
|
}
|
||
8 years ago
|
index++;
|
||
9 years ago
|
|
||
8 years ago
|
if (query.HasField(ItemFields.OriginalTitle))
|
||
9 years ago
|
{
|
||
8 years ago
|
if (!reader.IsDBNull(index))
|
||
|
{
|
||
|
item.OriginalTitle = reader.GetString(index);
|
||
|
}
|
||
|
index++;
|
||
9 years ago
|
}
|
||
|
|
||
|
var video = item as Video;
|
||
|
if (video != null)
|
||
|
{
|
||
9 years ago
|
if (!reader.IsDBNull(index))
|
||
9 years ago
|
{
|
||
9 years ago
|
video.PrimaryVersionId = reader.GetString(index);
|
||
9 years ago
|
}
|
||
|
}
|
||
9 years ago
|
index++;
|
||
9 years ago
|
|
||
8 years ago
|
if (query.HasField(ItemFields.DateLastMediaAdded))
|
||
9 years ago
|
{
|
||
8 years ago
|
var folder = item as Folder;
|
||
|
if (folder != null && !reader.IsDBNull(index))
|
||
|
{
|
||
|
folder.DateLastMediaAdded = reader.GetDateTime(index).ToUniversalTime();
|
||
|
}
|
||
|
index++;
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
if (!reader.IsDBNull(index))
|
||
9 years ago
|
{
|
||
9 years ago
|
item.Album = reader.GetString(index);
|
||
9 years ago
|
}
|
||
9 years ago
|
index++;
|
||
9 years ago
|
|
||
9 years ago
|
if (!reader.IsDBNull(index))
|
||
9 years ago
|
{
|
||
9 years ago
|
item.CriticRating = reader.GetFloat(index);
|
||
9 years ago
|
}
|
||
9 years ago
|
index++;
|
||
9 years ago
|
|
||
8 years ago
|
if (query.HasField(ItemFields.CriticRatingSummary))
|
||
9 years ago
|
{
|
||
8 years ago
|
if (!reader.IsDBNull(index))
|
||
|
{
|
||
|
item.CriticRatingSummary = reader.GetString(index);
|
||
|
}
|
||
|
index++;
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
if (!reader.IsDBNull(index))
|
||
9 years ago
|
{
|
||
9 years ago
|
item.IsVirtualItem = reader.GetBoolean(index);
|
||
9 years ago
|
}
|
||
9 years ago
|
index++;
|
||
9 years ago
|
|
||
9 years ago
|
var hasSeries = item as IHasSeries;
|
||
|
if (hasSeries != null)
|
||
|
{
|
||
9 years ago
|
if (!reader.IsDBNull(index))
|
||
9 years ago
|
{
|
||
9 years ago
|
hasSeries.SeriesName = reader.GetString(index);
|
||
9 years ago
|
}
|
||
|
}
|
||
9 years ago
|
index++;
|
||
9 years ago
|
|
||
|
var episode = item as Episode;
|
||
|
if (episode != null)
|
||
|
{
|
||
9 years ago
|
if (!reader.IsDBNull(index))
|
||
9 years ago
|
{
|
||
9 years ago
|
episode.SeasonName = reader.GetString(index);
|
||
9 years ago
|
}
|
||
9 years ago
|
index++;
|
||
|
if (!reader.IsDBNull(index))
|
||
9 years ago
|
{
|
||
9 years ago
|
episode.SeasonId = reader.GetGuid(index);
|
||
9 years ago
|
}
|
||
9 years ago
|
}
|
||
9 years ago
|
else
|
||
|
{
|
||
|
index++;
|
||
|
}
|
||
|
index++;
|
||
9 years ago
|
|
||
9 years ago
|
if (hasSeries != null)
|
||
|
{
|
||
9 years ago
|
if (!reader.IsDBNull(index))
|
||
9 years ago
|
{
|
||
9 years ago
|
hasSeries.SeriesId = reader.GetGuid(index);
|
||
9 years ago
|
}
|
||
|
}
|
||
9 years ago
|
index++;
|
||
9 years ago
|
|
||
9 years ago
|
if (hasSeries != null)
|
||
|
{
|
||
9 years ago
|
if (!reader.IsDBNull(index))
|
||
9 years ago
|
{
|
||
9 years ago
|
hasSeries.SeriesSortName = reader.GetString(index);
|
||
9 years ago
|
}
|
||
|
}
|
||
9 years ago
|
index++;
|
||
9 years ago
|
|
||
9 years ago
|
if (!reader.IsDBNull(index))
|
||
|
{
|
||
|
item.PresentationUniqueKey = reader.GetString(index);
|
||
|
}
|
||
|
index++;
|
||
|
|
||
8 years ago
|
if (!reader.IsDBNull(index))
|
||
|
{
|
||
|
item.InheritedParentalRatingValue = reader.GetInt32(index);
|
||
|
}
|
||
|
index++;
|
||
|
|
||
8 years ago
|
if (!reader.IsDBNull(index))
|
||
|
{
|
||
|
item.InheritedTags = reader.GetString(index).Split('|').Where(i => !string.IsNullOrWhiteSpace(i)).ToList();
|
||
|
}
|
||
|
index++;
|
||
|
|
||
8 years ago
|
if (!reader.IsDBNull(index))
|
||
|
{
|
||
|
item.ExternalSeriesId = reader.GetString(index);
|
||
|
}
|
||
|
index++;
|
||
|
|
||
8 years ago
|
if (query.HasField(ItemFields.ShortOverview))
|
||
|
{
|
||
|
if (!reader.IsDBNull(index))
|
||
|
{
|
||
|
item.ShortOverview = reader.GetString(index);
|
||
|
}
|
||
|
index++;
|
||
|
}
|
||
|
|
||
|
if (query.HasField(ItemFields.Taglines))
|
||
|
{
|
||
|
if (!reader.IsDBNull(index))
|
||
|
{
|
||
|
item.Tagline = reader.GetString(index);
|
||
|
}
|
||
|
index++;
|
||
|
}
|
||
|
|
||
|
if (query.HasField(ItemFields.Keywords))
|
||
|
{
|
||
|
if (!reader.IsDBNull(index))
|
||
|
{
|
||
|
item.Keywords = reader.GetString(index).Split('|').Where(i => !string.IsNullOrWhiteSpace(i)).ToList();
|
||
|
}
|
||
|
index++;
|
||
|
}
|
||
|
|
||
|
if (!reader.IsDBNull(index))
|
||
|
{
|
||
|
DeserializeProviderIds(reader.GetString(index), item);
|
||
|
}
|
||
|
index++;
|
||
|
|
||
8 years ago
|
if (query.DtoOptions.EnableImages)
|
||
8 years ago
|
{
|
||
8 years ago
|
if (!reader.IsDBNull(index))
|
||
|
{
|
||
|
DeserializeImages(reader.GetString(index), item);
|
||
|
}
|
||
|
index++;
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
if (query.HasField(ItemFields.ProductionLocations))
|
||
|
{
|
||
|
if (!reader.IsDBNull(index))
|
||
|
{
|
||
|
item.ProductionLocations = reader.GetString(index).Split('|').Where(i => !string.IsNullOrWhiteSpace(i)).ToList();
|
||
|
}
|
||
|
index++;
|
||
|
}
|
||
|
|
||
8 years ago
|
if (query.HasField(ItemFields.ThemeSongIds))
|
||
8 years ago
|
{
|
||
8 years ago
|
if (!reader.IsDBNull(index))
|
||
|
{
|
||
|
item.ThemeSongIds = reader.GetString(index).Split('|').Where(i => !string.IsNullOrWhiteSpace(i)).Select(i => new Guid(i)).ToList();
|
||
|
}
|
||
|
index++;
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
if (query.HasField(ItemFields.ThemeVideoIds))
|
||
8 years ago
|
{
|
||
8 years ago
|
if (!reader.IsDBNull(index))
|
||
|
{
|
||
|
item.ThemeVideoIds = reader.GetString(index).Split('|').Where(i => !string.IsNullOrWhiteSpace(i)).Select(i => new Guid(i)).ToList();
|
||
|
}
|
||
|
index++;
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
if (!reader.IsDBNull(index))
|
||
|
{
|
||
|
item.TotalBitrate = reader.GetInt32(index);
|
||
|
}
|
||
|
index++;
|
||
|
|
||
|
if (!reader.IsDBNull(index))
|
||
|
{
|
||
|
item.ExtraType = (ExtraType)Enum.Parse(typeof(ExtraType), reader.GetString(index), true);
|
||
|
}
|
||
|
index++;
|
||
|
|
||
|
var hasArtists = item as IHasArtist;
|
||
|
if (hasArtists != null && !reader.IsDBNull(index))
|
||
|
{
|
||
|
hasArtists.Artists = reader.GetString(index).Split('|').Where(i => !string.IsNullOrWhiteSpace(i)).ToList();
|
||
|
}
|
||
|
index++;
|
||
|
|
||
|
var hasAlbumArtists = item as IHasAlbumArtist;
|
||
|
if (hasAlbumArtists != null && !reader.IsDBNull(index))
|
||
|
{
|
||
|
hasAlbumArtists.AlbumArtists = reader.GetString(index).Split('|').Where(i => !string.IsNullOrWhiteSpace(i)).ToList();
|
||
|
}
|
||
|
index++;
|
||
|
|
||
8 years ago
|
if (string.IsNullOrWhiteSpace(item.Tagline))
|
||
|
{
|
||
|
var movie = item as Movie;
|
||
|
if (movie != null && movie.Taglines.Count > 0)
|
||
|
{
|
||
|
movie.Tagline = movie.Taglines[0];
|
||
|
}
|
||
|
}
|
||
|
|
||
8 years ago
|
if (type == typeof(Person) && item.ProductionLocations.Count == 0)
|
||
|
{
|
||
|
var person = (Person)item;
|
||
|
if (!string.IsNullOrWhiteSpace(person.PlaceOfBirth))
|
||
|
{
|
||
|
item.ProductionLocations = new List<string> { person.PlaceOfBirth };
|
||
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
return item;
|
||
12 years ago
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// Gets the critic reviews.
|
||
|
/// </summary>
|
||
|
/// <param name="itemId">The item id.</param>
|
||
|
/// <returns>Task{IEnumerable{ItemReview}}.</returns>
|
||
11 years ago
|
public IEnumerable<ItemReview> GetCriticReviews(Guid itemId)
|
||
12 years ago
|
{
|
||
11 years ago
|
try
|
||
12 years ago
|
{
|
||
11 years ago
|
var path = Path.Combine(_criticReviewsPath, itemId + ".json");
|
||
12 years ago
|
|
||
11 years ago
|
return _jsonSerializer.DeserializeFromFile<List<ItemReview>>(path);
|
||
|
}
|
||
|
catch (DirectoryNotFoundException)
|
||
|
{
|
||
|
return new List<ItemReview>();
|
||
|
}
|
||
|
catch (FileNotFoundException)
|
||
|
{
|
||
|
return new List<ItemReview>();
|
||
|
}
|
||
12 years ago
|
}
|
||
|
|
||
11 years ago
|
private readonly Task _cachedTask = Task.FromResult(true);
|
||
12 years ago
|
/// <summary>
|
||
|
/// Saves the critic reviews.
|
||
|
/// </summary>
|
||
|
/// <param name="itemId">The item id.</param>
|
||
|
/// <param name="criticReviews">The critic reviews.</param>
|
||
|
/// <returns>Task.</returns>
|
||
|
public Task SaveCriticReviews(Guid itemId, IEnumerable<ItemReview> criticReviews)
|
||
|
{
|
||
9 years ago
|
Directory.CreateDirectory(_criticReviewsPath);
|
||
12 years ago
|
|
||
11 years ago
|
var path = Path.Combine(_criticReviewsPath, itemId + ".json");
|
||
|
|
||
|
_jsonSerializer.SerializeToFile(criticReviews.ToList(), path);
|
||
12 years ago
|
|
||
11 years ago
|
return _cachedTask;
|
||
12 years ago
|
}
|
||
12 years ago
|
|
||
|
/// <summary>
|
||
|
/// Gets chapters for an item
|
||
|
/// </summary>
|
||
|
/// <param name="id">The id.</param>
|
||
|
/// <returns>IEnumerable{ChapterInfo}.</returns>
|
||
|
/// <exception cref="System.ArgumentNullException">id</exception>
|
||
|
public IEnumerable<ChapterInfo> GetChapters(Guid id)
|
||
|
{
|
||
10 years ago
|
CheckDisposed();
|
||
9 years ago
|
if (id == Guid.Empty)
|
||
|
{
|
||
|
throw new ArgumentNullException("id");
|
||
|
}
|
||
9 years ago
|
var list = new List<ChapterInfo>();
|
||
9 years ago
|
|
||
9 years ago
|
using (var cmd = _connection.CreateCommand())
|
||
9 years ago
|
{
|
||
9 years ago
|
cmd.CommandText = "select StartPositionTicks,Name,ImagePath,ImageDateModified from " + ChaptersTableName + " where ItemId = @ItemId order by ChapterIndex asc";
|
||
9 years ago
|
|
||
9 years ago
|
cmd.Parameters.Add(cmd, "@ItemId", DbType.Guid).Value = id;
|
||
9 years ago
|
|
||
9 years ago
|
using (var reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess | CommandBehavior.SingleResult))
|
||
|
{
|
||
|
while (reader.Read())
|
||
9 years ago
|
{
|
||
9 years ago
|
list.Add(GetChapter(reader));
|
||
9 years ago
|
}
|
||
|
}
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
|
return list;
|
||
12 years ago
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// Gets a single chapter for an item
|
||
|
/// </summary>
|
||
|
/// <param name="id">The id.</param>
|
||
|
/// <param name="index">The index.</param>
|
||
|
/// <returns>ChapterInfo.</returns>
|
||
|
/// <exception cref="System.ArgumentNullException">id</exception>
|
||
|
public ChapterInfo GetChapter(Guid id, int index)
|
||
|
{
|
||
10 years ago
|
CheckDisposed();
|
||
9 years ago
|
if (id == Guid.Empty)
|
||
|
{
|
||
|
throw new ArgumentNullException("id");
|
||
|
}
|
||
|
|
||
9 years ago
|
using (var cmd = _connection.CreateCommand())
|
||
9 years ago
|
{
|
||
9 years ago
|
cmd.CommandText = "select StartPositionTicks,Name,ImagePath,ImageDateModified from " + ChaptersTableName + " where ItemId = @ItemId and ChapterIndex=@ChapterIndex";
|
||
9 years ago
|
|
||
9 years ago
|
cmd.Parameters.Add(cmd, "@ItemId", DbType.Guid).Value = id;
|
||
|
cmd.Parameters.Add(cmd, "@ChapterIndex", DbType.Int32).Value = index;
|
||
9 years ago
|
|
||
9 years ago
|
using (var reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess | CommandBehavior.SingleResult | CommandBehavior.SingleRow))
|
||
|
{
|
||
|
if (reader.Read())
|
||
9 years ago
|
{
|
||
9 years ago
|
return GetChapter(reader);
|
||
9 years ago
|
}
|
||
|
}
|
||
9 years ago
|
return null;
|
||
9 years ago
|
}
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// Gets the chapter.
|
||
|
/// </summary>
|
||
|
/// <param name="reader">The reader.</param>
|
||
|
/// <returns>ChapterInfo.</returns>
|
||
|
private ChapterInfo GetChapter(IDataReader reader)
|
||
|
{
|
||
|
var chapter = new ChapterInfo
|
||
|
{
|
||
|
StartPositionTicks = reader.GetInt64(0)
|
||
|
};
|
||
|
|
||
|
if (!reader.IsDBNull(1))
|
||
|
{
|
||
|
chapter.Name = reader.GetString(1);
|
||
|
}
|
||
|
|
||
|
if (!reader.IsDBNull(2))
|
||
|
{
|
||
|
chapter.ImagePath = reader.GetString(2);
|
||
|
}
|
||
|
|
||
9 years ago
|
if (!reader.IsDBNull(3))
|
||
|
{
|
||
|
chapter.ImageDateModified = reader.GetDateTime(3).ToUniversalTime();
|
||
|
}
|
||
|
|
||
9 years ago
|
return chapter;
|
||
12 years ago
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// Saves the chapters.
|
||
|
/// </summary>
|
||
|
/// <param name="id">The id.</param>
|
||
|
/// <param name="chapters">The chapters.</param>
|
||
|
/// <param name="cancellationToken">The cancellation token.</param>
|
||
|
/// <returns>Task.</returns>
|
||
|
/// <exception cref="System.ArgumentNullException">
|
||
|
/// id
|
||
|
/// or
|
||
|
/// chapters
|
||
|
/// or
|
||
|
/// cancellationToken
|
||
|
/// </exception>
|
||
9 years ago
|
public async Task SaveChapters(Guid id, List<ChapterInfo> chapters, CancellationToken cancellationToken)
|
||
12 years ago
|
{
|
||
10 years ago
|
CheckDisposed();
|
||
9 years ago
|
|
||
|
if (id == Guid.Empty)
|
||
|
{
|
||
|
throw new ArgumentNullException("id");
|
||
|
}
|
||
|
|
||
|
if (chapters == null)
|
||
|
{
|
||
|
throw new ArgumentNullException("chapters");
|
||
|
}
|
||
|
|
||
|
cancellationToken.ThrowIfCancellationRequested();
|
||
|
|
||
9 years ago
|
await WriteLock.WaitAsync(cancellationToken).ConfigureAwait(false);
|
||
|
|
||
|
IDbTransaction transaction = null;
|
||
|
|
||
|
try
|
||
9 years ago
|
{
|
||
9 years ago
|
transaction = _connection.BeginTransaction();
|
||
9 years ago
|
|
||
9 years ago
|
// First delete chapters
|
||
|
_deleteChaptersCommand.GetParameter(0).Value = id;
|
||
|
|
||
|
_deleteChaptersCommand.Transaction = transaction;
|
||
|
|
||
|
_deleteChaptersCommand.ExecuteNonQuery();
|
||
|
|
||
|
var index = 0;
|
||
|
|
||
|
foreach (var chapter in chapters)
|
||
9 years ago
|
{
|
||
9 years ago
|
cancellationToken.ThrowIfCancellationRequested();
|
||
9 years ago
|
|
||
9 years ago
|
_saveChapterCommand.GetParameter(0).Value = id;
|
||
|
_saveChapterCommand.GetParameter(1).Value = index;
|
||
|
_saveChapterCommand.GetParameter(2).Value = chapter.StartPositionTicks;
|
||
|
_saveChapterCommand.GetParameter(3).Value = chapter.Name;
|
||
|
_saveChapterCommand.GetParameter(4).Value = chapter.ImagePath;
|
||
9 years ago
|
_saveChapterCommand.GetParameter(5).Value = chapter.ImageDateModified;
|
||
9 years ago
|
|
||
9 years ago
|
_saveChapterCommand.Transaction = transaction;
|
||
9 years ago
|
|
||
9 years ago
|
_saveChapterCommand.ExecuteNonQuery();
|
||
9 years ago
|
|
||
9 years ago
|
index++;
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
|
transaction.Commit();
|
||
|
}
|
||
|
catch (OperationCanceledException)
|
||
|
{
|
||
|
if (transaction != null)
|
||
9 years ago
|
{
|
||
9 years ago
|
transaction.Rollback();
|
||
|
}
|
||
|
|
||
|
throw;
|
||
|
}
|
||
|
catch (Exception e)
|
||
|
{
|
||
|
Logger.ErrorException("Failed to save chapters:", e);
|
||
9 years ago
|
|
||
9 years ago
|
if (transaction != null)
|
||
|
{
|
||
|
transaction.Rollback();
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
|
throw;
|
||
|
}
|
||
|
finally
|
||
|
{
|
||
|
if (transaction != null)
|
||
9 years ago
|
{
|
||
9 years ago
|
transaction.Dispose();
|
||
|
}
|
||
9 years ago
|
|
||
9 years ago
|
WriteLock.Release();
|
||
|
}
|
||
|
}
|
||
9 years ago
|
|
||
9 years ago
|
protected override void CloseConnection()
|
||
|
{
|
||
|
if (_connection != null)
|
||
|
{
|
||
|
if (_connection.IsOpen())
|
||
9 years ago
|
{
|
||
9 years ago
|
_connection.Close();
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
|
_connection.Dispose();
|
||
|
_connection = null;
|
||
9 years ago
|
}
|
||
12 years ago
|
}
|
||
|
|
||
9 years ago
|
private bool EnableJoinUserData(InternalItemsQuery query)
|
||
12 years ago
|
{
|
||
9 years ago
|
if (query.User == null)
|
||
9 years ago
|
{
|
||
|
return false;
|
||
|
}
|
||
10 years ago
|
|
||
9 years ago
|
if (query.SimilarTo != null && query.User != null)
|
||
9 years ago
|
{
|
||
|
return true;
|
||
|
}
|
||
|
|
||
8 years ago
|
var sortingFields = query.SortBy.ToList();
|
||
|
sortingFields.AddRange(query.OrderBy.Select(i => i.Item1));
|
||
|
|
||
|
if (sortingFields.Contains(ItemSortBy.IsFavoriteOrLiked, StringComparer.OrdinalIgnoreCase))
|
||
9 years ago
|
{
|
||
8 years ago
|
return true;
|
||
|
}
|
||
|
if (sortingFields.Contains(ItemSortBy.IsPlayed, StringComparer.OrdinalIgnoreCase))
|
||
|
{
|
||
|
return true;
|
||
|
}
|
||
|
if (sortingFields.Contains(ItemSortBy.IsUnplayed, StringComparer.OrdinalIgnoreCase))
|
||
|
{
|
||
|
return true;
|
||
|
}
|
||
|
if (sortingFields.Contains(ItemSortBy.PlayCount, StringComparer.OrdinalIgnoreCase))
|
||
|
{
|
||
|
return true;
|
||
|
}
|
||
|
if (sortingFields.Contains(ItemSortBy.DatePlayed, StringComparer.OrdinalIgnoreCase))
|
||
|
{
|
||
|
return true;
|
||
9 years ago
|
}
|
||
|
|
||
|
if (query.IsFavoriteOrLiked.HasValue)
|
||
|
{
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
if (query.IsFavorite.HasValue)
|
||
10 years ago
|
{
|
||
9 years ago
|
return true;
|
||
|
}
|
||
10 years ago
|
|
||
9 years ago
|
if (query.IsResumable.HasValue)
|
||
|
{
|
||
|
return true;
|
||
|
}
|
||
10 years ago
|
|
||
9 years ago
|
if (query.IsPlayed.HasValue)
|
||
|
{
|
||
|
return true;
|
||
|
}
|
||
10 years ago
|
|
||
9 years ago
|
if (query.IsLiked.HasValue)
|
||
|
{
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
return false;
|
||
|
}
|
||
|
|
||
8 years ago
|
private List<ItemFields> allFields = Enum.GetNames(typeof(ItemFields))
|
||
|
.Select(i => (ItemFields)Enum.Parse(typeof(ItemFields), i, true))
|
||
|
.ToList();
|
||
|
|
||
|
private IEnumerable<string> GetColumnNamesFromField(ItemFields field)
|
||
|
{
|
||
|
if (field == ItemFields.Settings)
|
||
|
{
|
||
|
return new[] { "IsLocked", "PreferredMetadataCountryCode", "PreferredMetadataLanguage", "LockedFields" };
|
||
|
}
|
||
|
if (field == ItemFields.ServiceName)
|
||
|
{
|
||
|
return new[] { "ExternalServiceId" };
|
||
|
}
|
||
|
if (field == ItemFields.SortName)
|
||
|
{
|
||
|
return new[] { "ForcedSortName" };
|
||
|
}
|
||
|
if (field == ItemFields.Taglines)
|
||
|
{
|
||
|
return new[] { "Tagline" };
|
||
|
}
|
||
|
|
||
|
return new[] { field.ToString() };
|
||
|
}
|
||
|
|
||
9 years ago
|
private string[] GetFinalColumnsToSelect(InternalItemsQuery query, string[] startColumns, IDbCommand cmd)
|
||
9 years ago
|
{
|
||
|
var list = startColumns.ToList();
|
||
|
|
||
8 years ago
|
foreach (var field in allFields)
|
||
|
{
|
||
|
if (!query.HasField(field))
|
||
|
{
|
||
|
foreach (var fieldToRemove in GetColumnNamesFromField(field).ToList())
|
||
|
{
|
||
|
list.Remove(fieldToRemove);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
8 years ago
|
if (!query.DtoOptions.EnableImages)
|
||
|
{
|
||
|
list.Remove("Images");
|
||
|
}
|
||
|
|
||
9 years ago
|
if (EnableJoinUserData(query))
|
||
|
{
|
||
9 years ago
|
list.Add("UserDataDb.UserData.UserId");
|
||
|
list.Add("UserDataDb.UserData.lastPlayedDate");
|
||
|
list.Add("UserDataDb.UserData.playbackPositionTicks");
|
||
|
list.Add("UserDataDb.UserData.playcount");
|
||
|
list.Add("UserDataDb.UserData.isFavorite");
|
||
|
list.Add("UserDataDb.UserData.played");
|
||
|
list.Add("UserDataDb.UserData.rating");
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
if (query.SimilarTo != null)
|
||
|
{
|
||
|
var item = query.SimilarTo;
|
||
|
|
||
|
var builder = new StringBuilder();
|
||
9 years ago
|
builder.Append("(");
|
||
|
|
||
|
builder.Append("((OfficialRating=@ItemOfficialRating) * 10)");
|
||
|
//builder.Append("+ ((ProductionYear=@ItemProductionYear) * 10)");
|
||
9 years ago
|
|
||
9 years ago
|
builder.Append("+(Select Case When Abs(COALESCE(ProductionYear, 0) - @ItemProductionYear) < 10 Then 2 Else 0 End )");
|
||
|
builder.Append("+(Select Case When Abs(COALESCE(ProductionYear, 0) - @ItemProductionYear) < 5 Then 2 Else 0 End )");
|
||
9 years ago
|
|
||
9 years ago
|
//// genres
|
||
9 years ago
|
builder.Append("+ ((Select count(CleanValue) from ItemValues where ItemId=Guid and Type=2 and CleanValue in (select CleanValue from itemvalues where ItemId=@SimilarItemId and type=2)) * 10)");
|
||
9 years ago
|
|
||
9 years ago
|
//// tags
|
||
9 years ago
|
builder.Append("+ ((Select count(CleanValue) from ItemValues where ItemId=Guid and Type=4 and CleanValue in (select CleanValue from itemvalues where ItemId=@SimilarItemId and type=4)) * 10)");
|
||
9 years ago
|
|
||
9 years ago
|
builder.Append("+ ((Select count(CleanValue) from ItemValues where ItemId=Guid and Type=5 and CleanValue in (select CleanValue from itemvalues where ItemId=@SimilarItemId and type=5)) * 10)");
|
||
9 years ago
|
|
||
9 years ago
|
builder.Append("+ ((Select count(CleanValue) from ItemValues where ItemId=Guid and Type=3 and CleanValue in (select CleanValue from itemvalues where ItemId=@SimilarItemId and type=3)) * 3)");
|
||
9 years ago
|
|
||
9 years ago
|
//builder.Append("+ ((Select count(Name) from People where ItemId=Guid and Name in (select Name from People where ItemId=@SimilarItemId)) * 3)");
|
||
9 years ago
|
|
||
9 years ago
|
////builder.Append("(select group_concat((Select Name from People where ItemId=Guid and Name in (Select Name from People where ItemId=@SimilarItemId)), '|'))");
|
||
9 years ago
|
|
||
9 years ago
|
builder.Append(") as SimilarityScore");
|
||
|
|
||
|
list.Add(builder.ToString());
|
||
|
cmd.Parameters.Add(cmd, "@ItemOfficialRating", DbType.String).Value = item.OfficialRating;
|
||
9 years ago
|
cmd.Parameters.Add(cmd, "@ItemProductionYear", DbType.Int32).Value = item.ProductionYear ?? 0;
|
||
9 years ago
|
cmd.Parameters.Add(cmd, "@SimilarItemId", DbType.Guid).Value = item.Id;
|
||
9 years ago
|
|
||
|
var excludeIds = query.ExcludeItemIds.ToList();
|
||
|
excludeIds.Add(item.Id.ToString("N"));
|
||
|
query.ExcludeItemIds = excludeIds.ToArray();
|
||
9 years ago
|
|
||
|
query.ExcludeProviderIds = item.ProviderIds;
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
return list.ToArray();
|
||
|
}
|
||
|
|
||
|
private string GetJoinUserDataText(InternalItemsQuery query)
|
||
|
{
|
||
|
if (!EnableJoinUserData(query))
|
||
|
{
|
||
|
return string.Empty;
|
||
10 years ago
|
}
|
||
9 years ago
|
|
||
8 years ago
|
return " left join UserDataDb.UserData on UserDataKey=UserDataDb.UserData.Key And (UserId=@UserId)";
|
||
10 years ago
|
}
|
||
|
|
||
9 years ago
|
private string GetGroupBy(InternalItemsQuery query)
|
||
|
{
|
||
|
var groups = new List<string>();
|
||
|
|
||
|
if (EnableGroupByPresentationUniqueKey(query))
|
||
|
{
|
||
|
groups.Add("PresentationUniqueKey");
|
||
|
}
|
||
|
|
||
|
if (groups.Count > 0)
|
||
|
{
|
||
|
return " Group by " + string.Join(",", groups.ToArray());
|
||
|
}
|
||
|
|
||
|
return string.Empty;
|
||
|
}
|
||
|
|
||
9 years ago
|
private string GetFromText(string alias = "A")
|
||
9 years ago
|
{
|
||
9 years ago
|
return " from TypedBaseItems " + alias;
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
public List<BaseItem> GetItemList(InternalItemsQuery query)
|
||
9 years ago
|
{
|
||
|
if (query == null)
|
||
|
{
|
||
|
throw new ArgumentNullException("query");
|
||
|
}
|
||
|
|
||
|
CheckDisposed();
|
||
|
|
||
9 years ago
|
var now = DateTime.UtcNow;
|
||
|
|
||
9 years ago
|
var list = new List<BaseItem>();
|
||
|
|
||
9 years ago
|
// 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;
|
||
|
}
|
||
|
|
||
9 years ago
|
using (var cmd = _connection.CreateCommand())
|
||
9 years ago
|
{
|
||
9 years ago
|
cmd.CommandText = "select " + string.Join(",", GetFinalColumnsToSelect(query, _retriveItemColumns, cmd)) + GetFromText();
|
||
9 years ago
|
cmd.CommandText += GetJoinUserDataText(query);
|
||
|
|
||
9 years ago
|
if (EnableJoinUserData(query))
|
||
|
{
|
||
9 years ago
|
cmd.Parameters.Add(cmd, "@UserId", DbType.Guid).Value = query.User.Id;
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
9 years ago
|
var whereClauses = GetWhereClauses(query, cmd);
|
||
9 years ago
|
|
||
9 years ago
|
var whereText = whereClauses.Count == 0 ?
|
||
|
string.Empty :
|
||
|
" where " + string.Join(" AND ", whereClauses.ToArray());
|
||
9 years ago
|
|
||
9 years ago
|
cmd.CommandText += whereText;
|
||
9 years ago
|
|
||
9 years ago
|
cmd.CommandText += GetGroupBy(query);
|
||
9 years ago
|
|
||
9 years ago
|
cmd.CommandText += GetOrderByText(query);
|
||
9 years ago
|
|
||
9 years ago
|
if (query.Limit.HasValue || query.StartIndex.HasValue)
|
||
|
{
|
||
9 years ago
|
var offset = query.StartIndex ?? 0;
|
||
9 years ago
|
|
||
9 years ago
|
if (query.Limit.HasValue || offset > 0)
|
||
|
{
|
||
|
cmd.CommandText += " LIMIT " + (query.Limit ?? int.MaxValue).ToString(CultureInfo.InvariantCulture);
|
||
|
}
|
||
9 years ago
|
|
||
9 years ago
|
if (offset > 0)
|
||
9 years ago
|
{
|
||
9 years ago
|
cmd.CommandText += " OFFSET " + offset.ToString(CultureInfo.InvariantCulture);
|
||
9 years ago
|
}
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
9 years ago
|
using (var reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess | CommandBehavior.SingleResult))
|
||
|
{
|
||
|
LogQueryTime("GetItemList", cmd, now);
|
||
9 years ago
|
|
||
9 years ago
|
while (reader.Read())
|
||
|
{
|
||
8 years ago
|
var item = GetItem(reader, query);
|
||
9 years ago
|
if (item != null)
|
||
9 years ago
|
{
|
||
9 years ago
|
list.Add(item);
|
||
9 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
9 years ago
|
|
||
9 years ago
|
// 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>();
|
||
|
|
||
|
foreach (var item in list)
|
||
|
{
|
||
|
AddItem(newList, item);
|
||
|
|
||
|
if (newList.Count >= limit)
|
||
|
{
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
list = newList;
|
||
|
}
|
||
|
|
||
9 years ago
|
return list;
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
private void AddItem(List<BaseItem> items, BaseItem newItem)
|
||
|
{
|
||
|
var providerIds = newItem.ProviderIds.ToList();
|
||
|
|
||
|
for (var i = 0; i < items.Count; i++)
|
||
|
{
|
||
|
var item = items[i];
|
||
|
|
||
|
foreach (var providerId in providerIds)
|
||
|
{
|
||
|
if (providerId.Key == MetadataProviders.TmdbCollection.ToString())
|
||
|
{
|
||
|
continue;
|
||
|
}
|
||
|
if (item.GetProviderId(providerId.Key) == providerId.Value)
|
||
|
{
|
||
|
if (newItem.SourceType == SourceType.Library)
|
||
|
{
|
||
|
items[i] = newItem;
|
||
|
}
|
||
|
return;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
items.Add(newItem);
|
||
|
}
|
||
|
|
||
9 years ago
|
private void LogQueryTime(string methodName, IDbCommand cmd, DateTime startDate)
|
||
|
{
|
||
|
var elapsed = (DateTime.UtcNow - startDate).TotalMilliseconds;
|
||
|
|
||
9 years ago
|
var slowThreshold = 1000;
|
||
|
|
||
|
#if DEBUG
|
||
9 years ago
|
slowThreshold = 50;
|
||
9 years ago
|
#endif
|
||
|
|
||
|
if (elapsed >= slowThreshold)
|
||
9 years ago
|
{
|
||
|
Logger.Debug("{2} query time (slow): {0}ms. Query: {1}",
|
||
|
Convert.ToInt32(elapsed),
|
||
|
cmd.CommandText,
|
||
|
methodName);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
9 years ago
|
//Logger.Debug("{2} query time: {0}ms. Query: {1}",
|
||
|
// Convert.ToInt32(elapsed),
|
||
|
// cmd.CommandText,
|
||
|
// methodName);
|
||
9 years ago
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
public QueryResult<BaseItem> GetItems(InternalItemsQuery query)
|
||
|
{
|
||
|
if (query == null)
|
||
|
{
|
||
|
throw new ArgumentNullException("query");
|
||
|
}
|
||
|
|
||
|
CheckDisposed();
|
||
|
|
||
9 years ago
|
if (!query.EnableTotalRecordCount || (!query.Limit.HasValue && (query.StartIndex ?? 0) == 0))
|
||
|
{
|
||
|
var list = GetItemList(query);
|
||
|
return new QueryResult<BaseItem>
|
||
|
{
|
||
|
Items = list.ToArray(),
|
||
|
TotalRecordCount = list.Count
|
||
|
};
|
||
|
}
|
||
|
|
||
9 years ago
|
var now = DateTime.UtcNow;
|
||
|
|
||
9 years ago
|
using (var cmd = _connection.CreateCommand())
|
||
10 years ago
|
{
|
||
9 years ago
|
cmd.CommandText = "select " + string.Join(",", GetFinalColumnsToSelect(query, _retriveItemColumns, cmd)) + GetFromText();
|
||
9 years ago
|
cmd.CommandText += GetJoinUserDataText(query);
|
||
|
|
||
9 years ago
|
if (EnableJoinUserData(query))
|
||
|
{
|
||
9 years ago
|
cmd.Parameters.Add(cmd, "@UserId", DbType.Guid).Value = query.User.Id;
|
||
9 years ago
|
}
|
||
10 years ago
|
|
||
9 years ago
|
var whereClauses = GetWhereClauses(query, cmd);
|
||
10 years ago
|
|
||
9 years ago
|
var whereTextWithoutPaging = whereClauses.Count == 0 ?
|
||
|
string.Empty :
|
||
|
" where " + string.Join(" AND ", whereClauses.ToArray());
|
||
10 years ago
|
|
||
9 years ago
|
var whereText = whereClauses.Count == 0 ?
|
||
|
string.Empty :
|
||
|
" where " + string.Join(" AND ", whereClauses.ToArray());
|
||
10 years ago
|
|
||
9 years ago
|
cmd.CommandText += whereText;
|
||
9 years ago
|
|
||
9 years ago
|
cmd.CommandText += GetGroupBy(query);
|
||
10 years ago
|
|
||
9 years ago
|
cmd.CommandText += GetOrderByText(query);
|
||
9 years ago
|
|
||
9 years ago
|
if (query.Limit.HasValue || query.StartIndex.HasValue)
|
||
|
{
|
||
9 years ago
|
var offset = query.StartIndex ?? 0;
|
||
9 years ago
|
|
||
9 years ago
|
if (query.Limit.HasValue || offset > 0)
|
||
|
{
|
||
|
cmd.CommandText += " LIMIT " + (query.Limit ?? int.MaxValue).ToString(CultureInfo.InvariantCulture);
|
||
|
}
|
||
9 years ago
|
|
||
9 years ago
|
if (offset > 0)
|
||
9 years ago
|
{
|
||
9 years ago
|
cmd.CommandText += " OFFSET " + offset.ToString(CultureInfo.InvariantCulture);
|
||
9 years ago
|
}
|
||
9 years ago
|
}
|
||
10 years ago
|
|
||
9 years ago
|
cmd.CommandText += ";";
|
||
|
|
||
|
var isReturningZeroItems = query.Limit.HasValue && query.Limit <= 0;
|
||
|
|
||
|
if (isReturningZeroItems)
|
||
|
{
|
||
|
cmd.CommandText = "";
|
||
|
}
|
||
|
|
||
9 years ago
|
if (EnableGroupByPresentationUniqueKey(query))
|
||
|
{
|
||
9 years ago
|
cmd.CommandText += " select count (distinct PresentationUniqueKey)" + GetFromText();
|
||
9 years ago
|
}
|
||
|
else
|
||
|
{
|
||
9 years ago
|
cmd.CommandText += " select count (guid)" + GetFromText();
|
||
9 years ago
|
}
|
||
10 years ago
|
|
||
9 years ago
|
cmd.CommandText += GetJoinUserDataText(query);
|
||
|
cmd.CommandText += whereTextWithoutPaging;
|
||
9 years ago
|
|
||
9 years ago
|
var list = new List<BaseItem>();
|
||
|
var count = 0;
|
||
10 years ago
|
|
||
9 years ago
|
using (var reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess))
|
||
|
{
|
||
|
LogQueryTime("GetItems", cmd, now);
|
||
9 years ago
|
|
||
9 years ago
|
if (isReturningZeroItems)
|
||
9 years ago
|
{
|
||
9 years ago
|
if (reader.Read())
|
||
10 years ago
|
{
|
||
9 years ago
|
count = reader.GetInt32(0);
|
||
10 years ago
|
}
|
||
10 years ago
|
}
|
||
9 years ago
|
else
|
||
10 years ago
|
{
|
||
9 years ago
|
while (reader.Read())
|
||
|
{
|
||
8 years ago
|
var item = GetItem(reader, query);
|
||
9 years ago
|
if (item != null)
|
||
|
{
|
||
|
list.Add(item);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (reader.NextResult() && reader.Read())
|
||
|
{
|
||
|
count = reader.GetInt32(0);
|
||
|
}
|
||
9 years ago
|
}
|
||
10 years ago
|
}
|
||
9 years ago
|
|
||
|
return new QueryResult<BaseItem>()
|
||
|
{
|
||
|
Items = list.ToArray(),
|
||
|
TotalRecordCount = count
|
||
|
};
|
||
10 years ago
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
private string GetOrderByText(InternalItemsQuery query)
|
||
|
{
|
||
8 years ago
|
var orderBy = query.OrderBy.ToList();
|
||
|
var enableOrderInversion = true;
|
||
|
|
||
|
if (orderBy.Count == 0)
|
||
|
{
|
||
|
orderBy.AddRange(query.SortBy.Select(i => new Tuple<string, SortOrder>(i, query.SortOrder)));
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
enableOrderInversion = false;
|
||
|
}
|
||
|
|
||
9 years ago
|
if (query.SimilarTo != null)
|
||
|
{
|
||
8 years ago
|
if (orderBy.Count == 0)
|
||
9 years ago
|
{
|
||
8 years ago
|
orderBy.Add(new Tuple<string, SortOrder>("SimilarityScore", SortOrder.Descending));
|
||
|
orderBy.Add(new Tuple<string, SortOrder>(ItemSortBy.Random, SortOrder.Ascending));
|
||
9 years ago
|
query.SortOrder = SortOrder.Descending;
|
||
8 years ago
|
enableOrderInversion = false;
|
||
9 years ago
|
}
|
||
|
}
|
||
|
|
||
8 years ago
|
query.OrderBy = orderBy;
|
||
|
|
||
|
if (orderBy.Count == 0)
|
||
10 years ago
|
{
|
||
|
return string.Empty;
|
||
|
}
|
||
|
|
||
8 years ago
|
return " ORDER BY " + string.Join(",", orderBy.Select(i =>
|
||
9 years ago
|
{
|
||
8 years ago
|
var columnMap = MapOrderByField(i.Item1, query);
|
||
|
var columnAscending = i.Item2 == SortOrder.Ascending;
|
||
|
if (columnMap.Item2 && enableOrderInversion)
|
||
9 years ago
|
{
|
||
|
columnAscending = !columnAscending;
|
||
|
}
|
||
|
|
||
|
var sortOrder = columnAscending ? "ASC" : "DESC";
|
||
|
|
||
|
return columnMap.Item1 + " " + sortOrder;
|
||
|
}).ToArray());
|
||
10 years ago
|
}
|
||
|
|
||
9 years ago
|
private Tuple<string, bool> MapOrderByField(string name, InternalItemsQuery query)
|
||
10 years ago
|
{
|
||
9 years ago
|
if (string.Equals(name, ItemSortBy.AirTime, StringComparison.OrdinalIgnoreCase))
|
||
9 years ago
|
{
|
||
|
// TODO
|
||
9 years ago
|
return new Tuple<string, bool>("SortName", false);
|
||
9 years ago
|
}
|
||
9 years ago
|
if (string.Equals(name, ItemSortBy.Runtime, StringComparison.OrdinalIgnoreCase))
|
||
|
{
|
||
9 years ago
|
return new Tuple<string, bool>("RuntimeTicks", false);
|
||
|
}
|
||
|
if (string.Equals(name, ItemSortBy.Random, StringComparison.OrdinalIgnoreCase))
|
||
|
{
|
||
|
return new Tuple<string, bool>("RANDOM()", false);
|
||
|
}
|
||
|
if (string.Equals(name, ItemSortBy.DatePlayed, StringComparison.OrdinalIgnoreCase))
|
||
|
{
|
||
|
return new Tuple<string, bool>("LastPlayedDate", false);
|
||
|
}
|
||
|
if (string.Equals(name, ItemSortBy.PlayCount, StringComparison.OrdinalIgnoreCase))
|
||
|
{
|
||
|
return new Tuple<string, bool>("PlayCount", false);
|
||
|
}
|
||
|
if (string.Equals(name, ItemSortBy.IsFavoriteOrLiked, StringComparison.OrdinalIgnoreCase))
|
||
|
{
|
||
|
return new Tuple<string, bool>("IsFavorite", true);
|
||
9 years ago
|
}
|
||
9 years ago
|
if (string.Equals(name, ItemSortBy.IsFolder, StringComparison.OrdinalIgnoreCase))
|
||
|
{
|
||
9 years ago
|
return new Tuple<string, bool>("IsFolder", true);
|
||
9 years ago
|
}
|
||
9 years ago
|
if (string.Equals(name, ItemSortBy.IsPlayed, StringComparison.OrdinalIgnoreCase))
|
||
|
{
|
||
|
return new Tuple<string, bool>("played", true);
|
||
|
}
|
||
|
if (string.Equals(name, ItemSortBy.IsUnplayed, StringComparison.OrdinalIgnoreCase))
|
||
9 years ago
|
{
|
||
9 years ago
|
return new Tuple<string, bool>("played", false);
|
||
9 years ago
|
}
|
||
9 years ago
|
if (string.Equals(name, ItemSortBy.DateLastContentAdded, StringComparison.OrdinalIgnoreCase))
|
||
|
{
|
||
|
return new Tuple<string, bool>("DateLastMediaAdded", false);
|
||
|
}
|
||
9 years ago
|
if (string.Equals(name, ItemSortBy.Artist, StringComparison.OrdinalIgnoreCase))
|
||
|
{
|
||
9 years ago
|
return new Tuple<string, bool>("(select CleanValue from itemvalues where ItemId=Guid and Type=0 LIMIT 1)", false);
|
||
9 years ago
|
}
|
||
|
if (string.Equals(name, ItemSortBy.AlbumArtist, StringComparison.OrdinalIgnoreCase))
|
||
|
{
|
||
9 years ago
|
return new Tuple<string, bool>("(select CleanValue from itemvalues where ItemId=Guid and Type=1 LIMIT 1)", false);
|
||
9 years ago
|
}
|
||
9 years ago
|
if (string.Equals(name, ItemSortBy.OfficialRating, StringComparison.OrdinalIgnoreCase))
|
||
|
{
|
||
9 years ago
|
return new Tuple<string, bool>("InheritedParentalRatingValue", false);
|
||
9 years ago
|
}
|
||
|
if (string.Equals(name, ItemSortBy.Studio, StringComparison.OrdinalIgnoreCase))
|
||
|
{
|
||
9 years ago
|
return new Tuple<string, bool>("(select CleanValue from itemvalues where ItemId=Guid and Type=3 LIMIT 1)", false);
|
||
9 years ago
|
}
|
||
9 years ago
|
if (string.Equals(name, ItemSortBy.SeriesDatePlayed, StringComparison.OrdinalIgnoreCase))
|
||
|
{
|
||
9 years ago
|
return new Tuple<string, bool>("(Select MAX(LastPlayedDate) from TypedBaseItems B" + GetJoinUserDataText(query) + " where B.Guid in (Select ItemId from AncestorIds where AncestorId in (select guid from typedbaseitems c where C.Type = 'MediaBrowser.Controller.Entities.TV.Series' And C.Guid in (Select AncestorId from AncestorIds where ItemId=A.Guid))))", false);
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
9 years ago
|
return new Tuple<string, bool>(name, false);
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
public List<Guid> GetItemIdsList(InternalItemsQuery query)
|
||
|
{
|
||
|
if (query == null)
|
||
|
{
|
||
|
throw new ArgumentNullException("query");
|
||
|
}
|
||
|
|
||
|
CheckDisposed();
|
||
|
|
||
9 years ago
|
var now = DateTime.UtcNow;
|
||
|
|
||
9 years ago
|
using (var cmd = _connection.CreateCommand())
|
||
10 years ago
|
{
|
||
9 years ago
|
cmd.CommandText = "select " + string.Join(",", GetFinalColumnsToSelect(query, new[] { "guid" }, cmd)) + GetFromText();
|
||
9 years ago
|
cmd.CommandText += GetJoinUserDataText(query);
|
||
|
|
||
9 years ago
|
if (EnableJoinUserData(query))
|
||
|
{
|
||
9 years ago
|
cmd.Parameters.Add(cmd, "@UserId", DbType.Guid).Value = query.User.Id;
|
||
9 years ago
|
}
|
||
10 years ago
|
|
||
9 years ago
|
var whereClauses = GetWhereClauses(query, cmd);
|
||
10 years ago
|
|
||
9 years ago
|
var whereText = whereClauses.Count == 0 ?
|
||
|
string.Empty :
|
||
|
" where " + string.Join(" AND ", whereClauses.ToArray());
|
||
10 years ago
|
|
||
9 years ago
|
cmd.CommandText += whereText;
|
||
10 years ago
|
|
||
9 years ago
|
cmd.CommandText += GetGroupBy(query);
|
||
9 years ago
|
|
||
9 years ago
|
cmd.CommandText += GetOrderByText(query);
|
||
10 years ago
|
|
||
9 years ago
|
if (query.Limit.HasValue || query.StartIndex.HasValue)
|
||
|
{
|
||
9 years ago
|
var offset = query.StartIndex ?? 0;
|
||
9 years ago
|
|
||
9 years ago
|
if (query.Limit.HasValue || offset > 0)
|
||
|
{
|
||
|
cmd.CommandText += " LIMIT " + (query.Limit ?? int.MaxValue).ToString(CultureInfo.InvariantCulture);
|
||
|
}
|
||
9 years ago
|
|
||
9 years ago
|
if (offset > 0)
|
||
9 years ago
|
{
|
||
9 years ago
|
cmd.CommandText += " OFFSET " + offset.ToString(CultureInfo.InvariantCulture);
|
||
9 years ago
|
}
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
9 years ago
|
var list = new List<Guid>();
|
||
9 years ago
|
|
||
9 years ago
|
using (var reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess | CommandBehavior.SingleResult))
|
||
|
{
|
||
|
LogQueryTime("GetItemIdsList", cmd, now);
|
||
9 years ago
|
|
||
9 years ago
|
while (reader.Read())
|
||
|
{
|
||
|
list.Add(reader.GetGuid(0));
|
||
10 years ago
|
}
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
|
return list;
|
||
10 years ago
|
}
|
||
|
}
|
||
|
|
||
9 years ago
|
public QueryResult<Tuple<Guid, string>> GetItemIdsWithPath(InternalItemsQuery query)
|
||
|
{
|
||
|
if (query == null)
|
||
|
{
|
||
|
throw new ArgumentNullException("query");
|
||
|
}
|
||
|
|
||
|
CheckDisposed();
|
||
|
|
||
9 years ago
|
using (var cmd = _connection.CreateCommand())
|
||
9 years ago
|
{
|
||
9 years ago
|
cmd.CommandText = "select guid,path from TypedBaseItems";
|
||
9 years ago
|
|
||
9 years ago
|
var whereClauses = GetWhereClauses(query, cmd);
|
||
9 years ago
|
|
||
9 years ago
|
var whereTextWithoutPaging = whereClauses.Count == 0 ?
|
||
|
string.Empty :
|
||
|
" where " + string.Join(" AND ", whereClauses.ToArray());
|
||
9 years ago
|
|
||
9 years ago
|
var whereText = whereClauses.Count == 0 ?
|
||
|
string.Empty :
|
||
|
" where " + string.Join(" AND ", whereClauses.ToArray());
|
||
9 years ago
|
|
||
9 years ago
|
cmd.CommandText += whereText;
|
||
9 years ago
|
|
||
9 years ago
|
cmd.CommandText += GetGroupBy(query);
|
||
9 years ago
|
|
||
9 years ago
|
cmd.CommandText += GetOrderByText(query);
|
||
9 years ago
|
|
||
9 years ago
|
if (query.Limit.HasValue || query.StartIndex.HasValue)
|
||
|
{
|
||
9 years ago
|
var offset = query.StartIndex ?? 0;
|
||
9 years ago
|
|
||
9 years ago
|
if (query.Limit.HasValue || offset > 0)
|
||
|
{
|
||
|
cmd.CommandText += " LIMIT " + (query.Limit ?? int.MaxValue).ToString(CultureInfo.InvariantCulture);
|
||
|
}
|
||
9 years ago
|
|
||
9 years ago
|
if (offset > 0)
|
||
9 years ago
|
{
|
||
9 years ago
|
cmd.CommandText += " OFFSET " + offset.ToString(CultureInfo.InvariantCulture);
|
||
9 years ago
|
}
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
9 years ago
|
cmd.CommandText += "; select count (guid) from TypedBaseItems" + whereTextWithoutPaging;
|
||
9 years ago
|
|
||
9 years ago
|
var list = new List<Tuple<Guid, string>>();
|
||
|
var count = 0;
|
||
9 years ago
|
|
||
9 years ago
|
Logger.Debug(cmd.CommandText);
|
||
9 years ago
|
|
||
9 years ago
|
using (var reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess))
|
||
|
{
|
||
|
while (reader.Read())
|
||
9 years ago
|
{
|
||
9 years ago
|
var id = reader.GetGuid(0);
|
||
|
string path = null;
|
||
9 years ago
|
|
||
9 years ago
|
if (!reader.IsDBNull(1))
|
||
9 years ago
|
{
|
||
9 years ago
|
path = reader.GetString(1);
|
||
9 years ago
|
}
|
||
9 years ago
|
list.Add(new Tuple<Guid, string>(id, path));
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
if (reader.NextResult() && reader.Read())
|
||
9 years ago
|
{
|
||
9 years ago
|
count = reader.GetInt32(0);
|
||
|
}
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
|
return new QueryResult<Tuple<Guid, string>>()
|
||
|
{
|
||
|
Items = list.ToArray(),
|
||
|
TotalRecordCount = count
|
||
|
};
|
||
9 years ago
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
public QueryResult<Guid> GetItemIds(InternalItemsQuery query)
|
||
|
{
|
||
|
if (query == null)
|
||
|
{
|
||
|
throw new ArgumentNullException("query");
|
||
|
}
|
||
|
|
||
|
CheckDisposed();
|
||
|
|
||
9 years ago
|
if (!query.EnableTotalRecordCount || (!query.Limit.HasValue && (query.StartIndex ?? 0) == 0))
|
||
|
{
|
||
|
var list = GetItemIdsList(query);
|
||
|
return new QueryResult<Guid>
|
||
|
{
|
||
|
Items = list.ToArray(),
|
||
|
TotalRecordCount = list.Count
|
||
|
};
|
||
|
}
|
||
|
|
||
9 years ago
|
var now = DateTime.UtcNow;
|
||
|
|
||
9 years ago
|
using (var cmd = _connection.CreateCommand())
|
||
10 years ago
|
{
|
||
9 years ago
|
cmd.CommandText = "select " + string.Join(",", GetFinalColumnsToSelect(query, new[] { "guid" }, cmd)) + GetFromText();
|
||
9 years ago
|
|
||
|
var whereClauses = GetWhereClauses(query, cmd);
|
||
|
cmd.CommandText += GetJoinUserDataText(query);
|
||
|
|
||
9 years ago
|
if (EnableJoinUserData(query))
|
||
|
{
|
||
9 years ago
|
cmd.Parameters.Add(cmd, "@UserId", DbType.Guid).Value = query.User.Id;
|
||
9 years ago
|
}
|
||
10 years ago
|
|
||
9 years ago
|
var whereText = whereClauses.Count == 0 ?
|
||
|
string.Empty :
|
||
|
" where " + string.Join(" AND ", whereClauses.ToArray());
|
||
9 years ago
|
|
||
9 years ago
|
cmd.CommandText += whereText;
|
||
10 years ago
|
|
||
9 years ago
|
cmd.CommandText += GetGroupBy(query);
|
||
9 years ago
|
|
||
9 years ago
|
cmd.CommandText += GetOrderByText(query);
|
||
9 years ago
|
|
||
9 years ago
|
if (query.Limit.HasValue || query.StartIndex.HasValue)
|
||
|
{
|
||
9 years ago
|
var offset = query.StartIndex ?? 0;
|
||
9 years ago
|
|
||
9 years ago
|
if (query.Limit.HasValue || offset > 0)
|
||
|
{
|
||
|
cmd.CommandText += " LIMIT " + (query.Limit ?? int.MaxValue).ToString(CultureInfo.InvariantCulture);
|
||
|
}
|
||
9 years ago
|
|
||
9 years ago
|
if (offset > 0)
|
||
9 years ago
|
{
|
||
9 years ago
|
cmd.CommandText += " OFFSET " + offset.ToString(CultureInfo.InvariantCulture);
|
||
9 years ago
|
}
|
||
9 years ago
|
}
|
||
10 years ago
|
|
||
9 years ago
|
if (EnableGroupByPresentationUniqueKey(query))
|
||
|
{
|
||
9 years ago
|
cmd.CommandText += "; select count (distinct PresentationUniqueKey)" + GetFromText();
|
||
9 years ago
|
}
|
||
|
else
|
||
|
{
|
||
9 years ago
|
cmd.CommandText += "; select count (guid)" + GetFromText();
|
||
9 years ago
|
}
|
||
10 years ago
|
|
||
9 years ago
|
cmd.CommandText += GetJoinUserDataText(query);
|
||
|
cmd.CommandText += whereText;
|
||
9 years ago
|
|
||
9 years ago
|
var list = new List<Guid>();
|
||
|
var count = 0;
|
||
10 years ago
|
|
||
9 years ago
|
using (var reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess))
|
||
|
{
|
||
|
LogQueryTime("GetItemIds", cmd, now);
|
||
9 years ago
|
|
||
9 years ago
|
while (reader.Read())
|
||
9 years ago
|
{
|
||
9 years ago
|
list.Add(reader.GetGuid(0));
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
if (reader.NextResult() && reader.Read())
|
||
10 years ago
|
{
|
||
9 years ago
|
count = reader.GetInt32(0);
|
||
10 years ago
|
}
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
|
return new QueryResult<Guid>()
|
||
|
{
|
||
|
Items = list.ToArray(),
|
||
|
TotalRecordCount = count
|
||
|
};
|
||
10 years ago
|
}
|
||
|
}
|
||
|
|
||
9 years ago
|
private List<string> GetWhereClauses(InternalItemsQuery query, IDbCommand cmd, string paramSuffix = "")
|
||
10 years ago
|
{
|
||
|
var whereClauses = new List<string>();
|
||
|
|
||
9 years ago
|
if (EnableJoinUserData(query))
|
||
|
{
|
||
9 years ago
|
//whereClauses.Add("(UserId is null or UserId=@UserId)");
|
||
9 years ago
|
}
|
||
10 years ago
|
if (query.IsCurrentSchema.HasValue)
|
||
|
{
|
||
|
if (query.IsCurrentSchema.Value)
|
||
|
{
|
||
|
whereClauses.Add("(SchemaVersion not null AND SchemaVersion=@SchemaVersion)");
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
whereClauses.Add("(SchemaVersion is null or SchemaVersion<>@SchemaVersion)");
|
||
|
}
|
||
|
cmd.Parameters.Add(cmd, "@SchemaVersion", DbType.Int32).Value = LatestSchemaVersion;
|
||
|
}
|
||
9 years ago
|
if (query.IsHD.HasValue)
|
||
|
{
|
||
|
whereClauses.Add("IsHD=@IsHD");
|
||
|
cmd.Parameters.Add(cmd, "@IsHD", DbType.Boolean).Value = query.IsHD;
|
||
|
}
|
||
|
if (query.IsLocked.HasValue)
|
||
|
{
|
||
|
whereClauses.Add("IsLocked=@IsLocked");
|
||
|
cmd.Parameters.Add(cmd, "@IsLocked", DbType.Boolean).Value = query.IsLocked;
|
||
|
}
|
||
9 years ago
|
if (query.IsOffline.HasValue)
|
||
|
{
|
||
|
whereClauses.Add("IsOffline=@IsOffline");
|
||
|
cmd.Parameters.Add(cmd, "@IsOffline", DbType.Boolean).Value = query.IsOffline;
|
||
|
}
|
||
8 years ago
|
|
||
|
var exclusiveProgramAttribtues = !(query.IsMovie ?? true) ||
|
||
|
!(query.IsSports ?? true) ||
|
||
|
!(query.IsKids ?? true) ||
|
||
|
!(query.IsNews ?? true) ||
|
||
|
!(query.IsSeries ?? true);
|
||
|
|
||
|
if (exclusiveProgramAttribtues)
|
||
10 years ago
|
{
|
||
8 years ago
|
if (query.IsMovie.HasValue)
|
||
|
{
|
||
|
var alternateTypes = new List<string>();
|
||
|
if (query.IncludeItemTypes.Length == 0 || query.IncludeItemTypes.Contains(typeof(Movie).Name))
|
||
|
{
|
||
|
alternateTypes.Add(typeof(Movie).FullName);
|
||
|
}
|
||
|
if (query.IncludeItemTypes.Length == 0 || query.IncludeItemTypes.Contains(typeof(Trailer).Name))
|
||
|
{
|
||
|
alternateTypes.Add(typeof(Trailer).FullName);
|
||
|
}
|
||
|
|
||
|
if (alternateTypes.Count == 0)
|
||
|
{
|
||
|
whereClauses.Add("IsMovie=@IsMovie");
|
||
|
cmd.Parameters.Add(cmd, "@IsMovie", DbType.Boolean).Value = query.IsMovie;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
whereClauses.Add("(IsMovie is null OR IsMovie=@IsMovie)");
|
||
|
cmd.Parameters.Add(cmd, "@IsMovie", DbType.Boolean).Value = query.IsMovie;
|
||
|
}
|
||
|
}
|
||
|
if (query.IsSeries.HasValue)
|
||
9 years ago
|
{
|
||
8 years ago
|
whereClauses.Add("IsSeries=@IsSeries");
|
||
|
cmd.Parameters.Add(cmd, "@IsSeries", DbType.Boolean).Value = query.IsSeries;
|
||
9 years ago
|
}
|
||
8 years ago
|
if (query.IsNews.HasValue)
|
||
9 years ago
|
{
|
||
8 years ago
|
whereClauses.Add("IsNews=@IsNews");
|
||
|
cmd.Parameters.Add(cmd, "@IsNews", DbType.Boolean).Value = query.IsNews;
|
||
9 years ago
|
}
|
||
8 years ago
|
if (query.IsKids.HasValue)
|
||
9 years ago
|
{
|
||
8 years ago
|
whereClauses.Add("IsKids=@IsKids");
|
||
|
cmd.Parameters.Add(cmd, "@IsKids", DbType.Boolean).Value = query.IsKids;
|
||
9 years ago
|
}
|
||
8 years ago
|
if (query.IsSports.HasValue)
|
||
9 years ago
|
{
|
||
8 years ago
|
whereClauses.Add("IsSports=@IsSports");
|
||
|
cmd.Parameters.Add(cmd, "@IsSports", DbType.Boolean).Value = query.IsSports;
|
||
9 years ago
|
}
|
||
10 years ago
|
}
|
||
8 years ago
|
else
|
||
10 years ago
|
{
|
||
8 years ago
|
var programAttribtues = new List<string>();
|
||
|
if (query.IsMovie ?? false)
|
||
|
{
|
||
|
var alternateTypes = new List<string>();
|
||
|
if (query.IncludeItemTypes.Length == 0 || query.IncludeItemTypes.Contains(typeof(Movie).Name))
|
||
|
{
|
||
|
alternateTypes.Add(typeof(Movie).FullName);
|
||
|
}
|
||
|
if (query.IncludeItemTypes.Length == 0 || query.IncludeItemTypes.Contains(typeof(Trailer).Name))
|
||
|
{
|
||
|
alternateTypes.Add(typeof(Trailer).FullName);
|
||
|
}
|
||
|
|
||
|
if (alternateTypes.Count == 0)
|
||
|
{
|
||
|
programAttribtues.Add("IsMovie=@IsMovie");
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
programAttribtues.Add("(IsMovie is null OR IsMovie=@IsMovie)");
|
||
|
}
|
||
|
|
||
|
cmd.Parameters.Add(cmd, "@IsMovie", DbType.Boolean).Value = true;
|
||
|
}
|
||
|
if (query.IsSports ?? false)
|
||
|
{
|
||
|
programAttribtues.Add("IsSports=@IsSports");
|
||
|
cmd.Parameters.Add(cmd, "@IsSports", DbType.Boolean).Value = true;
|
||
|
}
|
||
|
if (query.IsNews ?? false)
|
||
|
{
|
||
|
programAttribtues.Add("IsNews=@IsNews");
|
||
|
cmd.Parameters.Add(cmd, "@IsNews", DbType.Boolean).Value = true;
|
||
|
}
|
||
|
if (query.IsSeries ?? false)
|
||
|
{
|
||
|
programAttribtues.Add("IsSeries=@IsSeries");
|
||
|
cmd.Parameters.Add(cmd, "@IsSeries", DbType.Boolean).Value = true;
|
||
|
}
|
||
|
if (query.IsKids ?? false)
|
||
|
{
|
||
|
programAttribtues.Add("IsKids=@IsKids");
|
||
|
cmd.Parameters.Add(cmd, "@IsKids", DbType.Boolean).Value = true;
|
||
|
}
|
||
|
if (programAttribtues.Count > 0)
|
||
|
{
|
||
8 years ago
|
whereClauses.Add("(" + string.Join(" OR ", programAttribtues.ToArray()) + ")");
|
||
8 years ago
|
}
|
||
10 years ago
|
}
|
||
8 years ago
|
|
||
8 years ago
|
if (query.SimilarTo != null)
|
||
|
{
|
||
|
whereClauses.Add("SimilarityScore > 0");
|
||
|
}
|
||
|
|
||
9 years ago
|
if (query.IsFolder.HasValue)
|
||
|
{
|
||
|
whereClauses.Add("IsFolder=@IsFolder");
|
||
|
cmd.Parameters.Add(cmd, "@IsFolder", DbType.Boolean).Value = query.IsFolder;
|
||
|
}
|
||
10 years ago
|
|
||
9 years ago
|
var includeTypes = query.IncludeItemTypes.SelectMany(MapIncludeItemTypes).ToArray();
|
||
|
if (includeTypes.Length == 1)
|
||
|
{
|
||
9 years ago
|
whereClauses.Add("type=@type" + paramSuffix);
|
||
|
cmd.Parameters.Add(cmd, "@type" + paramSuffix, DbType.String).Value = includeTypes[0];
|
||
9 years ago
|
}
|
||
|
else if (includeTypes.Length > 1)
|
||
|
{
|
||
|
var inClause = string.Join(",", includeTypes.Select(i => "'" + i + "'").ToArray());
|
||
|
whereClauses.Add(string.Format("type in ({0})", inClause));
|
||
|
}
|
||
|
|
||
9 years ago
|
var excludeTypes = query.ExcludeItemTypes.SelectMany(MapIncludeItemTypes).ToArray();
|
||
|
if (excludeTypes.Length == 1)
|
||
|
{
|
||
|
whereClauses.Add("type<>@type");
|
||
|
cmd.Parameters.Add(cmd, "@type", DbType.String).Value = excludeTypes[0];
|
||
|
}
|
||
|
else if (excludeTypes.Length > 1)
|
||
|
{
|
||
|
var inClause = string.Join(",", excludeTypes.Select(i => "'" + i + "'").ToArray());
|
||
|
whereClauses.Add(string.Format("type not in ({0})", inClause));
|
||
|
}
|
||
|
|
||
10 years ago
|
if (query.ChannelIds.Length == 1)
|
||
|
{
|
||
|
whereClauses.Add("ChannelId=@ChannelId");
|
||
|
cmd.Parameters.Add(cmd, "@ChannelId", DbType.String).Value = query.ChannelIds[0];
|
||
|
}
|
||
|
if (query.ChannelIds.Length > 1)
|
||
|
{
|
||
|
var inClause = string.Join(",", query.ChannelIds.Select(i => "'" + i + "'").ToArray());
|
||
|
whereClauses.Add(string.Format("ChannelId in ({0})", inClause));
|
||
|
}
|
||
|
|
||
9 years ago
|
if (query.ParentId.HasValue)
|
||
|
{
|
||
|
whereClauses.Add("ParentId=@ParentId");
|
||
|
cmd.Parameters.Add(cmd, "@ParentId", DbType.Guid).Value = query.ParentId.Value;
|
||
|
}
|
||
|
|
||
9 years ago
|
if (!string.IsNullOrWhiteSpace(query.Path))
|
||
|
{
|
||
|
whereClauses.Add("Path=@Path");
|
||
|
cmd.Parameters.Add(cmd, "@Path", DbType.String).Value = query.Path;
|
||
|
}
|
||
|
|
||
9 years ago
|
if (!string.IsNullOrWhiteSpace(query.PresentationUniqueKey))
|
||
|
{
|
||
|
whereClauses.Add("PresentationUniqueKey=@PresentationUniqueKey");
|
||
|
cmd.Parameters.Add(cmd, "@PresentationUniqueKey", DbType.String).Value = query.PresentationUniqueKey;
|
||
|
}
|
||
|
|
||
9 years ago
|
if (query.MinCommunityRating.HasValue)
|
||
|
{
|
||
|
whereClauses.Add("CommunityRating>=@MinCommunityRating");
|
||
|
cmd.Parameters.Add(cmd, "@MinCommunityRating", DbType.Double).Value = query.MinCommunityRating.Value;
|
||
|
}
|
||
|
|
||
|
if (query.MinIndexNumber.HasValue)
|
||
|
{
|
||
|
whereClauses.Add("IndexNumber>=@MinIndexNumber");
|
||
|
cmd.Parameters.Add(cmd, "@MinIndexNumber", DbType.Int32).Value = query.MinIndexNumber.Value;
|
||
|
}
|
||
|
|
||
8 years ago
|
if (query.MinDateCreated.HasValue)
|
||
|
{
|
||
|
whereClauses.Add("DateCreated>=@MinDateCreated");
|
||
|
cmd.Parameters.Add(cmd, "@MinDateCreated", DbType.DateTime).Value = query.MinDateCreated.Value;
|
||
|
}
|
||
|
|
||
|
if (query.MinDateLastSaved.HasValue)
|
||
|
{
|
||
|
whereClauses.Add("DateLastSaved>=@MinDateLastSaved");
|
||
|
cmd.Parameters.Add(cmd, "@MinDateLastSaved", DbType.DateTime).Value = query.MinDateLastSaved.Value;
|
||
|
}
|
||
|
|
||
9 years ago
|
//if (query.MinPlayers.HasValue)
|
||
|
//{
|
||
|
// whereClauses.Add("Players>=@MinPlayers");
|
||
|
// cmd.Parameters.Add(cmd, "@MinPlayers", DbType.Int32).Value = query.MinPlayers.Value;
|
||
|
//}
|
||
|
|
||
|
//if (query.MaxPlayers.HasValue)
|
||
|
//{
|
||
|
// whereClauses.Add("Players<=@MaxPlayers");
|
||
|
// cmd.Parameters.Add(cmd, "@MaxPlayers", DbType.Int32).Value = query.MaxPlayers.Value;
|
||
|
//}
|
||
|
|
||
9 years ago
|
if (query.IndexNumber.HasValue)
|
||
|
{
|
||
|
whereClauses.Add("IndexNumber=@IndexNumber");
|
||
|
cmd.Parameters.Add(cmd, "@IndexNumber", DbType.Int32).Value = query.IndexNumber.Value;
|
||
|
}
|
||
9 years ago
|
if (query.ParentIndexNumber.HasValue)
|
||
|
{
|
||
9 years ago
|
whereClauses.Add("ParentIndexNumber=@ParentIndexNumber");
|
||
9 years ago
|
cmd.Parameters.Add(cmd, "@ParentIndexNumber", DbType.Int32).Value = query.ParentIndexNumber.Value;
|
||
|
}
|
||
9 years ago
|
if (query.ParentIndexNumberNotEquals.HasValue)
|
||
|
{
|
||
9 years ago
|
whereClauses.Add("(ParentIndexNumber<>@ParentIndexNumberNotEquals or ParentIndexNumber is null)");
|
||
|
cmd.Parameters.Add(cmd, "@ParentIndexNumberNotEquals", DbType.Int32).Value = query.ParentIndexNumberNotEquals.Value;
|
||
9 years ago
|
}
|
||
10 years ago
|
if (query.MinEndDate.HasValue)
|
||
|
{
|
||
|
whereClauses.Add("EndDate>=@MinEndDate");
|
||
|
cmd.Parameters.Add(cmd, "@MinEndDate", DbType.Date).Value = query.MinEndDate.Value;
|
||
|
}
|
||
|
|
||
|
if (query.MaxEndDate.HasValue)
|
||
|
{
|
||
|
whereClauses.Add("EndDate<=@MaxEndDate");
|
||
|
cmd.Parameters.Add(cmd, "@MaxEndDate", DbType.Date).Value = query.MaxEndDate.Value;
|
||
|
}
|
||
|
|
||
|
if (query.MinStartDate.HasValue)
|
||
|
{
|
||
|
whereClauses.Add("StartDate>=@MinStartDate");
|
||
|
cmd.Parameters.Add(cmd, "@MinStartDate", DbType.Date).Value = query.MinStartDate.Value;
|
||
|
}
|
||
|
|
||
9 years ago
|
if (query.MaxStartDate.HasValue)
|
||
|
{
|
||
|
whereClauses.Add("StartDate<=@MaxStartDate");
|
||
|
cmd.Parameters.Add(cmd, "@MaxStartDate", DbType.Date).Value = query.MaxStartDate.Value;
|
||
|
}
|
||
|
|
||
9 years ago
|
if (query.MinPremiereDate.HasValue)
|
||
|
{
|
||
|
whereClauses.Add("PremiereDate>=@MinPremiereDate");
|
||
|
cmd.Parameters.Add(cmd, "@MinPremiereDate", DbType.Date).Value = query.MinPremiereDate.Value;
|
||
|
}
|
||
9 years ago
|
if (query.MaxPremiereDate.HasValue)
|
||
10 years ago
|
{
|
||
9 years ago
|
whereClauses.Add("PremiereDate<=@MaxPremiereDate");
|
||
|
cmd.Parameters.Add(cmd, "@MaxPremiereDate", DbType.Date).Value = query.MaxPremiereDate.Value;
|
||
10 years ago
|
}
|
||
|
|
||
9 years ago
|
if (query.SourceTypes.Length == 1)
|
||
|
{
|
||
|
whereClauses.Add("SourceType=@SourceType");
|
||
|
cmd.Parameters.Add(cmd, "@SourceType", DbType.String).Value = query.SourceTypes[0];
|
||
|
}
|
||
|
else if (query.SourceTypes.Length > 1)
|
||
|
{
|
||
|
var inClause = string.Join(",", query.SourceTypes.Select(i => "'" + i + "'").ToArray());
|
||
|
whereClauses.Add(string.Format("SourceType in ({0})", inClause));
|
||
|
}
|
||
9 years ago
|
|
||
|
if (query.ExcludeSourceTypes.Length == 1)
|
||
|
{
|
||
|
whereClauses.Add("SourceType<>@SourceType");
|
||
|
cmd.Parameters.Add(cmd, "@SourceType", DbType.String).Value = query.SourceTypes[0];
|
||
|
}
|
||
|
else if (query.ExcludeSourceTypes.Length > 1)
|
||
|
{
|
||
|
var inClause = string.Join(",", query.ExcludeSourceTypes.Select(i => "'" + i + "'").ToArray());
|
||
|
whereClauses.Add(string.Format("SourceType not in ({0})", inClause));
|
||
|
}
|
||
9 years ago
|
|
||
|
if (query.TrailerTypes.Length > 0)
|
||
|
{
|
||
|
var clauses = new List<string>();
|
||
|
var index = 0;
|
||
|
foreach (var type in query.TrailerTypes)
|
||
|
{
|
||
|
clauses.Add("TrailerTypes like @TrailerTypes" + index);
|
||
|
cmd.Parameters.Add(cmd, "@TrailerTypes" + index, DbType.String).Value = "%" + type + "%";
|
||
|
index++;
|
||
|
}
|
||
|
var clause = "(" + string.Join(" OR ", clauses.ToArray()) + ")";
|
||
|
whereClauses.Add(clause);
|
||
|
}
|
||
|
|
||
10 years ago
|
if (query.IsAiring.HasValue)
|
||
|
{
|
||
|
if (query.IsAiring.Value)
|
||
|
{
|
||
|
whereClauses.Add("StartDate<=@MaxStartDate");
|
||
|
cmd.Parameters.Add(cmd, "@MaxStartDate", DbType.Date).Value = DateTime.UtcNow;
|
||
|
|
||
|
whereClauses.Add("EndDate>=@MinEndDate");
|
||
|
cmd.Parameters.Add(cmd, "@MinEndDate", DbType.Date).Value = DateTime.UtcNow;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
whereClauses.Add("(StartDate>@IsAiringDate OR EndDate < @IsAiringDate)");
|
||
|
cmd.Parameters.Add(cmd, "@IsAiringDate", DbType.Date).Value = DateTime.UtcNow;
|
||
10 years ago
|
}
|
||
10 years ago
|
}
|
||
|
|
||
9 years ago
|
if (query.PersonIds.Length > 0)
|
||
|
{
|
||
|
// Todo: improve without having to do this
|
||
|
query.Person = query.PersonIds.Select(i => RetrieveItem(new Guid(i))).Where(i => i != null).Select(i => i.Name).FirstOrDefault();
|
||
|
}
|
||
|
|
||
10 years ago
|
if (!string.IsNullOrWhiteSpace(query.Person))
|
||
|
{
|
||
|
whereClauses.Add("Guid in (select ItemId from People where Name=@PersonName)");
|
||
|
cmd.Parameters.Add(cmd, "@PersonName", DbType.String).Value = query.Person;
|
||
|
}
|
||
9 years ago
|
|
||
9 years ago
|
if (!string.IsNullOrWhiteSpace(query.SlugName))
|
||
|
{
|
||
9 years ago
|
whereClauses.Add("SlugName=@SlugName");
|
||
9 years ago
|
cmd.Parameters.Add(cmd, "@SlugName", DbType.String).Value = query.SlugName;
|
||
|
}
|
||
|
|
||
9 years ago
|
if (!string.IsNullOrWhiteSpace(query.MinSortName))
|
||
|
{
|
||
|
whereClauses.Add("SortName>=@MinSortName");
|
||
|
cmd.Parameters.Add(cmd, "@MinSortName", DbType.String).Value = query.MinSortName;
|
||
|
}
|
||
|
|
||
8 years ago
|
if (!string.IsNullOrWhiteSpace(query.ExternalSeriesId))
|
||
|
{
|
||
|
whereClauses.Add("ExternalSeriesId=@ExternalSeriesId");
|
||
|
cmd.Parameters.Add(cmd, "@ExternalSeriesId", DbType.String).Value = query.ExternalSeriesId;
|
||
|
}
|
||
|
|
||
9 years ago
|
if (!string.IsNullOrWhiteSpace(query.Name))
|
||
|
{
|
||
9 years ago
|
whereClauses.Add("CleanName=@Name");
|
||
9 years ago
|
cmd.Parameters.Add(cmd, "@Name", DbType.String).Value = GetCleanValue(query.Name);
|
||
9 years ago
|
}
|
||
10 years ago
|
|
||
10 years ago
|
if (!string.IsNullOrWhiteSpace(query.NameContains))
|
||
|
{
|
||
9 years ago
|
whereClauses.Add("CleanName like @NameContains");
|
||
9 years ago
|
cmd.Parameters.Add(cmd, "@NameContains", DbType.String).Value = "%" + GetCleanValue(query.NameContains) + "%";
|
||
10 years ago
|
}
|
||
9 years ago
|
if (!string.IsNullOrWhiteSpace(query.NameStartsWith))
|
||
|
{
|
||
9 years ago
|
whereClauses.Add("SortName like @NameStartsWith");
|
||
9 years ago
|
cmd.Parameters.Add(cmd, "@NameStartsWith", DbType.String).Value = query.NameStartsWith + "%";
|
||
|
}
|
||
|
if (!string.IsNullOrWhiteSpace(query.NameStartsWithOrGreater))
|
||
|
{
|
||
|
whereClauses.Add("SortName >= @NameStartsWithOrGreater");
|
||
|
// lowercase this because SortName is stored as lowercase
|
||
|
cmd.Parameters.Add(cmd, "@NameStartsWithOrGreater", DbType.String).Value = query.NameStartsWithOrGreater.ToLower();
|
||
|
}
|
||
|
if (!string.IsNullOrWhiteSpace(query.NameLessThan))
|
||
|
{
|
||
|
whereClauses.Add("SortName < @NameLessThan");
|
||
|
// lowercase this because SortName is stored as lowercase
|
||
|
cmd.Parameters.Add(cmd, "@NameLessThan", DbType.String).Value = query.NameLessThan.ToLower();
|
||
|
}
|
||
10 years ago
|
|
||
9 years ago
|
if (query.ImageTypes.Length > 0 && _config.Configuration.SchemaVersion >= 87)
|
||
|
{
|
||
|
var requiredImageIndex = 0;
|
||
|
|
||
|
foreach (var requiredImage in query.ImageTypes)
|
||
|
{
|
||
|
var paramName = "@RequiredImageType" + requiredImageIndex;
|
||
|
whereClauses.Add("(select path from images where ItemId=Guid and ImageType=" + paramName + " limit 1) not null");
|
||
|
cmd.Parameters.Add(cmd, paramName, DbType.Int32).Value = (int)requiredImage;
|
||
|
requiredImageIndex++;
|
||
|
}
|
||
|
}
|
||
|
|
||
9 years ago
|
if (query.IsLiked.HasValue)
|
||
|
{
|
||
|
if (query.IsLiked.Value)
|
||
|
{
|
||
|
whereClauses.Add("rating>=@UserRating");
|
||
|
cmd.Parameters.Add(cmd, "@UserRating", DbType.Double).Value = UserItemData.MinLikeValue;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
whereClauses.Add("(rating is null or rating<@UserRating)");
|
||
|
cmd.Parameters.Add(cmd, "@UserRating", DbType.Double).Value = UserItemData.MinLikeValue;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (query.IsFavoriteOrLiked.HasValue)
|
||
|
{
|
||
|
if (query.IsFavoriteOrLiked.Value)
|
||
|
{
|
||
9 years ago
|
whereClauses.Add("IsFavorite=@IsFavoriteOrLiked");
|
||
9 years ago
|
}
|
||
|
else
|
||
|
{
|
||
9 years ago
|
whereClauses.Add("(IsFavorite is null or IsFavorite=@IsFavoriteOrLiked)");
|
||
9 years ago
|
}
|
||
9 years ago
|
cmd.Parameters.Add(cmd, "@IsFavoriteOrLiked", DbType.Boolean).Value = query.IsFavoriteOrLiked.Value;
|
||
9 years ago
|
}
|
||
|
|
||
|
if (query.IsFavorite.HasValue)
|
||
|
{
|
||
9 years ago
|
if (query.IsFavorite.Value)
|
||
|
{
|
||
|
whereClauses.Add("IsFavorite=@IsFavorite");
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
whereClauses.Add("(IsFavorite is null or IsFavorite=@IsFavorite)");
|
||
|
}
|
||
9 years ago
|
cmd.Parameters.Add(cmd, "@IsFavorite", DbType.Boolean).Value = query.IsFavorite.Value;
|
||
|
}
|
||
|
|
||
9 years ago
|
if (EnableJoinUserData(query))
|
||
9 years ago
|
{
|
||
9 years ago
|
if (query.IsPlayed.HasValue)
|
||
9 years ago
|
{
|
||
9 years ago
|
if (query.IsPlayed.Value)
|
||
|
{
|
||
|
whereClauses.Add("(played=@IsPlayed)");
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
whereClauses.Add("(played is null or played=@IsPlayed)");
|
||
|
}
|
||
|
cmd.Parameters.Add(cmd, "@IsPlayed", DbType.Boolean).Value = query.IsPlayed.Value;
|
||
9 years ago
|
}
|
||
9 years ago
|
}
|
||
|
|
||
|
if (query.IsResumable.HasValue)
|
||
|
{
|
||
|
if (query.IsResumable.Value)
|
||
|
{
|
||
|
whereClauses.Add("playbackPositionTicks > 0");
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
whereClauses.Add("(playbackPositionTicks is null or playbackPositionTicks = 0)");
|
||
|
}
|
||
|
}
|
||
|
|
||
9 years ago
|
if (query.ArtistNames.Length > 0)
|
||
|
{
|
||
|
var clauses = new List<string>();
|
||
|
var index = 0;
|
||
|
foreach (var artist in query.ArtistNames)
|
||
|
{
|
||
9 years ago
|
clauses.Add("@ArtistName" + index + " in (select CleanValue from itemvalues where ItemId=Guid and Type <= 1)");
|
||
9 years ago
|
cmd.Parameters.Add(cmd, "@ArtistName" + index, DbType.String).Value = GetCleanValue(artist);
|
||
9 years ago
|
index++;
|
||
|
}
|
||
|
var clause = "(" + string.Join(" OR ", clauses.ToArray()) + ")";
|
||
|
whereClauses.Add(clause);
|
||
|
}
|
||
9 years ago
|
|
||
9 years ago
|
if (query.ExcludeArtistIds.Length > 0)
|
||
9 years ago
|
{
|
||
|
var clauses = new List<string>();
|
||
|
var index = 0;
|
||
9 years ago
|
foreach (var artistId in query.ExcludeArtistIds)
|
||
9 years ago
|
{
|
||
9 years ago
|
var artistItem = RetrieveItem(new Guid(artistId));
|
||
|
if (artistItem != null)
|
||
|
{
|
||
|
clauses.Add("@ExcludeArtistName" + index + " not in (select CleanValue from itemvalues where ItemId=Guid and Type <= 1)");
|
||
9 years ago
|
cmd.Parameters.Add(cmd, "@ExcludeArtistName" + index, DbType.String).Value = GetCleanValue(artistItem.Name);
|
||
9 years ago
|
index++;
|
||
|
}
|
||
9 years ago
|
}
|
||
|
var clause = "(" + string.Join(" AND ", clauses.ToArray()) + ")";
|
||
|
whereClauses.Add(clause);
|
||
|
}
|
||
9 years ago
|
|
||
9 years ago
|
if (query.GenreIds.Length > 0)
|
||
|
{
|
||
|
// Todo: improve without having to do this
|
||
|
query.Genres = query.GenreIds.Select(i => RetrieveItem(new Guid(i))).Where(i => i != null).Select(i => i.Name).ToArray();
|
||
|
}
|
||
|
|
||
10 years ago
|
if (query.Genres.Length > 0)
|
||
|
{
|
||
9 years ago
|
var clauses = new List<string>();
|
||
|
var index = 0;
|
||
|
foreach (var item in query.Genres)
|
||
|
{
|
||
9 years ago
|
clauses.Add("@Genre" + index + " in (select CleanValue from itemvalues where ItemId=Guid and Type=2)");
|
||
9 years ago
|
cmd.Parameters.Add(cmd, "@Genre" + index, DbType.String).Value = GetCleanValue(item);
|
||
9 years ago
|
index++;
|
||
|
}
|
||
|
var clause = "(" + string.Join(" OR ", clauses.ToArray()) + ")";
|
||
|
whereClauses.Add(clause);
|
||
|
}
|
||
|
|
||
|
if (query.Tags.Length > 0)
|
||
|
{
|
||
|
var clauses = new List<string>();
|
||
|
var index = 0;
|
||
|
foreach (var item in query.Tags)
|
||
|
{
|
||
9 years ago
|
clauses.Add("@Tag" + index + " in (select CleanValue from itemvalues where ItemId=Guid and Type=4)");
|
||
9 years ago
|
cmd.Parameters.Add(cmd, "@Tag" + index, DbType.String).Value = GetCleanValue(item);
|
||
9 years ago
|
index++;
|
||
|
}
|
||
|
var clause = "(" + string.Join(" OR ", clauses.ToArray()) + ")";
|
||
|
whereClauses.Add(clause);
|
||
|
}
|
||
|
|
||
9 years ago
|
if (query.StudioIds.Length > 0)
|
||
|
{
|
||
|
// Todo: improve without having to do this
|
||
|
query.Studios = query.StudioIds.Select(i => RetrieveItem(new Guid(i))).Where(i => i != null).Select(i => i.Name).ToArray();
|
||
|
}
|
||
|
|
||
9 years ago
|
if (query.Studios.Length > 0)
|
||
|
{
|
||
|
var clauses = new List<string>();
|
||
10 years ago
|
var index = 0;
|
||
9 years ago
|
foreach (var item in query.Studios)
|
||
10 years ago
|
{
|
||
9 years ago
|
clauses.Add("@Studio" + index + " in (select CleanValue from itemvalues where ItemId=Guid and Type=3)");
|
||
9 years ago
|
cmd.Parameters.Add(cmd, "@Studio" + index, DbType.String).Value = GetCleanValue(item);
|
||
9 years ago
|
index++;
|
||
|
}
|
||
|
var clause = "(" + string.Join(" OR ", clauses.ToArray()) + ")";
|
||
|
whereClauses.Add(clause);
|
||
|
}
|
||
|
|
||
|
if (query.Keywords.Length > 0)
|
||
|
{
|
||
|
var clauses = new List<string>();
|
||
|
var index = 0;
|
||
|
foreach (var item in query.Keywords)
|
||
|
{
|
||
9 years ago
|
clauses.Add("@Keyword" + index + " in (select CleanValue from itemvalues where ItemId=Guid and Type=5)");
|
||
9 years ago
|
cmd.Parameters.Add(cmd, "@Keyword" + index, DbType.String).Value = GetCleanValue(item);
|
||
10 years ago
|
index++;
|
||
|
}
|
||
9 years ago
|
var clause = "(" + string.Join(" OR ", clauses.ToArray()) + ")";
|
||
|
whereClauses.Add(clause);
|
||
10 years ago
|
}
|
||
|
|
||
9 years ago
|
if (query.OfficialRatings.Length > 0)
|
||
|
{
|
||
|
var clauses = new List<string>();
|
||
|
var index = 0;
|
||
|
foreach (var item in query.OfficialRatings)
|
||
|
{
|
||
|
clauses.Add("OfficialRating=@OfficialRating" + index);
|
||
|
cmd.Parameters.Add(cmd, "@OfficialRating" + index, DbType.String).Value = item;
|
||
|
index++;
|
||
|
}
|
||
|
var clause = "(" + string.Join(" OR ", clauses.ToArray()) + ")";
|
||
|
whereClauses.Add(clause);
|
||
|
}
|
||
|
|
||
9 years ago
|
if (query.MinParentalRating.HasValue)
|
||
|
{
|
||
|
whereClauses.Add("InheritedParentalRatingValue<=@MinParentalRating");
|
||
|
cmd.Parameters.Add(cmd, "@MinParentalRating", DbType.Int32).Value = query.MinParentalRating.Value;
|
||
|
}
|
||
|
|
||
10 years ago
|
if (query.MaxParentalRating.HasValue)
|
||
|
{
|
||
9 years ago
|
whereClauses.Add("InheritedParentalRatingValue<=@MaxParentalRating");
|
||
10 years ago
|
cmd.Parameters.Add(cmd, "@MaxParentalRating", DbType.Int32).Value = query.MaxParentalRating.Value;
|
||
|
}
|
||
|
|
||
|
if (query.HasParentalRating.HasValue)
|
||
|
{
|
||
|
if (query.HasParentalRating.Value)
|
||
|
{
|
||
9 years ago
|
whereClauses.Add("InheritedParentalRatingValue > 0");
|
||
10 years ago
|
}
|
||
|
else
|
||
|
{
|
||
9 years ago
|
whereClauses.Add("InheritedParentalRatingValue = 0");
|
||
10 years ago
|
}
|
||
|
}
|
||
|
|
||
9 years ago
|
if (query.HasOverview.HasValue)
|
||
|
{
|
||
|
if (query.HasOverview.Value)
|
||
|
{
|
||
|
whereClauses.Add("(Overview not null AND Overview<>'')");
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
whereClauses.Add("(Overview is null OR Overview='')");
|
||
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
if (query.HasDeadParentId.HasValue)
|
||
|
{
|
||
|
if (query.HasDeadParentId.Value)
|
||
|
{
|
||
|
whereClauses.Add("ParentId NOT NULL AND ParentId NOT IN (select guid from TypedBaseItems)");
|
||
|
}
|
||
|
}
|
||
9 years ago
|
|
||
|
if (query.Years.Length == 1)
|
||
|
{
|
||
|
whereClauses.Add("ProductionYear=@Years");
|
||
|
cmd.Parameters.Add(cmd, "@Years", DbType.Int32).Value = query.Years[0].ToString();
|
||
|
}
|
||
|
else if (query.Years.Length > 1)
|
||
|
{
|
||
|
var val = string.Join(",", query.Years.ToArray());
|
||
|
|
||
|
whereClauses.Add("ProductionYear in (" + val + ")");
|
||
|
}
|
||
|
|
||
|
if (query.LocationTypes.Length == 1)
|
||
|
{
|
||
9 years ago
|
if (query.LocationTypes[0] == LocationType.Virtual && _config.Configuration.SchemaVersion >= 90)
|
||
|
{
|
||
|
query.IsVirtualItem = true;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
whereClauses.Add("LocationType=@LocationType");
|
||
|
cmd.Parameters.Add(cmd, "@LocationType", DbType.String).Value = query.LocationTypes[0].ToString();
|
||
|
}
|
||
9 years ago
|
}
|
||
|
else if (query.LocationTypes.Length > 1)
|
||
|
{
|
||
|
var val = string.Join(",", query.LocationTypes.Select(i => "'" + i + "'").ToArray());
|
||
|
|
||
|
whereClauses.Add("LocationType in (" + val + ")");
|
||
|
}
|
||
9 years ago
|
if (query.ExcludeLocationTypes.Length == 1)
|
||
|
{
|
||
9 years ago
|
if (query.ExcludeLocationTypes[0] == LocationType.Virtual && _config.Configuration.SchemaVersion >= 90)
|
||
|
{
|
||
|
query.IsVirtualItem = false;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
whereClauses.Add("LocationType<>@ExcludeLocationTypes");
|
||
|
cmd.Parameters.Add(cmd, "@ExcludeLocationTypes", DbType.String).Value = query.ExcludeLocationTypes[0].ToString();
|
||
|
}
|
||
9 years ago
|
}
|
||
9 years ago
|
else if (query.ExcludeLocationTypes.Length > 1)
|
||
9 years ago
|
{
|
||
|
var val = string.Join(",", query.ExcludeLocationTypes.Select(i => "'" + i + "'").ToArray());
|
||
|
|
||
|
whereClauses.Add("LocationType not in (" + val + ")");
|
||
|
}
|
||
9 years ago
|
if (query.IsVirtualItem.HasValue)
|
||
|
{
|
||
9 years ago
|
if (_config.Configuration.SchemaVersion >= 90)
|
||
|
{
|
||
|
whereClauses.Add("IsVirtualItem=@IsVirtualItem");
|
||
|
cmd.Parameters.Add(cmd, "@IsVirtualItem", DbType.Boolean).Value = query.IsVirtualItem.Value;
|
||
|
}
|
||
|
else if (!query.IsVirtualItem.Value)
|
||
|
{
|
||
|
whereClauses.Add("LocationType<>'Virtual'");
|
||
|
}
|
||
9 years ago
|
}
|
||
9 years ago
|
if (query.IsSpecialSeason.HasValue)
|
||
|
{
|
||
|
if (query.IsSpecialSeason.Value)
|
||
|
{
|
||
|
whereClauses.Add("IndexNumber = 0");
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
whereClauses.Add("IndexNumber <> 0");
|
||
|
}
|
||
|
}
|
||
9 years ago
|
if (query.IsUnaired.HasValue)
|
||
|
{
|
||
|
if (query.IsUnaired.Value)
|
||
|
{
|
||
|
whereClauses.Add("PremiereDate >= DATETIME('now')");
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
whereClauses.Add("PremiereDate < DATETIME('now')");
|
||
|
}
|
||
|
}
|
||
|
if (query.IsMissing.HasValue && _config.Configuration.SchemaVersion >= 90)
|
||
|
{
|
||
|
if (query.IsMissing.Value)
|
||
|
{
|
||
|
whereClauses.Add("(IsVirtualItem=1 AND PremiereDate < DATETIME('now'))");
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
whereClauses.Add("(IsVirtualItem=0 OR PremiereDate >= DATETIME('now'))");
|
||
|
}
|
||
|
}
|
||
|
if (query.IsVirtualUnaired.HasValue && _config.Configuration.SchemaVersion >= 90)
|
||
|
{
|
||
|
if (query.IsVirtualUnaired.Value)
|
||
|
{
|
||
|
whereClauses.Add("(IsVirtualItem=1 AND PremiereDate >= DATETIME('now'))");
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
whereClauses.Add("(IsVirtualItem=0 OR PremiereDate < DATETIME('now'))");
|
||
|
}
|
||
|
}
|
||
9 years ago
|
if (query.MediaTypes.Length == 1)
|
||
|
{
|
||
|
whereClauses.Add("MediaType=@MediaTypes");
|
||
9 years ago
|
cmd.Parameters.Add(cmd, "@MediaTypes", DbType.String).Value = query.MediaTypes[0];
|
||
9 years ago
|
}
|
||
|
if (query.MediaTypes.Length > 1)
|
||
|
{
|
||
|
var val = string.Join(",", query.MediaTypes.Select(i => "'" + i + "'").ToArray());
|
||
|
|
||
|
whereClauses.Add("MediaType in (" + val + ")");
|
||
|
}
|
||
9 years ago
|
if (query.ItemIds.Length > 0)
|
||
|
{
|
||
9 years ago
|
var includeIds = new List<string>();
|
||
9 years ago
|
|
||
|
var index = 0;
|
||
|
foreach (var id in query.ItemIds)
|
||
|
{
|
||
9 years ago
|
includeIds.Add("Guid = @IncludeId" + index);
|
||
9 years ago
|
cmd.Parameters.Add(cmd, "@IncludeId" + index, DbType.Guid).Value = new Guid(id);
|
||
|
index++;
|
||
|
}
|
||
|
|
||
9 years ago
|
whereClauses.Add(string.Join(" OR ", includeIds.ToArray()));
|
||
9 years ago
|
}
|
||
9 years ago
|
if (query.ExcludeItemIds.Length > 0)
|
||
|
{
|
||
|
var excludeIds = new List<string>();
|
||
|
|
||
|
var index = 0;
|
||
|
foreach (var id in query.ExcludeItemIds)
|
||
|
{
|
||
|
excludeIds.Add("Guid <> @ExcludeId" + index);
|
||
|
cmd.Parameters.Add(cmd, "@ExcludeId" + index, DbType.Guid).Value = new Guid(id);
|
||
|
index++;
|
||
|
}
|
||
|
|
||
|
whereClauses.Add(string.Join(" AND ", excludeIds.ToArray()));
|
||
|
}
|
||
10 years ago
|
|
||
9 years ago
|
if (query.ExcludeProviderIds.Count > 0)
|
||
|
{
|
||
|
var excludeIds = new List<string>();
|
||
|
|
||
|
var index = 0;
|
||
|
foreach (var pair in query.ExcludeProviderIds)
|
||
|
{
|
||
9 years ago
|
if (string.Equals(pair.Key, MetadataProviders.TmdbCollection.ToString(), StringComparison.OrdinalIgnoreCase))
|
||
|
{
|
||
|
continue;
|
||
|
}
|
||
|
|
||
9 years ago
|
var paramName = "@ExcludeProviderId" + index;
|
||
9 years ago
|
excludeIds.Add("(COALESCE((select value from ProviderIds where ItemId=Guid and Name = '" + pair.Key + "'), '') <> " + paramName + ")");
|
||
9 years ago
|
cmd.Parameters.Add(cmd, paramName, DbType.String).Value = pair.Value;
|
||
|
index++;
|
||
|
}
|
||
|
|
||
|
whereClauses.Add(string.Join(" AND ", excludeIds.ToArray()));
|
||
|
}
|
||
|
|
||
|
if (query.HasImdbId.HasValue)
|
||
|
{
|
||
9 years ago
|
var fn = query.HasImdbId.Value ? "<>" : "=";
|
||
|
whereClauses.Add("(COALESCE((select value from ProviderIds where ItemId=Guid and Name = 'Imdb'), '') " + fn + " '')");
|
||
9 years ago
|
}
|
||
|
|
||
|
if (query.HasTmdbId.HasValue)
|
||
|
{
|
||
9 years ago
|
var fn = query.HasTmdbId.Value ? "<>" : "=";
|
||
|
whereClauses.Add("(COALESCE((select value from ProviderIds where ItemId=Guid and Name = 'Tmdb'), '') " + fn + " '')");
|
||
9 years ago
|
}
|
||
|
|
||
|
if (query.HasTvdbId.HasValue)
|
||
|
{
|
||
9 years ago
|
var fn = query.HasTvdbId.Value ? "<>" : "=";
|
||
|
whereClauses.Add("(COALESCE((select value from ProviderIds where ItemId=Guid and Name = 'Tvdb'), '') " + fn + " '')");
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
if (query.AlbumNames.Length > 0)
|
||
|
{
|
||
|
var clause = "(";
|
||
|
|
||
|
var index = 0;
|
||
|
foreach (var name in query.AlbumNames)
|
||
|
{
|
||
|
if (index > 0)
|
||
|
{
|
||
|
clause += " OR ";
|
||
|
}
|
||
|
clause += "Album=@AlbumName" + index;
|
||
|
cmd.Parameters.Add(cmd, "@AlbumName" + index, DbType.String).Value = name;
|
||
9 years ago
|
index++;
|
||
9 years ago
|
}
|
||
|
|
||
|
clause += ")";
|
||
|
whereClauses.Add(clause);
|
||
|
}
|
||
8 years ago
|
if (query.HasThemeSong.HasValue)
|
||
|
{
|
||
|
if (query.HasThemeSong.Value)
|
||
|
{
|
||
|
whereClauses.Add("ThemeSongIds not null");
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
whereClauses.Add("ThemeSongIds is null");
|
||
|
}
|
||
|
}
|
||
|
if (query.HasThemeVideo.HasValue)
|
||
|
{
|
||
|
if (query.HasThemeVideo.Value)
|
||
|
{
|
||
|
whereClauses.Add("ThemeVideoIds not null");
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
whereClauses.Add("ThemeVideoIds is null");
|
||
|
}
|
||
|
}
|
||
9 years ago
|
|
||
9 years ago
|
//var enableItemsByName = query.IncludeItemsByName ?? query.IncludeItemTypes.Length > 0;
|
||
|
var enableItemsByName = query.IncludeItemsByName ?? false;
|
||
9 years ago
|
|
||
9 years ago
|
if (query.TopParentIds.Length == 1)
|
||
|
{
|
||
9 years ago
|
if (enableItemsByName)
|
||
|
{
|
||
|
whereClauses.Add("(TopParentId=@TopParentId or IsItemByName=@IsItemByName)");
|
||
|
cmd.Parameters.Add(cmd, "@IsItemByName", DbType.Boolean).Value = true;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
whereClauses.Add("(TopParentId=@TopParentId)");
|
||
|
}
|
||
9 years ago
|
cmd.Parameters.Add(cmd, "@TopParentId", DbType.String).Value = query.TopParentIds[0];
|
||
|
}
|
||
|
if (query.TopParentIds.Length > 1)
|
||
|
{
|
||
|
var val = string.Join(",", query.TopParentIds.Select(i => "'" + i + "'").ToArray());
|
||
9 years ago
|
|
||
9 years ago
|
if (enableItemsByName)
|
||
|
{
|
||
|
whereClauses.Add("(IsItemByName=@IsItemByName or TopParentId in (" + val + "))");
|
||
|
cmd.Parameters.Add(cmd, "@IsItemByName", DbType.Boolean).Value = true;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
whereClauses.Add("(TopParentId in (" + val + "))");
|
||
|
}
|
||
9 years ago
|
}
|
||
|
|
||
|
if (query.AncestorIds.Length == 1)
|
||
|
{
|
||
|
whereClauses.Add("Guid in (select itemId from AncestorIds where AncestorId=@AncestorId)");
|
||
|
cmd.Parameters.Add(cmd, "@AncestorId", DbType.Guid).Value = new Guid(query.AncestorIds[0]);
|
||
|
}
|
||
|
if (query.AncestorIds.Length > 1)
|
||
|
{
|
||
|
var inClause = string.Join(",", query.AncestorIds.Select(i => "'" + new Guid(i).ToString("N") + "'").ToArray());
|
||
|
whereClauses.Add(string.Format("Guid in (select itemId from AncestorIds where AncestorIdText in ({0}))", inClause));
|
||
|
}
|
||
9 years ago
|
if (!string.IsNullOrWhiteSpace(query.AncestorWithPresentationUniqueKey))
|
||
|
{
|
||
|
var inClause = "select guid from TypedBaseItems where PresentationUniqueKey=@AncestorWithPresentationUniqueKey";
|
||
|
whereClauses.Add(string.Format("Guid in (select itemId from AncestorIds where AncestorId in ({0}))", inClause));
|
||
|
cmd.Parameters.Add(cmd, "@AncestorWithPresentationUniqueKey", DbType.String).Value = query.AncestorWithPresentationUniqueKey;
|
||
|
}
|
||
9 years ago
|
|
||
|
if (query.BlockUnratedItems.Length == 1)
|
||
|
{
|
||
|
whereClauses.Add("(InheritedParentalRatingValue > 0 or UnratedType <> @UnratedType)");
|
||
|
cmd.Parameters.Add(cmd, "@UnratedType", DbType.String).Value = query.BlockUnratedItems[0].ToString();
|
||
|
}
|
||
|
if (query.BlockUnratedItems.Length > 1)
|
||
|
{
|
||
|
var inClause = string.Join(",", query.BlockUnratedItems.Select(i => "'" + i.ToString() + "'").ToArray());
|
||
|
whereClauses.Add(string.Format("(InheritedParentalRatingValue > 0 or UnratedType not in ({0}))", inClause));
|
||
|
}
|
||
|
|
||
9 years ago
|
var excludeTagIndex = 0;
|
||
|
foreach (var excludeTag in query.ExcludeTags)
|
||
|
{
|
||
9 years ago
|
whereClauses.Add("(Tags is null OR Tags not like @excludeTag" + excludeTagIndex + ")");
|
||
9 years ago
|
cmd.Parameters.Add(cmd, "@excludeTag" + excludeTagIndex, DbType.String).Value = "%" + excludeTag + "%";
|
||
|
excludeTagIndex++;
|
||
|
}
|
||
9 years ago
|
|
||
9 years ago
|
excludeTagIndex = 0;
|
||
|
foreach (var excludeTag in query.ExcludeInheritedTags)
|
||
|
{
|
||
9 years ago
|
whereClauses.Add("(InheritedTags is null OR InheritedTags not like @excludeInheritedTag" + excludeTagIndex + ")");
|
||
9 years ago
|
cmd.Parameters.Add(cmd, "@excludeInheritedTag" + excludeTagIndex, DbType.String).Value = "%" + excludeTag + "%";
|
||
|
excludeTagIndex++;
|
||
|
}
|
||
|
|
||
10 years ago
|
return whereClauses;
|
||
|
}
|
||
|
|
||
9 years ago
|
private string GetCleanValue(string value)
|
||
|
{
|
||
|
if (string.IsNullOrWhiteSpace(value))
|
||
|
{
|
||
|
return value;
|
||
|
}
|
||
|
|
||
|
return value.RemoveDiacritics().ToLower();
|
||
|
}
|
||
|
|
||
9 years ago
|
private bool EnableGroupByPresentationUniqueKey(InternalItemsQuery query)
|
||
|
{
|
||
9 years ago
|
if (!query.GroupByPresentationUniqueKey)
|
||
|
{
|
||
|
return false;
|
||
|
}
|
||
|
|
||
9 years ago
|
if (!string.IsNullOrWhiteSpace(query.PresentationUniqueKey))
|
||
|
{
|
||
|
return false;
|
||
|
}
|
||
|
|
||
9 years ago
|
if (query.User == null)
|
||
|
{
|
||
|
return false;
|
||
|
}
|
||
|
|
||
9 years ago
|
if (query.IncludeItemTypes.Length == 0)
|
||
|
{
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
var types = new[] {
|
||
|
typeof(Episode).Name,
|
||
|
typeof(Video).Name ,
|
||
|
typeof(Movie).Name ,
|
||
|
typeof(MusicVideo).Name ,
|
||
9 years ago
|
typeof(Series).Name ,
|
||
|
typeof(Season).Name };
|
||
9 years ago
|
|
||
|
if (types.Any(i => query.IncludeItemTypes.Contains(i, StringComparer.OrdinalIgnoreCase)))
|
||
|
{
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
return false;
|
||
|
}
|
||
|
|
||
10 years ago
|
private static readonly Type[] KnownTypes =
|
||
|
{
|
||
|
typeof(LiveTvProgram),
|
||
|
typeof(LiveTvChannel),
|
||
|
typeof(LiveTvVideoRecording),
|
||
|
typeof(LiveTvAudioRecording),
|
||
|
typeof(Series),
|
||
|
typeof(Audio),
|
||
|
typeof(MusicAlbum),
|
||
|
typeof(MusicArtist),
|
||
|
typeof(MusicGenre),
|
||
|
typeof(MusicVideo),
|
||
|
typeof(Movie),
|
||
9 years ago
|
typeof(Playlist),
|
||
|
typeof(AudioPodcast),
|
||
9 years ago
|
typeof(Trailer),
|
||
10 years ago
|
typeof(BoxSet),
|
||
|
typeof(Episode),
|
||
|
typeof(Season),
|
||
|
typeof(Series),
|
||
|
typeof(Book),
|
||
|
typeof(CollectionFolder),
|
||
|
typeof(Folder),
|
||
|
typeof(Game),
|
||
|
typeof(GameGenre),
|
||
|
typeof(GameSystem),
|
||
|
typeof(Genre),
|
||
|
typeof(Person),
|
||
|
typeof(Photo),
|
||
|
typeof(PhotoAlbum),
|
||
|
typeof(Studio),
|
||
|
typeof(UserRootFolder),
|
||
|
typeof(UserView),
|
||
|
typeof(Video),
|
||
9 years ago
|
typeof(Year),
|
||
9 years ago
|
typeof(Channel),
|
||
|
typeof(AggregateFolder)
|
||
10 years ago
|
};
|
||
|
|
||
9 years ago
|
public async Task UpdateInheritedValues(CancellationToken cancellationToken)
|
||
9 years ago
|
{
|
||
|
await UpdateInheritedTags(cancellationToken).ConfigureAwait(false);
|
||
|
}
|
||
|
|
||
|
private async Task UpdateInheritedTags(CancellationToken cancellationToken)
|
||
|
{
|
||
9 years ago
|
var newValues = new List<Tuple<Guid, string>>();
|
||
|
|
||
|
using (var cmd = _connection.CreateCommand())
|
||
9 years ago
|
{
|
||
9 years ago
|
cmd.CommandText = "select Guid,InheritedTags,(select group_concat(Tags, '|') from TypedBaseItems where (guid=outer.guid) OR (guid in (Select AncestorId from AncestorIds where ItemId=Outer.guid))) as NewInheritedTags from typedbaseitems as Outer where NewInheritedTags <> InheritedTags";
|
||
9 years ago
|
|
||
9 years ago
|
using (var reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess | CommandBehavior.SingleResult))
|
||
9 years ago
|
{
|
||
9 years ago
|
while (reader.Read())
|
||
9 years ago
|
{
|
||
9 years ago
|
var id = reader.GetGuid(0);
|
||
|
string value = reader.IsDBNull(2) ? null : reader.GetString(2);
|
||
9 years ago
|
|
||
9 years ago
|
newValues.Add(new Tuple<Guid, string>(id, value));
|
||
9 years ago
|
}
|
||
|
}
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
9 years ago
|
Logger.Debug("UpdateInheritedTags - {0} rows", newValues.Count);
|
||
|
if (newValues.Count == 0)
|
||
|
{
|
||
|
return;
|
||
|
}
|
||
9 years ago
|
|
||
9 years ago
|
await WriteLock.WaitAsync(cancellationToken).ConfigureAwait(false);
|
||
9 years ago
|
|
||
9 years ago
|
IDbTransaction transaction = null;
|
||
9 years ago
|
|
||
9 years ago
|
try
|
||
|
{
|
||
|
transaction = _connection.BeginTransaction();
|
||
9 years ago
|
|
||
9 years ago
|
foreach (var item in newValues)
|
||
9 years ago
|
{
|
||
9 years ago
|
_updateInheritedTagsCommand.GetParameter(0).Value = item.Item1;
|
||
|
_updateInheritedTagsCommand.GetParameter(1).Value = item.Item2;
|
||
9 years ago
|
|
||
9 years ago
|
_updateInheritedTagsCommand.Transaction = transaction;
|
||
|
_updateInheritedTagsCommand.ExecuteNonQuery();
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
|
transaction.Commit();
|
||
|
}
|
||
|
catch (OperationCanceledException)
|
||
|
{
|
||
|
if (transaction != null)
|
||
|
{
|
||
|
transaction.Rollback();
|
||
|
}
|
||
9 years ago
|
|
||
9 years ago
|
throw;
|
||
|
}
|
||
|
catch (Exception e)
|
||
|
{
|
||
|
Logger.ErrorException("Error running query:", e);
|
||
9 years ago
|
|
||
9 years ago
|
if (transaction != null)
|
||
|
{
|
||
|
transaction.Rollback();
|
||
|
}
|
||
9 years ago
|
|
||
9 years ago
|
throw;
|
||
|
}
|
||
|
finally
|
||
|
{
|
||
|
if (transaction != null)
|
||
|
{
|
||
|
transaction.Dispose();
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
|
WriteLock.Release();
|
||
9 years ago
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
private static Dictionary<string, string[]> GetTypeMapDictionary()
|
||
|
{
|
||
9 years ago
|
var dict = new Dictionary<string, string[]>(StringComparer.OrdinalIgnoreCase);
|
||
10 years ago
|
|
||
|
foreach (var t in KnownTypes)
|
||
|
{
|
||
|
dict[t.Name] = new[] { t.FullName };
|
||
|
}
|
||
|
|
||
|
dict["Recording"] = new[] { typeof(LiveTvAudioRecording).FullName, typeof(LiveTvVideoRecording).FullName };
|
||
10 years ago
|
dict["Program"] = new[] { typeof(LiveTvProgram).FullName };
|
||
|
dict["TvChannel"] = new[] { typeof(LiveTvChannel).FullName };
|
||
10 years ago
|
|
||
|
return dict;
|
||
|
}
|
||
|
|
||
10 years ago
|
// Not crazy about having this all the way down here, but at least it's in one place
|
||
10 years ago
|
readonly Dictionary<string, string[]> _types = GetTypeMapDictionary();
|
||
10 years ago
|
|
||
10 years ago
|
private IEnumerable<string> MapIncludeItemTypes(string value)
|
||
10 years ago
|
{
|
||
10 years ago
|
string[] result;
|
||
10 years ago
|
if (_types.TryGetValue(value, out result))
|
||
|
{
|
||
|
return result;
|
||
|
}
|
||
|
|
||
10 years ago
|
return new[] { value };
|
||
10 years ago
|
}
|
||
|
|
||
11 years ago
|
public async Task DeleteItem(Guid id, CancellationToken cancellationToken)
|
||
|
{
|
||
|
if (id == Guid.Empty)
|
||
|
{
|
||
|
throw new ArgumentNullException("id");
|
||
|
}
|
||
|
|
||
10 years ago
|
CheckDisposed();
|
||
10 years ago
|
|
||
9 years ago
|
await WriteLock.WaitAsync(cancellationToken).ConfigureAwait(false);
|
||
|
|
||
|
IDbTransaction transaction = null;
|
||
|
|
||
|
try
|
||
11 years ago
|
{
|
||
9 years ago
|
transaction = _connection.BeginTransaction();
|
||
11 years ago
|
|
||
9 years ago
|
// Delete people
|
||
|
_deletePeopleCommand.GetParameter(0).Value = id;
|
||
|
_deletePeopleCommand.Transaction = transaction;
|
||
|
_deletePeopleCommand.ExecuteNonQuery();
|
||
10 years ago
|
|
||
9 years ago
|
// Delete chapters
|
||
|
_deleteChaptersCommand.GetParameter(0).Value = id;
|
||
|
_deleteChaptersCommand.Transaction = transaction;
|
||
|
_deleteChaptersCommand.ExecuteNonQuery();
|
||
9 years ago
|
|
||
9 years ago
|
// Delete media streams
|
||
|
_deleteStreamsCommand.GetParameter(0).Value = id;
|
||
|
_deleteStreamsCommand.Transaction = transaction;
|
||
|
_deleteStreamsCommand.ExecuteNonQuery();
|
||
9 years ago
|
|
||
9 years ago
|
// Delete ancestors
|
||
|
_deleteAncestorsCommand.GetParameter(0).Value = id;
|
||
|
_deleteAncestorsCommand.Transaction = transaction;
|
||
|
_deleteAncestorsCommand.ExecuteNonQuery();
|
||
9 years ago
|
|
||
9 years ago
|
// Delete item values
|
||
|
_deleteItemValuesCommand.GetParameter(0).Value = id;
|
||
|
_deleteItemValuesCommand.Transaction = transaction;
|
||
|
_deleteItemValuesCommand.ExecuteNonQuery();
|
||
9 years ago
|
|
||
9 years ago
|
// Delete provider ids
|
||
|
_deleteProviderIdsCommand.GetParameter(0).Value = id;
|
||
|
_deleteProviderIdsCommand.Transaction = transaction;
|
||
|
_deleteProviderIdsCommand.ExecuteNonQuery();
|
||
9 years ago
|
|
||
9 years ago
|
// Delete images
|
||
|
_deleteImagesCommand.GetParameter(0).Value = id;
|
||
|
_deleteImagesCommand.Transaction = transaction;
|
||
|
_deleteImagesCommand.ExecuteNonQuery();
|
||
9 years ago
|
|
||
9 years ago
|
// Delete the item
|
||
|
_deleteItemCommand.GetParameter(0).Value = id;
|
||
|
_deleteItemCommand.Transaction = transaction;
|
||
|
_deleteItemCommand.ExecuteNonQuery();
|
||
9 years ago
|
|
||
9 years ago
|
transaction.Commit();
|
||
|
}
|
||
|
catch (OperationCanceledException)
|
||
|
{
|
||
|
if (transaction != null)
|
||
11 years ago
|
{
|
||
9 years ago
|
transaction.Rollback();
|
||
11 years ago
|
}
|
||
|
|
||
9 years ago
|
throw;
|
||
|
}
|
||
|
catch (Exception e)
|
||
|
{
|
||
|
Logger.ErrorException("Failed to save children:", e);
|
||
11 years ago
|
|
||
9 years ago
|
if (transaction != null)
|
||
|
{
|
||
|
transaction.Rollback();
|
||
11 years ago
|
}
|
||
9 years ago
|
|
||
|
throw;
|
||
|
}
|
||
|
finally
|
||
|
{
|
||
|
if (transaction != null)
|
||
11 years ago
|
{
|
||
9 years ago
|
transaction.Dispose();
|
||
11 years ago
|
}
|
||
9 years ago
|
|
||
|
WriteLock.Release();
|
||
11 years ago
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
public List<string> GetPeopleNames(InternalPeopleQuery query)
|
||
10 years ago
|
{
|
||
10 years ago
|
if (query == null)
|
||
10 years ago
|
{
|
||
10 years ago
|
throw new ArgumentNullException("query");
|
||
10 years ago
|
}
|
||
|
|
||
|
CheckDisposed();
|
||
|
|
||
9 years ago
|
using (var cmd = _connection.CreateCommand())
|
||
10 years ago
|
{
|
||
9 years ago
|
cmd.CommandText = "select Distinct Name from People";
|
||
10 years ago
|
|
||
9 years ago
|
var whereClauses = GetPeopleWhereClauses(query, cmd);
|
||
10 years ago
|
|
||
9 years ago
|
if (whereClauses.Count > 0)
|
||
|
{
|
||
|
cmd.CommandText += " where " + string.Join(" AND ", whereClauses.ToArray());
|
||
|
}
|
||
10 years ago
|
|
||
9 years ago
|
cmd.CommandText += " order by ListOrder";
|
||
10 years ago
|
|
||
9 years ago
|
var list = new List<string>();
|
||
10 years ago
|
|
||
9 years ago
|
using (var reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess | CommandBehavior.SingleResult))
|
||
|
{
|
||
|
while (reader.Read())
|
||
10 years ago
|
{
|
||
9 years ago
|
list.Add(reader.GetString(0));
|
||
10 years ago
|
}
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
|
return list;
|
||
10 years ago
|
}
|
||
|
}
|
||
10 years ago
|
|
||
10 years ago
|
public List<PersonInfo> GetPeople(InternalPeopleQuery query)
|
||
10 years ago
|
{
|
||
10 years ago
|
if (query == null)
|
||
10 years ago
|
{
|
||
10 years ago
|
throw new ArgumentNullException("query");
|
||
10 years ago
|
}
|
||
|
|
||
|
CheckDisposed();
|
||
|
|
||
9 years ago
|
using (var cmd = _connection.CreateCommand())
|
||
10 years ago
|
{
|
||
9 years ago
|
cmd.CommandText = "select ItemId, Name, Role, PersonType, SortOrder from People";
|
||
10 years ago
|
|
||
9 years ago
|
var whereClauses = GetPeopleWhereClauses(query, cmd);
|
||
10 years ago
|
|
||
9 years ago
|
if (whereClauses.Count > 0)
|
||
|
{
|
||
|
cmd.CommandText += " where " + string.Join(" AND ", whereClauses.ToArray());
|
||
|
}
|
||
10 years ago
|
|
||
9 years ago
|
cmd.CommandText += " order by ListOrder";
|
||
10 years ago
|
|
||
9 years ago
|
var list = new List<PersonInfo>();
|
||
10 years ago
|
|
||
9 years ago
|
using (var reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess | CommandBehavior.SingleResult))
|
||
|
{
|
||
|
while (reader.Read())
|
||
10 years ago
|
{
|
||
9 years ago
|
list.Add(GetPerson(reader));
|
||
10 years ago
|
}
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
|
return list;
|
||
10 years ago
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
private List<string> GetPeopleWhereClauses(InternalPeopleQuery query, IDbCommand cmd)
|
||
|
{
|
||
|
var whereClauses = new List<string>();
|
||
|
|
||
|
if (query.ItemId != Guid.Empty)
|
||
|
{
|
||
|
whereClauses.Add("ItemId=@ItemId");
|
||
|
cmd.Parameters.Add(cmd, "@ItemId", DbType.Guid).Value = query.ItemId;
|
||
|
}
|
||
|
if (query.AppearsInItemId != Guid.Empty)
|
||
|
{
|
||
|
whereClauses.Add("Name in (Select Name from People where ItemId=@AppearsInItemId)");
|
||
|
cmd.Parameters.Add(cmd, "@AppearsInItemId", DbType.Guid).Value = query.AppearsInItemId;
|
||
|
}
|
||
|
if (query.PersonTypes.Count == 1)
|
||
|
{
|
||
|
whereClauses.Add("PersonType=@PersonType");
|
||
|
cmd.Parameters.Add(cmd, "@PersonType", DbType.String).Value = query.PersonTypes[0];
|
||
|
}
|
||
|
if (query.PersonTypes.Count > 1)
|
||
|
{
|
||
|
var val = string.Join(",", query.PersonTypes.Select(i => "'" + i + "'").ToArray());
|
||
|
|
||
|
whereClauses.Add("PersonType in (" + val + ")");
|
||
|
}
|
||
|
if (query.ExcludePersonTypes.Count == 1)
|
||
|
{
|
||
|
whereClauses.Add("PersonType<>@PersonType");
|
||
|
cmd.Parameters.Add(cmd, "@PersonType", DbType.String).Value = query.ExcludePersonTypes[0];
|
||
|
}
|
||
|
if (query.ExcludePersonTypes.Count > 1)
|
||
|
{
|
||
|
var val = string.Join(",", query.ExcludePersonTypes.Select(i => "'" + i + "'").ToArray());
|
||
|
|
||
|
whereClauses.Add("PersonType not in (" + val + ")");
|
||
|
}
|
||
|
if (query.MaxListOrder.HasValue)
|
||
|
{
|
||
|
whereClauses.Add("ListOrder<=@MaxListOrder");
|
||
|
cmd.Parameters.Add(cmd, "@MaxListOrder", DbType.Int32).Value = query.MaxListOrder.Value;
|
||
|
}
|
||
10 years ago
|
if (!string.IsNullOrWhiteSpace(query.NameContains))
|
||
|
{
|
||
|
whereClauses.Add("Name like @NameContains");
|
||
10 years ago
|
cmd.Parameters.Add(cmd, "@NameContains", DbType.String).Value = "%" + query.NameContains + "%";
|
||
10 years ago
|
}
|
||
9 years ago
|
if (query.SourceTypes.Length == 1)
|
||
|
{
|
||
|
whereClauses.Add("(select sourcetype from typedbaseitems where guid=ItemId) = @SourceTypes");
|
||
|
cmd.Parameters.Add(cmd, "@SourceTypes", DbType.String).Value = query.SourceTypes[0].ToString();
|
||
|
}
|
||
10 years ago
|
|
||
|
return whereClauses;
|
||
|
}
|
||
|
|
||
9 years ago
|
private void UpdateAncestors(Guid itemId, List<Guid> ancestorIds, IDbTransaction transaction)
|
||
9 years ago
|
{
|
||
|
if (itemId == Guid.Empty)
|
||
|
{
|
||
|
throw new ArgumentNullException("itemId");
|
||
|
}
|
||
|
|
||
|
if (ancestorIds == null)
|
||
|
{
|
||
|
throw new ArgumentNullException("ancestorIds");
|
||
|
}
|
||
|
|
||
|
CheckDisposed();
|
||
|
|
||
|
// First delete
|
||
9 years ago
|
_deleteAncestorsCommand.GetParameter(0).Value = itemId;
|
||
|
_deleteAncestorsCommand.Transaction = transaction;
|
||
9 years ago
|
|
||
9 years ago
|
_deleteAncestorsCommand.ExecuteNonQuery();
|
||
9 years ago
|
|
||
9 years ago
|
foreach (var ancestorId in ancestorIds)
|
||
|
{
|
||
|
_saveAncestorCommand.GetParameter(0).Value = itemId;
|
||
|
_saveAncestorCommand.GetParameter(1).Value = ancestorId;
|
||
|
_saveAncestorCommand.GetParameter(2).Value = ancestorId.ToString("N");
|
||
9 years ago
|
|
||
9 years ago
|
_saveAncestorCommand.Transaction = transaction;
|
||
9 years ago
|
|
||
9 years ago
|
_saveAncestorCommand.ExecuteNonQuery();
|
||
9 years ago
|
}
|
||
|
}
|
||
|
|
||
9 years ago
|
public QueryResult<Tuple<BaseItem, ItemCounts>> GetAllArtists(InternalItemsQuery query)
|
||
|
{
|
||
|
return GetItemValues(query, new[] { 0, 1 }, typeof(MusicArtist).FullName);
|
||
|
}
|
||
|
|
||
9 years ago
|
public QueryResult<Tuple<BaseItem, ItemCounts>> GetArtists(InternalItemsQuery query)
|
||
|
{
|
||
9 years ago
|
return GetItemValues(query, new[] { 0 }, typeof(MusicArtist).FullName);
|
||
9 years ago
|
}
|
||
|
|
||
|
public QueryResult<Tuple<BaseItem, ItemCounts>> GetAlbumArtists(InternalItemsQuery query)
|
||
|
{
|
||
9 years ago
|
return GetItemValues(query, new[] { 1 }, typeof(MusicArtist).FullName);
|
||
9 years ago
|
}
|
||
|
|
||
|
public QueryResult<Tuple<BaseItem, ItemCounts>> GetStudios(InternalItemsQuery query)
|
||
|
{
|
||
9 years ago
|
return GetItemValues(query, new[] { 3 }, typeof(Studio).FullName);
|
||
9 years ago
|
}
|
||
|
|
||
|
public QueryResult<Tuple<BaseItem, ItemCounts>> GetGenres(InternalItemsQuery query)
|
||
|
{
|
||
9 years ago
|
return GetItemValues(query, new[] { 2 }, typeof(Genre).FullName);
|
||
9 years ago
|
}
|
||
|
|
||
|
public QueryResult<Tuple<BaseItem, ItemCounts>> GetGameGenres(InternalItemsQuery query)
|
||
|
{
|
||
9 years ago
|
return GetItemValues(query, new[] { 2 }, typeof(GameGenre).FullName);
|
||
9 years ago
|
}
|
||
|
|
||
|
public QueryResult<Tuple<BaseItem, ItemCounts>> GetMusicGenres(InternalItemsQuery query)
|
||
|
{
|
||
9 years ago
|
return GetItemValues(query, new[] { 2 }, typeof(MusicGenre).FullName);
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
public List<string> GetStudioNames()
|
||
|
{
|
||
9 years ago
|
return GetItemValueNames(new[] { 3 }, new List<string>(), new List<string>());
|
||
9 years ago
|
}
|
||
|
|
||
|
public List<string> GetAllArtistNames()
|
||
|
{
|
||
9 years ago
|
return GetItemValueNames(new[] { 0, 1 }, new List<string>(), new List<string>());
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
public List<string> GetMusicGenreNames()
|
||
|
{
|
||
|
return GetItemValueNames(new[] { 2 }, new List<string> { "Audio", "MusicVideo", "MusicAlbum", "MusicArtist" }, new List<string>());
|
||
|
}
|
||
|
|
||
|
public List<string> GetGameGenreNames()
|
||
|
{
|
||
|
return GetItemValueNames(new[] { 2 }, new List<string> { "Game" }, new List<string>());
|
||
|
}
|
||
|
|
||
|
public List<string> GetGenreNames()
|
||
|
{
|
||
|
return GetItemValueNames(new[] { 2 }, new List<string>(), new List<string> { "Audio", "MusicVideo", "MusicAlbum", "MusicArtist", "Game", "GameSystem" });
|
||
|
}
|
||
|
|
||
|
private List<string> GetItemValueNames(int[] itemValueTypes, List<string> withItemTypes, List<string> excludeItemTypes)
|
||
9 years ago
|
{
|
||
|
CheckDisposed();
|
||
|
|
||
9 years ago
|
withItemTypes = withItemTypes.SelectMany(MapIncludeItemTypes).ToList();
|
||
|
excludeItemTypes = excludeItemTypes.SelectMany(MapIncludeItemTypes).ToList();
|
||
|
|
||
9 years ago
|
var now = DateTime.UtcNow;
|
||
|
|
||
|
var typeClause = itemValueTypes.Length == 1 ?
|
||
|
("Type=" + itemValueTypes[0].ToString(CultureInfo.InvariantCulture)) :
|
||
|
("Type in (" + string.Join(",", itemValueTypes.Select(i => i.ToString(CultureInfo.InvariantCulture)).ToArray()) + ")");
|
||
|
|
||
|
var list = new List<string>();
|
||
|
|
||
|
using (var cmd = _connection.CreateCommand())
|
||
|
{
|
||
9 years ago
|
cmd.CommandText = "Select Value From ItemValues where " + typeClause;
|
||
|
|
||
|
if (withItemTypes.Count > 0)
|
||
|
{
|
||
|
var typeString = string.Join(",", withItemTypes.Select(i => "'" + i + "'").ToArray());
|
||
|
cmd.CommandText += " AND ItemId In (select guid from typedbaseitems where type in (" + typeString + "))";
|
||
|
}
|
||
|
if (excludeItemTypes.Count > 0)
|
||
|
{
|
||
|
var typeString = string.Join(",", excludeItemTypes.Select(i => "'" + i + "'").ToArray());
|
||
|
cmd.CommandText += " AND ItemId not In (select guid from typedbaseitems where type in (" + typeString + "))";
|
||
|
}
|
||
|
|
||
|
cmd.CommandText += " Group By CleanValue";
|
||
9 years ago
|
|
||
|
var commandBehavior = CommandBehavior.SequentialAccess | CommandBehavior.SingleResult;
|
||
|
|
||
|
using (var reader = cmd.ExecuteReader(commandBehavior))
|
||
|
{
|
||
|
LogQueryTime("GetItemValueNames", cmd, now);
|
||
|
|
||
|
while (reader.Read())
|
||
|
{
|
||
|
if (!reader.IsDBNull(0))
|
||
|
{
|
||
|
list.Add(reader.GetString(0));
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
return list;
|
||
|
}
|
||
|
|
||
9 years ago
|
private QueryResult<Tuple<BaseItem, ItemCounts>> GetItemValues(InternalItemsQuery query, int[] itemValueTypes, string returnType)
|
||
9 years ago
|
{
|
||
|
if (query == null)
|
||
|
{
|
||
|
throw new ArgumentNullException("query");
|
||
|
}
|
||
|
|
||
|
if (!query.Limit.HasValue)
|
||
|
{
|
||
|
query.EnableTotalRecordCount = false;
|
||
|
}
|
||
|
|
||
|
CheckDisposed();
|
||
|
|
||
|
var now = DateTime.UtcNow;
|
||
|
|
||
9 years ago
|
var typeClause = itemValueTypes.Length == 1 ?
|
||
|
("Type=" + itemValueTypes[0].ToString(CultureInfo.InvariantCulture)) :
|
||
|
("Type in (" + string.Join(",", itemValueTypes.Select(i => i.ToString(CultureInfo.InvariantCulture)).ToArray()) + ")");
|
||
|
|
||
9 years ago
|
using (var cmd = _connection.CreateCommand())
|
||
|
{
|
||
|
var itemCountColumns = new List<Tuple<string, string>>();
|
||
|
|
||
|
var typesToCount = query.IncludeItemTypes.ToList();
|
||
|
|
||
9 years ago
|
if (typesToCount.Count > 0)
|
||
9 years ago
|
{
|
||
9 years ago
|
var itemCountColumnQuery = "select group_concat(type, '|')" + GetFromText("B");
|
||
9 years ago
|
|
||
|
var typeSubQuery = new InternalItemsQuery(query.User)
|
||
|
{
|
||
|
ExcludeItemTypes = query.ExcludeItemTypes,
|
||
9 years ago
|
IncludeItemTypes = query.IncludeItemTypes,
|
||
9 years ago
|
MediaTypes = query.MediaTypes,
|
||
|
AncestorIds = query.AncestorIds,
|
||
|
ExcludeItemIds = query.ExcludeItemIds,
|
||
|
ItemIds = query.ItemIds,
|
||
|
TopParentIds = query.TopParentIds,
|
||
|
ParentId = query.ParentId,
|
||
|
IsPlayed = query.IsPlayed
|
||
|
};
|
||
9 years ago
|
var whereClauses = GetWhereClauses(typeSubQuery, cmd, "itemTypes");
|
||
|
|
||
9 years ago
|
whereClauses.Add("guid in (select ItemId from ItemValues where ItemValues.CleanValue=A.CleanName AND " + typeClause + ")");
|
||
9 years ago
|
|
||
|
var typeWhereText = whereClauses.Count == 0 ?
|
||
|
string.Empty :
|
||
|
" where " + string.Join(" AND ", whereClauses.ToArray());
|
||
|
|
||
|
itemCountColumnQuery += typeWhereText;
|
||
|
|
||
9 years ago
|
//itemCountColumnQuery += ")";
|
||
9 years ago
|
|
||
9 years ago
|
itemCountColumns.Add(new Tuple<string, string>("itemTypes", "(" + itemCountColumnQuery + ") as itemTypes"));
|
||
9 years ago
|
}
|
||
|
|
||
|
var columns = _retriveItemColumns.ToList();
|
||
|
columns.AddRange(itemCountColumns.Select(i => i.Item2).ToArray());
|
||
|
|
||
|
cmd.CommandText = "select " + string.Join(",", GetFinalColumnsToSelect(query, columns.ToArray(), cmd)) + GetFromText();
|
||
|
cmd.CommandText += GetJoinUserDataText(query);
|
||
|
|
||
|
var innerQuery = 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
|
||
|
};
|
||
|
|
||
|
var innerWhereClauses = GetWhereClauses(innerQuery, cmd);
|
||
|
|
||
|
var innerWhereText = innerWhereClauses.Count == 0 ?
|
||
|
string.Empty :
|
||
|
" where " + string.Join(" AND ", innerWhereClauses.ToArray());
|
||
|
|
||
|
var whereText = " where Type=@SelectType";
|
||
9 years ago
|
|
||
|
if (typesToCount.Count == 0)
|
||
|
{
|
||
9 years ago
|
whereText += " And CleanName In (Select CleanValue from ItemValues where " + typeClause + " AND ItemId in (select guid from TypedBaseItems" + innerWhereText + "))";
|
||
9 years ago
|
}
|
||
|
else
|
||
|
{
|
||
9 years ago
|
//whereText += " And itemTypes not null";
|
||
9 years ago
|
whereText += " And CleanName In (Select CleanValue from ItemValues where " + typeClause + " AND ItemId in (select guid from TypedBaseItems" + innerWhereText + "))";
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
|
var outerQuery = new InternalItemsQuery(query.User)
|
||
|
{
|
||
|
IsFavorite = query.IsFavorite,
|
||
|
IsFavoriteOrLiked = query.IsFavoriteOrLiked,
|
||
|
IsLiked = query.IsLiked,
|
||
|
IsLocked = query.IsLocked,
|
||
|
NameLessThan = query.NameLessThan,
|
||
|
NameStartsWith = query.NameStartsWith,
|
||
|
NameStartsWithOrGreater = query.NameStartsWithOrGreater,
|
||
|
AlbumArtistStartsWithOrGreater = query.AlbumArtistStartsWithOrGreater,
|
||
|
Tags = query.Tags,
|
||
|
OfficialRatings = query.OfficialRatings,
|
||
9 years ago
|
GenreIds = query.GenreIds,
|
||
|
Genres = query.Genres,
|
||
9 years ago
|
Years = query.Years
|
||
|
};
|
||
|
|
||
|
var outerWhereClauses = GetWhereClauses(outerQuery, cmd);
|
||
|
|
||
9 years ago
|
whereText += outerWhereClauses.Count == 0 ?
|
||
9 years ago
|
string.Empty :
|
||
|
" AND " + string.Join(" AND ", outerWhereClauses.ToArray());
|
||
9 years ago
|
//cmd.CommandText += GetGroupBy(query);
|
||
|
|
||
|
cmd.CommandText += whereText;
|
||
|
cmd.CommandText += " group by PresentationUniqueKey";
|
||
9 years ago
|
|
||
|
cmd.Parameters.Add(cmd, "@SelectType", DbType.String).Value = returnType;
|
||
|
|
||
|
if (EnableJoinUserData(query))
|
||
|
{
|
||
|
cmd.Parameters.Add(cmd, "@UserId", DbType.Guid).Value = query.User.Id;
|
||
|
}
|
||
|
|
||
|
cmd.CommandText += " order by SortName";
|
||
|
|
||
|
if (query.Limit.HasValue || query.StartIndex.HasValue)
|
||
|
{
|
||
9 years ago
|
var offset = query.StartIndex ?? 0;
|
||
9 years ago
|
|
||
9 years ago
|
if (query.Limit.HasValue || offset > 0)
|
||
|
{
|
||
|
cmd.CommandText += " LIMIT " + (query.Limit ?? int.MaxValue).ToString(CultureInfo.InvariantCulture);
|
||
|
}
|
||
9 years ago
|
|
||
9 years ago
|
if (offset > 0)
|
||
9 years ago
|
{
|
||
9 years ago
|
cmd.CommandText += " OFFSET " + offset.ToString(CultureInfo.InvariantCulture);
|
||
9 years ago
|
}
|
||
|
}
|
||
|
|
||
|
cmd.CommandText += ";";
|
||
|
|
||
|
var isReturningZeroItems = query.Limit.HasValue && query.Limit <= 0;
|
||
|
|
||
|
if (isReturningZeroItems)
|
||
|
{
|
||
|
cmd.CommandText = "";
|
||
|
}
|
||
|
|
||
|
if (query.EnableTotalRecordCount)
|
||
|
{
|
||
9 years ago
|
cmd.CommandText += "select count (distinct PresentationUniqueKey)" + GetFromText();
|
||
9 years ago
|
|
||
|
cmd.CommandText += GetJoinUserDataText(query);
|
||
|
cmd.CommandText += whereText;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
cmd.CommandText = cmd.CommandText.TrimEnd(';');
|
||
|
}
|
||
|
|
||
|
var list = new List<Tuple<BaseItem, ItemCounts>>();
|
||
|
var count = 0;
|
||
|
|
||
|
var commandBehavior = isReturningZeroItems || !query.EnableTotalRecordCount
|
||
|
? (CommandBehavior.SequentialAccess | CommandBehavior.SingleResult)
|
||
|
: CommandBehavior.SequentialAccess;
|
||
|
|
||
9 years ago
|
//Logger.Debug("GetItemValues: " + cmd.CommandText);
|
||
9 years ago
|
|
||
9 years ago
|
using (var reader = cmd.ExecuteReader(commandBehavior))
|
||
|
{
|
||
|
LogQueryTime("GetItemValues", cmd, now);
|
||
|
|
||
|
if (isReturningZeroItems)
|
||
|
{
|
||
|
if (reader.Read())
|
||
|
{
|
||
|
count = reader.GetInt32(0);
|
||
|
}
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
while (reader.Read())
|
||
|
{
|
||
|
var item = GetItem(reader);
|
||
|
if (item != null)
|
||
|
{
|
||
9 years ago
|
var countStartColumn = columns.Count - 1;
|
||
9 years ago
|
|
||
|
list.Add(new Tuple<BaseItem, ItemCounts>(item, GetItemCounts(reader, countStartColumn, typesToCount)));
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (reader.NextResult() && reader.Read())
|
||
|
{
|
||
|
count = reader.GetInt32(0);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (count == 0)
|
||
|
{
|
||
|
count = list.Count;
|
||
|
}
|
||
|
|
||
|
return new QueryResult<Tuple<BaseItem, ItemCounts>>
|
||
|
{
|
||
|
Items = list.ToArray(),
|
||
|
TotalRecordCount = count
|
||
|
};
|
||
|
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private ItemCounts GetItemCounts(IDataReader reader, int countStartColumn, List<string> typesToCount)
|
||
|
{
|
||
|
var counts = new ItemCounts();
|
||
|
|
||
9 years ago
|
if (typesToCount.Count == 0)
|
||
|
{
|
||
|
return counts;
|
||
|
}
|
||
|
|
||
|
var typeString = reader.IsDBNull(countStartColumn) ? null : reader.GetString(countStartColumn);
|
||
|
|
||
|
if (string.IsNullOrWhiteSpace(typeString))
|
||
9 years ago
|
{
|
||
9 years ago
|
return counts;
|
||
|
}
|
||
9 years ago
|
|
||
9 years ago
|
var allTypes = typeString.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries)
|
||
9 years ago
|
.ToLookup(i => i).ToList();
|
||
|
|
||
|
foreach (var type in allTypes)
|
||
|
{
|
||
|
var value = type.ToList().Count;
|
||
|
var typeName = type.Key;
|
||
|
|
||
|
if (string.Equals(typeName, typeof(Series).FullName, StringComparison.OrdinalIgnoreCase))
|
||
9 years ago
|
{
|
||
|
counts.SeriesCount = value;
|
||
|
}
|
||
9 years ago
|
else if (string.Equals(typeName, typeof(Episode).FullName, StringComparison.OrdinalIgnoreCase))
|
||
9 years ago
|
{
|
||
|
counts.EpisodeCount = value;
|
||
|
}
|
||
9 years ago
|
else if (string.Equals(typeName, typeof(Movie).FullName, StringComparison.OrdinalIgnoreCase))
|
||
9 years ago
|
{
|
||
|
counts.MovieCount = value;
|
||
|
}
|
||
9 years ago
|
else if (string.Equals(typeName, typeof(MusicAlbum).FullName, StringComparison.OrdinalIgnoreCase))
|
||
9 years ago
|
{
|
||
|
counts.AlbumCount = value;
|
||
|
}
|
||
9 years ago
|
else if (string.Equals(typeName, typeof(MusicArtist).FullName, StringComparison.OrdinalIgnoreCase))
|
||
|
{
|
||
|
counts.ArtistCount = value;
|
||
|
}
|
||
9 years ago
|
else if (string.Equals(typeName, typeof(Audio).FullName, StringComparison.OrdinalIgnoreCase))
|
||
9 years ago
|
{
|
||
|
counts.SongCount = value;
|
||
|
}
|
||
9 years ago
|
else if (string.Equals(typeName, typeof(Game).FullName, StringComparison.OrdinalIgnoreCase))
|
||
9 years ago
|
{
|
||
|
counts.GameCount = value;
|
||
|
}
|
||
9 years ago
|
else if (string.Equals(typeName, typeof(Trailer).FullName, StringComparison.OrdinalIgnoreCase))
|
||
|
{
|
||
|
counts.TrailerCount = value;
|
||
|
}
|
||
9 years ago
|
counts.ItemCount += value;
|
||
|
}
|
||
|
|
||
|
return counts;
|
||
|
}
|
||
|
|
||
|
private List<Tuple<int, string>> GetItemValuesToSave(BaseItem item)
|
||
9 years ago
|
{
|
||
|
var list = new List<Tuple<int, string>>();
|
||
|
|
||
|
var hasArtist = item as IHasArtist;
|
||
|
if (hasArtist != null)
|
||
|
{
|
||
|
list.AddRange(hasArtist.Artists.Select(i => new Tuple<int, string>(0, i)));
|
||
|
}
|
||
|
|
||
|
var hasAlbumArtist = item as IHasAlbumArtist;
|
||
|
if (hasAlbumArtist != null)
|
||
|
{
|
||
|
list.AddRange(hasAlbumArtist.AlbumArtists.Select(i => new Tuple<int, string>(1, i)));
|
||
|
}
|
||
|
|
||
9 years ago
|
list.AddRange(item.Genres.Select(i => new Tuple<int, string>(2, i)));
|
||
|
list.AddRange(item.Studios.Select(i => new Tuple<int, string>(3, i)));
|
||
|
list.AddRange(item.Tags.Select(i => new Tuple<int, string>(4, i)));
|
||
|
list.AddRange(item.Keywords.Select(i => new Tuple<int, string>(5, i)));
|
||
|
|
||
9 years ago
|
return list;
|
||
|
}
|
||
|
|
||
9 years ago
|
private void UpdateImages(Guid itemId, List<ItemImageInfo> images, IDbTransaction transaction)
|
||
9 years ago
|
{
|
||
|
if (itemId == Guid.Empty)
|
||
|
{
|
||
|
throw new ArgumentNullException("itemId");
|
||
|
}
|
||
|
|
||
|
if (images == null)
|
||
|
{
|
||
|
throw new ArgumentNullException("images");
|
||
|
}
|
||
|
|
||
|
CheckDisposed();
|
||
|
|
||
|
// First delete
|
||
9 years ago
|
_deleteImagesCommand.GetParameter(0).Value = itemId;
|
||
|
_deleteImagesCommand.Transaction = transaction;
|
||
9 years ago
|
|
||
9 years ago
|
_deleteImagesCommand.ExecuteNonQuery();
|
||
9 years ago
|
|
||
9 years ago
|
var index = 0;
|
||
|
foreach (var image in images)
|
||
|
{
|
||
9 years ago
|
if (string.IsNullOrWhiteSpace(image.Path))
|
||
|
{
|
||
|
// Invalid
|
||
|
continue;
|
||
|
}
|
||
|
|
||
9 years ago
|
_saveImagesCommand.GetParameter(0).Value = itemId;
|
||
|
_saveImagesCommand.GetParameter(1).Value = image.Type;
|
||
|
_saveImagesCommand.GetParameter(2).Value = image.Path;
|
||
9 years ago
|
|
||
9 years ago
|
if (image.DateModified == default(DateTime))
|
||
|
{
|
||
|
_saveImagesCommand.GetParameter(3).Value = null;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
_saveImagesCommand.GetParameter(3).Value = image.DateModified;
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
_saveImagesCommand.GetParameter(4).Value = image.IsPlaceholder;
|
||
|
_saveImagesCommand.GetParameter(5).Value = index;
|
||
9 years ago
|
|
||
9 years ago
|
_saveImagesCommand.Transaction = transaction;
|
||
9 years ago
|
|
||
9 years ago
|
_saveImagesCommand.ExecuteNonQuery();
|
||
|
index++;
|
||
9 years ago
|
}
|
||
|
}
|
||
|
|
||
9 years ago
|
private void UpdateProviderIds(Guid itemId, Dictionary<string, string> values, IDbTransaction transaction)
|
||
9 years ago
|
{
|
||
|
if (itemId == Guid.Empty)
|
||
|
{
|
||
|
throw new ArgumentNullException("itemId");
|
||
|
}
|
||
|
|
||
|
if (values == null)
|
||
|
{
|
||
9 years ago
|
throw new ArgumentNullException("values");
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
// Just in case there might be case-insensitive duplicates, strip them out now
|
||
|
var newValues = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
|
||
|
foreach (var pair in values)
|
||
|
{
|
||
|
newValues[pair.Key] = pair.Value;
|
||
|
}
|
||
|
|
||
9 years ago
|
CheckDisposed();
|
||
|
|
||
|
// First delete
|
||
9 years ago
|
_deleteProviderIdsCommand.GetParameter(0).Value = itemId;
|
||
|
_deleteProviderIdsCommand.Transaction = transaction;
|
||
9 years ago
|
|
||
9 years ago
|
_deleteProviderIdsCommand.ExecuteNonQuery();
|
||
9 years ago
|
|
||
9 years ago
|
foreach (var pair in newValues)
|
||
9 years ago
|
{
|
||
8 years ago
|
if (string.IsNullOrWhiteSpace(pair.Key))
|
||
|
{
|
||
|
continue;
|
||
|
}
|
||
|
if (string.IsNullOrWhiteSpace(pair.Value))
|
||
|
{
|
||
|
continue;
|
||
|
}
|
||
|
|
||
9 years ago
|
_saveProviderIdsCommand.GetParameter(0).Value = itemId;
|
||
|
_saveProviderIdsCommand.GetParameter(1).Value = pair.Key;
|
||
|
_saveProviderIdsCommand.GetParameter(2).Value = pair.Value;
|
||
|
_saveProviderIdsCommand.Transaction = transaction;
|
||
9 years ago
|
|
||
9 years ago
|
_saveProviderIdsCommand.ExecuteNonQuery();
|
||
9 years ago
|
}
|
||
|
}
|
||
|
|
||
9 years ago
|
private void UpdateItemValues(Guid itemId, List<Tuple<int, string>> values, IDbTransaction transaction)
|
||
9 years ago
|
{
|
||
|
if (itemId == Guid.Empty)
|
||
|
{
|
||
|
throw new ArgumentNullException("itemId");
|
||
|
}
|
||
|
|
||
|
if (values == null)
|
||
|
{
|
||
|
throw new ArgumentNullException("keys");
|
||
|
}
|
||
|
|
||
|
CheckDisposed();
|
||
|
|
||
|
// First delete
|
||
9 years ago
|
_deleteItemValuesCommand.GetParameter(0).Value = itemId;
|
||
|
_deleteItemValuesCommand.Transaction = transaction;
|
||
9 years ago
|
|
||
9 years ago
|
_deleteItemValuesCommand.ExecuteNonQuery();
|
||
9 years ago
|
|
||
9 years ago
|
foreach (var pair in values)
|
||
9 years ago
|
{
|
||
9 years ago
|
_saveItemValuesCommand.GetParameter(0).Value = itemId;
|
||
|
_saveItemValuesCommand.GetParameter(1).Value = pair.Item1;
|
||
|
_saveItemValuesCommand.GetParameter(2).Value = pair.Item2;
|
||
9 years ago
|
if (pair.Item2 == null)
|
||
|
{
|
||
|
_saveItemValuesCommand.GetParameter(3).Value = null;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
9 years ago
|
_saveItemValuesCommand.GetParameter(3).Value = GetCleanValue(pair.Item2);
|
||
9 years ago
|
}
|
||
9 years ago
|
_saveItemValuesCommand.Transaction = transaction;
|
||
9 years ago
|
|
||
9 years ago
|
_saveItemValuesCommand.ExecuteNonQuery();
|
||
9 years ago
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
public async Task UpdatePeople(Guid itemId, List<PersonInfo> people)
|
||
|
{
|
||
|
if (itemId == Guid.Empty)
|
||
|
{
|
||
|
throw new ArgumentNullException("itemId");
|
||
|
}
|
||
|
|
||
|
if (people == null)
|
||
|
{
|
||
|
throw new ArgumentNullException("people");
|
||
|
}
|
||
|
|
||
|
CheckDisposed();
|
||
|
|
||
|
var cancellationToken = CancellationToken.None;
|
||
|
|
||
9 years ago
|
await WriteLock.WaitAsync(cancellationToken).ConfigureAwait(false);
|
||
10 years ago
|
|
||
9 years ago
|
IDbTransaction transaction = null;
|
||
10 years ago
|
|
||
9 years ago
|
try
|
||
|
{
|
||
|
transaction = _connection.BeginTransaction();
|
||
10 years ago
|
|
||
9 years ago
|
// First delete
|
||
|
_deletePeopleCommand.GetParameter(0).Value = itemId;
|
||
|
_deletePeopleCommand.Transaction = transaction;
|
||
10 years ago
|
|
||
9 years ago
|
_deletePeopleCommand.ExecuteNonQuery();
|
||
10 years ago
|
|
||
9 years ago
|
var listIndex = 0;
|
||
10 years ago
|
|
||
9 years ago
|
foreach (var person in people)
|
||
10 years ago
|
{
|
||
9 years ago
|
cancellationToken.ThrowIfCancellationRequested();
|
||
|
|
||
|
_savePersonCommand.GetParameter(0).Value = itemId;
|
||
|
_savePersonCommand.GetParameter(1).Value = person.Name;
|
||
|
_savePersonCommand.GetParameter(2).Value = person.Role;
|
||
|
_savePersonCommand.GetParameter(3).Value = person.Type;
|
||
|
_savePersonCommand.GetParameter(4).Value = person.SortOrder;
|
||
|
_savePersonCommand.GetParameter(5).Value = listIndex;
|
||
9 years ago
|
|
||
9 years ago
|
_savePersonCommand.Transaction = transaction;
|
||
|
|
||
|
_savePersonCommand.ExecuteNonQuery();
|
||
|
listIndex++;
|
||
10 years ago
|
}
|
||
9 years ago
|
|
||
|
transaction.Commit();
|
||
|
}
|
||
|
catch (OperationCanceledException)
|
||
|
{
|
||
|
if (transaction != null)
|
||
9 years ago
|
{
|
||
9 years ago
|
transaction.Rollback();
|
||
|
}
|
||
10 years ago
|
|
||
9 years ago
|
throw;
|
||
|
}
|
||
|
catch (Exception e)
|
||
|
{
|
||
|
Logger.ErrorException("Failed to save people:", e);
|
||
10 years ago
|
|
||
9 years ago
|
if (transaction != null)
|
||
|
{
|
||
|
transaction.Rollback();
|
||
10 years ago
|
}
|
||
9 years ago
|
|
||
|
throw;
|
||
|
}
|
||
|
finally
|
||
|
{
|
||
|
if (transaction != null)
|
||
10 years ago
|
{
|
||
9 years ago
|
transaction.Dispose();
|
||
10 years ago
|
}
|
||
9 years ago
|
|
||
|
WriteLock.Release();
|
||
10 years ago
|
}
|
||
|
}
|
||
|
|
||
|
private PersonInfo GetPerson(IDataReader reader)
|
||
|
{
|
||
|
var item = new PersonInfo();
|
||
|
|
||
10 years ago
|
item.ItemId = reader.GetGuid(0);
|
||
10 years ago
|
item.Name = reader.GetString(1);
|
||
|
|
||
|
if (!reader.IsDBNull(2))
|
||
|
{
|
||
|
item.Role = reader.GetString(2);
|
||
|
}
|
||
|
|
||
|
if (!reader.IsDBNull(3))
|
||
|
{
|
||
|
item.Type = reader.GetString(3);
|
||
|
}
|
||
|
|
||
|
if (!reader.IsDBNull(4))
|
||
|
{
|
||
|
item.SortOrder = reader.GetInt32(4);
|
||
|
}
|
||
|
|
||
|
return item;
|
||
|
}
|
||
9 years ago
|
|
||
|
public IEnumerable<MediaStream> GetMediaStreams(MediaStreamQuery query)
|
||
|
{
|
||
|
CheckDisposed();
|
||
|
|
||
|
if (query == null)
|
||
|
{
|
||
|
throw new ArgumentNullException("query");
|
||
|
}
|
||
|
|
||
9 years ago
|
var list = new List<MediaStream>();
|
||
|
|
||
9 years ago
|
using (var cmd = _connection.CreateCommand())
|
||
9 years ago
|
{
|
||
9 years ago
|
var cmdText = "select " + string.Join(",", _mediaStreamSaveColumns) + " from mediastreams where";
|
||
9 years ago
|
|
||
9 years ago
|
cmdText += " ItemId=@ItemId";
|
||
|
cmd.Parameters.Add(cmd, "@ItemId", DbType.Guid).Value = query.ItemId;
|
||
9 years ago
|
|
||
9 years ago
|
if (query.Type.HasValue)
|
||
|
{
|
||
|
cmdText += " AND StreamType=@StreamType";
|
||
|
cmd.Parameters.Add(cmd, "@StreamType", DbType.String).Value = query.Type.Value.ToString();
|
||
|
}
|
||
9 years ago
|
|
||
9 years ago
|
if (query.Index.HasValue)
|
||
|
{
|
||
|
cmdText += " AND StreamIndex=@StreamIndex";
|
||
|
cmd.Parameters.Add(cmd, "@StreamIndex", DbType.Int32).Value = query.Index.Value;
|
||
|
}
|
||
9 years ago
|
|
||
9 years ago
|
cmdText += " order by StreamIndex ASC";
|
||
9 years ago
|
|
||
9 years ago
|
cmd.CommandText = cmdText;
|
||
9 years ago
|
|
||
9 years ago
|
using (var reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess | CommandBehavior.SingleResult))
|
||
|
{
|
||
|
while (reader.Read())
|
||
9 years ago
|
{
|
||
9 years ago
|
list.Add(GetMediaStream(reader));
|
||
9 years ago
|
}
|
||
|
}
|
||
|
}
|
||
9 years ago
|
|
||
9 years ago
|
return list;
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
public async Task SaveMediaStreams(Guid id, List<MediaStream> streams, CancellationToken cancellationToken)
|
||
9 years ago
|
{
|
||
|
CheckDisposed();
|
||
|
|
||
|
if (id == Guid.Empty)
|
||
|
{
|
||
|
throw new ArgumentNullException("id");
|
||
|
}
|
||
|
|
||
|
if (streams == null)
|
||
|
{
|
||
|
throw new ArgumentNullException("streams");
|
||
|
}
|
||
|
|
||
|
cancellationToken.ThrowIfCancellationRequested();
|
||
|
|
||
9 years ago
|
await WriteLock.WaitAsync(cancellationToken).ConfigureAwait(false);
|
||
|
|
||
|
IDbTransaction transaction = null;
|
||
|
|
||
|
try
|
||
9 years ago
|
{
|
||
9 years ago
|
transaction = _connection.BeginTransaction();
|
||
9 years ago
|
|
||
9 years ago
|
// First delete chapters
|
||
|
_deleteStreamsCommand.GetParameter(0).Value = id;
|
||
9 years ago
|
|
||
9 years ago
|
_deleteStreamsCommand.Transaction = transaction;
|
||
9 years ago
|
|
||
9 years ago
|
_deleteStreamsCommand.ExecuteNonQuery();
|
||
9 years ago
|
|
||
9 years ago
|
foreach (var stream in streams)
|
||
9 years ago
|
{
|
||
9 years ago
|
cancellationToken.ThrowIfCancellationRequested();
|
||
|
|
||
|
var index = 0;
|
||
|
|
||
|
_saveStreamCommand.GetParameter(index++).Value = id;
|
||
|
_saveStreamCommand.GetParameter(index++).Value = stream.Index;
|
||
|
_saveStreamCommand.GetParameter(index++).Value = stream.Type.ToString();
|
||
|
_saveStreamCommand.GetParameter(index++).Value = stream.Codec;
|
||
|
_saveStreamCommand.GetParameter(index++).Value = stream.Language;
|
||
|
_saveStreamCommand.GetParameter(index++).Value = stream.ChannelLayout;
|
||
|
_saveStreamCommand.GetParameter(index++).Value = stream.Profile;
|
||
|
_saveStreamCommand.GetParameter(index++).Value = stream.AspectRatio;
|
||
|
_saveStreamCommand.GetParameter(index++).Value = stream.Path;
|
||
9 years ago
|
|
||
9 years ago
|
_saveStreamCommand.GetParameter(index++).Value = stream.IsInterlaced;
|
||
|
|
||
|
_saveStreamCommand.GetParameter(index++).Value = stream.BitRate;
|
||
|
_saveStreamCommand.GetParameter(index++).Value = stream.Channels;
|
||
|
_saveStreamCommand.GetParameter(index++).Value = stream.SampleRate;
|
||
|
|
||
|
_saveStreamCommand.GetParameter(index++).Value = stream.IsDefault;
|
||
|
_saveStreamCommand.GetParameter(index++).Value = stream.IsForced;
|
||
|
_saveStreamCommand.GetParameter(index++).Value = stream.IsExternal;
|
||
|
|
||
|
_saveStreamCommand.GetParameter(index++).Value = stream.Width;
|
||
|
_saveStreamCommand.GetParameter(index++).Value = stream.Height;
|
||
|
_saveStreamCommand.GetParameter(index++).Value = stream.AverageFrameRate;
|
||
|
_saveStreamCommand.GetParameter(index++).Value = stream.RealFrameRate;
|
||
|
_saveStreamCommand.GetParameter(index++).Value = stream.Level;
|
||
|
_saveStreamCommand.GetParameter(index++).Value = stream.PixelFormat;
|
||
|
_saveStreamCommand.GetParameter(index++).Value = stream.BitDepth;
|
||
|
_saveStreamCommand.GetParameter(index++).Value = stream.IsAnamorphic;
|
||
|
_saveStreamCommand.GetParameter(index++).Value = stream.RefFrames;
|
||
|
|
||
|
_saveStreamCommand.GetParameter(index++).Value = stream.CodecTag;
|
||
|
_saveStreamCommand.GetParameter(index++).Value = stream.Comment;
|
||
|
_saveStreamCommand.GetParameter(index++).Value = stream.NalLengthSize;
|
||
|
_saveStreamCommand.GetParameter(index++).Value = stream.IsAVC;
|
||
|
_saveStreamCommand.GetParameter(index++).Value = stream.Title;
|
||
|
|
||
|
_saveStreamCommand.GetParameter(index++).Value = stream.TimeBase;
|
||
|
_saveStreamCommand.GetParameter(index++).Value = stream.CodecTimeBase;
|
||
|
|
||
|
_saveStreamCommand.Transaction = transaction;
|
||
|
_saveStreamCommand.ExecuteNonQuery();
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
|
transaction.Commit();
|
||
|
}
|
||
|
catch (OperationCanceledException)
|
||
|
{
|
||
|
if (transaction != null)
|
||
9 years ago
|
{
|
||
9 years ago
|
transaction.Rollback();
|
||
|
}
|
||
9 years ago
|
|
||
9 years ago
|
throw;
|
||
|
}
|
||
|
catch (Exception e)
|
||
|
{
|
||
|
Logger.ErrorException("Failed to save media streams:", e);
|
||
9 years ago
|
|
||
9 years ago
|
if (transaction != null)
|
||
|
{
|
||
|
transaction.Rollback();
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
|
throw;
|
||
|
}
|
||
|
finally
|
||
|
{
|
||
|
if (transaction != null)
|
||
9 years ago
|
{
|
||
9 years ago
|
transaction.Dispose();
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
|
WriteLock.Release();
|
||
9 years ago
|
}
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// Gets the chapter.
|
||
|
/// </summary>
|
||
|
/// <param name="reader">The reader.</param>
|
||
|
/// <returns>ChapterInfo.</returns>
|
||
|
private MediaStream GetMediaStream(IDataReader reader)
|
||
|
{
|
||
|
var item = new MediaStream
|
||
|
{
|
||
|
Index = reader.GetInt32(1)
|
||
|
};
|
||
|
|
||
|
item.Type = (MediaStreamType)Enum.Parse(typeof(MediaStreamType), reader.GetString(2), true);
|
||
|
|
||
|
if (!reader.IsDBNull(3))
|
||
|
{
|
||
|
item.Codec = reader.GetString(3);
|
||
|
}
|
||
|
|
||
|
if (!reader.IsDBNull(4))
|
||
|
{
|
||
|
item.Language = reader.GetString(4);
|
||
|
}
|
||
|
|
||
|
if (!reader.IsDBNull(5))
|
||
|
{
|
||
|
item.ChannelLayout = reader.GetString(5);
|
||
|
}
|
||
|
|
||
|
if (!reader.IsDBNull(6))
|
||
|
{
|
||
|
item.Profile = reader.GetString(6);
|
||
|
}
|
||
|
|
||
|
if (!reader.IsDBNull(7))
|
||
|
{
|
||
|
item.AspectRatio = reader.GetString(7);
|
||
|
}
|
||
|
|
||
|
if (!reader.IsDBNull(8))
|
||
|
{
|
||
|
item.Path = reader.GetString(8);
|
||
|
}
|
||
|
|
||
|
item.IsInterlaced = reader.GetBoolean(9);
|
||
|
|
||
|
if (!reader.IsDBNull(10))
|
||
|
{
|
||
|
item.BitRate = reader.GetInt32(10);
|
||
|
}
|
||
|
|
||
|
if (!reader.IsDBNull(11))
|
||
|
{
|
||
|
item.Channels = reader.GetInt32(11);
|
||
|
}
|
||
|
|
||
|
if (!reader.IsDBNull(12))
|
||
|
{
|
||
|
item.SampleRate = reader.GetInt32(12);
|
||
|
}
|
||
|
|
||
|
item.IsDefault = reader.GetBoolean(13);
|
||
|
item.IsForced = reader.GetBoolean(14);
|
||
|
item.IsExternal = reader.GetBoolean(15);
|
||
|
|
||
|
if (!reader.IsDBNull(16))
|
||
|
{
|
||
|
item.Width = reader.GetInt32(16);
|
||
|
}
|
||
|
|
||
|
if (!reader.IsDBNull(17))
|
||
|
{
|
||
|
item.Height = reader.GetInt32(17);
|
||
|
}
|
||
|
|
||
|
if (!reader.IsDBNull(18))
|
||
|
{
|
||
|
item.AverageFrameRate = reader.GetFloat(18);
|
||
|
}
|
||
|
|
||
|
if (!reader.IsDBNull(19))
|
||
|
{
|
||
|
item.RealFrameRate = reader.GetFloat(19);
|
||
|
}
|
||
|
|
||
|
if (!reader.IsDBNull(20))
|
||
|
{
|
||
|
item.Level = reader.GetFloat(20);
|
||
|
}
|
||
|
|
||
|
if (!reader.IsDBNull(21))
|
||
|
{
|
||
|
item.PixelFormat = reader.GetString(21);
|
||
|
}
|
||
|
|
||
|
if (!reader.IsDBNull(22))
|
||
|
{
|
||
|
item.BitDepth = reader.GetInt32(22);
|
||
|
}
|
||
|
|
||
|
if (!reader.IsDBNull(23))
|
||
|
{
|
||
|
item.IsAnamorphic = reader.GetBoolean(23);
|
||
|
}
|
||
|
|
||
|
if (!reader.IsDBNull(24))
|
||
|
{
|
||
|
item.RefFrames = reader.GetInt32(24);
|
||
|
}
|
||
|
|
||
9 years ago
|
if (!reader.IsDBNull(25))
|
||
9 years ago
|
{
|
||
9 years ago
|
item.CodecTag = reader.GetString(25);
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
if (!reader.IsDBNull(26))
|
||
9 years ago
|
{
|
||
9 years ago
|
item.Comment = reader.GetString(26);
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
if (!reader.IsDBNull(27))
|
||
9 years ago
|
{
|
||
9 years ago
|
item.NalLengthSize = reader.GetString(27);
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
if (!reader.IsDBNull(28))
|
||
|
{
|
||
|
item.IsAVC = reader.GetBoolean(28);
|
||
|
}
|
||
|
|
||
9 years ago
|
if (!reader.IsDBNull(29))
|
||
|
{
|
||
|
item.Title = reader.GetString(29);
|
||
|
}
|
||
|
|
||
9 years ago
|
if (!reader.IsDBNull(30))
|
||
|
{
|
||
|
item.TimeBase = reader.GetString(30);
|
||
|
}
|
||
|
|
||
|
if (!reader.IsDBNull(31))
|
||
|
{
|
||
|
item.CodecTimeBase = reader.GetString(31);
|
||
|
}
|
||
|
|
||
9 years ago
|
return item;
|
||
|
}
|
||
|
|
||
12 years ago
|
}
|
||
|
}
|