Example #1
0
        /// <summary>
        /// 解析脚本条件
        /// </summary>
        /// <param name="expression">条件表达式</param>
        public void Build(Expression expression)
        {
            this._lstArguments     = new List <object>();
            this._stcConditions    = new Stack <string>();
            this._parameters       = new XDictionary <string, object>();
            this._parameterMembers = new XDictionary <string, string>();
            string condition = string.Empty;

            //计算常量
            PartialEvaluator evaluator = new PartialEvaluator();
            Expression       evalExpr  = evaluator.Eval(expression);

            //x=>true 表达式,转成 1==1
            if (evalExpr.NodeType == ExpressionType.Constant)
            {
                evalExpr = VisitBoolean(evalExpr);
            }
            //遍历整个树节点
            this.Visit(evalExpr);

            if (this._stcConditions.Count > 0)
            {
                condition = string.Format(" AND {0}", _stcConditions.Pop());
                MatchCollection matches = Regex.Matches(condition, string.Format(@"{0}(?<Name>p(?<Index>[0-9]+))", _parameterPrefix));
                foreach (Match match in matches)
                {
                    if (!match.Success)
                    {
                        continue;
                    }

                    string index         = match.Groups["Index"].Value;
                    string parameterName = match.Groups["Name"].Value;
                    if (_parameters[parameterName] == null)
                    {
                        _parameters.Add(parameterName, _lstArguments[Convert.ToInt32(index)]);
                    }
                }
            }

            this.Condition  = condition;
            this.Parameters = _parameters;
        }
Example #2
0
        public void Build(Expression expression, Dictionary <String, PropertyEx> propertiesMap, IDictionary <string, object> dynParameters = null)
        {
            this._lstArguments     = new List <object>();
            this._stcConditions    = new Stack <string>();
            this._parameters       = new XDictionary <string, object>();
            this._parameterMembers = new XDictionary <string, string>();
            this._propertiesMap    = propertiesMap;
            string condition = string.Empty;



            //计算常量
            PartialEvaluator evaluator = new PartialEvaluator();
            Expression       evalExpr  = evaluator.Eval(expression);

            //x=>true 表达式,转成 1==1
            if (evalExpr.NodeType == ExpressionType.Constant)
            {
                evalExpr = VisitBoolean(evalExpr);
            }
            //遍历整个树节点
            this.Visit(evalExpr);

            if (this._stcConditions.Count > 0)
            {
                condition = string.Format(" AND {0}", _stcConditions.Pop());
                MatchCollection matches = Regex.Matches(condition, string.Format(@"{0}(?<Name>p(?<Index>[0-9]+))", _parameterPrefix));
                foreach (Match match in matches)
                {
                    if (!match.Success)
                    {
                        continue;
                    }

                    string index         = match.Groups["Index"].Value;
                    string parameterName = match.Groups["Name"].Value;
                    if (_parameters[parameterName] == null)
                    {
                        _parameters.Add(parameterName, _lstArguments[Convert.ToInt32(index)]);
                    }
                }
            }
            //处理动态参数
            if (dynParameters != null)
            {
                StringBuilder sb = new StringBuilder();
                foreach (KeyValuePair <string, object> item in dynParameters)
                {
                    var pm = propertiesMap.Where(x => x.Key == item.Key);
                    if (pm.Count() != 0)
                    {
                        sb.AppendFormat(" AND {3}{0} = {1}{2} ", pm.First().Value.DbName, _parameterPrefix, item.Key, TableAlias);
                    }
                    else
                    {
                        sb.AppendFormat(" AND {3}{0} = {1}{2} ", item.Key, _parameterPrefix, item.Key, TableAlias);
                    }
                    _parameters.Add(item.Key, item.Value);
                }
                condition = condition + sb.ToString();
            }

            this.Condition  = condition;
            this.Parameters = _parameters;
        }