SqliteDisplayPreferencesRepository.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. RunDefaultInitialization(connection);
  53. string[] queries = {
  54. "create table if not exists userdisplaypreferences (id GUID, userId GUID, client text, data BLOB)",
  55. "create unique index if not exists userdisplaypreferencesindex on userdisplaypreferences (id, userId, client)"
  56. };
  57. connection.RunQueries(queries);
  58. }
  59. }
  60. /// <summary>
  61. /// Save the display preferences associated with an item in the repo
  62. /// </summary>
  63. /// <param name="displayPreferences">The display preferences.</param>
  64. /// <param name="userId">The user id.</param>
  65. /// <param name="client">The client.</param>
  66. /// <param name="cancellationToken">The cancellation token.</param>
  67. /// <returns>Task.</returns>
  68. /// <exception cref="System.ArgumentNullException">item</exception>
  69. public async Task SaveDisplayPreferences(DisplayPreferences displayPreferences, Guid userId, string client, CancellationToken cancellationToken)
  70. {
  71. if (displayPreferences == null)
  72. {
  73. throw new ArgumentNullException("displayPreferences");
  74. }
  75. if (string.IsNullOrWhiteSpace(displayPreferences.Id))
  76. {
  77. throw new ArgumentNullException("displayPreferences.Id");
  78. }
  79. cancellationToken.ThrowIfCancellationRequested();
  80. using (var connection = CreateConnection())
  81. {
  82. using (WriteLock.Write())
  83. {
  84. connection.RunInTransaction(db =>
  85. {
  86. SaveDisplayPreferences(displayPreferences, userId, client, db);
  87. }, TransactionMode);
  88. }
  89. }
  90. }
  91. private void SaveDisplayPreferences(DisplayPreferences displayPreferences, Guid userId, string client, IDatabaseConnection connection)
  92. {
  93. var serialized = _jsonSerializer.SerializeToBytes(displayPreferences, _memoryStreamProvider);
  94. using (var statement = connection.PrepareStatement("replace into userdisplaypreferences (id, userid, client, data) values (@id, @userId, @client, @data)"))
  95. {
  96. statement.TryBind("@id", displayPreferences.Id.ToGuidParamValue());
  97. statement.TryBind("@userId", userId.ToGuidParamValue());
  98. statement.TryBind("@client", client);
  99. statement.TryBind("@data", serialized);
  100. statement.MoveNext();
  101. }
  102. }
  103. /// <summary>
  104. /// Save all display preferences associated with a user in the repo
  105. /// </summary>
  106. /// <param name="displayPreferences">The display preferences.</param>
  107. /// <param name="userId">The user id.</param>
  108. /// <param name="cancellationToken">The cancellation token.</param>
  109. /// <returns>Task.</returns>
  110. /// <exception cref="System.ArgumentNullException">item</exception>
  111. public async Task SaveAllDisplayPreferences(IEnumerable<DisplayPreferences> displayPreferences, Guid userId, CancellationToken cancellationToken)
  112. {
  113. if (displayPreferences == null)
  114. {
  115. throw new ArgumentNullException("displayPreferences");
  116. }
  117. cancellationToken.ThrowIfCancellationRequested();
  118. using (var connection = CreateConnection())
  119. {
  120. using (WriteLock.Write())
  121. {
  122. connection.RunInTransaction(db =>
  123. {
  124. foreach (var displayPreference in displayPreferences)
  125. {
  126. SaveDisplayPreferences(displayPreference, userId, displayPreference.Client, db);
  127. }
  128. }, TransactionMode);
  129. }
  130. }
  131. }
  132. /// <summary>
  133. /// Gets the display preferences.
  134. /// </summary>
  135. /// <param name="displayPreferencesId">The display preferences id.</param>
  136. /// <param name="userId">The user id.</param>
  137. /// <param name="client">The client.</param>
  138. /// <returns>Task{DisplayPreferences}.</returns>
  139. /// <exception cref="System.ArgumentNullException">item</exception>
  140. public DisplayPreferences GetDisplayPreferences(string displayPreferencesId, Guid userId, string client)
  141. {
  142. if (string.IsNullOrWhiteSpace(displayPreferencesId))
  143. {
  144. throw new ArgumentNullException("displayPreferencesId");
  145. }
  146. var guidId = displayPreferencesId.GetMD5();
  147. using (var connection = CreateConnection(true))
  148. {
  149. using (WriteLock.Read())
  150. {
  151. using (var statement = connection.PrepareStatement("select data from userdisplaypreferences where id = @id and userId=@userId and client=@client"))
  152. {
  153. statement.TryBind("@id", guidId.ToGuidParamValue());
  154. statement.TryBind("@userId", userId.ToGuidParamValue());
  155. statement.TryBind("@client", client);
  156. foreach (var row in statement.ExecuteQuery())
  157. {
  158. return Get(row);
  159. }
  160. }
  161. return new DisplayPreferences
  162. {
  163. Id = guidId.ToString("N")
  164. };
  165. }
  166. }
  167. }
  168. /// <summary>
  169. /// Gets all display preferences for the given user.
  170. /// </summary>
  171. /// <param name="userId">The user id.</param>
  172. /// <returns>Task{DisplayPreferences}.</returns>
  173. /// <exception cref="System.ArgumentNullException">item</exception>
  174. public IEnumerable<DisplayPreferences> GetAllDisplayPreferences(Guid userId)
  175. {
  176. var list = new List<DisplayPreferences>();
  177. using (var connection = CreateConnection(true))
  178. {
  179. using (WriteLock.Read())
  180. {
  181. using (var statement = connection.PrepareStatement("select data from userdisplaypreferences where userId=@userId"))
  182. {
  183. statement.TryBind("@userId", userId.ToGuidParamValue());
  184. foreach (var row in statement.ExecuteQuery())
  185. {
  186. list.Add(Get(row));
  187. }
  188. }
  189. }
  190. }
  191. return list;
  192. }
  193. private DisplayPreferences Get(IReadOnlyList<IResultSetValue> row)
  194. {
  195. using (var stream = _memoryStreamProvider.CreateNew(row[0].ToBlob()))
  196. {
  197. stream.Position = 0;
  198. return _jsonSerializer.DeserializeFromStream<DisplayPreferences>(stream);
  199. }
  200. }
  201. public Task SaveDisplayPreferences(DisplayPreferences displayPreferences, string userId, string client, CancellationToken cancellationToken)
  202. {
  203. return SaveDisplayPreferences(displayPreferences, new Guid(userId), client, cancellationToken);
  204. }
  205. public DisplayPreferences GetDisplayPreferences(string displayPreferencesId, string userId, string client)
  206. {
  207. return GetDisplayPreferences(displayPreferencesId, new Guid(userId), client);
  208. }
  209. }
  210. }