Optimize the code

This commit is contained in:
sunkaixuan
2022-08-02 16:28:42 +08:00
parent 13e323d595
commit 93207f038f

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,7 +460,16 @@ 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;
asName = ResolveAnObject(parameter, item, asName, isSameType); if (item is MemberInitExpression)
{
}
else if (item is NewExpression)
{
}
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))
{ {
@@ -523,7 +498,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"))
@@ -546,166 +521,10 @@ namespace SqlSugar
} }
} }
private string ResolveAnObject(ExpressionParameter parameter, Expression item, string asName, bool isSameType) #endregion
{
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; #region Helper
}
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)
{
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 void SetNavigateResult() protected void SetNavigateResult()
{ {
if (this.Context != null) if (this.Context != null)
@@ -767,6 +586,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 &&
@@ -775,12 +732,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 &&
@@ -799,11 +754,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"},
@@ -827,7 +845,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},
@@ -837,5 +854,6 @@ namespace SqlSugar
{ "AddSeconds",DateType.Second}, { "AddSeconds",DateType.Second},
{ "AddMilliseconds",DateType.Millisecond} { "AddMilliseconds",DateType.Millisecond}
}; };
#endregion
} }
} }