Skip to content

Commit

Permalink
Merge pull request #97 from mreyeros/master
Browse files Browse the repository at this point in the history
Pull request to include Paged function for ad-hoc queries
  • Loading branch information
robconery committed Nov 10, 2011
2 parents e3666f2 + 5a384d8 commit d526896
Showing 1 changed file with 34 additions and 8 deletions.
42 changes: 34 additions & 8 deletions Massive.cs
Expand Up @@ -314,26 +314,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>
Expand Down

0 comments on commit d526896

Please sign in to comment.