mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-04-24 18:04:52 +08:00
Update Core
This commit is contained in:
parent
319c6e62f1
commit
3288457cee
@ -280,6 +280,7 @@ namespace SqlSugar
|
||||
column.UIndexGroupNameList = sugarColumn.UniqueGroupNameList;
|
||||
column.IsOnlyIgnoreUpdate = sugarColumn.IsOnlyIgnoreUpdate;
|
||||
column.IsArray = sugarColumn.IsArray;
|
||||
column.IsTreeKey = sugarColumn.IsTreeKey;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -1047,7 +1047,8 @@ namespace SqlSugar
|
||||
if (this.QueryBuilder.Skip == null&&
|
||||
this.QueryBuilder.Take == null&&
|
||||
this.QueryBuilder.OrderByValue == null &&
|
||||
this.QueryBuilder.PartitionByValue == null)
|
||||
this.QueryBuilder.PartitionByValue == null&&
|
||||
this.QueryBuilder.SelectValue==null)
|
||||
{
|
||||
|
||||
return this.Clone().Select<int>(" COUNT(1) ").ToList().First();
|
||||
@ -1177,23 +1178,26 @@ namespace SqlSugar
|
||||
public List<T> ToChildList(Expression<Func<T, object>> parentIdExpression, object primaryKeyValue)
|
||||
{
|
||||
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 pk = GetTreeKey(entity);
|
||||
var list = this.ToList();
|
||||
return GetChildList(parentIdExpression, pk, list, primaryKeyValue);
|
||||
}
|
||||
public async Task<List<T>> ToChildListAsync(Expression<Func<T, object>> parentIdExpression, object primaryKeyValue)
|
||||
{
|
||||
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 pk = GetTreeKey(entity);
|
||||
var list = await this.ToListAsync();
|
||||
return GetChildList(parentIdExpression,pk,list, primaryKeyValue);
|
||||
}
|
||||
public List<T> ToParentList(Expression<Func<T, object>> parentIdExpression, object primaryKeyValue)
|
||||
{
|
||||
List<T> result = new List<T>() { };
|
||||
var entity = this.Context.EntityMaintenance.GetEntityInfo<T>();
|
||||
var isTreeKey = entity.Columns.Any(it => it.IsTreeKey);
|
||||
if (isTreeKey)
|
||||
{
|
||||
return _ToParentListByTreeKey(parentIdExpression,primaryKeyValue);
|
||||
}
|
||||
List<T> result = new List<T>() { };
|
||||
Check.Exception(entity.Columns.Where(it => it.IsPrimarykey).Count() == 0, "No Primary key");
|
||||
var parentIdName =UtilConvert.ToMemberExpression((parentIdExpression as LambdaExpression).Body).Member.Name;
|
||||
var ParentInfo = entity.Columns.First(it => it.PropertyName == parentIdName);
|
||||
@ -1227,10 +1231,75 @@ namespace SqlSugar
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private List<T> _ToParentListByTreeKey(Expression<Func<T, object>> parentIdExpression, object primaryKeyValue)
|
||||
{
|
||||
var entity = this.Context.EntityMaintenance.GetEntityInfo<T>();
|
||||
var treeKey = entity.Columns.FirstOrDefault(it => it.IsTreeKey);
|
||||
List<T> result = new List<T>() { };
|
||||
var parentIdName = UtilConvert.ToMemberExpression((parentIdExpression as LambdaExpression).Body).Member.Name;
|
||||
var ParentInfo = entity.Columns.First(it => it.PropertyName == parentIdName);
|
||||
var parentPropertyName = ParentInfo.DbColumnName;
|
||||
var tableName = this.QueryBuilder.GetTableNameString;
|
||||
if (this.QueryBuilder.IsSingle() == false)
|
||||
{
|
||||
if (this.QueryBuilder.JoinQueryInfos.Count > 0)
|
||||
{
|
||||
tableName = this.QueryBuilder.JoinQueryInfos.First().TableName;
|
||||
}
|
||||
if (this.QueryBuilder.EasyJoinInfos.Count > 0)
|
||||
{
|
||||
tableName = this.QueryBuilder.JoinQueryInfos.First().TableName;
|
||||
}
|
||||
}
|
||||
var current = this.Context.Queryable<T>().AS(tableName).Where(new List<IConditionalModel>() {
|
||||
new ConditionalModel()
|
||||
{
|
||||
ConditionalType = ConditionalType.Equal,
|
||||
CSharpTypeName = treeKey.PropertyInfo.PropertyType.Name,
|
||||
FieldValue = primaryKeyValue + "",
|
||||
FieldName = treeKey.DbColumnName
|
||||
} }).First();
|
||||
if (current != null)
|
||||
{
|
||||
result.Add(current);
|
||||
object parentId = ParentInfo.PropertyInfo.GetValue(current, null);
|
||||
int i = 0;
|
||||
while (parentId != null && this.Context.Queryable<T>().AS(tableName).Where(new List<IConditionalModel>() {
|
||||
new ConditionalModel()
|
||||
{
|
||||
ConditionalType = ConditionalType.Equal,
|
||||
CSharpTypeName = treeKey.PropertyInfo.PropertyType.Name,
|
||||
FieldValue = parentId + "",
|
||||
FieldName = treeKey.DbColumnName
|
||||
} }).Any())
|
||||
{
|
||||
Check.Exception(i > 100, ErrorMessage.GetThrowMessage("Dead cycle", "出现死循环或超出循环上限(100),检查最顶层的ParentId是否是null或者0"));
|
||||
var parent = this.Context.Queryable<T>().AS(tableName).Where(new List<IConditionalModel>() {
|
||||
new ConditionalModel()
|
||||
{
|
||||
ConditionalType = ConditionalType.Equal,
|
||||
CSharpTypeName = treeKey.PropertyInfo.PropertyType.Name,
|
||||
FieldValue = parentId + "",
|
||||
FieldName = treeKey.DbColumnName
|
||||
} }).First();
|
||||
result.Add(parent);
|
||||
parentId = ParentInfo.PropertyInfo.GetValue(parent, null);
|
||||
++i;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public async Task<List<T>> ToParentListAsync(Expression<Func<T, object>> parentIdExpression, object primaryKeyValue)
|
||||
{
|
||||
List<T> result = new List<T>() { };
|
||||
var entity = this.Context.EntityMaintenance.GetEntityInfo<T>();
|
||||
var isTreeKey = entity.Columns.Any(it => it.IsTreeKey);
|
||||
if (isTreeKey)
|
||||
{
|
||||
return await _ToParentListByTreeKeyAsync(parentIdExpression, primaryKeyValue);
|
||||
}
|
||||
Check.Exception(entity.Columns.Where(it => it.IsPrimarykey).Count() == 0, "No Primary key");
|
||||
var parentIdName = UtilConvert.ToMemberExpression((parentIdExpression as LambdaExpression).Body).Member.Name;
|
||||
var ParentInfo = entity.Columns.First(it => it.PropertyName == parentIdName);
|
||||
@ -1264,11 +1333,69 @@ namespace SqlSugar
|
||||
}
|
||||
return result;
|
||||
}
|
||||
private async Task<List<T>> _ToParentListByTreeKeyAsync(Expression<Func<T, object>> parentIdExpression, object primaryKeyValue)
|
||||
{
|
||||
var entity = this.Context.EntityMaintenance.GetEntityInfo<T>();
|
||||
var treeKey = entity.Columns.FirstOrDefault(it => it.IsTreeKey);
|
||||
List<T> result = new List<T>() { };
|
||||
var parentIdName = UtilConvert.ToMemberExpression((parentIdExpression as LambdaExpression).Body).Member.Name;
|
||||
var ParentInfo = entity.Columns.First(it => it.PropertyName == parentIdName);
|
||||
var parentPropertyName = ParentInfo.DbColumnName;
|
||||
var tableName = this.QueryBuilder.GetTableNameString;
|
||||
if (this.QueryBuilder.IsSingle() == false)
|
||||
{
|
||||
if (this.QueryBuilder.JoinQueryInfos.Count > 0)
|
||||
{
|
||||
tableName = this.QueryBuilder.JoinQueryInfos.First().TableName;
|
||||
}
|
||||
if (this.QueryBuilder.EasyJoinInfos.Count > 0)
|
||||
{
|
||||
tableName = this.QueryBuilder.JoinQueryInfos.First().TableName;
|
||||
}
|
||||
}
|
||||
var current = await this.Context.Queryable<T>().AS(tableName).Where(new List<IConditionalModel>() {
|
||||
new ConditionalModel()
|
||||
{
|
||||
ConditionalType = ConditionalType.Equal,
|
||||
CSharpTypeName = treeKey.PropertyInfo.PropertyType.Name,
|
||||
FieldValue = primaryKeyValue + "",
|
||||
FieldName = treeKey.DbColumnName
|
||||
} }).FirstAsync();
|
||||
if (current != null)
|
||||
{
|
||||
result.Add(current);
|
||||
object parentId = ParentInfo.PropertyInfo.GetValue(current, null);
|
||||
int i = 0;
|
||||
while (parentId != null && await this.Context.Queryable<T>().AS(tableName).Where(new List<IConditionalModel>() {
|
||||
new ConditionalModel()
|
||||
{
|
||||
ConditionalType = ConditionalType.Equal,
|
||||
CSharpTypeName = treeKey.PropertyInfo.PropertyType.Name,
|
||||
FieldValue = parentId + "",
|
||||
FieldName = treeKey.DbColumnName
|
||||
} }).AnyAsync())
|
||||
{
|
||||
Check.Exception(i > 100, ErrorMessage.GetThrowMessage("Dead cycle", "出现死循环或超出循环上限(100),检查最顶层的ParentId是否是null或者0"));
|
||||
var parent = await this.Context.Queryable<T>().AS(tableName).Where(new List<IConditionalModel>() {
|
||||
new ConditionalModel()
|
||||
{
|
||||
ConditionalType = ConditionalType.Equal,
|
||||
CSharpTypeName = treeKey.PropertyInfo.PropertyType.Name,
|
||||
FieldValue = parentId + "",
|
||||
FieldName = treeKey.DbColumnName
|
||||
} }).FirstAsync();
|
||||
result.Add(parent);
|
||||
parentId = ParentInfo.PropertyInfo.GetValue(parent, null);
|
||||
++i;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
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 pk = GetTreeKey(entity);
|
||||
var list = this.ToList();
|
||||
return GetTreeRoot(childListExpression, parentIdExpression, pk, list, rootValue);
|
||||
}
|
||||
@ -1276,8 +1403,7 @@ namespace SqlSugar
|
||||
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 pk = GetTreeKey(entity); ;
|
||||
var list =await this.ToListAsync();
|
||||
return GetTreeRoot(childListExpression, parentIdExpression, pk, list,rootValue);
|
||||
}
|
||||
@ -2215,6 +2341,14 @@ namespace SqlSugar
|
||||
QueryBuilder.IsCount = true;
|
||||
result = 0;
|
||||
}
|
||||
private static string GetTreeKey(EntityInfo entity)
|
||||
{
|
||||
Check.Exception(entity.Columns.Where(it => it.IsPrimarykey || it.IsTreeKey).Count() == 0, "need IsPrimary=true Or IsTreeKey=true");
|
||||
string pk = entity.Columns.Where(it => it.IsTreeKey).FirstOrDefault()?.PropertyName;
|
||||
if (pk == null)
|
||||
pk = entity.Columns.Where(it => it.IsPrimarykey).FirstOrDefault()?.PropertyName;
|
||||
return pk;
|
||||
}
|
||||
protected ISugarQueryable<TResult> _Select<TResult>(Expression expression)
|
||||
{
|
||||
QueryBuilder.CheckExpression(expression, "Select");
|
||||
|
@ -19,6 +19,7 @@ namespace SqlSugar
|
||||
public bool IsNullable { get; set; }
|
||||
public bool IsIdentity { get; set; }
|
||||
public bool IsPrimarykey { get; set; }
|
||||
public bool IsTreeKey { get; set; }
|
||||
public bool IsEnableUpdateVersionValidation { get; set; }
|
||||
public string EntityName { get; set; }
|
||||
public string DbTableName { get; set; }
|
||||
|
@ -190,6 +190,12 @@ namespace SqlSugar
|
||||
set { _IsArray = value; }
|
||||
}
|
||||
|
||||
private bool _IsTreeKey;
|
||||
public bool IsTreeKey
|
||||
{
|
||||
get { return _IsTreeKey; }
|
||||
set { _IsTreeKey = value; }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user