SQLiteDisplayPreferencesRepository.cs 6.4 KB

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