SQLiteDisplayPreferencesRepository.cs 6.6 KB

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