jellyfin/Emby.Server.Implementations/Data/SqliteDisplayPreferencesRepository.cs

237 lines
8.9 KiB
C#
Raw Normal View History

using System;
2016-11-18 09:28:39 +00:00
using System.Collections.Generic;
using System.Globalization;
2016-11-18 09:28:39 +00:00
using System.IO;
using System.Threading;
using MediaBrowser.Common.Configuration;
using MediaBrowser.Common.Extensions;
using MediaBrowser.Controller.Persistence;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.IO;
using MediaBrowser.Model.Serialization;
using Microsoft.Extensions.Logging;
2016-11-18 09:28:39 +00:00
using SQLitePCL.pretty;
namespace Emby.Server.Implementations.Data
{
/// <summary>
/// Class SQLiteDisplayPreferencesRepository
/// </summary>
public class SqliteDisplayPreferencesRepository : BaseSqliteRepository, IDisplayPreferencesRepository
{
2019-03-11 22:07:38 +00:00
private readonly IFileSystem _fileSystem;
2016-11-18 09:28:39 +00:00
public SqliteDisplayPreferencesRepository(ILogger<SqliteDisplayPreferencesRepository> logger, IJsonSerializer jsonSerializer, IApplicationPaths appPaths, IFileSystem fileSystem)
: base(logger)
2016-11-18 09:28:39 +00:00
{
_jsonSerializer = jsonSerializer;
2019-03-11 22:07:38 +00:00
_fileSystem = fileSystem;
2016-11-18 09:28:39 +00:00
DbFilePath = Path.Combine(appPaths.DataPath, "displaypreferences.db");
}
/// <summary>
/// Gets the name of the repository
/// </summary>
/// <value>The name.</value>
public string Name => "SQLite";
2016-11-18 09:28:39 +00:00
/// <summary>
/// The _json serializer
/// </summary>
private readonly IJsonSerializer _jsonSerializer;
public void Initialize()
{
try
{
InitializeInternal();
}
catch (Exception ex)
{
2018-12-20 12:11:26 +00:00
Logger.LogError(ex, "Error loading database file. Will reset and retry.");
2019-03-11 22:07:38 +00:00
_fileSystem.DeleteFile(DbFilePath);
InitializeInternal();
}
}
2016-11-18 09:28:39 +00:00
/// <summary>
/// Opens the connection to the database
/// </summary>
/// <returns>Task.</returns>
private void InitializeInternal()
2016-11-18 09:28:39 +00:00
{
using (var connection = GetConnection())
2016-11-18 09:28:39 +00:00
{
string[] queries = {
2017-10-22 06:22:43 +00:00
"create table if not exists userdisplaypreferences (id GUID NOT NULL, userId GUID NOT NULL, client text NOT NULL, data BLOB NOT NULL)",
2016-11-27 19:36:56 +00:00
"create unique index if not exists userdisplaypreferencesindex on userdisplaypreferences (id, userId, client)"
2016-11-18 09:28:39 +00:00
};
connection.RunQueries(queries);
}
}
/// <summary>
/// Save the display preferences associated with an item in the repo
/// </summary>
/// <param name="displayPreferences">The display preferences.</param>
/// <param name="userId">The user id.</param>
/// <param name="client">The client.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>Task.</returns>
2019-01-13 20:37:13 +00:00
/// <exception cref="ArgumentNullException">item</exception>
public void SaveDisplayPreferences(DisplayPreferences displayPreferences, Guid userId, string client, CancellationToken cancellationToken)
2016-11-18 09:28:39 +00:00
{
if (displayPreferences == null)
{
throw new ArgumentNullException(nameof(displayPreferences));
2016-11-18 09:28:39 +00:00
}
2018-09-12 17:26:21 +00:00
if (string.IsNullOrEmpty(displayPreferences.Id))
2016-11-18 09:28:39 +00:00
{
throw new ArgumentException("Display preferences has an invalid Id", nameof(displayPreferences));
2016-11-18 09:28:39 +00:00
}
cancellationToken.ThrowIfCancellationRequested();
using (var connection = GetConnection())
2016-11-18 09:28:39 +00:00
{
2019-02-20 13:26:49 +00:00
connection.RunInTransaction(db =>
2016-11-18 09:28:39 +00:00
{
2019-02-20 13:26:49 +00:00
SaveDisplayPreferences(displayPreferences, userId, client, db);
}, TransactionMode);
2016-11-18 09:28:39 +00:00
}
}
private void SaveDisplayPreferences(DisplayPreferences displayPreferences, Guid userId, string client, IDatabaseConnection connection)
{
2018-09-12 17:26:21 +00:00
var serialized = _jsonSerializer.SerializeToBytes(displayPreferences);
2016-11-20 05:59:36 +00:00
2016-11-27 19:36:56 +00:00
using (var statement = connection.PrepareStatement("replace into userdisplaypreferences (id, userid, client, data) values (@id, @userId, @client, @data)"))
{
2017-05-07 20:02:32 +00:00
statement.TryBind("@id", displayPreferences.Id.ToGuidBlob());
statement.TryBind("@userId", userId.ToGuidBlob());
2016-11-20 07:10:07 +00:00
statement.TryBind("@client", client);
statement.TryBind("@data", serialized);
2016-11-20 05:59:36 +00:00
statement.MoveNext();
}
2016-11-18 09:28:39 +00:00
}
/// <summary>
/// Save all display preferences associated with a user in the repo
/// </summary>
/// <param name="displayPreferences">The display preferences.</param>
/// <param name="userId">The user id.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>Task.</returns>
2019-01-13 20:37:13 +00:00
/// <exception cref="ArgumentNullException">item</exception>
public void SaveAllDisplayPreferences(IEnumerable<DisplayPreferences> displayPreferences, Guid userId, CancellationToken cancellationToken)
2016-11-18 09:28:39 +00:00
{
if (displayPreferences == null)
{
throw new ArgumentNullException(nameof(displayPreferences));
2016-11-18 09:28:39 +00:00
}
cancellationToken.ThrowIfCancellationRequested();
using (var connection = GetConnection())
2016-11-18 09:28:39 +00:00
{
2019-02-20 13:26:49 +00:00
connection.RunInTransaction(db =>
2016-11-18 09:28:39 +00:00
{
2019-02-20 13:26:49 +00:00
foreach (var displayPreference in displayPreferences)
2016-11-18 09:28:39 +00:00
{
2019-02-20 13:26:49 +00:00
SaveDisplayPreferences(displayPreference, userId, displayPreference.Client, db);
}
}, TransactionMode);
2016-11-18 09:28:39 +00:00
}
}
/// <summary>
/// Gets the display preferences.
/// </summary>
/// <param name="displayPreferencesId">The display preferences id.</param>
/// <param name="userId">The user id.</param>
/// <param name="client">The client.</param>
/// <returns>Task{DisplayPreferences}.</returns>
2019-01-13 20:37:13 +00:00
/// <exception cref="ArgumentNullException">item</exception>
2016-11-18 09:28:39 +00:00
public DisplayPreferences GetDisplayPreferences(string displayPreferencesId, Guid userId, string client)
{
2018-09-12 17:26:21 +00:00
if (string.IsNullOrEmpty(displayPreferencesId))
2016-11-18 09:28:39 +00:00
{
throw new ArgumentNullException(nameof(displayPreferencesId));
2016-11-18 09:28:39 +00:00
}
var guidId = displayPreferencesId.GetMD5();
using (var connection = GetConnection(true))
2016-11-18 09:28:39 +00:00
{
2019-02-20 13:26:49 +00:00
using (var statement = connection.PrepareStatement("select data from userdisplaypreferences where id = @id and userId=@userId and client=@client"))
2016-11-19 08:40:13 +00:00
{
2019-02-20 13:26:49 +00:00
statement.TryBind("@id", guidId.ToGuidBlob());
statement.TryBind("@userId", userId.ToGuidBlob());
statement.TryBind("@client", client);
2016-11-18 09:28:39 +00:00
2019-02-20 13:26:49 +00:00
foreach (var row in statement.ExecuteQuery())
2016-11-19 08:40:13 +00:00
{
2019-02-20 13:26:49 +00:00
return Get(row);
}
2016-11-19 08:40:13 +00:00
}
2019-02-20 13:26:49 +00:00
return new DisplayPreferences
{
Id = guidId.ToString("N", CultureInfo.InvariantCulture)
2019-02-20 13:26:49 +00:00
};
2016-11-18 09:28:39 +00:00
}
}
/// <summary>
/// Gets all display preferences for the given user.
/// </summary>
/// <param name="userId">The user id.</param>
/// <returns>Task{DisplayPreferences}.</returns>
2019-01-13 20:37:13 +00:00
/// <exception cref="ArgumentNullException">item</exception>
2016-11-18 09:28:39 +00:00
public IEnumerable<DisplayPreferences> GetAllDisplayPreferences(Guid userId)
{
var list = new List<DisplayPreferences>();
using (var connection = GetConnection(true))
2016-11-18 09:28:39 +00:00
{
2019-02-20 13:26:49 +00:00
using (var statement = connection.PrepareStatement("select data from userdisplaypreferences where userId=@userId"))
2016-11-19 08:40:13 +00:00
{
2019-02-20 13:26:49 +00:00
statement.TryBind("@userId", userId.ToGuidBlob());
2016-11-20 05:59:36 +00:00
2019-02-20 13:26:49 +00:00
foreach (var row in statement.ExecuteQuery())
{
list.Add(Get(row));
2016-11-19 08:40:13 +00:00
}
2016-11-18 09:28:39 +00:00
}
}
return list;
}
private DisplayPreferences Get(IReadOnlyList<IResultSetValue> row)
{
2018-09-12 17:26:21 +00:00
using (var stream = new MemoryStream(row[0].ToBlob()))
2016-11-18 09:28:39 +00:00
{
stream.Position = 0;
return _jsonSerializer.DeserializeFromStream<DisplayPreferences>(stream);
}
}
public void SaveDisplayPreferences(DisplayPreferences displayPreferences, string userId, string client, CancellationToken cancellationToken)
2016-11-18 09:28:39 +00:00
{
SaveDisplayPreferences(displayPreferences, new Guid(userId), client, cancellationToken);
2016-11-18 09:28:39 +00:00
}
public DisplayPreferences GetDisplayPreferences(string displayPreferencesId, string userId, string client)
{
return GetDisplayPreferences(displayPreferencesId, new Guid(userId), client);
}
}
}