SQLiteDisplayPreferencesRepository.cs 6.5 KB

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