mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-08-01 10:10:16 +08:00
UPDATE CORE
This commit is contained in:
parent
ec553b4fab
commit
d247a3ac92
@ -262,21 +262,37 @@ namespace SqlSugar
|
|||||||
{
|
{
|
||||||
return new SubInsertable<T>();
|
return new SubInsertable<T>();
|
||||||
}
|
}
|
||||||
string subMemberName;
|
|
||||||
object sublist;
|
|
||||||
SubInsertable<T> result = new SubInsertable<T>();
|
SubInsertable<T> result = new SubInsertable<T>();
|
||||||
result.GetList(this.InsertObjs,items, out subMemberName, out sublist);
|
|
||||||
result.InsertObjects = this.InsertObjs;
|
result.InsertObjects = this.InsertObjs;
|
||||||
result.Context = this.Context;
|
result.Context = this.Context;
|
||||||
result.SubList = new Dictionary<string, object>();
|
result.SubList = new List<SubInsertTreeExpression>();
|
||||||
result.SubList.Add(subMemberName, sublist);
|
result.SubList.Add(new SubInsertTreeExpression() { Expression= items });
|
||||||
result.InsertBuilder = this.InsertBuilder;
|
result.InsertBuilder = this.InsertBuilder;
|
||||||
result.Pk = GetPrimaryKeys().First();
|
result.Pk = GetPrimaryKeys().First();
|
||||||
result.Entity = this.EntityInfo;
|
result.Entity = this.EntityInfo;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
public ISubInsertable<T> AddSubList(Expression<Func<T, SubInsertTree>> tree)
|
||||||
|
{
|
||||||
|
Check.Exception(GetPrimaryKeys().Count == 0, typeof(T).Name + " need Primary key");
|
||||||
|
Check.Exception(GetPrimaryKeys().Count > 1, typeof(T).Name + "Multiple primary keys are not supported");
|
||||||
|
//Check.Exception(this.InsertObjs.Count() > 1, "SubInserable No Support Insertable(List<T>)");
|
||||||
|
//Check.Exception(items.ToString().Contains(".First().")==false, items.ToString()+ " not supported ");
|
||||||
|
if (this.InsertObjs == null || this.InsertObjs.Count() == 0)
|
||||||
|
{
|
||||||
|
return new SubInsertable<T>();
|
||||||
|
}
|
||||||
|
SubInsertable<T> result = new SubInsertable<T>();
|
||||||
|
result.InsertObjects = this.InsertObjs;
|
||||||
|
result.Context = this.Context;
|
||||||
|
result.SubList = new List<SubInsertTreeExpression>();
|
||||||
|
result.InsertBuilder = this.InsertBuilder;
|
||||||
|
result.Pk = GetPrimaryKeys().First();
|
||||||
|
result.Entity = this.EntityInfo;
|
||||||
|
result.AddSubList(tree);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Protected Methods
|
#region Protected Methods
|
||||||
|
@ -10,176 +10,220 @@ namespace SqlSugar
|
|||||||
public class SubInsertable<T> : ISubInsertable<T> where T : class, new()
|
public class SubInsertable<T> : ISubInsertable<T> where T : class, new()
|
||||||
{
|
{
|
||||||
internal EntityInfo Entity { get; set; }
|
internal EntityInfo Entity { get; set; }
|
||||||
internal Dictionary<string, object> SubList { get; set; }
|
internal List<SubInsertTreeExpression> SubList { get; set; }
|
||||||
internal SqlSugarProvider Context { get; set; }
|
internal SqlSugarProvider Context { get; set; }
|
||||||
internal T [] InsertObjects { get; set; }
|
internal T[] InsertObjects { get; set; }
|
||||||
internal InsertBuilder InsertBuilder { get; set; }
|
internal InsertBuilder InsertBuilder { get; set; }
|
||||||
internal string Pk { get; set; }
|
internal string Pk { get; set; }
|
||||||
|
|
||||||
public ISubInsertable<T> AddSubList(Expression<Func<T, object>> items)
|
public ISubInsertable<T> AddSubList(Expression<Func<T, object>> items)
|
||||||
{
|
{
|
||||||
if (InsertObjects != null&&InsertObjects.Count() > 0)
|
if (this.SubList == null)
|
||||||
|
this.SubList = new List<SubInsertTreeExpression>();
|
||||||
|
this.SubList.Add(new SubInsertTreeExpression() { Expression = items });
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
public ISubInsertable<T> AddSubList(Expression<Func<T, SubInsertTree>> tree)
|
||||||
|
{
|
||||||
|
try
|
||||||
{
|
{
|
||||||
string subMemberName;
|
var lamda = (tree as LambdaExpression);
|
||||||
object sublist;
|
var memInit = lamda.Body as MemberInitExpression;
|
||||||
GetList(InsertObjects, items, out subMemberName, out sublist);
|
if (memInit.Bindings != null)
|
||||||
if (!this.SubList.ContainsKey(subMemberName))
|
|
||||||
{
|
{
|
||||||
this.SubList.Add(subMemberName, sublist);
|
|
||||||
|
MemberAssignment memberAssignment = (MemberAssignment)memInit.Bindings[0];
|
||||||
|
SubList.Add(new SubInsertTreeExpression()
|
||||||
|
{
|
||||||
|
Expression = memberAssignment.Expression,
|
||||||
|
Childs = GetSubInsertTree(((MemberAssignment)memInit.Bindings[1]).Expression)
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
Check.Exception(true, tree.ToString() + " format error ");
|
||||||
|
}
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private List<SubInsertTreeExpression> GetSubInsertTree(Expression expression)
|
||||||
|
{
|
||||||
|
List<SubInsertTreeExpression> resul = new List<SubInsertTreeExpression>();
|
||||||
|
|
||||||
|
if (expression is ListInitExpression)
|
||||||
|
{
|
||||||
|
ListInitExpression exp = expression as ListInitExpression;
|
||||||
|
foreach (var item in exp.Initializers)
|
||||||
|
{
|
||||||
|
SubInsertTreeExpression tree = new SubInsertTreeExpression();
|
||||||
|
var memInit = item.Arguments[0] as MemberInitExpression;
|
||||||
|
if (memInit.Bindings != null)
|
||||||
|
{
|
||||||
|
MemberAssignment memberAssignment = (MemberAssignment)memInit.Bindings[0];
|
||||||
|
tree.Expression = memberAssignment.Expression;
|
||||||
|
}
|
||||||
|
resul.Add(tree);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
return resul;
|
||||||
|
}
|
||||||
|
|
||||||
public object ExecuteReturnPrimaryKey()
|
public object ExecuteReturnPrimaryKey()
|
||||||
{
|
{
|
||||||
|
var isNoTrean = this.Context.Ado.Transaction == null;
|
||||||
if (InsertObjects != null && InsertObjects.Count()>0)
|
try
|
||||||
{
|
{
|
||||||
int count = 1;
|
if (isNoTrean)
|
||||||
foreach (var InsertObject in InsertObjects)
|
this.Context.Ado.BeginTran();
|
||||||
{
|
|
||||||
List<ConditionalModel> conModel = new List<ConditionalModel>();
|
|
||||||
int id = this.Context.Insertable(InsertObject).ExecuteReturnIdentity();
|
|
||||||
object pkValue = null;
|
|
||||||
var qureyable = this.Context.Queryable<T>();
|
|
||||||
if (id.ObjToInt() == 0)
|
|
||||||
{
|
|
||||||
var primaryProperty = this.Entity.Columns.FirstOrDefault(it =>
|
|
||||||
it.PropertyName.Equals(this.Pk, StringComparison.CurrentCultureIgnoreCase) ||
|
|
||||||
it.DbColumnName.Equals(this.Pk, StringComparison.CurrentCultureIgnoreCase)
|
|
||||||
);
|
|
||||||
pkValue = primaryProperty.PropertyInfo.GetValue(InsertObject);
|
|
||||||
qureyable.In(pkValue);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
qureyable.In(id);
|
|
||||||
pkValue = id;
|
|
||||||
}
|
|
||||||
var data = qureyable.First();
|
|
||||||
foreach (var item in this.SubList)
|
|
||||||
{
|
|
||||||
|
|
||||||
Dictionary<string, object> insertDictionary = new Dictionary<string, object>();
|
var result = Execute();
|
||||||
if (item.Value == null)
|
|
||||||
{
|
if (isNoTrean)
|
||||||
continue;
|
this.Context.Ado.CommitTran();
|
||||||
}
|
return result;
|
||||||
EntityInfo subEntity = null;
|
|
||||||
if (item.Value is IEnumerable<object>)
|
|
||||||
{
|
|
||||||
var list = item.Value as IEnumerable<object>;
|
|
||||||
if (list.Count() == 0)
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
var type = list.First().GetType();
|
|
||||||
this.Context.InitMappingInfo(type);
|
|
||||||
subEntity = this.Context.EntityMaintenance.GetEntityInfo(type);
|
|
||||||
foreach (var sbItem in list)
|
|
||||||
{
|
|
||||||
SetItems(insertDictionary, sbItem, subEntity, item.Key, pkValue);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (item.Value.GetType().IsClass())
|
|
||||||
{
|
|
||||||
var type = item.Value.GetType();
|
|
||||||
this.Context.InitMappingInfo(type);
|
|
||||||
subEntity = this.Context.EntityMaintenance.GetEntityInfo(type);
|
|
||||||
SetItems(insertDictionary, item.Value, subEntity, item.Key, pkValue);
|
|
||||||
}
|
|
||||||
count += this.Context.Insertable(insertDictionary).AS(subEntity.DbTableName).ExecuteCommand();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return count;
|
|
||||||
}
|
}
|
||||||
else
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
return 0;
|
if (isNoTrean)
|
||||||
|
this.Context.Ado.RollbackTran();
|
||||||
|
throw ex;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
public async Task<object> ExecuteReturnPrimaryKeyAsync()
|
|
||||||
{
|
|
||||||
|
|
||||||
|
private object Execute()
|
||||||
|
{
|
||||||
if (InsertObjects != null && InsertObjects.Count() > 0)
|
if (InsertObjects != null && InsertObjects.Count() > 0)
|
||||||
{
|
{
|
||||||
int count = 1;
|
var isIdEntity = IsIdEntity(this.Entity);
|
||||||
|
if (!isIdEntity)
|
||||||
|
{
|
||||||
|
this.Context.Insertable(InsertObjects).ExecuteCommand();
|
||||||
|
}
|
||||||
foreach (var InsertObject in InsertObjects)
|
foreach (var InsertObject in InsertObjects)
|
||||||
{
|
{
|
||||||
List<ConditionalModel> conModel = new List<ConditionalModel>();
|
int id = 0;
|
||||||
int id = await this.Context.Insertable(InsertObject).ExecuteReturnIdentityAsync();
|
if (isIdEntity)
|
||||||
object pkValue = null;
|
|
||||||
var qureyable = this.Context.Queryable<T>();
|
|
||||||
if (id.ObjToInt() == 0)
|
|
||||||
{
|
{
|
||||||
var primaryProperty = this.Entity.Columns.FirstOrDefault(it =>
|
id = this.Context.Insertable(InsertObject).ExecuteReturnIdentity();
|
||||||
it.PropertyName.Equals(this.Pk, StringComparison.CurrentCultureIgnoreCase) ||
|
|
||||||
it.DbColumnName.Equals(this.Pk, StringComparison.CurrentCultureIgnoreCase)
|
|
||||||
);
|
|
||||||
pkValue = primaryProperty.PropertyInfo.GetValue(InsertObject);
|
|
||||||
qureyable.In(pkValue);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
qureyable.In(id);
|
|
||||||
pkValue = id;
|
|
||||||
}
|
|
||||||
var data =await qureyable.FirstAsync();
|
|
||||||
foreach (var item in this.SubList)
|
|
||||||
{
|
|
||||||
|
|
||||||
Dictionary<string, object> insertDictionary = new Dictionary<string, object>();
|
|
||||||
if (item.Value == null)
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
EntityInfo subEntity = null;
|
|
||||||
if (item.Value is IEnumerable<object>)
|
|
||||||
{
|
|
||||||
var list = item.Value as IEnumerable<object>;
|
|
||||||
if (list.Count() == 0)
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
var type = list.First().GetType();
|
|
||||||
this.Context.InitMappingInfo(type);
|
|
||||||
subEntity = this.Context.EntityMaintenance.GetEntityInfo(type);
|
|
||||||
foreach (var sbItem in list)
|
|
||||||
{
|
|
||||||
SetItems(insertDictionary, sbItem, subEntity, item.Key, pkValue);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (item.Value.GetType().IsClass())
|
|
||||||
{
|
|
||||||
var type = item.Value.GetType();
|
|
||||||
this.Context.InitMappingInfo(type);
|
|
||||||
subEntity = this.Context.EntityMaintenance.GetEntityInfo(type);
|
|
||||||
SetItems(insertDictionary, item.Value, subEntity, item.Key, pkValue);
|
|
||||||
}
|
|
||||||
count +=await this.Context.Insertable(insertDictionary).AS(subEntity.DbTableName).ExecuteCommandAsync();
|
|
||||||
}
|
}
|
||||||
|
var pk = GetPrimaryKey(this.Entity, InsertObject, id);
|
||||||
|
AddChildList(this.SubList, InsertObject, pk);
|
||||||
}
|
}
|
||||||
return count;
|
return InsertObjects.Count();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
public void GetList(T[] inserts,Expression<Func<T, object>> items, out string subMemberName, out object sublist)
|
|
||||||
|
public Task<object> ExecuteReturnPrimaryKeyAsync()
|
||||||
{
|
{
|
||||||
var lambdaExpression = (items as LambdaExpression).Body;
|
return Task.FromResult(ExecuteReturnPrimaryKey());
|
||||||
if (lambdaExpression is UnaryExpression)
|
}
|
||||||
|
|
||||||
|
private bool IsIdEntity(EntityInfo entity)
|
||||||
|
{
|
||||||
|
return entity.Columns.Where(it => it.IsIdentity||it.OracleSequenceName.HasValue()).Count() > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void AddChildList(List<SubInsertTreeExpression> items, object insertObject, object pkValue)
|
||||||
|
{
|
||||||
|
if (items != null)
|
||||||
{
|
{
|
||||||
lambdaExpression = (lambdaExpression as UnaryExpression).Operand;
|
foreach (var item in items)
|
||||||
|
{
|
||||||
|
MemberExpression subMemberException;
|
||||||
|
string subMemberName = GetMemberName(item, out subMemberException);
|
||||||
|
string childName = GetChildName(item, subMemberException);
|
||||||
|
var childListProperty = insertObject.GetType().GetProperty(childName);
|
||||||
|
if (childListProperty == null)
|
||||||
|
{
|
||||||
|
childName = subMemberName;
|
||||||
|
childListProperty = insertObject.GetType().GetProperty(childName);
|
||||||
|
}
|
||||||
|
var childList = childListProperty.GetValue(insertObject);
|
||||||
|
if (childList != null)
|
||||||
|
{
|
||||||
|
if (!(childList is IEnumerable<object>))
|
||||||
|
{
|
||||||
|
childList = new List<object>() { childList };
|
||||||
|
}
|
||||||
|
if (!string.IsNullOrEmpty(subMemberName) &&subMemberName!=childName)
|
||||||
|
{
|
||||||
|
foreach (var child in childList as IEnumerable<object>)
|
||||||
|
{
|
||||||
|
child.GetType().GetProperty(subMemberName).SetValue(child, pkValue);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var type = (childList as IEnumerable<object>).First().GetType();
|
||||||
|
this.Context.InitMappingInfo(type);
|
||||||
|
var entityInfo = this.Context.EntityMaintenance.GetEntityInfo(type);
|
||||||
|
var isIdentity = IsIdEntity(entityInfo);
|
||||||
|
var tableName = entityInfo.DbTableName;
|
||||||
|
List<Dictionary<string, object>> insertList = new List<Dictionary<string, object>>();
|
||||||
|
var entityList = (childList as IEnumerable<object>).ToList();
|
||||||
|
foreach (var child in entityList)
|
||||||
|
{
|
||||||
|
insertList.Add(GetInsertDictionary(child, entityInfo));
|
||||||
|
}
|
||||||
|
if (!isIdentity)
|
||||||
|
{
|
||||||
|
this.Context.Insertable(insertList).AS(tableName).ExecuteCommand();
|
||||||
|
}
|
||||||
|
int i = 0;
|
||||||
|
foreach (var insert in insertList)
|
||||||
|
{
|
||||||
|
int id = 0;
|
||||||
|
if (isIdentity)
|
||||||
|
{
|
||||||
|
id = this.Context.Insertable(insert).AS(tableName).ExecuteReturnIdentity();
|
||||||
|
if (this.Context.CurrentConnectionConfig.DbType == DbType.Oracle&&id==0)
|
||||||
|
{
|
||||||
|
var seqName=entityInfo.Columns.First(it => it.OracleSequenceName.HasValue())?.OracleSequenceName;
|
||||||
|
id = this.Context.Ado.GetInt("select "+seqName+".currval from dual");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var entity = entityList[i];
|
||||||
|
var pk = GetPrimaryKey(entityInfo,entity, id);
|
||||||
|
AddChildList(item.Childs, entity, pk);
|
||||||
|
++i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
MemberExpression subMemberException = lambdaExpression as MemberExpression;
|
}
|
||||||
subMemberName = subMemberException.Member.Name;
|
private Dictionary<string, object> GetInsertDictionary(object insetObject, EntityInfo subEntity)
|
||||||
|
{
|
||||||
|
Dictionary<string, object> insertDictionary = new Dictionary<string, object>();
|
||||||
|
foreach (var item in subEntity.Columns)
|
||||||
|
{
|
||||||
|
if (item.IsIdentity || item.IsIgnore)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
else if (!string.IsNullOrEmpty(item.OracleSequenceName) && this.Context.CurrentConnectionConfig.DbType == DbType.Oracle)
|
||||||
|
{
|
||||||
|
var value = "{SugarSeq:=}" + item.OracleSequenceName + ".nextval{SugarSeq:=}";
|
||||||
|
insertDictionary.Add(item.DbColumnName, value);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
insertDictionary.Add(item.DbColumnName, item.PropertyInfo.GetValue(insetObject));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return insertDictionary;
|
||||||
|
}
|
||||||
|
private static string GetChildName(SubInsertTreeExpression item, MemberExpression subMemberException)
|
||||||
|
{
|
||||||
|
string childName;
|
||||||
MemberExpression listMember = null;
|
MemberExpression listMember = null;
|
||||||
sublist = null;
|
|
||||||
if (subMemberException.Expression is MethodCallExpression)
|
if (subMemberException.Expression is MethodCallExpression)
|
||||||
{
|
{
|
||||||
listMember = (subMemberException.Expression as MethodCallExpression).Arguments.First() as MemberExpression;
|
listMember = (subMemberException.Expression as MethodCallExpression).Arguments.First() as MemberExpression;
|
||||||
@ -189,34 +233,54 @@ namespace SqlSugar
|
|||||||
{
|
{
|
||||||
listMember = (subMemberException.Expression as MemberExpression);
|
listMember = (subMemberException.Expression as MemberExpression);
|
||||||
}
|
}
|
||||||
if (listMember == null)
|
if (listMember == null&& item.Expression is LambdaExpression)
|
||||||
{
|
{
|
||||||
listMember = (items as LambdaExpression).Body as MemberExpression;
|
listMember = (item.Expression as LambdaExpression).Body as MemberExpression;
|
||||||
subMemberName = Guid.NewGuid().ToString();
|
|
||||||
}
|
}
|
||||||
sublist = inserts.First().GetType().GetProperty(listMember.Member.Name).GetValue(inserts.First());
|
if (listMember == null && item.Expression is MemberExpression)
|
||||||
|
{
|
||||||
|
listMember = item.Expression as MemberExpression;
|
||||||
|
}
|
||||||
|
childName = listMember.Member.Name;
|
||||||
|
return childName;
|
||||||
}
|
}
|
||||||
private void SetItems(Dictionary<string, object> insertDictionary, object sbItem, EntityInfo subEntity,string key,object pkValue)
|
|
||||||
|
private static string GetMemberName(SubInsertTreeExpression item, out MemberExpression subMemberException)
|
||||||
{
|
{
|
||||||
foreach (var item in subEntity.Columns)
|
string subMemberName = null;
|
||||||
|
Expression lambdaExpression;
|
||||||
|
if (item.Expression is LambdaExpression)
|
||||||
{
|
{
|
||||||
if (item.IsIdentity||item.IsIgnore)
|
lambdaExpression = (item.Expression as LambdaExpression).Body;
|
||||||
continue;
|
|
||||||
if (!string.IsNullOrEmpty(item.OracleSequenceName)&&this.Context.CurrentConnectionConfig.DbType==DbType.Oracle)
|
|
||||||
{
|
|
||||||
var value = "{SugarSeq:=}"+item.OracleSequenceName+ ".nextval{SugarSeq:=}";
|
|
||||||
insertDictionary.Add(item.DbColumnName, value);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (item.PropertyInfo.Name == key)
|
|
||||||
{
|
|
||||||
insertDictionary.Add(item.DbColumnName, pkValue);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
insertDictionary.Add(item.DbColumnName, item.PropertyInfo.GetValue(sbItem));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
lambdaExpression = item.Expression;
|
||||||
|
}
|
||||||
|
if (lambdaExpression is UnaryExpression)
|
||||||
|
{
|
||||||
|
lambdaExpression = (lambdaExpression as UnaryExpression).Operand;
|
||||||
|
}
|
||||||
|
subMemberException = lambdaExpression as MemberExpression;
|
||||||
|
subMemberName = subMemberException.Member.Name;
|
||||||
|
return subMemberName;
|
||||||
|
}
|
||||||
|
|
||||||
|
private object GetPrimaryKey(EntityInfo entityInfo,object InsertObject, int id)
|
||||||
|
{
|
||||||
|
object pkValue;
|
||||||
|
if (id.ObjToInt() == 0)
|
||||||
|
{
|
||||||
|
var primaryProperty = entityInfo.Columns.FirstOrDefault(it => it.IsPrimarykey);
|
||||||
|
Check.Exception(primaryProperty == null, entityInfo.EntityName + " no primarykey");
|
||||||
|
pkValue = primaryProperty.PropertyInfo.GetValue(InsertObject);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
pkValue = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
return pkValue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -716,6 +716,23 @@ namespace SqlSugar
|
|||||||
{
|
{
|
||||||
return this.Context.Utilities.SerializeObject(this.ToPageList(pageIndex, pageSize, ref totalNumber), typeof(T));
|
return this.Context.Utilities.SerializeObject(this.ToPageList(pageIndex, pageSize, ref totalNumber), typeof(T));
|
||||||
}
|
}
|
||||||
|
public List<T> ToTree(Expression<Func<T, IEnumerable<object>>> childListExpression, Expression<Func<T, object>> parentIdExpression, object rootValue)
|
||||||
|
{
|
||||||
|
var entity = this.Context.EntityMaintenance.GetEntityInfo<T>();
|
||||||
|
Check.Exception(entity.Columns.Where(it => it.IsPrimarykey).Count() == 0, "No Primary key");
|
||||||
|
var pk = entity.Columns.Where(it => it.IsPrimarykey).First().PropertyName;
|
||||||
|
var list = this.ToList();
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
var entity = this.Context.EntityMaintenance.GetEntityInfo<T>();
|
||||||
|
Check.Exception(entity.Columns.Where(it => it.IsPrimarykey).Count() == 0, "No Primary key");
|
||||||
|
var pk = entity.Columns.Where(it => it.IsPrimarykey).First().PropertyName;
|
||||||
|
var list =await this.ToListAsync();
|
||||||
|
return GetTreeRoot(childListExpression, parentIdExpression, pk, list,rootValue);
|
||||||
|
}
|
||||||
|
|
||||||
public virtual DataTable ToDataTable()
|
public virtual DataTable ToDataTable()
|
||||||
{
|
{
|
||||||
@ -1082,6 +1099,63 @@ namespace SqlSugar
|
|||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Private Methods
|
#region Private Methods
|
||||||
|
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;
|
||||||
|
var exp = (parentIdExpression as LambdaExpression).Body;
|
||||||
|
if (exp is UnaryExpression)
|
||||||
|
{
|
||||||
|
exp = (exp as UnaryExpression).Operand;
|
||||||
|
}
|
||||||
|
var parentIdName = (exp as MemberExpression).Member.Name;
|
||||||
|
var result = list.Where(it =>
|
||||||
|
{
|
||||||
|
|
||||||
|
var value = it.GetType().GetProperty(parentIdName).GetValue(it);
|
||||||
|
if (rootValue != null)
|
||||||
|
{
|
||||||
|
return value.ObjToString() == rootValue.ObjToString();
|
||||||
|
}
|
||||||
|
else if (value == null || value.ObjToString() == "" || value.ObjToString() == "0" || value.ObjToString() == Guid.Empty.ToString())
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}).ToList();
|
||||||
|
if (result != null && result.Count > 0)
|
||||||
|
{
|
||||||
|
foreach (var item in result)
|
||||||
|
{
|
||||||
|
var pkValue = item.GetType().GetProperty(pk).GetValue(item);
|
||||||
|
item.GetType().GetProperty(childName).SetValue(item, GetTreeChildList(list, pkValue, pk, childName, parentIdName));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<T> GetTreeChildList(List<T> alllist, object pkValue, string pkName, string childName, string parentIdName)
|
||||||
|
{
|
||||||
|
var result = alllist.Where(it =>
|
||||||
|
{
|
||||||
|
|
||||||
|
var value = it.GetType().GetProperty(parentIdName).GetValue(it);
|
||||||
|
return value.ObjToString() == pkValue.ObjToString();
|
||||||
|
|
||||||
|
}).ToList();
|
||||||
|
if (result != null && result.Count > 0)
|
||||||
|
{
|
||||||
|
foreach (var item in result)
|
||||||
|
{
|
||||||
|
var itemPkValue = item.GetType().GetProperty(pkName).GetValue(item);
|
||||||
|
item.GetType().GetProperty(childName).SetValue(item, GetTreeChildList(alllist, itemPkValue, pkName, childName, parentIdName));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
private void _CountEnd(MappingTableList expMapping)
|
private void _CountEnd(MappingTableList expMapping)
|
||||||
{
|
{
|
||||||
RestoreMapping();
|
RestoreMapping();
|
||||||
|
@ -208,6 +208,17 @@ namespace SqlSugar
|
|||||||
parameters.Add(new SugarParameter(parameterName, item.FieldValue));
|
parameters.Add(new SugarParameter(parameterName, item.FieldValue));
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
case ConditionalType.EqualNull:
|
||||||
|
if (GetFieldValue(item) == null)
|
||||||
|
{
|
||||||
|
builder.AppendFormat(temp, type, item.FieldName.ToSqlFilter(), " IS ", " NULL ");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
builder.AppendFormat(temp, type, item.FieldName.ToSqlFilter(), "=", parameterName);
|
||||||
|
parameters.Add(new SugarParameter(parameterName, GetFieldValue(item)));
|
||||||
|
}
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,21 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace SqlSugar
|
||||||
|
{
|
||||||
|
public class SubInsertTree
|
||||||
|
{
|
||||||
|
public object Expression { get; set; }
|
||||||
|
public List<SubInsertTree> ChildExpression { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
internal class SubInsertTreeExpression
|
||||||
|
{
|
||||||
|
public Expression Expression { get; set; }
|
||||||
|
public List<SubInsertTreeExpression> Childs { get; set; }
|
||||||
|
}
|
||||||
|
}
|
@ -21,5 +21,6 @@ namespace SqlSugar
|
|||||||
IsNullOrEmpty=11,
|
IsNullOrEmpty=11,
|
||||||
IsNot=12,
|
IsNot=12,
|
||||||
NoLike = 13,
|
NoLike = 13,
|
||||||
|
EqualNull = 14,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -26,7 +26,10 @@ namespace SqlSugar
|
|||||||
}
|
}
|
||||||
else if (context.IsSingle)
|
else if (context.IsSingle)
|
||||||
{
|
{
|
||||||
this.context.SingleTableNameSubqueryShortName = (context.Expression as LambdaExpression).Parameters.First().Name;
|
if ((context.Expression as LambdaExpression) != null)
|
||||||
|
{
|
||||||
|
this.context.SingleTableNameSubqueryShortName = (context.Expression as LambdaExpression).Parameters.First().Name;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
while (currentExpression != null)
|
while (currentExpression != null)
|
||||||
{
|
{
|
||||||
|
@ -343,6 +343,10 @@ namespace SqlSugar
|
|||||||
this.Context.Result.IsLockCurrentParameter = true;
|
this.Context.Result.IsLockCurrentParameter = true;
|
||||||
parameter.IsAppendTempDate();
|
parameter.IsAppendTempDate();
|
||||||
this.Expression = item;
|
this.Expression = item;
|
||||||
|
if (IsBoolValue(item))
|
||||||
|
{
|
||||||
|
this.Expression = (item as MemberExpression).Expression;
|
||||||
|
}
|
||||||
this.Start();
|
this.Start();
|
||||||
parameter.IsAppendResult();
|
parameter.IsAppendResult();
|
||||||
this.Context.Result.Append(this.Context.GetAsString(asName, parameter.CommonTempData.ObjToString()));
|
this.Context.Result.Append(this.Context.GetAsString(asName, parameter.CommonTempData.ObjToString()));
|
||||||
@ -378,7 +382,7 @@ namespace SqlSugar
|
|||||||
this.Context.Result.CurrentParameter = parameter;
|
this.Context.Result.CurrentParameter = parameter;
|
||||||
this.Context.Result.IsLockCurrentParameter = true;
|
this.Context.Result.IsLockCurrentParameter = true;
|
||||||
parameter.IsAppendTempDate();
|
parameter.IsAppendTempDate();
|
||||||
this.Expression = item;
|
this.Expression = expression;
|
||||||
this.Start();
|
this.Start();
|
||||||
parameter.IsAppendResult();
|
parameter.IsAppendResult();
|
||||||
this.Context.Result.Append(this.Context.GetAsString(asName, parameter.CommonTempData.ObjToString()));
|
this.Context.Result.Append(this.Context.GetAsString(asName, parameter.CommonTempData.ObjToString()));
|
||||||
@ -491,6 +495,16 @@ namespace SqlSugar
|
|||||||
Check.ThrowNotSupportedException(item.GetType().Name);
|
Check.ThrowNotSupportedException(item.GetType().Name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static bool IsBoolValue(Expression item)
|
||||||
|
{
|
||||||
|
return item.Type == UtilConstants.BoolType &&
|
||||||
|
(item is MemberExpression) &&
|
||||||
|
(item as MemberExpression).Expression != null &&
|
||||||
|
(item as MemberExpression).Expression.Type == typeof(bool?) &&
|
||||||
|
(item as MemberExpression).Member.Name == "Value";
|
||||||
|
}
|
||||||
|
|
||||||
protected static bool IsConvert(Expression item)
|
protected static bool IsConvert(Expression item)
|
||||||
{
|
{
|
||||||
return item is UnaryExpression && item.NodeType == ExpressionType.Convert;
|
return item is UnaryExpression && item.NodeType == ExpressionType.Convert;
|
||||||
|
@ -100,9 +100,9 @@ namespace SqlSugar
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
parameter.Context.Result.Append(base.Context.GetEqString(memberName,AppendParameter(ExpressionTool.DynamicInvoke(item).ObjToBool())));
|
parameter.Context.Result.Append(base.Context.GetEqString(memberName, AppendParameter(ExpressionTool.DynamicInvoke(item).ObjToBool())));
|
||||||
}
|
}
|
||||||
catch
|
catch
|
||||||
{
|
{
|
||||||
throw new NotSupportedException(item.ToString());
|
throw new NotSupportedException(item.ToString());
|
||||||
}
|
}
|
||||||
@ -123,7 +123,7 @@ namespace SqlSugar
|
|||||||
this.Context.Result.Append(base.Context.GetEqString(memberName, parameterName));
|
this.Context.Result.Append(base.Context.GetEqString(memberName, parameterName));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (IsConst(item)&&IsConvert(item)&&UtilMethods.IsNullable(item.Type) && UtilMethods.GetUnderType(item.Type)==UtilConstants.BoolType)
|
else if (IsConst(item) && IsConvert(item) && UtilMethods.IsNullable(item.Type) && UtilMethods.GetUnderType(item.Type) == UtilConstants.BoolType)
|
||||||
{
|
{
|
||||||
item = (item as UnaryExpression).Operand;
|
item = (item as UnaryExpression).Operand;
|
||||||
parameter.Context.Result.Append(base.Context.GetEqString(memberName, GetNewExpressionValue(item)));
|
parameter.Context.Result.Append(base.Context.GetEqString(memberName, GetNewExpressionValue(item)));
|
||||||
@ -204,7 +204,8 @@ namespace SqlSugar
|
|||||||
base.Expression = item;
|
base.Expression = item;
|
||||||
base.Start();
|
base.Start();
|
||||||
var subSql = base.Context.GetEqString(memberName, parameter.CommonTempData.ObjToString());
|
var subSql = base.Context.GetEqString(memberName, parameter.CommonTempData.ObjToString());
|
||||||
if (subSql.Contains(",")) {
|
if (subSql.Contains(","))
|
||||||
|
{
|
||||||
subSql = subSql.Replace(",", UtilConstants.ReplaceCommaKey);
|
subSql = subSql.Replace(",", UtilConstants.ReplaceCommaKey);
|
||||||
}
|
}
|
||||||
if (ResolveExpressType.Update == this.Context.ResolveType)
|
if (ResolveExpressType.Update == this.Context.ResolveType)
|
||||||
@ -220,7 +221,7 @@ namespace SqlSugar
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
parameter.Context.Result.Append(subSql);
|
parameter.Context.Result.Append(subSql);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -133,7 +133,8 @@ namespace SqlSugar
|
|||||||
Task<string> ToJsonPageAsync(int pageIndex, int pageSize, RefAsync<int> totalNumber);
|
Task<string> ToJsonPageAsync(int pageIndex, int pageSize, RefAsync<int> totalNumber);
|
||||||
KeyValuePair<string, List<SugarParameter>> ToSql();
|
KeyValuePair<string, List<SugarParameter>> ToSql();
|
||||||
|
|
||||||
|
List<T> ToTree(Expression<Func<T,IEnumerable<object>>> childListExpression, Expression<Func<T,object>> parentIdExpression,object rootValue);
|
||||||
|
Task<List<T>> ToTreeAsync(Expression<Func<T, IEnumerable<object>>> childListExpression, Expression<Func<T, object>> parentIdExpression, object rootValue);
|
||||||
DataTable ToDataTable();
|
DataTable ToDataTable();
|
||||||
Task<DataTable> ToDataTableAsync();
|
Task<DataTable> ToDataTableAsync();
|
||||||
DataTable ToDataTablePage(int pageIndex, int pageSize);
|
DataTable ToDataTablePage(int pageIndex, int pageSize);
|
||||||
|
@ -6,6 +6,7 @@ namespace SqlSugar
|
|||||||
public interface ISubInsertable<T>
|
public interface ISubInsertable<T>
|
||||||
{
|
{
|
||||||
ISubInsertable<T> AddSubList(Expression<Func<T, object>> items);
|
ISubInsertable<T> AddSubList(Expression<Func<T, object>> items);
|
||||||
|
ISubInsertable<T> AddSubList(Expression<Func<T, SubInsertTree>> tree);
|
||||||
object ExecuteReturnPrimaryKey();
|
object ExecuteReturnPrimaryKey();
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -29,7 +29,8 @@ namespace SqlSugar
|
|||||||
IInsertable<T> IgnoreColumns(params string[]columns);
|
IInsertable<T> IgnoreColumns(params string[]columns);
|
||||||
IInsertable<T> IgnoreColumns(bool ignoreNullColumn, bool isOffIdentity = false);
|
IInsertable<T> IgnoreColumns(bool ignoreNullColumn, bool isOffIdentity = false);
|
||||||
|
|
||||||
ISubInsertable<T> AddSubList(Expression<Func<T, object>> SubForeignKey);
|
ISubInsertable<T> AddSubList(Expression<Func<T, object>> subForeignKey);
|
||||||
|
ISubInsertable<T> AddSubList(Expression<Func<T, SubInsertTree>> tree);
|
||||||
|
|
||||||
IInsertable<T> EnableDiffLogEvent(object businessData = null);
|
IInsertable<T> EnableDiffLogEvent(object businessData = null);
|
||||||
IInsertable<T> RemoveDataCache();
|
IInsertable<T> RemoveDataCache();
|
||||||
|
@ -79,6 +79,7 @@ namespace SqlSugar
|
|||||||
|
|
||||||
new KeyValuePair<string, CSharpDataType>("varchar",CSharpDataType.@string),
|
new KeyValuePair<string, CSharpDataType>("varchar",CSharpDataType.@string),
|
||||||
new KeyValuePair<string, CSharpDataType>("character varying",CSharpDataType.@string),
|
new KeyValuePair<string, CSharpDataType>("character varying",CSharpDataType.@string),
|
||||||
|
new KeyValuePair<string, CSharpDataType>("geometry",CSharpDataType.@string),
|
||||||
new KeyValuePair<string, CSharpDataType>("name",CSharpDataType.@string),
|
new KeyValuePair<string, CSharpDataType>("name",CSharpDataType.@string),
|
||||||
new KeyValuePair<string, CSharpDataType>("text",CSharpDataType.@string),
|
new KeyValuePair<string, CSharpDataType>("text",CSharpDataType.@string),
|
||||||
new KeyValuePair<string, CSharpDataType>("char",CSharpDataType.@string),
|
new KeyValuePair<string, CSharpDataType>("char",CSharpDataType.@string),
|
||||||
|
Loading…
Reference in New Issue
Block a user