/
PageableQuery.cs
70 lines (58 loc) · 1.67 KB
/
PageableQuery.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
using Dapper.QX.Attributes;
using System;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
namespace Dapper.QX
{
/// <summary>
/// use this when you need to get the total number of pages in a query that uses pagination
/// </summary>
public class PageableQuery<TResult> : Query<TResult>
{
public PageableQuery(string sql) : base(sql)
{
}
[Offset(DefaultPageSize)]
public int? Page { get; set; }
protected const int DefaultPageSize = 30;
public async Task<int> GetPageCountAsync(Func<IDbConnection> getConnection, int newPageSize = 0)
{
using (var cn = getConnection.Invoke())
{
return await GetPageCountAsync(cn, newPageSize);
}
}
public async Task<int> GetPageCountAsync(IDbConnection connection, int newPageSize = 0)
{
var metrics = await GetMetricsAsync(connection, newPageSize);
return metrics.Pages;
}
public async Task<ResultMetrics> GetMetricsAsync(Func<IDbConnection> getConnection, int newPageSize = 0)
{
using (var cn = getConnection.Invoke())
{
return await GetMetricsAsync(cn, newPageSize);
}
}
public async Task<ResultMetrics> GetMetricsAsync(IDbConnection connection, int newPageSize = 0)
{
int pageSize = (newPageSize > 0) ? newPageSize : DefaultPageSize;
var currentPage = this.Page;
this.Page = null;
var results = await ExecuteAsync(connection, newPageSize: newPageSize);
int count = results.Count();
this.Page = currentPage;
return new ResultMetrics()
{
Rows = count,
Pages = (count / pageSize) + (((count % pageSize) > 0) ? 1 : 0)
};
}
public class ResultMetrics
{
public int Pages { get; set; }
public int Rows { get; set; }
}
}
}