SQLiteUserRepository.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. using MediaBrowser.Common.Configuration;
  2. using MediaBrowser.Common.Kernel;
  3. using MediaBrowser.Controller.Entities;
  4. using MediaBrowser.Controller.Persistence;
  5. using MediaBrowser.Model.Logging;
  6. using MediaBrowser.Model.Serialization;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Data;
  10. using System.IO;
  11. using System.Threading;
  12. using System.Threading.Tasks;
  13. namespace MediaBrowser.Server.Implementations.Sqlite
  14. {
  15. /// <summary>
  16. /// Class SQLiteUserRepository
  17. /// </summary>
  18. public class SQLiteUserRepository : SqliteRepository, IUserRepository
  19. {
  20. /// <summary>
  21. /// The repository name
  22. /// </summary>
  23. public const string RepositoryName = "SQLite";
  24. /// <summary>
  25. /// Gets the name of the repository
  26. /// </summary>
  27. /// <value>The name.</value>
  28. public string Name
  29. {
  30. get
  31. {
  32. return RepositoryName;
  33. }
  34. }
  35. /// <summary>
  36. /// Gets the json serializer.
  37. /// </summary>
  38. /// <value>The json serializer.</value>
  39. private readonly IJsonSerializer _jsonSerializer;
  40. /// <summary>
  41. /// The _app paths
  42. /// </summary>
  43. private readonly IApplicationPaths _appPaths;
  44. /// <summary>
  45. /// Initializes a new instance of the <see cref="SQLiteUserDataRepository" /> class.
  46. /// </summary>
  47. /// <param name="appPaths">The app paths.</param>
  48. /// <param name="jsonSerializer">The json serializer.</param>
  49. /// <param name="logger">The logger.</param>
  50. /// <exception cref="System.ArgumentNullException">appPaths</exception>
  51. public SQLiteUserRepository(IApplicationPaths appPaths, IJsonSerializer jsonSerializer, ILogger logger)
  52. : base(logger)
  53. {
  54. if (appPaths == null)
  55. {
  56. throw new ArgumentNullException("appPaths");
  57. }
  58. if (jsonSerializer == null)
  59. {
  60. throw new ArgumentNullException("jsonSerializer");
  61. }
  62. _appPaths = appPaths;
  63. _jsonSerializer = jsonSerializer;
  64. }
  65. /// <summary>
  66. /// Opens the connection to the database
  67. /// </summary>
  68. /// <returns>Task.</returns>
  69. public async Task Initialize()
  70. {
  71. var dbFile = Path.Combine(_appPaths.DataPath, "users.db");
  72. await ConnectToDB(dbFile).ConfigureAwait(false);
  73. string[] queries = {
  74. "create table if not exists users (guid GUID primary key, data BLOB)",
  75. "create index if not exists idx_users on users(guid)",
  76. "create table if not exists schema_version (table_name primary key, version)",
  77. //pragmas
  78. "pragma temp_store = memory"
  79. };
  80. RunQueries(queries);
  81. }
  82. /// <summary>
  83. /// Save a user in the repo
  84. /// </summary>
  85. /// <param name="user">The user.</param>
  86. /// <param name="cancellationToken">The cancellation token.</param>
  87. /// <returns>Task.</returns>
  88. /// <exception cref="System.ArgumentNullException">user</exception>
  89. public Task SaveUser(User user, CancellationToken cancellationToken)
  90. {
  91. if (user == null)
  92. {
  93. throw new ArgumentNullException("user");
  94. }
  95. if (cancellationToken == null)
  96. {
  97. throw new ArgumentNullException("cancellationToken");
  98. }
  99. return Task.Run(() =>
  100. {
  101. cancellationToken.ThrowIfCancellationRequested();
  102. var serialized = _jsonSerializer.SerializeToBytes(user);
  103. cancellationToken.ThrowIfCancellationRequested();
  104. var cmd = connection.CreateCommand();
  105. cmd.CommandText = "replace into users (guid, data) values (@1, @2)";
  106. cmd.AddParam("@1", user.Id);
  107. cmd.AddParam("@2", serialized);
  108. QueueCommand(cmd);
  109. });
  110. }
  111. /// <summary>
  112. /// Retrieve all users from the database
  113. /// </summary>
  114. /// <returns>IEnumerable{User}.</returns>
  115. public IEnumerable<User> RetrieveAllUsers()
  116. {
  117. var cmd = connection.CreateCommand();
  118. cmd.CommandText = "select data from users";
  119. using (var reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess | CommandBehavior.SingleResult))
  120. {
  121. while (reader.Read())
  122. {
  123. using (var stream = GetStream(reader, 0))
  124. {
  125. var user = _jsonSerializer.DeserializeFromStream<User>(stream);
  126. yield return user;
  127. }
  128. }
  129. }
  130. }
  131. /// <summary>
  132. /// Deletes the user.
  133. /// </summary>
  134. /// <param name="user">The user.</param>
  135. /// <param name="cancellationToken">The cancellation token.</param>
  136. /// <returns>Task.</returns>
  137. /// <exception cref="System.ArgumentNullException">user</exception>
  138. public Task DeleteUser(User user, CancellationToken cancellationToken)
  139. {
  140. if (user == null)
  141. {
  142. throw new ArgumentNullException("user");
  143. }
  144. if (cancellationToken == null)
  145. {
  146. throw new ArgumentNullException("cancellationToken");
  147. }
  148. return Task.Run(() =>
  149. {
  150. cancellationToken.ThrowIfCancellationRequested();
  151. var cmd = connection.CreateCommand();
  152. cmd.CommandText = "delete from users where guid=@guid";
  153. var guidParam = cmd.Parameters.Add("@guid", DbType.Guid);
  154. guidParam.Value = user.Id;
  155. return ExecuteCommand(cmd);
  156. });
  157. }
  158. }
  159. }