SharingRepository.cs 3.5 KB

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