SqliteDisplayPreferencesRepository.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using MediaBrowser.Common.Configuration;
  7. using MediaBrowser.Common.Extensions;
  8. using MediaBrowser.Controller.Persistence;
  9. using MediaBrowser.Model.Entities;
  10. using MediaBrowser.Model.IO;
  11. using MediaBrowser.Model.Logging;
  12. using MediaBrowser.Model.Serialization;
  13. using SQLitePCL.pretty;
  14. namespace Emby.Server.Implementations.Data
  15. {
  16. /// <summary>
  17. /// Class SQLiteDisplayPreferencesRepository
  18. /// </summary>
  19. public class SqliteDisplayPreferencesRepository : BaseSqliteRepository, IDisplayPreferencesRepository
  20. {
  21. private readonly IMemoryStreamFactory _memoryStreamProvider;
  22. public SqliteDisplayPreferencesRepository(ILogger logger, IJsonSerializer jsonSerializer, IApplicationPaths appPaths, IMemoryStreamFactory memoryStreamProvider)
  23. : base(logger)
  24. {
  25. _jsonSerializer = jsonSerializer;
  26. _memoryStreamProvider = memoryStreamProvider;
  27. DbFilePath = Path.Combine(appPaths.DataPath, "displaypreferences.db");
  28. }
  29. /// <summary>
  30. /// Gets the name of the repository
  31. /// </summary>
  32. /// <value>The name.</value>
  33. public string Name
  34. {
  35. get
  36. {
  37. return "SQLite";
  38. }
  39. }
  40. /// <summary>
  41. /// The _json serializer
  42. /// </summary>
  43. private readonly IJsonSerializer _jsonSerializer;
  44. /// <summary>
  45. /// Opens the connection to the database
  46. /// </summary>
  47. /// <returns>Task.</returns>
  48. public void Initialize()
  49. {
  50. using (var connection = CreateConnection())
  51. {
  52. string[] queries = {
  53. "create table if not exists userdisplaypreferences (id GUID, userId GUID, client text, data BLOB)",
  54. "create unique index if not exists userdisplaypreferencesindex on userdisplaypreferences (id, userId, client)"
  55. };
  56. connection.RunQueries(queries);
  57. }
  58. }
  59. /// <summary>
  60. /// Save the display preferences associated with an item in the repo
  61. /// </summary>
  62. /// <param name="displayPreferences">The display preferences.</param>
  63. /// <param name="userId">The user id.</param>
  64. /// <param name="client">The client.</param>
  65. /// <param name="cancellationToken">The cancellation token.</param>
  66. /// <returns>Task.</returns>
  67. /// <exception cref="System.ArgumentNullException">item</exception>
  68. public async Task SaveDisplayPreferences(DisplayPreferences displayPreferences, Guid userId, string client, CancellationToken cancellationToken)
  69. {
  70. if (displayPreferences == null)
  71. {
  72. throw new ArgumentNullException("displayPreferences");
  73. }
  74. if (string.IsNullOrWhiteSpace(displayPreferences.Id))
  75. {
  76. throw new ArgumentNullException("displayPreferences.Id");
  77. }
  78. cancellationToken.ThrowIfCancellationRequested();
  79. lock (WriteLock)
  80. {
  81. using (var connection = CreateConnection())
  82. {
  83. connection.RunInTransaction(db =>
  84. {
  85. SaveDisplayPreferences(displayPreferences, userId, client, db);
  86. });
  87. }
  88. }
  89. }
  90. private void SaveDisplayPreferences(DisplayPreferences displayPreferences, Guid userId, string client, IDatabaseConnection connection)
  91. {
  92. var commandText = "replace into userdisplaypreferences (id, userid, client, data) values (?, ?, ?, ?)";
  93. var serialized = _jsonSerializer.SerializeToBytes(displayPreferences, _memoryStreamProvider);
  94. connection.Execute(commandText,
  95. displayPreferences.Id.ToGuidParamValue(),
  96. userId.ToGuidParamValue(),
  97. client,
  98. serialized);
  99. }
  100. /// <summary>
  101. /// Save all display preferences associated with a user in the repo
  102. /// </summary>
  103. /// <param name="displayPreferences">The display preferences.</param>
  104. /// <param name="userId">The user id.</param>
  105. /// <param name="cancellationToken">The cancellation token.</param>
  106. /// <returns>Task.</returns>
  107. /// <exception cref="System.ArgumentNullException">item</exception>
  108. public async Task SaveAllDisplayPreferences(IEnumerable<DisplayPreferences> displayPreferences, Guid userId, CancellationToken cancellationToken)
  109. {
  110. if (displayPreferences == null)
  111. {
  112. throw new ArgumentNullException("displayPreferences");
  113. }
  114. cancellationToken.ThrowIfCancellationRequested();
  115. lock (WriteLock)
  116. {
  117. using (var connection = CreateConnection())
  118. {
  119. connection.RunInTransaction(db =>
  120. {
  121. foreach (var displayPreference in displayPreferences)
  122. {
  123. SaveDisplayPreferences(displayPreference, userId, displayPreference.Client, db);
  124. }
  125. });
  126. }
  127. }
  128. }
  129. /// <summary>
  130. /// Gets the display preferences.
  131. /// </summary>
  132. /// <param name="displayPreferencesId">The display preferences id.</param>
  133. /// <param name="userId">The user id.</param>
  134. /// <param name="client">The client.</param>
  135. /// <returns>Task{DisplayPreferences}.</returns>
  136. /// <exception cref="System.ArgumentNullException">item</exception>
  137. public DisplayPreferences GetDisplayPreferences(string displayPreferencesId, Guid userId, string client)
  138. {
  139. if (string.IsNullOrWhiteSpace(displayPreferencesId))
  140. {
  141. throw new ArgumentNullException("displayPreferencesId");
  142. }
  143. var guidId = displayPreferencesId.GetMD5();
  144. using (var connection = CreateConnection(true))
  145. {
  146. var commandText = "select data from userdisplaypreferences where id = ? and userId=? and client=?";
  147. var paramList = new List<object>();
  148. paramList.Add(guidId.ToGuidParamValue());
  149. paramList.Add(userId.ToGuidParamValue());
  150. paramList.Add(client);
  151. foreach (var row in connection.Query(commandText, paramList.ToArray()))
  152. {
  153. return Get(row);
  154. }
  155. return new DisplayPreferences
  156. {
  157. Id = guidId.ToString("N")
  158. };
  159. }
  160. }
  161. /// <summary>
  162. /// Gets all display preferences for the given user.
  163. /// </summary>
  164. /// <param name="userId">The user id.</param>
  165. /// <returns>Task{DisplayPreferences}.</returns>
  166. /// <exception cref="System.ArgumentNullException">item</exception>
  167. public IEnumerable<DisplayPreferences> GetAllDisplayPreferences(Guid userId)
  168. {
  169. var list = new List<DisplayPreferences>();
  170. using (var connection = CreateConnection(true))
  171. {
  172. var commandText = "select data from userdisplaypreferences where userId=?";
  173. var paramList = new List<object>();
  174. paramList.Add(userId.ToGuidParamValue());
  175. foreach (var row in connection.Query(commandText, paramList.ToArray()))
  176. {
  177. list.Add(Get(row));
  178. }
  179. }
  180. return list;
  181. }
  182. private DisplayPreferences Get(IReadOnlyList<IResultSetValue> row)
  183. {
  184. using (var stream = _memoryStreamProvider.CreateNew(row[0].ToBlob()))
  185. {
  186. stream.Position = 0;
  187. return _jsonSerializer.DeserializeFromStream<DisplayPreferences>(stream);
  188. }
  189. }
  190. public Task SaveDisplayPreferences(DisplayPreferences displayPreferences, string userId, string client, CancellationToken cancellationToken)
  191. {
  192. return SaveDisplayPreferences(displayPreferences, new Guid(userId), client, cancellationToken);
  193. }
  194. public DisplayPreferences GetDisplayPreferences(string displayPreferencesId, string userId, string client)
  195. {
  196. return GetDisplayPreferences(displayPreferencesId, new Guid(userId), client);
  197. }
  198. }
  199. }