Merge pull request #2755 from Bond-009/dbcleanup

Clean up SqliteItemRepository
pull/2825/head
Vasily 5 years ago committed by GitHub
commit 06c5b73c15
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -287,7 +287,7 @@ namespace Emby.Server.Implementations.Data
} }
} }
public static void TryBind(this IStatement statement, string name, byte[] value) public static void TryBind(this IStatement statement, string name, ReadOnlySpan<byte> value)
{ {
if (statement.BindParameters.TryGetValue(name, out IBindParameter bindParam)) if (statement.BindParameters.TryGetValue(name, out IBindParameter bindParam))
{ {

@ -3315,7 +3315,7 @@ namespace Emby.Server.Implementations.Data
for (int i = 0; i < str.Length; i++) for (int i = 0; i < str.Length; i++)
{ {
if (!(char.IsLetter(str[i])) && (!(char.IsNumber(str[i])))) if (!char.IsLetter(str[i]) && !char.IsNumber(str[i]))
{ {
return false; return false;
} }
@ -3339,7 +3339,7 @@ namespace Emby.Server.Implementations.Data
return IsAlphaNumeric(value); return IsAlphaNumeric(value);
} }
private List<string> GetWhereClauses(InternalItemsQuery query, IStatement statement, string paramSuffix = "") private List<string> GetWhereClauses(InternalItemsQuery query, IStatement statement)
{ {
if (query.IsResumable ?? false) if (query.IsResumable ?? false)
{ {
@ -3351,27 +3351,27 @@ namespace Emby.Server.Implementations.Data
if (query.IsHD.HasValue) if (query.IsHD.HasValue)
{ {
var threshold = 1200; const int Threshold = 1200;
if (query.IsHD.Value) if (query.IsHD.Value)
{ {
minWidth = threshold; minWidth = Threshold;
} }
else else
{ {
maxWidth = threshold - 1; maxWidth = Threshold - 1;
} }
} }
if (query.Is4K.HasValue) if (query.Is4K.HasValue)
{ {
var threshold = 3800; const int Threshold = 3800;
if (query.Is4K.Value) if (query.Is4K.Value)
{ {
minWidth = threshold; minWidth = Threshold;
} }
else else
{ {
maxWidth = threshold - 1; maxWidth = Threshold - 1;
} }
} }
@ -3380,93 +3380,61 @@ namespace Emby.Server.Implementations.Data
if (minWidth.HasValue) if (minWidth.HasValue)
{ {
whereClauses.Add("Width>=@MinWidth"); whereClauses.Add("Width>=@MinWidth");
if (statement != null) statement?.TryBind("@MinWidth", minWidth);
{
statement.TryBind("@MinWidth", minWidth);
}
} }
if (query.MinHeight.HasValue) if (query.MinHeight.HasValue)
{ {
whereClauses.Add("Height>=@MinHeight"); whereClauses.Add("Height>=@MinHeight");
if (statement != null) statement?.TryBind("@MinHeight", query.MinHeight);
{
statement.TryBind("@MinHeight", query.MinHeight);
}
} }
if (maxWidth.HasValue) if (maxWidth.HasValue)
{ {
whereClauses.Add("Width<=@MaxWidth"); whereClauses.Add("Width<=@MaxWidth");
if (statement != null) statement?.TryBind("@MaxWidth", maxWidth);
{
statement.TryBind("@MaxWidth", maxWidth);
}
} }
if (query.MaxHeight.HasValue) if (query.MaxHeight.HasValue)
{ {
whereClauses.Add("Height<=@MaxHeight"); whereClauses.Add("Height<=@MaxHeight");
if (statement != null) statement?.TryBind("@MaxHeight", query.MaxHeight);
{
statement.TryBind("@MaxHeight", query.MaxHeight);
}
} }
if (query.IsLocked.HasValue) if (query.IsLocked.HasValue)
{ {
whereClauses.Add("IsLocked=@IsLocked"); whereClauses.Add("IsLocked=@IsLocked");
if (statement != null) statement?.TryBind("@IsLocked", query.IsLocked);
{
statement.TryBind("@IsLocked", query.IsLocked);
}
} }
var tags = query.Tags.ToList(); var tags = query.Tags.ToList();
var excludeTags = query.ExcludeTags.ToList(); var excludeTags = query.ExcludeTags.ToList();
if (query.IsMovie ?? false) if (query.IsMovie == true)
{
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(nameof(Movie))
if (query.IncludeItemTypes.Length == 0 || query.IncludeItemTypes.Contains(typeof(Trailer).Name)) || query.IncludeItemTypes.Contains(nameof(Trailer)))
{ {
alternateTypes.Add(typeof(Trailer).FullName); whereClauses.Add("(IsMovie is null OR IsMovie=@IsMovie)");
}
var programAttribtues = new List<string>();
if (alternateTypes.Count == 0)
{
programAttribtues.Add("IsMovie=@IsMovie");
} }
else else
{ {
programAttribtues.Add("(IsMovie is null OR IsMovie=@IsMovie)"); whereClauses.Add("IsMovie=@IsMovie");
}
if (statement != null)
{
statement.TryBind("@IsMovie", true);
} }
whereClauses.Add("(" + string.Join(" OR ", programAttribtues) + ")"); statement?.TryBind("@IsMovie", true);
} }
else if (query.IsMovie.HasValue) else if (query.IsMovie.HasValue)
{ {
whereClauses.Add("IsMovie=@IsMovie"); whereClauses.Add("IsMovie=@IsMovie");
if (statement != null) statement?.TryBind("@IsMovie", query.IsMovie);
{
statement.TryBind("@IsMovie", query.IsMovie);
}
} }
if (query.IsSeries.HasValue) if (query.IsSeries.HasValue)
{ {
whereClauses.Add("IsSeries=@IsSeries"); whereClauses.Add("IsSeries=@IsSeries");
if (statement != null) statement?.TryBind("@IsSeries", query.IsSeries);
{
statement.TryBind("@IsSeries", query.IsSeries);
}
} }
if (query.IsSports.HasValue) if (query.IsSports.HasValue)
@ -3518,10 +3486,7 @@ namespace Emby.Server.Implementations.Data
if (query.IsFolder.HasValue) if (query.IsFolder.HasValue)
{ {
whereClauses.Add("IsFolder=@IsFolder"); whereClauses.Add("IsFolder=@IsFolder");
if (statement != null) statement?.TryBind("@IsFolder", query.IsFolder);
{
statement.TryBind("@IsFolder", query.IsFolder);
}
} }
var includeTypes = query.IncludeItemTypes.SelectMany(MapIncludeItemTypes).ToArray(); var includeTypes = query.IncludeItemTypes.SelectMany(MapIncludeItemTypes).ToArray();
@ -3532,10 +3497,7 @@ namespace Emby.Server.Implementations.Data
if (excludeTypes.Length == 1) if (excludeTypes.Length == 1)
{ {
whereClauses.Add("type<>@type"); whereClauses.Add("type<>@type");
if (statement != null) statement?.TryBind("@type", excludeTypes[0]);
{
statement.TryBind("@type", excludeTypes[0]);
}
} }
else if (excludeTypes.Length > 1) else if (excludeTypes.Length > 1)
{ {
@ -3546,10 +3508,7 @@ namespace Emby.Server.Implementations.Data
else if (includeTypes.Length == 1) else if (includeTypes.Length == 1)
{ {
whereClauses.Add("type=@type"); whereClauses.Add("type=@type");
if (statement != null) statement?.TryBind("@type", includeTypes[0]);
{
statement.TryBind("@type", includeTypes[0]);
}
} }
else if (includeTypes.Length > 1) else if (includeTypes.Length > 1)
{ {
@ -3560,10 +3519,7 @@ namespace Emby.Server.Implementations.Data
if (query.ChannelIds.Length == 1) if (query.ChannelIds.Length == 1)
{ {
whereClauses.Add("ChannelId=@ChannelId"); whereClauses.Add("ChannelId=@ChannelId");
if (statement != null) statement?.TryBind("@ChannelId", query.ChannelIds[0].ToString("N", CultureInfo.InvariantCulture));
{
statement.TryBind("@ChannelId", query.ChannelIds[0].ToString("N", CultureInfo.InvariantCulture));
}
} }
else if (query.ChannelIds.Length > 1) else if (query.ChannelIds.Length > 1)
{ {
@ -3574,98 +3530,65 @@ namespace Emby.Server.Implementations.Data
if (!query.ParentId.Equals(Guid.Empty)) if (!query.ParentId.Equals(Guid.Empty))
{ {
whereClauses.Add("ParentId=@ParentId"); whereClauses.Add("ParentId=@ParentId");
if (statement != null) statement?.TryBind("@ParentId", query.ParentId);
{
statement.TryBind("@ParentId", query.ParentId);
}
} }
if (!string.IsNullOrWhiteSpace(query.Path)) if (!string.IsNullOrWhiteSpace(query.Path))
{ {
whereClauses.Add("Path=@Path"); whereClauses.Add("Path=@Path");
if (statement != null) statement?.TryBind("@Path", GetPathToSave(query.Path));
{
statement.TryBind("@Path", GetPathToSave(query.Path));
}
} }
if (!string.IsNullOrWhiteSpace(query.PresentationUniqueKey)) if (!string.IsNullOrWhiteSpace(query.PresentationUniqueKey))
{ {
whereClauses.Add("PresentationUniqueKey=@PresentationUniqueKey"); whereClauses.Add("PresentationUniqueKey=@PresentationUniqueKey");
if (statement != null) statement?.TryBind("@PresentationUniqueKey", query.PresentationUniqueKey);
{
statement.TryBind("@PresentationUniqueKey", query.PresentationUniqueKey);
}
} }
if (query.MinCommunityRating.HasValue) if (query.MinCommunityRating.HasValue)
{ {
whereClauses.Add("CommunityRating>=@MinCommunityRating"); whereClauses.Add("CommunityRating>=@MinCommunityRating");
if (statement != null) statement?.TryBind("@MinCommunityRating", query.MinCommunityRating.Value);
{
statement.TryBind("@MinCommunityRating", query.MinCommunityRating.Value);
}
} }
if (query.MinIndexNumber.HasValue) if (query.MinIndexNumber.HasValue)
{ {
whereClauses.Add("IndexNumber>=@MinIndexNumber"); whereClauses.Add("IndexNumber>=@MinIndexNumber");
if (statement != null) statement?.TryBind("@MinIndexNumber", query.MinIndexNumber.Value);
{
statement.TryBind("@MinIndexNumber", query.MinIndexNumber.Value);
}
} }
if (query.MinDateCreated.HasValue) if (query.MinDateCreated.HasValue)
{ {
whereClauses.Add("DateCreated>=@MinDateCreated"); whereClauses.Add("DateCreated>=@MinDateCreated");
if (statement != null) statement?.TryBind("@MinDateCreated", query.MinDateCreated.Value);
{
statement.TryBind("@MinDateCreated", query.MinDateCreated.Value);
}
} }
if (query.MinDateLastSaved.HasValue) if (query.MinDateLastSaved.HasValue)
{ {
whereClauses.Add("(DateLastSaved not null and DateLastSaved>=@MinDateLastSavedForUser)"); whereClauses.Add("(DateLastSaved not null and DateLastSaved>=@MinDateLastSavedForUser)");
if (statement != null) statement?.TryBind("@MinDateLastSaved", query.MinDateLastSaved.Value);
{
statement.TryBind("@MinDateLastSaved", query.MinDateLastSaved.Value);
}
} }
if (query.MinDateLastSavedForUser.HasValue) if (query.MinDateLastSavedForUser.HasValue)
{ {
whereClauses.Add("(DateLastSaved not null and DateLastSaved>=@MinDateLastSavedForUser)"); whereClauses.Add("(DateLastSaved not null and DateLastSaved>=@MinDateLastSavedForUser)");
if (statement != null) statement?.TryBind("@MinDateLastSavedForUser", query.MinDateLastSavedForUser.Value);
{
statement.TryBind("@MinDateLastSavedForUser", query.MinDateLastSavedForUser.Value);
}
} }
if (query.IndexNumber.HasValue) if (query.IndexNumber.HasValue)
{ {
whereClauses.Add("IndexNumber=@IndexNumber"); whereClauses.Add("IndexNumber=@IndexNumber");
if (statement != null) statement?.TryBind("@IndexNumber", query.IndexNumber.Value);
{
statement.TryBind("@IndexNumber", query.IndexNumber.Value);
}
} }
if (query.ParentIndexNumber.HasValue) if (query.ParentIndexNumber.HasValue)
{ {
whereClauses.Add("ParentIndexNumber=@ParentIndexNumber"); whereClauses.Add("ParentIndexNumber=@ParentIndexNumber");
if (statement != null) statement?.TryBind("@ParentIndexNumber", query.ParentIndexNumber.Value);
{
statement.TryBind("@ParentIndexNumber", query.ParentIndexNumber.Value);
}
} }
if (query.ParentIndexNumberNotEquals.HasValue) if (query.ParentIndexNumberNotEquals.HasValue)
{ {
whereClauses.Add("(ParentIndexNumber<>@ParentIndexNumberNotEquals or ParentIndexNumber is null)"); whereClauses.Add("(ParentIndexNumber<>@ParentIndexNumberNotEquals or ParentIndexNumber is null)");
if (statement != null) statement?.TryBind("@ParentIndexNumberNotEquals", query.ParentIndexNumberNotEquals.Value);
{
statement.TryBind("@ParentIndexNumberNotEquals", query.ParentIndexNumberNotEquals.Value);
}
} }
var minEndDate = query.MinEndDate; var minEndDate = query.MinEndDate;
@ -3686,73 +3609,59 @@ namespace Emby.Server.Implementations.Data
if (minEndDate.HasValue) if (minEndDate.HasValue)
{ {
whereClauses.Add("EndDate>=@MinEndDate"); whereClauses.Add("EndDate>=@MinEndDate");
if (statement != null) statement?.TryBind("@MinEndDate", minEndDate.Value);
{
statement.TryBind("@MinEndDate", minEndDate.Value);
}
} }
if (maxEndDate.HasValue) if (maxEndDate.HasValue)
{ {
whereClauses.Add("EndDate<=@MaxEndDate"); whereClauses.Add("EndDate<=@MaxEndDate");
if (statement != null) statement?.TryBind("@MaxEndDate", maxEndDate.Value);
{
statement.TryBind("@MaxEndDate", maxEndDate.Value);
}
} }
if (query.MinStartDate.HasValue) if (query.MinStartDate.HasValue)
{ {
whereClauses.Add("StartDate>=@MinStartDate"); whereClauses.Add("StartDate>=@MinStartDate");
if (statement != null) statement?.TryBind("@MinStartDate", query.MinStartDate.Value);
{
statement.TryBind("@MinStartDate", query.MinStartDate.Value);
}
} }
if (query.MaxStartDate.HasValue) if (query.MaxStartDate.HasValue)
{ {
whereClauses.Add("StartDate<=@MaxStartDate"); whereClauses.Add("StartDate<=@MaxStartDate");
if (statement != null) statement?.TryBind("@MaxStartDate", query.MaxStartDate.Value);
{
statement.TryBind("@MaxStartDate", query.MaxStartDate.Value);
}
} }
if (query.MinPremiereDate.HasValue) if (query.MinPremiereDate.HasValue)
{ {
whereClauses.Add("PremiereDate>=@MinPremiereDate"); whereClauses.Add("PremiereDate>=@MinPremiereDate");
if (statement != null) statement?.TryBind("@MinPremiereDate", query.MinPremiereDate.Value);
{
statement.TryBind("@MinPremiereDate", query.MinPremiereDate.Value);
}
} }
if (query.MaxPremiereDate.HasValue) if (query.MaxPremiereDate.HasValue)
{ {
whereClauses.Add("PremiereDate<=@MaxPremiereDate"); whereClauses.Add("PremiereDate<=@MaxPremiereDate");
if (statement != null) statement?.TryBind("@MaxPremiereDate", query.MaxPremiereDate.Value);
{
statement.TryBind("@MaxPremiereDate", query.MaxPremiereDate.Value);
}
} }
if (query.TrailerTypes.Length > 0) var trailerTypes = query.TrailerTypes;
{ int trailerTypesLen = trailerTypes.Length;
var clauses = new List<string>(); if (trailerTypesLen > 0)
var index = 0;
foreach (var type in query.TrailerTypes)
{ {
var paramName = "@TrailerTypes" + index; const string Or = " OR ";
StringBuilder clause = new StringBuilder("(", trailerTypesLen * 32);
clauses.Add("TrailerTypes like " + paramName); for (int i = 0; i < trailerTypesLen; i++)
if (statement != null)
{ {
statement.TryBind(paramName, "%" + type + "%"); var paramName = "@TrailerTypes" + i;
} clause.Append("TrailerTypes like ")
index++; .Append(paramName)
.Append(Or);
statement?.TryBind(paramName, "%" + trailerTypes[i] + "%");
} }
var clause = "(" + string.Join(" OR ", clauses) + ")";
whereClauses.Add(clause); // Remove last " OR "
clause.Length -= Or.Length;
clause.Append(')');
whereClauses.Add(clause.ToString());
} }
if (query.IsAiring.HasValue) if (query.IsAiring.HasValue)
@ -3760,24 +3669,15 @@ namespace Emby.Server.Implementations.Data
if (query.IsAiring.Value) if (query.IsAiring.Value)
{ {
whereClauses.Add("StartDate<=@MaxStartDate"); whereClauses.Add("StartDate<=@MaxStartDate");
if (statement != null) statement?.TryBind("@MaxStartDate", DateTime.UtcNow);
{
statement.TryBind("@MaxStartDate", DateTime.UtcNow);
}
whereClauses.Add("EndDate>=@MinEndDate"); whereClauses.Add("EndDate>=@MinEndDate");
if (statement != null) statement?.TryBind("@MinEndDate", DateTime.UtcNow);
{
statement.TryBind("@MinEndDate", DateTime.UtcNow);
}
} }
else else
{ {
whereClauses.Add("(StartDate>@IsAiringDate OR EndDate < @IsAiringDate)"); whereClauses.Add("(StartDate>@IsAiringDate OR EndDate < @IsAiringDate)");
if (statement != null) statement?.TryBind("@IsAiringDate", DateTime.UtcNow);
{
statement.TryBind("@IsAiringDate", DateTime.UtcNow);
}
} }
} }
@ -3792,13 +3692,10 @@ namespace Emby.Server.Implementations.Data
var paramName = "@PersonId" + index; var paramName = "@PersonId" + index;
clauses.Add("(guid in (select itemid from People where Name = (select Name from TypedBaseItems where guid=" + paramName + ")))"); clauses.Add("(guid in (select itemid from People where Name = (select Name from TypedBaseItems where guid=" + paramName + ")))");
statement?.TryBind(paramName, personId.ToByteArray());
if (statement != null)
{
statement.TryBind(paramName, personId.ToByteArray());
}
index++; index++;
} }
var clause = "(" + string.Join(" OR ", clauses) + ")"; var clause = "(" + string.Join(" OR ", clauses) + ")";
whereClauses.Add(clause); whereClauses.Add(clause);
} }
@ -3806,47 +3703,31 @@ namespace Emby.Server.Implementations.Data
if (!string.IsNullOrWhiteSpace(query.Person)) if (!string.IsNullOrWhiteSpace(query.Person))
{ {
whereClauses.Add("Guid in (select ItemId from People where Name=@PersonName)"); whereClauses.Add("Guid in (select ItemId from People where Name=@PersonName)");
if (statement != null) statement?.TryBind("@PersonName", query.Person);
{
statement.TryBind("@PersonName", query.Person);
}
} }
if (!string.IsNullOrWhiteSpace(query.MinSortName)) if (!string.IsNullOrWhiteSpace(query.MinSortName))
{ {
whereClauses.Add("SortName>=@MinSortName"); whereClauses.Add("SortName>=@MinSortName");
if (statement != null) statement?.TryBind("@MinSortName", query.MinSortName);
{
statement.TryBind("@MinSortName", query.MinSortName);
}
} }
if (!string.IsNullOrWhiteSpace(query.ExternalSeriesId)) if (!string.IsNullOrWhiteSpace(query.ExternalSeriesId))
{ {
whereClauses.Add("ExternalSeriesId=@ExternalSeriesId"); whereClauses.Add("ExternalSeriesId=@ExternalSeriesId");
if (statement != null) statement?.TryBind("@ExternalSeriesId", query.ExternalSeriesId);
{
statement.TryBind("@ExternalSeriesId", query.ExternalSeriesId);
}
} }
if (!string.IsNullOrWhiteSpace(query.ExternalId)) if (!string.IsNullOrWhiteSpace(query.ExternalId))
{ {
whereClauses.Add("ExternalId=@ExternalId"); whereClauses.Add("ExternalId=@ExternalId");
if (statement != null) statement?.TryBind("@ExternalId", query.ExternalId);
{
statement.TryBind("@ExternalId", query.ExternalId);
}
} }
if (!string.IsNullOrWhiteSpace(query.Name)) if (!string.IsNullOrWhiteSpace(query.Name))
{ {
whereClauses.Add("CleanName=@Name"); whereClauses.Add("CleanName=@Name");
statement?.TryBind("@Name", GetCleanValue(query.Name));
if (statement != null)
{
statement.TryBind("@Name", GetCleanValue(query.Name));
}
} }
// These are the same, for now // These are the same, for now
@ -3865,28 +3746,21 @@ namespace Emby.Server.Implementations.Data
if (!string.IsNullOrWhiteSpace(query.NameStartsWith)) if (!string.IsNullOrWhiteSpace(query.NameStartsWith))
{ {
whereClauses.Add("SortName like @NameStartsWith"); whereClauses.Add("SortName like @NameStartsWith");
if (statement != null) statement?.TryBind("@NameStartsWith", query.NameStartsWith + "%");
{
statement.TryBind("@NameStartsWith", query.NameStartsWith + "%");
}
} }
if (!string.IsNullOrWhiteSpace(query.NameStartsWithOrGreater)) if (!string.IsNullOrWhiteSpace(query.NameStartsWithOrGreater))
{ {
whereClauses.Add("SortName >= @NameStartsWithOrGreater"); whereClauses.Add("SortName >= @NameStartsWithOrGreater");
// lowercase this because SortName is stored as lowercase // lowercase this because SortName is stored as lowercase
if (statement != null) statement?.TryBind("@NameStartsWithOrGreater", query.NameStartsWithOrGreater.ToLowerInvariant());
{
statement.TryBind("@NameStartsWithOrGreater", query.NameStartsWithOrGreater.ToLowerInvariant());
}
} }
if (!string.IsNullOrWhiteSpace(query.NameLessThan)) if (!string.IsNullOrWhiteSpace(query.NameLessThan))
{ {
whereClauses.Add("SortName < @NameLessThan"); whereClauses.Add("SortName < @NameLessThan");
// lowercase this because SortName is stored as lowercase // lowercase this because SortName is stored as lowercase
if (statement != null) statement?.TryBind("@NameLessThan", query.NameLessThan.ToLowerInvariant());
{
statement.TryBind("@NameLessThan", query.NameLessThan.ToLowerInvariant());
}
} }
if (query.ImageTypes.Length > 0) if (query.ImageTypes.Length > 0)
@ -3902,18 +3776,12 @@ namespace Emby.Server.Implementations.Data
if (query.IsLiked.Value) if (query.IsLiked.Value)
{ {
whereClauses.Add("rating>=@UserRating"); whereClauses.Add("rating>=@UserRating");
if (statement != null) statement?.TryBind("@UserRating", UserItemData.MinLikeValue);
{
statement.TryBind("@UserRating", UserItemData.MinLikeValue);
}
} }
else else
{ {
whereClauses.Add("(rating is null or rating<@UserRating)"); whereClauses.Add("(rating is null or rating<@UserRating)");
if (statement != null) statement?.TryBind("@UserRating", UserItemData.MinLikeValue);
{
statement.TryBind("@UserRating", UserItemData.MinLikeValue);
}
} }
} }
@ -3927,10 +3795,8 @@ namespace Emby.Server.Implementations.Data
{ {
whereClauses.Add("(IsFavorite is null or IsFavorite=@IsFavoriteOrLiked)"); whereClauses.Add("(IsFavorite is null or IsFavorite=@IsFavoriteOrLiked)");
} }
if (statement != null)
{ statement?.TryBind("@IsFavoriteOrLiked", query.IsFavoriteOrLiked.Value);
statement.TryBind("@IsFavoriteOrLiked", query.IsFavoriteOrLiked.Value);
}
} }
if (query.IsFavorite.HasValue) if (query.IsFavorite.HasValue)
@ -3943,10 +3809,8 @@ namespace Emby.Server.Implementations.Data
{ {
whereClauses.Add("(IsFavorite is null or IsFavorite=@IsFavorite)"); whereClauses.Add("(IsFavorite is null or IsFavorite=@IsFavorite)");
} }
if (statement != null)
{ statement?.TryBind("@IsFavorite", query.IsFavorite.Value);
statement.TryBind("@IsFavorite", query.IsFavorite.Value);
}
} }
if (EnableJoinUserData(query)) if (EnableJoinUserData(query))
@ -3975,10 +3839,8 @@ namespace Emby.Server.Implementations.Data
{ {
whereClauses.Add("(played is null or played=@IsPlayed)"); whereClauses.Add("(played is null or played=@IsPlayed)");
} }
if (statement != null)
{ statement?.TryBind("@IsPlayed", query.IsPlayed.Value);
statement.TryBind("@IsPlayed", query.IsPlayed.Value);
}
} }
} }
} }
@ -4010,6 +3872,7 @@ namespace Emby.Server.Implementations.Data
} }
index++; index++;
} }
var clause = "(" + string.Join(" OR ", clauses) + ")"; var clause = "(" + string.Join(" OR ", clauses) + ")";
whereClauses.Add(clause); whereClauses.Add(clause);
} }
@ -4029,6 +3892,7 @@ namespace Emby.Server.Implementations.Data
} }
index++; index++;
} }
var clause = "(" + string.Join(" OR ", clauses) + ")"; var clause = "(" + string.Join(" OR ", clauses) + ")";
whereClauses.Add(clause); whereClauses.Add(clause);
} }
@ -4762,18 +4626,22 @@ namespace Emby.Server.Implementations.Data
{ {
list.Add(typeof(Person).Name); list.Add(typeof(Person).Name);
} }
if (IsTypeInQuery(typeof(Genre).Name, query)) if (IsTypeInQuery(typeof(Genre).Name, query))
{ {
list.Add(typeof(Genre).Name); list.Add(typeof(Genre).Name);
} }
if (IsTypeInQuery(typeof(MusicGenre).Name, query)) if (IsTypeInQuery(typeof(MusicGenre).Name, query))
{ {
list.Add(typeof(MusicGenre).Name); list.Add(typeof(MusicGenre).Name);
} }
if (IsTypeInQuery(typeof(MusicArtist).Name, query)) if (IsTypeInQuery(typeof(MusicArtist).Name, query))
{ {
list.Add(typeof(MusicArtist).Name); list.Add(typeof(MusicArtist).Name);
} }
if (IsTypeInQuery(typeof(Studio).Name, query)) if (IsTypeInQuery(typeof(Studio).Name, query))
{ {
list.Add(typeof(Studio).Name); list.Add(typeof(Studio).Name);
@ -4847,7 +4715,7 @@ namespace Emby.Server.Implementations.Data
return false; return false;
} }
private static readonly Type[] KnownTypes = private static readonly Type[] _knownTypes =
{ {
typeof(LiveTvProgram), typeof(LiveTvProgram),
typeof(LiveTvChannel), typeof(LiveTvChannel),
@ -4916,7 +4784,7 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type
{ {
var dict = new Dictionary<string, string[]>(StringComparer.OrdinalIgnoreCase); var dict = new Dictionary<string, string[]>(StringComparer.OrdinalIgnoreCase);
foreach (var t in KnownTypes) foreach (var t in _knownTypes)
{ {
dict[t.Name] = new[] { t.FullName }; dict[t.Name] = new[] { t.FullName };
} }
@ -4928,7 +4796,7 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type
} }
// Not crazy about having this all the way down here, but at least it's in one place // Not crazy about having this all the way down here, but at least it's in one place
readonly Dictionary<string, string[]> _types = GetTypeMapDictionary(); private readonly Dictionary<string, string[]> _types = GetTypeMapDictionary();
private string[] MapIncludeItemTypes(string value) private string[] MapIncludeItemTypes(string value)
{ {
@ -4945,7 +4813,7 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type
return Array.Empty<string>(); return Array.Empty<string>();
} }
public void DeleteItem(Guid id, CancellationToken cancellationToken) public void DeleteItem(Guid id)
{ {
if (id == Guid.Empty) if (id == Guid.Empty)
{ {
@ -4981,7 +4849,7 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type
} }
} }
private void ExecuteWithSingleParam(IDatabaseConnection db, string query, byte[] value) private void ExecuteWithSingleParam(IDatabaseConnection db, string query, ReadOnlySpan<byte> value)
{ {
using (var statement = PrepareStatement(db, query)) using (var statement = PrepareStatement(db, query))
{ {
@ -5541,6 +5409,7 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type
{ {
GetWhereClauses(typeSubQuery, null); GetWhereClauses(typeSubQuery, null);
} }
BindSimilarParams(query, statement); BindSimilarParams(query, statement);
BindSearchParams(query, statement); BindSearchParams(query, statement);
GetWhereClauses(innerQuery, statement); GetWhereClauses(innerQuery, statement);
@ -5582,7 +5451,7 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type
} }
var allTypes = typeString.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries) var allTypes = typeString.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries)
.ToLookup(i => i); .ToLookup(x => x);
foreach (var type in allTypes) foreach (var type in allTypes)
{ {
@ -5673,30 +5542,26 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type
private void InsertItemValues(byte[] idBlob, List<(int, string)> values, IDatabaseConnection db) private void InsertItemValues(byte[] idBlob, List<(int, string)> values, IDatabaseConnection db)
{ {
const int Limit = 100;
var startIndex = 0; var startIndex = 0;
var limit = 100;
while (startIndex < values.Count) while (startIndex < values.Count)
{ {
var insertText = new StringBuilder("insert into ItemValues (ItemId, Type, Value, CleanValue) values "); var insertText = new StringBuilder("insert into ItemValues (ItemId, Type, Value, CleanValue) values ");
var endIndex = Math.Min(values.Count, startIndex + limit); var endIndex = Math.Min(values.Count, startIndex + Limit);
var isSubsequentRow = false;
for (var i = startIndex; i < endIndex; i++) for (var i = startIndex; i < endIndex; i++)
{ {
if (isSubsequentRow)
{
insertText.Append(',');
}
insertText.AppendFormat( insertText.AppendFormat(
CultureInfo.InvariantCulture, CultureInfo.InvariantCulture,
"(@ItemId, @Type{0}, @Value{0}, @CleanValue{0})", "(@ItemId, @Type{0}, @Value{0}, @CleanValue{0}),",
i); i);
isSubsequentRow = true;
} }
// Remove last comma
insertText.Length--;
using (var statement = PrepareStatement(db, insertText.ToString())) using (var statement = PrepareStatement(db, insertText.ToString()))
{ {
statement.TryBind("@ItemId", idBlob); statement.TryBind("@ItemId", idBlob);
@ -5724,7 +5589,7 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type
statement.MoveNext(); statement.MoveNext();
} }
startIndex += limit; startIndex += Limit;
} }
} }
@ -5759,27 +5624,22 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type
private void InsertPeople(byte[] idBlob, List<PersonInfo> people, IDatabaseConnection db) private void InsertPeople(byte[] idBlob, List<PersonInfo> people, IDatabaseConnection db)
{ {
const int Limit = 100;
var startIndex = 0; var startIndex = 0;
var limit = 100;
var listIndex = 0; var listIndex = 0;
while (startIndex < people.Count) while (startIndex < people.Count)
{ {
var insertText = new StringBuilder("insert into People (ItemId, Name, Role, PersonType, SortOrder, ListOrder) values "); var insertText = new StringBuilder("insert into People (ItemId, Name, Role, PersonType, SortOrder, ListOrder) values ");
var endIndex = Math.Min(people.Count, startIndex + limit); var endIndex = Math.Min(people.Count, startIndex + Limit);
var isSubsequentRow = false;
for (var i = startIndex; i < endIndex; i++) for (var i = startIndex; i < endIndex; i++)
{ {
if (isSubsequentRow) insertText.AppendFormat("(@ItemId, @Name{0}, @Role{0}, @PersonType{0}, @SortOrder{0}, @ListOrder{0}),", i.ToString(CultureInfo.InvariantCulture));
{
insertText.Append(',');
} }
insertText.AppendFormat("(@ItemId, @Name{0}, @Role{0}, @PersonType{0}, @SortOrder{0}, @ListOrder{0})", i.ToString(CultureInfo.InvariantCulture)); // Remove last comma
isSubsequentRow = true; insertText.Length--;
}
using (var statement = PrepareStatement(db, insertText.ToString())) using (var statement = PrepareStatement(db, insertText.ToString()))
{ {
@ -5804,16 +5664,17 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type
statement.MoveNext(); statement.MoveNext();
} }
startIndex += limit; startIndex += Limit;
} }
} }
private PersonInfo GetPerson(IReadOnlyList<IResultSetValue> reader) private PersonInfo GetPerson(IReadOnlyList<IResultSetValue> reader)
{ {
var item = new PersonInfo(); var item = new PersonInfo
{
item.ItemId = reader.GetGuid(0); ItemId = reader.GetGuid(0),
item.Name = reader.GetString(1); Name = reader.GetString(1)
};
if (!reader.IsDBNull(2)) if (!reader.IsDBNull(2))
{ {
@ -5920,20 +5781,28 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type
private void InsertMediaStreams(byte[] idBlob, List<MediaStream> streams, IDatabaseConnection db) private void InsertMediaStreams(byte[] idBlob, List<MediaStream> streams, IDatabaseConnection db)
{ {
const int Limit = 10;
var startIndex = 0; var startIndex = 0;
var limit = 10;
while (startIndex < streams.Count) while (startIndex < streams.Count)
{ {
var insertText = new StringBuilder(string.Format("insert into mediastreams ({0}) values ", string.Join(",", _mediaStreamSaveColumns))); var insertText = new StringBuilder("insert into mediastreams (");
foreach (var column in _mediaStreamSaveColumns)
{
insertText.Append(column).Append(',');
}
// Remove last comma
insertText.Length--;
insertText.Append(") values ");
var endIndex = Math.Min(streams.Count, startIndex + limit); var endIndex = Math.Min(streams.Count, startIndex + Limit);
for (var i = startIndex; i < endIndex; i++) for (var i = startIndex; i < endIndex; i++)
{ {
if (i != startIndex) if (i != startIndex)
{ {
insertText.Append(","); insertText.Append(',');
} }
var index = i.ToString(CultureInfo.InvariantCulture); var index = i.ToString(CultureInfo.InvariantCulture);
@ -5941,11 +5810,12 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type
foreach (var column in _mediaStreamSaveColumns.Skip(1)) foreach (var column in _mediaStreamSaveColumns.Skip(1))
{ {
insertText.Append("@" + column + index + ","); insertText.Append('@').Append(column).Append(index).Append(',');
} }
insertText.Length -= 1; // Remove the last comma insertText.Length -= 1; // Remove the last comma
insertText.Append(")"); insertText.Append(')');
} }
using (var statement = PrepareStatement(db, insertText.ToString())) using (var statement = PrepareStatement(db, insertText.ToString()))
@ -6007,7 +5877,7 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type
statement.MoveNext(); statement.MoveNext();
} }
startIndex += limit; startIndex += Limit;
} }
} }
@ -6024,7 +5894,7 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type
Index = reader[1].ToInt() Index = reader[1].ToInt()
}; };
item.Type = (MediaStreamType)Enum.Parse(typeof(MediaStreamType), reader[2].ToString(), true); item.Type = Enum.Parse<MediaStreamType>(reader[2].ToString(), true);
if (reader[3].SQLiteType != SQLiteType.Null) if (reader[3].SQLiteType != SQLiteType.Null)
{ {

@ -437,10 +437,10 @@ namespace Emby.Server.Implementations.Library
item.SetParent(null); item.SetParent(null);
ItemRepository.DeleteItem(item.Id, CancellationToken.None); ItemRepository.DeleteItem(item.Id);
foreach (var child in children) foreach (var child in children)
{ {
ItemRepository.DeleteItem(child.Id, CancellationToken.None); ItemRepository.DeleteItem(child.Id);
} }
_libraryItemsCache.TryRemove(item.Id, out BaseItem removed); _libraryItemsCache.TryRemove(item.Id, out BaseItem removed);

@ -24,8 +24,7 @@ namespace MediaBrowser.Controller.Persistence
/// Deletes the item. /// Deletes the item.
/// </summary> /// </summary>
/// <param name="id">The identifier.</param> /// <param name="id">The identifier.</param>
/// <param name="cancellationToken">The cancellation token.</param> void DeleteItem(Guid id);
void DeleteItem(Guid id, CancellationToken cancellationToken);
/// <summary> /// <summary>
/// Saves the items. /// Saves the items.
@ -169,4 +168,3 @@ namespace MediaBrowser.Controller.Persistence
List<string> GetAllArtistNames(); List<string> GetAllArtistNames();
} }
} }

Loading…
Cancel
Save