aboutsummaryrefslogtreecommitdiff
path: root/Emby.Server.Implementations/Data/SqliteItemRepository.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Emby.Server.Implementations/Data/SqliteItemRepository.cs')
-rw-r--r--Emby.Server.Implementations/Data/SqliteItemRepository.cs86
1 files changed, 41 insertions, 45 deletions
diff --git a/Emby.Server.Implementations/Data/SqliteItemRepository.cs b/Emby.Server.Implementations/Data/SqliteItemRepository.cs
index 2f083dda4..33402f0e3 100644
--- a/Emby.Server.Implementations/Data/SqliteItemRepository.cs
+++ b/Emby.Server.Implementations/Data/SqliteItemRepository.cs
@@ -5,8 +5,11 @@ using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
+using System.Text.Json;
+using System.Text.Json.Serialization;
using System.Threading;
using Emby.Server.Implementations.Playlists;
+using MediaBrowser.Common.Json;
using MediaBrowser.Controller;
using MediaBrowser.Controller.Channels;
using MediaBrowser.Controller.Configuration;
@@ -38,14 +41,6 @@ namespace Emby.Server.Implementations.Data
{
private const string ChaptersTableName = "Chapters2";
- private readonly TypeMapper _typeMapper;
-
- /// <summary>
- /// Gets the json serializer.
- /// </summary>
- /// <value>The json serializer.</value>
- private readonly IJsonSerializer _jsonSerializer;
-
/// <summary>
/// The _app paths
/// </summary>
@@ -53,33 +48,31 @@ namespace Emby.Server.Implementations.Data
private readonly IServerApplicationHost _appHost;
private readonly ILocalizationManager _localization;
+ private readonly TypeMapper _typeMapper;
+ private readonly JsonSerializerOptions _jsonOptions;
+
/// <summary>
/// Initializes a new instance of the <see cref="SqliteItemRepository"/> class.
/// </summary>
public SqliteItemRepository(
IServerConfigurationManager config,
IServerApplicationHost appHost,
- IJsonSerializer jsonSerializer,
- ILoggerFactory loggerFactory,
+ ILogger<SqliteItemRepository> logger,
ILocalizationManager localization)
- : base(loggerFactory.CreateLogger(nameof(SqliteItemRepository)))
+ : base(logger)
{
if (config == null)
{
throw new ArgumentNullException(nameof(config));
}
- if (jsonSerializer == null)
- {
- throw new ArgumentNullException(nameof(jsonSerializer));
- }
-
- _appHost = appHost;
_config = config;
- _jsonSerializer = jsonSerializer;
- _typeMapper = new TypeMapper();
+ _appHost = appHost;
_localization = localization;
+ _typeMapper = new TypeMapper();
+ _jsonOptions = JsonDefaults.GetOptions();
+
DbFilePath = Path.Combine(_config.ApplicationPaths.DataPath, "library.db");
}
@@ -556,7 +549,7 @@ namespace Emby.Server.Implementations.Data
{
using (var saveImagesStatement = base.PrepareStatement(db, "Update TypedBaseItems set Images=@Images where guid=@Id"))
{
- saveImagesStatement.TryBind("@Id", item.Id.ToGuidBlob());
+ saveImagesStatement.TryBind("@Id", item.Id.ToByteArray());
saveImagesStatement.TryBind("@Images", SerializeImages(item));
saveImagesStatement.MoveNext();
@@ -671,7 +664,7 @@ namespace Emby.Server.Implementations.Data
if (TypeRequiresDeserialization(item.GetType()))
{
- saveItemStatement.TryBind("@data", _jsonSerializer.SerializeToBytes(item));
+ saveItemStatement.TryBind("@data", JsonSerializer.SerializeToUtf8Bytes(item, _jsonOptions));
}
else
{
@@ -1302,11 +1295,11 @@ namespace Emby.Server.Implementations.Data
{
try
{
- item = _jsonSerializer.DeserializeFromString(reader.GetString(1), type) as BaseItem;
+ item = JsonSerializer.Deserialize(reader[1].ToBlob(), type, _jsonOptions) as BaseItem;
}
- catch (SerializationException ex)
+ catch (JsonException ex)
{
- Logger.LogError(ex, "Error deserializing item");
+ Logger.LogError(ex, "Error deserializing item with JSON: {Data}", reader.GetString(1));
}
}
@@ -1996,7 +1989,7 @@ namespace Emby.Server.Implementations.Data
throw new ArgumentNullException(nameof(chapters));
}
- var idBlob = id.ToGuidBlob();
+ var idBlob = id.ToByteArray();
using (var connection = GetConnection())
{
@@ -2724,7 +2717,7 @@ namespace Emby.Server.Implementations.Data
if (elapsed >= SlowThreshold)
{
- Logger.LogWarning(
+ Logger.LogDebug(
"{Method} query time (slow): {ElapsedMs}ms. Query: {Query}",
methodName,
elapsed,
@@ -3775,7 +3768,7 @@ namespace Emby.Server.Implementations.Data
if (statement != null)
{
- statement.TryBind(paramName, personId.ToGuidBlob());
+ statement.TryBind(paramName, personId.ToByteArray());
}
index++;
}
@@ -3986,7 +3979,7 @@ namespace Emby.Server.Implementations.Data
clauses.Add("(guid in (select itemid from itemvalues where CleanValue = (select CleanName from TypedBaseItems where guid=" + paramName + ") and Type<=1))");
if (statement != null)
{
- statement.TryBind(paramName, artistId.ToGuidBlob());
+ statement.TryBind(paramName, artistId.ToByteArray());
}
index++;
}
@@ -4005,7 +3998,7 @@ namespace Emby.Server.Implementations.Data
clauses.Add("(guid in (select itemid from itemvalues where CleanValue = (select CleanName from TypedBaseItems where guid=" + paramName + ") and Type=1))");
if (statement != null)
{
- statement.TryBind(paramName, artistId.ToGuidBlob());
+ statement.TryBind(paramName, artistId.ToByteArray());
}
index++;
}
@@ -4024,7 +4017,7 @@ namespace Emby.Server.Implementations.Data
clauses.Add("((select CleanName from TypedBaseItems where guid=" + paramName + ") in (select CleanValue from itemvalues where ItemId=Guid and Type=0) AND (select CleanName from TypedBaseItems where guid=" + paramName + ") not in (select CleanValue from itemvalues where ItemId=Guid and Type=1))");
if (statement != null)
{
- statement.TryBind(paramName, artistId.ToGuidBlob());
+ statement.TryBind(paramName, artistId.ToByteArray());
}
index++;
}
@@ -4043,7 +4036,7 @@ namespace Emby.Server.Implementations.Data
clauses.Add("Album in (select Name from typedbaseitems where guid=" + paramName + ")");
if (statement != null)
{
- statement.TryBind(paramName, albumId.ToGuidBlob());
+ statement.TryBind(paramName, albumId.ToByteArray());
}
index++;
}
@@ -4062,7 +4055,7 @@ namespace Emby.Server.Implementations.Data
clauses.Add("(guid not in (select itemid from itemvalues where CleanValue = (select CleanName from TypedBaseItems where guid=" + paramName + ") and Type<=1))");
if (statement != null)
{
- statement.TryBind(paramName, artistId.ToGuidBlob());
+ statement.TryBind(paramName, artistId.ToByteArray());
}
index++;
}
@@ -4081,7 +4074,7 @@ namespace Emby.Server.Implementations.Data
clauses.Add("(guid in (select itemid from itemvalues where CleanValue = (select CleanName from TypedBaseItems where guid=" + paramName + ") and Type=2))");
if (statement != null)
{
- statement.TryBind(paramName, genreId.ToGuidBlob());
+ statement.TryBind(paramName, genreId.ToByteArray());
}
index++;
}
@@ -4152,7 +4145,7 @@ namespace Emby.Server.Implementations.Data
if (statement != null)
{
- statement.TryBind(paramName, studioId.ToGuidBlob());
+ statement.TryBind(paramName, studioId.ToByteArray());
}
index++;
}
@@ -4928,7 +4921,7 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type
{
connection.RunInTransaction(db =>
{
- var idBlob = id.ToGuidBlob();
+ var idBlob = id.ToByteArray();
// Delete people
ExecuteWithSingleParam(db, "delete from People where ItemId=@Id", idBlob);
@@ -5047,7 +5040,7 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type
whereClauses.Add("ItemId=@ItemId");
if (statement != null)
{
- statement.TryBind("@ItemId", query.ItemId.ToGuidBlob());
+ statement.TryBind("@ItemId", query.ItemId.ToByteArray());
}
}
if (!query.AppearsInItemId.Equals(Guid.Empty))
@@ -5055,7 +5048,7 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type
whereClauses.Add("Name in (Select Name from People where ItemId=@AppearsInItemId)");
if (statement != null)
{
- statement.TryBind("@AppearsInItemId", query.AppearsInItemId.ToGuidBlob());
+ statement.TryBind("@AppearsInItemId", query.AppearsInItemId.ToByteArray());
}
}
var queryPersonTypes = query.PersonTypes.Where(IsValidPersonType).ToList();
@@ -5124,7 +5117,7 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type
CheckDisposed();
- var itemIdBlob = itemId.ToGuidBlob();
+ var itemIdBlob = itemId.ToByteArray();
// First delete
deleteAncestorsStatement.Reset();
@@ -5158,7 +5151,7 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type
var ancestorId = ancestorIds[i];
- statement.TryBind("@AncestorId" + index, ancestorId.ToGuidBlob());
+ statement.TryBind("@AncestorId" + index, ancestorId.ToByteArray());
statement.TryBind("@AncestorIdText" + index, ancestorId.ToString("N", CultureInfo.InvariantCulture));
}
@@ -5623,7 +5616,7 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type
CheckDisposed();
- var guidBlob = itemId.ToGuidBlob();
+ var guidBlob = itemId.ToByteArray();
// First delete
db.Execute("delete from ItemValues where ItemId=@Id", guidBlob);
@@ -5647,10 +5640,13 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type
{
if (isSubsequentRow)
{
- insertText.Append(",");
+ insertText.Append(',');
}
- insertText.AppendFormat("(@ItemId, @Type{0}, @Value{0}, @CleanValue{0})", i.ToString(CultureInfo.InvariantCulture));
+ insertText.AppendFormat(
+ CultureInfo.InvariantCulture,
+ "(@ItemId, @Type{0}, @Value{0}, @CleanValue{0})",
+ i);
isSubsequentRow = true;
}
@@ -5703,7 +5699,7 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type
{
connection.RunInTransaction(db =>
{
- var itemIdBlob = itemId.ToGuidBlob();
+ var itemIdBlob = itemId.ToByteArray();
// First delete chapters
db.Execute("delete from People where ItemId=@ItemId", itemIdBlob);
@@ -5822,7 +5818,7 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type
using (var statement = PrepareStatement(connection, cmdText))
{
- statement.TryBind("@ItemId", query.ItemId.ToGuidBlob());
+ statement.TryBind("@ItemId", query.ItemId.ToByteArray());
if (query.Type.HasValue)
{
@@ -5864,7 +5860,7 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type
{
connection.RunInTransaction(db =>
{
- var itemIdBlob = id.ToGuidBlob();
+ var itemIdBlob = id.ToByteArray();
// First delete chapters
db.Execute("delete from mediastreams where ItemId=@ItemId", itemIdBlob);