@@ -19,6 +19,7 @@ public class DapperGriddlyResult<T> : GriddlyResult<T>
19
19
20
20
long ? _overallCount = null ;
21
21
bool _fixedSort ;
22
+ static readonly bool _hasOverallCount = typeof ( IHasOverallCount ) . IsAssignableFrom ( typeof ( T ) ) ;
22
23
23
24
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 )
24
25
: base ( null )
@@ -35,6 +36,7 @@ public DapperGriddlyResult(Func<IDbConnection> getConnection, string sql, object
35
36
_massage = massage ;
36
37
_fixedSort = fixedSort ;
37
38
_getTransaction = getTransaction ;
39
+
38
40
}
39
41
40
42
public override void PopulateSummaryValues ( GriddlySettings < T > settings )
@@ -101,17 +103,22 @@ public override long GetCount()
101
103
102
104
public override IList < T > GetPage ( int pageNumber , int pageSize , SortField [ ] sortFields )
103
105
{
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 = @"
106
113
;WITH _data AS (
107
- select * from fin_lineitem where not oldpk is null
114
+ {0}
108
115
),
109
116
_count AS (
110
- SELECT COUNT(0) AS _AllRows FROM _data
117
+ SELECT COUNT(0) AS OverallCount FROM _data
111
118
)
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 ) ;
115
122
116
123
return ExecuteQuery ( sql , _param ) ;
117
124
}
@@ -137,10 +144,14 @@ IList<T> ExecuteQuery(string sql, object param)
137
144
try
138
145
{
139
146
IEnumerable < T > result = _map ( _getConnection ( ) , _getTransaction != null ? _getTransaction ( ) : null , sql , param ) ;
140
- IHasOverallCount overallCount = result as IHasOverallCount ;
141
147
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
+ }
144
155
145
156
IList < T > results = result . ToList ( ) ;
146
157
@@ -159,7 +170,7 @@ protected IEnumerable<T> DefaultMap(IDbConnection cn, IDbTransaction tx, string
159
170
{
160
171
IEnumerable < T > result = cn . Query < T > ( sql , param , tx ) ;
161
172
162
- if ( typeof ( IHasOverallCount ) . IsAssignableFrom ( typeof ( T ) ) )
173
+ if ( _hasOverallCount )
163
174
{
164
175
IHasOverallCount firstRow = result . FirstOrDefault ( ) as IHasOverallCount ;
165
176
ListPage < T > lp = new ListPage < T > ( ) ;
0 commit comments