Ejemplo n.º 1
0
 public static string CallProcMethodGenerator(DbParameter[] dbParameter, string spname, int prefiexIndex, bool isPaging)
 {
     if (string.IsNullOrEmpty(spname))
     {
         throw new ArgumentNullException("spname");
     }
     if ((dbParameter == null) || (dbParameter.Length == 0))
     {
         if (((spname.IndexOf("get", StringComparison.OrdinalIgnoreCase) >= 0) || (spname.IndexOf("query", StringComparison.OrdinalIgnoreCase) >= 0)) || (spname.IndexOf("search", StringComparison.OrdinalIgnoreCase) >= 0))
         {
             return (string.Format("//TModel item = DbHelper.GetDataItem<TModel>(\"{0}\", null);\r\n", spname) + string.Format("List<TModel> list = DbHelper.FillList<TModel>(\"{0}\", null);\r\n", spname));
         }
         return string.Format("DbHelper.ExecuteNonQuery(\"{0}\", null);", spname);
     }
     bool flag = false;
     bool flag2 = false;
     bool flag3 = false;
     bool flag4 = false;
     foreach (DbParameter parameter in dbParameter)
     {
         if ((parameter.Direction == ParameterDirection.Output) || (parameter.Direction == ParameterDirection.InputOutput))
         {
             flag = true;
             if (string.Compare(parameter.ParameterName.Substring(prefiexIndex), DbContext.STR_TotalRecords, StringComparison.OrdinalIgnoreCase) == 0)
             {
                 flag4 = true;
             }
         }
         else
         {
             if (string.Compare(parameter.ParameterName.Substring(prefiexIndex), DbContext.STR_PageIndex, StringComparison.OrdinalIgnoreCase) == 0)
             {
                 flag2 = true;
             }
             if (string.Compare(parameter.ParameterName.Substring(prefiexIndex), DbContext.STR_PageSize, StringComparison.OrdinalIgnoreCase) == 0)
             {
                 flag3 = true;
             }
         }
     }
     string str2 = spname.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries).Aggregate((i, j) =>
     {
         return i.Substring(0, 1).ToUpper() + i.Substring(1, i.Length - 1).ToLower()
             + j.Substring(0, 1).ToUpper() + j.Substring(1, j.Length - 1).ToLower();
     }).Replace(".", "");
     str2 = (isPaging || flag) ? (str2.FilterStr() + "Parameters") : string.Empty;
     StringBuilder builder = new StringBuilder();
     if (!string.IsNullOrEmpty(str2))
     {
         if ((flag2 && flag3) && flag4)
         {
             builder.Append("public class ").Append(str2).AppendLine(" : PagingInfo {");
             foreach (DbParameter parameter in dbParameter)
             {
                 if (parameter.DbType == DbType.Object)
                     continue;
                 if (((string.Compare(parameter.ParameterName.Substring(prefiexIndex), DbContext.STR_TotalRecords, StringComparison.OrdinalIgnoreCase) != 0) && (string.Compare(parameter.ParameterName.Substring(prefiexIndex), DbContext.STR_PageIndex, StringComparison.OrdinalIgnoreCase) != 0)) && (string.Compare(parameter.ParameterName.Substring(prefiexIndex), DbContext.STR_PageSize, StringComparison.OrdinalIgnoreCase) != 0))
                 {
                     builder.AppendFormat("\tpublic {0} {1} {{ get; set; }}\r\n", TypeConverter.ConvertToClrType(parameter.DbType), parameter.ParameterName.Substring(prefiexIndex));
                 }
             }
         }
         else
         {
             builder.Append("public class ").Append(str2).AppendLine(" {");
             foreach (DbParameter parameter in dbParameter)
             {
                 if (parameter.DbType == DbType.Object)
                     continue;
                 builder.AppendFormat("\tpublic {0} {1} {{ get; set; }}\r\n", TypeConverter.ConvertToClrType(parameter.DbType), parameter.ParameterName.Substring(prefiexIndex));
             }
         }
         builder.AppendLine("}\r\n");
     }
     builder.AppendFormat("var parameters = new {0}{{\r\n", str2);
     int num2 = 1;
     foreach (DbParameter parameter in dbParameter)
     {
         if (parameter.DbType == DbType.Object)
             continue;
         builder.AppendFormat("\t{0} = xxxxxxx{1}{2}\r\n", parameter.ParameterName.Substring(prefiexIndex), (num2++ < dbParameter.Length) ? "," : "", ((parameter.Direction == ParameterDirection.Output) || (parameter.Direction == ParameterDirection.InputOutput)) ? "\t\t// output" : "");
     }
     builder.AppendLine("};");
     if ((flag && flag2) && flag3)
     {
         builder.AppendFormat("List<TModel> list = DbHelper.FillListPaged<TModel>(\"{0}\", parameters);\r\n", spname);
         builder.AppendFormat("//List<TModel> list = DbHelper.FillList<TModel>(\"{0}\", parameters);\r\n", spname);
     }
     else if (((spname.IndexOf("get", StringComparison.OrdinalIgnoreCase) >= 0) || (spname.IndexOf("query", StringComparison.OrdinalIgnoreCase) >= 0)) || (spname.IndexOf("search", StringComparison.OrdinalIgnoreCase) >= 0))
     {
         if (dbParameter.Count(t => t.DbType == DbType.Object) > 0)
         {
             builder.AppendFormat("List<TModel> list = DbHelper.FillList<TModel>(\"{0}\", parameters);\r\n", spname);
         }
         else
         {
             builder.AppendFormat("TModel item = DbHelper.GetDataItem<TModel>(\"{0}\", parameters);\r\n", spname);
             builder.AppendFormat("//List<TModel> list = DbHelper.FillList<TModel>(\"{0}\", parameters);\r\n", spname);
         }
     }
     else
     {
         builder.AppendFormat("DbHelper.ExecuteNonQuery(\"{0}\", parameters);", spname);
     }
     return builder.ToString();
 }
Ejemplo n.º 2
0
        public virtual GridData GetGridData(HttpContext context)
        {
            string view        = context.Request["view"];
            string columns     = context.Request["columns"];
            string sortname    = context.Request["sortname"];
            string sortorder   = context.Request["sortorder"];
            string _pagenumber = context.Request["page"];
            string _pagesize   = context.Request["pagesize"];
            string procedure   = context.Request["procedure"];

            string where = context.Request["where"];
            if (string.IsNullOrEmpty(procedure))
            {
                int?pagenumber = null, pagesize = null;
                //可分页
                if (!_pagenumber.IsNullOrEmpty() && !_pagesize.IsNullOrEmpty())
                {
                    pagenumber = _pagenumber.ToInt();
                    pagesize   = _pagesize.ToInt();
                    if (pagesize == 0)
                    {
                        pagesize = 20;
                    }
                }
                //可排序
                if (!sortname.IsNullOrEmpty())
                {
                    sortorder = sortorder.IsNullOrEmpty() || sortorder.EqualsTo("asc") ? "asc" : "desc";
                }

                /*
                 * where 为 json参数,格式如下:
                 * {
                 *    "roles":[
                 *       {"field":"ID","value":112,"op":"equal"},
                 *        {"field":"Time","value":"2011-3-4","op":"greaterorequal"}
                 *     ],
                 *    "op":"and","groups":null
                 *  }
                 *  FilterTranslator可以为以上格式的where表达式翻译为sql,并生成参数列表(FilterParam[])
                 */
                var dpRule = new DataPrivilegeRule(DbHelper.Db);


                var whereTranslator = new FilterTranslator();

                if (!where.IsNullOrEmpty())
                {
                    //反序列化Filter Group JSON
                    whereTranslator.Group = JSONHelper.FromJson <FilterGroup>(where);
                    //合并数据权限规则

                    whereTranslator.Group = dpRule.GetRuleGroup(view, whereTranslator.Group);
                }
                else
                {
                    //如果没有定义前台搜索规则
                    whereTranslator.Group = dpRule.GetRuleGroup(view, whereTranslator.Group);
                }
                if (whereTranslator.Group != null && whereTranslator.Group.rules != null)
                {
                    whereTranslator.Group.rules = whereTranslator.Group.rules.Where(i => i.value != null && i.value.ToString() != "全部" && i.value != "" && i.value != "undefined").ToList();
                }
                whereTranslator.Translate();

                if (string.IsNullOrEmpty(columns))
                {
                    return(GetGridData(view, whereTranslator.CommandText, sortname, sortorder, pagenumber, pagesize, whereTranslator.Parms.ToArray()));
                }
                else
                {
                    return(GetGridData(view, columns, whereTranslator.CommandText, sortname, sortorder, pagenumber, pagesize, whereTranslator.Parms.ToArray()));
                }
            }
            else
            {
                if (string.IsNullOrEmpty(where))
                {
                    return(GetGridData(procedure, null));
                }
                else
                {
                    var whereTranslator = new FilterTranslator();
                    whereTranslator.Group = JSONHelper.FromJson <FilterGroup>(where);
                    DbParameter[] parms = new DbParameter[whereTranslator.Group.rules.Count];
                    for (int i = 0; i < parms.Count(); i++)
                    {
                        DbParameter p    = this.DbProvider.DbProviderFactory.CreateParameter();
                        FilterRule  rule = whereTranslator.Group.rules[i];
                        p.ParameterName = "@" + rule.field.ToLower();
                        switch (rule.type)
                        {
                        case "string":
                            p.DbType = DbType.String;
                            break;

                        case "int":
                            p.DbType = DbType.Int32;
                            break;
                        }
                        p.Value  = string.IsNullOrEmpty(rule.value.ToString()) || rule.value.ToString().Equals("全部")?"":rule.value.ToString();
                        parms[i] = p;
                    }
                    return(GetGridData(procedure, parms));
                }
            }
        }