241 lines
6.7 KiB
C#
241 lines
6.7 KiB
C#
using System.Reflection;
|
|
using MasstransferInfrastructure.Database.DTO;
|
|
using Masuit.Tools;
|
|
using Masuit.Tools.Systems;
|
|
using SQLite;
|
|
|
|
namespace MasstransferInfrastructure.Database.Sqlite;
|
|
|
|
/// <summary>
|
|
/// SQLite 数据库ORM工具类
|
|
/// </summary>
|
|
public class SqliteHelper
|
|
{
|
|
// 默认的数据库密码
|
|
private const string Password = "88888888";
|
|
private readonly SQLiteConnection _db;
|
|
|
|
private static SqliteHelper? _instance;
|
|
|
|
private static readonly object Locker = new();
|
|
|
|
private SqliteHelper()
|
|
{
|
|
var profile = Environment.GetEnvironmentVariable("USERPROFILE");
|
|
var path = Path.Combine(profile, "masstransfer", "mass-transfer.db");
|
|
|
|
if (!Directory.Exists(Path.GetDirectoryName(path)!))
|
|
{
|
|
Directory.CreateDirectory(Path.GetDirectoryName(path)!);
|
|
|
|
// 将文件复模板数据库复制到目标目录下
|
|
var template = Path.Combine(Environment.CurrentDirectory, "mass-transfer.db");
|
|
File.Copy(template, path);
|
|
}
|
|
|
|
|
|
_db = new SQLiteConnection(path);
|
|
_db.Execute($"PRAGMA key = '{Password}'");
|
|
}
|
|
|
|
public static SqliteHelper GetInstance()
|
|
{
|
|
lock (Locker)
|
|
{
|
|
// 如果类的实例不存在则创建,否则直接返回
|
|
_instance ??= new SqliteHelper();
|
|
}
|
|
|
|
return _instance;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 插入数据
|
|
/// </summary>
|
|
/// <param name="item">待插入的数据</param>
|
|
/// <typeparam name="T">数据类型</typeparam>
|
|
public int Insert<T>(T item)
|
|
{
|
|
var id = item?.GetType().GetProperty("Id");
|
|
if (id != null && id.CanWrite) id.SetValue(item, SnowFlakeNew.LongId.ToString());
|
|
|
|
var createTime = item?.GetType().GetProperty("CreateTime");
|
|
if (createTime != null && createTime.CanWrite) createTime.SetValue(item, DateTime.Now);
|
|
|
|
|
|
return _db.Insert(item);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 更新数据
|
|
/// </summary>
|
|
/// <param name="item">待更新的数据</param>
|
|
/// <typeparam name="T">数据类型</typeparam>
|
|
public int Update<T>(T item)
|
|
{
|
|
var updateTime = item?.GetType().GetProperty("UpdateTime");
|
|
if (updateTime != null && updateTime.CanWrite) updateTime.SetValue(item, DateTime.Now);
|
|
|
|
return _db.Update(item);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 删除数据
|
|
/// </summary>
|
|
/// <param name="id">待删除数据的ID</param>
|
|
/// <typeparam name="T">数据类型</typeparam>
|
|
public int DeleteById<T>(object id) where T : new()
|
|
{
|
|
var type = typeof(T);
|
|
var tableName = type.GetCustomAttribute<TableAttribute>()!.Name ?? type.Name;
|
|
|
|
return _db.Execute($"delete from {tableName} where id = {id}");
|
|
}
|
|
|
|
/// <summary>
|
|
/// 删除数据
|
|
/// </summary>
|
|
/// <param name="item">待删除数据的对象</param>
|
|
/// <typeparam name="T">数据类型</typeparam>
|
|
public int Delete<T>(T item)
|
|
{
|
|
return _db.Delete<T>(item);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 根据ID获取数据
|
|
/// </summary>
|
|
/// <param name="id">数据ID</param>
|
|
/// <typeparam name="T">数据类型</typeparam>
|
|
public T GetById<T>(object id) where T : new()
|
|
{
|
|
return _db.Get<T>(id);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 查询数据
|
|
/// </summary>
|
|
/// <param name="sql">查询语句</param>
|
|
/// <param name="args">查询参数</param>
|
|
/// <typeparam name="T">数据类型</typeparam>
|
|
public List<T> Query<T>(string sql, params object[] args) where T : new()
|
|
{
|
|
return _db.Query<T>(sql, args);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 查询所有数据
|
|
/// </summary>
|
|
/// <typeparam name="T">数据类型</typeparam>
|
|
public List<T> ListAll<T>() where T : new()
|
|
{
|
|
return _db.Table<T>().ToList();
|
|
}
|
|
|
|
public T SaveOrUpdate<T>(T item) where T : new()
|
|
{
|
|
var id = item?.GetType().GetProperty("Id");
|
|
if (id != null && id.GetValue(item).IsNullOrEmpty())
|
|
// 插入
|
|
Insert(item);
|
|
else
|
|
// 否则是更新操作
|
|
Update(item);
|
|
|
|
return item;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 执行SQL语句
|
|
/// </summary>
|
|
/// <param name="sql">SQL语句</param>
|
|
public int Execute(string sql)
|
|
{
|
|
return _db.Execute(sql);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取当前的表信息
|
|
/// </summary>
|
|
/// <param name="tableName"></param>
|
|
/// <returns></returns>
|
|
public List<SQLiteConnection.ColumnInfo> GetTableInfo(string tableName)
|
|
{
|
|
return _db.GetTableInfo(tableName);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 创建表
|
|
/// </summary>
|
|
/// <param name="entity"></param>
|
|
public void CreateTable(Type entity)
|
|
{
|
|
_db.CreateTable(entity);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 分页查询结果
|
|
/// </summary>
|
|
/// <param name="page"></param>
|
|
/// <param name="pageSize"></param>
|
|
/// <param name="sql"></param>
|
|
/// <param name="args"></param>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <returns></returns>
|
|
public Page<T> Page<T>(int page, int pageSize, string sql, params object[] args) where T : new()
|
|
{
|
|
var countSql = "select count(*) from ( " + sql + " ) as c";
|
|
var total = _db.ExecuteScalar<int>(countSql, args);
|
|
var data = Query<T>(sql + " limit " + (page - 1) * pageSize + "," + pageSize, args);
|
|
return new Page<T>
|
|
{
|
|
CurrentPage = page,
|
|
PageSize = pageSize,
|
|
Total = total,
|
|
Pages = total / pageSize + (total % pageSize == 0 ? 0 : 1),
|
|
Data = data
|
|
};
|
|
}
|
|
|
|
/// <summary>
|
|
/// 批量插入
|
|
/// </summary>
|
|
/// <param name="objects"></param>
|
|
/// <param name="runInTransaction"></param>
|
|
/// <typeparam name="T"></typeparam>
|
|
public void BatchInsert<T>(IEnumerable<T> objects, bool runInTransaction = false) where T : new()
|
|
{
|
|
if (runInTransaction)
|
|
{
|
|
_db.RunInTransaction((Action)(() =>
|
|
{
|
|
foreach (object obj in objects)
|
|
Insert(obj);
|
|
}));
|
|
}
|
|
else
|
|
{
|
|
foreach (object obj in objects)
|
|
Insert(obj);
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 更新指定字段
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <param name="field"></param>
|
|
/// <param name="value"></param>
|
|
/// <typeparam name="T"></typeparam>
|
|
public void UpdateFieldById<T>(object? id, string field, object? value) where T : new()
|
|
{
|
|
var type = typeof(T);
|
|
var tableName = type.GetCustomAttribute<TableAttribute>()!.Name ?? type.Name;
|
|
|
|
var sql = $"update {tableName} set {field} = ? where id = ?";
|
|
|
|
_db.Execute(sql, value, id);
|
|
}
|
|
} |