Exemplo n.º 1
0
        public static PagedData GetCommonPageData(DbContext db, string viewName, QueryDescriptor descriptor)
        {
            string sortName = descriptor.SortName;
            string text     = descriptor.SortOrder;
            bool   flag     = false;
            bool   flag2    = false;

            if (descriptor.PageIndex.HasValue && descriptor.PageSize.HasValue)
            {
                flag2 = true;
                if (descriptor.PageSize == 0L)
                {
                    descriptor.PageSize = 20L;
                }
            }
            if (!StringExtensions.IsNullOrEmpty(sortName))
            {
                flag = true;
                text = ((StringExtensions.IsNullOrEmpty(text) || StringExtensions.EqualsTo(text, "asc")) ? "asc" : "desc");
            }
            FilterTranslator filterTranslator = new FilterTranslator();

            if (descriptor.Condition != null)
            {
                filterTranslator.Group = descriptor.Condition;
            }
            filterTranslator.Translate();
            string commandText = filterTranslator.CommandText;
            int    num         = 0;

            if (flag2)
            {
                commandText = (string.IsNullOrEmpty(commandText) ? "" : ("where " + commandText));
                commandText = string.Format("SELECT * FROM (SELECT ROW_NUMBER() OVER (order by {1} {2}) peta_rn, * FROM [{0}] {3}  ) peta_paged WHERE peta_rn> {4} AND peta_rn<= {5}", viewName, sortName ?? "ID", text, commandText, (descriptor.PageIndex.Value - 1L) * descriptor.PageSize.Value, descriptor.PageIndex.Value * descriptor.PageSize.Value);
                num         = db.ExecuteScalar <int>("select count(*) from " + viewName + " " + (string.IsNullOrEmpty(filterTranslator.CommandText) ? "" : (" where " + filterTranslator.CommandText)), new object[1]
                {
                    filterTranslator.Parms
                });
            }
            else
            {
                commandText = (string.IsNullOrEmpty(commandText) ? "" : ("where " + commandText));
                if (flag)
                {
                    commandText += string.Format(" order by {0} {1}", sortName, text);
                }
                commandText = "select * from " + viewName + " " + commandText;
            }
            DataTable dataTable = new DataTable();

            db.Fill(dataTable, commandText, new object[1]
            {
                filterTranslator.Parms
            });
            List <Dictionary <string, object> > list = new List <Dictionary <string, object> >();

            foreach (DataRow row in dataTable.Rows)
            {
                Dictionary <string, object> dictionary = new Dictionary <string, object>();
                foreach (DataColumn column in dataTable.Columns)
                {
                    dictionary.Add(column.ColumnName, row[column.ColumnName]);
                }
                list.Add(dictionary);
            }
            if (!flag2)
            {
                num = list.Count;
            }
            return(new PagedData(list, num));
        }