diff options
Diffstat (limited to 'Emby.Server.Implementations/Data/SqliteDisplayPreferencesRepository.cs')
| -rw-r--r-- | Emby.Server.Implementations/Data/SqliteDisplayPreferencesRepository.cs | 98 |
1 files changed, 43 insertions, 55 deletions
diff --git a/Emby.Server.Implementations/Data/SqliteDisplayPreferencesRepository.cs b/Emby.Server.Implementations/Data/SqliteDisplayPreferencesRepository.cs index 01ef9851d..2cd4d65b3 100644 --- a/Emby.Server.Implementations/Data/SqliteDisplayPreferencesRepository.cs +++ b/Emby.Server.Implementations/Data/SqliteDisplayPreferencesRepository.cs @@ -1,44 +1,45 @@ using System; using System.Collections.Generic; +using System.Globalization; using System.IO; +using System.Text.Json; using System.Threading; using MediaBrowser.Common.Configuration; using MediaBrowser.Common.Extensions; +using MediaBrowser.Common.Json; using MediaBrowser.Controller.Persistence; using MediaBrowser.Model.Entities; using MediaBrowser.Model.IO; -using MediaBrowser.Model.Serialization; using Microsoft.Extensions.Logging; using SQLitePCL.pretty; namespace Emby.Server.Implementations.Data { /// <summary> - /// Class SQLiteDisplayPreferencesRepository + /// Class SQLiteDisplayPreferencesRepository. /// </summary> public class SqliteDisplayPreferencesRepository : BaseSqliteRepository, IDisplayPreferencesRepository { private readonly IFileSystem _fileSystem; - public SqliteDisplayPreferencesRepository(ILoggerFactory loggerFactory, IJsonSerializer jsonSerializer, IApplicationPaths appPaths, IFileSystem fileSystem) - : base(loggerFactory.CreateLogger(nameof(SqliteDisplayPreferencesRepository))) + private readonly JsonSerializerOptions _jsonOptions; + + public SqliteDisplayPreferencesRepository(ILogger<SqliteDisplayPreferencesRepository> logger, IApplicationPaths appPaths, IFileSystem fileSystem) + : base(logger) { - _jsonSerializer = jsonSerializer; _fileSystem = fileSystem; + + _jsonOptions = JsonDefaults.GetOptions(); + DbFilePath = Path.Combine(appPaths.DataPath, "displaypreferences.db"); } /// <summary> - /// Gets the name of the repository + /// Gets the name of the repository. /// </summary> /// <value>The name.</value> public string Name => "SQLite"; - /// <summary> - /// The _json serializer - /// </summary> - private readonly IJsonSerializer _jsonSerializer; - public void Initialize() { try @@ -61,14 +62,14 @@ namespace Emby.Server.Implementations.Data /// <returns>Task.</returns> private void InitializeInternal() { - using (var connection = GetConnection()) + string[] queries = { - string[] queries = { - - "create table if not exists userdisplaypreferences (id GUID NOT NULL, userId GUID NOT NULL, client text NOT NULL, data BLOB NOT NULL)", - "create unique index if not exists userdisplaypreferencesindex on userdisplaypreferences (id, userId, client)" - }; + "create table if not exists userdisplaypreferences (id GUID NOT NULL, userId GUID NOT NULL, client text NOT NULL, data BLOB NOT NULL)", + "create unique index if not exists userdisplaypreferencesindex on userdisplaypreferences (id, userId, client)" + }; + using (var connection = GetConnection()) + { connection.RunQueries(queries); } } @@ -80,7 +81,6 @@ namespace Emby.Server.Implementations.Data /// <param name="userId">The user id.</param> /// <param name="client">The client.</param> /// <param name="cancellationToken">The cancellation token.</param> - /// <returns>Task.</returns> /// <exception cref="ArgumentNullException">item</exception> public void SaveDisplayPreferences(DisplayPreferences displayPreferences, Guid userId, string client, CancellationToken cancellationToken) { @@ -98,16 +98,15 @@ namespace Emby.Server.Implementations.Data using (var connection = GetConnection()) { - connection.RunInTransaction(db => - { - SaveDisplayPreferences(displayPreferences, userId, client, db); - }, TransactionMode); + connection.RunInTransaction( + db => SaveDisplayPreferences(displayPreferences, userId, client, db), + TransactionMode); } } private void SaveDisplayPreferences(DisplayPreferences displayPreferences, Guid userId, string client, IDatabaseConnection connection) { - var serialized = _jsonSerializer.SerializeToBytes(displayPreferences); + var serialized = JsonSerializer.SerializeToUtf8Bytes(displayPreferences, _jsonOptions); using (var statement = connection.PrepareStatement("replace into userdisplaypreferences (id, userid, client, data) values (@id, @userId, @client, @data)")) { @@ -126,7 +125,6 @@ namespace Emby.Server.Implementations.Data /// <param name="displayPreferences">The display preferences.</param> /// <param name="userId">The user id.</param> /// <param name="cancellationToken">The cancellation token.</param> - /// <returns>Task.</returns> /// <exception cref="ArgumentNullException">item</exception> public void SaveAllDisplayPreferences(IEnumerable<DisplayPreferences> displayPreferences, Guid userId, CancellationToken cancellationToken) { @@ -139,13 +137,15 @@ namespace Emby.Server.Implementations.Data using (var connection = GetConnection()) { - connection.RunInTransaction(db => - { - foreach (var displayPreference in displayPreferences) + connection.RunInTransaction( + db => { - SaveDisplayPreferences(displayPreference, userId, displayPreference.Client, db); - } - }, TransactionMode); + foreach (var displayPreference in displayPreferences) + { + SaveDisplayPreferences(displayPreference, userId, displayPreference.Client, db); + } + }, + TransactionMode); } } @@ -179,12 +179,12 @@ namespace Emby.Server.Implementations.Data return Get(row); } } - - return new DisplayPreferences - { - Id = guidId.ToString("N") - }; } + + return new DisplayPreferences + { + Id = guidId.ToString("N", CultureInfo.InvariantCulture) + }; } /// <summary> @@ -198,15 +198,13 @@ namespace Emby.Server.Implementations.Data var list = new List<DisplayPreferences>(); using (var connection = GetConnection(true)) + using (var statement = connection.PrepareStatement("select data from userdisplaypreferences where userId=@userId")) { - using (var statement = connection.PrepareStatement("select data from userdisplaypreferences where userId=@userId")) - { - statement.TryBind("@userId", userId.ToGuidBlob()); + statement.TryBind("@userId", userId.ToGuidBlob()); - foreach (var row in statement.ExecuteQuery()) - { - list.Add(Get(row)); - } + foreach (var row in statement.ExecuteQuery()) + { + list.Add(Get(row)); } } @@ -214,22 +212,12 @@ namespace Emby.Server.Implementations.Data } private DisplayPreferences Get(IReadOnlyList<IResultSetValue> row) - { - using (var stream = new MemoryStream(row[0].ToBlob())) - { - stream.Position = 0; - return _jsonSerializer.DeserializeFromStream<DisplayPreferences>(stream); - } - } + => JsonSerializer.Deserialize<DisplayPreferences>(row[0].ToBlob(), _jsonOptions); public void SaveDisplayPreferences(DisplayPreferences displayPreferences, string userId, string client, CancellationToken cancellationToken) - { - SaveDisplayPreferences(displayPreferences, new Guid(userId), client, cancellationToken); - } + => SaveDisplayPreferences(displayPreferences, new Guid(userId), client, cancellationToken); public DisplayPreferences GetDisplayPreferences(string displayPreferencesId, string userId, string client) - { - return GetDisplayPreferences(displayPreferencesId, new Guid(userId), client); - } + => GetDisplayPreferences(displayPreferencesId, new Guid(userId), client); } } |
