From 493229cc15ea865d151b54d5d6d5c1eea831219a Mon Sep 17 00:00:00 2001 From: cvium Date: Mon, 21 Aug 2023 12:27:07 +0200 Subject: [PATCH] fix guid blobs --- .../Data/SqliteExtensions.cs | 20 ++++---- .../Data/SqliteItemRepository.cs | 51 +++++++------------ 2 files changed, 29 insertions(+), 42 deletions(-) diff --git a/Emby.Server.Implementations/Data/SqliteExtensions.cs b/Emby.Server.Implementations/Data/SqliteExtensions.cs index 9e5cb17027..541153af2d 100644 --- a/Emby.Server.Implementations/Data/SqliteExtensions.cs +++ b/Emby.Server.Implementations/Data/SqliteExtensions.cs @@ -264,17 +264,10 @@ namespace Emby.Server.Implementations.Data public static void TryBind(this SqliteCommand statement, string name, Guid value) { - if (statement.Parameters.Contains(name)) - { - statement.Parameters[name].Value = value; - } - else - { - statement.Parameters.Add(new SqliteParameter(name, SqliteType.Blob) { Value = value }); - } + statement.TryBind(name, value, true); } - public static void TryBind(this SqliteCommand statement, string name, object? value) + public static void TryBind(this SqliteCommand statement, string name, object? value, bool isBlob = false) { var preparedValue = value ?? DBNull.Value; if (statement.Parameters.Contains(name)) @@ -283,7 +276,14 @@ namespace Emby.Server.Implementations.Data } else { - statement.Parameters.AddWithValue(name, preparedValue); + if (isBlob) + { + statement.Parameters.Add(new SqliteParameter(name, SqliteType.Blob) { Value = value }); + } + else + { + statement.Parameters.AddWithValue(name, preparedValue); + } } } diff --git a/Emby.Server.Implementations/Data/SqliteItemRepository.cs b/Emby.Server.Implementations/Data/SqliteItemRepository.cs index 4ceaafa128..f3ad1121a2 100644 --- a/Emby.Server.Implementations/Data/SqliteItemRepository.cs +++ b/Emby.Server.Implementations/Data/SqliteItemRepository.cs @@ -4891,11 +4891,6 @@ AND Type = @InternalPersonType)"); CheckDisposed(); - // TODO how to handle span? - Span itemIdBlob2 = stackalloc byte[16]; - itemId.TryWriteBytes(itemIdBlob2); - var itemIdBlob = Encoding.ASCII.GetBytes(itemId.ToString()); - // First delete // TODO deleteAncestorsStatement.Parameters.Clear(); deleteAncestorsStatement.TryBind("@ItemId", itemId); @@ -4921,14 +4916,13 @@ AND Type = @InternalPersonType)"); using (var statement = PrepareStatement(db, insertText.ToString())) { - statement.TryBind("@ItemId", itemIdBlob); + statement.TryBind("@ItemId", itemId); for (var i = 0; i < ancestorIds.Count; i++) { var index = i.ToString(CultureInfo.InvariantCulture); var ancestorId = ancestorIds[i]; - itemIdBlob = Encoding.ASCII.GetBytes(itemId.ToString()); statement.TryBind("@AncestorId" + index, ancestorId); statement.TryBind("@AncestorIdText" + index, ancestorId.ToString("N", CultureInfo.InvariantCulture)); @@ -5416,17 +5410,15 @@ AND Type = @InternalPersonType)"); CheckDisposed(); - var guidBlob = itemId.ToByteArray(); - // First delete using var command = db.PrepareStatement("delete from ItemValues where ItemId=@Id"); - command.TryBind("@Id", guidBlob); + command.TryBind("@Id", itemId); command.ExecuteNonQuery(); - InsertItemValues(guidBlob, values, db); + InsertItemValues(itemId, values, db); } - private void InsertItemValues(byte[] idBlob, List<(int MagicNumber, string Value)> values, SqliteConnection db) + private void InsertItemValues(Guid id, List<(int MagicNumber, string Value)> values, SqliteConnection db) { const int Limit = 100; var startIndex = 0; @@ -5450,7 +5442,7 @@ AND Type = @InternalPersonType)"); using (var statement = PrepareStatement(db, insertText.ToString())) { - statement.TryBind("@ItemId", idBlob); + statement.TryBind("@ItemId", id); for (var i = startIndex; i < endIndex; i++) { @@ -5496,20 +5488,18 @@ AND Type = @InternalPersonType)"); connection.RunInTransaction( db => { - var itemIdBlob = itemId.ToByteArray(); - // First delete chapters using var command = db.CreateCommand(); command.CommandText = "delete from People where ItemId=@ItemId"; - command.TryBind("@ItemId", itemIdBlob); + command.TryBind("@ItemId", itemId); command.ExecuteNonQuery(); - InsertPeople(itemIdBlob, people, db); + InsertPeople(itemId, people, db); }); } } - private void InsertPeople(byte[] idBlob, List people, SqliteConnection db) + private void InsertPeople(Guid id, List people, SqliteConnection db) { const int Limit = 100; var startIndex = 0; @@ -5533,7 +5523,7 @@ AND Type = @InternalPersonType)"); using (var statement = PrepareStatement(db, insertText.ToString())) { - statement.TryBind("@ItemId", idBlob); + statement.TryBind("@ItemId", id); for (var i = startIndex; i < endIndex; i++) { @@ -5651,19 +5641,17 @@ AND Type = @InternalPersonType)"); connection.RunInTransaction( db => { - var itemIdBlob = id.ToByteArray(); - // Delete existing mediastreams using var command = db.PrepareStatement("delete from mediastreams where ItemId=@ItemId"); - command.TryBind("@ItemId", itemIdBlob); + command.TryBind("@ItemId", id); command.ExecuteNonQuery(); - InsertMediaStreams(itemIdBlob, streams, db); + InsertMediaStreams(id, streams, db); }); } } - private void InsertMediaStreams(byte[] idBlob, IReadOnlyList streams, SqliteConnection db) + private void InsertMediaStreams(Guid id, IReadOnlyList streams, SqliteConnection db) { const int Limit = 10; var startIndex = 0; @@ -5695,7 +5683,7 @@ AND Type = @InternalPersonType)"); using (var statement = PrepareStatement(db, insertText.ToString())) { - statement.TryBind("@ItemId", idBlob); + statement.TryBind("@ItemId", id); for (var i = startIndex; i < endIndex; i++) { @@ -5731,6 +5719,7 @@ AND Type = @InternalPersonType)"); statement.TryBind("@PixelFormat" + index, stream.PixelFormat); statement.TryBind("@BitDepth" + index, stream.BitDepth); + statement.TryBind("@IsAnamorphic" + index, stream.IsAnamorphic); statement.TryBind("@IsExternal" + index, stream.IsExternal); statement.TryBind("@RefFrames" + index, stream.RefFrames); @@ -6000,7 +5989,7 @@ AND Type = @InternalPersonType)"); using (var connection = GetConnection(true)) using (var statement = PrepareStatement(connection, cmdText)) { - statement.TryBind("@ItemId", query.ItemId.ToByteArray()); + statement.TryBind("@ItemId", query.ItemId); if (query.Index.HasValue) { @@ -6036,19 +6025,17 @@ AND Type = @InternalPersonType)"); connection.RunInTransaction( db => { - var itemIdBlob = id.ToByteArray(); - using var command = db.PrepareStatement("delete from mediaattachments where ItemId=@ItemId"); - command.TryBind("@ItemId", itemIdBlob); + command.TryBind("@ItemId", id); command.ExecuteNonQuery(); - InsertMediaAttachments(itemIdBlob, attachments, db, cancellationToken); + InsertMediaAttachments(id, attachments, db, cancellationToken); }); } } private void InsertMediaAttachments( - byte[] idBlob, + Guid id, IReadOnlyList attachments, SqliteConnection db, CancellationToken cancellationToken) @@ -6084,7 +6071,7 @@ AND Type = @InternalPersonType)"); using (var statement = PrepareStatement(db, insertText.ToString())) { - statement.TryBind("@ItemId", idBlob); + statement.TryBind("@ItemId", id); for (var i = startIndex; i < endIndex; i++) {