SQLiteUserDataRepository.cs 4.7 KB

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