SQLiteUserRepository.cs 5.0 KB

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