public MemberExpressionResolve(ExpressionParameter parameter) : base(parameter)
        {
            var baseParameter     = parameter.BaseParameter;
            var isLeft            = parameter.IsLeft;
            var isSetTempData     = baseParameter.CommonTempData.IsValuable();
            var expression        = base.Expression as MemberExpression;
            var childExpression   = expression.Expression as MemberExpression;
            var childIsMember     = childExpression != null;
            var isValue           = expression.Member.Name == "Value" && expression.Member.DeclaringType.Name == "Nullable`1";
            var isBool            = expression.Type == UtilConstants.BoolType;
            var isValueBool       = isValue && isBool && parameter.BaseExpression == null;
            var isLength          = expression.Member.Name == "Length" && childIsMember && childExpression.Type == UtilConstants.StringType;
            var isDateValue       = expression.Member.Name.IsIn(Enum.GetNames(typeof(DateType))) && (expression.Expression as MemberExpression).Type == UtilConstants.DateType;
            var isLogicOperator   = ExpressionTool.IsLogicOperator(baseParameter.OperatorValue) || baseParameter.OperatorValue.IsNullOrEmpty();
            var isHasValue        = isLogicOperator && expression.Member.Name == "HasValue" && expression.Expression != null && expression.NodeType == ExpressionType.MemberAccess;
            var isDateTimeNowDate = expression.Member.Name == "Date" && childIsMember && childExpression.Member.Name == "Now";
            var isDateDate        = expression.Member.Name == "Date" && expression.Expression.Type == UtilConstants.DateType;

            if (isLength)
            {
                var oldCommonTempDate = parameter.CommonTempData;
                this.Expression = expression.Expression;
                var isConst = this.Expression is ConstantExpression;
                this.Start();
                var methodParamter = new MethodCallExpressionArgs()
                {
                    IsMember = !isConst, MemberName = parameter.CommonTempData, MemberValue = null
                };
                var result = this.Context.DbMehtods.Length(new MethodCallExpressionModel()
                {
                    Args = new List <MethodCallExpressionArgs>()
                    {
                        methodParamter
                    }
                });
                base.AppendMember(parameter, isLeft, result);
                parameter.CommonTempData = oldCommonTempDate;
                return;
            }
            else if (isHasValue)
            {
                this.Expression = expression.Expression;
                this.Start();
                var methodParamter = 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;
                return;
            }
            else if (isDateValue)
            {
                var name = expression.Member.Name;
                var oldCommonTempDate = parameter.CommonTempData;
                this.Expression = expression.Expression;
                var isConst = this.Expression is ConstantExpression;
                this.Start();
                var result = this.Context.DbMehtods.DateValue(new MethodCallExpressionModel()
                {
                    Args = new List <MethodCallExpressionArgs>()
                    {
                        new MethodCallExpressionArgs()
                        {
                            IsMember = !isConst, MemberName = parameter.CommonTempData, MemberValue = null
                        },
                        new MethodCallExpressionArgs()
                        {
                            IsMember = true, MemberName = name, MemberValue = name
                        }
                    }
                });
                base.AppendMember(parameter, isLeft, result);
                parameter.CommonTempData = oldCommonTempDate;
                return;
            }
            else if (isValueBool)
            {
                isValue = false;
            }
            else if (isValue)
            {
                expression = expression.Expression as MemberExpression;
            }
            else if (isDateDate)
            {
                var name = expression.Member.Name;
                var oldCommonTempDate = parameter.CommonTempData;
                this.Expression = expression.Expression;
                this.Start();
                var isConst = parameter.CommonTempData.GetType() == UtilConstants.DateType;
                if (isConst)
                {
                    AppendValue(parameter, isLeft, parameter.CommonTempData.ObjToDate().Date);
                }
                else
                {
                    var GetYear = new MethodCallExpressionModel()
                    {
                        Args = new List <MethodCallExpressionArgs>()
                        {
                            new MethodCallExpressionArgs()
                            {
                                IsMember = true, MemberName = parameter.CommonTempData, MemberValue = parameter.CommonTempData
                            },
                            new MethodCallExpressionArgs()
                            {
                                MemberName = DateType.Year, MemberValue = DateType.Year
                            }
                        }
                    };
                    AppendMember(parameter, isLeft, GetToDate(this.Context.DbMehtods.MergeString(
                                                                  this.GetDateValue(parameter.CommonTempData, DateType.Year),
                                                                  "+'-'+",
                                                                  this.GetDateValue(parameter.CommonTempData, DateType.Month),
                                                                  "+'-'+",
                                                                  this.GetDateValue(parameter.CommonTempData, DateType.Day))));
                }
                parameter.CommonTempData = oldCommonTempDate;
                return;
            }
            else if (isDateTimeNowDate)
            {
                AppendValue(parameter, isLeft, DateTime.Now.Date);
                return;
            }
            else if (expression.Expression != null && expression.Expression.NodeType != ExpressionType.Parameter && !isValueBool)
            {
                var value = ExpressionTool.GetMemberValue(expression.Member, expression);
                if (isSetTempData)
                {
                    baseParameter.CommonTempData = value;
                }
                else
                {
                    AppendValue(parameter, isLeft, value);
                }
                return;
            }
            string fieldName = string.Empty;

            baseParameter.ChildExpression = expression;
            switch (parameter.Context.ResolveType)
            {
            case ResolveExpressType.SelectSingle:
                fieldName = GetSingleName(parameter, expression, isLeft);
                if (isSetTempData)
                {
                    baseParameter.CommonTempData = fieldName;
                }
                else
                {
                    base.Context.Result.Append(fieldName);
                }
                break;

            case ResolveExpressType.SelectMultiple:
                fieldName = GetMultipleName(parameter, expression, isLeft);
                if (isSetTempData)
                {
                    baseParameter.CommonTempData = fieldName;
                }
                else
                {
                    base.Context.Result.Append(fieldName);
                }
                break;

            case ResolveExpressType.WhereSingle:
            case ResolveExpressType.WhereMultiple:
                var isSingle = parameter.Context.ResolveType == ResolveExpressType.WhereSingle;
                if (isSetTempData)
                {
                    fieldName = GetName(parameter, expression, null, isSingle);
                    baseParameter.CommonTempData = fieldName;
                }
                else
                {
                    if (isValueBool)
                    {
                        fieldName = GetName(parameter, expression.Expression as MemberExpression, isLeft, isSingle);
                    }
                    else if (ExpressionTool.IsConstExpression(expression))
                    {
                        var value = ExpressionTool.GetMemberValue(expression.Member, expression);
                        base.AppendValue(parameter, isLeft, value);
                        return;
                    }
                    else
                    {
                        fieldName = GetName(parameter, expression, isLeft, isSingle);
                    }
                    if (expression.Type == UtilConstants.BoolType && baseParameter.OperatorValue.IsNullOrEmpty())
                    {
                        fieldName = "( " + fieldName + "=1 )";
                    }
                    fieldName = AppendMember(parameter, isLeft, fieldName);
                }
                break;

            case ResolveExpressType.FieldSingle:
                fieldName = GetSingleName(parameter, expression, isLeft);
                base.Context.Result.Append(fieldName);
                break;

            case ResolveExpressType.FieldMultiple:
                fieldName = GetMultipleName(parameter, expression, isLeft);
                base.Context.Result.Append(fieldName);
                break;

            case ResolveExpressType.ArrayMultiple:
            case ResolveExpressType.ArraySingle:
                fieldName = GetName(parameter, expression, isLeft, parameter.Context.ResolveType == ResolveExpressType.ArraySingle);
                base.Context.Result.Append(fieldName);
                break;

            default:
                break;
            }
        }
Example #2
0
 protected void ResolveNewExpressions(ExpressionParameter parameter, Expression item, string asName)
 {
     if (item is ConstantExpression)
     {
         this.Expression = item;
         this.Start();
         string parameterName = this.Context.SqlParameterKeyWord + "constant" + this.Context.ParameterIndex;
         this.Context.ParameterIndex++;
         parameter.Context.Result.Append(this.Context.GetAsString(asName, parameterName));
         this.Context.Parameters.Add(new Parameter(parameterName, parameter.CommonTempData));
     }
     else if ((item is MemberExpression) && ((MemberExpression)item).Expression == null)
     {
         var    paramterValue = ExpressionTool.GetPropertyValue(item as MemberExpression);
         string parameterName = this.Context.SqlParameterKeyWord + "constant" + this.Context.ParameterIndex;
         this.Context.ParameterIndex++;
         parameter.Context.Result.Append(this.Context.GetAsString(asName, parameterName));
         this.Context.Parameters.Add(new Parameter(parameterName, paramterValue));
     }
     else if ((item is MemberExpression) && ((MemberExpression)item).Expression.NodeType == ExpressionType.Constant)
     {
         this.Expression = item;
         this.Start();
         string parameterName = this.Context.SqlParameterKeyWord + "constant" + this.Context.ParameterIndex;
         this.Context.ParameterIndex++;
         parameter.Context.Result.Append(this.Context.GetAsString(asName, parameterName));
         this.Context.Parameters.Add(new Parameter(parameterName, parameter.CommonTempData));
     }
     else if (item is MemberExpression)
     {
         if (this.Context.Result.IsLockCurrentParameter == false)
         {
             this.Context.Result.CurrentParameter       = parameter;
             this.Context.Result.IsLockCurrentParameter = true;
             parameter.IsAppendTempDate();
             this.Expression = item;
             this.Start();
             parameter.IsAppendResult();
             this.Context.Result.Append(this.Context.GetAsString(asName, parameter.CommonTempData.ObjToString()));
             this.Context.Result.CurrentParameter = null;
         }
     }
     else if (item is UnaryExpression && ((UnaryExpression)item).Operand is MemberExpression)
     {
         if (this.Context.Result.IsLockCurrentParameter == false)
         {
             var expression = ((UnaryExpression)item).Operand as MemberExpression;
             if (expression.Expression == null)
             {
                 this.Context.Result.CurrentParameter       = parameter;
                 this.Context.Result.IsLockCurrentParameter = true;
                 parameter.IsAppendTempDate();
                 this.Expression = item;
                 this.Start();
                 parameter.IsAppendResult();
                 this.Context.Result.Append(this.Context.GetAsString(asName, parameter.CommonTempData.ObjToString()));
                 this.Context.Result.CurrentParameter = null;
             }
             else if (expression.Expression is ConstantExpression)
             {
                 string parameterName = this.Context.SqlParameterKeyWord + "constant" + this.Context.ParameterIndex;
                 this.Context.ParameterIndex++;
                 parameter.Context.Result.Append(this.Context.GetAsString(asName, parameterName));
                 this.Context.Parameters.Add(new Parameter(parameterName, ExpressionTool.GetMemberValue(expression.Member, expression)));
             }
             else
             {
                 this.Context.Result.CurrentParameter       = parameter;
                 this.Context.Result.IsLockCurrentParameter = true;
                 parameter.IsAppendTempDate();
                 this.Expression = item;
                 this.Start();
                 parameter.IsAppendResult();
                 this.Context.Result.Append(this.Context.GetAsString(asName, parameter.CommonTempData.ObjToString()));
                 this.Context.Result.CurrentParameter = null;
             }
         }
     }
     else if (item is UnaryExpression && ((UnaryExpression)item).Operand is ConstantExpression)
     {
         if (this.Context.Result.IsLockCurrentParameter == false)
         {
             this.Expression = ((UnaryExpression)item).Operand;
             this.Start();
             string parameterName = this.Context.SqlParameterKeyWord + "constant" + this.Context.ParameterIndex;
             this.Context.ParameterIndex++;
             parameter.Context.Result.Append(this.Context.GetAsString(asName, parameterName));
             this.Context.Parameters.Add(new Parameter(parameterName, parameter.CommonTempData));
         }
     }
     else if (item is BinaryExpression)
     {
         if (this.Context.Result.IsLockCurrentParameter == false)
         {
             var newContext         = this.Context.GetCopyContext();
             var resolveExpressType = this.Context.IsSingle ? ResolveExpressType.WhereSingle : ResolveExpressType.WhereMultiple;
             newContext.Resolve(item, resolveExpressType);
             this.Context.Index          = newContext.Index;
             this.Context.ParameterIndex = newContext.ParameterIndex;
             if (newContext.Parameters.IsValuable())
             {
                 this.Context.Parameters.AddRange(newContext.Parameters);
             }
             this.Context.Result.Append(this.Context.GetAsString(asName, newContext.Result.GetString()));
             this.Context.Result.CurrentParameter = null;
         }
     }
     else if (item.Type.IsClass())
     {
         this.Expression = item;
         this.Start();
         var shortName      = parameter.CommonTempData;
         var listProperties = item.Type.GetProperties().Cast <PropertyInfo>().ToList();
         foreach (var property in listProperties)
         {
             if (property.PropertyType.IsClass())
             {
             }
             else
             {
                 asName = this.Context.GetTranslationText(item.Type.Name + "." + property.Name);
                 var columnName = property.Name;
                 if (Context.IsJoin)
                 {
                     this.Context.Result.Append(Context.GetAsString(asName, columnName, shortName.ObjToString()));
                 }
                 else
                 {
                     this.Context.Result.Append(Context.GetAsString(asName, columnName));
                 }
             }
         }
     }
     else if (item is MethodCallExpression || item is UnaryExpression)
     {
         this.Expression = item;
         this.Start();
         parameter.Context.Result.Append(this.Context.GetAsString(asName, parameter.CommonTempData.ObjToString()));
     }
     else
     {
         // Check.ThrowNotSupportedException(item.GetType().Name);
     }
 }