DicomCacheDB.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using SQLite;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq.Expressions;
  6. using Vinno.FIS.Sonopost.Common;
  7. using Vinno.FIS.Sonopost.Helpers;
  8. namespace Vinno.FIS.Sonopost.Features.Dicom
  9. {
  10. public class DicomCacheDB
  11. {
  12. private readonly object _dbLock = new object();
  13. private SQLiteConnection _connection;
  14. private static DicomCacheDB _instance;
  15. public static DicomCacheDB Instance => _instance ?? (_instance = new DicomCacheDB());
  16. private DicomCacheDB()
  17. {
  18. var folder = Path.Combine(SonopostConstants.DataFolder, SonopostConstants.RemedicalFolder);
  19. DirectoryHelper.CreateDirectory(folder);
  20. var cacheDbPath = Path.Combine(folder, "Sonopost.RemedicalCache.db");
  21. _connection = new SQLiteConnection(cacheDbPath, false);
  22. CreateTable<DicomUploadContext>();
  23. }
  24. /// <summary>
  25. /// 创建表
  26. /// </summary>
  27. /// <typeparam name="T"></typeparam>
  28. private void CreateTable<T>()
  29. {
  30. lock (_dbLock)
  31. {
  32. _connection.CreateTable<T>();
  33. }
  34. }
  35. /// <summary>
  36. /// 添加数据
  37. /// </summary>
  38. /// <typeparam name="T"></typeparam>
  39. /// <param name="item"></param>
  40. public bool Create<T>(T item) where T : class
  41. {
  42. lock (_dbLock)
  43. {
  44. var ret = _connection.InsertOrReplace(item, typeof(T));
  45. return ret > 0;
  46. }
  47. }
  48. /// <summary>
  49. /// 删除数据
  50. /// </summary>
  51. /// <typeparam name="T"></typeparam>
  52. /// <param name="expr"></param>
  53. public bool Delete<T>(Expression<Func<T, bool>> expr) where T : class, new()
  54. {
  55. lock (_dbLock)
  56. {
  57. var existingItems = _connection.Table<T>().Where(expr);
  58. var ret = 0;
  59. foreach (var item in existingItems)
  60. {
  61. var temp = _connection.Delete(item);
  62. ret += temp;
  63. }
  64. return ret > 0;
  65. }
  66. }
  67. /// <summary>
  68. /// 更新数据
  69. /// </summary>
  70. /// <typeparam name="T"></typeparam>
  71. /// <param name="item"></param>
  72. public bool Update<T>(T item) where T : class
  73. {
  74. lock (_dbLock)
  75. {
  76. var ret = _connection.Update(item);
  77. return ret > 0;
  78. }
  79. }
  80. /// <summary>
  81. ///查询所有数据
  82. /// </summary>
  83. /// <typeparam name="T">The type of db model</typeparam>
  84. /// <returns>All items in one table.</returns>
  85. public IList<T> GetAll<T>() where T : class, new()
  86. {
  87. lock (_dbLock)
  88. {
  89. return _connection.Table<T>().ToList();
  90. }
  91. }
  92. /// <summary>
  93. /// 查询指定数据
  94. /// </summary>
  95. /// <typeparam name="T"></typeparam>
  96. /// <param name="expr"></param>
  97. /// <returns></returns>
  98. public T Get<T>(Expression<Func<T, bool>> expr) where T : class, new()
  99. {
  100. lock (_dbLock)
  101. {
  102. return _connection.Table<T>().Where(expr).FirstOrDefault();
  103. }
  104. }
  105. /// <summary>
  106. /// 关闭连接
  107. /// </summary>
  108. public void Close()
  109. {
  110. lock (_dbLock)
  111. {
  112. if (_connection != null)
  113. {
  114. _connection.Close();
  115. _connection.Dispose();
  116. _connection = null;
  117. }
  118. }
  119. }
  120. }
  121. }