private void SettingParameters(ExpressionParameter parameter, out ExpressionParameter baseParameter, out MemberExpression expression, out bool?isLeft, out bool isSetTempData, out bool isValue, out bool isValueBool, out bool isLength, out bool isDateValue, out bool isHasValue, out bool isDateDate, out bool isMemberValue, out bool isSingle, out bool fieldIsBool, out bool isSelectField, out bool isField) { baseParameter = parameter.BaseParameter; expression = base.Expression as MemberExpression; var childExpression = expression.Expression as MemberExpression; var memberName = expression.Member.Name; var childIsMember = childExpression != null; var isRoot = parameter.BaseExpression == null; isLeft = parameter.IsLeft; isSetTempData = parameter.IsSetTempData; isValue = memberName == "Value" && expression.Member.DeclaringType.Name == "Nullable`1"; var isBool = expression.Type == UtilConstants.BoolType; isValueBool = isValue && isBool && isRoot; isLength = memberName == "Length" && childIsMember && childExpression.Type == UtilConstants.StringType; isDateValue = memberName.IsIn(Enum.GetNames(typeof(DateType))) && (childIsMember && childExpression.Type == UtilConstants.DateType); var isLogicOperator = ExpressionTool.IsLogicOperator(baseParameter.OperatorValue) || baseParameter.OperatorValue.IsNullOrEmpty(); isHasValue = isLogicOperator && memberName == "HasValue" && expression.Expression != null && expression.NodeType == ExpressionType.MemberAccess; isDateDate = memberName == "Date" && expression.Expression.Type == UtilConstants.DateType; isMemberValue = expression.Expression != null && expression.Expression.NodeType != ExpressionType.Parameter && !isValueBool; isSingle = parameter.Context.ResolveType.IsIn(ResolveExpressType.WhereSingle, ResolveExpressType.SelectSingle, ResolveExpressType.FieldSingle, ResolveExpressType.ArraySingle); fieldIsBool = isBool && isLogicOperator && (parameter.BaseParameter == null || !(parameter.BaseParameter.CurrentExpression is MemberInitExpression || parameter.BaseParameter.CurrentExpression is NewExpression)); var isSelect = this.Context.ResolveType.IsIn(ResolveExpressType.SelectSingle, ResolveExpressType.SelectMultiple); isSelectField = isSelect && isRoot; isField = this.Context.ResolveType.IsIn(ResolveExpressType.FieldSingle, ResolveExpressType.FieldMultiple); }
private void MemberLogic(ExpressionParameter parameter, ExpressionParameter baseParameter, ExpressionType nodeType) { var memberExpression = (base.Expression as MemberExpression); var isLogicOperator = ExpressionTool.IsLogicOperator(baseParameter.OperatorValue) || baseParameter.OperatorValue.IsNullOrEmpty(); var isHasValue = isLogicOperator && memberExpression.Member.Name == "HasValue" && memberExpression.Expression != null && memberExpression.NodeType == ExpressionType.MemberAccess; if (isHasValue) { var member = memberExpression.Expression as MemberExpression; parameter.CommonTempData = CommonTempDataType.Result; var isConst = member.Expression != null && member.Expression is ConstantExpression; if (isConst) { var paramterValue = ExpressionTool.DynamicInvoke(member); var paramterName = base.AppendParameter(paramterValue); var result = this.Context.DbMehtods.HasValue(new MethodCallExpressionModel() { Args = new List <MethodCallExpressionArgs>() { new MethodCallExpressionArgs() { IsMember = false, MemberName = paramterName, MemberValue = paramterValue } } }); this.Context.Result.Append(result); } else { this.Expression = isConst ? member.Expression : member; this.Start(); var methodParamter = isConst ? new MethodCallExpressionArgs() { IsMember = false } : new MethodCallExpressionArgs() { IsMember = true, MemberName = parameter.CommonTempData, MemberValue = null }; var result = this.Context.DbMehtods.HasValue(new MethodCallExpressionModel() { Args = new List <MethodCallExpressionArgs>() { methodParamter } }); this.Context.Result.Append(result); parameter.CommonTempData = null; } } else if (memberExpression.Type == UtilConstants.BoolType && isLogicOperator) { Append(parameter, nodeType); } else { Result(parameter, nodeType); } }
protected void Where(ExpressionParameter parameter, bool?isLeft, string name, IEnumerable <Expression> args, MethodCallExpressionModel model, List <MethodCallExpressionArgs> appendArgs = null) { foreach (var item in args) { var expItem = item; if (item is UnaryExpression) { expItem = (item as UnaryExpression).Operand; } AppendItem(parameter, name, args, model, expItem); } if (appendArgs != null) { model.Args.AddRange(appendArgs); } var methodValue = GetMethodValue(name, model); if (parameter.BaseExpression is BinaryExpression && parameter.OppsiteExpression.Type == UtilConstants.BoolType && name == "HasValue" && !(parameter.OppsiteExpression is BinaryExpression) && !(parameter.OppsiteExpression is MethodCallExpression && parameter.OppsiteExpression.Type == UtilConstants.BoolType)) { methodValue = packIfElse(methodValue); } if (parameter.OppsiteExpression != null && name == "IsNullOrEmpty" && parameter.OppsiteExpression.Type == UtilConstants.BoolType && parameter.OppsiteExpression is ConstantExpression) { methodValue = packIfElse(methodValue); } var isRoot = contextIndex == 2; if (isRoot && parameter.BaseExpression == null && this.Context.ResolveType.IsIn(ResolveExpressType.WhereMultiple, ResolveExpressType.WhereSingle) && (parameter.CurrentExpression is MethodCallExpression) && ((parameter.CurrentExpression as MethodCallExpression).Method.Name.IsIn("ToBool", "ToBoolean"))) { methodValue = methodValue + "=1 "; ; } if (isRoot && parameter.BaseExpression == null && this.Context.ResolveType.IsIn(ResolveExpressType.WhereMultiple, ResolveExpressType.WhereSingle) && (parameter.CurrentExpression is ConditionalExpression) && ((parameter.CurrentExpression as ConditionalExpression).Type == UtilConstants.BoolType)) { methodValue = methodValue + "=1 "; } if (isRoot && parameter.BaseExpression == null && this.Context.ResolveType.IsIn(ResolveExpressType.WhereMultiple, ResolveExpressType.WhereSingle) && (parameter.CurrentExpression is MethodCallExpression) && ((parameter.CurrentExpression as MethodCallExpression).Method.Name.IsIn("IIF")) && (parameter.CurrentExpression as MethodCallExpression).Method.ReturnType == UtilConstants.BoolType) { methodValue = methodValue + "=1 "; } if (parameter.BaseExpression != null && ExpressionTool.IsLogicOperator(parameter.BaseExpression) && this.Context.ResolveType.IsIn(ResolveExpressType.WhereMultiple, ResolveExpressType.WhereSingle) && (parameter.CurrentExpression is ConditionalExpression) && ((parameter.CurrentExpression as ConditionalExpression).Type == UtilConstants.BoolType)) { methodValue = methodValue + "=1 "; } if (parameter.BaseExpression != null && ExpressionTool.IsLogicOperator(parameter.BaseExpression) && this.Context.ResolveType.IsIn(ResolveExpressType.WhereMultiple, ResolveExpressType.WhereSingle) && (parameter.CurrentExpression is MethodCallExpression) && ((parameter.CurrentExpression as MethodCallExpression).Method.Name.IsIn("IIF")) && (parameter.CurrentExpression as MethodCallExpression).Method.ReturnType == UtilConstants.BoolType) { methodValue = methodValue + "=1 "; } if (parameter.BaseExpression != null && ExpressionTool.IsLogicOperator(parameter.BaseExpression) && this.Context.ResolveType.IsIn(ResolveExpressType.WhereMultiple, ResolveExpressType.WhereSingle) && (parameter.CurrentExpression is MethodCallExpression) && ((parameter.CurrentExpression as MethodCallExpression).Method.Name.IsIn("ToBool", "ToBoolean"))) { methodValue = methodValue + "=1 "; } base.AppendValue(parameter, isLeft, methodValue); }
private void ResolveHasValue(ExpressionParameter parameter, MemberExpression expression) { parameter.CommonTempData = CommonTempDataType.Result; this.Expression = expression.Expression; this.Start(); var methodParamter = new MethodCallExpressionArgs() { IsMember = true, MemberName = parameter.CommonTempData, MemberValue = null }; if (expression.Expression?.Type != null) { methodParamter.Type = UtilMethods.GetUnderType(expression.Expression?.Type); } var result = this.Context.DbMehtods.HasValue(new MethodCallExpressionModel() { Args = new List <MethodCallExpressionArgs>() { methodParamter } }); if (parameter.BaseExpression != null && ExpressionTool.IsLogicOperator(parameter.BaseExpression) && parameter.IsLeft == true) { if (base.Context.Result.Contains(ExpressionConst.FormatSymbol)) { base.Context.Result.Replace(ExpressionConst.FormatSymbol, ""); } this.Context.Result.Append(result + " " + ExpressionTool.GetOperator(parameter.BaseExpression.NodeType) + " "); } else { this.Context.Result.Append(result); } parameter.CommonTempData = null; }
private MemberExpression ResolveValue(ExpressionParameter parameter, ExpressionParameter baseParameter, MemberExpression expression, bool?isLeft, bool isSetTempData, bool isSingle) { expression = expression.Expression as MemberExpression; baseParameter.ChildExpression = expression; if (UtilMethods.GetUnderType(expression.Type) == UtilConstants.BoolType && parameter.BaseExpression != null && ExpressionTool.IsLogicOperator(parameter.BaseExpression)) { ResolveBoolLogic(parameter, baseParameter, expression, isLeft, isSetTempData, isSingle); } else { ResolveWhereLogic(parameter, baseParameter, expression, isLeft, isSetTempData, isSingle); } return(expression); }
public MethodCallExpressionResolve(ExpressionParameter parameter) : base(parameter) { contextIndex = this.Context.Index; var express = base.Expression as MethodCallExpression; if (express == null) { return; } var isLeft = parameter.IsLeft; string methodName = express.Method.Name; var isValidNativeMethod = IsValidNativeMethod(express, methodName); List <MethodCallExpressionArgs> appendArgs = null; if (MethodTimeMapping.ContainsKey(methodName)) { appendArgs = new List <MethodCallExpressionArgs>(); var dateType = MethodTimeMapping[methodName]; string paramterName = this.Context.SqlParameterKeyWord + ExpressionConst.Const + this.Context.ParameterIndex; appendArgs.Add(new MethodCallExpressionArgs() { IsMember = false, MemberName = paramterName, MemberValue = dateType }); this.Context.Parameters.Add(new SugarParameter(paramterName, dateType.ToString())); this.Context.ParameterIndex++; methodName = "DateAdd"; isValidNativeMethod = true; } else if (methodName == "get_Item") { string paramterName = this.Context.SqlParameterKeyWord + ExpressionConst.Const + this.Context.ParameterIndex; this.Context.Parameters.Add(new SugarParameter(paramterName, ExpressionTool.DynamicInvoke(express))); this.Context.Result.Append(string.Format(" {0} ", paramterName)); this.Context.ParameterIndex++; return; } else if (methodName == "NewGuid") { this.Context.Result.Append(this.Context.DbMehtods.GuidNew()); return; } else if (IsSubMethod(express, methodName)) { //Check.Exception(!(parameter.BaseExpression is BinaryExpression), "Current expressions are not supported"); SubResolve subResolve = new SubResolve(express, this.Context, parameter.OppsiteExpression); var appendSql = subResolve.GetSql(); if (this.Context.ResolveType.IsIn(ResolveExpressType.SelectMultiple, ResolveExpressType.SelectSingle) || (parameter.BaseParameter != null && parameter.BaseParameter.CommonTempData != null && parameter.BaseParameter.CommonTempData.Equals(CommonTempDataType.Result))) { parameter.BaseParameter.CommonTempData = appendSql; } else { base.AppendValue(parameter, isLeft, appendSql); } return; } else if (IsIfElse(express, methodName)) { CaseWhenResolve caseResole = new CaseWhenResolve(express, this.Context, parameter.OppsiteExpression); var appendSql = caseResole.GetSql(); var isRoot = contextIndex == 2 && parameter.BaseExpression == null; if (isRoot || (parameter.BaseExpression != null && ExpressionTool.IsLogicOperator(parameter.BaseExpression))) { appendSql = appendSql + "=1 "; } if (this.Context.ResolveType.IsIn(ResolveExpressType.SelectMultiple, ResolveExpressType.SelectSingle, ResolveExpressType.Update)) { parameter.BaseParameter.CommonTempData = appendSql; } else { base.AppendValue(parameter, isLeft, appendSql); } return; } if (IsContainsArray(express, methodName, isValidNativeMethod)) { methodName = "ContainsArray"; isValidNativeMethod = true; } if (isValidNativeMethod) { NativeExtensionMethod(parameter, express, isLeft, MethodMapping[methodName], appendArgs); } else { SqlFuncMethod(parameter, express, isLeft); } }