mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-06-18 20:08:15 +08:00
FreeSql.DbContext 内部仓储融合
This commit is contained in:
@ -6,6 +6,13 @@ using System.Linq;
|
||||
namespace FreeSql {
|
||||
partial class DbSet<TEntity> {
|
||||
|
||||
void DbContextExecCommand() {
|
||||
if (IsNoneDbContext == false) {
|
||||
_dicUpdateTimes.Clear();
|
||||
_ctx.ExecCommand();
|
||||
}
|
||||
}
|
||||
|
||||
int DbContextBetchAdd(EntityState[] adds) {
|
||||
if (adds.Any() == false) return 0;
|
||||
var affrows = this.OrmInsert(adds.Select(a => a.Value)).ExecuteAffrows();
|
||||
@ -21,14 +28,14 @@ namespace FreeSql {
|
||||
case DataType.SqlServer:
|
||||
case DataType.PostgreSQL:
|
||||
if (_tableIdentitys.Length == 1 && _table.Primarys.Length == 1) {
|
||||
ExecuteCommand();
|
||||
DbContextExecCommand();
|
||||
var idtval = this.OrmInsert(data).ExecuteIdentity();
|
||||
IncrAffrows(1);
|
||||
_fsql.SetEntityIdentityValueWithPrimary(data, idtval);
|
||||
var state = CreateEntityState(data);
|
||||
_states.Add(state.Key, state);
|
||||
} else {
|
||||
ExecuteCommand();
|
||||
DbContextExecCommand();
|
||||
var newval = this.OrmInsert(data).ExecuteInserted().First();
|
||||
IncrAffrows(1);
|
||||
_fsql.MapEntityValue(newval, data);
|
||||
@ -40,7 +47,7 @@ namespace FreeSql {
|
||||
case DataType.Oracle:
|
||||
case DataType.Sqlite:
|
||||
if (_tableIdentitys.Length == 1 && _table.Primarys.Length == 1) {
|
||||
ExecuteCommand();
|
||||
DbContextExecCommand();
|
||||
var idtval = this.OrmInsert(data).ExecuteIdentity();
|
||||
IncrAffrows(1);
|
||||
_fsql.SetEntityIdentityValueWithPrimary(data, idtval);
|
||||
@ -50,47 +57,13 @@ namespace FreeSql {
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
//进入队列,等待 SaveChanges 时执行
|
||||
EnqueueAction(DbContext.ExecCommandInfoType.Insert, this, typeof(EntityState), CreateEntityState(data));
|
||||
if (IsNoneDbContext)
|
||||
IncrAffrows(OrmInsert(data).ExecuteAffrows());
|
||||
else
|
||||
EnqueueToDbContext(DbContext.ExecCommandInfoType.Insert, CreateEntityState(data));
|
||||
}
|
||||
}
|
||||
public void Add(TEntity data) => AddPriv(data, true);
|
||||
#endregion
|
||||
|
||||
#region AddRange
|
||||
public void AddRange(TEntity[] data) {
|
||||
if (CanAdd(data, true) == false) return;
|
||||
if (data.Length == 1) {
|
||||
Add(data.First());
|
||||
return;
|
||||
}
|
||||
if (_tableIdentitys.Length > 0) {
|
||||
//有自增,马上执行
|
||||
switch (_fsql.Ado.DataType) {
|
||||
case DataType.SqlServer:
|
||||
case DataType.PostgreSQL:
|
||||
ExecuteCommand();
|
||||
var rets = this.OrmInsert(data).ExecuteInserted();
|
||||
if (rets.Count != data.Count()) throw new Exception($"特别错误:批量添加失败,{_fsql.Ado.DataType} 的返回数据,与添加的数目不匹配");
|
||||
var idx = 0;
|
||||
foreach (var s in data)
|
||||
_fsql.MapEntityValue(rets[idx++], s);
|
||||
IncrAffrows(rets.Count);
|
||||
TrackToList(rets);
|
||||
return;
|
||||
case DataType.MySql:
|
||||
case DataType.Oracle:
|
||||
case DataType.Sqlite:
|
||||
foreach (var s in data)
|
||||
AddPriv(s, false);
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
//进入队列,等待 SaveChanges 时执行
|
||||
foreach (var s in data)
|
||||
EnqueueAction(DbContext.ExecCommandInfoType.Insert, this, typeof(EntityState), CreateEntityState(s));
|
||||
}
|
||||
}
|
||||
public void AddRange(IEnumerable<TEntity> data) {
|
||||
if (CanAdd(data, true) == false) return;
|
||||
if (data.ElementAtOrDefault(1) == default(TEntity)) {
|
||||
@ -102,11 +75,11 @@ namespace FreeSql {
|
||||
switch (_fsql.Ado.DataType) {
|
||||
case DataType.SqlServer:
|
||||
case DataType.PostgreSQL:
|
||||
ExecuteCommand();
|
||||
DbContextExecCommand();
|
||||
var rets = this.OrmInsert(data).ExecuteInserted();
|
||||
if (rets.Count != data.Count()) throw new Exception($"特别错误:批量添加失败,{_fsql.Ado.DataType} 的返回数据,与添加的数目不匹配");
|
||||
var idx = 0;
|
||||
foreach(var s in data)
|
||||
foreach (var s in data)
|
||||
_fsql.MapEntityValue(rets[idx++], s);
|
||||
IncrAffrows(rets.Count);
|
||||
TrackToList(rets);
|
||||
@ -119,13 +92,17 @@ namespace FreeSql {
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
//进入队列,等待 SaveChanges 时执行
|
||||
foreach (var s in data)
|
||||
EnqueueAction(DbContext.ExecCommandInfoType.Insert, this, typeof(EntityState), CreateEntityState(s));
|
||||
if (IsNoneDbContext)
|
||||
IncrAffrows(OrmInsert(data).ExecuteAffrows());
|
||||
else
|
||||
//进入队列,等待 SaveChanges 时执行
|
||||
foreach (var s in data)
|
||||
EnqueueToDbContext(DbContext.ExecCommandInfoType.Insert, CreateEntityState(s));
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Update
|
||||
int DbContextBetchUpdate(EntityState[] ups) => DbContextBetchUpdatePriv(ups, false);
|
||||
int DbContextBetchUpdateNow(EntityState[] ups) => DbContextBetchUpdatePriv(ups, true);
|
||||
int DbContextBetchUpdatePriv(EntityState[] ups, bool isLiveUpdate) {
|
||||
@ -176,47 +153,86 @@ namespace FreeSql {
|
||||
return 0;
|
||||
}
|
||||
|
||||
void UpdatePriv(TEntity data, bool isCheck) {
|
||||
if (isCheck && CanUpdate(data, true) == false) return;
|
||||
var state = CreateEntityState(data);
|
||||
state.OldValue = data;
|
||||
EnqueueAction(DbContext.ExecCommandInfoType.Update, this, typeof(EntityState), state);
|
||||
}
|
||||
public void Update(TEntity data) => UpdatePriv(data, true);
|
||||
public void UpdateRange(TEntity[] data) {
|
||||
if (CanUpdate(data, true) == false) return;
|
||||
foreach (var item in data)
|
||||
UpdatePriv(item, false);
|
||||
}
|
||||
public void UpdateRange(IEnumerable<TEntity> data) {
|
||||
if (CanUpdate(data, true) == false) return;
|
||||
foreach (var item in data)
|
||||
UpdatePriv(item, false);
|
||||
}
|
||||
Dictionary<TEntity, byte> _dicUpdateTimes = new Dictionary<TEntity, byte>();
|
||||
internal int UpdateAffrows(TEntity data) => UpdateRangeAffrows(new[] { data });
|
||||
internal int UpdateRangeAffrows(IEnumerable<TEntity> data) {
|
||||
if (CanUpdate(data, true) == false) return 0;
|
||||
if (IsNoneDbContext) {
|
||||
var dataarray = data.ToArray();
|
||||
var ups = new List<EntityState>();
|
||||
var totalAffrows = 0;
|
||||
for (var a = 0; a < dataarray.Length + 1; a++) {
|
||||
var item = a < dataarray.Length ? dataarray[a] : null;
|
||||
if (item != null) {
|
||||
var state = CreateEntityState(item);
|
||||
state.Value = item;
|
||||
ups.Add(state);
|
||||
}
|
||||
|
||||
var affrows = DbContextBetchUpdatePriv(ups.ToArray(), item == null);
|
||||
if (affrows == -999) { //最后一个元素已被删除
|
||||
ups.RemoveAt(ups.Count - 1);
|
||||
continue;
|
||||
}
|
||||
if (affrows == -998 || affrows == -997) { //没有执行更新
|
||||
var laststate = ups[ups.Count - 1];
|
||||
ups.Clear();
|
||||
if (affrows == -997) ups.Add(laststate); //保留最后一个
|
||||
}
|
||||
if (affrows > 0) {
|
||||
totalAffrows += affrows;
|
||||
var islastNotUpdated = ups.Count != affrows;
|
||||
var laststate = ups[ups.Count - 1];
|
||||
ups.Clear();
|
||||
if (islastNotUpdated) ups.Add(laststate); //保留最后一个
|
||||
}
|
||||
}
|
||||
IncrAffrows(totalAffrows);
|
||||
return totalAffrows;
|
||||
}
|
||||
foreach (var item in data) {
|
||||
if (_dicUpdateTimes.ContainsKey(item))
|
||||
DbContextExecCommand();
|
||||
_dicUpdateTimes.Add(item, 1);
|
||||
|
||||
var state = CreateEntityState(item);
|
||||
state.OldValue = item;
|
||||
EnqueueToDbContext(DbContext.ExecCommandInfoType.Update, state);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
public void Update(TEntity data) => UpdateAffrows(data);
|
||||
public void UpdateRange(IEnumerable<TEntity> data) => UpdateRangeAffrows(data);
|
||||
#endregion
|
||||
|
||||
#region Remove
|
||||
int DbContextBetchRemove(EntityState[] dels) {
|
||||
if (dels.Any() == false) return 0;
|
||||
var affrows = this.OrmDelete(dels.Select(a => a.Value)).ExecuteAffrows();
|
||||
return Math.Max(dels.Length, affrows);
|
||||
}
|
||||
void RemovePriv(TEntity data, bool isCheck) {
|
||||
if (isCheck && CanRemove(data, true) == false) return;
|
||||
var state = CreateEntityState(data);
|
||||
EnqueueAction(DbContext.ExecCommandInfoType.Delete, this, typeof(EntityState), state);
|
||||
|
||||
if (_states.ContainsKey(state.Key)) _states.Remove(state.Key);
|
||||
_fsql.ClearEntityPrimaryValueWithIdentityAndGuid(data);
|
||||
}
|
||||
public void Remove(TEntity data) => RemovePriv(data, true);
|
||||
public void RemoveRange(TEntity[] data) {
|
||||
if (CanRemove(data, true) == false) return;
|
||||
foreach (var item in data)
|
||||
RemovePriv(item, false);
|
||||
}
|
||||
public void RemoveRange(IEnumerable<TEntity> data) {
|
||||
if (CanRemove(data, true) == false) return;
|
||||
foreach (var item in data)
|
||||
RemovePriv(item, false);
|
||||
internal int RemoveAffrows(TEntity data) => RemoveRangeAffrows(new[] { data });
|
||||
internal int RemoveRangeAffrows(IEnumerable<TEntity> data) {
|
||||
if (CanRemove(data, true) == false) return 0;
|
||||
var dels = new List<EntityState>();
|
||||
foreach (var item in data) {
|
||||
var state = CreateEntityState(item);
|
||||
if (_states.ContainsKey(state.Key)) _states.Remove(state.Key);
|
||||
_fsql.ClearEntityPrimaryValueWithIdentityAndGuid(item);
|
||||
|
||||
if (IsNoneDbContext) dels.Add(state);
|
||||
EnqueueToDbContext(DbContext.ExecCommandInfoType.Delete, state);
|
||||
}
|
||||
if (IsNoneDbContext) {
|
||||
var affrows = DbContextBetchRemove(dels.ToArray());
|
||||
IncrAffrows(affrows);
|
||||
return affrows;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
public void Remove(TEntity data) => RemoveAffrows(data);
|
||||
public void RemoveRange(IEnumerable<TEntity> data) => RemoveRangeAffrows(data);
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user