## v0.3.21

- 增加 IUpdate IgnoreColumns 重载方法,支持传入字符串数组忽略修改;
- 完善 FreeSql.DbContext,支持对象操作 + SaveChanges 最后保存操作;
This commit is contained in:
28810
2019-03-21 05:24:50 +08:00
parent d9de8e986b
commit c20a0bbd54
24 changed files with 815 additions and 48 deletions

View File

@ -5,9 +5,10 @@ using System.Collections.Concurrent;
using System.Data.Common;
using System.Linq;
using System.Reflection;
using System.Linq.Expressions;
namespace FreeSql {
public abstract class DbContext : IDisposable {
public abstract partial class DbContext : IDisposable {
internal IFreeSql _orm;
internal IFreeSql _fsql => _orm ?? throw new ArgumentNullException("请在 OnConfiguring 或 AddFreeDbContext 中配置 UseFreeSql");
@ -44,8 +45,143 @@ namespace FreeSql {
protected Dictionary<PropertyInfo, object> AllSets => new Dictionary<PropertyInfo, object>();
public void SaveChanges() {
public long SaveChanges() {
ExecCommand();
Commit();
return _affrows;
}
internal class ExecCommandInfo {
public ExecCommandInfoType actionType { get; set; }
public Type entityType { get; set; }
public object dbSet { 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 });
}
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>>();
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]);
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.Call(Expression.Convert(parm1DbSet, dbsetType), dbsetTypeInsert, var1Vals),
insertExecuteAffrows
)
),
Expression.Label(returnTarget, Expression.Default(typeof(int)))
), new[] { parm1DbSet, parm2Vals }).Compile();
});
_affrows += insertFunc(oldinfo.dbSet, states.ToArray());
states.Clear();
};
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());
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);
if (affrows > 0) {
_affrows += affrows;
var islastNotUpdated = states.Count != affrows;
states.Clear();
if (islastNotUpdated) states.Add(oldinfo.state);
}
};
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) {
if (info != null && oldinfo.actionType == info.actionType && oldinfo.entityType == info.entityType) {
//最后一个,合起来发送
states.Add(info.state);
info = null;
}
switch (oldinfo.actionType) {
case ExecCommandInfoType.Insert:
funcInsert();
break;
case ExecCommandInfoType.Delete:
funcDelete();
break;
}
isLiveUpdate = true;
}
if (isLiveUpdate || oldinfo.actionType == ExecCommandInfoType.Update) {
if (states.Any())
funcUpdate(isLiveUpdate);
}
if (info != null) {
states.Add(info.state);
oldinfo = info;
}
}
}
void ReturnObject() {

View File

@ -0,0 +1,139 @@
using SafeObjectPool;
using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Data.Common;
using System.Linq;
using System.Reflection;
using System.Linq.Expressions;
using System.Threading.Tasks;
namespace FreeSql {
partial class DbContext {
async public Task<long> SaveChangesAsync() {
await ExecCommandAsync();
Commit();
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>>>();
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]);
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.Call(Expression.Convert(parm1DbSet, dbsetType), dbsetTypeInsert, var1Vals),
insertExecuteAffrows
)
),
Expression.Label(returnTarget, Expression.Default(typeof(Task<int>)))
), new[] { parm1DbSet, parm2Vals }).Compile();
});
_affrows += await insertFunc(oldinfo.dbSet, states.ToArray());
states.Clear();
};
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());
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);
if (affrows > 0) {
_affrows += affrows;
var islastNotUpdated = states.Count != affrows;
states.Clear();
if (islastNotUpdated) states.Add(oldinfo.state);
}
};
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) {
if (info != null && oldinfo.actionType == info.actionType && oldinfo.entityType == info.entityType) {
//最后一个,合起来发送
states.Add(info.state);
info = null;
}
switch (oldinfo.actionType) {
case ExecCommandInfoType.Insert:
await funcInsert();
break;
case ExecCommandInfoType.Delete:
await funcDelete();
break;
}
isLiveUpdate = true;
}
if (isLiveUpdate || oldinfo.actionType == ExecCommandInfoType.Update) {
if (states.Any())
await funcUpdate(isLiveUpdate);
}
if (info != null) {
states.Add(info.state);
oldinfo = info;
}
}
}
}
}

View File

@ -1,58 +1,328 @@
using System;
using FreeSql.Internal.Model;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
namespace FreeSql {
public abstract class DbSet<TEntity> where TEntity : class {
public abstract partial class DbSet<TEntity> where TEntity : class {
protected DbContext _ctx;
public ISelect<TEntity> Select => _ctx._fsql.Select<TEntity>().WithTransaction(_ctx.GetOrBeginTransaction(false));
protected ISelect<TEntity> OrmSelect(object dywhere) => _ctx._fsql.Select<TEntity>(dywhere).WithTransaction(_ctx.GetOrBeginTransaction(false));
public IInsert<TEntity> Insert(TEntity source) => _ctx._fsql.Insert<TEntity>(source).WithTransaction(_ctx.GetOrBeginTransaction());
public IInsert<TEntity> Insert(TEntity[] source) => _ctx._fsql.Insert<TEntity>(source).WithTransaction(_ctx.GetOrBeginTransaction());
public IInsert<TEntity> Insert(IEnumerable<TEntity> source) => _ctx._fsql.Insert<TEntity>(source).WithTransaction(_ctx.GetOrBeginTransaction());
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());
public IUpdate<TEntity> Update => _ctx._fsql.Update<TEntity>().WithTransaction(_ctx.GetOrBeginTransaction());
public IDelete<TEntity> Delete => _ctx._fsql.Delete<TEntity>().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 Dictionary<string, TEntity> _vals = new Dictionary<string, TEntity>();
//protected tableinfo
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);
//public void Add(TEntity source) {
protected Dictionary<string, TEntity> _vals = new Dictionary<string, TEntity>();
TableInfo _tablePriv;
protected TableInfo _table => _tablePriv ?? (_tablePriv = _ctx._orm.CodeFirst.GetTableByEntity(_entityType));
protected Type _entityType = typeof(TEntity);
//}
//public void AddRange(TEntity[] source) {
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>();
//}
//public void AddRange(IEnumerable<TEntity> source) {
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 void Update(TEntity source) {
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);
}
//}
//public void UpdateRange(TEntity[] source) {
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 UpdateRange(IEnumerable<TEntity> source) {
public void Add(TEntity source) {
if (source == null) throw new ArgumentNullException(nameof(source));
var key = GetEntityKeyString(source);
TEntity newval = null;
if (string.IsNullOrEmpty(key)) {
var ids = _table.Primarys.Where(a => a.Attribute.IsIdentity).ToArray();
//}
//public void Remove(TEntity source) {
switch(_ctx._orm.Ado.DataType) {
case DataType.SqlServer:
case DataType.PostgreSQL:
if (ids.Length == 1 && _table.Primarys.Length == 1) {
_ctx.ExecCommand();
var idtval = this.OrmInsert(source).ExecuteIdentity();
_ctx._affrows++;
SetEntityIdentityValue(source, idtval);
} else {
_ctx.ExecCommand();
newval = this.OrmInsert(source).ExecuteInserted().First();
_ctx._affrows++;
CopyNewValueToEntity(source, newval);
}
break;
case DataType.MySql:
case DataType.Oracle:
case DataType.Sqlite:
if (ids.Length == 1 && _table.Primarys.Length == 1) {
_ctx.ExecCommand();
var idtval = this.OrmInsert(source).ExecuteIdentity();
_ctx._affrows++;
SetEntityIdentityValue(source, idtval);
} else {
throw new Exception("DbSet.Add 失败由于实体没有主键值或者没有配置自增或者自增列数不为1。");
}
break;
}
//}
//public void RemoveRange(TEntity[] source) {
key = GetEntityKeyString(source);
} else {
if (_vals.ContainsKey(key))
throw new Exception("DbSet.Add 失败,实体数据已存在,请勿重复添加。");
_ctx.EnqueueAction(DbContext.ExecCommandInfoType.Insert, _entityType, this, source);
}
if (newval == null) {
newval = Activator.CreateInstance<TEntity>();
CopyNewValueToEntity(newval, source);
}
_vals.Add(key, newval);
}
public void AddRange(TEntity[] source) {
if (source == null) throw new ArgumentNullException(nameof(source));
for (var a = 0; a < source.Length; a++)
Add(source[a]);
}
public void AddRange(IEnumerable<TEntity> source) {
if (source == null) throw new ArgumentNullException(nameof(source));
foreach(var item in source)
Add(item);
}
//}
//public void RemoveRange(IEnumerable<TEntity> source) {
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) {
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 失败,实体应该先查询再修改。");
}
var cuig1 = CompareUpdateIngoreColumns(uplst1, lstval1);
var cuig2 = uplst2 != null ? CompareUpdateIngoreColumns(uplst2, lstval2) : null;
if (uplst2 != null && string.Compare(cuig1, cuig2, true) != 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);
}
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);
}
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 失败,实体没有设置主键值。");
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);
}
public void UpdateRange(TEntity[] source) {
if (source == null) throw new ArgumentNullException(nameof(source));
for (var a = 0; a < source.Length; a++)
Update(source[a]);
}
public void UpdateRange(IEnumerable<TEntity> source) {
if (source == null) throw new ArgumentNullException(nameof(source));
foreach (var item in source)
Update(item);
}
int DbContextBetchRemove(TEntity[] 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);
}
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 失败,实体没有设置主键值。");
var snap = Activator.CreateInstance<TEntity>();
CopyNewValueToEntity(snap, source);
if (_vals.TryGetValue(key, out var val) == false) _vals.Add(key, snap);
_ctx.EnqueueAction(DbContext.ExecCommandInfoType.Delete, _entityType, this, snap);
}
public void RemoveRange(TEntity[] source) {
if (source == null) throw new ArgumentNullException(nameof(source));
for (var a = 0; a < source.Length; a++)
Remove(source[a]);
}
public void RemoveRange(IEnumerable<TEntity> source) {
if (source == null) throw new ArgumentNullException(nameof(source));
foreach (var item in source)
Remove(item);
}
}
internal class BaseDbSet<TEntity> : DbSet<TEntity> where TEntity : class {

View File

@ -0,0 +1,129 @@
using FreeSql.Internal.Model;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
namespace FreeSql {
partial class DbSet<TEntity> {
async public Task AddAsync(TEntity source) {
if (source == null) throw new ArgumentNullException(nameof(source));
var key = GetEntityKeyString(source);
TEntity newval = null;
if (string.IsNullOrEmpty(key)) {
var ids = _table.Primarys.Where(a => a.Attribute.IsIdentity).ToArray();
switch (_ctx._orm.Ado.DataType) {
case DataType.SqlServer:
case DataType.PostgreSQL:
if (ids.Length == 1 && _table.Primarys.Length == 1) {
await _ctx.ExecCommandAsync();
var idtval = await this.OrmInsert(source).ExecuteIdentityAsync();
_ctx._affrows++;
SetEntityIdentityValue(source, idtval);
} else {
await _ctx.ExecCommandAsync();
newval = (await this.OrmInsert(source).ExecuteInsertedAsync()).First();
_ctx._affrows++;
CopyNewValueToEntity(source, newval);
}
break;
case DataType.MySql:
case DataType.Oracle:
case DataType.Sqlite:
if (ids.Length == 1 && _table.Primarys.Length == 1) {
await _ctx.ExecCommandAsync();
var idtval = await this.OrmInsert(source).ExecuteIdentityAsync();
_ctx._affrows++;
SetEntityIdentityValue(source, idtval);
} else {
throw new Exception("DbSet.Add 失败由于实体没有主键值或者没有配置自增或者自增列数不为1。");
}
break;
}
key = GetEntityKeyString(source);
} else {
if (_vals.ContainsKey(key))
throw new Exception("DbSet.Add 失败,实体数据已存在,请勿重复添加。");
_ctx.EnqueueAction(DbContext.ExecCommandInfoType.Insert, _entityType, this, source);
}
if (newval == null) {
newval = Activator.CreateInstance<TEntity>();
CopyNewValueToEntity(newval, source);
}
_vals.Add(key, newval);
}
async public Task AddRangeAsync(TEntity[] source) {
if (source == null) throw new ArgumentNullException(nameof(source));
for (var a = 0; a < source.Length; a++)
await AddAsync(source[a]);
}
async public Task AddRangeAsync(IEnumerable<TEntity> source) {
if (source == null) throw new ArgumentNullException(nameof(source));
foreach (var item in source)
await AddAsync(item);
}
async Task<int> DbContextBetchUpdateAsync(TEntity[] 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 失败,实体应该先查询再修改。");
}
var cuig1 = CompareUpdateIngoreColumns(uplst1, lstval1);
var cuig2 = uplst2 != null ? CompareUpdateIngoreColumns(uplst2, lstval2) : null;
if (uplst2 != null && string.Compare(cuig1, cuig2, true) != 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();
foreach (var newval in source) {
var newkey = GetEntityKeyString(newval);
if (_vals.TryGetValue(newkey, out var tryold))
CopyNewValueToEntity(tryold, newval);
}
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);
}
return Math.Min(ups.Length, affrows);
}
//等待下次对比再保存
return 0;
}
async Task<int> DbContextBetchRemoveAsync(TEntity[] 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);
}
return affrows;
}
}
}

View File

@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>0.3.20</Version>
<Version>0.3.21</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>