SharingRepository.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using Emby.Server.Implementations.Data;
  7. using MediaBrowser.Common.Configuration;
  8. using MediaBrowser.Model.Logging;
  9. using MediaBrowser.Model.Social;
  10. using SQLitePCL.pretty;
  11. namespace Emby.Server.Implementations.Social
  12. {
  13. public class SharingRepository : BaseSqliteRepository, ISharingRepository
  14. {
  15. public SharingRepository(ILogger logger, IApplicationPaths appPaths)
  16. : base(logger)
  17. {
  18. DbFilePath = Path.Combine(appPaths.DataPath, "shares.db");
  19. }
  20. /// <summary>
  21. /// Opens the connection to the database
  22. /// </summary>
  23. /// <returns>Task.</returns>
  24. public void Initialize()
  25. {
  26. using (var connection = CreateConnection())
  27. {
  28. connection.ExecuteAll(string.Join(";", new[]
  29. {
  30. "PRAGMA page_size=4096",
  31. "pragma default_temp_store = memory",
  32. "pragma temp_store = memory"
  33. }));
  34. string[] queries = {
  35. "create table if not exists Shares (Id GUID, ItemId TEXT, UserId TEXT, ExpirationDate DateTime, PRIMARY KEY (Id))",
  36. "create index if not exists idx_Shares on Shares(Id)",
  37. "pragma shrink_memory"
  38. };
  39. connection.RunQueries(queries);
  40. }
  41. }
  42. public async Task CreateShare(SocialShareInfo info)
  43. {
  44. if (info == null)
  45. {
  46. throw new ArgumentNullException("info");
  47. }
  48. if (string.IsNullOrWhiteSpace(info.Id))
  49. {
  50. throw new ArgumentNullException("info.Id");
  51. }
  52. using (var connection = CreateConnection())
  53. {
  54. using (WriteLock.Write())
  55. {
  56. connection.RunInTransaction(db =>
  57. {
  58. var commandText = "replace into Shares (Id, ItemId, UserId, ExpirationDate) values (?, ?, ?, ?)";
  59. db.Execute(commandText,
  60. info.Id.ToGuidParamValue(),
  61. info.ItemId,
  62. info.UserId,
  63. info.ExpirationDate.ToDateTimeParamValue());
  64. }, TransactionMode);
  65. }
  66. }
  67. }
  68. public SocialShareInfo GetShareInfo(string id)
  69. {
  70. if (string.IsNullOrWhiteSpace(id))
  71. {
  72. throw new ArgumentNullException("id");
  73. }
  74. using (var connection = CreateConnection(true))
  75. {
  76. using (WriteLock.Read())
  77. {
  78. var commandText = "select Id, ItemId, UserId, ExpirationDate from Shares where id = ?";
  79. var paramList = new List<object>();
  80. paramList.Add(id.ToGuidParamValue());
  81. foreach (var row in connection.Query(commandText, paramList.ToArray()))
  82. {
  83. return GetSocialShareInfo(row);
  84. }
  85. }
  86. }
  87. return null;
  88. }
  89. private SocialShareInfo GetSocialShareInfo(IReadOnlyList<IResultSetValue> reader)
  90. {
  91. var info = new SocialShareInfo();
  92. info.Id = reader[0].ReadGuid().ToString("N");
  93. info.ItemId = reader[1].ToString();
  94. info.UserId = reader[2].ToString();
  95. info.ExpirationDate = reader[3].ReadDateTime();
  96. return info;
  97. }
  98. public async Task DeleteShare(string id)
  99. {
  100. }
  101. }
  102. }