SQLiteDisplayPreferencesRepository.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. public 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. /// Gets a value indicating whether [enable delayed commands].
  35. /// </summary>
  36. /// <value><c>true</c> if [enable delayed commands]; otherwise, <c>false</c>.</value>
  37. protected override bool EnableDelayedCommands
  38. {
  39. get
  40. {
  41. return false;
  42. }
  43. }
  44. /// <summary>
  45. /// The _json serializer
  46. /// </summary>
  47. private readonly IJsonSerializer _jsonSerializer;
  48. /// <summary>
  49. /// The _app paths
  50. /// </summary>
  51. private readonly IApplicationPaths _appPaths;
  52. /// <summary>
  53. /// Initializes a new instance of the <see cref="SQLiteUserDataRepository" /> class.
  54. /// </summary>
  55. /// <param name="appPaths">The app paths.</param>
  56. /// <param name="jsonSerializer">The json serializer.</param>
  57. /// <param name="logManager">The log manager.</param>
  58. /// <exception cref="System.ArgumentNullException">
  59. /// jsonSerializer
  60. /// or
  61. /// appPaths
  62. /// </exception>
  63. public SQLiteDisplayPreferencesRepository(IApplicationPaths appPaths, IJsonSerializer jsonSerializer, ILogManager logManager)
  64. : base(logManager)
  65. {
  66. if (jsonSerializer == null)
  67. {
  68. throw new ArgumentNullException("jsonSerializer");
  69. }
  70. if (appPaths == null)
  71. {
  72. throw new ArgumentNullException("appPaths");
  73. }
  74. _jsonSerializer = jsonSerializer;
  75. _appPaths = appPaths;
  76. }
  77. /// <summary>
  78. /// Opens the connection to the database
  79. /// </summary>
  80. /// <returns>Task.</returns>
  81. public async Task Initialize()
  82. {
  83. var dbFile = Path.Combine(_appPaths.DataPath, "displaypreferences.db");
  84. await ConnectToDb(dbFile).ConfigureAwait(false);
  85. string[] queries = {
  86. "create table if not exists displaypreferences (id GUID, data BLOB)",
  87. "create unique index if not exists displaypreferencesindex on displaypreferences (id)",
  88. "create table if not exists schema_version (table_name primary key, version)",
  89. //pragmas
  90. "pragma temp_store = memory"
  91. };
  92. RunQueries(queries);
  93. }
  94. /// <summary>
  95. /// Save the display preferences associated with an item in the repo
  96. /// </summary>
  97. /// <param name="displayPreferences">The display preferences.</param>
  98. /// <param name="cancellationToken">The cancellation token.</param>
  99. /// <returns>Task.</returns>
  100. /// <exception cref="System.ArgumentNullException">item</exception>
  101. public async Task SaveDisplayPreferences(DisplayPreferences displayPreferences, CancellationToken cancellationToken)
  102. {
  103. if (displayPreferences == null)
  104. {
  105. throw new ArgumentNullException("displayPreferences");
  106. }
  107. if (displayPreferences.Id == Guid.Empty)
  108. {
  109. throw new ArgumentNullException("displayPreferences.Id");
  110. }
  111. if (cancellationToken == null)
  112. {
  113. throw new ArgumentNullException("cancellationToken");
  114. }
  115. cancellationToken.ThrowIfCancellationRequested();
  116. var serialized = _jsonSerializer.SerializeToBytes(displayPreferences);
  117. cancellationToken.ThrowIfCancellationRequested();
  118. var cmd = connection.CreateCommand();
  119. cmd.CommandText = "replace into displaypreferences (id, data) values (@1, @2)";
  120. cmd.AddParam("@1", displayPreferences.Id);
  121. cmd.AddParam("@2", serialized);
  122. using (var tran = connection.BeginTransaction())
  123. {
  124. try
  125. {
  126. cmd.Transaction = tran;
  127. await cmd.ExecuteNonQueryAsync(cancellationToken);
  128. tran.Commit();
  129. }
  130. catch (OperationCanceledException)
  131. {
  132. tran.Rollback();
  133. }
  134. catch (Exception e)
  135. {
  136. Logger.ErrorException("Failed to commit transaction.", e);
  137. tran.Rollback();
  138. }
  139. }
  140. }
  141. /// <summary>
  142. /// Gets the display preferences.
  143. /// </summary>
  144. /// <param name="displayPreferencesId">The display preferences id.</param>
  145. /// <returns>Task{DisplayPreferences}.</returns>
  146. /// <exception cref="System.ArgumentNullException">item</exception>
  147. public async Task<DisplayPreferences> GetDisplayPreferences(Guid displayPreferencesId)
  148. {
  149. if (displayPreferencesId == Guid.Empty)
  150. {
  151. throw new ArgumentNullException("displayPreferencesId");
  152. }
  153. var cmd = connection.CreateCommand();
  154. cmd.CommandText = "select data from displaypreferences where id = @id";
  155. var idParam = cmd.Parameters.Add("@id", DbType.Guid);
  156. idParam.Value = displayPreferencesId;
  157. using (var reader = await cmd.ExecuteReaderAsync(CommandBehavior.SequentialAccess | CommandBehavior.SingleResult | CommandBehavior.SingleRow).ConfigureAwait(false))
  158. {
  159. if (reader.Read())
  160. {
  161. using (var stream = GetStream(reader, 0))
  162. {
  163. return _jsonSerializer.DeserializeFromStream<DisplayPreferences>(stream);
  164. }
  165. }
  166. }
  167. return null;
  168. }
  169. }
  170. }