mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-07-16 16:50:41 +08:00
Update .net core project
This commit is contained in:
parent
d8d2be1274
commit
2d9eb9c8ac
@ -284,12 +284,17 @@ namespace SqlSugar
|
|||||||
column.IsArray = sugarColumn.IsArray;
|
column.IsArray = sugarColumn.IsArray;
|
||||||
column.IsTreeKey = sugarColumn.IsTreeKey;
|
column.IsTreeKey = sugarColumn.IsTreeKey;
|
||||||
column.SqlParameterDbType = sugarColumn.SqlParameterDbType;
|
column.SqlParameterDbType = sugarColumn.SqlParameterDbType;
|
||||||
|
|
||||||
if (sugarColumn.IsJson && String.IsNullOrEmpty(sugarColumn.ColumnDataType))
|
if (sugarColumn.IsJson && String.IsNullOrEmpty(sugarColumn.ColumnDataType))
|
||||||
{
|
{
|
||||||
if (this.Context.CurrentConnectionConfig.DbType == DbType.PostgreSQL)
|
if (this.Context.CurrentConnectionConfig.DbType == DbType.PostgreSQL)
|
||||||
{
|
{
|
||||||
column.DataType = "json";
|
column.DataType = "json";
|
||||||
}
|
}
|
||||||
|
else if (column.Length > 0)
|
||||||
|
{
|
||||||
|
column.DataType = "varchar";
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
column.DataType = "varchar(4000)";
|
column.DataType = "varchar(4000)";
|
||||||
|
@ -7,112 +7,72 @@ using System.Threading.Tasks;
|
|||||||
|
|
||||||
namespace SqlSugar
|
namespace SqlSugar
|
||||||
{
|
{
|
||||||
public class InsertNavProvider<Root,T> where T : class,new() where Root:class,new()
|
public partial class InsertNavProvider<Root,T> where T : class,new() where Root:class,new()
|
||||||
{
|
{
|
||||||
|
|
||||||
public List<Root> Roots { get; set; }
|
public List<Root> _Roots { get; set; }
|
||||||
public List<object> ParentList { get; set; }
|
public List<object> _ParentList { get; set; }
|
||||||
public EntityInfo ParentEntity { get; set; }
|
public List<object> _RootList { get; set; }
|
||||||
public SqlSugarProvider Context { get; set; }
|
public EntityInfo _ParentEntity { get; set; }
|
||||||
|
public EntityColumnInfo _ParentPkColumn { get; set; }
|
||||||
|
public SqlSugarProvider _Context { get; set; }
|
||||||
|
|
||||||
public InsertNavProvider<Root,TChild> ThenInclude<TChild>(Expression<Func<T,TChild>> expression) where TChild : class, new()
|
|
||||||
|
public InsertNavProvider<Root, Root> AsNav()
|
||||||
{
|
{
|
||||||
if (ParentList == null)
|
return new InsertNavProvider<Root, Root> {
|
||||||
{
|
_Context = _Context,
|
||||||
ParentList = GetParentList(Roots).Cast<object>().ToList();
|
_ParentEntity = null,
|
||||||
|
_ParentList=null,
|
||||||
|
_Roots= _Roots ,
|
||||||
|
_ParentPkColumn=this._Context.EntityMaintenance.GetEntityInfo<Root>().Columns.First(it=>it.IsPrimarykey)
|
||||||
|
};
|
||||||
}
|
}
|
||||||
var name=ExpressionTool.GetMemberName(expression);
|
public InsertNavProvider<Root, TChild> ThenInclude<TChild>(Expression<Func<T, TChild>> expression) where TChild : class, new()
|
||||||
var nav = this.ParentEntity.Columns.FirstOrDefault(x => x.PropertyName == name);
|
{
|
||||||
|
InitParentList();
|
||||||
|
var name = ExpressionTool.GetMemberName(expression);
|
||||||
|
var nav = this._ParentEntity.Columns.FirstOrDefault(x => x.PropertyName == name);
|
||||||
if (nav.Navigat == null)
|
if (nav.Navigat == null)
|
||||||
{
|
{
|
||||||
Check.ExceptionEasy($"{name} no navigate attribute", $"{this.ParentEntity.EntityName}的属性{name}没有导航属性");
|
Check.ExceptionEasy($"{name} no navigate attribute", $"{this._ParentEntity.EntityName}的属性{name}没有导航属性");
|
||||||
}
|
}
|
||||||
if (nav.Navigat.NavigatType == NavigateType.OneToOne || nav.Navigat.NavigatType == NavigateType.ManyToOne)
|
if (nav.Navigat.NavigatType == NavigateType.OneToOne || nav.Navigat.NavigatType == NavigateType.ManyToOne)
|
||||||
{
|
{
|
||||||
InsertOneToOne<TChild>(ParentList,this.ParentEntity, name,nav);
|
InsertOneToOne<TChild>(name, nav);
|
||||||
}
|
}
|
||||||
else if (nav.Navigat.NavigatType == NavigateType.OneToMany)
|
else if (nav.Navigat.NavigatType == NavigateType.OneToMany)
|
||||||
{
|
{
|
||||||
InsertOneToMany();
|
InsertOneToMany<TChild>(name, nav);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
InsertManyToMany();
|
InsertManyToMany<TChild>(name, nav);
|
||||||
}
|
}
|
||||||
return null;
|
return GetResult<TChild>();
|
||||||
}
|
}
|
||||||
|
public InsertNavProvider<Root, TChild> ThenInclude<TChild>(Expression<Func<T,List<TChild>>> expression) where TChild : class, new()
|
||||||
private void InsertManyToMany()
|
|
||||||
{
|
{
|
||||||
|
InitParentList();
|
||||||
|
var name = ExpressionTool.GetMemberName(expression);
|
||||||
|
var nav = this._ParentEntity.Columns.FirstOrDefault(x => x.PropertyName == name);
|
||||||
|
if (nav.Navigat == null)
|
||||||
|
{
|
||||||
|
Check.ExceptionEasy($"{name} no navigate attribute", $"{this._ParentEntity.EntityName}的属性{name}没有导航属性");
|
||||||
}
|
}
|
||||||
|
if (nav.Navigat.NavigatType == NavigateType.OneToOne || nav.Navigat.NavigatType == NavigateType.ManyToOne)
|
||||||
private void InsertOneToMany()
|
|
||||||
{
|
{
|
||||||
|
InsertOneToOne<TChild>(name, nav);
|
||||||
}
|
}
|
||||||
|
else if (nav.Navigat.NavigatType == NavigateType.OneToMany)
|
||||||
private void InsertOneToOne<TChild>(List<object> parentList, EntityInfo parentEntity, string name, EntityColumnInfo nav)
|
|
||||||
{
|
{
|
||||||
var parentColumn = parentEntity.Columns.FirstOrDefault(it => it.PropertyName==nav.Navigat.Name);
|
InsertOneToMany<TChild>(name, nav);
|
||||||
this.ParentEntity = this.Context.EntityMaintenance.GetEntityInfo<TChild>();
|
|
||||||
var pkColumn = ParentEntity.Columns.FirstOrDefault(it=>it.IsPrimarykey==true);
|
|
||||||
if (nav.Navigat.Name2.HasValue())
|
|
||||||
{
|
|
||||||
pkColumn = ParentEntity.Columns.FirstOrDefault(it => it.PropertyName==nav.Navigat.Name2);
|
|
||||||
}
|
|
||||||
Check.Exception(pkColumn == null, $" Navigate {parentEntity.EntityName} : {name} is error ", $"导航实体 {parentEntity.EntityName} 属性 {name} 配置错误");
|
|
||||||
List<object> childList = new List<object>();
|
|
||||||
foreach (var parent in parentList)
|
|
||||||
{
|
|
||||||
var childItems=(TChild)nav.PropertyInfo.GetValue(parent);
|
|
||||||
if (childItems != null)
|
|
||||||
{
|
|
||||||
var pkValue = pkColumn.PropertyInfo.GetValue(childItems);
|
|
||||||
var pvValue = parentColumn.PropertyInfo.GetValue(parent);
|
|
||||||
if (IsDefaultValue(pvValue))
|
|
||||||
{
|
|
||||||
pvValue = pkValue;
|
|
||||||
}
|
|
||||||
if (IsDefaultValue(pvValue))
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
parentList = childList;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static bool IsDefaultValue(object pvValue)
|
|
||||||
{
|
|
||||||
return pvValue == null || pvValue == UtilMethods.GetDefaultValue(pvValue.GetType());
|
|
||||||
}
|
|
||||||
|
|
||||||
private List<Type> GetParentList<Type>(List<Type> datas) where Type : class ,new()
|
|
||||||
{
|
|
||||||
List<Type> result = new List<Type>();
|
|
||||||
this.Context.InitMappingInfo<Type>();
|
|
||||||
var entity = this.Context.EntityMaintenance.GetEntityInfo<Type>();
|
|
||||||
var isIdentity = entity.Columns.Where(it=>it.IsIdentity).Any();
|
|
||||||
if (isIdentity)
|
|
||||||
{
|
|
||||||
foreach (var item in datas)
|
|
||||||
{
|
|
||||||
this.Context.Insertable(datas).ExecuteCommandIdentityIntoEntity();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
this.Context.Insertable(datas).ExecuteCommand();
|
InsertManyToMany<TChild>(name, nav);
|
||||||
}
|
}
|
||||||
this.ParentEntity = entity;
|
return GetResult<TChild>();
|
||||||
result = datas;
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
public InsertNavProvider<Root,Root> AsNav()
|
|
||||||
{
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,95 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace SqlSugar
|
||||||
|
{
|
||||||
|
public partial class InsertNavProvider<Root,T> where T : class,new() where Root:class,new()
|
||||||
|
{
|
||||||
|
|
||||||
|
private static bool IsDefaultValue(object pvValue)
|
||||||
|
{
|
||||||
|
return pvValue == null || pvValue == UtilMethods.GetDefaultValue(pvValue.GetType());
|
||||||
|
}
|
||||||
|
private void InitParentList()
|
||||||
|
{
|
||||||
|
if (_RootList == null)
|
||||||
|
{
|
||||||
|
_RootList= _ParentList = GetRootList(_Roots).Cast<object>().ToList();
|
||||||
|
}
|
||||||
|
else if (_ParentList == null)
|
||||||
|
{
|
||||||
|
_ParentList = _RootList;
|
||||||
|
var pkColumn = this._Context.EntityMaintenance.GetEntityInfo<T>().Columns.FirstOrDefault(it => it.IsPrimarykey);
|
||||||
|
this._ParentPkColumn = pkColumn;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private InsertNavProvider<Root, TChild> GetResult<TChild>() where TChild : class, new()
|
||||||
|
{
|
||||||
|
return new InsertNavProvider<Root, TChild>()
|
||||||
|
{
|
||||||
|
_Context = this._Context,
|
||||||
|
_ParentEntity = this._ParentEntity,
|
||||||
|
_ParentList = this._ParentList,
|
||||||
|
_Roots = this._Roots,
|
||||||
|
_ParentPkColumn=this._ParentPkColumn,
|
||||||
|
_RootList=this._RootList
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<Type> GetRootList<Type>(List<Type> datas) where Type : class, new()
|
||||||
|
{
|
||||||
|
List<Type> result = new List<Type>();
|
||||||
|
this._Context.InitMappingInfo<Type>();
|
||||||
|
var entity = this._Context.EntityMaintenance.GetEntityInfo<Type>();
|
||||||
|
var pkColumn = entity.Columns.FirstOrDefault(it => it.IsPrimarykey);
|
||||||
|
InsertDatas(datas, pkColumn);
|
||||||
|
this._ParentEntity = entity;
|
||||||
|
result = datas;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void InsertIdentity<Type>(List<Type> datas) where Type : class, new()
|
||||||
|
{
|
||||||
|
foreach (var item in datas)
|
||||||
|
{
|
||||||
|
this._Context.Insertable(item).ExecuteCommandIdentityIntoEntity();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private EntityColumnInfo GetPkColumnByNav(EntityInfo entity,EntityColumnInfo nav)
|
||||||
|
{
|
||||||
|
var pkColumn = entity.Columns.FirstOrDefault(it => it.IsPrimarykey == true);
|
||||||
|
if (nav.Navigat.Name2.HasValue())
|
||||||
|
{
|
||||||
|
pkColumn = _ParentEntity.Columns.FirstOrDefault(it => it.PropertyName == nav.Navigat.Name2);
|
||||||
|
}
|
||||||
|
return pkColumn;
|
||||||
|
}
|
||||||
|
private EntityColumnInfo GetFKColumnByNav(EntityInfo entity, EntityColumnInfo nav)
|
||||||
|
{
|
||||||
|
var fkColumn = entity.Columns.FirstOrDefault(it => it.PropertyName == nav.Navigat.Name);
|
||||||
|
return fkColumn;
|
||||||
|
}
|
||||||
|
private void InsertDatas<TChild>(List<TChild> children, EntityColumnInfo pkColumn) where TChild : class, new()
|
||||||
|
{
|
||||||
|
children = children.Distinct().ToList();
|
||||||
|
var x = this._Context.Storageable(children).WhereColumns(new string[] { pkColumn.PropertyName }).ToStorage();
|
||||||
|
var insertData = children = x.InsertList.Select(it => it.Item).ToList();
|
||||||
|
if (pkColumn.IsIdentity)
|
||||||
|
{
|
||||||
|
InsertIdentity(insertData);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
this._Context.Insertable(insertData).ExecuteCommand();
|
||||||
|
}
|
||||||
|
this._ParentList = children.Cast<object>().ToList();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,16 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
namespace SqlSugar
|
||||||
|
{
|
||||||
|
public partial class InsertNavProvider<Root, T> where T : class, new() where Root : class, new()
|
||||||
|
{
|
||||||
|
private void InsertManyToMany<TChild>(string name, EntityColumnInfo nav) where TChild : class, new()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,56 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
namespace SqlSugar
|
||||||
|
{
|
||||||
|
public partial class InsertNavProvider<Root, T> where T : class, new() where Root : class, new()
|
||||||
|
{
|
||||||
|
|
||||||
|
private void InsertOneToMany<TChild>(string name, EntityColumnInfo nav) where TChild : class, new()
|
||||||
|
{
|
||||||
|
List<TChild> children = new List<TChild>();
|
||||||
|
var parentEntity = _ParentEntity;
|
||||||
|
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 thisFkColumn= GetFKColumnByNav(thisEntity, nav);
|
||||||
|
EntityColumnInfo parentPkColumn = GetParentPkColumn();
|
||||||
|
foreach (var item in parentList)
|
||||||
|
{
|
||||||
|
var parentValue = parentPkColumn.PropertyInfo.GetValue(item);
|
||||||
|
var childs = parentNavigateProperty.PropertyInfo.GetValue(item) as List<TChild>;
|
||||||
|
if (childs != null)
|
||||||
|
{
|
||||||
|
foreach (var child in childs)
|
||||||
|
{
|
||||||
|
thisFkColumn.PropertyInfo.SetValue(child, parentValue, null);
|
||||||
|
}
|
||||||
|
children.AddRange(childs);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
InsertDatas(children, thisPkColumn);
|
||||||
|
SetNewParent<TChild>(thisEntity,thisPkColumn);
|
||||||
|
}
|
||||||
|
|
||||||
|
private EntityColumnInfo GetParentPkColumn()
|
||||||
|
{
|
||||||
|
EntityColumnInfo parentPkColumn = _ParentPkColumn;
|
||||||
|
if (_ParentPkColumn == null)
|
||||||
|
{
|
||||||
|
_ParentPkColumn= this._ParentEntity.Columns.FirstOrDefault(it => it.IsPrimarykey);
|
||||||
|
}
|
||||||
|
|
||||||
|
return parentPkColumn;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void SetNewParent<TChild>(EntityInfo entityInfo,EntityColumnInfo entityColumnInfo) where TChild : class, new()
|
||||||
|
{
|
||||||
|
this._ParentEntity = entityInfo;
|
||||||
|
this._ParentPkColumn = entityColumnInfo;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,44 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
namespace SqlSugar
|
||||||
|
{
|
||||||
|
public partial class InsertNavProvider<Root, T> where T : class, new() where Root : class, new()
|
||||||
|
{
|
||||||
|
private void InsertOneToOne<TChild>(string name, EntityColumnInfo nav) where TChild : class, new()
|
||||||
|
{
|
||||||
|
var parentEntity = _ParentEntity;
|
||||||
|
var parentList = _ParentList;
|
||||||
|
var parentColumn = parentEntity.Columns.FirstOrDefault(it => it.PropertyName == nav.Navigat.Name);
|
||||||
|
var thisEntity = this._Context.EntityMaintenance.GetEntityInfo<TChild>();
|
||||||
|
EntityColumnInfo thisPkColumn = GetPkColumnByNav(thisEntity, nav);
|
||||||
|
Check.Exception(thisPkColumn == null, $" Navigate {parentEntity.EntityName} : {name} is error ", $"导航实体 {parentEntity.EntityName} 属性 {name} 配置错误");
|
||||||
|
List<TChild> childList = new List<TChild>();
|
||||||
|
foreach (var parent in parentList)
|
||||||
|
{
|
||||||
|
var navPropertyValue = parentColumn.PropertyInfo.GetValue(parent);
|
||||||
|
var childItem = (TChild)nav.PropertyInfo.GetValue(parent);
|
||||||
|
if (childItem != null)
|
||||||
|
{
|
||||||
|
if (IsDefaultValue(navPropertyValue))
|
||||||
|
{
|
||||||
|
var pkValue = thisPkColumn.PropertyInfo.GetValue(childItem);
|
||||||
|
if (IsDefaultValue(navPropertyValue))
|
||||||
|
{
|
||||||
|
navPropertyValue = pkValue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
thisPkColumn.PropertyInfo.SetValue(childItem, navPropertyValue);
|
||||||
|
childList.Add(childItem);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
InsertDatas<TChild>(childList,thisPkColumn);
|
||||||
|
this._ParentList = childList.Cast<object>().ToList();
|
||||||
|
SetNewParent<TChild>(thisEntity, thisPkColumn);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -296,7 +296,7 @@ namespace SqlSugar
|
|||||||
|
|
||||||
public IStorageable<T> WhereColumns(string [] columns)
|
public IStorageable<T> WhereColumns(string [] columns)
|
||||||
{
|
{
|
||||||
var list = columns.Select(it=>this.Context.EntityMaintenance.GetDbColumnName<T>(it)).ToList();
|
var list = columns.Select(it=>this.Context.EntityMaintenance.GetPropertyName<T>(it)).ToList();
|
||||||
var exp=ExpressionBuilderHelper.CreateNewFields<T>(this.Context.EntityMaintenance.GetEntityInfo<T>(), list);
|
var exp=ExpressionBuilderHelper.CreateNewFields<T>(this.Context.EntityMaintenance.GetEntityInfo<T>(), list);
|
||||||
return this.WhereColumns(exp);
|
return this.WhereColumns(exp);
|
||||||
}
|
}
|
||||||
|
@ -897,8 +897,8 @@ namespace SqlSugar
|
|||||||
public InsertNavProvider<T, T> InsertNav<T>(List<T> datas) where T : class, new()
|
public InsertNavProvider<T, T> InsertNav<T>(List<T> datas) where T : class, new()
|
||||||
{
|
{
|
||||||
var result = new InsertNavProvider<T, T>();
|
var result = new InsertNavProvider<T, T>();
|
||||||
result.Roots = datas;
|
result._Roots = datas;
|
||||||
result.Context = this;
|
result._Context = this;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
public DeleteNavProvider<T, T> DeleteNav<T>(T data)
|
public DeleteNavProvider<T, T> DeleteNav<T>(T data)
|
||||||
|
@ -750,7 +750,7 @@ namespace SqlSugar
|
|||||||
{
|
{
|
||||||
var attr = typeof(T).GetCustomAttribute<TenantAttribute>();
|
var attr = typeof(T).GetCustomAttribute<TenantAttribute>();
|
||||||
if (attr == null)
|
if (attr == null)
|
||||||
return this.GetConnection(this.CurrentConnectionConfig.ConfigId);
|
return this.GetConnectionScope(this.CurrentConnectionConfig.ConfigId);
|
||||||
var configId = attr.configId;
|
var configId = attr.configId;
|
||||||
return this.GetConnectionScope(configId);
|
return this.GetConnectionScope(configId);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user