SQLiteUserDataRepository.cs 4.7 KB

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