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(); } /// /// 创建表 /// /// private void CreateTable() { lock (_dbLock) { _connection.CreateTable(); } } /// /// 添加数据 /// /// /// public bool Create(T item) where T : class { lock (_dbLock) { var ret = _connection.InsertOrReplace(item, typeof(T)); return ret > 0; } } /// /// 删除数据 /// /// /// public bool Delete(Expression> expr) where T : class, new() { lock (_dbLock) { var existingItems = _connection.Table().Where(expr); var ret = 0; foreach (var item in existingItems) { var temp = _connection.Delete(item); ret += temp; } return ret > 0; } } /// /// 更新数据 /// /// /// public bool Update(T item) where T : class { lock (_dbLock) { var ret = _connection.Update(item); return ret > 0; } } /// ///查询所有数据 /// /// The type of db model /// All items in one table. public IList GetAll() where T : class, new() { lock (_dbLock) { return _connection.Table().ToList(); } } /// /// 查询指定数据 /// /// /// /// public T Get(Expression> expr) where T : class, new() { lock (_dbLock) { return _connection.Table().Where(expr).FirstOrDefault(); } } /// /// 关闭连接 /// public void Close() { lock (_dbLock) { if (_connection != null) { _connection.Close(); _connection.Dispose(); _connection = null; } } } } }