2
0

SQLiteDisplayPreferencesRepository.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. using MediaBrowser.Controller;
  2. using MediaBrowser.Controller.Entities;
  3. using MediaBrowser.Controller.Persistence;
  4. using MediaBrowser.Model.Entities;
  5. using MediaBrowser.Model.Logging;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.ComponentModel.Composition;
  9. using System.Data;
  10. using System.IO;
  11. using System.Threading;
  12. using System.Threading.Tasks;
  13. namespace MediaBrowser.Server.Sqlite
  14. {
  15. /// <summary>
  16. /// Class SQLiteDisplayPreferencesRepository
  17. /// </summary>
  18. [Export(typeof(IDisplayPreferencesRepository))]
  19. class SQLiteDisplayPreferencesRepository : SqliteRepository, IDisplayPreferencesRepository
  20. {
  21. /// <summary>
  22. /// The repository name
  23. /// </summary>
  24. public const string RepositoryName = "SQLite";
  25. /// <summary>
  26. /// Gets the name of the repository
  27. /// </summary>
  28. /// <value>The name.</value>
  29. public string Name
  30. {
  31. get
  32. {
  33. return RepositoryName;
  34. }
  35. }
  36. /// <summary>
  37. /// Initializes a new instance of the <see cref="SQLiteUserDataRepository" /> class.
  38. /// </summary>
  39. /// <param name="logger">The logger.</param>
  40. [ImportingConstructor]
  41. protected SQLiteDisplayPreferencesRepository([Import("logger")] ILogger logger)
  42. : base(logger)
  43. {
  44. }
  45. /// <summary>
  46. /// Opens the connection to the database
  47. /// </summary>
  48. /// <returns>Task.</returns>
  49. public async Task Initialize()
  50. {
  51. var dbFile = Path.Combine(Kernel.Instance.ApplicationPaths.DataPath, "displaypreferences.db");
  52. await ConnectToDB(dbFile).ConfigureAwait(false);
  53. string[] queries = {
  54. "create table if not exists display_prefs (item_id GUID, user_id GUID, data BLOB)",
  55. "create unique index if not exists idx_display_prefs on display_prefs (item_id, user_id)",
  56. "create table if not exists schema_version (table_name primary key, version)",
  57. //pragmas
  58. "pragma temp_store = memory"
  59. };
  60. RunQueries(queries);
  61. }
  62. /// <summary>
  63. /// Save the display preferences associated with an item in the repo
  64. /// </summary>
  65. /// <param name="item">The item.</param>
  66. /// <param name="cancellationToken">The cancellation token.</param>
  67. /// <returns>Task.</returns>
  68. /// <exception cref="System.ArgumentNullException">item</exception>
  69. public Task SaveDisplayPrefs(Folder item, CancellationToken cancellationToken)
  70. {
  71. if (item == null)
  72. {
  73. throw new ArgumentNullException("item");
  74. }
  75. if (cancellationToken == null)
  76. {
  77. throw new ArgumentNullException("cancellationToken");
  78. }
  79. cancellationToken.ThrowIfCancellationRequested();
  80. return Task.Run(() =>
  81. {
  82. var cmd = connection.CreateCommand();
  83. cmd.CommandText = "delete from display_prefs where item_id = @guid";
  84. cmd.AddParam("@guid", item.DisplayPrefsId);
  85. QueueCommand(cmd);
  86. if (item.DisplayPrefs != null)
  87. {
  88. foreach (var data in item.DisplayPrefs)
  89. {
  90. cmd = connection.CreateCommand();
  91. cmd.CommandText = "insert into display_prefs (item_id, user_id, data) values (@1, @2, @3)";
  92. cmd.AddParam("@1", item.DisplayPrefsId);
  93. cmd.AddParam("@2", data.UserId);
  94. cmd.AddParam("@3", Kernel.Instance.ProtobufSerializer.SerializeToBytes(data));
  95. QueueCommand(cmd);
  96. }
  97. }
  98. });
  99. }
  100. /// <summary>
  101. /// Gets display preferences for an item
  102. /// </summary>
  103. /// <param name="item">The item.</param>
  104. /// <returns>IEnumerable{DisplayPreferences}.</returns>
  105. /// <exception cref="System.ArgumentNullException"></exception>
  106. public IEnumerable<DisplayPreferences> RetrieveDisplayPrefs(Folder item)
  107. {
  108. if (item == null)
  109. {
  110. throw new ArgumentNullException("item");
  111. }
  112. var cmd = connection.CreateCommand();
  113. cmd.CommandText = "select data from display_prefs where item_id = @guid";
  114. var guidParam = cmd.Parameters.Add("@guid", DbType.Guid);
  115. guidParam.Value = item.DisplayPrefsId;
  116. using (var reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess | CommandBehavior.SingleResult))
  117. {
  118. while (reader.Read())
  119. {
  120. using (var stream = GetStream(reader, 0))
  121. {
  122. var data = Kernel.Instance.ProtobufSerializer.DeserializeFromStream<DisplayPreferences>(stream);
  123. if (data != null)
  124. {
  125. yield return data;
  126. }
  127. }
  128. }
  129. }
  130. }
  131. }
  132. }