- 修复 Aop.AuditValue 与 FreeSql.Repository 主键状态管理的冲突;

This commit is contained in:
28810
2019-08-31 03:00:17 +08:00
parent 16bd3941c5
commit baf6d768a4
14 changed files with 514 additions and 52 deletions

View File

@ -71,19 +71,56 @@ namespace FreeSql.Internal.CommonProvider
public IInsert<T1> AppendData(T1 source)
{
if (source != null) _source.Add(source);
if (source != null)
{
AuditDataValue(this, source, _orm, _table);
_source.Add(source);
}
return this;
}
public IInsert<T1> AppendData(T1[] source)
{
if (source != null) _source.AddRange(source);
if (source != null)
{
AuditDataValue(this, source, _orm, _table);
_source.AddRange(source);
}
return this;
}
public IInsert<T1> AppendData(IEnumerable<T1> source)
{
if (source != null) _source.AddRange(source.Where(a => a != null));
if (source != null)
{
source = source.Where(a => a != null).ToList();
AuditDataValue(this, source, _orm, _table);
_source.AddRange(source);
}
return this;
}
public static void AuditDataValue(object sender, IEnumerable<T1> data, IFreeSql orm, TableInfo table)
{
if (data?.Any() != true) return;
foreach (var d in data)
AuditDataValue(sender, d, orm, table);
}
public static void AuditDataValue(object sender, T1 data, IFreeSql orm, TableInfo table)
{
if (data == null) return;
foreach (var col in table.Columns.Values)
{
object val = col.GetMapValue(data);
if (col.Attribute.IsPrimary && col.Attribute.MapType.NullableTypeOrThis() == typeof(Guid) && (val == null || (Guid)val == Guid.Empty))
col.SetMapValue(data, val = FreeUtil.NewMongodbId());
if (orm.Aop.AuditValue != null)
{
var auditArgs = new Aop.AuditValueEventArgs(Aop.AuditValueType.Insert, col, table.Properties[col.CsName], val);
orm.Aop.AuditValue(sender, auditArgs);
if (auditArgs.IsChanged)
col.SetMapValue(data, val = auditArgs.Value);
}
}
}
#region values数量限制
protected List<T1>[] SplitSource(int valuesLimit, int parameterLimit)
@ -557,15 +594,6 @@ namespace FreeSql.Internal.CommonProvider
if (colidx2 > 0) sb.Append(", ");
object val = col.GetMapValue(d);
if (col.Attribute.IsPrimary && col.Attribute.MapType.NullableTypeOrThis() == typeof(Guid) && (val == null || (Guid)val == Guid.Empty))
col.SetMapValue(d, val = FreeUtil.NewMongodbId());
if (_orm.Aop.AuditValue != null)
{
var auditArgs = new Aop.AuditValueEventArgs(Aop.AutoValueType.Insert, col, _table.Properties[col.CsName], val);
_orm.Aop.AuditValue(this, auditArgs);
if (auditArgs.IsChanged)
col.SetMapValue(d, val = auditArgs.Value);
}
if (_noneParameter)
sb.Append(_commonUtils.GetNoneParamaterSqlValue(specialParams, col.Attribute.MapType, val));
else

View File

@ -401,10 +401,42 @@ namespace FreeSql.Internal.CommonProvider
return this;
}
public static void AuditDataValue(object sender, IEnumerable<T1> data, IFreeSql orm, TableInfo table)
{
if (data?.Any() != true) return;
if (orm.Aop.AuditValue == null) return;
foreach (var d in data)
{
if (d == null) continue;
foreach (var col in table.Columns.Values)
{
object val = col.GetMapValue(d);
var auditArgs = new Aop.AuditValueEventArgs(Aop.AuditValueType.Update, col, table.Properties[col.CsName], val);
orm.Aop.AuditValue(sender, auditArgs);
if (auditArgs.IsChanged)
col.SetMapValue(d, val = auditArgs.Value);
}
}
}
public static void AuditDataValue(object sender, T1 data, IFreeSql orm, TableInfo table)
{
if (orm.Aop.AuditValue == null) return;
if (data == null) return;
foreach (var col in table.Columns.Values)
{
object val = col.GetMapValue(data);
var auditArgs = new Aop.AuditValueEventArgs(Aop.AuditValueType.Update, col, table.Properties[col.CsName], val);
orm.Aop.AuditValue(sender, auditArgs);
if (auditArgs.IsChanged)
col.SetMapValue(data, val = auditArgs.Value);
}
}
public IUpdate<T1> SetSource(T1 source) => this.SetSource(new[] { source });
public IUpdate<T1> SetSource(IEnumerable<T1> source)
{
if (source == null || source.Any() == false) return this;
AuditDataValue(this, source, _orm, _table);
_source.AddRange(source.Where(a => a != null));
return this;
}
@ -606,13 +638,6 @@ namespace FreeSql.Internal.CommonProvider
if (colidx > 0) sb.Append(", ");
sb.Append(_commonUtils.QuoteSqlName(col.Attribute.Name)).Append(" = ");
var val = col.GetMapValue(_source.First());
if (_orm.Aop.AuditValue != null)
{
var auditArgs = new Aop.AuditValueEventArgs(Aop.AutoValueType.Update, col, _table.Properties[col.CsName], val);
_orm.Aop.AuditValue(this, auditArgs);
if (auditArgs.IsChanged)
col.SetMapValue(_source.First(), val = auditArgs.Value);
}
if (_noneParameter)
sb.Append(_commonUtils.GetNoneParamaterSqlValue(_paramsSource, col.Attribute.MapType, val));
else
@ -629,7 +654,7 @@ namespace FreeSql.Internal.CommonProvider
else if (_source.Count > 1)
{ //批量保存 Source
if (_table.Primarys.Any() == false) return null;
var caseWhen = new StringBuilder();
caseWhen.Append("CASE ");
ToSqlCase(caseWhen, _table.Primarys);
@ -652,13 +677,6 @@ namespace FreeSql.Internal.CommonProvider
ToSqlWhen(cwsb, _table.Primarys, d);
cwsb.Append(" THEN ");
var val = col.GetMapValue(d);
if (_orm.Aop.AuditValue != null)
{
var auditArgs = new Aop.AuditValueEventArgs(Aop.AutoValueType.Update, col, _table.Properties[col.CsName], val);
_orm.Aop.AuditValue(this, auditArgs);
if (auditArgs.IsChanged)
col.SetMapValue(_source.First(), val = auditArgs.Value);
}
if (_noneParameter)
cwsb.Append(_commonUtils.GetNoneParamaterSqlValue(_paramsSource, col.Attribute.MapType, val));
else

View File

@ -57,22 +57,22 @@ namespace FreeSql.Internal.Model
var objExp = Expression.Parameter(typeof(object), "obj");
var valExp = Expression.Parameter(typeof(object), "val");
if (Attribute.MapType == CsType)
return Expression.Lambda<Action<object, object>>(
Expression.Assign(Expression.MakeMemberAccess(
Expression.TypeAs(objExp, col.Table.Type),
Table.Properties[col.CsName]
), Expression.Convert(
valExp,
Attribute.MapType)), objExp, valExp).Compile();
//if (Attribute.MapType == CsType)
// return Expression.Lambda<Action<object, object>>(
// Expression.Assign(Expression.MakeMemberAccess(
// Expression.TypeAs(objExp, col.Table.Type),
// Table.Properties[col.CsName]
// ), Expression.Convert(
// valExp,
// Attribute.MapType)), objExp, valExp).Compile();
return Expression.Lambda<Action<object, object>>(
Expression.Assign(Expression.MakeMemberAccess(
Expression.TypeAs(objExp, col.Table.Type),
Table.Properties[col.CsName]
), Expression.Convert(
Utils.GetDataReaderValueBlockExpression(Attribute.MapType, valExp),
Attribute.MapType)), objExp, valExp).Compile();
Utils.GetDataReaderValueBlockExpression(CsType, valExp),
CsType)), objExp, valExp).Compile();
});
func(obj, val);
}