SQLiteDisplayPreferencesRepository.cs 4.9 KB

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