SQLiteDisplayPreferencesRepository.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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="logManager">The log manager.</param>
  47. /// <exception cref="System.ArgumentNullException">protobufSerializer</exception>
  48. public SQLiteDisplayPreferencesRepository(IApplicationPaths appPaths, IProtobufSerializer protobufSerializer, ILogManager logManager)
  49. : base(logManager)
  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, data BLOB)",
  72. "create unique index if not exists displaypreferencesindex on displaypreferences (id)",
  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="displayPreferences">The display preferences.</param>
  83. /// <param name="cancellationToken">The cancellation token.</param>
  84. /// <returns>Task.</returns>
  85. /// <exception cref="System.ArgumentNullException">item</exception>
  86. public async Task SaveDisplayPreferences(DisplayPreferences displayPreferences, CancellationToken cancellationToken)
  87. {
  88. if (displayPreferences == null)
  89. {
  90. throw new ArgumentNullException("displayPreferences");
  91. }
  92. if (displayPreferences.Id == Guid.Empty)
  93. {
  94. throw new ArgumentNullException("displayPreferences.Id");
  95. }
  96. if (cancellationToken == null)
  97. {
  98. throw new ArgumentNullException("cancellationToken");
  99. }
  100. cancellationToken.ThrowIfCancellationRequested();
  101. var serialized = _protobufSerializer.SerializeToBytes(displayPreferences);
  102. cancellationToken.ThrowIfCancellationRequested();
  103. var cmd = connection.CreateCommand();
  104. cmd.CommandText = "replace into displaypreferences (id, data) values (@1, @2)";
  105. cmd.AddParam("@1", displayPreferences.Id);
  106. cmd.AddParam("@2", serialized);
  107. using (var tran = connection.BeginTransaction())
  108. {
  109. try
  110. {
  111. cmd.Transaction = tran;
  112. await cmd.ExecuteNonQueryAsync(cancellationToken);
  113. tran.Commit();
  114. }
  115. catch (OperationCanceledException)
  116. {
  117. tran.Rollback();
  118. }
  119. catch (Exception e)
  120. {
  121. Logger.ErrorException("Failed to commit transaction.", e);
  122. tran.Rollback();
  123. }
  124. }
  125. }
  126. /// <summary>
  127. /// Gets the display preferences.
  128. /// </summary>
  129. /// <param name="displayPreferencesId">The display preferences id.</param>
  130. /// <returns>Task{DisplayPreferences}.</returns>
  131. /// <exception cref="System.ArgumentNullException">item</exception>
  132. public async Task<DisplayPreferences> GetDisplayPreferences(Guid displayPreferencesId)
  133. {
  134. if (displayPreferencesId == Guid.Empty)
  135. {
  136. throw new ArgumentNullException("displayPreferencesId");
  137. }
  138. var cmd = connection.CreateCommand();
  139. cmd.CommandText = "select data from displaypreferences where id = @id";
  140. var idParam = cmd.Parameters.Add("@id", DbType.Guid);
  141. idParam.Value = displayPreferencesId;
  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. }