Synchronization code

This commit is contained in:
sunkaixuan 2022-10-24 14:02:40 +08:00
parent 7fde122f12
commit 07b0d2acbb
19 changed files with 215 additions and 61 deletions

View File

@ -9,7 +9,7 @@ namespace SqlSugar
{
public partial class InsertNavProvider<Root,T> where T : class,new() where Root:class,new()
{
public InsertNavRootOptions _RootOptions { get; set; }
public List<Root> _Roots { get; set; }
public List<object> _ParentList { get; set; }
public List<object> _RootList { get; set; }

View File

@ -58,7 +58,17 @@ namespace SqlSugar
{
foreach (var item in datas)
{
this._Context.Insertable(item).ExecuteCommandIdentityIntoEntity();
if (IsFirst&&_RootOptions!=null)
{
this._Context.Insertable(item)
.IgnoreColumns(_RootOptions.IgnoreColumns)
.InsertColumns(_RootOptions.InsertColumns)
.ExecuteCommandIdentityIntoEntity();
}
else
{
this._Context.Insertable(item).ExecuteCommandIdentityIntoEntity();
}
}
}
@ -71,6 +81,11 @@ namespace SqlSugar
}
return pkColumn;
}
private EntityColumnInfo GetPkColumnByNav2(EntityInfo entity, EntityColumnInfo nav)
{
var pkColumn = entity.Columns.FirstOrDefault(it => it.IsPrimarykey == true);
return pkColumn;
}
private EntityColumnInfo GetFKColumnByNav(EntityInfo entity, EntityColumnInfo nav)
{
var fkColumn = entity.Columns.FirstOrDefault(it => it.PropertyName == nav.Navigat.Name);
@ -142,7 +157,17 @@ namespace SqlSugar
pkColumn.PropertyInfo.SetValue(child, value());
}
}
this._Context.Insertable(insertData).ExecuteCommand();
if (IsFirst && _RootOptions != null)
{
this._Context.Insertable(insertData)
.IgnoreColumns(_RootOptions.IgnoreColumns)
.InsertColumns(_RootOptions.InsertColumns)
.ExecuteCommand();
}
else
{
this._Context.Insertable(insertData).ExecuteCommand();
}
}
private void SetError<TChild>(EntityColumnInfo pkColumn, List<TChild> insertData) where TChild : class, new()
{
@ -154,7 +179,17 @@ namespace SqlSugar
Check.ExceptionEasy($"The field {name} is not an autoassignment type and requires an assignment", $"字段{name}不是可自动赋值类型需要赋值(并且不能是已存在值) , 可赋值类型有 自增、long、Guid、string");
}
}
this._Context.Insertable(insertData).ExecuteCommand();
if (IsFirst && _RootOptions != null)
{
this._Context.Insertable(insertData)
.IgnoreColumns(_RootOptions.IgnoreColumns)
.InsertColumns(_RootOptions.InsertColumns)
.ExecuteCommand();
}
else
{
this._Context.Insertable(insertData).ExecuteCommand();
}
}
}
}

View File

@ -16,7 +16,7 @@ namespace SqlSugar
var parentList = _ParentList;
var parentNavigateProperty = parentEntity.Columns.FirstOrDefault(it => it.PropertyName == name);
var thisEntity = this._Context.EntityMaintenance.GetEntityInfo<TChild>();
var thisPkColumn = GetPkColumnByNav(thisEntity, nav);
var thisPkColumn = GetPkColumnByNav2(thisEntity, nav);
var thisFkColumn = GetFKColumnByNav(thisEntity, nav);
EntityColumnInfo parentPkColumn = GetParentPkColumn();
EntityColumnInfo parentNavColumn = GetParentPkNavColumn(nav);

View File

@ -39,6 +39,10 @@ namespace SqlSugar
foreach (var item in parentList)
{
var items = parentNavigateProperty.PropertyInfo.GetValue(item);
if (items == null)
{
continue;
}
var children = ((List<TChild>)items);
if (this._Options != null && this._Options.ManyToManyIsUpdateB)
{

View File

@ -9,7 +9,7 @@ namespace SqlSugar
{
public partial class UpdateNavProvider<Root, T> where T : class, new() where Root : class, new()
{
internal UpdateNavRootOptions _RootOptions { get; set; }
public List<Root> _Roots { get; set; }
public List<object> _ParentList { get; set; }
public List<object> _RootList { get; set; }
@ -18,6 +18,7 @@ namespace SqlSugar
public SqlSugarProvider _Context { get; set; }
public UpdateNavOptions _Options { get; set; }
public bool IsFirst { get; set; }
public UpdateNavProvider<Root, Root> AsNav()
{
return new UpdateNavProvider<Root, Root>
@ -54,6 +55,7 @@ namespace SqlSugar
private UpdateNavProvider<Root, TChild> _ThenInclude<TChild>(Expression<Func<T, TChild>> expression) where TChild : class, new()
{
var isRoot = _RootList == null;
IsFirst = isRoot && this._ParentList == null;
InitParentList();
var name = ExpressionTool.GetMemberName(expression);
var nav = this._ParentEntity.Columns.FirstOrDefault(x => x.PropertyName == name);
@ -62,6 +64,7 @@ namespace SqlSugar
Check.ExceptionEasy($"{name} no navigate attribute", $"{this._ParentEntity.EntityName}的属性{name}没有导航属性");
}
UpdateRoot(isRoot, nav);
IsFirst = false;
if (nav.Navigat.NavigatType == NavigateType.OneToOne || nav.Navigat.NavigatType == NavigateType.ManyToOne)
{
UpdateOneToOne<TChild>(name, nav);
@ -79,6 +82,7 @@ namespace SqlSugar
private UpdateNavProvider<Root, TChild> _ThenInclude<TChild>(Expression<Func<T, List<TChild>>> expression) where TChild : class, new()
{
var isRoot = _RootList == null;
IsFirst = isRoot && this._ParentList == null;
InitParentList();
var name = ExpressionTool.GetMemberName(expression);
var nav = this._ParentEntity.Columns.FirstOrDefault(x => x.PropertyName == name);
@ -87,6 +91,7 @@ namespace SqlSugar
Check.ExceptionEasy($"{name} no navigate attribute", $"{this._ParentEntity.EntityName}的属性{name}没有导航属性");
}
UpdateRoot(isRoot, nav);
IsFirst = false;
if (nav.Navigat.NavigatType == NavigateType.OneToOne || nav.Navigat.NavigatType == NavigateType.ManyToOne)
{
UpdateOneToOne<TChild>(name, nav);
@ -121,12 +126,46 @@ namespace SqlSugar
if (_Options != null && _Options.RootFunc != null)
{
var updateable = this._Context.Updateable(_Roots);
var exp= _Options.RootFunc as Expression<Action<IUpdateable<Root>>>;
var exp = _Options.RootFunc as Expression<Action<IUpdateable<Root>>>;
Check.ExceptionEasy(exp == null, "UpdateOptions.RootFunc is error", "UpdateOptions.RootFunc");
var com= exp.Compile();
var com = exp.Compile();
com(updateable);
updateable.ExecuteCommand();
}
else if (IsFirst && _RootOptions != null)
{
var isInsert = _RootOptions.IsInsertRoot;
if (isInsert)
{
var newRoots = new List<Root>();
foreach (var item in _Roots)
{
var x = this._Context.Storageable(item).ToStorage();
if (x.InsertList.HasValue())
{
newRoots.Add(x.AsInsertable.EnableDiffLogEventIF(_RootOptions.IsDiffLogEvent, _RootOptions.DiffLogBizData).ExecuteReturnEntity());
}
else
{
x.AsUpdateable.EnableDiffLogEventIF(_RootOptions.IsDiffLogEvent, _RootOptions.DiffLogBizData).ExecuteCommand();
newRoots.Add(item);
}
}
_ParentList = _RootList = newRoots.Cast<object>().ToList();
}
else
{
this._Context.Updateable(_Roots)
.EnableDiffLogEventIF(_RootOptions.IsDiffLogEvent,_RootOptions.DiffLogBizData)
.UpdateColumns(_RootOptions.UpdateColumns)
.IgnoreColumns(_RootOptions.IgnoreColumns)
.ExecuteCommand();
}
}
else if (_RootOptions != null && _RootOptions?.IsDiffLogEvent == true)
{
this._Context.Updateable(_Roots).EnableDiffLogEvent(_RootOptions.DiffLogBizData).ExecuteCommand();
}
else
{
this._Context.Updateable(_Roots).ExecuteCommand();

View File

@ -499,7 +499,14 @@ namespace SqlSugar
}
public IInsertable<T> EnableDiffLogEventIF(bool isDiffLogEvent, object diffLogBizData)
{
if (isDiffLogEvent)
{
return EnableDiffLogEvent(diffLogBizData);
}
return this;
}
public IInsertable<T> EnableDiffLogEvent(object businessData = null)
{
//Check.Exception(this.InsertObjs.HasValue() && this.InsertObjs.Count() > 1, "DiffLog does not support batch operations");

View File

@ -294,11 +294,7 @@ namespace SqlSugar
var list = this.ToList();
return GetTreeRoot(childListExpression, parentIdExpression, pk, list, rootValue);
}
public List<T> ToTree(Expression<Func<T, IEnumerable<object>>> childListExpression, Expression<Func<T, object>> parentIdExpression, object rootValue, object[] childIds)
{
var list = this.ToList();
return TreeAndFilterIds(childListExpression, parentIdExpression, rootValue, childIds, ref list);
}
public virtual DataTable ToDataTable()
{
QueryBuilder.ResultType = typeof(SugarCacheDataTable);

View File

@ -500,11 +500,7 @@ ParameterT parameter)
var list = await this.ToListAsync();
return GetTreeRoot(childListExpression, parentIdExpression, pk, list, rootValue);
}
public async Task<List<T>> ToTreeAsync(Expression<Func<T, IEnumerable<object>>> childListExpression, Expression<Func<T, object>> parentIdExpression, object rootValue, object[] childIds)
{
var list =await this.ToListAsync();
return TreeAndFilterIds(childListExpression, parentIdExpression, rootValue, childIds, ref list);
}
public async Task<List<T>> ToParentListAsync(Expression<Func<T, object>> parentIdExpression, object primaryKeyValue)
{
List<T> result = new List<T>() { };

View File

@ -182,51 +182,18 @@ namespace SqlSugar
return result;
}
private List<object> GetPrentIds(List<T> list, object id,EntityColumnInfo pkName, EntityColumnInfo parentName)
{
var currentId = id;
List<object> result = new List<object>();
result.Add(id);
while (list.Any(it => pkName.PropertyInfo.GetValue(it).Equals(currentId)))
{
var data = list.First(it => pkName.PropertyInfo.GetValue(it).Equals(currentId));
currentId = parentName.PropertyInfo.GetValue(data);
result.Add(currentId);
}
return result;
}
private List<T> TreeAndFilterIds(Expression<Func<T, IEnumerable<object>>> childListExpression, Expression<Func<T, object>> parentIdExpression, object rootValue, object[] childIds, ref List<T> list)
{
var entity = this.Context.EntityMaintenance.GetEntityInfo<T>();
var pk = GetTreeKey(entity);
var pkColumn = entity.Columns.FirstOrDefault(z => z.PropertyName == pk);
var newIds = new List<object>();
string parentIdName = GetParentName(parentIdExpression);
var parentColumn = entity.Columns.FirstOrDefault(z => z.PropertyName == parentIdName);
foreach (var id in childIds)
{
newIds.AddRange(GetPrentIds(list, id, pkColumn, parentColumn));
}
list = list.Where(z => newIds.Any(it => it.Equals(pkColumn.PropertyInfo.GetValue(z)))).ToList();
return GetTreeRoot(childListExpression, parentIdExpression, pk, list, rootValue);
}
private List<T> GetTreeRoot(Expression<Func<T, IEnumerable<object>>> childListExpression, Expression<Func<T, object>> parentIdExpression, string pk, List<T> list, object rootValue)
{
var childName = ((childListExpression as LambdaExpression).Body as MemberExpression).Member.Name;
string parentIdName = GetParentName(parentIdExpression);
return BuildTree(list, pk, parentIdName, childName, rootValue)?.ToList() ?? default;
}
private static string GetParentName(Expression<Func<T, object>> parentIdExpression)
{
var exp = (parentIdExpression as LambdaExpression).Body;
if (exp is UnaryExpression)
{
exp = (exp as UnaryExpression).Operand;
}
var parentIdName = (exp as MemberExpression).Member.Name;
return parentIdName;
return BuildTree(list, pk, parentIdName, childName, rootValue)?.ToList() ?? default;
}
private static IEnumerable<T> BuildTree(IEnumerable<T> list, string idName, string pIdName, string childName, object rootValue)

View File

@ -905,6 +905,20 @@ namespace SqlSugar
result.insertNavProvider = provider;
return result;
}
public InsertNavTaskInit<T, T> InsertNav<T>(T data, InsertNavRootOptions rootOptions) where T : class, new()
{
return InsertNav(new List<T>() { data },rootOptions); ;
}
public InsertNavTaskInit<T, T> InsertNav<T>(List<T> datas, InsertNavRootOptions rootOptions) where T : class, new()
{
var result = new InsertNavTaskInit<T, T>();
var provider = new InsertNavProvider<T, T>();
provider._Roots = datas;
provider._Context = this;
provider._RootOptions = rootOptions;
result.insertNavProvider = provider;
return result;
}
public DeleteNavTaskInit<T, T> DeleteNav<T>(T data) where T : class, new()
{
return DeleteNav(new List<T>() { data });
@ -934,6 +948,20 @@ namespace SqlSugar
result.UpdateNavProvider = provider;
return result;
}
public UpdateNavTaskInit<T, T> UpdateNav<T>(T data, UpdateNavRootOptions rootOptions) where T : class, new()
{
return UpdateNav(new List<T>() { data},rootOptions);
}
public UpdateNavTaskInit<T, T> UpdateNav<T>(List<T> datas, UpdateNavRootOptions rootOptions) where T : class, new()
{
var result = new UpdateNavTaskInit<T, T>();
var provider = new UpdateNavProvider<T, T>();
provider._Roots = datas;
provider._RootOptions = rootOptions;
provider._Context = this;
result.UpdateNavProvider = provider;
return result; ;
}
#endregion
#region DbFirst

View File

@ -689,6 +689,14 @@ namespace SqlSugar
{
return ScopedContext.InsertNav(datas);
}
public InsertNavTaskInit<T, T> InsertNav<T>(T data,InsertNavRootOptions rootOptions) where T : class, new()
{
return ScopedContext.InsertNav(data,rootOptions);
}
public InsertNavTaskInit<T, T> InsertNav<T>(List<T> datas, InsertNavRootOptions rootOptions) where T : class, new()
{
return ScopedContext.InsertNav(datas,rootOptions);
}
public DeleteNavTaskInit<T, T> DeleteNav<T>(T data) where T : class, new()
{
return ScopedContext.DeleteNav(data);
@ -709,6 +717,14 @@ namespace SqlSugar
{
return ScopedContext.UpdateNav(datas);
}
public UpdateNavTaskInit<T, T> UpdateNav<T>(List<T> datas, UpdateNavRootOptions rootOptions) where T : class, new()
{
return this.ScopedContext.UpdateNav(datas, rootOptions);
}
public UpdateNavTaskInit<T, T> UpdateNav<T>(T data, UpdateNavRootOptions rootOptions) where T : class, new()
{
return this.ScopedContext.UpdateNav(data, rootOptions);
}
public SqlSugarClient CopyNew()
{
return new SqlSugarClient(UtilMethods.CopyConfig(this.Ado.Context.CurrentConnectionConfig));

View File

@ -198,6 +198,14 @@ namespace SqlSugar
this.Context.MappingTables.Add(entityName, tableName);
return this; ;
}
public IUpdateable<T> EnableDiffLogEventIF(bool isEnableDiffLog, object businessData = null)
{
if (isEnableDiffLog)
{
return EnableDiffLogEvent(businessData);
}
return this;
}
public IUpdateable<T> EnableDiffLogEvent(object businessData = null)
{
//Check.Exception(this.UpdateObjs.HasValue() && this.UpdateObjs.Count() > 1, "DiffLog does not support batch operations");
@ -343,8 +351,11 @@ namespace SqlSugar
}
public IUpdateable<T> UpdateColumns(string[] columns)
{
ThrowUpdateByExpression();
this.UpdateBuilder.DbColumnInfoList = this.UpdateBuilder.DbColumnInfoList.Where(it => GetPrimaryKeys().Select(iit => iit.ToLower()).Contains(it.DbColumnName.ToLower()) || columns.Contains(it.PropertyName, StringComparer.OrdinalIgnoreCase)).ToList();
if (columns.HasValue())
{
ThrowUpdateByExpression();
this.UpdateBuilder.DbColumnInfoList = this.UpdateBuilder.DbColumnInfoList.Where(it => GetPrimaryKeys().Select(iit => iit.ToLower()).Contains(it.DbColumnName.ToLower()) || columns.Contains(it.PropertyName, StringComparer.OrdinalIgnoreCase)).ToList();
}
return this;
}
public IUpdateable<T> UpdateColumnsIF(bool isUpdateColumns, Expression<Func<T, object>> columns)

View File

@ -5,13 +5,31 @@ using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
namespace SqlSugar
namespace SqlSugar
{
public class DeleteNavOptions
{
public bool ManyToManyIsDeleteA { get; set; }
public bool ManyToManyIsDeleteB { get; set; }
}
}
public class InsertNavRootOptions
{
public string[] IgnoreColumns { get; set; }
public string[] InsertColumns { get; set; }
}
public class InertNavRootOptions
{
public string[] IgnoreColumns { get; set; }
public string[] InsertColumns { get; set; }
}
public class UpdateNavRootOptions
{
public string IgnoreColumns { get; set; }
public string[] UpdateColumns { get; set; }
public bool IsInsertRoot { get; set; }
public bool IsDiffLogEvent { get; set; }
public object DiffLogBizData { get; set; }
}
public class UpdateNavOptions
{
public bool ManyToManyIsUpdateA { get; set; }

View File

@ -177,7 +177,6 @@ namespace SqlSugar
List<T> ToParentList(Expression<Func<T, object>> parentIdExpression, object primaryKeyValue);
Task<List<T>> ToParentListAsync(Expression<Func<T, object>> parentIdExpression, object primaryKeyValue);
List<T> ToTree(Expression<Func<T,IEnumerable<object>>> childListExpression, Expression<Func<T,object>> parentIdExpression,object rootValue);
List<T> ToTree(Expression<Func<T, IEnumerable<object>>> childListExpression, Expression<Func<T, object>> parentIdExpression, object rootValue,object [] childIds);
Task<List<T>> ToTreeAsync(Expression<Func<T, IEnumerable<object>>> childListExpression, Expression<Func<T, object>> parentIdExpression, object rootValue);
DataTable ToDataTable();
Task<DataTable> ToDataTableAsync();

View File

@ -209,11 +209,15 @@ namespace SqlSugar
#region Nav CUD
InsertNavTaskInit<T, T> InsertNav<T>(T data) where T : class, new();
InsertNavTaskInit<T, T> InsertNav<T>(List<T> datas) where T : class, new();
InsertNavTaskInit<T, T> InsertNav<T>(T data,InsertNavRootOptions rootOptions) where T : class, new();
InsertNavTaskInit<T, T> InsertNav<T>(List<T> datas, InsertNavRootOptions rootOptions) where T : class, new();
DeleteNavTaskInit<T, T> DeleteNav<T>(T data) where T : class, new();
DeleteNavTaskInit<T, T> DeleteNav<T>(List<T> datas) where T : class, new();
DeleteNavTaskInit<T, T> DeleteNav<T>(Expression<Func<T,bool>> whereExpression) where T : class, new();
UpdateNavTaskInit<T, T> UpdateNav<T>(T data) where T : class, new ();
UpdateNavTaskInit<T, T> UpdateNav<T>(List<T> datas) where T : class, new ();
UpdateNavTaskInit<T, T> UpdateNav<T>(List<T> datas) where T : class, new ();
UpdateNavTaskInit<T, T> UpdateNav<T>(T data,UpdateNavRootOptions rootOptions) where T : class, new();
UpdateNavTaskInit<T, T> UpdateNav<T>(List<T> datas, UpdateNavRootOptions rootOptions) where T : class, new();
#endregion
}

View File

@ -92,6 +92,7 @@ namespace SqlSugar
IUpdateable<T> IsEnableUpdateVersionValidation();
IUpdateable<T> EnableDiffLogEvent(object businessData = null);
IUpdateable<T> EnableDiffLogEventIF(bool isEnableDiffLog,object businessData = null);
IUpdateable<T> ReSetValue(Action<T> setValueExpression);
IUpdateable<T> RemoveDataCache();
IUpdateable<T> RemoveDataCache(string likeString);

View File

@ -40,6 +40,7 @@ namespace SqlSugar
IInsertable<T> CallEntityMethod(Expression<Action<T>> method);
IInsertable<T> EnableDiffLogEvent(object businessData = null);
IInsertable<T> EnableDiffLogEventIF(bool isDiffLogEvent, object businessData=null);
IInsertable<T> RemoveDataCache();
IInsertable<T> RemoveDataCache(string likeString);
KeyValuePair<string, List<SugarParameter>> ToSql();

View File

@ -198,6 +198,14 @@ namespace SqlSugar
{
return this.Context.InsertNav(datas);
}
public InsertNavTaskInit<T, T> InsertNav<T>(T data, InsertNavRootOptions rootOptions) where T : class, new()
{
return this.Context.InsertNav(data, rootOptions);
}
public InsertNavTaskInit<T, T> InsertNav<T>(List<T> datas, InsertNavRootOptions rootOptions) where T : class, new()
{
return this.Context.InsertNav(datas, rootOptions);
}
public DeleteNavTaskInit<T, T> DeleteNav<T>(T data) where T : class, new()
{
return this.Context.DeleteNav(data);
@ -214,9 +222,17 @@ namespace SqlSugar
{
return this.Context.UpdateNav(data);
}
public UpdateNavTaskInit<T, T> UpdateNav<T>(List<T> datas,UpdateNavRootOptions rootOptions) where T : class, new()
{
return this.Context.UpdateNav(datas, rootOptions);
}
public UpdateNavTaskInit<T, T> UpdateNav<T>(T data, UpdateNavRootOptions rootOptions) where T : class, new()
{
return this.Context.UpdateNav(data,rootOptions);
}
public UpdateNavTaskInit<T, T> UpdateNav<T>(List<T> datas) where T : class, new()
{
return this.Context.UpdateNav(datas);
return this.Context.UpdateNav(datas);
}
#endregion

View File

@ -745,6 +745,14 @@ namespace SqlSugar
{
return ScopedContext.InsertNav(datas);
}
public InsertNavTaskInit<T, T> InsertNav<T>(T data, InsertNavRootOptions rootOptions) where T : class, new()
{
return ScopedContext.InsertNav(data, rootOptions);
}
public InsertNavTaskInit<T, T> InsertNav<T>(List<T> datas, InsertNavRootOptions rootOptions) where T : class, new()
{
return ScopedContext.InsertNav(datas, rootOptions);
}
public DeleteNavTaskInit<T, T> DeleteNav<T>(T data) where T : class, new()
{
return ScopedContext.DeleteNav(data);
@ -765,6 +773,14 @@ namespace SqlSugar
{
return ScopedContext.UpdateNav(datas);
}
public UpdateNavTaskInit<T, T> UpdateNav<T>(T data,UpdateNavRootOptions rootOptions) where T : class, new()
{
return ScopedContext.UpdateNav(data);
}
public UpdateNavTaskInit<T, T> UpdateNav<T>(List<T> datas, UpdateNavRootOptions rootOptions) where T : class, new()
{
return ScopedContext.UpdateNav(datas);
}
public SqlSugarClient CopyNew()
{
return new SqlSugarClient(UtilMethods.CopyConfig(this.Ado.Context.CurrentConnectionConfig));