using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Common.Configuration;
using MediaBrowser.Common.Extensions;
using MediaBrowser.Controller.Persistence;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.IO;
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.Serialization;
using SQLitePCL.pretty;
namespace Emby.Server.Implementations.Data
{
///
/// Class SQLiteDisplayPreferencesRepository
///
public class SqliteDisplayPreferencesRepository : BaseSqliteRepository, IDisplayPreferencesRepository
{
private readonly IMemoryStreamFactory _memoryStreamProvider;
public SqliteDisplayPreferencesRepository(ILogger logger, IJsonSerializer jsonSerializer, IApplicationPaths appPaths, IMemoryStreamFactory memoryStreamProvider)
: base(logger)
{
_jsonSerializer = jsonSerializer;
_memoryStreamProvider = memoryStreamProvider;
DbFilePath = Path.Combine(appPaths.DataPath, "displaypreferences.db");
}
///
/// Gets the name of the repository
///
/// The name.
public string Name
{
get
{
return "SQLite";
}
}
///
/// The _json serializer
///
private readonly IJsonSerializer _jsonSerializer;
///
/// Opens the connection to the database
///
/// Task.
public void Initialize()
{
using (var connection = CreateConnection())
{
string[] queries = {
"create table if not exists userdisplaypreferences (id GUID, userId GUID, client text, data BLOB)",
"create unique index if not exists userdisplaypreferencesindex on userdisplaypreferences (id, userId, client)"
};
connection.RunQueries(queries);
}
}
///
/// Save the display preferences associated with an item in the repo
///
/// The display preferences.
/// The user id.
/// The client.
/// The cancellation token.
/// Task.
/// item
public async Task SaveDisplayPreferences(DisplayPreferences displayPreferences, Guid userId, string client, CancellationToken cancellationToken)
{
if (displayPreferences == null)
{
throw new ArgumentNullException("displayPreferences");
}
if (string.IsNullOrWhiteSpace(displayPreferences.Id))
{
throw new ArgumentNullException("displayPreferences.Id");
}
cancellationToken.ThrowIfCancellationRequested();
lock (WriteLock)
{
using (var connection = CreateConnection())
{
connection.RunInTransaction(db =>
{
SaveDisplayPreferences(displayPreferences, userId, client, db);
});
}
}
}
private void SaveDisplayPreferences(DisplayPreferences displayPreferences, Guid userId, string client, IDatabaseConnection connection)
{
var commandText = "replace into userdisplaypreferences (id, userid, client, data) values (?, ?, ?, ?)";
var serialized = _jsonSerializer.SerializeToBytes(displayPreferences, _memoryStreamProvider);
connection.Execute(commandText,
displayPreferences.Id.ToGuidParamValue(),
userId.ToGuidParamValue(),
client,
serialized);
}
///
/// Save all display preferences associated with a user in the repo
///
/// The display preferences.
/// The user id.
/// The cancellation token.
/// Task.
/// item
public async Task SaveAllDisplayPreferences(IEnumerable displayPreferences, Guid userId, CancellationToken cancellationToken)
{
if (displayPreferences == null)
{
throw new ArgumentNullException("displayPreferences");
}
cancellationToken.ThrowIfCancellationRequested();
lock (WriteLock)
{
using (var connection = CreateConnection())
{
connection.RunInTransaction(db =>
{
foreach (var displayPreference in displayPreferences)
{
SaveDisplayPreferences(displayPreference, userId, displayPreference.Client, db);
}
});
}
}
}
///
/// Gets the display preferences.
///
/// The display preferences id.
/// The user id.
/// The client.
/// Task{DisplayPreferences}.
/// item
public DisplayPreferences GetDisplayPreferences(string displayPreferencesId, Guid userId, string client)
{
if (string.IsNullOrWhiteSpace(displayPreferencesId))
{
throw new ArgumentNullException("displayPreferencesId");
}
var guidId = displayPreferencesId.GetMD5();
using (var connection = CreateConnection(true))
{
var commandText = "select data from userdisplaypreferences where id = ? and userId=? and client=?";
var paramList = new List