## v0.3.22

- 优化 导航属性 ManyToOne 名称查找规则;
- 增加 IFreeSql.Aop 属性,未来所有拦截方法都在这里,第一期支持如下:
  * 监控 ToList 返回的的数据,用于拦截重新装饰;
  * 监视 Where,包括 select/update/delete,返回值 true 时可使上层不被执行;
  * 可自定义解析表达式;
- 增加 ISelect.TractToList,用于单次跟踪或审核实体;
- 优化 FreeSql.DbContext SaveChanges;
This commit is contained in:
28810
2019-03-22 00:26:08 +08:00
parent c20a0bbd54
commit 7f3aa84ffe
27 changed files with 720 additions and 523 deletions

View File

@ -32,8 +32,10 @@ namespace FreeSql {
var set = this.Set(prop.PropertyType.GenericTypeArguments[0]);
prop.SetValue(this, set);
AllSets.Add(prop, set);
AllSets.Add(prop.Name, set);
}
//_fsql.Aop.ToList += AopToList;
}
protected virtual void OnConfiguring(DbContextOptionsBuilder builder) {
@ -43,7 +45,7 @@ namespace FreeSql {
public DbSet<TEntity> Set<TEntity>() where TEntity : class => this.Set(typeof(TEntity)) as DbSet<TEntity>;
public object Set(Type entityType) => Activator.CreateInstance(typeof(BaseDbSet<>).MakeGenericType(entityType), this);
protected Dictionary<PropertyInfo, object> AllSets => new Dictionary<PropertyInfo, object>();
protected Dictionary<string, object> AllSets { get; } = new Dictionary<string, object>();
public long SaveChanges() {
ExecCommand();
@ -53,91 +55,61 @@ namespace FreeSql {
internal class ExecCommandInfo {
public ExecCommandInfoType actionType { get; set; }
public Type entityType { get; set; }
public object dbSet { get; set; }
public Type stateType { get; set; }
public object state { get; set; }
}
internal enum ExecCommandInfoType { Insert, Update, Delete }
Queue<ExecCommandInfo> _actions = new Queue<ExecCommandInfo>();
internal long _affrows = 0;
internal void EnqueueAction(ExecCommandInfoType actionType, Type entityType, object dbSet, object state) {
_actions.Enqueue(new ExecCommandInfo { actionType = actionType, entityType = entityType, dbSet = dbSet, state = state });
internal void EnqueueAction(ExecCommandInfoType actionType, object dbSet, Type stateType, object state) {
_actions.Enqueue(new ExecCommandInfo { actionType = actionType, dbSet = dbSet, stateType = stateType, state = state });
}
static ConcurrentDictionary<Type, Func<object, object[], int>> _dicExecCommandInsert = new ConcurrentDictionary<Type, Func<object, object[], int>>();
static ConcurrentDictionary<Type, Func<object, object[], int>> _dicExecCommandDelete = new ConcurrentDictionary<Type, Func<object, object[], int>>();
static ConcurrentDictionary<Type, Func<object, object[], bool, int>> _dicExecCommandUpdate = new ConcurrentDictionary<Type, Func<object, object[], bool, int>>();
static Dictionary<Type, Dictionary<string, Func<object, object[], int>>> _dicExecCommandDbContextBetch = new Dictionary<Type, Dictionary<string, Func<object, object[], int>>>();
internal void ExecCommand() {
ExecCommandInfo oldinfo = null;
var states = new List<object>();
Action funcInsert = () => {
var insertFunc = _dicExecCommandInsert.GetOrAdd(oldinfo.entityType, t => {
var arrType = t.MakeArrayType();
var dbsetType = typeof(DbSet<>).MakeGenericType(t);
var dbsetTypeInsert = dbsetType.GetMethod("OrmInsert", BindingFlags.NonPublic | BindingFlags.Instance, null, new[] { arrType }, null);
var insertBuilder = typeof(IInsert<>).MakeGenericType(t);
var insertExecuteAffrows = insertBuilder.GetMethod("ExecuteAffrows", new Type[0]);
Func<string, int> dbContextBetch = methodName => {
if (_dicExecCommandDbContextBetch.TryGetValue(oldinfo.stateType, out var trydic) == false)
trydic = new Dictionary<string, Func<object, object[], int>>();
if (trydic.TryGetValue(methodName, out var tryfunc) == false) {
var arrType = oldinfo.stateType.MakeArrayType();
var dbsetType = oldinfo.dbSet.GetType().BaseType;
var dbsetTypeMethod = dbsetType.GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Instance, null, new[] { arrType }, null);
var returnTarget = Expression.Label(typeof(int));
var parm1DbSet = Expression.Parameter(typeof(object));
var parm2Vals = Expression.Parameter(typeof(object[]));
var var1Vals = Expression.Variable(arrType);
return Expression.Lambda<Func<object, object[], int>>(Expression.Block(
tryfunc = Expression.Lambda<Func<object, object[], int>>(Expression.Block(
new[] { var1Vals },
Expression.Assign(var1Vals, Expression.Convert(FreeSql.Internal.Utils.GetDataReaderValueBlockExpression(arrType, parm2Vals), arrType)),
Expression.Return(returnTarget,
Expression.Call(
Expression.Call(Expression.Convert(parm1DbSet, dbsetType), dbsetTypeInsert, var1Vals),
insertExecuteAffrows
)
),
Expression.Return(returnTarget, Expression.Call(Expression.Convert(parm1DbSet, dbsetType), dbsetTypeMethod, var1Vals)),
Expression.Label(returnTarget, Expression.Default(typeof(int)))
), new[] { parm1DbSet, parm2Vals }).Compile();
});
_affrows += insertFunc(oldinfo.dbSet, states.ToArray());
states.Clear();
trydic.Add(methodName, tryfunc);
}
return tryfunc(oldinfo.dbSet, states.ToArray());
};
Action funcDelete = () => {
var deleteFunc = _dicExecCommandDelete.GetOrAdd(oldinfo.entityType, t => {
var arrType = t.MakeArrayType();
var dbsetType = typeof(DbSet<>).MakeGenericType(t);
var dbsetTypeDelete = dbsetType.GetMethod("DbContextBetchRemove", BindingFlags.NonPublic | BindingFlags.Instance, null, new[] { arrType }, null);
var returnTarget = Expression.Label(typeof(int));
var parm1DbSet = Expression.Parameter(typeof(object));
var parm2Vals = Expression.Parameter(typeof(object[]));
var var1Vals = Expression.Variable(arrType);
return Expression.Lambda<Func<object, object[], int>>(Expression.Block(
new[] { var1Vals },
Expression.Assign(var1Vals, Expression.Convert(FreeSql.Internal.Utils.GetDataReaderValueBlockExpression(arrType, parm2Vals), arrType)),
Expression.Return(returnTarget, Expression.Call(Expression.Convert(parm1DbSet, dbsetType), dbsetTypeDelete, var1Vals)),
Expression.Label(returnTarget, Expression.Default(typeof(int)))
), new[] { parm1DbSet, parm2Vals }).Compile();
});
_affrows += deleteFunc(oldinfo.dbSet, states.ToArray());
_affrows += dbContextBetch("DbContextBetchRemove");
states.Clear();
};
Action funcInsert = () => {
_affrows += dbContextBetch("DbContextBetchAdd");
states.Clear();
};
Action<bool> funcUpdate = isLiveUpdate => {
var updateFunc = _dicExecCommandUpdate.GetOrAdd(oldinfo.entityType, t => {
var arrType = t.MakeArrayType();
var dbsetType = typeof(DbSet<>).MakeGenericType(t);
var dbsetTypeUpdate = dbsetType.GetMethod("DbContextBetchUpdate", BindingFlags.NonPublic | BindingFlags.Instance, null, new[] { arrType, typeof(bool) }, null);
var returnTarget = Expression.Label(typeof(int));
var parm1DbSet = Expression.Parameter(typeof(object));
var parm2Vals = Expression.Parameter(typeof(object[]));
var parm3IsLiveUpdate = Expression.Parameter(typeof(bool));
var var1Vals = Expression.Variable(arrType);
return Expression.Lambda<Func<object, object[], bool, int>>(Expression.Block(
new[] { var1Vals },
Expression.Assign(var1Vals, Expression.Convert(FreeSql.Internal.Utils.GetDataReaderValueBlockExpression(arrType, parm2Vals), arrType)),
Expression.Return(returnTarget, Expression.Call(Expression.Convert(parm1DbSet, dbsetType), dbsetTypeUpdate, var1Vals, parm3IsLiveUpdate)),
Expression.Label(returnTarget, Expression.Default(typeof(int)))
), new[] { parm1DbSet, parm2Vals, parm3IsLiveUpdate }).Compile();
});
var affrows = updateFunc(oldinfo.dbSet, states.ToArray(), isLiveUpdate);
var affrows = 0;
if (isLiveUpdate) affrows = dbContextBetch("DbContextBetchUpdateNow");
else affrows = dbContextBetch("DbContextBetchUpdate");
if (affrows == -999) { //最后一个元素已被删除
states.RemoveAt(states.Count - 1);
return;
}
if (affrows > 0) {
_affrows += affrows;
var islastNotUpdated = states.Count != affrows;
@ -146,16 +118,16 @@ namespace FreeSql {
}
};
while(_actions.Any() || states.Any()) {
while (_actions.Any() || states.Any()) {
var info = _actions.Any() ? _actions.Dequeue() : null;
if (oldinfo == null) oldinfo = info;
var isLiveUpdate = false;
if (_actions.Any() == false && states.Any() ||
info != null && oldinfo.actionType != info.actionType ||
info != null && oldinfo.entityType != info.entityType) {
info != null && oldinfo.stateType != info.stateType) {
if (info != null && oldinfo.actionType == info.actionType && oldinfo.entityType == info.entityType) {
if (info != null && oldinfo.actionType == info.actionType && oldinfo.stateType == info.stateType) {
//最后一个,合起来发送
states.Add(info.state);
info = null;
@ -224,6 +196,7 @@ namespace FreeSql {
}
}
public void Dispose() {
//_fsql.Aop.ToList -= AopToList;
this.Rollback();
}
}

View File

@ -17,79 +17,49 @@ namespace FreeSql {
return _affrows;
}
static ConcurrentDictionary<Type, Func<object, object[], Task<int>>> _dicExecCommandAsyncInsert = new ConcurrentDictionary<Type, Func<object, object[], Task<int>>>();
static ConcurrentDictionary<Type, Func<object, object[], Task<int>>> _dicExecCommandAsyncDelete = new ConcurrentDictionary<Type, Func<object, object[], Task<int>>>();
static ConcurrentDictionary<Type, Func<object, object[], bool, Task<int>>> _dicExecCommandAsyncUpdate = new ConcurrentDictionary<Type, Func<object, object[], bool, Task<int>>>();
static Dictionary<Type, Dictionary<string, Func<object, object[], Task<int>>>> _dicExecCommandAsyncDbContextBetch = new Dictionary<Type, Dictionary<string, Func<object, object[], Task<int>>>>();
async internal Task ExecCommandAsync() {
ExecCommandInfo oldinfo = null;
var states = new List<object>();
Func<Task> funcInsert = async () => {
var insertFunc = _dicExecCommandAsyncInsert.GetOrAdd(oldinfo.entityType, t => {
var arrType = t.MakeArrayType();
var dbsetType = typeof(DbSet<>).MakeGenericType(t);
var dbsetTypeInsert = dbsetType.GetMethod("OrmInsert", BindingFlags.NonPublic | BindingFlags.Instance, null, new[] { arrType }, null);
var insertBuilder = typeof(IInsert<>).MakeGenericType(t);
var insertExecuteAffrows = insertBuilder.GetMethod("ExecuteAffrowsAsync", new Type[0]);
Func<string, Task<int>> dbContextBetch = methodName => {
if (_dicExecCommandAsyncDbContextBetch.TryGetValue(oldinfo.stateType, out var trydic) == false)
trydic = new Dictionary<string, Func<object, object[], Task<int>>>();
if (trydic.TryGetValue(methodName, out var tryfunc) == false) {
var arrType = oldinfo.stateType.MakeArrayType();
var dbsetType = oldinfo.dbSet.GetType().BaseType;
var dbsetTypeMethod = dbsetType.GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Instance, null, new[] { arrType }, null);
var returnTarget = Expression.Label(typeof(Task<int>));
var parm1DbSet = Expression.Parameter(typeof(object));
var parm2Vals = Expression.Parameter(typeof(object[]));
var var1Vals = Expression.Variable(arrType);
return Expression.Lambda<Func<object, object[], Task<int>>>(Expression.Block(
tryfunc = Expression.Lambda<Func<object, object[], Task<int>>>(Expression.Block(
new[] { var1Vals },
Expression.Assign(var1Vals, Expression.Convert(FreeSql.Internal.Utils.GetDataReaderValueBlockExpression(arrType, parm2Vals), arrType)),
Expression.Return(returnTarget,
Expression.Call(
Expression.Call(Expression.Convert(parm1DbSet, dbsetType), dbsetTypeInsert, var1Vals),
insertExecuteAffrows
)
),
Expression.Return(returnTarget, Expression.Call(Expression.Convert(parm1DbSet, dbsetType), dbsetTypeMethod, var1Vals)),
Expression.Label(returnTarget, Expression.Default(typeof(Task<int>)))
), new[] { parm1DbSet, parm2Vals }).Compile();
});
_affrows += await insertFunc(oldinfo.dbSet, states.ToArray());
states.Clear();
trydic.Add(methodName, tryfunc);
}
return tryfunc(oldinfo.dbSet, states.ToArray());
};
Func<Task> funcDelete = async () => {
var deleteFunc = _dicExecCommandAsyncDelete.GetOrAdd(oldinfo.entityType, t => {
var arrType = t.MakeArrayType();
var dbsetType = typeof(DbSet<>).MakeGenericType(t);
var dbsetTypeDelete = dbsetType.GetMethod("DbContextBetchRemoveAsync", BindingFlags.NonPublic | BindingFlags.Instance, null, new[] { arrType }, null);
var returnTarget = Expression.Label(typeof(Task<int>));
var parm1DbSet = Expression.Parameter(typeof(object));
var parm2Vals = Expression.Parameter(typeof(object[]));
var var1Vals = Expression.Variable(arrType);
return Expression.Lambda<Func<object, object[], Task<int>>>(Expression.Block(
new[] { var1Vals },
Expression.Assign(var1Vals, Expression.Convert(FreeSql.Internal.Utils.GetDataReaderValueBlockExpression(arrType, parm2Vals), arrType)),
Expression.Return(returnTarget, Expression.Call(Expression.Convert(parm1DbSet, dbsetType), dbsetTypeDelete, var1Vals)),
Expression.Label(returnTarget, Expression.Default(typeof(Task<int>)))
), new[] { parm1DbSet, parm2Vals }).Compile();
});
_affrows += await deleteFunc(oldinfo.dbSet, states.ToArray());
_affrows += await dbContextBetch("DbContextBetchRemoveAsync");
states.Clear();
};
Func<bool, Task> funcUpdate = async (isLiveUpdate) => {
var updateFunc = _dicExecCommandAsyncUpdate.GetOrAdd(oldinfo.entityType, t => {
var arrType = t.MakeArrayType();
var dbsetType = typeof(DbSet<>).MakeGenericType(t);
var dbsetTypeUpdate = dbsetType.GetMethod("DbContextBetchUpdateAsync", BindingFlags.NonPublic | BindingFlags.Instance, null, new[] { arrType, typeof(bool) }, null);
var returnTarget = Expression.Label(typeof(Task<int>));
var parm1DbSet = Expression.Parameter(typeof(object));
var parm2Vals = Expression.Parameter(typeof(object[]));
var parm3IsLiveUpdate = Expression.Parameter(typeof(bool));
var var1Vals = Expression.Variable(arrType);
return Expression.Lambda<Func<object, object[], bool, Task<int>>>(Expression.Block(
new[] { var1Vals },
Expression.Assign(var1Vals, Expression.Convert(FreeSql.Internal.Utils.GetDataReaderValueBlockExpression(arrType, parm2Vals), arrType)),
Expression.Return(returnTarget, Expression.Call(Expression.Convert(parm1DbSet, dbsetType), dbsetTypeUpdate, var1Vals, parm3IsLiveUpdate)),
Expression.Label(returnTarget, Expression.Default(typeof(Task<int>)))
), new[] { parm1DbSet, parm2Vals, parm3IsLiveUpdate }).Compile();
});
var affrows = await updateFunc(oldinfo.dbSet, states.ToArray(), isLiveUpdate);
Func<Task> funcInsert = async () => {
_affrows += await dbContextBetch("DbContextBetchAddAsync");
states.Clear();
};
Func<bool, Task> funcUpdate = async isLiveUpdate => {
var affrows = 0;
if (isLiveUpdate) affrows = await dbContextBetch("DbContextBetchUpdateNowAsync");
else affrows = await dbContextBetch("DbContextBetchUpdateAsync");
if (affrows == -999) { //最后一个元素已被删除
states.RemoveAt(states.Count - 1);
return;
}
if (affrows > 0) {
_affrows += affrows;
var islastNotUpdated = states.Count != affrows;
@ -98,16 +68,16 @@ namespace FreeSql {
}
};
while(_actions.Any() || states.Any()) {
while (_actions.Any() || states.Any()) {
var info = _actions.Any() ? _actions.Dequeue() : null;
if (oldinfo == null) oldinfo = info;
var isLiveUpdate = false;
if (_actions.Any() == false && states.Any() ||
info != null && oldinfo.actionType != info.actionType ||
info != null && oldinfo.entityType != info.entityType) {
info != null && oldinfo.stateType != info.stateType) {
if (info != null && oldinfo.actionType == info.actionType && oldinfo.entityType == info.entityType) {
if (info != null && oldinfo.actionType == info.actionType && oldinfo.stateType == info.stateType) {
//最后一个,合起来发送
states.Add(info.state);
info = null;

View File

@ -10,172 +10,92 @@ using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
using FreeSql.Extensions;
namespace FreeSql {
public abstract partial class DbSet<TEntity> where TEntity : class {
protected DbContext _ctx;
IFreeSql _fsql => _ctx._fsql;
protected ISelect<TEntity> OrmSelect(object dywhere) => _ctx._fsql.Select<TEntity>(dywhere).WithTransaction(_ctx.GetOrBeginTransaction(false));
protected ISelect<TEntity> OrmSelect(object dywhere) => _fsql.Select<TEntity>(dywhere).WithTransaction(_ctx.GetOrBeginTransaction(false)).TrackToList(TrackToList);
protected IInsert<TEntity> OrmInsert() => _ctx._fsql.Insert<TEntity>().WithTransaction(_ctx.GetOrBeginTransaction());
protected IInsert<TEntity> OrmInsert(TEntity source) => _ctx._fsql.Insert<TEntity>(source).WithTransaction(_ctx.GetOrBeginTransaction());
protected IInsert<TEntity> OrmInsert(TEntity[] source) => _ctx._fsql.Insert<TEntity>(source).WithTransaction(_ctx.GetOrBeginTransaction());
protected IInsert<TEntity> OrmInsert(IEnumerable<TEntity> source) => _ctx._fsql.Insert<TEntity>(source).WithTransaction(_ctx.GetOrBeginTransaction());
protected IInsert<TEntity> OrmInsert() => _fsql.Insert<TEntity>().WithTransaction(_ctx.GetOrBeginTransaction());
protected IInsert<TEntity> OrmInsert(TEntity source) => _fsql.Insert<TEntity>(source).WithTransaction(_ctx.GetOrBeginTransaction());
protected IInsert<TEntity> OrmInsert(TEntity[] source) => _fsql.Insert<TEntity>(source).WithTransaction(_ctx.GetOrBeginTransaction());
protected IInsert<TEntity> OrmInsert(IEnumerable<TEntity> source) => _fsql.Insert<TEntity>(source).WithTransaction(_ctx.GetOrBeginTransaction());
protected IUpdate<TEntity> OrmUpdate(object dywhere) => _ctx._fsql.Update<TEntity>(dywhere).WithTransaction(_ctx.GetOrBeginTransaction());
protected IDelete<TEntity> OrmDelete(object dywhere) => _ctx._fsql.Delete<TEntity>(dywhere).WithTransaction(_ctx.GetOrBeginTransaction());
protected IUpdate<TEntity> OrmUpdate(object dywhere) => _fsql.Update<TEntity>(dywhere).WithTransaction(_ctx.GetOrBeginTransaction());
protected IDelete<TEntity> OrmDelete(object dywhere) => _fsql.Delete<TEntity>(dywhere).WithTransaction(_ctx.GetOrBeginTransaction());
public ISelect<TEntity> Select => this.OrmSelect(null);
public ISelect<TEntity> Where(Expression<Func<TEntity, bool>> exp) => this.OrmSelect(null).Where(exp);
public ISelect<TEntity> WhereIf(bool condition, Expression<Func<TEntity, bool>> exp) => this.OrmSelect(null).WhereIf(condition, exp);
protected Dictionary<string, TEntity> _vals = new Dictionary<string, TEntity>();
protected Dictionary<string, EntityState> _vals = new Dictionary<string, EntityState>();
TableInfo _tablePriv;
protected TableInfo _table => _tablePriv ?? (_tablePriv = _ctx._orm.CodeFirst.GetTableByEntity(_entityType));
protected TableInfo _table => _tablePriv ?? (_tablePriv = _fsql.CodeFirst.GetTableByEntity(_entityType));
ColumnInfo[] _tableIdentitysPriv;
protected ColumnInfo[] _tableIdentitys => _tableIdentitysPriv ?? (_tableIdentitysPriv = _table.Primarys.Where(a => a.Attribute.IsIdentity).ToArray());
protected Type _entityType = typeof(TEntity);
static ConcurrentDictionary<Type, Func<TEntity, string>> _dicGetEntityKeyString = new ConcurrentDictionary<Type, Func<TEntity, string>>();
static MethodInfo MethodStringBuilderAppend = typeof(StringBuilder).GetMethod("Append", new Type[] { typeof(object) });
static MethodInfo MethodStringBuilderToString = typeof(StringBuilder).GetMethod("ToString", new Type[0]);
static PropertyInfo MethodStringBuilderLength = typeof(StringBuilder).GetProperty("Length");
static MethodInfo MethodStringConcat = typeof(string).GetMethod("Concat", new Type[]{ typeof(object) });
string GetEntityKeyString(TEntity item) {
var func = _dicGetEntityKeyString.GetOrAdd(_entityType, t => {
var pks = _table.Primarys;
var returnTarget = Expression.Label(typeof(string));
var parm1 = Expression.Parameter(_entityType);
var var1Sb = Expression.Variable(typeof(StringBuilder));
var var3IsNull = Expression.Variable(typeof(bool));
var exps = new List<Expression>();
exps.AddRange(new Expression[] {
Expression.Assign(var1Sb, Expression.New(typeof(StringBuilder))),
Expression.Assign(var3IsNull, Expression.Constant(false))
});
for (var a = 0; a < pks.Length; a++) {
exps.Add(
Expression.IfThen(
Expression.Equal(var3IsNull, Expression.Constant(false)),
Expression.IfThenElse(
Expression.Equal(Expression.MakeMemberAccess(parm1, _table.Properties[pks[a].CsName]), Expression.Default(pks[a].CsType)),
Expression.Assign(var3IsNull, Expression.Constant(true)),
Expression.Block(
new Expression[]{
a > 0 ? Expression.Call(var1Sb, MethodStringBuilderAppend, Expression.Constant("*|_,,_|*" )) : null,
Expression.Call(var1Sb, MethodStringBuilderAppend,
Expression.Convert(Expression.MakeMemberAccess(parm1, _table.Properties[pks[a].CsName]), typeof(object))
)
}.Where(c => c != null).ToArray()
)
)
)
);
}
exps.Add(
Expression.IfThen(
Expression.Equal(var3IsNull, Expression.Constant(false)),
Expression.Return(returnTarget, Expression.Call(var1Sb, MethodStringBuilderToString))
)
);
exps.Add(Expression.Label(returnTarget, Expression.Default(typeof(string))));
return Expression.Lambda<Func<TEntity, string>>(Expression.Block(new[] { var1Sb, var3IsNull }, exps), new[] { parm1 }).Compile();
});
return func(item);
public class EntityState {
public TEntity Value { get; set; }
public string Key { get; set; }
public DateTime Time { get; set; }
}
static ConcurrentDictionary<Type, Action<TEntity, TEntity>> _dicCopyNewValueToEntity = new ConcurrentDictionary<Type, Action<TEntity, TEntity>>();
void CopyNewValueToEntity(TEntity old, TEntity newvalue) {
var func = _dicCopyNewValueToEntity.GetOrAdd(_entityType, t => {
var parm1 = Expression.Parameter(_entityType);
var parm2 = Expression.Parameter(_entityType);
var exps = new List<Expression>();
foreach (var prop in _table.Properties.Values) {
if (_table.ColumnsByCs.ContainsKey(prop.Name)) {
exps.Add(
Expression.Assign(
Expression.MakeMemberAccess(parm1, prop),
Expression.MakeMemberAccess(parm2, prop)
)
);
} else {
exps.Add(
Expression.Assign(
Expression.MakeMemberAccess(parm1, prop),
Expression.Default(prop.PropertyType)
)
);
}
}
return Expression.Lambda<Action<TEntity, TEntity>>(Expression.Block(exps), new[] { parm1, parm2 }).Compile();
});
func(old, newvalue);
int DbContextBetcAdd(EntityState[] dels) {
if (dels.Any() == false) return 0;
var affrows = this.OrmInsert(dels.Select(a => a.Value)).ExecuteAffrows();
return affrows;
}
static ConcurrentDictionary<Type, Action<TEntity, long>> _dicSetEntityIdentityValue = new ConcurrentDictionary<Type, Action<TEntity, long>>();
void SetEntityIdentityValue(TEntity old, long idtval) {
var func = _dicSetEntityIdentityValue.GetOrAdd(_entityType, t => {
var parm1 = Expression.Parameter(_entityType);
var parm2 = Expression.Parameter(typeof(long));
var exps = new List<Expression>();
exps.Add(
Expression.Assign(
Expression.MakeMemberAccess(parm1, _table.Properties[_table.Primarys[0].CsName]),
Expression.Convert(FreeSql.Internal.Utils.GetDataReaderValueBlockExpression(_table.Primarys[0].CsType, Expression.Convert(parm2, typeof(object))), _table.Primarys[0].CsType)
)
);
return Expression.Lambda<Action<TEntity, long>>(Expression.Block(exps), new[] { parm1, parm2 }).Compile();
});
func(old, idtval);
}
public void Add(TEntity source) {
if (source == null) throw new ArgumentNullException(nameof(source));
var key = GetEntityKeyString(source);
TEntity newval = null;
var key = _fsql.GetEntityKeyString(source);
EntityState state = new EntityState();
if (string.IsNullOrEmpty(key)) {
var ids = _table.Primarys.Where(a => a.Attribute.IsIdentity).ToArray();
switch(_ctx._orm.Ado.DataType) {
switch(_fsql.Ado.DataType) {
case DataType.SqlServer:
case DataType.PostgreSQL:
if (ids.Length == 1 && _table.Primarys.Length == 1) {
if (_tableIdentitys.Length == 1 && _table.Primarys.Length == 1) {
_ctx.ExecCommand();
var idtval = this.OrmInsert(source).ExecuteIdentity();
_ctx._affrows++;
SetEntityIdentityValue(source, idtval);
_fsql.SetEntityIdentityValue(source, idtval);
} else {
_ctx.ExecCommand();
newval = this.OrmInsert(source).ExecuteInserted().First();
state.Value = this.OrmInsert(source).ExecuteInserted().First();
_ctx._affrows++;
CopyNewValueToEntity(source, newval);
_fsql.CopyEntityValue(source, state.Value);
}
break;
case DataType.MySql:
case DataType.Oracle:
case DataType.Sqlite:
if (ids.Length == 1 && _table.Primarys.Length == 1) {
if (_tableIdentitys.Length == 1 && _table.Primarys.Length == 1) {
_ctx.ExecCommand();
var idtval = this.OrmInsert(source).ExecuteIdentity();
_ctx._affrows++;
SetEntityIdentityValue(source, idtval);
_fsql.SetEntityIdentityValue(source, idtval);
} else {
throw new Exception("DbSet.Add 失败,由于实体没有主键值或者没有配置自增或者自增列数不为1");
throw new Exception($"DbSet.Add 失败,未设置主键或者没有配置自增或者自增列数不为1{_fsql.GetEntityString(source)}");
}
break;
}
key = GetEntityKeyString(source);
state.Key = key = _fsql.GetEntityKeyString(source);
state.Time = DateTime.Now;
} else {
if (_vals.ContainsKey(key))
throw new Exception("DbSet.Add 失败,实体数据已存在,请勿重复添加");
_ctx.EnqueueAction(DbContext.ExecCommandInfoType.Insert, _entityType, this, source);
throw new Exception($"DbSet.Add 失败,实体数据已存在,请勿重复添加{_fsql.GetEntityString(source)}");
_ctx.EnqueueAction(DbContext.ExecCommandInfoType.Insert, this, typeof(EntityState), state);
}
if (newval == null) {
newval = Activator.CreateInstance<TEntity>();
CopyNewValueToEntity(newval, source);
if (state.Value == null) {
state.Value = Activator.CreateInstance<TEntity>();
_fsql.CopyEntityValue(state.Value, source); //copy, 记录旧值版本
}
_vals.Add(key, newval);
_vals.Add(key, state);
}
public void AddRange(TEntity[] source) {
if (source == null) throw new ArgumentNullException(nameof(source));
@ -188,96 +108,52 @@ namespace FreeSql {
Add(item);
}
static ConcurrentDictionary<Type, Func<TEntity, TEntity, string>> _dicCompareUpdateIngoreColumns = new ConcurrentDictionary<Type, Func<TEntity, TEntity, string>>();
string CompareUpdateIngoreColumns(TEntity up, TEntity oldval) {
var func = _dicCompareUpdateIngoreColumns.GetOrAdd(_entityType, t => {
var returnTarget = Expression.Label(typeof(string));
var parm1 = Expression.Parameter(_entityType);
var parm2 = Expression.Parameter(_entityType);
var var1Sb = Expression.Variable(typeof(StringBuilder));
var exps = new List<Expression>();
exps.AddRange(new Expression[] {
Expression.Assign(var1Sb, Expression.New(typeof(StringBuilder)))
});
var a = 0;
foreach (var prop in _table.Properties.Values) {
if (_table.ColumnsByCs.TryGetValue(prop.Name, out var trycol) == false) continue;
exps.Add(
Expression.IfThen(
Expression.Equal(
Expression.MakeMemberAccess(parm1, prop),
Expression.MakeMemberAccess(parm2, prop)
),
Expression.Block(
new Expression[]{
a > 0 ? Expression.Call(var1Sb, MethodStringBuilderAppend, Expression.Constant(", " )) : null,
Expression.Call(var1Sb, MethodStringBuilderAppend, Expression.Constant(trycol.Attribute.Name))
}.Where(c => c != null).ToArray()
)
)
);
a++;
}
exps.Add(Expression.Return(returnTarget, Expression.Call(var1Sb, MethodStringBuilderToString)));
exps.Add(Expression.Label(returnTarget, Expression.Default(typeof(string))));
return Expression.Lambda<Func<TEntity, TEntity, string>>(Expression.Block(new[] { var1Sb }, exps), new[] { parm1, parm2 }).Compile();
});
return func(up, oldval);
}
int DbContextBetchUpdate(TEntity[] ups, bool isLiveUpdate) {
int DbContextBetchUpdate(EntityState[] ups) => DbContextBetchUpdatePriv(ups, false);
int DbContextBetchUpdateNow(EntityState[] ups) => DbContextBetchUpdatePriv(ups, true);
int DbContextBetchUpdatePriv(EntityState[] ups, bool isLiveUpdate) {
if (ups.Any() == false) return 0;
var uplst1 = ups[ups.Length - 1];
var uplst2 = ups.Length > 1 ? ups[ups.Length - 2] : null;
var lstkey1 = GetEntityKeyString(uplst1);
if (_vals.TryGetValue(lstkey1, out var lstval1) == false) throw new Exception("DbSet.Update 失败,实体应该先查询再修改。");
TEntity lstval2 = default(TEntity);
if (uplst2 != null) {
var lstkey2 = GetEntityKeyString(uplst2);
if (_vals.TryGetValue(lstkey2, out lstval2) == false) throw new Exception("DbSet.Update 失败,实体应该先查询再修改。");
}
if (_vals.TryGetValue(uplst1.Key, out var lstval1) == false) return -999;
var lstval2 = default(EntityState);
if (uplst2 != null && _vals.TryGetValue(uplst2.Key, out lstval2) == false) throw new Exception($"DbSet.Update 失败,实体应该先查询再修改:{_fsql.GetEntityString(uplst2.Value)}");
var cuig1 = CompareUpdateIngoreColumns(uplst1, lstval1);
var cuig2 = uplst2 != null ? CompareUpdateIngoreColumns(uplst2, lstval2) : null;
if (uplst2 != null && string.Compare(cuig1, cuig2, true) != 0) {
var cuig1 = _fsql.CompareEntityValueReturnColumns(uplst1.Value, lstval1.Value, true);
var cuig2 = uplst2 != null ? _fsql.CompareEntityValueReturnColumns(uplst2.Value, lstval2.Value, true) : null;
if (uplst2 != null && string.Compare(string.Join(",", cuig1), string.Join(",", cuig2)) != 0) {
//最后一个不保存
var ignores = cuig2.Split(new[] { ", " }, StringSplitOptions.None);
var source = ups.ToList();
source.RemoveAt(ups.Length - 1);
var affrows = this.OrmUpdate(null).SetSource(source).IgnoreColumns(ignores).ExecuteAffrows();
foreach(var newval in source) {
var newkey = GetEntityKeyString(newval);
if (_vals.TryGetValue(newkey, out var tryold))
CopyNewValueToEntity(tryold, newval);
var affrows = this.OrmUpdate(null).SetSource(source.Select(a => a.Value)).IgnoreColumns(cuig2).ExecuteAffrows();
foreach (var newval in source) {
if (_vals.TryGetValue(newval.Key, out var tryold))
_fsql.CopyEntityValue(tryold.Value, newval.Value);
}
return affrows;
} else if (isLiveUpdate) {
//立即保存
var ignores = cuig1.Split(new[] { ", " }, StringSplitOptions.None);
var affrows = this.OrmUpdate(null).SetSource(ups).IgnoreColumns(ignores).ExecuteAffrows();
foreach (var newval in ups) {
var newkey = GetEntityKeyString(newval);
if (_vals.TryGetValue(newkey, out var tryold))
CopyNewValueToEntity(tryold, newval);
var source = ups;
var affrows = this.OrmUpdate(null).SetSource(source.Select(a => a.Value)).IgnoreColumns(cuig1).ExecuteAffrows();
foreach (var newval in source) {
if (_vals.TryGetValue(newval.Key, out var tryold))
_fsql.CopyEntityValue(tryold.Value, newval.Value);
}
return Math.Min(ups.Length, affrows);
}
//等待下次对比再保存
return 0;
}
public void Update(TEntity source) {
if (source == null) throw new ArgumentNullException(nameof(source));
if (_table.Primarys.Any() == false) throw new Exception("DbSet.Update 失败,实体没有主键");
var key = GetEntityKeyString(source);
if (string.IsNullOrEmpty(key)) throw new Exception("DbSet.Update 失败,实体没有设置主键值。");
if (_table.Primarys.Any() == false) throw new Exception($"DbSet.Update 失败,实体没有主键{_fsql.GetEntityString(source)}");
var key = _fsql.GetEntityKeyString(source);
if (string.IsNullOrEmpty(key)) throw new Exception($"DbSet.Update 失败,未设置主键的值:{_fsql.GetEntityString(source)}");
if (_vals.TryGetValue(key, out var tryval) == false) throw new Exception($"DbSet.Update 失败,实体未被跟踪,更新前应该先做查询:{_fsql.GetEntityString(source)}");
var snap = Activator.CreateInstance<TEntity>();
CopyNewValueToEntity(snap, source);
if (_vals.TryGetValue(key, out var val) == false) _vals.Add(key, snap);
_ctx.EnqueueAction(DbContext.ExecCommandInfoType.Update, _entityType, this, snap);
_fsql.CopyEntityValue(snap, source); //copy避免SaveChanges前对象再次被修改
_ctx.EnqueueAction(DbContext.ExecCommandInfoType.Update, this, typeof(EntityState), new EntityState { Value = snap, Key = key, Time = DateTime.Now });
}
public void UpdateRange(TEntity[] source) {
if (source == null) throw new ArgumentNullException(nameof(source));
@ -290,28 +166,26 @@ namespace FreeSql {
Update(item);
}
int DbContextBetchRemove(TEntity[] dels) {
int DbContextBetchRemove(EntityState[] dels) {
if (dels.Any() == false) return 0;
var affrows = this.OrmDelete(dels).ExecuteAffrows();
foreach(var del in dels) {
var key = GetEntityKeyString(del);
_vals.Remove(key);
}
var affrows = this.OrmDelete(dels.Select(a => a.Value)).ExecuteAffrows();
//foreach (var del in dels)
// _vals.Remove(del.Key);
return affrows;
}
public void Remove(TEntity source) {
if (source == null) throw new ArgumentNullException(nameof(source));
if (_table.Primarys.Any() == false) throw new Exception("DbSet.Remove 失败,实体没有主键");
var key = GetEntityKeyString(source);
if (string.IsNullOrEmpty(key)) throw new Exception("DbSet.Remove 失败,实体没有设置主键值。");
if (_table.Primarys.Any() == false) throw new Exception($"DbSet.Remove 失败,实体没有主键{_fsql.GetEntityString(source)}");
var key = _fsql.GetEntityKeyString(source);
if (string.IsNullOrEmpty(key)) throw new Exception($"DbSet.Remove 失败,未设置主键的值:{_fsql.GetEntityString(source)}");
if (_vals.TryGetValue(key, out var tryval) == false) throw new Exception($"DbSet.Remove 失败,实体未被跟踪,删除前应该先做查询:{_fsql.GetEntityString(source)}");
var snap = Activator.CreateInstance<TEntity>();
CopyNewValueToEntity(snap, source);
if (_vals.TryGetValue(key, out var val) == false) _vals.Add(key, snap);
_fsql.CopyEntityValue(snap, source); //copy避免SaveChanges前对象再次被修改
_ctx.EnqueueAction(DbContext.ExecCommandInfoType.Delete, this, typeof(EntityState), new EntityState { Value = snap, Key = key, Time = DateTime.Now });
_ctx.EnqueueAction(DbContext.ExecCommandInfoType.Delete, _entityType, this, snap);
_vals.Remove(key);
_fsql.ClearEntityPrimaryValueWithIdentityAndGuid(source);
}
public void RemoveRange(TEntity[] source) {
if (source == null) throw new ArgumentNullException(nameof(source));
@ -323,6 +197,23 @@ namespace FreeSql {
foreach (var item in source)
Remove(item);
}
void TrackToList(object list) {
if (list == null) return;
var ls = list as IList<TEntity>;
foreach (var item in ls) {
var key = _fsql.GetEntityKeyString(item);
if (_vals.ContainsKey(key)) {
_fsql.CopyEntityValue(_vals[key].Value, item);
_vals[key].Time = DateTime.Now;
} else {
var snap = Activator.CreateInstance<TEntity>();
_fsql.CopyEntityValue(snap, item);
_vals.Add(key, new EntityState { Value = snap, Key = key, Time = DateTime.Now });
}
}
}
}
internal class BaseDbSet<TEntity> : DbSet<TEntity> where TEntity : class {

View File

@ -10,57 +10,62 @@ using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
using FreeSql.Extensions;
namespace FreeSql {
partial class DbSet<TEntity> {
abstract partial class DbSet<TEntity> {
async Task<int> DbContextBetcAddAsync(EntityState[] dels) {
if (dels.Any() == false) return 0;
var affrows = await this.OrmInsert(dels.Select(a => a.Value)).ExecuteAffrowsAsync();
return affrows;
}
async public Task AddAsync(TEntity source) {
if (source == null) throw new ArgumentNullException(nameof(source));
var key = GetEntityKeyString(source);
TEntity newval = null;
var key = _fsql.GetEntityKeyString(source);
EntityState state = new EntityState();
if (string.IsNullOrEmpty(key)) {
var ids = _table.Primarys.Where(a => a.Attribute.IsIdentity).ToArray();
switch (_ctx._orm.Ado.DataType) {
switch (_fsql.Ado.DataType) {
case DataType.SqlServer:
case DataType.PostgreSQL:
if (ids.Length == 1 && _table.Primarys.Length == 1) {
await _ctx.ExecCommandAsync();
if (_tableIdentitys.Length == 1 && _table.Primarys.Length == 1) {
_ctx.ExecCommand();
var idtval = await this.OrmInsert(source).ExecuteIdentityAsync();
_ctx._affrows++;
SetEntityIdentityValue(source, idtval);
_fsql.SetEntityIdentityValue(source, idtval);
} else {
await _ctx.ExecCommandAsync();
newval = (await this.OrmInsert(source).ExecuteInsertedAsync()).First();
_ctx.ExecCommand();
state.Value = (await this.OrmInsert(source).ExecuteInsertedAsync()).First();
_ctx._affrows++;
CopyNewValueToEntity(source, newval);
_fsql.CopyEntityValue(source, state.Value);
}
break;
case DataType.MySql:
case DataType.Oracle:
case DataType.Sqlite:
if (ids.Length == 1 && _table.Primarys.Length == 1) {
await _ctx.ExecCommandAsync();
if (_tableIdentitys.Length == 1 && _table.Primarys.Length == 1) {
_ctx.ExecCommand();
var idtval = await this.OrmInsert(source).ExecuteIdentityAsync();
_ctx._affrows++;
SetEntityIdentityValue(source, idtval);
_fsql.SetEntityIdentityValue(source, idtval);
} else {
throw new Exception("DbSet.Add 失败,由于实体没有主键值或者没有配置自增或者自增列数不为1");
throw new Exception($"DbSet.Add 失败,未设置主键或者没有配置自增或者自增列数不为1{_fsql.GetEntityString(source)}");
}
break;
}
key = GetEntityKeyString(source);
state.Key = key = _fsql.GetEntityKeyString(source);
state.Time = DateTime.Now;
} else {
if (_vals.ContainsKey(key))
throw new Exception("DbSet.Add 失败,实体数据已存在,请勿重复添加");
_ctx.EnqueueAction(DbContext.ExecCommandInfoType.Insert, _entityType, this, source);
throw new Exception($"DbSet.Add 失败,实体数据已存在,请勿重复添加{_fsql.GetEntityString(source)}");
_ctx.EnqueueAction(DbContext.ExecCommandInfoType.Insert, this, typeof(EntityState), state);
}
if (newval == null) {
newval = Activator.CreateInstance<TEntity>();
CopyNewValueToEntity(newval, source);
if (state.Value == null) {
state.Value = Activator.CreateInstance<TEntity>();
_fsql.CopyEntityValue(state.Value, source); //copy, 记录旧值版本
}
_vals.Add(key, newval);
_vals.Add(key, state);
}
async public Task AddRangeAsync(TEntity[] source) {
if (source == null) throw new ArgumentNullException(nameof(source));
@ -73,56 +78,47 @@ namespace FreeSql {
await AddAsync(item);
}
async Task<int> DbContextBetchUpdateAsync(TEntity[] ups, bool isLiveUpdate) {
Task<int> DbContextBetchUpdateAsync(EntityState[] ups) => DbContextBetchUpdatePrivAsync(ups, false);
Task<int> DbContextBetchUpdateNowAsync(EntityState[] ups) => DbContextBetchUpdatePrivAsync(ups, true);
async Task<int> DbContextBetchUpdatePrivAsync(EntityState[] ups, bool isLiveUpdate) {
if (ups.Any() == false) return 0;
var uplst1 = ups[ups.Length - 1];
var uplst2 = ups.Length > 1 ? ups[ups.Length - 2] : null;
var lstkey1 = GetEntityKeyString(uplst1);
if (_vals.TryGetValue(lstkey1, out var lstval1) == false) throw new Exception("DbSet.Update 失败,实体应该先查询再修改。");
TEntity lstval2 = default(TEntity);
if (uplst2 != null) {
var lstkey2 = GetEntityKeyString(uplst2);
if (_vals.TryGetValue(lstkey2, out lstval2) == false) throw new Exception("DbSet.Update 失败,实体应该先查询再修改。");
}
if (_vals.TryGetValue(uplst1.Key, out var lstval1) == false) return -999;
var lstval2 = default(EntityState);
if (uplst2 != null && _vals.TryGetValue(uplst2.Key, out lstval2) == false) throw new Exception($"DbSet.Update 失败,实体应该先查询再修改:{_fsql.GetEntityString(uplst2.Value)}");
var cuig1 = CompareUpdateIngoreColumns(uplst1, lstval1);
var cuig2 = uplst2 != null ? CompareUpdateIngoreColumns(uplst2, lstval2) : null;
if (uplst2 != null && string.Compare(cuig1, cuig2, true) != 0) {
var cuig1 = _fsql.CompareEntityValueReturnColumns(uplst1.Value, lstval1.Value, true);
var cuig2 = uplst2 != null ? _fsql.CompareEntityValueReturnColumns(uplst2.Value, lstval2.Value, true) : null;
if (uplst2 != null && string.Compare(string.Join(",", cuig1), string.Join(",", cuig2)) != 0) {
//最后一个不保存
var ignores = cuig2.Split(new[] { ", " }, StringSplitOptions.None);
var source = ups.ToList();
source.RemoveAt(ups.Length - 1);
var affrows = await this.OrmUpdate(null).SetSource(source).IgnoreColumns(ignores).ExecuteAffrowsAsync();
var affrows = await this.OrmUpdate(null).SetSource(source.Select(a => a.Value)).IgnoreColumns(cuig2).ExecuteAffrowsAsync();
foreach (var newval in source) {
var newkey = GetEntityKeyString(newval);
if (_vals.TryGetValue(newkey, out var tryold))
CopyNewValueToEntity(tryold, newval);
if (_vals.TryGetValue(newval.Key, out var tryold))
_fsql.CopyEntityValue(tryold.Value, newval.Value);
}
return affrows;
} else if (isLiveUpdate) {
//立即保存
var ignores = cuig1.Split(new[] { ", " }, StringSplitOptions.None);
var affrows = await this.OrmUpdate(null).SetSource(ups).IgnoreColumns(ignores).ExecuteAffrowsAsync();
foreach (var newval in ups) {
var newkey = GetEntityKeyString(newval);
if (_vals.TryGetValue(newkey, out var tryold))
CopyNewValueToEntity(tryold, newval);
var source = ups;
var affrows = await this.OrmUpdate(null).SetSource(source.Select(a => a.Value)).IgnoreColumns(cuig1).ExecuteAffrowsAsync();
foreach (var newval in source) {
if (_vals.TryGetValue(newval.Key, out var tryold))
_fsql.CopyEntityValue(tryold.Value, newval.Value);
}
return Math.Min(ups.Length, affrows);
}
//等待下次对比再保存
return 0;
}
async Task<int> DbContextBetchRemoveAsync(TEntity[] dels) {
async Task<int> DbContextBetchRemoveAsync(EntityState[] dels) {
if (dels.Any() == false) return 0;
var affrows = await this.OrmDelete(dels).ExecuteAffrowsAsync();
foreach (var del in dels) {
var key = GetEntityKeyString(del);
_vals.Remove(key);
}
var affrows = await this.OrmDelete(dels.Select(a => a.Value)).ExecuteAffrowsAsync();
//foreach (var del in dels)
// _vals.Remove(del.Key);
return affrows;
}
}

View File

@ -0,0 +1,278 @@
using FreeSql.Internal.Model;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
namespace FreeSql.Extensions {
public static class EntityUtilFreeSqlExtensions {
static MethodInfo MethodStringBuilderAppend = typeof(StringBuilder).GetMethod("Append", new Type[] { typeof(object) });
static MethodInfo MethodStringBuilderToString = typeof(StringBuilder).GetMethod("ToString", new Type[0]);
static PropertyInfo MethodStringBuilderLength = typeof(StringBuilder).GetProperty("Length");
static MethodInfo MethodStringConcat = typeof(string).GetMethod("Concat", new Type[] { typeof(object) });
static ConcurrentDictionary<DataType, ConcurrentDictionary<Type, Func<object, string>>> _dicGetEntityKeyString = new ConcurrentDictionary<DataType, ConcurrentDictionary<Type, Func<object, string>>>();
/// <summary>
/// 获取实体的主键值,以 "*|_,[,_|*" 分割
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <param name="_table"></param>
/// <param name="item"></param>
/// <returns></returns>
public static string GetEntityKeyString<TEntity>(this IFreeSql orm, TEntity item, string splitString = "*|_,[,_|*") {
var func = _dicGetEntityKeyString.GetOrAdd(orm.Ado.DataType, dt => new ConcurrentDictionary<Type, Func<object, string>>()).GetOrAdd(typeof(TEntity), t => {
var _table = orm.CodeFirst.GetTableByEntity(t);
var pks = _table.Primarys;
var returnTarget = Expression.Label(typeof(string));
var parm1 = Expression.Parameter(typeof(object));
var var1Parm = Expression.Variable(t);
var var2Sb = Expression.Variable(typeof(StringBuilder));
var var3IsNull = Expression.Variable(typeof(bool));
var exps = new List<Expression>(new Expression[] {
Expression.Assign(var1Parm, Expression.TypeAs(parm1, t)),
Expression.Assign(var2Sb, Expression.New(typeof(StringBuilder))),
Expression.Assign(var3IsNull, Expression.Constant(false))
});
for (var a = 0; a < pks.Length; a++) {
exps.Add(
Expression.IfThen(
Expression.Equal(var3IsNull, Expression.Constant(false)),
Expression.IfThenElse(
Expression.Equal(Expression.MakeMemberAccess(var1Parm, _table.Properties[pks[a].CsName]), Expression.Default(pks[a].CsType)),
Expression.Assign(var3IsNull, Expression.Constant(true)),
Expression.Block(
new Expression[]{
a > 0 ? Expression.Call(var2Sb, MethodStringBuilderAppend, Expression.Constant(splitString)) : null,
Expression.Call(var2Sb, MethodStringBuilderAppend,
Expression.Convert(Expression.MakeMemberAccess(var1Parm, _table.Properties[pks[a].CsName]), typeof(object))
)
}.Where(c => c != null).ToArray()
)
)
)
);
}
exps.Add(
Expression.IfThen(
Expression.Equal(var3IsNull, Expression.Constant(false)),
Expression.Return(returnTarget, Expression.Call(var2Sb, MethodStringBuilderToString))
)
);
exps.Add(Expression.Label(returnTarget, Expression.Default(typeof(string))));
return Expression.Lambda<Func<object, string>>(Expression.Block(new[] { var1Parm, var2Sb, var3IsNull }, exps), new[] { parm1 }).Compile();
});
return func(item);
}
static ConcurrentDictionary<DataType, ConcurrentDictionary<Type, Func<object, string>>> _dicGetEntityString = new ConcurrentDictionary<DataType, ConcurrentDictionary<Type, Func<object, string>>>();
/// <summary>
/// 获取实体的所有数据,以 (1, 2, xxx) 的形式
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <param name="_table"></param>
/// <param name="item"></param>
/// <returns></returns>
public static string GetEntityString<TEntity>(this IFreeSql orm, TEntity item) {
var func = _dicGetEntityString.GetOrAdd(orm.Ado.DataType, dt => new ConcurrentDictionary<Type, Func<object, string>>()).GetOrAdd(typeof(TEntity), t => {
var _table = orm.CodeFirst.GetTableByEntity(t);
var cols = _table.Columns;
var returnTarget = Expression.Label(typeof(string));
var parm1 = Expression.Parameter(typeof(object));
var var1Parm = Expression.Variable(t);
var var2Sb = Expression.Variable(typeof(StringBuilder));
var var3IsNull = Expression.Variable(typeof(bool));
var exps = new List<Expression>(new Expression[] {
Expression.Assign(var1Parm, Expression.TypeAs(parm1, t)),
Expression.Assign(var2Sb, Expression.New(typeof(StringBuilder))),
Expression.Assign(var3IsNull, Expression.Constant(false)),
Expression.Call(var2Sb, MethodStringBuilderAppend, Expression.Constant("(" ))
});
var a = 0;
foreach (var col in cols.Values) {
exps.Add(
Expression.IfThen(
Expression.Equal(var3IsNull, Expression.Constant(false)),
Expression.IfThenElse(
Expression.Equal(Expression.MakeMemberAccess(var1Parm, _table.Properties[col.CsName]), Expression.Default(col.CsType)),
Expression.Assign(var3IsNull, Expression.Constant(true)),
Expression.Block(
new Expression[]{
a > 0 ? Expression.Call(var2Sb, MethodStringBuilderAppend, Expression.Constant(", " )) : null,
Expression.Call(var2Sb, MethodStringBuilderAppend,
Expression.Convert(Expression.MakeMemberAccess(var1Parm, _table.Properties[col.CsName]), typeof(object))
)
}.Where(c => c != null).ToArray()
)
)
)
);
a++;
}
exps.AddRange(new Expression[] {
Expression.Call(var2Sb, MethodStringBuilderAppend, Expression.Constant(")" )),
Expression.IfThen(
Expression.Equal(var3IsNull, Expression.Constant(false)),
Expression.Return(returnTarget, Expression.Call(var2Sb, MethodStringBuilderToString))
)
});
exps.Add(Expression.Label(returnTarget, Expression.Default(typeof(string))));
return Expression.Lambda<Func<object, string>>(Expression.Block(new[] { var1Parm, var2Sb, var3IsNull }, exps), new[] { parm1 }).Compile();
});
return func(item);
}
/// <summary>
/// 使用新实体的值,复盖旧实体的值
/// </summary>
static ConcurrentDictionary<DataType, ConcurrentDictionary<Type, Action<object, object>>> _dicCopyNewValueToEntity = new ConcurrentDictionary<DataType, ConcurrentDictionary<Type, Action<object, object>>>();
public static void CopyEntityValue<TEntity>(this IFreeSql orm, TEntity oldValue, TEntity newValue) {
var func = _dicCopyNewValueToEntity.GetOrAdd(orm.Ado.DataType, dt => new ConcurrentDictionary<Type, Action<object, object>>()).GetOrAdd(typeof(TEntity), t => {
var _table = orm.CodeFirst.GetTableByEntity(t);
var parm1 = Expression.Parameter(typeof(object));
var parm2 = Expression.Parameter(typeof(object));
var var1Parm = Expression.Variable(t);
var var2Parm = Expression.Variable(t);
var exps = new List<Expression>(new Expression[] {
Expression.Assign(var1Parm, Expression.TypeAs(parm1, t)),
Expression.Assign(var2Parm, Expression.TypeAs(parm2, t))
});
foreach (var prop in _table.Properties.Values) {
if (_table.ColumnsByCs.ContainsKey(prop.Name)) {
exps.Add(
Expression.Assign(
Expression.MakeMemberAccess(var1Parm, prop),
Expression.MakeMemberAccess(var2Parm, prop)
)
);
} else {
exps.Add(
Expression.Assign(
Expression.MakeMemberAccess(var1Parm, prop),
Expression.Default(prop.PropertyType)
)
);
}
}
return Expression.Lambda<Action<object, object>>(Expression.Block(new[] { var1Parm, var2Parm }, exps), new[] { parm1, parm2 }).Compile();
});
func(oldValue, newValue);
}
static ConcurrentDictionary<DataType, ConcurrentDictionary<Type, Action<object, long>>> _dicSetEntityIdentityValue = new ConcurrentDictionary<DataType, ConcurrentDictionary<Type, Action<object, long>>>();
/// <summary>
/// 设置实体的自增字段值(若存在)
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <param name="orm"></param>
/// <param name="item"></param>
/// <param name="idtval"></param>
public static void SetEntityIdentityValue<TEntity>(this IFreeSql orm, TEntity item, long idtval) {
var func = _dicSetEntityIdentityValue.GetOrAdd(orm.Ado.DataType, dt => new ConcurrentDictionary<Type, Action<object, long>>()).GetOrAdd(typeof(TEntity), t => {
var _table = orm.CodeFirst.GetTableByEntity(t);
var identitys = _table.Primarys.Where(a => a.Attribute.IsIdentity);
var parm1 = Expression.Parameter(typeof(object));
var parm2 = Expression.Parameter(typeof(long));
var var1Parm = Expression.Variable(t);
var exps = new List<Expression>(new Expression[] {
Expression.Assign(var1Parm, Expression.TypeAs(parm1, t))
});
foreach (var pk in identitys) {
exps.Add(
Expression.Assign(
Expression.MakeMemberAccess(var1Parm, _table.Properties[pk.CsName]),
Expression.Convert(FreeSql.Internal.Utils.GetDataReaderValueBlockExpression(pk.CsType, Expression.Convert(parm2, typeof(object))), pk.CsType)
)
);
}
return Expression.Lambda<Action<object, long>>(Expression.Block(new[] { var1Parm }, exps), new[] { parm1, parm2 }).Compile();
});
func(item, idtval);
}
static ConcurrentDictionary<DataType, ConcurrentDictionary<Type, Action<object>>> _dicClearEntityPrimaryValueWithIdentityAndGuid = new ConcurrentDictionary<DataType, ConcurrentDictionary<Type, Action<object>>>();
/// <summary>
/// 清除实体的主键值将自增、Guid类型的主键值清除
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <param name="orm"></param>
/// <param name="item"></param>
public static void ClearEntityPrimaryValueWithIdentityAndGuid<TEntity>(this IFreeSql orm, TEntity item) {
var func = _dicClearEntityPrimaryValueWithIdentityAndGuid.GetOrAdd(orm.Ado.DataType, dt => new ConcurrentDictionary<Type, Action<object>>()).GetOrAdd(typeof(TEntity), t => {
var _table = orm.CodeFirst.GetTableByEntity(t);
var identitys = _table.Primarys.Where(a => a.Attribute.IsIdentity);
var parm1 = Expression.Parameter(typeof(object));
var var1Parm = Expression.Variable(t);
var exps = new List<Expression>(new Expression[] {
Expression.Assign(var1Parm, Expression.TypeAs(parm1, t))
});
foreach (var pk in _table.Primarys) {
if (pk.CsType == typeof(Guid) || pk.CsType == typeof(Guid?) ||
pk.Attribute.IsIdentity) {
exps.Add(
Expression.Assign(
Expression.MakeMemberAccess(var1Parm, _table.Properties[pk.CsName]),
Expression.Default(pk.CsType)
)
);
}
}
return Expression.Lambda<Action<object>>(Expression.Block(new[] { var1Parm }, exps), new[] { parm1 }).Compile();
});
func(item);
}
static ConcurrentDictionary<DataType, ConcurrentDictionary<Type, Func<object, object, bool, string[]>>> _dicCompareEntityValueReturnColumns = new ConcurrentDictionary<DataType, ConcurrentDictionary<Type, Func<object, object, bool, string[]>>>();
/// <summary>
/// 对比两个实体值,返回相同/或不相同的列名
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <param name="orm"></param>
/// <param name="up"></param>
/// <param name="oldval"></param>
/// <returns></returns>
public static string[] CompareEntityValueReturnColumns<TEntity>(this IFreeSql orm, TEntity up, TEntity oldval, bool isEqual) {
var func = _dicCompareEntityValueReturnColumns.GetOrAdd(orm.Ado.DataType, dt => new ConcurrentDictionary<Type, Func<object, object, bool, string[]>>()).GetOrAdd(typeof(TEntity), t => {
var _table = orm.CodeFirst.GetTableByEntity(t);
var returnTarget = Expression.Label(typeof(string[]));
var parm1 = Expression.Parameter(typeof(object));
var parm2 = Expression.Parameter(typeof(object));
var parm3 = Expression.Parameter(typeof(bool));
var var1Ret = Expression.Variable(typeof(List<string>));
var var1Parm = Expression.Variable(t);
var var2Parm = Expression.Variable(t);
var exps = new List<Expression>(new Expression[] {
Expression.Assign(var1Parm, Expression.TypeAs(parm1, t)),
Expression.Assign(var2Parm, Expression.TypeAs(parm2, t)),
Expression.Assign(var1Ret, Expression.New(typeof(List<string>)))
});
var a = 0;
foreach (var prop in _table.Properties.Values) {
if (_table.ColumnsByCs.TryGetValue(prop.Name, out var trycol) == false) continue;
exps.Add(
Expression.IfThenElse(
Expression.Equal(
Expression.MakeMemberAccess(var1Parm, prop),
Expression.MakeMemberAccess(var2Parm, prop)
),
Expression.IfThen(
Expression.IsTrue(parm3),
Expression.Call(var1Ret, typeof(List<string>).GetMethod("Add", new Type[] { typeof(string) }), Expression.Constant(trycol.Attribute.Name))
),
Expression.IfThen(
Expression.IsFalse(parm3),
Expression.Call(var1Ret, typeof(List<string>).GetMethod("Add", new Type[] { typeof(string) }), Expression.Constant(trycol.Attribute.Name))
)
)
);
a++;
}
exps.Add(Expression.Return(returnTarget, Expression.Call(var1Ret, typeof(List<string>).GetMethod("ToArray", new Type[0]))));
exps.Add(Expression.Label(returnTarget, Expression.Constant(new string[0])));
return Expression.Lambda<Func<object, object, bool, string[]>>(Expression.Block(new[] { var1Ret, var1Parm, var2Parm }, exps), new[] { parm1, parm2, parm3 }).Compile();
});
return func(up, oldval, isEqual);
}
}
}

View File

@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>0.3.21</Version>
<Version>0.3.22</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>YeXiangQin</Authors>
<Description>FreeSql is the most convenient ORM in dotnet. It supports Mysql, Postgresql, SqlServer, Oracle and Sqlite.</Description>