Exemple #1
0
        public MemberNewExpressionResolve(ExpressionParameter parameter) : base(parameter)
        {
            var    expression = base.Expression as MemberExpression;
            var    isLeft     = parameter.IsLeft;
            object value      = null;

            value = ExpressionTool.DynamicInvoke(expression);
        }
        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 == PubConst.BoolType && isLogicOperator)
            {
                Append(parameter, nodeType);
            }
            else
            {
                Result(parameter, nodeType);
            }
        }
        private void Update(MemberInitExpression expression, ExpressionParameter parameter)
        {
            int i = 0;

            foreach (MemberBinding binding in expression.Bindings)
            {
                ++i;
                if (binding.BindingType != MemberBindingType.Assignment)
                {
                    throw new NotSupportedException();
                }
                MemberAssignment memberAssignment = (MemberAssignment)binding;
                var type       = memberAssignment.Member.ReflectedType;
                var memberName = this.Context.GetDbColumnName(type.Name, memberAssignment.Member.Name);
                var item       = memberAssignment.Expression;
                if ((item is MemberExpression) && ((MemberExpression)item).Expression == null)
                {
                    var    paramterValue = ExpressionTool.DynamicInvoke(item);
                    string parameterName = AppendParameter(paramterValue);
                    this.Context.Result.Append(base.Context.GetEqString(memberName, parameterName));
                }
                else if (item is UnaryExpression || item.NodeType == ExpressionType.Constant || (item is MemberExpression) && ((MemberExpression)item).Expression.NodeType == ExpressionType.Constant)
                {
                    base.Expression = item;
                    base.Start();
                    string parameterName = this.Context.SqlParameterKeyWord + ExpressionConst.Const + this.Context.ParameterIndex;
                    parameter.Context.Result.Append(base.Context.GetEqString(memberName, parameterName));
                    this.Context.Parameters.Add(new SugarParameter(parameterName, parameter.CommonTempData));
                    this.Context.ParameterIndex++;
                }
                else if (item is MethodCallExpression)
                {
                    base.Expression = item;
                    base.Start();
                    parameter.Context.Result.Append(base.Context.GetEqString(memberName, parameter.CommonTempData.ObjToString()));
                }
                else if (item is MemberExpression)
                {
                    if (base.Context.Result.IsLockCurrentParameter == false)
                    {
                        base.Context.Result.CurrentParameter       = parameter;
                        base.Context.Result.IsLockCurrentParameter = true;
                        parameter.IsAppendTempDate();
                        base.Expression = item;
                        base.Start();
                        parameter.IsAppendResult();
                        parameter.Context.Result.Append(base.Context.GetEqString(memberName, parameter.CommonTempData.ObjToString()));
                        base.Context.Result.CurrentParameter = null;
                    }
                }
                else if (item is BinaryExpression)
                {
                    var result = GetNewExpressionValue(item);
                    this.Context.Result.Append(base.Context.GetEqString(memberName, result));
                }
            }
        }
Exemple #4
0
        private void GetConfigValue(MethodCallExpression express, ExpressionParameter parameter)
        {
            var    exp  = express.Arguments[0];
            var    name = Regex.Match(express.Method.ToString(), @"GetConfigValue\[(.+)\]").Groups[1].Value;
            string code = null;

            if (express.Arguments.Count > 1)
            {
                code = ExpressionTool.GetExpressionValue(express.Arguments[1]) + "";
            }
            var entity = SqlFuncExtendsion.TableInfos.FirstOrDefault(y => y.Type.Name == name && y.Code == code);

            Check.Exception(entity == null, string.Format("GetConfigValue no configuration  Entity={0} UniqueCode={1}", name, code));
            string sql = " (SELECT {0} FROM {1} WHERE {2}={3}";

            if (ExpressionTool.IsUnConvertExpress(exp))
            {
                exp = (exp as UnaryExpression).Operand;
            }
            var member      = exp as MemberExpression;
            var it          = member.Expression;
            var type        = it.Type;
            var properyName = member.Member.Name;
            var eqName      = string.Format("{0}.{1}", this.Context.GetTranslationColumnName(it.ToString()), this.Context.GetDbColumnName(type.Name, properyName));

            if (this.Context.IsSingle)
            {
                this.Context.SingleTableNameSubqueryShortName = it.ToString();
            }
            sql = string.Format(sql, entity.Value, this.Context.GetTranslationColumnName(entity.TableName), entity.Key, eqName);
            if (entity.Parameter != null)
            {
                foreach (var item in entity.Parameter)
                {
                    var oldName = item.ParameterName;
                    item.ParameterName = oldName + "_con_" + this.Context.ParameterIndex;
                    entity.Where       = entity.Where.Replace(oldName, item.ParameterName);
                }
                this.Context.ParameterIndex++;
                this.Context.Parameters.AddRange(entity.Parameter);
            }
            if (entity.Where.HasValue())
            {
                sql += " AND " + entity.Where;
            }
            sql += " )";
            if (this.Context.ResolveType.IsIn(ResolveExpressType.SelectMultiple, ResolveExpressType.SelectSingle, ResolveExpressType.Update))
            {
                parameter.BaseParameter.CommonTempData = sql;
            }
            else
            {
                AppendMember(parameter, parameter.IsLeft, sql);
            }
        }
        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 = 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")
                });
            }
            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);
        }
Exemple #6
0
        private void AppendModel(ExpressionParameter parameter, MethodCallExpressionModel model, Expression item)
        {
            parameter.CommonTempData = CommonTempDataType.Result;
            base.Expression          = item;
            if (item.Type == UtilConstants.DateType && parameter.CommonTempData.ObjToString() == CommonTempDataType.Result.ToString() && item.ToString() == "DateTime.Now.Date")
            {
                parameter.CommonTempData = DateTime.Now.Date;
            }
            else if (model.Name == "ToString" && item is ConstantExpression && (item as ConstantExpression).Type.IsEnum())
            {
                parameter.CommonTempData = item.ToString();
            }
            else
            {
                base.Start();
            }
            var methodCallExpressionArgs = new MethodCallExpressionArgs()
            {
                IsMember   = parameter.ChildExpression is MemberExpression && !ExpressionTool.IsConstExpression(parameter.ChildExpression as MemberExpression),
                MemberName = parameter.CommonTempData
            };

            if (methodCallExpressionArgs.IsMember && parameter.ChildExpression != null && parameter.ChildExpression.ToString() == "DateTime.Now")
            {
                methodCallExpressionArgs.IsMember = false;
            }
            var value = methodCallExpressionArgs.MemberName;

            if (methodCallExpressionArgs.IsMember)
            {
                var childExpression = parameter.ChildExpression as MemberExpression;
                if (childExpression.Expression != null && childExpression.Expression is ConstantExpression)
                {
                    methodCallExpressionArgs.IsMember = false;
                }
            }
            if (methodCallExpressionArgs.IsMember == false && (item is MethodCallExpression && item.ToString() == "GetDate()") || (item is UnaryExpression && ((UnaryExpression)item).Operand.ToString() == "GetDate()"))
            {
                var parameterName = this.Context.SqlParameterKeyWord + ExpressionConst.MethodConst + this.Context.ParameterIndex;
                this.Context.ParameterIndex++;
                methodCallExpressionArgs.MemberName  = value;
                methodCallExpressionArgs.MemberValue = null;
            }
            else if (methodCallExpressionArgs.IsMember == false)
            {
                var parameterName = this.Context.SqlParameterKeyWord + ExpressionConst.MethodConst + this.Context.ParameterIndex;
                this.Context.ParameterIndex++;
                methodCallExpressionArgs.MemberName  = parameterName;
                methodCallExpressionArgs.MemberValue = value;
                this.Context.Parameters.Add(new SugarParameter(parameterName, value));
            }
            model.Args.Add(methodCallExpressionArgs);
            parameter.ChildExpression = null;
        }
Exemple #7
0
        public UnaryExpressionResolve(ExpressionParameter parameter) : base(parameter)
        {
            var expression    = base.Expression as UnaryExpression;
            var baseParameter = parameter.BaseParameter;

            switch (this.Context.ResolveType)
            {
            case ResolveExpressType.WhereSingle:
            case ResolveExpressType.WhereMultiple:
            case ResolveExpressType.FieldSingle:
            case ResolveExpressType.FieldMultiple:
            case ResolveExpressType.SelectSingle:
            case ResolveExpressType.SelectMultiple:
            case ResolveExpressType.ArraySingle:
            case ResolveExpressType.ArrayMultiple:
            case ResolveExpressType.Update:
                var nodeType = expression.NodeType;
                base.Expression = expression.Operand;
                var isMember     = expression.Operand is MemberExpression;
                var isConst      = expression.Operand is ConstantExpression;
                var offsetIsNull = (parameter.OppsiteExpression is ConstantExpression) &&
                                   (parameter.OppsiteExpression as ConstantExpression).Value == null &&
                                   ExpressionTool.IsComparisonOperator(expression.Operand);
                if (isMember && offsetIsNull)
                {
                    Append(parameter, nodeType);
                }
                else if (baseParameter.CurrentExpression is NewArrayExpression)
                {
                    Result(parameter, nodeType);
                }
                else if (base.Expression is BinaryExpression || parameter.BaseExpression is BinaryExpression || baseParameter.CommonTempData.ObjToString() == CommonTempDataType.Append.ToString())
                {
                    Append(parameter, nodeType);
                }
                else if (isMember)
                {
                    MemberLogic(parameter, baseParameter, nodeType);
                }
                else if (isConst)
                {
                    Result(parameter, nodeType);
                }
                else
                {
                    Append(parameter, nodeType);
                }
                break;

            default:
                break;
            }
        }
Exemple #8
0
        public BinaryExpressionResolve(ExpressionParameter parameter) : base(parameter)
        {
            var expression           = this.Expression as BinaryExpression;
            var operatorValue        = parameter.OperatorValue = ExpressionTool.GetOperator(expression.NodeType);
            var isEqual              = expression.NodeType == ExpressionType.Equal;
            var isComparisonOperator = ExpressionTool.IsComparisonOperator(expression);

            base.ExactExpression = expression;
            var leftExpression  = expression.Left;
            var rightExpression = expression.Right;
            var leftIsBinary    = leftExpression is BinaryExpression;
            var rightBinary     = rightExpression is BinaryExpression;
            var lbrs            = leftIsBinary && !rightBinary;
            var lsrb            = !leftIsBinary && rightBinary;
            var lbrb            = rightBinary && leftIsBinary;
            var lsbs            = !leftIsBinary && !rightBinary;
            var isAppend        = !base.Context.Result.Contains(ExpressionConst.FormatSymbol);

            if (isAppend)
            {
                base.Context.Result.Append(ExpressionConst.LeftParenthesis);
                base.Context.Result.Append(ExpressionConst.FormatSymbol);
            }
            else
            {
                base.Context.Result.Replace(ExpressionConst.FormatSymbol, ExpressionConst.LeftParenthesis + ExpressionConst.FormatSymbol);
            }
            parameter.LeftExpression  = leftExpression;
            parameter.RightExpression = rightExpression;
            base.Expression           = leftExpression;
            base.IsLeft = true;
            base.Start();
            base.IsLeft     = false;
            base.Expression = rightExpression;
            base.Start();
            base.IsLeft = null;
            if (lsbs && parameter.ValueIsNull)
            {
                base.Context.Result.Replace(ExpressionConst.ExpressionReplace + parameter.Index, isEqual ? "IS" : "IS NOT");
                base.Context.Result.Replace(ExpressionConst.ExpressionReplace + (parameter.Index + 1), isEqual ? "IS" : "IS NOT");
            }
            else
            {
                base.Context.Result.Replace(ExpressionConst.ExpressionReplace + parameter.Index, operatorValue);
                base.Context.Result.Replace(ExpressionConst.ExpressionReplace + (parameter.Index + 1), operatorValue);
            }
            base.Context.Result.Append(ExpressionConst.RightParenthesis);
            if (parameter.BaseExpression is BinaryExpression && parameter.IsLeft == true)
            {
                base.Context.Result.Append(" " + ExpressionConst.ExpressionReplace + parameter.BaseParameter.Index + " ");
            }
        }
Exemple #9
0
        private void ResolveMemberValue(ExpressionParameter parameter, ExpressionParameter baseParameter, bool?isLeft, bool isSetTempData, MemberExpression expression)
        {
            var value = ExpressionTool.GetMemberValue(expression.Member, expression);

            if (isSetTempData)
            {
                baseParameter.CommonTempData = value;
            }
            else
            {
                AppendValue(parameter, isLeft, value);
            }
        }
Exemple #10
0
        private void AppendItem(ExpressionParameter parameter, string name, IEnumerable <Expression> args, MethodCallExpressionModel model, Expression item)
        {
            if (ExpressionTool.IsUnConvertExpress(item))
            {
                item = (item as UnaryExpression).Operand;
            }
            var isBinaryExpression = item is BinaryExpression || item is MethodCallExpression;
            var isConst            = item is ConstantExpression;
            var isIIF           = name == "IIF";
            var isIFFBoolMember = isIIF && (item is MemberExpression) && (item as MemberExpression).Type == UtilConstants.BoolType;
            var isIFFUnary      = isIIF && (item is UnaryExpression) && (item as UnaryExpression).Operand.Type == UtilConstants.BoolType;
            var isIFFBoolBinary = isIIF && (item is BinaryExpression) && (item as BinaryExpression).Type == UtilConstants.BoolType;
            var isIFFBoolMethod = isIIF && (item is MethodCallExpression) && (item as MethodCallExpression).Type == UtilConstants.BoolType;
            var isFirst         = item == args.First();

            if (isFirst && isIIF && isConst)
            {
                var value = (item as ConstantExpression).Value.ObjToBool() ? this.Context.DbMehtods.True() : this.Context.DbMehtods.False();
                var methodCallExpressionArgs = new MethodCallExpressionArgs()
                {
                    IsMember    = true,
                    MemberName  = value,
                    MemberValue = value
                };
                model.Args.Add(methodCallExpressionArgs);
            }
            else if (isIFFUnary && !isFirst)
            {
                AppendModelByIIFMember(parameter, model, (item as UnaryExpression).Operand);
            }
            else if (isIFFBoolMember && !isFirst)
            {
                AppendModelByIIFMember(parameter, model, item);
            }
            else if (isIFFBoolBinary && !isFirst)
            {
                AppendModelByIIFBinary(parameter, model, item);
            }
            else if (isIFFBoolMethod && !isFirst)
            {
                AppendModelByIIFMethod(parameter, model, item);
            }
            else if (isBinaryExpression)
            {
                model.Args.Add(GetMethodCallArgs(parameter, item));
            }
            else
            {
                AppendModel(parameter, model, item);
            }
        }
Exemple #11
0
        public IUpdateable <T> UpdateColumns(Expression <Func <T, bool> > columns)
        {
            var binaryExp = columns.Body as BinaryExpression;

            Check.Exception(!binaryExp.NodeType.IsIn(ExpressionType.Equal), "No support {0}", columns.ToString());
            Check.Exception(!(binaryExp.Left is MemberExpression), "No support {0}", columns.ToString());
            Check.Exception(ExpressionTool.IsConstExpression(binaryExp.Left as MemberExpression), "No support {0}", columns.ToString());
            var    expResult = UpdateBuilder.GetExpressionValue(columns, ResolveExpressType.WhereSingle).GetResultString().Replace("))", ") )").Replace("((", "( (").Trim().TrimStart('(').TrimEnd(')');
            string key       = SqlBuilder.GetNoTranslationColumnName(expResult);

            UpdateBuilder.SetValues.Add(new KeyValuePair <string, string>(SqlBuilder.GetTranslationColumnName(key), expResult));
            this.UpdateBuilder.DbColumnInfoList = this.UpdateBuilder.DbColumnInfoList.Where(it => UpdateBuilder.SetValues.Any(v => SqlBuilder.GetNoTranslationColumnName(v.Key).Equals(it.DbColumnName, StringComparison.CurrentCultureIgnoreCase) || SqlBuilder.GetNoTranslationColumnName(v.Key).Equals(it.PropertyName, StringComparison.CurrentCultureIgnoreCase)) || it.IsPrimarykey == true).ToList();
            return(this);
        }
        public MethodCallExpressionResolve(ExpressionParameter parameter) : base(parameter)
        {
            var    express             = base.Expression as MethodCallExpression;
            var    isLeft              = parameter.IsLeft;
            string methodName          = express.Method.Name;
            var    isValidNativeMethod = MethodMapping.ContainsKey(methodName) && express.Method.DeclaringType.Namespace == ("System");
            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;
            }
            if (!isValidNativeMethod && express.Method.DeclaringType.Namespace.IsIn("System.Linq", "System.Collections.Generic") && methodName == "Contains")
            {
                methodName          = "ContainsArray";
                isValidNativeMethod = true;
            }
            if (isValidNativeMethod)
            {
                NativeExtensionMethod(parameter, express, isLeft, MethodMapping[methodName], appendArgs);
            }
            else
            {
                SqlFuncMethod(parameter, express, isLeft);
            }
        }
Exemple #13
0
        public MemberNoExpressionResolve(ExpressionParameter parameter) : base(parameter)
        {
            var    expression    = base.Expression as MemberExpression;
            var    isLeft        = parameter.IsLeft;
            object value         = null;
            var    isField       = expression.Member is System.Reflection.FieldInfo;
            var    isProperty    = expression.Member is System.Reflection.PropertyInfo;
            var    baseParameter = parameter.BaseParameter;
            var    isSetTempData = baseParameter.CommonTempData.HasValue() && baseParameter.CommonTempData.Equals(CommonTempDataType.Result);

            if (isField)
            {
                value = ExpressionTool.GetFiledValue(expression);
            }
            else if (isProperty)
            {
                value = ExpressionTool.GetPropertyValue(expression);
            }
            switch (base.Context.ResolveType)
            {
            case ResolveExpressType.WhereSingle:
            case ResolveExpressType.WhereMultiple:
            case ResolveExpressType.SelectSingle:
            case ResolveExpressType.SelectMultiple:
            case ResolveExpressType.Update:
                if (isSetTempData)
                {
                    baseParameter.CommonTempData = value;
                }
                else
                {
                    AppendValue(parameter, isLeft, value);
                }
                break;

            case ResolveExpressType.FieldSingle:
                break;

            case ResolveExpressType.FieldMultiple:
                break;

            default:
                break;
            }
        }
 private void NewValueType(ExpressionParameter parameter, NewExpression expression)
 {
     try
     {
         var value         = ExpressionTool.DynamicInvoke(expression);
         var isSetTempData = parameter.CommonTempData.HasValue() && parameter.CommonTempData.Equals(CommonTempDataType.Result);
         if (isSetTempData)
         {
             parameter.CommonTempData = value;
         }
         else
         {
             AppendValue(parameter, parameter.IsLeft, value);
         }
     }
     catch (Exception ex)
     {
         Check.Exception(expression.Type == UtilConstants.DateType, "ThrowNotSupportedException {0} ", ex.ToString());
     }
 }
Exemple #15
0
 private void CusMethod(ExpressionParameter parameter, MethodCallExpression express, bool?isLeft)
 {
     try
     {
         var constValue = ExpressionTool.DynamicInvoke(express);
         parameter.BaseParameter.CommonTempData = constValue;
         var parameterName = base.AppendParameter(constValue);
         if (parameter.BaseParameter.CommonTempData != null && parameter.BaseParameter.CommonTempData.Equals(CommonTempDataType.Result))
         {
             this.Context.Result.Append(parameterName);
         }
         else
         {
             base.AppendValue(parameter, isLeft, parameterName);
         }
     }
     catch
     {
         Check.Exception(true, string.Format(ErrorMessage.MethodError, express.Method.Name));
     }
 }
        public IUpdateable <T> SetColumns(Expression <Func <T, bool> > columns)
        {
            ThrowUpdateByObject();
            CheckTranscodeing();
            var binaryExp = columns.Body as BinaryExpression;

            Check.Exception(!binaryExp.NodeType.IsIn(ExpressionType.Equal), "No support {0}", columns.ToString());
            Check.Exception(!(binaryExp.Left is MemberExpression) && !(binaryExp.Left is UnaryExpression), "No support {0}", columns.ToString());
            Check.Exception(ExpressionTool.IsConstExpression(binaryExp.Left as MemberExpression), "No support {0}", columns.ToString());
            var expResult = UpdateBuilder.GetExpressionValue(columns, ResolveExpressType.WhereSingle).GetResultString().Replace(")", " )").Replace("(", "( ").Trim().TrimStart('(').TrimEnd(')');

            if (expResult.EndsWith(" IS NULL  "))
            {
                expResult = Regex.Split(expResult, " IS NULL  ")[0] + " = NULL ";
            }
            string key = SqlBuilder.GetNoTranslationColumnName(expResult);

            UpdateBuilder.SetValues.Add(new KeyValuePair <string, string>(SqlBuilder.GetTranslationColumnName(key), expResult));
            this.UpdateBuilder.DbColumnInfoList = this.UpdateBuilder.DbColumnInfoList.Where(it => (UpdateParameterIsNull == false && IsPrimaryKey(it)) || UpdateBuilder.SetValues.Any(v => SqlBuilder.GetNoTranslationColumnName(v.Key).Equals(it.DbColumnName, StringComparison.CurrentCultureIgnoreCase) || SqlBuilder.GetNoTranslationColumnName(v.Key).Equals(it.PropertyName, StringComparison.CurrentCultureIgnoreCase)) || it.IsPrimarykey == true).ToList();
            AppendSets();
            return(this);
        }
 private void Select(MemberInitExpression expression, ExpressionParameter parameter, bool isSingle)
 {
     foreach (MemberBinding binding in expression.Bindings)
     {
         if (binding.BindingType != MemberBindingType.Assignment)
         {
             throw new NotSupportedException();
         }
         MemberAssignment memberAssignment = (MemberAssignment)binding;
         var memberName = memberAssignment.Member.Name;
         var item       = memberAssignment.Expression;
         if (IsNullable(item) && item is UnaryExpression)
         {
             var memtype = ExpressionTool.GetMemberInfoType(memberAssignment.Member);
             if (IsNullable(memtype) && UtilMethods.GetUnderType(memtype) == UtilMethods.GetUnderType(item.Type))
             {
                 item = (item as UnaryExpression).Operand;
             }
         }
         ResolveNewExpressions(parameter, item, memberName);
     }
 }
 private void ResolveCallValue(ExpressionParameter parameter, ExpressionParameter baseParameter, MemberExpression expression, bool?isLeft, bool isSetTempData, bool isSingle)
 {
     try
     {
         baseParameter.ChildExpression = expression;
         string fieldName = string.Empty;
         if (isSetTempData)
         {
             var value = ExpressionTool.DynamicInvoke(expression);
             baseParameter.CommonTempData = value;
         }
         else
         {
             var value = ExpressionTool.DynamicInvoke(expression);
             base.AppendValue(parameter, isLeft, value);
         }
     }
     catch
     {
         Check.Exception(true, "Not Support {0}", expression.ToString());
     }
 }
Exemple #19
0
        public MemberConstExpressionResolve(ExpressionParameter parameter) : base(parameter)
        {
            var    expression    = base.Expression as MemberExpression;
            var    isLeft        = parameter.IsLeft;
            object value         = ExpressionTool.GetMemberValue(expression.Member, expression);
            var    baseParameter = parameter.BaseParameter;
            var    isSetTempData = baseParameter.CommonTempData.IsValuable() && baseParameter.CommonTempData.Equals(CommonTempDataType.Result);

            switch (parameter.Context.ResolveType)
            {
            case ResolveExpressType.Update:
            case ResolveExpressType.SelectSingle:
            case ResolveExpressType.SelectMultiple:
                if (value != null && value.GetType().IsEnum())
                {
                    value = Convert.ToInt64(value);
                }
                parameter.BaseParameter.CommonTempData = value;
                break;

            case ResolveExpressType.WhereSingle:
            case ResolveExpressType.WhereMultiple:
                if (isSetTempData)
                {
                    baseParameter.CommonTempData = value;
                }
                else
                {
                    AppendValue(parameter, isLeft, value);
                }
                break;

            case ResolveExpressType.FieldSingle:
            case ResolveExpressType.FieldMultiple:
                break;
            }
        }
        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;
        }
Exemple #21
0
        private void ResolveDateValue(ExpressionParameter parameter, bool?isLeft, MemberExpression expression)
        {
            var name = expression.Member.Name;
            var oldCommonTempDate = parameter.CommonTempData;

            parameter.CommonTempData = CommonTempDataType.Result;
            this.Expression          = expression.Expression;
            var isConst = this.Expression is ConstantExpression;

            if (this.Expression.Type == UtilConstants.DateType && this.Expression.ToString() == "DateTime.Now")
            {
                this.Expression = expression;
                var parameterName = base.AppendParameter(ExpressionTool.GetMemberValue(expression.Member, expression));
                base.AppendMember(parameter, isLeft, parameterName);
            }
            else
            {
                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;
        }
Exemple #22
0
        public string GetValue(Expression expression)
        {
            var    exp   = expression as MethodCallExpression;
            object value = null;

            try
            {
                value = ExpressionTool.DynamicInvoke(exp.Arguments[0]);
            }
            catch
            {
                Check.Exception(true, ErrorMessage.WhereIFCheck, exp.Arguments[0].ToString());
            }
            if (Regex.Matches(expression.ToString(), "Subqueryable").Count >= 2)
            {
                new SubSelect()
                {
                    Context = this.Context
                }.SetShortNameNext(exp, "+");
            }
            var isWhere = Convert.ToBoolean(value);

            if (!Convert.ToBoolean(isWhere))
            {
                return("WHERE 1=1 ");
            }
            var argExp            = exp.Arguments[1];
            var result            = "WHERE " + SubTools.GetMethodValue(Context, argExp, ResolveExpressType.WhereMultiple);;
            var selfParameterName = Context.GetTranslationColumnName((argExp as LambdaExpression).Parameters.First().Name) + UtilConstants.Dot;

            if (this.Context.JoinIndex == 0)
            {
                result = result.Replace(selfParameterName, SubTools.GetSubReplace(this.Context));
            }
            return(result);
        }
Exemple #23
0
        private void AppendModel(ExpressionParameter parameter, MethodCallExpressionModel model, Expression item)
        {
            parameter.CommonTempData = CommonTempDataType.Result;
            base.Expression          = item;
            base.Start();
            var methodCallExpressionArgs = new MethodCallExpressionArgs()
            {
                IsMember   = parameter.ChildExpression is MemberExpression && !ExpressionTool.IsConstExpression(parameter.ChildExpression as MemberExpression),
                MemberName = parameter.CommonTempData
            };

            if (methodCallExpressionArgs.IsMember && parameter.ChildExpression != null && parameter.ChildExpression.ToString() == "DateTime.Now")
            {
                methodCallExpressionArgs.IsMember = false;
            }
            var value = methodCallExpressionArgs.MemberName;

            if (methodCallExpressionArgs.IsMember)
            {
                var childExpression = parameter.ChildExpression as MemberExpression;
                if (childExpression.Expression != null && childExpression.Expression is ConstantExpression)
                {
                    methodCallExpressionArgs.IsMember = false;
                }
            }
            if (methodCallExpressionArgs.IsMember == false)
            {
                var parameterName = this.Context.SqlParameterKeyWord + ExpressionConst.MethodConst + this.Context.ParameterIndex;
                this.Context.ParameterIndex++;
                methodCallExpressionArgs.MemberName  = parameterName;
                methodCallExpressionArgs.MemberValue = value;
                this.Context.Parameters.Add(new SugarParameter(parameterName, value));
            }
            model.Args.Add(methodCallExpressionArgs);
            parameter.ChildExpression = null;
        }
Exemple #24
0
        private void Update(MemberInitExpression expression, ExpressionParameter parameter)
        {
            int i = 0;

            foreach (MemberBinding binding in expression.Bindings)
            {
                ++i;
                if (binding.BindingType != MemberBindingType.Assignment)
                {
                    throw new NotSupportedException();
                }
                MemberAssignment memberAssignment = (MemberAssignment)binding;
                var type       = expression.Type;
                var memberName = this.Context.GetDbColumnName(type.Name, memberAssignment.Member.Name);
                var item       = memberAssignment.Expression;
                if ((item is MemberExpression) && ((MemberExpression)item).Expression == null)
                {
                    var    paramterValue = ExpressionTool.DynamicInvoke(item);
                    string parameterName = AppendParameter(paramterValue);
                    this.Context.Result.Append(base.Context.GetEqString(memberName, parameterName));
                }
                else if (IsMethod(item))
                {
                    if (item is UnaryExpression)
                    {
                        item = (item as UnaryExpression).Operand;
                    }
                    var callMethod = item as MethodCallExpression;
                    if (MethodTimeMapping.Any(it => it.Key == callMethod.Method.Name) || MethodMapping.Any(it => it.Key == callMethod.Method.Name) || IsExtMethod(callMethod.Method.Name) || IsSubMethod(callMethod) || callMethod.Method.DeclaringType.FullName.StartsWith(UtilConstants.AssemblyName + UtilConstants.Dot))
                    {
                        MethodCall(parameter, memberName, item);
                    }
                    else
                    {
                        var    paramterValue = ExpressionTool.DynamicInvoke(item);
                        string parameterName = AppendParameter(paramterValue);
                        this.Context.Result.Append(base.Context.GetEqString(memberName, parameterName));
                    }
                }
                else if (IsConst(item))
                {
                    base.Expression = item;
                    base.Start();
                    string parameterName = this.Context.SqlParameterKeyWord + ExpressionConst.Const + this.Context.ParameterIndex;
                    parameter.Context.Result.Append(base.Context.GetEqString(memberName, parameterName));
                    this.Context.Parameters.Add(new SugarParameter(parameterName, parameter.CommonTempData));
                    this.Context.ParameterIndex++;
                }
                else if (item is MemberExpression)
                {
                    if (base.Context.Result.IsLockCurrentParameter == false)
                    {
                        base.Context.Result.CurrentParameter       = parameter;
                        base.Context.Result.IsLockCurrentParameter = true;
                        parameter.IsAppendTempDate();
                        base.Expression = item;
                        base.Start();
                        parameter.IsAppendResult();
                        parameter.Context.Result.Append(base.Context.GetEqString(memberName, parameter.CommonTempData.ObjToString()));
                        base.Context.Result.CurrentParameter = null;
                    }
                }
                else if (item is BinaryExpression)
                {
                    var result = GetNewExpressionValue(item);
                    this.Context.Result.Append(base.Context.GetEqString(memberName, result));
                }
            }
        }
        private void Update(MemberInitExpression expression, ExpressionParameter parameter)
        {
            int i = 0;

            foreach (MemberBinding binding in expression.Bindings)
            {
                ++i;
                if (binding.BindingType != MemberBindingType.Assignment)
                {
                    throw new NotSupportedException();
                }
                MemberAssignment memberAssignment = (MemberAssignment)binding;
                var type       = expression.Type;
                var memberName = this.Context.GetDbColumnName(type.Name, memberAssignment.Member.Name);
                var item       = memberAssignment.Expression;

                //Column IsJson Handler
                if (memberAssignment.Member.CustomAttributes != null)
                {
                    var customAttribute = memberAssignment.Member.GetCustomAttribute <SugarColumn>();

                    if (customAttribute?.IsJson ?? false)
                    {
                        var paramterValue = ExpressionTool.DynamicInvoke(item);
                        var parameterName = AppendParameter(new SerializeService().SerializeObject(paramterValue));
                        this.Context.Result.Append(base.Context.GetEqString(memberName, parameterName));

                        continue;
                    }
                }

                if ((item is MemberExpression) && ((MemberExpression)item).Expression == null)
                {
                    var    paramterValue = ExpressionTool.DynamicInvoke(item);
                    string parameterName = AppendParameter(paramterValue);
                    this.Context.Result.Append(base.Context.GetEqString(memberName, parameterName));
                }
                else if (IsNotMember(item))
                {
                    if (base.Context.Result.IsLockCurrentParameter == false)
                    {
                        base.Context.Result.CurrentParameter       = parameter;
                        base.Context.Result.IsLockCurrentParameter = true;
                        parameter.IsAppendTempDate();
                        base.Expression = item;
                        base.Expression = (item as UnaryExpression).Operand;
                        base.Start();
                        parameter.IsAppendResult();
                        var result = this.Context.DbMehtods.IIF(new MethodCallExpressionModel()
                        {
                            Args = new List <MethodCallExpressionArgs>()
                            {
                                new MethodCallExpressionArgs()
                                {
                                    IsMember = true, MemberName = parameter.CommonTempData.ObjToString() + "=1"
                                },
                                new MethodCallExpressionArgs()
                                {
                                    IsMember = true, MemberName = AppendParameter(0)
                                },
                                new MethodCallExpressionArgs()
                                {
                                    IsMember = true, MemberName = AppendParameter(1)
                                }
                            }
                        });
                        parameter.Context.Result.Append(base.Context.GetEqString(memberName, result));
                        base.Context.Result.CurrentParameter = null;
                    }
                }
                else if (IsNotParameter(item))
                {
                    try
                    {
                        parameter.Context.Result.Append(base.Context.GetEqString(memberName, AppendParameter(ExpressionTool.DynamicInvoke(item).ObjToBool())));
                    }
                    catch
                    {
                        throw new NotSupportedException(item.ToString());
                    }
                }
                else if (IsMethod(item))
                {
                    if (item is UnaryExpression)
                    {
                        item = (item as UnaryExpression).Operand;
                    }
                    var callMethod = item as MethodCallExpression;
                    if (MethodTimeMapping.Any(it => it.Key == callMethod.Method.Name) || MethodMapping.Any(it => it.Key == callMethod.Method.Name) || IsExtMethod(callMethod.Method.Name) || IsSubMethod(callMethod) || callMethod.Method.DeclaringType.FullName.StartsWith(UtilConstants.AssemblyName + UtilConstants.Dot))
                    {
                        MethodCall(parameter, memberName, item);
                    }
                    else
                    {
                        var    paramterValue = ExpressionTool.DynamicInvoke(item);
                        string parameterName = AppendParameter(paramterValue);
                        this.Context.Result.Append(base.Context.GetEqString(memberName, parameterName));
                    }
                }
                else if (IsConst(item) && IsConvert(item) && UtilMethods.IsNullable(item.Type) && UtilMethods.GetUnderType(item.Type) == UtilConstants.BoolType)
                {
                    item = (item as UnaryExpression).Operand;
                    parameter.Context.Result.Append(base.Context.GetEqString(memberName, GetNewExpressionValue(item)));
                }
                else if (IsConst(item))
                {
                    base.Expression = item;
                    base.Start();
                    string parameterName = this.Context.SqlParameterKeyWord + ExpressionConst.Const + this.Context.ParameterIndex;
                    parameter.Context.Result.Append(base.Context.GetEqString(memberName, parameterName));
                    var addItem  = new SugarParameter(parameterName, parameter.CommonTempData);
                    var dataType = UtilMethods.GetUnderType(item.Type);
                    if (addItem.Value == null && dataType == UtilConstants.DateType)
                    {
                        addItem.DbType = System.Data.DbType.Date;
                    }
                    this.Context.Parameters.Add(addItem);
                    this.Context.ParameterIndex++;
                }
                else if (item is MemberExpression)
                {
                    if (base.Context.Result.IsLockCurrentParameter == false)
                    {
                        base.Context.Result.CurrentParameter       = parameter;
                        base.Context.Result.IsLockCurrentParameter = true;
                        parameter.IsAppendTempDate();
                        base.Expression = item;
                        base.Start();
                        parameter.IsAppendResult();
                        parameter.Context.Result.Append(base.Context.GetEqString(memberName, parameter.CommonTempData.ObjToString()));
                        base.Context.Result.CurrentParameter = null;
                    }
                }
                else if (item is BinaryExpression)
                {
                    var result = GetNewExpressionValue(item);
                    if (result.HasValue())
                    {
                        result = result.Replace(",", UtilConstants.ReplaceCommaKey);
                    }
                    this.Context.Result.Append(base.Context.GetEqString(memberName, result));
                }
                else if (item is MemberInitExpression)
                {
                    try
                    {
                        var value         = ExpressionTool.DynamicInvoke(item);
                        var parameterName = AppendParameter(value);
                        parameter.Context.Result.Append(base.Context.GetEqString(memberName, parameterName));
                    }
                    catch (Exception ex)
                    {
                        throw new NotSupportedException("Not Supported " + item.ToString() + " " + ex.Message);
                    }
                }
                else if (item is NewExpression)
                {
                    try
                    {
                        var value         = ExpressionTool.DynamicInvoke(item);
                        var parameterName = AppendParameter(value);
                        parameter.Context.Result.Append(base.Context.GetEqString(memberName, parameterName));
                    }
                    catch (Exception ex)
                    {
                        throw new NotSupportedException("Not Supported " + item.ToString() + " " + ex.Message);
                    }
                }
                else if (item is ConditionalExpression)
                {
                    var result = GetNewExpressionValue(item);
                    this.Context.Result.Append(base.Context.GetEqString(memberName, result));
                }
            }
        }
 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);
 }
Exemple #27
0
        public MemberExpressionResolve(ExpressionParameter parameter) : base(parameter)
        {
            var baseParameter = parameter.BaseParameter;
            var isLeft        = parameter.IsLeft;
            var isSetTempData = baseParameter.CommonTempData.IsValuable() && baseParameter.CommonTempData.Equals(CommonTempDataType.Result);
            var expression    = base.Expression as MemberExpression;
            var isValue       = expression.Member.Name == "Value" && expression.Member.DeclaringType.Name == "Nullable`1";
            var isBool        = expression.Type == PubConst.BoolType;
            var isValueBool   = isValue && isBool && parameter.BaseExpression == null;
            var isLength      = expression.Member.Name == "Length" && (expression.Expression as MemberExpression).Type == PubConst.StringType;
            var isDateValue   = expression.Member.Name.IsIn(Enum.GetNames(typeof(DateType))) && (expression.Expression as MemberExpression).Type == PubConst.DateType;

            if (isLength)
            {
                var oldCommonTempDate = parameter.CommonTempData;
                parameter.CommonTempData = CommonTempDataType.Result;
                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 (isDateValue)
            {
                var name = expression.Member.Name;
                var oldCommonTempDate = parameter.CommonTempData;
                parameter.CommonTempData = CommonTempDataType.Result;
                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 (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 == PubConst.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;
            }
        }
Exemple #28
0
        public MethodCallExpressionResolve(ExpressionParameter parameter) : base(parameter)
        {
            var    express             = base.Expression as MethodCallExpression;
            var    isLeft              = parameter.IsLeft;
            string methodName          = express.Method.Name;
            var    isValidNativeMethod = MethodMapping.ContainsKey(methodName) && express.Method.DeclaringType.Namespace == ("System");
            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.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);
            }
        }
        public MemberExpressionResolve(ExpressionParameter parameter) : base(parameter)
        {
            var baseParameter   = parameter.BaseParameter;
            var expression      = base.Expression as MemberExpression;
            var childExpression = expression.Expression as MemberExpression;
            var memberName      = expression.Member.Name;
            var childIsMember   = childExpression != null;
            var isLeft          = parameter.IsLeft;
            var isSetTempData   = parameter.IsSetTempData;
            var isValue         = memberName == "Value" && expression.Member.DeclaringType.Name == "Nullable`1";
            var isBool          = expression.Type == UtilConstants.BoolType;
            var isValueBool     = isValue && isBool && parameter.BaseExpression == null;
            var isLength        = memberName == "Length" && childIsMember && childExpression.Type == UtilConstants.StringType;
            var isDateValue     = memberName.IsIn(Enum.GetNames(typeof(DateType))) && (childIsMember && childExpression.Type == UtilConstants.DateType);
            var isLogicOperator = ExpressionTool.IsLogicOperator(baseParameter.OperatorValue) || baseParameter.OperatorValue.IsNullOrEmpty();
            var isHasValue      = isLogicOperator && memberName == "HasValue" && expression.Expression != null && expression.NodeType == ExpressionType.MemberAccess;
            var isDateDate      = memberName == "Date" && expression.Expression.Type == UtilConstants.DateType;
            var isMemberValue   = expression.Expression != null && expression.Expression.NodeType != ExpressionType.Parameter && !isValueBool;
            var isSingle        = parameter.Context.ResolveType == ResolveExpressType.WhereSingle;

            if (isLength)
            {
                ResolveLength(parameter, isLeft, expression);
            }
            else if (isHasValue)
            {
                ResolveHasValue(parameter, expression);
            }
            else if (isDateValue)
            {
                ResolveDateValue(parameter, isLeft, expression);
            }
            else if (isValueBool)
            {
                ResolveValueBool(parameter, baseParameter, expression, isLeft, isSingle);
            }
            else if (isValue)
            {
                ResolveValue(parameter, baseParameter, expression, isLeft, isSetTempData, isSingle);
            }
            else if (isDateDate)
            {
                ResolveDateDate(parameter, isLeft, expression);
            }
            else if (isMemberValue)
            {
                ResolveMemberValue(parameter, baseParameter, isLeft, isSetTempData, expression);
            }
            else
            {
                baseParameter.ChildExpression = expression;
                string fieldName = string.Empty;
                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:
                    ResolveWhereLogic(parameter, baseParameter, expression, isLeft, isSetTempData, isSingle);
                    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;
                }
            }
        }
        private void Other(ExpressionParameter parameter)
        {
            var expression           = this.Expression as BinaryExpression;
            var operatorValue        = parameter.OperatorValue = ExpressionTool.GetOperator(expression.NodeType);
            var isEqual              = expression.NodeType == ExpressionType.Equal;
            var isComparisonOperator = ExpressionTool.IsComparisonOperator(expression);

            base.ExactExpression = expression;
            var leftExpression  = expression.Left;
            var rightExpression = expression.Right;
            var leftIsBinary    = leftExpression is BinaryExpression;
            var rightBinary     = rightExpression is BinaryExpression;
            var lbrs            = leftIsBinary && !rightBinary;
            var lsrb            = !leftIsBinary && rightBinary;
            var lbrb            = rightBinary && leftIsBinary;
            var lsbs            = !leftIsBinary && !rightBinary;
            var isAppend        = !base.Context.Result.Contains(ExpressionConst.FormatSymbol);

            if (isAppend)
            {
                base.Context.Result.Append(ExpressionConst.LeftParenthesis);
                base.Context.Result.Append(ExpressionConst.FormatSymbol);
            }
            else
            {
                base.Context.Result.Replace(ExpressionConst.FormatSymbol, ExpressionConst.LeftParenthesis + ExpressionConst.FormatSymbol);
            }
            if (leftExpression is UnaryExpression && (leftExpression as UnaryExpression).Operand is UnaryExpression && (leftExpression as UnaryExpression).NodeType == ExpressionType.Convert)
            {
                leftExpression = (leftExpression as UnaryExpression).Operand;
            }
            if (leftExpression is UnaryExpression && (leftExpression as UnaryExpression).Operand.Type == UtilConstants.BoolType && (leftExpression as UnaryExpression).NodeType == ExpressionType.Convert && rightExpression.Type == UtilConstants.BoolTypeNull)
            {
                leftExpression = (leftExpression as UnaryExpression).Operand;
            }
            if (rightExpression is UnaryExpression && (rightExpression as UnaryExpression).Operand.Type == UtilConstants.BoolType && (rightExpression as UnaryExpression).NodeType == ExpressionType.Convert)
            {
                rightExpression = (rightExpression as UnaryExpression).Operand;
            }
            parameter.LeftExpression  = leftExpression;
            parameter.RightExpression = rightExpression;
            base.Expression           = leftExpression;
            base.IsLeft = true;
            base.Start();
            if (leftExpression is UnaryExpression && leftExpression.Type == UtilConstants.BoolType && !this.Context.Result.Contains(ExpressionConst.ExpressionReplace))
            {
                this.Context.Result.AppendFormat(" {0} ", ExpressionTool.GetOperator(expression.NodeType));
            }
            base.IsLeft     = false;
            base.Expression = rightExpression;
            base.Start();
            base.IsLeft = null;
            if (lsbs && parameter.ValueIsNull)
            {
                base.Context.Result.Replace(ExpressionConst.ExpressionReplace + parameter.Index, isEqual ? "IS" : "IS NOT");
                base.Context.Result.Replace(ExpressionConst.ExpressionReplace + (parameter.Index + 1), isEqual ? "IS" : "IS NOT");
            }
            else
            {
                base.Context.Result.Replace(ExpressionConst.ExpressionReplace + parameter.Index, operatorValue);
                base.Context.Result.Replace(ExpressionConst.ExpressionReplace + (parameter.Index + 1), operatorValue);
            }
            base.Context.Result.Append(ExpressionConst.RightParenthesis);
            if (parameter.BaseExpression is BinaryExpression && parameter.IsLeft == true)
            {
                base.Context.Result.Append(" " + ExpressionConst.ExpressionReplace + parameter.BaseParameter.Index + " ");
            }
        }