123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- using SQLite;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq.Expressions;
- using Vinno.FIS.Sonopost.Common;
- using Vinno.FIS.Sonopost.Helpers;
- namespace Vinno.FIS.Sonopost.Features.Dicom
- {
- public class DicomCacheDB
- {
- private readonly object _dbLock = new object();
- private SQLiteConnection _connection;
- private static DicomCacheDB _instance;
- public static DicomCacheDB Instance => _instance ?? (_instance = new DicomCacheDB());
- private DicomCacheDB()
- {
- var folder = Path.Combine(SonopostConstants.DataFolder, SonopostConstants.RemedicalFolder);
- DirectoryHelper.CreateDirectory(folder);
- var cacheDbPath = Path.Combine(folder, "Sonopost.RemedicalCache.db");
- _connection = new SQLiteConnection(cacheDbPath, false);
- CreateTable<DicomUploadContext>();
- }
- /// <summary>
- /// 创建表
- /// </summary>
- /// <typeparam name="T"></typeparam>
- private void CreateTable<T>()
- {
- lock (_dbLock)
- {
- _connection.CreateTable<T>();
- }
- }
- /// <summary>
- /// 添加数据
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="item"></param>
- public bool Create<T>(T item) where T : class
- {
- lock (_dbLock)
- {
- var ret = _connection.InsertOrReplace(item, typeof(T));
- return ret > 0;
- }
- }
- /// <summary>
- /// 删除数据
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="expr"></param>
- public bool Delete<T>(Expression<Func<T, bool>> expr) where T : class, new()
- {
- lock (_dbLock)
- {
- var existingItems = _connection.Table<T>().Where(expr);
- var ret = 0;
- foreach (var item in existingItems)
- {
- var temp = _connection.Delete(item);
- ret += temp;
- }
- return ret > 0;
- }
- }
- /// <summary>
- /// 更新数据
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="item"></param>
- public bool Update<T>(T item) where T : class
- {
- lock (_dbLock)
- {
- var ret = _connection.Update(item);
- return ret > 0;
- }
- }
- /// <summary>
- ///查询所有数据
- /// </summary>
- /// <typeparam name="T">The type of db model</typeparam>
- /// <returns>All items in one table.</returns>
- public IList<T> GetAll<T>() where T : class, new()
- {
- lock (_dbLock)
- {
- return _connection.Table<T>().ToList();
- }
- }
- /// <summary>
- /// 查询指定数据
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="expr"></param>
- /// <returns></returns>
- public T Get<T>(Expression<Func<T, bool>> expr) where T : class, new()
- {
- lock (_dbLock)
- {
- return _connection.Table<T>().Where(expr).FirstOrDefault();
- }
- }
- /// <summary>
- /// 关闭连接
- /// </summary>
- public void Close()
- {
- lock (_dbLock)
- {
- if (_connection != null)
- {
- _connection.Close();
- _connection.Dispose();
- _connection = null;
- }
- }
- }
- }
- }
|