SQLiteDisplayPreferencesRepository.cs 4.8 KB

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