Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Pull request to include Paged function for ad-hoc queries #97

Merged
merged 1 commit into from Nov 10, 2011
Jump to file or symbol
Failed to load files and symbols.
+34 −8
Split
View
@@ -318,26 +318,52 @@ public class DynamicModel : DynamicObject {
/// <summary>
/// Returns a dynamic PagedResult. Result properties are Items, TotalPages, and TotalRecords.
/// </summary>
- public virtual dynamic Paged(string where = "", string orderBy = "", string columns = "*", int pageSize = 20, int currentPage = 1, params object[] args) {
+ public virtual dynamic Paged(string where = "", string orderBy = "", string columns = "*", int pageSize = 20, int currentPage = 1, params object[] args)
+ {
+ return BuildPagedResult(where: where, orderBy: orderBy, columns: columns, pageSize: pageSize, currentPage: currentPage, args: args);
+ }
+
+ public virtual dynamic Paged(string sql, string primaryKey, string where = "", string orderBy = "", string columns = "*", int pageSize = 20, int currentPage = 1, params object[] args)
+ {
+ return BuildPagedResult(sql, primaryKey, where, orderBy, columns, pageSize, currentPage, args);
+ }
+
+ private dynamic BuildPagedResult(string sql = "", string primaryKeyField = "", string where = "", string orderBy = "", string columns = "*", int pageSize = 20, int currentPage = 1, params object[] args)
+ {
dynamic result = new ExpandoObject();
- var countSQL = string.Format("SELECT COUNT({0}) FROM {1} ", PrimaryKeyField, TableName);
+ var countSQL = "";
+ if (!string.IsNullOrEmpty(sql))
+ countSQL = string.Format("SELECT COUNT({0}) FROM ({1}) AS PagedTable", primaryKeyField, sql);
+ else
+ countSQL = string.Format("SELECT COUNT({0}) FROM {1}", PrimaryKeyField, TableName);
+
if (String.IsNullOrEmpty(orderBy))
- orderBy = PrimaryKeyField;
+ {
+ orderBy = string.IsNullOrEmpty(primaryKeyField) ? PrimaryKeyField : primaryKeyField;
+ }
- if (!string.IsNullOrEmpty(where)) {
- if (!where.Trim().StartsWith("where", StringComparison.OrdinalIgnoreCase)) {
+ if (!string.IsNullOrEmpty(where))
+ {
+ if (!where.Trim().StartsWith("where", StringComparison.CurrentCultureIgnoreCase))
+ {
where = "WHERE " + where;
}
}
- var sql = string.Format("SELECT {0} FROM (SELECT ROW_NUMBER() OVER (ORDER BY {2}) AS Row, {0} FROM {3} {4}) AS Paged ", columns, pageSize, orderBy, TableName, where);
+
+ var query = "";
+ if (!string.IsNullOrEmpty(sql))
+ query = string.Format("SELECT {0} FROM (SELECT ROW_NUMBER() OVER (ORDER BY {2}) AS Row, {0} FROM ({3}) AS PagedTable {4}) AS Paged ", columns, pageSize, orderBy, sql, where);
+ else
+ query = string.Format("SELECT {0} FROM (SELECT ROW_NUMBER() OVER (ORDER BY {2}) AS Row, {0} FROM {3} {4}) AS Paged ", columns, pageSize, orderBy, TableName, where);
+
var pageStart = (currentPage - 1) * pageSize;
- sql += string.Format(" WHERE Row > {0} AND Row <={1}", pageStart, (pageStart + pageSize));
+ query += string.Format(" WHERE Row > {0} AND Row <={1}", pageStart, (pageStart + pageSize));
countSQL += where;
result.TotalRecords = Scalar(countSQL, args);
result.TotalPages = result.TotalRecords / pageSize;
if (result.TotalRecords % pageSize > 0)
result.TotalPages += 1;
- result.Items = Query(string.Format(sql, columns, TableName), args);
+ result.Items = Query(string.Format(query, columns, TableName), args);
return result;
}
/// <summary>