Skip to content

Commit 58ea7e2

Browse files
committed
Added support for implicit overallcount
1 parent b6425dd commit 58ea7e2

File tree

2 files changed

+24
-13
lines changed

2 files changed

+24
-13
lines changed

Build/CommonAssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@
1515
//
1616
// You can specify all the values or you can default the Revision and Build Numbers
1717
// by using the '*' as shown below:
18-
[assembly: AssemblyVersion("1.0.79.0")]
19-
[assembly: AssemblyFileVersion("1.0.79.0")]
18+
[assembly: AssemblyVersion("1.0.80.0")]
19+
[assembly: AssemblyFileVersion("1.0.80.0")]

Griddly.Mvc/DapperGriddlyResult.cs

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public class DapperGriddlyResult<T> : GriddlyResult<T>
1919

2020
long? _overallCount = null;
2121
bool _fixedSort;
22+
static readonly bool _hasOverallCount = typeof(IHasOverallCount).IsAssignableFrom(typeof(T));
2223

2324
public DapperGriddlyResult(Func<IDbConnection> getConnection, string sql, object param, Func<IDbConnection, IDbTransaction, string, object, IEnumerable<T>> map = null, Action<IDbConnection, IDbTransaction, IList<T>> massage = null, bool fixedSort = false, Func<IDbTransaction> getTransaction = null)
2425
: base(null)
@@ -35,6 +36,7 @@ public DapperGriddlyResult(Func<IDbConnection> getConnection, string sql, object
3536
_massage = massage;
3637
_fixedSort = fixedSort;
3738
_getTransaction = getTransaction;
39+
3840
}
3941

4042
public override void PopulateSummaryValues(GriddlySettings<T> settings)
@@ -101,17 +103,22 @@ public override long GetCount()
101103

102104
public override IList<T> GetPage(int pageNumber, int pageSize, SortField[] sortFields)
103105
{
104-
/* TODO: grab the count all at once like this:
105-
* TODO: also grab the other summary values in the _count branch too
106+
string format;
107+
108+
if (!_hasOverallCount || _sql.IndexOf("OverallCount", StringComparison.InvariantCultureIgnoreCase) != -1)
109+
format = "{0} " + (_fixedSort ? "" : "ORDER BY {1}") + " OFFSET {2} ROWS FETCH NEXT {3} ROWS ONLY";
110+
else
111+
// TODO: use dapper multimap Query<T, Dictionary<string, object>> to map all summary values in one go
112+
format = @"
106113
;WITH _data AS (
107-
select * from fin_lineitem where not oldpk is null
114+
{0}
108115
),
109116
_count AS (
110-
SELECT COUNT(0) AS _AllRows FROM _data
117+
SELECT COUNT(0) AS OverallCount FROM _data
111118
)
112-
SELECT * FROM _data CROSS APPLY _count ORDER BY CURRENT_TIMESTAMP OFFSET 50 ROWS FETCH NEXT 50 ROWS ONLY
113-
*/
114-
string sql = string.Format("{0} " + (_fixedSort ? "" : "ORDER BY {1}") + " OFFSET {2} ROWS FETCH NEXT {3} ROWS ONLY", _sql, BuildSortClause(sortFields), pageNumber * pageSize, pageSize);
119+
SELECT * FROM _data CROSS APPLY _count " + (_fixedSort ? "" : "ORDER BY {1}") + " OFFSET {2} ROWS FETCH NEXT {3} ROWS ONLY";
120+
121+
string sql = string.Format(format, _sql, BuildSortClause(sortFields), pageNumber * pageSize, pageSize);
115122

116123
return ExecuteQuery(sql, _param);
117124
}
@@ -137,10 +144,14 @@ IList<T> ExecuteQuery(string sql, object param)
137144
try
138145
{
139146
IEnumerable<T> result = _map(_getConnection(), _getTransaction != null ? _getTransaction() : null, sql, param);
140-
IHasOverallCount overallCount = result as IHasOverallCount;
141147

142-
if (overallCount != null)
143-
_overallCount = overallCount.OverallCount;
148+
if (_hasOverallCount)
149+
{
150+
IHasOverallCount overallCount = result as IHasOverallCount;
151+
152+
if (overallCount != null)
153+
_overallCount = overallCount.OverallCount;
154+
}
144155

145156
IList<T> results = result.ToList();
146157

@@ -159,7 +170,7 @@ protected IEnumerable<T> DefaultMap(IDbConnection cn, IDbTransaction tx, string
159170
{
160171
IEnumerable<T> result = cn.Query<T>(sql, param, tx);
161172

162-
if (typeof(IHasOverallCount).IsAssignableFrom(typeof(T)))
173+
if (_hasOverallCount)
163174
{
164175
IHasOverallCount firstRow = result.FirstOrDefault() as IHasOverallCount;
165176
ListPage<T> lp = new ListPage<T>();

0 commit comments

Comments
 (0)