jellyfin-server/Emby.Server.Implementations/Data/SqliteUserRepository.cs

167 lines
5.6 KiB
C#
Raw Normal View History

2016-11-18 18:28:45 +00:00
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Controller;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Persistence;
using MediaBrowser.Model.IO;
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.Serialization;
using SQLitePCL.pretty;
namespace Emby.Server.Implementations.Data
{
/// <summary>
/// Class SQLiteUserRepository
/// </summary>
public class SqliteUserRepository : BaseSqliteRepository, IUserRepository
{
private readonly IJsonSerializer _jsonSerializer;
private readonly IMemoryStreamFactory _memoryStreamProvider;
public SqliteUserRepository(ILogger logger, IServerApplicationPaths appPaths, IJsonSerializer jsonSerializer, IMemoryStreamFactory memoryStreamProvider)
: base(logger)
{
_jsonSerializer = jsonSerializer;
_memoryStreamProvider = memoryStreamProvider;
DbFilePath = Path.Combine(appPaths.DataPath, "users.db");
}
/// <summary>
/// Gets the name of the repository
/// </summary>
/// <value>The name.</value>
public string Name
{
get
{
return "SQLite";
}
}
/// <summary>
/// Opens the connection to the database
/// </summary>
/// <returns>Task.</returns>
public void Initialize()
{
using (var connection = CreateConnection())
{
2016-11-29 19:12:37 +00:00
RunDefaultInitialization(connection);
2016-11-21 03:52:58 +00:00
2016-11-18 18:28:45 +00: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)",
"pragma shrink_memory"
};
connection.RunQueries(queries);
}
}
/// <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, _memoryStreamProvider);
cancellationToken.ThrowIfCancellationRequested();
2016-12-11 05:12:00 +00:00
using (WriteLock.Write())
2016-11-18 18:28:45 +00:00
{
2016-12-11 05:12:00 +00:00
using (var connection = CreateConnection())
2016-11-18 18:28:45 +00:00
{
connection.RunInTransaction(db =>
{
2016-11-20 05:59:36 +00:00
using (var statement = db.PrepareStatement("replace into users (guid, data) values (@guid, @data)"))
{
2017-05-07 20:02:32 +00:00
statement.TryBind("@guid", user.Id.ToGuidBlob());
2016-11-20 07:10:07 +00:00
statement.TryBind("@data", serialized);
2016-11-20 05:59:36 +00:00
statement.MoveNext();
}
2016-11-28 19:26:48 +00:00
}, TransactionMode);
2016-11-18 18:28:45 +00:00
}
}
}
/// <summary>
/// Retrieve all users from the database
/// </summary>
/// <returns>IEnumerable{User}.</returns>
public IEnumerable<User> RetrieveAllUsers()
{
var list = new List<User>();
2016-12-11 05:12:00 +00:00
using (WriteLock.Read())
2016-11-18 18:28:45 +00:00
{
2016-12-11 05:12:00 +00:00
using (var connection = CreateConnection(true))
2016-11-18 18:28:45 +00:00
{
2016-11-19 08:40:13 +00:00
foreach (var row in connection.Query("select guid,data from users"))
2016-11-18 18:28:45 +00:00
{
2017-05-07 20:02:32 +00:00
var id = row[0].ReadGuidFromBlob();
2016-11-19 08:40:13 +00:00
using (var stream = _memoryStreamProvider.CreateNew(row[1].ToBlob()))
{
stream.Position = 0;
var user = _jsonSerializer.DeserializeFromStream<User>(stream);
user.Id = id;
list.Add(user);
}
2016-11-18 18:28:45 +00:00
}
}
}
return list;
}
/// <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-12-11 05:12:00 +00:00
using (WriteLock.Write())
2016-11-18 18:28:45 +00:00
{
2016-12-11 05:12:00 +00:00
using (var connection = CreateConnection())
2016-11-18 18:28:45 +00:00
{
connection.RunInTransaction(db =>
{
2016-11-20 05:59:36 +00:00
using (var statement = db.PrepareStatement("delete from users where guid=@id"))
{
2017-05-07 20:02:32 +00:00
statement.TryBind("@id", user.Id.ToGuidBlob());
2016-11-20 05:59:36 +00:00
statement.MoveNext();
}
2016-11-28 19:26:48 +00:00
}, TransactionMode);
2016-11-18 18:28:45 +00:00
}
}
}
}
}