|
2 | 2 | using System;
|
3 | 3 | using System.Collections.Generic;
|
4 | 4 | using System.Data;
|
| 5 | +using System.Data.SqlClient; |
5 | 6 | using System.Linq;
|
6 | 7 | using System.Web.Helpers;
|
7 | 8 |
|
@@ -40,9 +41,17 @@ public override long GetCount()
|
40 | 41 | {
|
41 | 42 | string sql = string.Format("SELECT CAST(COUNT(*) as bigint) FROM ({0}) [_proj]", _sql);
|
42 | 43 |
|
43 |
| - IDbConnection cn = _getConnection(); |
| 44 | + try |
| 45 | + { |
| 46 | + IDbConnection cn = _getConnection(); |
| 47 | + |
| 48 | + _overallCount = cn.Query<long>(sql, _param).Single(); |
44 | 49 |
|
45 |
| - _overallCount = cn.Query<long>(sql, _param).Single(); |
| 50 | + } |
| 51 | + catch (Exception ex) |
| 52 | + { |
| 53 | + throw new DapperGriddlyException("Error executing count query.", sql, _param, ex); |
| 54 | + } |
46 | 55 | }
|
47 | 56 |
|
48 | 57 | return _overallCount.Value;
|
@@ -73,40 +82,46 @@ protected string BuildSortClause(SortField[] sortFields)
|
73 | 82 | // TODO: return IEnumerable so we don't have to .ToList()
|
74 | 83 | IList<T> ExecuteQuery(string sql, object param)
|
75 | 84 | {
|
76 |
| - IEnumerable<T> result = _map(_getConnection(), sql, param); |
77 |
| - IHasOverallCount overallCount = result as IHasOverallCount; |
| 85 | + try |
| 86 | + { |
| 87 | + IEnumerable<T> result = _map(_getConnection(), sql, param); |
| 88 | + IHasOverallCount overallCount = result as IHasOverallCount; |
78 | 89 |
|
79 |
| - if (overallCount != null) |
80 |
| - _overallCount = overallCount.OverallCount; |
| 90 | + if (overallCount != null) |
| 91 | + _overallCount = overallCount.OverallCount; |
81 | 92 |
|
82 |
| - IList<T> results = result.ToList(); |
| 93 | + IList<T> results = result.ToList(); |
83 | 94 |
|
84 |
| - if (_massage != null) |
85 |
| - _massage(_getConnection(), results); |
| 95 | + if (_massage != null) |
| 96 | + _massage(_getConnection(), results); |
86 | 97 |
|
87 |
| - return results; |
| 98 | + return results; |
| 99 | + } |
| 100 | + catch (Exception ex) |
| 101 | + { |
| 102 | + throw new DapperGriddlyException("Error executing list query.", sql, param, ex); |
| 103 | + } |
88 | 104 | }
|
89 | 105 |
|
90 | 106 | protected IEnumerable<T> DefaultMap(IDbConnection cn, string sql, object param)
|
91 | 107 | {
|
92 | 108 | IEnumerable<T> result = cn.Query<T>(sql, param);
|
93 | 109 |
|
94 |
| - if(typeof(IHasOverallCount).IsAssignableFrom(typeof(T))){ |
| 110 | + if (typeof(IHasOverallCount).IsAssignableFrom(typeof(T))) |
| 111 | + { |
95 | 112 | IHasOverallCount firstRow = result.FirstOrDefault() as IHasOverallCount;
|
| 113 | + ListPage<T> lp = new ListPage<T>(); |
| 114 | + |
96 | 115 | if (firstRow != null)
|
97 | 116 | {
|
98 |
| - var lp = new ListPage<T>(); |
99 | 117 | lp.OverallCount = firstRow.OverallCount;
|
100 | 118 | lp.AddRange(result);
|
101 |
| - result = lp; |
102 |
| - } |
103 |
| - else |
104 |
| - { |
105 |
| - return new ListPage<T>(); |
106 | 119 | }
|
| 120 | + |
| 121 | + result = lp; |
107 | 122 | }
|
108 |
| - return result; |
109 | 123 |
|
| 124 | + return result; |
110 | 125 | }
|
111 | 126 | }
|
112 | 127 | }
|
0 commit comments