Skip to content

Commit 8182e53

Browse files
Merge pull request #89 from jehhynes/master
Deterministic sorting, Dapper dynamic OverallCount
2 parents 420be7e + 9a3ce1d commit 8182e53

File tree

2 files changed

+43
-20
lines changed

2 files changed

+43
-20
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
}

Griddly.Mvc/Results/QueryableResult.cs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,24 @@ public class QueryableResult<T> : GriddlyResult<T>
1212
{
1313
IQueryable<T> _result;
1414
Func<IQueryable<T>, IQueryable<T>> _massage = null;
15+
string _finalSortField;
1516

16-
public QueryableResult(IQueryable<T> result, string viewName = null, Func<IQueryable<T>, IQueryable<T>> massage = null)
17+
static readonly bool _typeHasId = typeof(T).GetProperty("Id") != null;
18+
19+
public QueryableResult(IQueryable<T> result, string viewName = null, Func<IQueryable<T>, IQueryable<T>> massage = null, string finalSortField = null)
1720
: base(viewName)
1821
{
1922
_result = result;
2023
_massage = massage;
24+
_finalSortField = finalSortField;
25+
26+
if (_finalSortField == null && _typeHasId)
27+
_finalSortField = "Id";
2128
}
2229

2330
public override IEnumerable<T> GetAll(SortField[] sortFields)
2431
{
25-
IQueryable<T> sortedQuery = ApplySortFields(_result, sortFields);
32+
IQueryable<T> sortedQuery = ApplySortFields(_result, sortFields, _finalSortField);
2633

2734
if (_massage != null)
2835
sortedQuery = _massage(sortedQuery);
@@ -32,7 +39,7 @@ public override IEnumerable<T> GetAll(SortField[] sortFields)
3239

3340
public override IList<T> GetPage(int pageNumber, int pageSize, SortField[] sortFields)
3441
{
35-
IQueryable<T> sortedQuery = ApplySortFields(_result, sortFields);
42+
IQueryable<T> sortedQuery = ApplySortFields(_result, sortFields, _finalSortField);
3643

3744
if (_massage != null)
3845
sortedQuery = _massage(sortedQuery);
@@ -102,7 +109,7 @@ internal void PopulateSummaryValue(GriddlyColumn c)
102109

103110
public override IEnumerable<P> GetAllForProperty<P>(string propertyName, SortField[] sortFields)
104111
{
105-
return ApplySortFields(_result, sortFields)
112+
return ApplySortFields(_result, sortFields, _finalSortField)
106113
.Select<P>(propertyName, null);
107114
}
108115

@@ -111,7 +118,7 @@ public override long GetCount()
111118
return _result.Count();
112119
}
113120

114-
protected static IQueryable<T> ApplySortFields(IQueryable<T> source, SortField[] sortFields)
121+
protected static IQueryable<T> ApplySortFields(IQueryable<T> source, SortField[] sortFields, string finalSortField)
115122
{
116123
IOrderedQueryable<T> sortedQuery = null;
117124

@@ -138,6 +145,14 @@ protected static IQueryable<T> ApplySortFields(IQueryable<T> source, SortField[]
138145
}
139146
}
140147

148+
if (finalSortField != null)
149+
{
150+
if (sortedQuery == null)
151+
sortedQuery = OrderByDescending(source, finalSortField);
152+
else
153+
sortedQuery = ThenByDescending(sortedQuery, finalSortField);
154+
}
155+
141156
return sortedQuery ?? source;
142157
}
143158

0 commit comments

Comments
 (0)