增加数据库根据Tracking进行更新的方法。

This commit is contained in:
falcon 2023-04-19 16:34:47 +08:00
parent 92eb80ae66
commit deea9c1457

View File

@ -10,20 +10,20 @@ namespace Falcon.SugarApi.DatabaseDefinitions
/// <summary> /// <summary>
/// Sugar数据库上下文 /// Sugar数据库上下文
/// </summary> /// </summary>
public class SugarDbContext : SqlSugarClient public class SugarDbContext:SqlSugarClient
{ {
/// <summary> /// <summary>
/// 通过链接配置ConnectionConfig构造SqlSugarClient数据上下文 /// 通过链接配置ConnectionConfig构造SqlSugarClient数据上下文
/// </summary> /// </summary>
/// <param name="config"></param> /// <param name="config"></param>
/// <param name="logger">日志记录器</param> /// <param name="logger">日志记录器</param>
public SugarDbContext(SugarConnectionConfig config, ILogger<SugarDbContext> logger) : base(config) { public SugarDbContext(SugarConnectionConfig config,ILogger<SugarDbContext> logger) : base(config) {
this.Logger = logger; this.Logger = logger;
if (config.Log) { if(config.Log) {
this.Aop.OnLogExecuting = (string sql, SugarParameter[] paras) => { this.Aop.OnLogExecuting = (string sql,SugarParameter[] paras) => {
StringBuilder sb = new(); StringBuilder sb = new();
sb.AppendLine(sql); sb.AppendLine(sql);
sb.AppendLine(this.Utilities.SerializeObject(paras.ToDictionary(it => it.ParameterName, it => it.Value))); sb.AppendLine(this.Utilities.SerializeObject(paras.ToDictionary(it => it.ParameterName,it => it.Value)));
this.Logger.LogInformation(sb.ToString()); this.Logger.LogInformation(sb.ToString());
}; };
} }
@ -40,17 +40,17 @@ namespace Falcon.SugarApi.DatabaseDefinitions
/// <typeparam name="T">数据实体类型</typeparam> /// <typeparam name="T">数据实体类型</typeparam>
/// <param name="data">要插入的数据列表</param> /// <param name="data">要插入的数据列表</param>
/// <param name="createBy">创建人</param> /// <param name="createBy">创建人</param>
public void Insert<T>(List<T> data, string createBy) where T : class, new() { public void Insert<T>(List<T> data,string createBy) where T : class, new() {
foreach (var i in data) { foreach(var i in data) {
if (i is ICreateNew cn) { if(i is ICreateNew cn) {
cn.CreateNew(createBy); cn.CreateNew(createBy);
} }
} }
try { try {
this.Insertable(data).ExecuteCommand(); this.Insertable(data).ExecuteCommand();
} }
catch (Exception ex) { catch(Exception ex) {
throw new Exception("SugarDbContext.Insert数据发生异常", ex); throw new Exception("SugarDbContext.Insert数据发生异常",ex);
} }
} }
@ -60,8 +60,8 @@ namespace Falcon.SugarApi.DatabaseDefinitions
/// <typeparam name="T">数据模型</typeparam> /// <typeparam name="T">数据模型</typeparam>
/// <param name="data">要插入的数据</param> /// <param name="data">要插入的数据</param>
/// <param name="createBy">创建人</param> /// <param name="createBy">创建人</param>
public void Insert<T>(T data, string createBy) where T : class, new() { public void Insert<T>(T data,string createBy) where T : class, new() {
this.Insert(new List<T> { data }, createBy); this.Insert(new List<T> { data },createBy);
} }
#endregion #endregion
@ -73,9 +73,9 @@ namespace Falcon.SugarApi.DatabaseDefinitions
/// <typeparam name="T">数据模型</typeparam> /// <typeparam name="T">数据模型</typeparam>
/// <param name="data">要更新的数据</param> /// <param name="data">要更新的数据</param>
/// <param name="updateBy">更新人</param> /// <param name="updateBy">更新人</param>
public List<T> Update<T>(List<T> data, string updateBy) where T : class, new() { public List<T> Update<T>(List<T> data,string updateBy) where T : class, new() {
foreach (var i in data) { foreach(var i in data) {
if (i is IModify cn) { if(i is IModify cn) {
cn.Modify(updateBy); cn.Modify(updateBy);
} }
} }
@ -89,11 +89,37 @@ namespace Falcon.SugarApi.DatabaseDefinitions
/// <typeparam name="T">数据模型</typeparam> /// <typeparam name="T">数据模型</typeparam>
/// <param name="data">要更新的数据</param> /// <param name="data">要更新的数据</param>
/// <param name="updateBy">更新人</param> /// <param name="updateBy">更新人</param>
public T Update<T>(T data, string updateBy) where T : class, new() { public T Update<T>(T data,string updateBy) where T : class, new() {
this.Update(new List<T> { data }, updateBy); this.Update(new List<T> { data },updateBy);
return data; return data;
} }
/// <summary>
/// 利用Tracking跟踪并更新数据
/// </summary>
/// <typeparam name="T">要跟踪更新的实体类型</typeparam>
/// <param name="data">要跟踪更新的实体数据</param>
/// <param name="changeValfun">修改实体数据</param>
public void Update<T>(T data,Action<T> changeValfun) where T : class, new() {
if(data == null) return;
this.Tracking(data);
changeValfun?.Invoke(data);
this.Updateable(data).ExecuteCommand();
}
/// <summary>
/// 利用Tracking跟踪并更新数据
/// </summary>
/// <typeparam name="T">要跟踪更新的实体类型</typeparam>
/// <param name="data">要跟踪更新的实体数据</param>
/// <param name="changeValfun">修改实体数据</param>
public async void UpdateAsync<T>(T data,Action<T> changeValfun) where T : class, new() {
if(data == null) return;
this.Tracking(data);
changeValfun?.Invoke(data);
await this.Updateable(data).ExecuteCommandAsync();
}
#endregion #endregion
#region #region
@ -103,7 +129,7 @@ namespace Falcon.SugarApi.DatabaseDefinitions
/// <typeparam name="T">数据实体类型必须继承自SugarTableBase</typeparam> /// <typeparam name="T">数据实体类型必须继承自SugarTableBase</typeparam>
/// <param name="id">要删除的数据id</param> /// <param name="id">要删除的数据id</param>
/// <param name="deleteBy">删除人</param> /// <param name="deleteBy">删除人</param>
public void Delete<T>(Guid id, string deleteBy) where T : SugarTableBase, new() { public void Delete<T>(Guid id,string deleteBy) where T : SugarTableBase, new() {
var data = new T(); var data = new T();
data.Delete(deleteBy); data.Delete(deleteBy);
this.Updateable<T>() this.Updateable<T>()
@ -121,9 +147,9 @@ namespace Falcon.SugarApi.DatabaseDefinitions
/// <param name="data">实体对象列表</param> /// <param name="data">实体对象列表</param>
/// <param name="deleteBy">删除人</param> /// <param name="deleteBy">删除人</param>
/// <returns>删除后的实体列表</returns> /// <returns>删除后的实体列表</returns>
public List<T> Delete<T>(List<T> data, string deleteBy) where T : class, new() { public List<T> Delete<T>(List<T> data,string deleteBy) where T : class, new() {
foreach (var item in data) { foreach(var item in data) {
if (item is IDelete d) { if(item is IDelete d) {
d.Delete(deleteBy); d.Delete(deleteBy);
} }
} }
@ -137,8 +163,8 @@ namespace Falcon.SugarApi.DatabaseDefinitions
/// <param name="data">实体对象</param> /// <param name="data">实体对象</param>
/// <param name="deleteBy">删除人</param> /// <param name="deleteBy">删除人</param>
/// <returns>删除后的对象</returns> /// <returns>删除后的对象</returns>
public T Delete<T>(T data, string deleteBy) where T : class, new() { public T Delete<T>(T data,string deleteBy) where T : class, new() {
this.Delete(new List<T> { data }, deleteBy); this.Delete(new List<T> { data },deleteBy);
return data; return data;
} }
@ -155,11 +181,11 @@ namespace Falcon.SugarApi.DatabaseDefinitions
/// </summary> /// </summary>
/// <param name="types">要升级的表模型</param> /// <param name="types">要升级的表模型</param>
public void UpdateTableStructure(params Type[] types) { public void UpdateTableStructure(params Type[] types) {
for (int i = 0; i < types.Length; i++) { for(int i = 0;i < types.Length;i++) {
var type = types[i]; var type = types[i];
if (!InitdTable.Any(m => m == type.FullName)) { if(!InitdTable.Any(m => m == type.FullName)) {
this.CodeFirst.BackupTable().SetStringDefaultLength(200).InitTables(type); this.CodeFirst.BackupTable().SetStringDefaultLength(200).InitTables(type);
if (type.FullName != null) { if(type.FullName != null) {
InitdTable.Add(type.FullName); InitdTable.Add(type.FullName);
} }
} }
@ -181,10 +207,10 @@ namespace Falcon.SugarApi.DatabaseDefinitions
/// <typeparam name="TTable">初始化表的模型</typeparam> /// <typeparam name="TTable">初始化表的模型</typeparam>
/// <param name="tableName">表名</param> /// <param name="tableName">表名</param>
/// <param name="callbackWhenInitTable">表首次创建后执行的回调</param> /// <param name="callbackWhenInitTable">表首次创建后执行的回调</param>
public void UpdateTable<TTable>(string tableName, Action<SugarDbContext> callbackWhenInitTable) { public void UpdateTable<TTable>(string tableName,Action<SugarDbContext> callbackWhenInitTable) {
var hasTable = this.DbMaintenance.IsAnyTable(tableName, false); var hasTable = this.DbMaintenance.IsAnyTable(tableName,false);
this.CodeFirst.InitTables<TTable>(); this.CodeFirst.InitTables<TTable>();
if (!hasTable) { if(!hasTable) {
callbackWhenInitTable(this); callbackWhenInitTable(this);
} }
} }
@ -206,7 +232,7 @@ namespace Falcon.SugarApi.DatabaseDefinitions
/// <param name="name">参数名称</param> /// <param name="name">参数名称</param>
/// <returns></returns> /// <returns></returns>
public SugarParameter OracleRefCursor(string name = ":v_data") => public SugarParameter OracleRefCursor(string name = ":v_data") =>
new(name, null, true) { IsRefCursor = true, DbType = System.Data.DbType.String }; new(name,null,true) { IsRefCursor = true,DbType = System.Data.DbType.String };
/// <summary> /// <summary>
/// 从对象获取存储过程参数 /// 从对象获取存储过程参数
@ -215,18 +241,18 @@ namespace Falcon.SugarApi.DatabaseDefinitions
/// <param name="data">参数对象</param> /// <param name="data">参数对象</param>
/// <param name="otherParams">增加其他的参数</param> /// <param name="otherParams">增加其他的参数</param>
/// <returns>参数</returns> /// <returns>参数</returns>
public SugarParameter[] GetParameters<T>(T data, params SugarParameter[] otherParams) { public SugarParameter[] GetParameters<T>(T data,params SugarParameter[] otherParams) {
var result = new List<SugarParameter>(); var result = new List<SugarParameter>();
if (data != null) { if(data != null) {
var dict = new Dictionary<string, object>(); var dict = new Dictionary<string,object>();
foreach (var p in data.GetType().GetProperties()) { foreach(var p in data.GetType().GetProperties()) {
if (p.CanRead) { if(p.CanRead) {
dict.Add(p.Name, p.GetValue(data) ?? ""); dict.Add(p.Name,p.GetValue(data) ?? "");
} }
} }
result.AddRange(this.Ado.GetParameters(dict).ToArray()); result.AddRange(this.Ado.GetParameters(dict).ToArray());
} }
if (otherParams != null) { if(otherParams != null) {
result.AddRange(otherParams); result.AddRange(otherParams);
} }
return result.ToArray(); return result.ToArray();