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

248 lines
7.4 KiB
C#
Raw Normal View History

2013-09-26 23:20:26 +02:00
using MediaBrowser.Controller;
using MediaBrowser.Controller.Entities;
2013-06-18 11:43:07 +02: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 23:20:26 +02:00
using System.IO;
2013-06-18 11:43:07 +02:00
using System.Threading;
using System.Threading.Tasks;
namespace MediaBrowser.Server.Implementations.Persistence
{
/// <summary>
/// Class SQLiteUserRepository
/// </summary>
2015-05-03 00:59:01 +02:00
public class SqliteUserRepository : BaseSqliteRepository, IUserRepository
2013-06-18 11:43:07 +02:00
{
private IDbConnection _connection;
2013-09-26 23:20:26 +02:00
private readonly IServerApplicationPaths _appPaths;
2015-05-03 00:59:01 +02:00
private readonly IJsonSerializer _jsonSerializer;
public SqliteUserRepository(ILogManager logManager, IServerApplicationPaths appPaths, IJsonSerializer jsonSerializer) : base(logManager)
{
_appPaths = appPaths;
_jsonSerializer = jsonSerializer;
}
2013-06-18 11:43:07 +02:00
/// <summary>
/// Gets the name of the repository
/// </summary>
/// <value>The name.</value>
public string Name
{
get
{
2013-06-18 21:16:27 +02:00
return "SQLite";
2013-06-18 11:43:07 +02:00
}
}
/// <summary>
/// Opens the connection to the database
/// </summary>
/// <returns>Task.</returns>
2016-05-01 23:48:37 +02:00
public async Task Initialize(IDbConnector dbConnector)
2013-06-18 11:43:07 +02:00
{
2013-09-26 23:20:26 +02:00
var dbFile = Path.Combine(_appPaths.DataPath, "users.db");
2016-05-01 23:48:37 +02:00
_connection = await dbConnector.Connect(dbFile).ConfigureAwait(false);
2013-06-18 11:43:07 +02:00
string[] queries = {
"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 02:42:15 +01:00
2013-06-18 11:43:07 +02:00
//pragmas
2013-12-08 02:42:15 +01:00
"pragma temp_store = memory",
"pragma shrink_memory"
2013-06-18 11:43:07 +02:00
};
2015-05-03 00:59:01 +02:00
_connection.RunQueries(queries, Logger);
2013-06-18 11:43:07 +02: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();
2015-05-03 00:59:01 +02:00
await WriteLock.WaitAsync(cancellationToken).ConfigureAwait(false);
IDbTransaction transaction = null;
2013-06-18 11:43:07 +02:00
try
{
2013-06-18 21:16:27 +02:00
transaction = _connection.BeginTransaction();
2013-06-18 11:43:07 +02:00
2013-06-18 21:16:27 +02:00
using (var cmd = _connection.CreateCommand())
2013-06-18 11:43:07 +02:00
{
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 11:43:07 +02:00
cmd.Transaction = transaction;
cmd.ExecuteNonQuery();
2013-06-18 11:43:07 +02:00
}
transaction.Commit();
}
catch (OperationCanceledException)
{
if (transaction != null)
{
transaction.Rollback();
}
throw;
}
catch (Exception e)
{
2015-05-03 00:59:01 +02:00
Logger.ErrorException("Failed to save user:", e);
2013-06-18 11:43:07 +02:00
if (transaction != null)
{
transaction.Rollback();
}
throw;
}
finally
{
if (transaction != null)
{
transaction.Dispose();
}
2015-05-03 00:59:01 +02:00
WriteLock.Release();
2013-06-18 11:43:07 +02:00
}
}
/// <summary>
/// Retrieve all users from the database
/// </summary>
/// <returns>IEnumerable{User}.</returns>
public IEnumerable<User> RetrieveAllUsers()
{
2013-06-18 21:16:27 +02:00
using (var cmd = _connection.CreateCommand())
2013-06-18 11:43:07 +02:00
{
2015-10-27 18:26:04 +01:00
cmd.CommandText = "select guid,data from users";
2013-06-18 11:43:07 +02:00
using (var reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess | CommandBehavior.SingleResult))
{
while (reader.Read())
{
2015-10-27 18:26:04 +01:00
var id = reader.GetGuid(0);
using (var stream = reader.GetMemoryStream(1))
2013-06-18 11:43:07 +02:00
{
var user = _jsonSerializer.DeserializeFromStream<User>(stream);
2015-10-27 18:26:04 +01:00
user.Id = id;
2013-06-18 11:43:07 +02:00
yield return user;
}
}
}
}
}
/// <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();
2015-05-03 00:59:01 +02:00
await WriteLock.WaitAsync(cancellationToken).ConfigureAwait(false);
2013-06-18 11:43:07 +02:00
IDbTransaction transaction = null;
2013-06-18 11:43:07 +02:00
try
{
2013-06-18 21:16:27 +02:00
transaction = _connection.BeginTransaction();
2013-06-18 11:43:07 +02:00
2013-06-18 21:16:27 +02:00
using (var cmd = _connection.CreateCommand())
2013-06-18 11:43:07 +02:00
{
cmd.CommandText = "delete from users where guid=@guid";
cmd.Parameters.Add(cmd, "@guid", DbType.Guid).Value = user.Id;
2013-06-18 11:43:07 +02:00
cmd.Transaction = transaction;
cmd.ExecuteNonQuery();
2013-06-18 11:43:07 +02:00
}
transaction.Commit();
}
catch (OperationCanceledException)
{
if (transaction != null)
{
transaction.Rollback();
}
throw;
}
catch (Exception e)
{
2015-05-03 00:59:01 +02:00
Logger.ErrorException("Failed to delete user:", e);
2013-06-18 11:43:07 +02:00
if (transaction != null)
{
transaction.Rollback();
}
throw;
}
finally
{
if (transaction != null)
{
transaction.Dispose();
}
2015-05-03 00:59:01 +02:00
WriteLock.Release();
2013-06-18 11:43:07 +02:00
}
}
2013-06-18 21:16:27 +02:00
2015-05-03 00:59:01 +02:00
protected override void CloseConnection()
2013-06-18 21:16:27 +02:00
{
2015-05-03 00:59:01 +02:00
if (_connection != null)
2013-06-18 21:16:27 +02:00
{
2015-05-03 00:59:01 +02:00
if (_connection.IsOpen())
2013-06-18 21:16:27 +02:00
{
2015-05-03 00:59:01 +02:00
_connection.Close();
2013-06-18 21:16:27 +02:00
}
2015-05-03 00:59:01 +02:00
_connection.Dispose();
_connection = null;
2013-06-18 21:16:27 +02:00
}
}
2013-06-18 11:43:07 +02:00
}
}