Skip to content

Commit 5a384d8

Browse files
committed
Adding additional "Paged" override to allow for obtaining a Paged result for an ad-hoc query.
1 parent a9b7fdf commit 5a384d8

File tree

1 file changed

+34
-8
lines changed

1 file changed

+34
-8
lines changed

Massive.cs

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -318,26 +318,52 @@ private static string BuildSelect(string where, string orderBy, int limit) {
318318
/// <summary>
319319
/// Returns a dynamic PagedResult. Result properties are Items, TotalPages, and TotalRecords.
320320
/// </summary>
321-
public virtual dynamic Paged(string where = "", string orderBy = "", string columns = "*", int pageSize = 20, int currentPage = 1, params object[] args) {
321+
public virtual dynamic Paged(string where = "", string orderBy = "", string columns = "*", int pageSize = 20, int currentPage = 1, params object[] args)
322+
{
323+
return BuildPagedResult(where: where, orderBy: orderBy, columns: columns, pageSize: pageSize, currentPage: currentPage, args: args);
324+
}
325+
326+
public virtual dynamic Paged(string sql, string primaryKey, string where = "", string orderBy = "", string columns = "*", int pageSize = 20, int currentPage = 1, params object[] args)
327+
{
328+
return BuildPagedResult(sql, primaryKey, where, orderBy, columns, pageSize, currentPage, args);
329+
}
330+
331+
private dynamic BuildPagedResult(string sql = "", string primaryKeyField = "", string where = "", string orderBy = "", string columns = "*", int pageSize = 20, int currentPage = 1, params object[] args)
332+
{
322333
dynamic result = new ExpandoObject();
323-
var countSQL = string.Format("SELECT COUNT({0}) FROM {1} ", PrimaryKeyField, TableName);
334+
var countSQL = "";
335+
if (!string.IsNullOrEmpty(sql))
336+
countSQL = string.Format("SELECT COUNT({0}) FROM ({1}) AS PagedTable", primaryKeyField, sql);
337+
else
338+
countSQL = string.Format("SELECT COUNT({0}) FROM {1}", PrimaryKeyField, TableName);
339+
324340
if (String.IsNullOrEmpty(orderBy))
325-
orderBy = PrimaryKeyField;
341+
{
342+
orderBy = string.IsNullOrEmpty(primaryKeyField) ? PrimaryKeyField : primaryKeyField;
343+
}
326344

327-
if (!string.IsNullOrEmpty(where)) {
328-
if (!where.Trim().StartsWith("where", StringComparison.OrdinalIgnoreCase)) {
345+
if (!string.IsNullOrEmpty(where))
346+
{
347+
if (!where.Trim().StartsWith("where", StringComparison.CurrentCultureIgnoreCase))
348+
{
329349
where = "WHERE " + where;
330350
}
331351
}
332-
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);
352+
353+
var query = "";
354+
if (!string.IsNullOrEmpty(sql))
355+
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);
356+
else
357+
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);
358+
333359
var pageStart = (currentPage - 1) * pageSize;
334-
sql += string.Format(" WHERE Row > {0} AND Row <={1}", pageStart, (pageStart + pageSize));
360+
query += string.Format(" WHERE Row > {0} AND Row <={1}", pageStart, (pageStart + pageSize));
335361
countSQL += where;
336362
result.TotalRecords = Scalar(countSQL, args);
337363
result.TotalPages = result.TotalRecords / pageSize;
338364
if (result.TotalRecords % pageSize > 0)
339365
result.TotalPages += 1;
340-
result.Items = Query(string.Format(sql, columns, TableName), args);
366+
result.Items = Query(string.Format(query, columns, TableName), args);
341367
return result;
342368
}
343369
/// <summary>

0 commit comments

Comments
 (0)