Beispiel #1
0
        protected Command GetCommand <T>(string cmdName) where T : class
        {
            MapperBuilder mb  = new MapperBuilder(_parameterPrefix).Build <T>(cmdName);
            Command       cmd = new Command()
            {
                Text = mb.Sql
            };

            return(cmd);
        }
Beispiel #2
0
        public Command Build <T>(string cmdName, Expression <Func <T, bool> > predicate, IDictionary <string, object> dynParameters, OrderBy orderBy)
            where T : class
        {
            Command       cmd = new Command();
            MapperBuilder mb  = new MapperBuilder(_parameterPrefix).Build <T>(cmdName);

            cmd.Text = mb.Sql;
            //解析过滤条件
            if (predicate == null)
            {
                predicate = Common.True <T>();
            }
            ConditionBuilder builder = new ConditionBuilder(_parameterPrefix, cmdName == "SelectJoin" ? "A" : "");

            builder.Build(predicate, mb.PropertiesMap, dynParameters);
            cmd.Text = Regex.Replace(cmd.Text, _placeHolderWhere, builder.Condition, RegexOptions.IgnoreCase);

            //追加OrderBy
            if (orderBy != null && orderBy.Properties.Count() > 0)
            {
                cmd.Text += " Order By ";
                var list = orderBy.Properties.ToList();
                foreach (var item in list)
                {
                    PropertyEx p = null;
                    if (mb.PropertiesMap.TryGetValue(item, out p))
                    {
                        if (list.IndexOf(item) > 0)
                        {
                            cmd.Text += ",";
                        }
                        cmd.Text += p.DbName;
                    }
                }
                cmd.Text += " Desc ";
            }
            //添加脚本参数
            foreach (var bParameter in builder.Parameters)
            {
                //参数
                string parameterName = bParameter.Key;
                cmd.DynamicParameters.Add(parameterName, bParameter.Value);
            }
            return(cmd);
        }
Beispiel #3
0
        internal Command Build <T>(Expression <Func <T, T> > updater, Expression <Func <T, bool> > predicate) where T : class
        {
            //UPDATE TABLE #SET# WHERE 1=1 And FieldName = @p0
            Command cmd = this.Build <T>(CommandBuilder.UpdateByExpr, predicate, null, null);

            MemberInitExpression updateExpr = (MemberInitExpression)updater.Body;
            StringBuilder        sqlBuilder = new StringBuilder(Environment.NewLine);

            for (int i = 0; i < updateExpr.Bindings.Count; i++)
            {
                //SQL片断
                MemberAssignment member = (MemberAssignment)updateExpr.Bindings[i];

                var    propertiesMap = MapperBuilder.GetPropertiesMap <T>();
                string dbName        = member.Member.Name;
                if (propertiesMap.ContainsKey(member.Member.Name))
                {
                    dbName = propertiesMap[member.Member.Name].DbName;
                }
                sqlBuilder.AppendFormat("{0} = {1}{2}", dbName, _parameterPrefix, member.Member.Name);
                if (i < updateExpr.Bindings.Count - 1)
                {
                    sqlBuilder.Append(",");
                }
                sqlBuilder.AppendLine();

                //SQL参数
                if (member.Expression is ConstantExpression)
                {
                    cmd.DynamicParameters.Add(member.Member.Name, ((ConstantExpression)member.Expression).Value);
                }
                else
                {
                    //计算常量
                    PartialEvaluator evaluator = new PartialEvaluator();
                    Expression       evalExpr  = evaluator.Eval(member.Expression);

                    cmd.DynamicParameters.Add(member.Member.Name, ((ConstantExpression)evalExpr).Value);
                }
            }

            cmd.Text = Regex.Replace(cmd.Text, _placeHolderSet, sqlBuilder.ToString(), RegexOptions.IgnoreCase);
            return(cmd);
        }