mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-08-24 07:22:57 +08:00
Update .net core project
This commit is contained in:
parent
14a9051db6
commit
735619c5ec
@ -1342,6 +1342,27 @@ namespace SqlSugar
|
|||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Other
|
#region Other
|
||||||
|
public void Tracking<T>(T data) where T : class, new()
|
||||||
|
{
|
||||||
|
if (data != null)
|
||||||
|
{
|
||||||
|
UtilMethods.IsNullReturnNew(this.TempItems);
|
||||||
|
var key = "Tracking_" + data.GetHashCode() + "";
|
||||||
|
if (!this.TempItems.ContainsKey(key))
|
||||||
|
{
|
||||||
|
var newT = new T();
|
||||||
|
FastCopy.Copy(data, newT);
|
||||||
|
this.TempItems.Add(key, newT);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public void Tracking<T>(List<T> datas) where T : class, new()
|
||||||
|
{
|
||||||
|
foreach (var data in datas)
|
||||||
|
{
|
||||||
|
this.Tracking(data);
|
||||||
|
}
|
||||||
|
}
|
||||||
public SqlSugarClient CopyNew()
|
public SqlSugarClient CopyNew()
|
||||||
{
|
{
|
||||||
return new SqlSugarClient(UtilMethods.CopyConfig(this.Ado.Context.CurrentConnectionConfig));
|
return new SqlSugarClient(UtilMethods.CopyConfig(this.Ado.Context.CurrentConnectionConfig));
|
||||||
|
@ -710,6 +710,14 @@ namespace SqlSugar
|
|||||||
{
|
{
|
||||||
return new SqlSugarClient(UtilMethods.CopyConfig(this.Ado.Context.CurrentConnectionConfig));
|
return new SqlSugarClient(UtilMethods.CopyConfig(this.Ado.Context.CurrentConnectionConfig));
|
||||||
}
|
}
|
||||||
|
public void Tracking<T>(T data) where T : class, new()
|
||||||
|
{
|
||||||
|
ScopedContext.Tracking(data);
|
||||||
|
}
|
||||||
|
public void Tracking<T>(List<T> datas) where T : class, new()
|
||||||
|
{
|
||||||
|
ScopedContext.Tracking(datas);
|
||||||
|
}
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -85,6 +85,7 @@ namespace SqlSugar
|
|||||||
{
|
{
|
||||||
DataAop(item);
|
DataAop(item);
|
||||||
SetUpdateItemByEntity(i, item, updateItem);
|
SetUpdateItemByEntity(i, item, updateItem);
|
||||||
|
Tracking(item);
|
||||||
}
|
}
|
||||||
++i;
|
++i;
|
||||||
}
|
}
|
||||||
@ -97,6 +98,33 @@ namespace SqlSugar
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void Tracking(T item)
|
||||||
|
{
|
||||||
|
if (IsTrakingData())
|
||||||
|
{
|
||||||
|
var trackingData = this.Context.TempItems.FirstOrDefault(it => it.Key.StartsWith("Tracking_" + item.GetHashCode()));
|
||||||
|
var diffColumns = FastCopy.GetDiff(item, (T)trackingData.Value);
|
||||||
|
if (diffColumns.Count > 0)
|
||||||
|
{
|
||||||
|
var pks =EntityInfo.Columns
|
||||||
|
.Where(it => it.IsPrimarykey).Select(it => it.PropertyName).ToList();
|
||||||
|
diffColumns=diffColumns.Where(it => !pks.Contains(it)).ToList();
|
||||||
|
if (diffColumns.Count > 0)
|
||||||
|
{
|
||||||
|
this.UpdateColumns(diffColumns.ToArray());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private bool IsTrakingData()
|
||||||
|
{
|
||||||
|
return this.UpdateParameterIsNull == false
|
||||||
|
&& this.Context.TempItems != null
|
||||||
|
&& this.Context.TempItems.Any(it => it.Key.StartsWith("Tracking_"))
|
||||||
|
&& this.UpdateObjs.Length == 1;
|
||||||
|
}
|
||||||
|
|
||||||
private void DataAop(T item)
|
private void DataAop(T item)
|
||||||
{
|
{
|
||||||
var dataEvent = this.Context.CurrentConnectionConfig.AopEvents?.DataExecuting;
|
var dataEvent = this.Context.CurrentConnectionConfig.AopEvents?.DataExecuting;
|
||||||
|
@ -44,6 +44,8 @@ namespace SqlSugar
|
|||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Other methods
|
#region Other methods
|
||||||
|
void Tracking<T>(T data) where T : class, new();
|
||||||
|
void Tracking<T>(List<T> data) where T : class, new();
|
||||||
SqlSugarClient CopyNew();
|
SqlSugarClient CopyNew();
|
||||||
T CreateContext<T>(bool isTran=true) where T : SugarUnitOfWork, new();
|
T CreateContext<T>(bool isTran=true) where T : SugarUnitOfWork, new();
|
||||||
SugarUnitOfWork CreateContext(bool isTran = true);
|
SugarUnitOfWork CreateContext(bool isTran = true);
|
||||||
|
@ -179,7 +179,7 @@ namespace SqlSugar
|
|||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
return "EXECUTE sp_addextendedproperty N'MS_Description', '{2}', N'user', N'dbo', N'table', N'{1}', N'column', N'{0}'"; ;
|
return "EXECUTE sp_addextendedproperty N'MS_Description', N'{2}', N'user', N'dbo', N'table', N'{1}', N'column', N'{0}'"; ;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -440,7 +440,6 @@ namespace SqlSugar
|
|||||||
}
|
}
|
||||||
var oldDatabaseName = this.Context.Ado.Connection.Database;
|
var oldDatabaseName = this.Context.Ado.Connection.Database;
|
||||||
var connection = this.Context.CurrentConnectionConfig.ConnectionString;
|
var connection = this.Context.CurrentConnectionConfig.ConnectionString;
|
||||||
Check.ExceptionEasy(String.IsNullOrEmpty(connection), "ConnectionString is not null", "连接字符串ConnectionString不能为Null");
|
|
||||||
connection = connection.Replace(oldDatabaseName, "master");
|
connection = connection.Replace(oldDatabaseName, "master");
|
||||||
var newDb = new SqlSugarClient(new ConnectionConfig()
|
var newDb = new SqlSugarClient(new ConnectionConfig()
|
||||||
{
|
{
|
||||||
|
@ -996,6 +996,14 @@ namespace SqlSugar
|
|||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Other method
|
#region Other method
|
||||||
|
public void Tracking<T>(T data) where T : class, new()
|
||||||
|
{
|
||||||
|
this.Context.Tracking(data);
|
||||||
|
}
|
||||||
|
public void Tracking<T>(List<T> datas) where T : class, new()
|
||||||
|
{
|
||||||
|
this.Context.Tracking(datas);
|
||||||
|
}
|
||||||
public SqlSugarClient CopyNew()
|
public SqlSugarClient CopyNew()
|
||||||
{
|
{
|
||||||
return new SqlSugarClient(UtilMethods.CopyConfig(this.Ado.Context.CurrentConnectionConfig));
|
return new SqlSugarClient(UtilMethods.CopyConfig(this.Ado.Context.CurrentConnectionConfig));
|
||||||
|
@ -766,5 +766,13 @@ namespace SqlSugar
|
|||||||
{
|
{
|
||||||
return new SqlSugarClient(UtilMethods.CopyConfig(this.Ado.Context.CurrentConnectionConfig));
|
return new SqlSugarClient(UtilMethods.CopyConfig(this.Ado.Context.CurrentConnectionConfig));
|
||||||
}
|
}
|
||||||
|
public void Tracking<T>(T data) where T : class, new()
|
||||||
|
{
|
||||||
|
ScopedContext.Tracking(data);
|
||||||
|
}
|
||||||
|
public void Tracking<T>(List<T> datas) where T : class, new()
|
||||||
|
{
|
||||||
|
ScopedContext.Tracking(datas);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
130
Src/Asp.NetCore2/SqlSugar/Utilities/FastCopy.cs
Normal file
130
Src/Asp.NetCore2/SqlSugar/Utilities/FastCopy.cs
Normal file
@ -0,0 +1,130 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Concurrent;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using System.Reflection;
|
||||||
|
using static System.Linq.Expressions.Expression;
|
||||||
|
|
||||||
|
namespace SqlSugar
|
||||||
|
{
|
||||||
|
internal static class FastCopy
|
||||||
|
{
|
||||||
|
static ConcurrentDictionary<string, object> copiers = new ConcurrentDictionary<string, object>();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 复制两个对象同名属性值
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="S"></typeparam>
|
||||||
|
/// <typeparam name="T"></typeparam>
|
||||||
|
/// <param name="source">源对象</param>
|
||||||
|
/// <param name="target">目标对象</param>
|
||||||
|
/// <param name="copyNull">源对象属性值为null时,是否将值复制给目标对象</param>
|
||||||
|
public static void Copy<S, T>(S source, T target, bool copyNull = true)
|
||||||
|
{
|
||||||
|
string name = string.Format("{0}_{1}_{2}", typeof(S), typeof(T), copyNull);
|
||||||
|
|
||||||
|
object targetCopier;
|
||||||
|
if (!copiers.TryGetValue(name, out targetCopier))
|
||||||
|
{
|
||||||
|
Action<S, T> copier = CreateCopier<S, T>(copyNull);
|
||||||
|
copiers.TryAdd(name, copier);
|
||||||
|
targetCopier = copier;
|
||||||
|
}
|
||||||
|
|
||||||
|
Action<S, T> action = (Action<S, T>)targetCopier;
|
||||||
|
action(source, target);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 为指定的两种类型编译生成属性复制委托
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="S"></typeparam>
|
||||||
|
/// <typeparam name="T"></typeparam>
|
||||||
|
/// <param name="copyNull">源对象属性值为null时,是否将值复制给目标对象</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
private static Action<S, T> CreateCopier<S, T>(bool copyNull)
|
||||||
|
{
|
||||||
|
ParameterExpression source = Parameter(typeof(S));
|
||||||
|
ParameterExpression target = Parameter(typeof(T));
|
||||||
|
var sourceProps = typeof(S).GetProperties(BindingFlags.Instance | BindingFlags.Public).Where(p => p.CanRead).ToList();
|
||||||
|
var targetProps = typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.Public).Where(p => p.CanWrite).ToList();
|
||||||
|
|
||||||
|
// 查找可进行赋值的属性
|
||||||
|
var copyProps = targetProps.Where(tProp => sourceProps.Where(sProp => sProp.Name == tProp.Name// 名称一致 且
|
||||||
|
&& (
|
||||||
|
sProp.PropertyType == tProp.PropertyType// 属性类型一致 或
|
||||||
|
|| sProp.PropertyType.IsAssignableFrom(tProp.PropertyType) // 源属性类型 为 目标属性类型 的 子类;eg:object target = string source; 或
|
||||||
|
|| (tProp.PropertyType.IsValueType && sProp.PropertyType.IsValueType && // 属性为值类型且基础类型一致,但目标属性为可空类型 eg:int? num = int num;
|
||||||
|
((tProp.PropertyType.GenericTypeArguments.Length > 0 ? tProp.PropertyType.GenericTypeArguments[0] : tProp.PropertyType) == sProp.PropertyType))
|
||||||
|
)).Count() > 0);
|
||||||
|
|
||||||
|
List<Expression> expressionList = new List<Expression>();
|
||||||
|
foreach (var prop in copyProps)
|
||||||
|
{
|
||||||
|
if (prop.PropertyType.IsValueType)// 属性为值类型
|
||||||
|
{
|
||||||
|
PropertyInfo sProp = typeof(S).GetProperty(prop.Name);
|
||||||
|
PropertyInfo tProp = typeof(T).GetProperty(prop.Name);
|
||||||
|
if (sProp.PropertyType == tProp.PropertyType)// 属性类型一致 eg:int num = int num; 或 int? num = int? num;
|
||||||
|
{
|
||||||
|
var assign = Assign(Property(target, prop.Name), Property(source, prop.Name));
|
||||||
|
expressionList.Add(assign);
|
||||||
|
}
|
||||||
|
else if (sProp.PropertyType.GenericTypeArguments.Length <= 0 && tProp.PropertyType.GenericTypeArguments.Length > 0)// 属性类型不一致且目标属性类型为可空类型 eg:int? num = int num;
|
||||||
|
{
|
||||||
|
var convert = Convert(Expression.Property(source, prop.Name), tProp.PropertyType);
|
||||||
|
var cvAssign = Assign(Expression.Property(target, prop.Name), convert);
|
||||||
|
expressionList.Add(cvAssign);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else// 属性为引用类型
|
||||||
|
{
|
||||||
|
var assign = Assign(Property(target, prop.Name), Property(source, prop.Name));// 编译生成属性赋值语句 target.{PropertyName} = source.{PropertyName};
|
||||||
|
var sourcePropIsNull = Equal(Constant(null, prop.PropertyType), Property(source, prop.Name));// 判断源属性值是否为Null;编译生成 source.{PropertyName} == null
|
||||||
|
var setNull = IsTrue(Constant(copyNull));// 判断是否复制Null值 编译生成 copyNull == True
|
||||||
|
var setNullTest = IfThen(setNull, assign);
|
||||||
|
var condition = IfThenElse(sourcePropIsNull, setNullTest, assign);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 编译生成
|
||||||
|
* if(source.{PropertyName} == null)
|
||||||
|
* {
|
||||||
|
* if(setNull)
|
||||||
|
* {
|
||||||
|
* target.{PropertyName} = source.{PropertyName};
|
||||||
|
* }
|
||||||
|
* }
|
||||||
|
* else
|
||||||
|
* {
|
||||||
|
* target.{PropertyName} = source.{PropertyName};
|
||||||
|
* }
|
||||||
|
*/
|
||||||
|
expressionList.Add(condition);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var block = Block(expressionList.ToArray());
|
||||||
|
Expression<Action<S, T>> lambda = Lambda<Action<S, T>>(block, source, target);
|
||||||
|
return lambda.Compile();
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static List<string> GetDiff<T>(T item, T trackingData) where T : class, new()
|
||||||
|
{
|
||||||
|
List<string> result = new List<string>();
|
||||||
|
foreach (var t in typeof(T).GetProperties())
|
||||||
|
{
|
||||||
|
var leftvalue = t.GetValue(item);
|
||||||
|
var rightvalue = t.GetValue(trackingData);
|
||||||
|
if ((leftvalue == null && rightvalue != null))
|
||||||
|
{
|
||||||
|
result.Add(t.Name);
|
||||||
|
}
|
||||||
|
else if (leftvalue!=null&&!leftvalue.Equals(rightvalue))
|
||||||
|
{
|
||||||
|
result.Add(t.Name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user