Update .net core project

This commit is contained in:
sunkaixuan 2022-08-02 22:57:06 +08:00
parent e9b992673a
commit 7e084cb4bc
9 changed files with 358 additions and 210 deletions

View File

@ -16,7 +16,8 @@ namespace SqlSugar
public EntityInfo _ParentEntity { get; set; } public EntityInfo _ParentEntity { get; set; }
public EntityColumnInfo _ParentPkColumn { get; set; } public EntityColumnInfo _ParentPkColumn { get; set; }
public SqlSugarProvider _Context { get; set; } public SqlSugarProvider _Context { get; set; }
public NavigateType? _NavigateType { get; set; }
public bool IsFirst { get; set; }
public InsertNavProvider<Root, Root> AsNav() public InsertNavProvider<Root, Root> AsNav()
{ {
@ -30,8 +31,13 @@ namespace SqlSugar
} }
public InsertNavProvider<Root, TChild> ThenInclude<TChild>(Expression<Func<T, TChild>> expression) where TChild : class, new() public InsertNavProvider<Root, TChild> ThenInclude<TChild>(Expression<Func<T, TChild>> expression) where TChild : class, new()
{ {
InitParentList();
var name = ExpressionTool.GetMemberName(expression); var name = ExpressionTool.GetMemberName(expression);
if (this._ParentEntity == null)
{
this._ParentEntity = this._Context.EntityMaintenance.GetEntityInfo<Root>();
this.IsFirst = true;
}
var nav = this._ParentEntity.Columns.FirstOrDefault(x => x.PropertyName == name); var nav = this._ParentEntity.Columns.FirstOrDefault(x => x.PropertyName == name);
if (nav.Navigat == null) if (nav.Navigat == null)
{ {
@ -39,37 +45,51 @@ namespace SqlSugar
} }
if (nav.Navigat.NavigatType == NavigateType.OneToOne || nav.Navigat.NavigatType == NavigateType.ManyToOne) if (nav.Navigat.NavigatType == NavigateType.OneToOne || nav.Navigat.NavigatType == NavigateType.ManyToOne)
{ {
InitParentList();
InsertOneToOne<TChild>(name, nav); InsertOneToOne<TChild>(name, nav);
} }
else if (nav.Navigat.NavigatType == NavigateType.OneToMany) else if (nav.Navigat.NavigatType == NavigateType.OneToMany)
{ {
_NavigateType = NavigateType.OneToMany;
InitParentList();
InsertOneToMany<TChild>(name, nav); InsertOneToMany<TChild>(name, nav);
} }
else else
{ {
InitParentList();
InsertManyToMany<TChild>(name, nav); InsertManyToMany<TChild>(name, nav);
} }
return GetResult<TChild>(); return GetResult<TChild>();
} }
public InsertNavProvider<Root, TChild> ThenInclude<TChild>(Expression<Func<T,List<TChild>>> expression) where TChild : class, new() public InsertNavProvider<Root, TChild> ThenInclude<TChild>(Expression<Func<T,List<TChild>>> expression) where TChild : class, new()
{ {
InitParentList();
var name = ExpressionTool.GetMemberName(expression); var name = ExpressionTool.GetMemberName(expression);
if (this._ParentEntity == null)
{
this._ParentEntity = this._Context.EntityMaintenance.GetEntityInfo<Root>();
IsFirst = true;
}
var nav = this._ParentEntity.Columns.FirstOrDefault(x => x.PropertyName == name); 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)
{ {
InitParentList();
InsertOneToOne<TChild>(name, nav); InsertOneToOne<TChild>(name, nav);
} }
else if (nav.Navigat.NavigatType == NavigateType.OneToMany) else if (nav.Navigat.NavigatType == NavigateType.OneToMany)
{ {
_NavigateType = NavigateType.OneToMany;
InitParentList();
InsertOneToMany<TChild>(name, nav); InsertOneToMany<TChild>(name, nav);
} }
else else
{ {
InitParentList();
InsertManyToMany<TChild>(name, nav); InsertManyToMany<TChild>(name, nav);
} }
return GetResult<TChild>(); return GetResult<TChild>();

View File

@ -26,7 +26,7 @@ namespace SqlSugar
var pkColumn = this._Context.EntityMaintenance.GetEntityInfo<T>().Columns.FirstOrDefault(it => it.IsPrimarykey); var pkColumn = this._Context.EntityMaintenance.GetEntityInfo<T>().Columns.FirstOrDefault(it => it.IsPrimarykey);
this._ParentPkColumn = pkColumn; this._ParentPkColumn = pkColumn;
} }
IsFirst = false;
} }
private InsertNavProvider<Root, TChild> GetResult<TChild>() where TChild : class, new() private InsertNavProvider<Root, TChild> GetResult<TChild>() where TChild : class, new()
@ -81,11 +81,26 @@ namespace SqlSugar
children = children.Distinct().ToList(); children = children.Distinct().ToList();
var x = this._Context.Storageable(children).WhereColumns(new string[] { pkColumn.PropertyName }).ToStorage(); var x = this._Context.Storageable(children).WhereColumns(new string[] { pkColumn.PropertyName }).ToStorage();
var insertData = children = x.InsertList.Select(it => it.Item).ToList(); var insertData = children = x.InsertList.Select(it => it.Item).ToList();
if (_NavigateType == NavigateType.OneToMany&&IsFirst==false)
{
var updateData = x.UpdateList.Select(it => it.Item).ToList();
ClearPk(updateData, pkColumn);
insertData.AddRange(updateData);
}
Check.ExceptionEasy(pkColumn==null&&NavColumn==null,$"The entity is invalid",$"实体错误无法使用导航"); Check.ExceptionEasy(pkColumn==null&&NavColumn==null,$"The entity is invalid",$"实体错误无法使用导航");
InitData(pkColumn, insertData); InitData(pkColumn, insertData);
this._ParentList = children.Cast<object>().ToList(); this._ParentList = children.Cast<object>().ToList();
} }
private void ClearPk<TChild>(List<TChild> updateData, EntityColumnInfo pkColumn) where TChild : class, new()
{
foreach (var child in updateData)
{
var defaultValue =UtilMethods.DefaultForType(pkColumn.PropertyInfo.PropertyType);
pkColumn.PropertyInfo.SetValue(child, defaultValue);
}
}
private void InitData<TChild>(EntityColumnInfo pkColumn, List<TChild> insertData) where TChild : class, new() private void InitData<TChild>(EntityColumnInfo pkColumn, List<TChild> insertData) where TChild : class, new()
{ {
if (pkColumn.IsIdentity || pkColumn.OracleSequenceName.HasValue()) if (pkColumn.IsIdentity || pkColumn.OracleSequenceName.HasValue())
@ -128,7 +143,7 @@ namespace SqlSugar
if (IsDefaultValue(pkColumn.PropertyInfo.GetValue(child))) if (IsDefaultValue(pkColumn.PropertyInfo.GetValue(child)))
{ {
var name = pkColumn.EntityName + " " + pkColumn.DbColumnName; var name = pkColumn.EntityName + " " + pkColumn.DbColumnName;
Check.ExceptionEasy($"The field {name} is not an autoassignment type and requires an assignment", $"字段{name}不是可自动赋值类型需要赋值 , 可赋值类型有 自增、long、Guid、string"); Check.ExceptionEasy($"The field {name} is not an autoassignment type and requires an assignment", $"字段{name}不是可自动赋值类型需要赋值(并且不能是已存在值) , 可赋值类型有 自增、long、Guid、string");
} }
} }
this._Context.Insertable(insertData).ExecuteCommand(); this._Context.Insertable(insertData).ExecuteCommand();

View File

@ -382,7 +382,10 @@ namespace SqlSugar
DependencyManagement.TryPostgreSQL(); DependencyManagement.TryPostgreSQL();
break; break;
case DbType.OpenGauss: case DbType.OpenGauss:
Check.ExceptionEasy("Use DbType.PostgreSQL , ConnectionString add No Reset On Close=true", "OpenGausso数据库可以使用DbType.PostgreSQL 并且连接字符串加上 No Reset On Close=true"); Check.ExceptionEasy("Use DbType.PostgreSQL , ConnectionString add No Reset On Close=true", "OpenGausso数据库请使用DbType.PostgreSQL 并且连接字符串加上 No Reset On Close=true");
break;
case DbType.HG:
Check.ExceptionEasy("Use DbType.PostgreSQL", "瀚高数据库请使用DbType.PostgreSQL");
break; break;
case DbType.Kdbndp: case DbType.Kdbndp:
DependencyManagement.TryKdbndb(); DependencyManagement.TryKdbndb();

View File

@ -62,6 +62,7 @@ namespace SqlSugar
public virtual int ExecuteCommandWithOptLock(bool IsVersionValidation=false) public virtual int ExecuteCommandWithOptLock(bool IsVersionValidation=false)
{ {
Check.ExceptionEasy(this.UpdateBuilder.IsListUpdate==true, " OptLock can only be used on a single object, and the argument cannot be List", "乐观锁只能用于单个对象,参数不能是List,如果是一对多操作请更新主表统一用主表验证");
var updateData = UpdateObjs.FirstOrDefault(); var updateData = UpdateObjs.FirstOrDefault();
if (updateData == null) return 0; if (updateData == null) return 0;
var name=_ExecuteCommandWithOptLock(updateData); var name=_ExecuteCommandWithOptLock(updateData);
@ -92,6 +93,7 @@ namespace SqlSugar
public virtual async Task<int> ExecuteCommandWithOptLockAsync(bool IsVersionValidation = false) public virtual async Task<int> ExecuteCommandWithOptLockAsync(bool IsVersionValidation = false)
{ {
Check.ExceptionEasy(this.UpdateBuilder.IsListUpdate == true, " OptLock can only be used on a single object, and the argument cannot be List", "乐观锁只能用于单个对象,参数不能是List,如果是一对多操作请更新主表统一用主表验证");
var updateData = UpdateObjs.FirstOrDefault(); var updateData = UpdateObjs.FirstOrDefault();
if (updateData == null) return 0; if (updateData == null) return 0;
var name=_ExecuteCommandWithOptLock(updateData); var name=_ExecuteCommandWithOptLock(updateData);

View File

@ -19,6 +19,7 @@ namespace SqlSugar
Access, Access,
OpenGauss, OpenGauss,
QuestDB, QuestDB,
HG,
Custom =900 Custom =900
} }
} }

View File

@ -430,5 +430,50 @@ namespace SqlSugar
{ {
return item is UnaryExpression && item.NodeType == ExpressionType.Convert; return item is UnaryExpression && item.NodeType == ExpressionType.Convert;
} }
internal static List<NewExpressionInfo> GetNewexpressionInfos(Expression item,ExpressionContext context)
{
List<NewExpressionInfo> result = new List<NewExpressionInfo>();
foreach (MemberBinding binding in ((MemberInitExpression)item).Bindings)
{
if (binding.BindingType != MemberBindingType.Assignment)
{
throw new NotSupportedException();
}
MemberAssignment memberAssignment = (MemberAssignment)binding;
NewExpressionInfo additem = new NewExpressionInfo();
if ((memberAssignment.Expression is MemberExpression))
{
additem.LeftNameName = memberAssignment.Member.Name;
var member = (memberAssignment.Expression as MemberExpression).Expression;
additem.ShortName = member + "";
additem.RightName = (memberAssignment.Expression as MemberExpression).Member.Name;
additem.RightDbName = context.GetDbColumnName(member.Type.Name, additem.RightName);
result.Add(additem);
}
}
return result;
}
internal static List<NewExpressionInfo> GetNewDynamicexpressionInfos(Expression item, ExpressionContext context)
{
List<NewExpressionInfo> result = new List<NewExpressionInfo>();
foreach (var binding in ((NewExpression)item).Arguments)
{
NewExpressionInfo additem = new NewExpressionInfo();
if (binding is MemberExpression)
{
var member = (MemberExpression)binding;
//var memberAssignment = binding;
//NewExpressionInfo additem = new NewExpressionInfo();
additem.RightName = member.Member.Name;
additem.ShortName = member.Expression + "";
additem.RightName = member.Member.Name;
additem.RightDbName = context.GetDbColumnName(member.Type.Name, additem.RightName);
//additem.Value = "";
result.Add(additem);
}
}
return result;
}
} }
} }

View File

@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SqlSugar
{
internal class NewExpressionInfo
{
public string LeftNameName { get; set; }
public string RightName { get; set; }
public string RightDbName { get; set; }
public string ShortName { get; set; }
}
}

View File

@ -8,6 +8,7 @@ namespace SqlSugar
{ {
public class BaseResolve public class BaseResolve
{ {
#region Property
protected Expression Expression { get; set; } protected Expression Expression { get; set; }
protected Expression ExactExpression { get; set; } protected Expression ExactExpression { get; set; }
public ExpressionContext Context { get; set; } public ExpressionContext Context { get; set; }
@ -111,6 +112,9 @@ namespace SqlSugar
return null; return null;
} }
#endregion
#region Append
protected void AppendMember(ExpressionParameter parameter, bool? isLeft, object appendValue) protected void AppendMember(ExpressionParameter parameter, bool? isLeft, object appendValue)
{ {
@ -259,19 +263,6 @@ namespace SqlSugar
} }
} }
} }
private EntityColumnInfo GetColumnInfo(Expression oppoSiteExpression)
{
var oppsite = (oppoSiteExpression as MemberExpression);
if (oppsite == null) return null;
if (this.Context.SugarContext == null) return null;
if (this.Context.SugarContext.Context == null) return null;
if (oppsite.Expression == null) return null;
var columnInfo = this.Context.SugarContext.Context.EntityMaintenance
.GetEntityInfo(oppsite.Expression.Type).Columns.FirstOrDefault(it => it.PropertyName == oppsite.Member.Name);
return columnInfo;
}
protected void AppendOpreator(ExpressionParameter parameter, bool? isLeft) protected void AppendOpreator(ExpressionParameter parameter, bool? isLeft)
{ {
if (isLeft == true) if (isLeft == true)
@ -312,33 +303,9 @@ namespace SqlSugar
this.Context.Result.Replace(ExpressionConst.FormatSymbol, "-"); this.Context.Result.Replace(ExpressionConst.FormatSymbol, "-");
} }
} }
#endregion
protected MethodCallExpressionArgs GetMethodCallArgs(ExpressionParameter parameter, Expression item) #region New Expression
{
var newContext = this.Context.GetCopyContext();
newContext.MappingColumns = this.Context.MappingColumns;
newContext.MappingTables = this.Context.MappingTables;
newContext.IgnoreComumnList = this.Context.IgnoreComumnList;
newContext.IsSingle = this.Context.IsSingle;
newContext.SqlFuncServices = this.Context.SqlFuncServices;
newContext.Resolve(item, this.Context.IsJoin ? ResolveExpressType.WhereMultiple : ResolveExpressType.WhereSingle);
this.Context.Index = newContext.Index;
this.Context.ParameterIndex = newContext.ParameterIndex;
if (newContext.Parameters.HasValue())
{
this.Context.Parameters.AddRange(newContext.Parameters);
}
if (newContext.SingleTableNameSubqueryShortName.HasValue())
{
this.Context.SingleTableNameSubqueryShortName = newContext.SingleTableNameSubqueryShortName;
}
var methodCallExpressionArgs = new MethodCallExpressionArgs()
{
IsMember = true,
MemberName = newContext.Result.GetResultString()
};
return methodCallExpressionArgs;
}
public string GetNewExpressionValue(Expression item) public string GetNewExpressionValue(Expression item)
{ {
@ -370,7 +337,6 @@ namespace SqlSugar
} }
return newContext.Result.GetResultString(); return newContext.Result.GetResultString();
} }
protected void ResolveNewExpressions(ExpressionParameter parameter, Expression item, string asName) protected void ResolveNewExpressions(ExpressionParameter parameter, Expression item, string asName)
{ {
if (item is ConstantExpression) if (item is ConstantExpression)
@ -494,45 +460,30 @@ namespace SqlSugar
CallContextThread<Dictionary<string, string>>.SetData("Exp_Select_Mapping_Key", mappingKeys); CallContextThread<Dictionary<string, string>>.SetData("Exp_Select_Mapping_Key", mappingKeys);
CallContextAsync<Dictionary<string, string>>.SetData("Exp_Select_Mapping_Key", mappingKeys); CallContextAsync<Dictionary<string, string>>.SetData("Exp_Select_Mapping_Key", mappingKeys);
this.Expression = item; this.Expression = item;
this.Start(); if (this.Context.IsJoin&& (item is MemberInitExpression|| item is NewExpression))
var shortName = parameter.CommonTempData;
var listProperties = item.Type.GetProperties().Cast<PropertyInfo>().ToList();
foreach (var property in listProperties)
{ {
var hasIgnore = this.Context.IgnoreComumnList != null && this.Context.IgnoreComumnList.Any(it => it.EntityName.Equals(item.Type.Name, StringComparison.CurrentCultureIgnoreCase) && it.PropertyName.Equals(property.Name, StringComparison.CurrentCultureIgnoreCase)); List<NewExpressionInfo> newExpressionInfos = new List<NewExpressionInfo>();
if (hasIgnore) if (item is MemberInitExpression)
{ {
continue; newExpressionInfos = ExpressionTool.GetNewexpressionInfos(item,this.Context);
}
if (property.PropertyType.IsClass())
{
var comumnInfo=property.GetCustomAttribute<SugarColumn>();
if (comumnInfo != null && comumnInfo.IsJson && isSameType)
{
asName = GetAsNameAndShortName(item, shortName, property);
}
else if(comumnInfo != null && comumnInfo.IsJson)
{
asName = GetAsName(item, shortName, property);
}
else if (comumnInfo!=null&&this.Context.SugarContext != null&&this.Context.SugarContext.Context != null)
{
var entityInfo=this.Context.SugarContext.Context.EntityMaintenance.GetEntityInfo(item.Type);
var entityColumn = entityInfo.Columns.FirstOrDefault(it => it.PropertyName == property.Name);
if (entityColumn != null && entityColumn.IsJson)
{
asName = GetAsName(item, shortName, property);
}
}
}
else if (isSameType)
{
asName = GetAsNameAndShortName(item, shortName, property);
} }
else else
{ {
asName = GetAsName(item, shortName, property); newExpressionInfos = ExpressionTool.GetNewDynamicexpressionInfos(item, this.Context);
} }
foreach (NewExpressionInfo newExpressionInfo in newExpressionInfos)
{
//var property=item.Type.GetProperties().Where(it => it.Name == newExpressionInfo.l).First();
//asName = GetAsName(item, newExpressionInfo.ShortName, property);
parameter.Context.Result.Append(this.Context.GetAsString(
this.Context.SqlTranslationLeft+asName + "." + newExpressionInfo.RightDbName+this.Context.SqlTranslationRight,
newExpressionInfo.ShortName+"."+newExpressionInfo.RightDbName
));
}
}
else
{
asName = GetAsNameResolveAnObject(parameter, item, asName, isSameType);
} }
} }
else if (item.Type == UtilConstants.BoolType && item is MethodCallExpression && IsNotCaseExpression(item)) else if (item.Type == UtilConstants.BoolType && item is MethodCallExpression && IsNotCaseExpression(item))
@ -562,7 +513,7 @@ namespace SqlSugar
&& (item as UnaryExpression).Operand is MethodCallExpression && (item as UnaryExpression).Operand is MethodCallExpression
&& ((item as UnaryExpression).Operand as MethodCallExpression).Method.Name.IsIn("IsNullOrEmpty", "IsNullOrWhiteSpace")) && ((item as UnaryExpression).Operand as MethodCallExpression).Method.Name.IsIn("IsNullOrEmpty", "IsNullOrWhiteSpace"))
{ {
var asValue = packIfElse(GetNewExpressionValue(item)).ObjToString(); var asValue = GetAsNamePackIfElse(GetNewExpressionValue(item)).ObjToString();
parameter.Context.Result.Append(this.Context.GetAsString(asName, asValue)); parameter.Context.Result.Append(this.Context.GetAsString(asName, asValue));
} }
else if (item is MethodCallExpression && (item as MethodCallExpression).Method.Name.IsIn("Count", "Any") && !item.ToString().StartsWith("Subqueryable")) else if (item is MethodCallExpression && (item as MethodCallExpression).Method.Name.IsIn("Count", "Any") && !item.ToString().StartsWith("Subqueryable"))
@ -584,120 +535,11 @@ namespace SqlSugar
Check.ThrowNotSupportedException(item.GetType().Name); Check.ThrowNotSupportedException(item.GetType().Name);
} }
} }
public object packIfElse(object methodValue)
{
methodValue = this.Context.DbMehtods.CaseWhen(new List<KeyValuePair<string, string>>() {
new KeyValuePair<string, string>("IF",methodValue.ObjToString()),
new KeyValuePair<string, string>("Return","1"),
new KeyValuePair<string, string>("End","0")
});
return methodValue;
}
private static bool IsNotCaseExpression(Expression item)
{
if ((item as MethodCallExpression).Method.Name == "IIF")
{
return false;
}
else if ((item as MethodCallExpression).Method.Name == "IsNull")
{
return false;
}
else if ((item as MethodCallExpression).Method.Name == "End"&&item.ToString().Contains("IF("))
{
return false;
}
else if ((item as MethodCallExpression).Method.Name== "AggregateMax")
{
return false;
}
else if ((item as MethodCallExpression).Method.Name == "AggregateMin")
{
return false;
}
else if ((item as MethodCallExpression).Method.Name == "AggregateSum")
{
return false;
}
else if ((item as MethodCallExpression).Method.Name == "ToBool")
{
return false;
}
else if ((item as MethodCallExpression).Method.Name == "ToBoolean")
{
return false;
}
else if ((item as MethodCallExpression).Method.Name == "Select"&& item.ToString().Contains("Subqueryable()"))
{
return false;
}
else
{
return true;
}
}
private Dictionary<string, string> GetMappingColumns(Expression currentExpression) #endregion
{
Dictionary<string, string> result = new Dictionary<string, string>(); #region Helper
if (currentExpression == null)
{
return result;
}
List<Type> types = new List<Type>();
int i = 0;
if (currentExpression is NewExpression)
{
i = (currentExpression as NewExpression).Arguments.Count;
foreach (var item in (currentExpression as NewExpression).Arguments)
{
if (item.Type.IsClass())
{
types.Add(item.Type);
}
}
}
else if (currentExpression is MemberInitExpression)
{
i = (currentExpression as MemberInitExpression).Bindings.Count;
foreach (var item in (currentExpression as MemberInitExpression).Bindings)
{
MemberAssignment memberAssignment = (MemberAssignment)item;
if (memberAssignment.Expression.Type.IsClass())
{
types.Add(memberAssignment.Expression.Type);
}
}
}
if (types.Count == i&&(types.Count==types.Distinct().Count()))
{
return result;
}
var array = currentExpression.ToString().Split(',');
foreach (var item in array)
{
var itemArray = item.Split('=').ToArray();
var last = itemArray.Last().Trim().Split('.').First().TrimEnd(')').TrimEnd('}');
var first = itemArray.First().Trim();
if (first.Contains("{"))
{
first = first.Split('{').Last().Trim();
}
if (first.Contains("("))
{
first = first.Split('(').Last().Trim();
}
if (!result.ContainsKey(first))
{
result.Add(first, last);
}
else
{
//future
}
}
return result; ;
}
protected void SetNavigateResult() protected void SetNavigateResult()
{ {
if (this.Context != null) if (this.Context != null)
@ -759,6 +601,144 @@ namespace SqlSugar
return asName; return asName;
} }
private EntityColumnInfo GetColumnInfo(Expression oppoSiteExpression)
{
var oppsite = (oppoSiteExpression as MemberExpression);
if (oppsite == null) return null;
if (this.Context.SugarContext == null) return null;
if (this.Context.SugarContext.Context == null) return null;
if (oppsite.Expression == null) return null;
var columnInfo = this.Context.SugarContext.Context.EntityMaintenance
.GetEntityInfo(oppsite.Expression.Type).Columns.FirstOrDefault(it => it.PropertyName == oppsite.Member.Name);
return columnInfo;
}
protected MethodCallExpressionArgs GetMethodCallArgs(ExpressionParameter parameter, Expression item)
{
var newContext = this.Context.GetCopyContext();
newContext.MappingColumns = this.Context.MappingColumns;
newContext.MappingTables = this.Context.MappingTables;
newContext.IgnoreComumnList = this.Context.IgnoreComumnList;
newContext.IsSingle = this.Context.IsSingle;
newContext.SqlFuncServices = this.Context.SqlFuncServices;
newContext.Resolve(item, this.Context.IsJoin ? ResolveExpressType.WhereMultiple : ResolveExpressType.WhereSingle);
this.Context.Index = newContext.Index;
this.Context.ParameterIndex = newContext.ParameterIndex;
if (newContext.Parameters.HasValue())
{
this.Context.Parameters.AddRange(newContext.Parameters);
}
if (newContext.SingleTableNameSubqueryShortName.HasValue())
{
this.Context.SingleTableNameSubqueryShortName = newContext.SingleTableNameSubqueryShortName;
}
var methodCallExpressionArgs = new MethodCallExpressionArgs()
{
IsMember = true,
MemberName = newContext.Result.GetResultString()
};
return methodCallExpressionArgs;
}
private string GetAsNameResolveAnObject(ExpressionParameter parameter, Expression item, string asName, bool isSameType)
{
this.Start();
var shortName = parameter.CommonTempData;
var listProperties = item.Type.GetProperties().Cast<PropertyInfo>().ToList();
foreach (var property in listProperties)
{
var hasIgnore = this.Context.IgnoreComumnList != null && this.Context.IgnoreComumnList.Any(it => it.EntityName.Equals(item.Type.Name, StringComparison.CurrentCultureIgnoreCase) && it.PropertyName.Equals(property.Name, StringComparison.CurrentCultureIgnoreCase));
if (hasIgnore)
{
continue;
}
if (property.PropertyType.IsClass())
{
var comumnInfo = property.GetCustomAttribute<SugarColumn>();
if (comumnInfo != null && comumnInfo.IsJson && isSameType)
{
asName = GetAsNameAndShortName(item, shortName, property);
}
else if (comumnInfo != null && comumnInfo.IsJson)
{
asName = GetAsName(item, shortName, property);
}
else if (comumnInfo != null && this.Context.SugarContext != null && this.Context.SugarContext.Context != null)
{
var entityInfo = this.Context.SugarContext.Context.EntityMaintenance.GetEntityInfo(item.Type);
var entityColumn = entityInfo.Columns.FirstOrDefault(it => it.PropertyName == property.Name);
if (entityColumn != null && entityColumn.IsJson)
{
asName = GetAsName(item, shortName, property);
}
}
}
else if (isSameType)
{
asName = GetAsNameAndShortName(item, shortName, property);
}
else
{
asName = GetAsName(item, shortName, property);
}
}
return asName;
}
public object GetAsNamePackIfElse(object methodValue)
{
methodValue = this.Context.DbMehtods.CaseWhen(new List<KeyValuePair<string, string>>() {
new KeyValuePair<string, string>("IF",methodValue.ObjToString()),
new KeyValuePair<string, string>("Return","1"),
new KeyValuePair<string, string>("End","0")
});
return methodValue;
}
#endregion
#region Validate
private static bool IsNotCaseExpression(Expression item)
{
if ((item as MethodCallExpression).Method.Name == "IIF")
{
return false;
}
else if ((item as MethodCallExpression).Method.Name == "IsNull")
{
return false;
}
else if ((item as MethodCallExpression).Method.Name == "End" && item.ToString().Contains("IF("))
{
return false;
}
else if ((item as MethodCallExpression).Method.Name == "AggregateMax")
{
return false;
}
else if ((item as MethodCallExpression).Method.Name == "AggregateMin")
{
return false;
}
else if ((item as MethodCallExpression).Method.Name == "AggregateSum")
{
return false;
}
else if ((item as MethodCallExpression).Method.Name == "ToBool")
{
return false;
}
else if ((item as MethodCallExpression).Method.Name == "ToBoolean")
{
return false;
}
else if ((item as MethodCallExpression).Method.Name == "Select" && item.ToString().Contains("Subqueryable()"))
{
return false;
}
else
{
return true;
}
}
private static bool IsBoolValue(Expression item) private static bool IsBoolValue(Expression item)
{ {
return item.Type == UtilConstants.BoolType && return item.Type == UtilConstants.BoolType &&
@ -767,12 +747,10 @@ namespace SqlSugar
(item as MemberExpression).Expression.Type == typeof(bool?) && (item as MemberExpression).Expression.Type == typeof(bool?) &&
(item as MemberExpression).Member.Name == "Value"; (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;
} }
protected static bool IsNotMember(Expression item) protected static bool IsNotMember(Expression item)
{ {
return item is UnaryExpression && return item is UnaryExpression &&
@ -791,11 +769,74 @@ namespace SqlSugar
((item as UnaryExpression).Operand as MemberExpression).Expression != null && ((item as UnaryExpression).Operand as MemberExpression).Expression != null &&
((item as UnaryExpression).Operand as MemberExpression).Expression.NodeType == ExpressionType.MemberAccess; ((item as UnaryExpression).Operand as MemberExpression).Expression.NodeType == ExpressionType.MemberAccess;
} }
protected bool IsSubMethod(MethodCallExpression express) protected bool IsSubMethod(MethodCallExpression express)
{ {
return SubTools.SubItemsConst.Any(it => express.Object != null && express.Object.Type.Name.StartsWith("Subqueryable`")); return SubTools.SubItemsConst.Any(it => express.Object != null && express.Object.Type.Name.StartsWith("Subqueryable`"));
} }
#endregion
#region Dictionary
private Dictionary<string, string> GetMappingColumns(Expression currentExpression)
{
Dictionary<string, string> result = new Dictionary<string, string>();
if (currentExpression == null)
{
return result;
}
List<Type> types = new List<Type>();
int i = 0;
if (currentExpression is NewExpression)
{
i = (currentExpression as NewExpression).Arguments.Count;
foreach (var item in (currentExpression as NewExpression).Arguments)
{
if (item.Type.IsClass())
{
types.Add(item.Type);
}
}
}
else if (currentExpression is MemberInitExpression)
{
i = (currentExpression as MemberInitExpression).Bindings.Count;
foreach (var item in (currentExpression as MemberInitExpression).Bindings)
{
MemberAssignment memberAssignment = (MemberAssignment)item;
if (memberAssignment.Expression.Type.IsClass())
{
types.Add(memberAssignment.Expression.Type);
}
}
}
if (types.Count == i && (types.Count == types.Distinct().Count()))
{
return result;
}
var array = currentExpression.ToString().Split(',');
foreach (var item in array)
{
var itemArray = item.Split('=').ToArray();
var last = itemArray.Last().Trim().Split('.').First().TrimEnd(')').TrimEnd('}');
var first = itemArray.First().Trim();
if (first.Contains("{"))
{
first = first.Split('{').Last().Trim();
}
if (first.Contains("("))
{
first = first.Split('(').Last().Trim();
}
if (!result.ContainsKey(first))
{
result.Add(first, last);
}
else
{
//future
}
}
return result; ;
}
protected static Dictionary<string, string> MethodMapping = new Dictionary<string, string>() { protected static Dictionary<string, string> MethodMapping = new Dictionary<string, string>() {
{ "ToString","ToString"}, { "ToString","ToString"},
{ "ToInt32","ToInt32"}, { "ToInt32","ToInt32"},
@ -819,7 +860,6 @@ namespace SqlSugar
{ "Substring","Substring"}, { "Substring","Substring"},
{ "DateAdd","DateAdd"} { "DateAdd","DateAdd"}
}; };
protected static Dictionary<string, DateType> MethodTimeMapping = new Dictionary<string, DateType>() { protected static Dictionary<string, DateType> MethodTimeMapping = new Dictionary<string, DateType>() {
{ "AddYears",DateType.Year}, { "AddYears",DateType.Year},
{ "AddMonths",DateType.Month}, { "AddMonths",DateType.Month},
@ -829,5 +869,6 @@ namespace SqlSugar
{ "AddSeconds",DateType.Second}, { "AddSeconds",DateType.Second},
{ "AddMilliseconds",DateType.Millisecond} { "AddMilliseconds",DateType.Millisecond}
}; };
#endregion
} }
} }

View File

@ -436,6 +436,11 @@ namespace SqlSugar
{ {
var key = typeName + "." + name; var key = typeName + "." + name;
var info = readerValues.Select(it => it.Key).FirstOrDefault(it => it.ToLower() == key.ToLower()); var info = readerValues.Select(it => it.Key).FirstOrDefault(it => it.ToLower() == key.ToLower());
if (info == null && typeName !=null && typeName.Contains("<>f__AnonymousType"))
{
key = item.Name + "." + name;
info = readerValues.Select(it => it.Key).FirstOrDefault(it => it.ToLower() == key.ToLower());
}
var oldInfo = info; var oldInfo = info;
if (mappingKeys!=null&&mappingKeys.ContainsKey(item.Name)) if (mappingKeys!=null&&mappingKeys.ContainsKey(item.Name))
{ {