2019-11-01 10:38:54 -07:00
|
|
|
#pragma warning disable CS1591
|
2019-12-10 16:13:57 -07:00
|
|
|
#pragma warning disable SA1600
|
2019-11-01 10:38:54 -07:00
|
|
|
|
2019-01-13 12:54:44 -07:00
|
|
|
using System;
|
2016-11-18 02:28:39 -07:00
|
|
|
using System.Collections.Generic;
|
2019-02-28 15:22:57 -07:00
|
|
|
using System.Globalization;
|
2016-11-18 02:28:39 -07:00
|
|
|
using System.IO;
|
2019-09-25 08:43:20 -07:00
|
|
|
using System.Text.Json;
|
2016-11-18 02:28:39 -07:00
|
|
|
using System.Threading;
|
|
|
|
using MediaBrowser.Common.Configuration;
|
|
|
|
using MediaBrowser.Common.Extensions;
|
2019-09-25 08:43:20 -07:00
|
|
|
using MediaBrowser.Common.Json;
|
2016-11-18 02:28:39 -07:00
|
|
|
using MediaBrowser.Controller.Persistence;
|
|
|
|
using MediaBrowser.Model.Entities;
|
|
|
|
using MediaBrowser.Model.IO;
|
2019-01-13 12:20:16 -07:00
|
|
|
using Microsoft.Extensions.Logging;
|
2016-11-18 02:28:39 -07:00
|
|
|
using SQLitePCL.pretty;
|
|
|
|
|
|
|
|
namespace Emby.Server.Implementations.Data
|
|
|
|
{
|
|
|
|
/// <summary>
|
2019-09-25 08:43:20 -07:00
|
|
|
/// Class SQLiteDisplayPreferencesRepository.
|
2016-11-18 02:28:39 -07:00
|
|
|
/// </summary>
|
|
|
|
public class SqliteDisplayPreferencesRepository : BaseSqliteRepository, IDisplayPreferencesRepository
|
|
|
|
{
|
2019-03-11 15:07:38 -07:00
|
|
|
private readonly IFileSystem _fileSystem;
|
2016-11-18 02:28:39 -07:00
|
|
|
|
2019-09-25 08:43:20 -07:00
|
|
|
private readonly JsonSerializerOptions _jsonOptions;
|
|
|
|
|
|
|
|
public SqliteDisplayPreferencesRepository(ILogger<SqliteDisplayPreferencesRepository> logger, IApplicationPaths appPaths, IFileSystem fileSystem)
|
2019-08-14 11:35:36 -07:00
|
|
|
: base(logger)
|
2016-11-18 02:28:39 -07:00
|
|
|
{
|
2019-03-11 15:07:38 -07:00
|
|
|
_fileSystem = fileSystem;
|
2019-09-25 08:43:20 -07:00
|
|
|
|
|
|
|
_jsonOptions = JsonDefaults.GetOptions();
|
|
|
|
|
2016-11-18 02:28:39 -07:00
|
|
|
DbFilePath = Path.Combine(appPaths.DataPath, "displaypreferences.db");
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2019-09-25 08:43:20 -07:00
|
|
|
/// Gets the name of the repository.
|
2016-11-18 02:28:39 -07:00
|
|
|
/// </summary>
|
|
|
|
/// <value>The name.</value>
|
2019-01-06 13:50:43 -07:00
|
|
|
public string Name => "SQLite";
|
2016-11-18 02:28:39 -07:00
|
|
|
|
2017-09-21 22:54:57 -07:00
|
|
|
public void Initialize()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
InitializeInternal();
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
2018-12-20 05:11:26 -07:00
|
|
|
Logger.LogError(ex, "Error loading database file. Will reset and retry.");
|
2017-09-21 22:54:57 -07:00
|
|
|
|
2019-03-11 15:07:38 -07:00
|
|
|
_fileSystem.DeleteFile(DbFilePath);
|
2017-09-21 22:54:57 -07:00
|
|
|
|
|
|
|
InitializeInternal();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-18 02:28:39 -07:00
|
|
|
/// <summary>
|
|
|
|
/// Opens the connection to the database
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>Task.</returns>
|
2017-09-21 22:54:57 -07:00
|
|
|
private void InitializeInternal()
|
2016-11-18 02:28:39 -07:00
|
|
|
{
|
2019-08-18 13:05:06 -07:00
|
|
|
string[] queries =
|
2016-11-18 02:28:39 -07:00
|
|
|
{
|
2019-08-18 13:05:06 -07:00
|
|
|
"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)"
|
|
|
|
};
|
2016-11-18 02:28:39 -07:00
|
|
|
|
2019-08-18 13:05:06 -07:00
|
|
|
using (var connection = GetConnection())
|
|
|
|
{
|
2016-11-18 02:28:39 -07: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>
|
2019-01-13 13:37:13 -07:00
|
|
|
/// <exception cref="ArgumentNullException">item</exception>
|
2017-08-26 17:32:33 -07:00
|
|
|
public void SaveDisplayPreferences(DisplayPreferences displayPreferences, Guid userId, string client, CancellationToken cancellationToken)
|
2016-11-18 02:28:39 -07:00
|
|
|
{
|
|
|
|
if (displayPreferences == null)
|
|
|
|
{
|
2019-01-06 13:50:43 -07:00
|
|
|
throw new ArgumentNullException(nameof(displayPreferences));
|
2016-11-18 02:28:39 -07:00
|
|
|
}
|
2019-03-25 14:25:32 -07:00
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
if (string.IsNullOrEmpty(displayPreferences.Id))
|
2016-11-18 02:28:39 -07:00
|
|
|
{
|
2019-03-25 14:25:32 -07:00
|
|
|
throw new ArgumentException("Display preferences has an invalid Id", nameof(displayPreferences));
|
2016-11-18 02:28:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
2019-02-26 10:30:13 -07:00
|
|
|
using (var connection = GetConnection())
|
2016-11-18 02:28:39 -07:00
|
|
|
{
|
2019-08-18 13:05:06 -07:00
|
|
|
connection.RunInTransaction(
|
|
|
|
db => SaveDisplayPreferences(displayPreferences, userId, client, db),
|
|
|
|
TransactionMode);
|
2016-11-18 02:28:39 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void SaveDisplayPreferences(DisplayPreferences displayPreferences, Guid userId, string client, IDatabaseConnection connection)
|
|
|
|
{
|
2019-09-25 08:43:20 -07:00
|
|
|
var serialized = JsonSerializer.SerializeToUtf8Bytes(displayPreferences, _jsonOptions);
|
2016-11-19 22:59:36 -07:00
|
|
|
|
2016-11-27 12:36:56 -07:00
|
|
|
using (var statement = connection.PrepareStatement("replace into userdisplaypreferences (id, userid, client, data) values (@id, @userId, @client, @data)"))
|
|
|
|
{
|
2019-09-25 04:24:39 -07:00
|
|
|
statement.TryBind("@id", new Guid(displayPreferences.Id).ToByteArray());
|
|
|
|
statement.TryBind("@userId", userId.ToByteArray());
|
2016-11-20 00:10:07 -07:00
|
|
|
statement.TryBind("@client", client);
|
|
|
|
statement.TryBind("@data", serialized);
|
2016-11-19 22:59:36 -07:00
|
|
|
|
|
|
|
statement.MoveNext();
|
|
|
|
}
|
2016-11-18 02:28:39 -07: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>
|
2019-01-13 13:37:13 -07:00
|
|
|
/// <exception cref="ArgumentNullException">item</exception>
|
2017-08-26 17:32:33 -07:00
|
|
|
public void SaveAllDisplayPreferences(IEnumerable<DisplayPreferences> displayPreferences, Guid userId, CancellationToken cancellationToken)
|
2016-11-18 02:28:39 -07:00
|
|
|
{
|
|
|
|
if (displayPreferences == null)
|
|
|
|
{
|
2019-01-06 13:50:43 -07:00
|
|
|
throw new ArgumentNullException(nameof(displayPreferences));
|
2016-11-18 02:28:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
2019-02-26 10:30:13 -07:00
|
|
|
using (var connection = GetConnection())
|
2016-11-18 02:28:39 -07:00
|
|
|
{
|
2019-08-18 13:05:06 -07:00
|
|
|
connection.RunInTransaction(
|
|
|
|
db =>
|
2016-11-18 02:28:39 -07:00
|
|
|
{
|
2019-08-18 13:05:06 -07:00
|
|
|
foreach (var displayPreference in displayPreferences)
|
|
|
|
{
|
|
|
|
SaveDisplayPreferences(displayPreference, userId, displayPreference.Client, db);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
TransactionMode);
|
2016-11-18 02:28:39 -07: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 13:37:13 -07:00
|
|
|
/// <exception cref="ArgumentNullException">item</exception>
|
2016-11-18 02:28:39 -07:00
|
|
|
public DisplayPreferences GetDisplayPreferences(string displayPreferencesId, Guid userId, string client)
|
|
|
|
{
|
2018-09-12 10:26:21 -07:00
|
|
|
if (string.IsNullOrEmpty(displayPreferencesId))
|
2016-11-18 02:28:39 -07:00
|
|
|
{
|
2019-01-06 13:50:43 -07:00
|
|
|
throw new ArgumentNullException(nameof(displayPreferencesId));
|
2016-11-18 02:28:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
var guidId = displayPreferencesId.GetMD5();
|
|
|
|
|
2019-02-26 10:30:13 -07:00
|
|
|
using (var connection = GetConnection(true))
|
2016-11-18 02:28:39 -07:00
|
|
|
{
|
2019-02-20 06:26:49 -07:00
|
|
|
using (var statement = connection.PrepareStatement("select data from userdisplaypreferences where id = @id and userId=@userId and client=@client"))
|
2016-11-19 01:40:13 -07:00
|
|
|
{
|
2019-09-25 04:24:39 -07:00
|
|
|
statement.TryBind("@id", guidId.ToByteArray());
|
|
|
|
statement.TryBind("@userId", userId.ToByteArray());
|
2019-02-20 06:26:49 -07:00
|
|
|
statement.TryBind("@client", client);
|
2016-11-18 02:28:39 -07:00
|
|
|
|
2019-02-20 06:26:49 -07:00
|
|
|
foreach (var row in statement.ExecuteQuery())
|
2016-11-19 01:40:13 -07:00
|
|
|
{
|
2019-02-20 06:26:49 -07:00
|
|
|
return Get(row);
|
|
|
|
}
|
2016-11-19 01:40:13 -07:00
|
|
|
}
|
2016-11-18 02:28:39 -07:00
|
|
|
}
|
2019-08-18 13:05:06 -07:00
|
|
|
|
|
|
|
return new DisplayPreferences
|
|
|
|
{
|
|
|
|
Id = guidId.ToString("N", CultureInfo.InvariantCulture)
|
|
|
|
};
|
2016-11-18 02:28:39 -07: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 13:37:13 -07:00
|
|
|
/// <exception cref="ArgumentNullException">item</exception>
|
2016-11-18 02:28:39 -07:00
|
|
|
public IEnumerable<DisplayPreferences> GetAllDisplayPreferences(Guid userId)
|
|
|
|
{
|
|
|
|
var list = new List<DisplayPreferences>();
|
|
|
|
|
2019-02-26 10:30:13 -07:00
|
|
|
using (var connection = GetConnection(true))
|
2019-09-25 08:43:20 -07:00
|
|
|
using (var statement = connection.PrepareStatement("select data from userdisplaypreferences where userId=@userId"))
|
2016-11-18 02:28:39 -07:00
|
|
|
{
|
2019-09-25 04:24:39 -07:00
|
|
|
statement.TryBind("@userId", userId.ToByteArray());
|
2016-11-19 22:59:36 -07:00
|
|
|
|
2019-09-25 08:43:20 -07:00
|
|
|
foreach (var row in statement.ExecuteQuery())
|
|
|
|
{
|
|
|
|
list.Add(Get(row));
|
2016-11-18 02:28:39 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
private DisplayPreferences Get(IReadOnlyList<IResultSetValue> row)
|
2019-09-25 08:43:20 -07:00
|
|
|
=> JsonSerializer.Deserialize<DisplayPreferences>(row[0].ToBlob(), _jsonOptions);
|
2016-11-18 02:28:39 -07:00
|
|
|
|
2017-08-26 17:32:33 -07:00
|
|
|
public void SaveDisplayPreferences(DisplayPreferences displayPreferences, string userId, string client, CancellationToken cancellationToken)
|
2019-08-18 13:05:06 -07:00
|
|
|
=> SaveDisplayPreferences(displayPreferences, new Guid(userId), client, cancellationToken);
|
2016-11-18 02:28:39 -07:00
|
|
|
|
|
|
|
public DisplayPreferences GetDisplayPreferences(string displayPreferencesId, string userId, string client)
|
2019-08-18 13:05:06 -07:00
|
|
|
=> GetDisplayPreferences(displayPreferencesId, new Guid(userId), client);
|
2016-11-18 02:28:39 -07:00
|
|
|
}
|
2018-12-13 06:18:25 -07:00
|
|
|
}
|