Skip to content

Commit 9a3ce1d

Browse files
committed
Dapper dynamic OverallCount
1 parent de76453 commit 9a3ce1d

File tree

1 file changed

+23
-15
lines changed

1 file changed

+23
-15
lines changed

Griddly.Mvc/Results/DapperResult.cs

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public override long GetCount()
118118

119119
return _overallCount.Value;
120120
}
121-
121+
122122
protected string BuildSortClause(SortField[] sortFields)
123123
{
124124
if (sortFields != null && sortFields.Length > 0)
@@ -142,13 +142,8 @@ protected virtual IList<T> ExecuteQuery(string sql)
142142
{
143143
IEnumerable<T> result = _map(_getConnection(), _getTransaction != null ? _getTransaction() : null, sql, _param);
144144

145-
if (_hasOverallCount)
146-
{
147-
IHasOverallCount overallCount = result as IHasOverallCount;
148-
149-
if (overallCount != null)
150-
_overallCount = overallCount.OverallCount;
151-
}
145+
if (result is IHasOverallCount overallCount)
146+
_overallCount = overallCount.OverallCount;
152147

153148
IList<T> results = result.ToList();
154149

@@ -167,16 +162,29 @@ protected IEnumerable<T> DefaultMap(IDbConnection cn, IDbTransaction tx, string
167162
{
168163
IEnumerable<T> result = cn.Query<T>(sql, param, tx);
169164

170-
if (_hasOverallCount)
165+
var firstRow = result.FirstOrDefault();
166+
long? overallCount = null;
167+
168+
if (firstRow == null)
169+
{
170+
overallCount = 0;
171+
}
172+
else if (_hasOverallCount && firstRow is IHasOverallCount iHasOverallCount)
173+
{
174+
overallCount = iHasOverallCount.OverallCount;
175+
}
176+
else if (_sql.IndexOf("OverallCount", StringComparison.InvariantCultureIgnoreCase) != -1
177+
&& firstRow is IDictionary<string, object> dapperRow)
178+
{
179+
overallCount = Convert.ToInt64(dapperRow["OverallCount"]);
180+
}
181+
182+
if (overallCount != null)
171183
{
172-
IHasOverallCount firstRow = result.FirstOrDefault() as IHasOverallCount;
173184
ListPage<T> lp = new ListPage<T>();
174185

175-
if (firstRow != null)
176-
{
177-
lp.OverallCount = firstRow.OverallCount;
178-
lp.AddRange(result);
179-
}
186+
lp.OverallCount = overallCount.Value;
187+
lp.AddRange(result);
180188

181189
result = lp;
182190
}

0 commit comments

Comments
 (0)