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

245 lines
7.3 KiB
C#
Raw Normal View History

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