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;
|
2016-10-06 11:55:01 -07:00
|
|
|
|
using MediaBrowser.Common.IO;
|
2016-10-23 12:14:57 -07:00
|
|
|
|
using MediaBrowser.Model.IO;
|
2013-06-18 02:43:07 -07:00
|
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
2015-05-02 15:59:01 -07:00
|
|
|
|
private readonly IJsonSerializer _jsonSerializer;
|
2016-10-06 11:55:01 -07:00
|
|
|
|
private readonly IMemoryStreamProvider _memoryStreamProvider;
|
2015-05-02 15:59:01 -07:00
|
|
|
|
|
2016-10-06 11:55:01 -07:00
|
|
|
|
public SqliteUserRepository(ILogManager logManager, IServerApplicationPaths appPaths, IJsonSerializer jsonSerializer, IDbConnector dbConnector, IMemoryStreamProvider memoryStreamProvider) : base(logManager, dbConnector)
|
2015-05-02 15:59:01 -07:00
|
|
|
|
{
|
|
|
|
|
_jsonSerializer = jsonSerializer;
|
2016-10-06 11:55:01 -07:00
|
|
|
|
_memoryStreamProvider = memoryStreamProvider;
|
2016-06-11 08:55:05 -07:00
|
|
|
|
|
|
|
|
|
DbFilePath = Path.Combine(appPaths.DataPath, "users.db");
|
2015-05-02 15:59:01 -07:00
|
|
|
|
}
|
2013-08-09 17:46:34 -07:00
|
|
|
|
|
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>
|
2016-06-11 08:55:05 -07:00
|
|
|
|
public async Task Initialize()
|
2013-06-18 02:43:07 -07:00
|
|
|
|
{
|
2016-06-11 08:55:05 -07:00
|
|
|
|
using (var connection = await CreateConnection().ConfigureAwait(false))
|
|
|
|
|
{
|
|
|
|
|
string[] queries = {
|
2013-06-18 02:43:07 -07: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-07 18:42:15 -07:00
|
|
|
|
|
|
|
|
|
"pragma shrink_memory"
|
2013-06-18 02:43:07 -07:00
|
|
|
|
};
|
|
|
|
|
|
2016-06-11 08:55:05 -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();
|
|
|
|
|
|
2016-10-06 11:55:01 -07:00
|
|
|
|
var serialized = _jsonSerializer.SerializeToBytes(user, _memoryStreamProvider);
|
2013-06-18 02:43:07 -07:00
|
|
|
|
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
|
2016-06-11 08:55:05 -07:00
|
|
|
|
using (var connection = await CreateConnection().ConfigureAwait(false))
|
2013-06-18 02:43:07 -07:00
|
|
|
|
{
|
2016-06-11 08:55:05 -07:00
|
|
|
|
IDbTransaction transaction = null;
|
2013-06-18 02:43:07 -07:00
|
|
|
|
|
2016-06-11 08:55:05 -07:00
|
|
|
|
try
|
2013-06-18 02:43:07 -07:00
|
|
|
|
{
|
2016-06-11 08:55:05 -07:00
|
|
|
|
transaction = connection.BeginTransaction();
|
2013-06-18 02:43:07 -07:00
|
|
|
|
|
2016-06-11 08:55:05 -07: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 02:43:07 -07:00
|
|
|
|
|
2016-06-11 08:55:05 -07:00
|
|
|
|
cmd.Transaction = transaction;
|
2013-06-18 02:43:07 -07:00
|
|
|
|
|
2016-06-11 08:55:05 -07:00
|
|
|
|
cmd.ExecuteNonQuery();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
transaction.Commit();
|
|
|
|
|
}
|
|
|
|
|
catch (OperationCanceledException)
|
2013-06-18 02:43:07 -07:00
|
|
|
|
{
|
2016-06-11 08:55:05 -07:00
|
|
|
|
if (transaction != null)
|
|
|
|
|
{
|
|
|
|
|
transaction.Rollback();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw;
|
2013-06-18 02:43:07 -07:00
|
|
|
|
}
|
2016-06-11 08:55:05 -07:00
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
Logger.ErrorException("Failed to save user:", e);
|
2013-06-18 02:43:07 -07:00
|
|
|
|
|
2016-06-11 08:55:05 -07:00
|
|
|
|
if (transaction != null)
|
|
|
|
|
{
|
|
|
|
|
transaction.Rollback();
|
|
|
|
|
}
|
2013-06-18 02:43:07 -07:00
|
|
|
|
|
2016-06-11 08:55:05 -07:00
|
|
|
|
throw;
|
2013-06-18 02:43:07 -07:00
|
|
|
|
}
|
2016-06-11 08:55:05 -07:00
|
|
|
|
finally
|
2013-06-18 02:43:07 -07:00
|
|
|
|
{
|
2016-06-11 08:55:05 -07:00
|
|
|
|
if (transaction != null)
|
|
|
|
|
{
|
|
|
|
|
transaction.Dispose();
|
|
|
|
|
}
|
2013-06-18 02:43:07 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Retrieve all users from the database
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>IEnumerable{User}.</returns>
|
|
|
|
|
public IEnumerable<User> RetrieveAllUsers()
|
|
|
|
|
{
|
2016-06-11 08:55:05 -07:00
|
|
|
|
var list = new List<User>();
|
2013-06-18 02:43:07 -07:00
|
|
|
|
|
2016-06-11 08:55:05 -07:00
|
|
|
|
using (var connection = CreateConnection(true).Result)
|
|
|
|
|
{
|
|
|
|
|
using (var cmd = connection.CreateCommand())
|
2013-06-18 02:43:07 -07:00
|
|
|
|
{
|
2016-06-11 08:55:05 -07:00
|
|
|
|
cmd.CommandText = "select guid,data from users";
|
2015-10-27 10:26:04 -07:00
|
|
|
|
|
2016-06-11 08:55:05 -07:00
|
|
|
|
using (var reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess | CommandBehavior.SingleResult))
|
|
|
|
|
{
|
|
|
|
|
while (reader.Read())
|
2013-06-18 02:43:07 -07:00
|
|
|
|
{
|
2016-06-11 08:55:05 -07:00
|
|
|
|
var id = reader.GetGuid(0);
|
|
|
|
|
|
2016-10-06 11:55:01 -07:00
|
|
|
|
using (var stream = reader.GetMemoryStream(1, _memoryStreamProvider))
|
2016-06-11 08:55:05 -07:00
|
|
|
|
{
|
|
|
|
|
var user = _jsonSerializer.DeserializeFromStream<User>(stream);
|
|
|
|
|
user.Id = id;
|
|
|
|
|
list.Add(user);
|
|
|
|
|
}
|
2013-06-18 02:43:07 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-06-11 08:55:05 -07:00
|
|
|
|
|
|
|
|
|
return list;
|
2013-06-18 02:43:07 -07: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 08:55:05 -07:00
|
|
|
|
using (var connection = await CreateConnection().ConfigureAwait(false))
|
2013-06-18 02:43:07 -07:00
|
|
|
|
{
|
2016-06-11 08:55:05 -07:00
|
|
|
|
IDbTransaction transaction = null;
|
2013-06-18 02:43:07 -07:00
|
|
|
|
|
2016-06-11 08:55:05 -07:00
|
|
|
|
try
|
2013-06-18 02:43:07 -07:00
|
|
|
|
{
|
2016-06-11 08:55:05 -07:00
|
|
|
|
transaction = connection.BeginTransaction();
|
2013-06-18 02:43:07 -07:00
|
|
|
|
|
2016-06-11 08:55:05 -07:00
|
|
|
|
using (var cmd = connection.CreateCommand())
|
|
|
|
|
{
|
|
|
|
|
cmd.CommandText = "delete from users where guid=@guid";
|
2013-06-18 02:43:07 -07:00
|
|
|
|
|
2016-06-11 08:55:05 -07:00
|
|
|
|
cmd.Parameters.Add(cmd, "@guid", DbType.Guid).Value = user.Id;
|
2013-06-18 02:43:07 -07:00
|
|
|
|
|
2016-06-11 08:55:05 -07:00
|
|
|
|
cmd.Transaction = transaction;
|
2013-06-18 02:43:07 -07:00
|
|
|
|
|
2016-06-11 08:55:05 -07:00
|
|
|
|
cmd.ExecuteNonQuery();
|
|
|
|
|
}
|
2013-06-18 02:43:07 -07:00
|
|
|
|
|
2016-06-11 08:55:05 -07:00
|
|
|
|
transaction.Commit();
|
2013-06-18 02:43:07 -07:00
|
|
|
|
}
|
2016-06-11 08:55:05 -07:00
|
|
|
|
catch (OperationCanceledException)
|
2013-06-18 02:43:07 -07:00
|
|
|
|
{
|
2016-06-11 08:55:05 -07:00
|
|
|
|
if (transaction != null)
|
|
|
|
|
{
|
|
|
|
|
transaction.Rollback();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw;
|
2013-06-18 02:43:07 -07:00
|
|
|
|
}
|
2016-06-11 08:55:05 -07:00
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
Logger.ErrorException("Failed to delete user:", e);
|
2013-06-18 02:43:07 -07:00
|
|
|
|
|
2016-06-11 08:55:05 -07:00
|
|
|
|
if (transaction != null)
|
|
|
|
|
{
|
|
|
|
|
transaction.Rollback();
|
|
|
|
|
}
|
2013-06-18 12:16:27 -07:00
|
|
|
|
|
2016-06-11 08:55:05 -07:00
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
finally
|
2013-06-18 12:16:27 -07:00
|
|
|
|
{
|
2016-06-11 08:55:05 -07:00
|
|
|
|
if (transaction != null)
|
|
|
|
|
{
|
|
|
|
|
transaction.Dispose();
|
|
|
|
|
}
|
2013-06-18 12:16:27 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-06-18 02:43:07 -07:00
|
|
|
|
}
|
|
|
|
|
}
|