From 13abfbc3b84079f43dcf1da7731c3360192c7c96 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Fri, 18 Nov 2016 13:28:45 -0500 Subject: rework users repository --- .../Data/SqliteUserRepository.cs | 160 +++++++++++++++++++++ 1 file changed, 160 insertions(+) create mode 100644 Emby.Server.Implementations/Data/SqliteUserRepository.cs (limited to 'Emby.Server.Implementations/Data/SqliteUserRepository.cs') diff --git a/Emby.Server.Implementations/Data/SqliteUserRepository.cs b/Emby.Server.Implementations/Data/SqliteUserRepository.cs new file mode 100644 index 000000000..5e4b1c7b8 --- /dev/null +++ b/Emby.Server.Implementations/Data/SqliteUserRepository.cs @@ -0,0 +1,160 @@ +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 +{ + /// + /// Class SQLiteUserRepository + /// + 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"); + } + + /// + /// Gets the name of the repository + /// + /// The name. + public string Name + { + get + { + return "SQLite"; + } + } + + /// + /// Opens the connection to the database + /// + /// Task. + public void Initialize() + { + using (var connection = CreateConnection()) + { + 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); + } + } + + /// + /// Save a user in the repo + /// + /// The user. + /// The cancellation token. + /// Task. + /// user + 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(); + + lock (WriteLock) + { + using (var connection = CreateConnection()) + { + connection.RunInTransaction(db => + { + var commandText = "replace into users (guid, data) values (?, ?)"; + + db.Execute(commandText, + user.Id.ToGuidParamValue(), + serialized); + }); + } + } + } + + /// + /// Retrieve all users from the database + /// + /// IEnumerable{User}. + public IEnumerable RetrieveAllUsers() + { + var list = new List(); + + using (var connection = CreateConnection(true)) + { + foreach (var row in connection.Query("select guid,data from users")) + { + var id = row[0].ReadGuid(); + + using (var stream = _memoryStreamProvider.CreateNew(row[1].ToBlob())) + { + stream.Position = 0; + var user = _jsonSerializer.DeserializeFromStream(stream); + user.Id = id; + list.Add(user); + } + } + } + + return list; + } + + /// + /// Deletes the user. + /// + /// The user. + /// The cancellation token. + /// Task. + /// user + public async Task DeleteUser(User user, CancellationToken cancellationToken) + { + if (user == null) + { + throw new ArgumentNullException("user"); + } + + cancellationToken.ThrowIfCancellationRequested(); + + lock (WriteLock) + { + using (var connection = CreateConnection()) + { + connection.RunInTransaction(db => + { + var commandText = "delete from users where guid=?"; + + db.Execute(commandText, + user.Id.ToGuidParamValue()); + }); + } + } + } + } +} \ No newline at end of file -- cgit v1.2.3