jellyfin/MediaBrowser.Server.Implementations/Persistence/SqliteUserRepository.cs

237 lines
7.5 KiB
C#
Raw Normal View History

2013-09-26 21:20:26 +00:00
using MediaBrowser.Controller;
using MediaBrowser.Controller.Entities;
2013-06-18 09:43:07 +00:00
using MediaBrowser.Controller.Persistence;
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.Serialization;
using System;
using System.Collections.Generic;
using System.Data;
2013-09-26 21:20:26 +00:00
using System.IO;
2013-06-18 09:43:07 +00:00
using System.Threading;
using System.Threading.Tasks;
namespace MediaBrowser.Server.Implementations.Persistence
{
/// <summary>
/// Class SQLiteUserRepository
/// </summary>
2015-05-02 22:59:01 +00:00
public class SqliteUserRepository : BaseSqliteRepository, IUserRepository
2013-06-18 09:43:07 +00:00
{
2015-05-02 22:59:01 +00:00
private readonly IJsonSerializer _jsonSerializer;
2016-06-11 15:55:05 +00:00
public SqliteUserRepository(ILogManager logManager, IServerApplicationPaths appPaths, IJsonSerializer jsonSerializer, IDbConnector dbConnector) : base(logManager, dbConnector)
2015-05-02 22:59:01 +00:00
{
_jsonSerializer = jsonSerializer;
2016-06-11 15:55:05 +00:00
DbFilePath = Path.Combine(appPaths.DataPath, "users.db");
2015-05-02 22:59:01 +00:00
}
2013-06-18 09:43:07 +00:00
/// <summary>
/// Gets the name of the repository
/// </summary>
/// <value>The name.</value>
public string Name
{
get
{
2013-06-18 19:16:27 +00:00
return "SQLite";
2013-06-18 09:43:07 +00:00
}
}
/// <summary>
/// Opens the connection to the database
/// </summary>
/// <returns>Task.</returns>
2016-06-11 15:55:05 +00:00
public async Task Initialize()
2013-06-18 09:43:07 +00:00
{
2016-06-11 15:55:05 +00:00
using (var connection = await CreateConnection().ConfigureAwait(false))
{
string[] queries = {
2013-06-18 09:43:07 +00:00
"create table if not exists users (guid GUID primary key, data BLOB)",
"create index if not exists idx_users on users(guid)",
"create table if not exists schema_version (table_name primary key, version)",
2013-12-08 01:42:15 +00:00
2013-06-18 09:43:07 +00:00
//pragmas
2013-12-08 01:42:15 +00:00
"pragma temp_store = memory",
"pragma shrink_memory"
2013-06-18 09:43:07 +00:00
};
2016-06-11 15:55:05 +00:00
connection.RunQueries(queries, Logger);
}
2013-06-18 09:43:07 +00:00
}
/// <summary>
/// Save a user in the repo
/// </summary>
/// <param name="user">The user.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>Task.</returns>
/// <exception cref="System.ArgumentNullException">user</exception>
public async Task SaveUser(User user, CancellationToken cancellationToken)
{
if (user == null)
{
throw new ArgumentNullException("user");
}
cancellationToken.ThrowIfCancellationRequested();
var serialized = _jsonSerializer.SerializeToBytes(user);
cancellationToken.ThrowIfCancellationRequested();
2016-06-11 15:55:05 +00:00
using (var connection = await CreateConnection().ConfigureAwait(false))
2013-06-18 09:43:07 +00:00
{
2016-06-11 15:55:05 +00:00
IDbTransaction transaction = null;
2013-06-18 09:43:07 +00:00
2016-06-11 15:55:05 +00:00
try
2013-06-18 09:43:07 +00:00
{
2016-06-11 15:55:05 +00:00
transaction = connection.BeginTransaction();
2013-06-18 09:43:07 +00:00
2016-06-11 15:55:05 +00:00
using (var cmd = connection.CreateCommand())
{
cmd.CommandText = "replace into users (guid, data) values (@1, @2)";
cmd.Parameters.Add(cmd, "@1", DbType.Guid).Value = user.Id;
cmd.Parameters.Add(cmd, "@2", DbType.Binary).Value = serialized;
2013-06-18 09:43:07 +00:00
2016-06-11 15:55:05 +00:00
cmd.Transaction = transaction;
2013-06-18 09:43:07 +00:00
2016-06-11 15:55:05 +00:00
cmd.ExecuteNonQuery();
}
transaction.Commit();
}
catch (OperationCanceledException)
2013-06-18 09:43:07 +00:00
{
2016-06-11 15:55:05 +00:00
if (transaction != null)
{
transaction.Rollback();
}
throw;
2013-06-18 09:43:07 +00:00
}
2016-06-11 15:55:05 +00:00
catch (Exception e)
{
Logger.ErrorException("Failed to save user:", e);
2013-06-18 09:43:07 +00:00
2016-06-11 15:55:05 +00:00
if (transaction != null)
{
transaction.Rollback();
}
2013-06-18 09:43:07 +00:00
2016-06-11 15:55:05 +00:00
throw;
2013-06-18 09:43:07 +00:00
}
2016-06-11 15:55:05 +00:00
finally
2013-06-18 09:43:07 +00:00
{
2016-06-11 15:55:05 +00:00
if (transaction != null)
{
transaction.Dispose();
}
2013-06-18 09:43:07 +00:00
}
}
}
/// <summary>
/// Retrieve all users from the database
/// </summary>
/// <returns>IEnumerable{User}.</returns>
public IEnumerable<User> RetrieveAllUsers()
{
2016-06-11 15:55:05 +00:00
var list = new List<User>();
2013-06-18 09:43:07 +00:00
2016-06-11 15:55:05 +00:00
using (var connection = CreateConnection(true).Result)
{
using (var cmd = connection.CreateCommand())
2013-06-18 09:43:07 +00:00
{
2016-06-11 15:55:05 +00:00
cmd.CommandText = "select guid,data from users";
2015-10-27 17:26:04 +00:00
2016-06-11 15:55:05 +00:00
using (var reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess | CommandBehavior.SingleResult))
{
while (reader.Read())
2013-06-18 09:43:07 +00:00
{
2016-06-11 15:55:05 +00:00
var id = reader.GetGuid(0);
using (var stream = reader.GetMemoryStream(1))
{
var user = _jsonSerializer.DeserializeFromStream<User>(stream);
user.Id = id;
list.Add(user);
}
2013-06-18 09:43:07 +00:00
}
}
}
}
2016-06-11 15:55:05 +00:00
return list;
2013-06-18 09:43:07 +00:00
}
/// <summary>
/// Deletes the user.
/// </summary>
/// <param name="user">The user.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>Task.</returns>
/// <exception cref="System.ArgumentNullException">user</exception>
public async Task DeleteUser(User user, CancellationToken cancellationToken)
{
if (user == null)
{
throw new ArgumentNullException("user");
}
cancellationToken.ThrowIfCancellationRequested();
2016-06-11 15:55:05 +00:00
using (var connection = await CreateConnection().ConfigureAwait(false))
2013-06-18 09:43:07 +00:00
{
2016-06-11 15:55:05 +00:00
IDbTransaction transaction = null;
2013-06-18 09:43:07 +00:00
2016-06-11 15:55:05 +00:00
try
2013-06-18 09:43:07 +00:00
{
2016-06-11 15:55:05 +00:00
transaction = connection.BeginTransaction();
2013-06-18 09:43:07 +00:00
2016-06-11 15:55:05 +00:00
using (var cmd = connection.CreateCommand())
{
cmd.CommandText = "delete from users where guid=@guid";
2013-06-18 09:43:07 +00:00
2016-06-11 15:55:05 +00:00
cmd.Parameters.Add(cmd, "@guid", DbType.Guid).Value = user.Id;
2013-06-18 09:43:07 +00:00
2016-06-11 15:55:05 +00:00
cmd.Transaction = transaction;
2013-06-18 09:43:07 +00:00
2016-06-11 15:55:05 +00:00
cmd.ExecuteNonQuery();
}
2013-06-18 09:43:07 +00:00
2016-06-11 15:55:05 +00:00
transaction.Commit();
2013-06-18 09:43:07 +00:00
}
2016-06-11 15:55:05 +00:00
catch (OperationCanceledException)
2013-06-18 09:43:07 +00:00
{
2016-06-11 15:55:05 +00:00
if (transaction != null)
{
transaction.Rollback();
}
throw;
2013-06-18 09:43:07 +00:00
}
2016-06-11 15:55:05 +00:00
catch (Exception e)
{
Logger.ErrorException("Failed to delete user:", e);
2013-06-18 09:43:07 +00:00
2016-06-11 15:55:05 +00:00
if (transaction != null)
{
transaction.Rollback();
}
2013-06-18 19:16:27 +00:00
2016-06-11 15:55:05 +00:00
throw;
}
finally
2013-06-18 19:16:27 +00:00
{
2016-06-11 15:55:05 +00:00
if (transaction != null)
{
transaction.Dispose();
}
2013-06-18 19:16:27 +00:00
}
}
}
2013-06-18 09:43:07 +00:00
}
}