Skip to content

Commit 6f969e8

Browse files
committed
Added exception to pass sql and param context
1 parent bf8c1f9 commit 6f969e8

File tree

4 files changed

+57
-20
lines changed

4 files changed

+57
-20
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.33.0")]
19-
[assembly: AssemblyFileVersion("1.0.33.0")]
18+
[assembly: AssemblyVersion("1.0.34.0")]
19+
[assembly: AssemblyFileVersion("1.0.34.0")]

Griddly.Mvc/DapperGriddlyException.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Griddly.Mvc
8+
{
9+
public class DapperGriddlyException : Exception
10+
{
11+
public string Sql { get; protected set; }
12+
public object Params { get; protected set; }
13+
14+
public DapperGriddlyException(string message, string sql, object param = null, Exception ex = null)
15+
: base(message, ex)
16+
{
17+
Sql = sql;
18+
Params = param;
19+
}
20+
}
21+
}

Griddly.Mvc/DapperGriddlyResult.cs

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System;
33
using System.Collections.Generic;
44
using System.Data;
5+
using System.Data.SqlClient;
56
using System.Linq;
67
using System.Web.Helpers;
78

@@ -40,9 +41,17 @@ public override long GetCount()
4041
{
4142
string sql = string.Format("SELECT CAST(COUNT(*) as bigint) FROM ({0}) [_proj]", _sql);
4243

43-
IDbConnection cn = _getConnection();
44+
try
45+
{
46+
IDbConnection cn = _getConnection();
47+
48+
_overallCount = cn.Query<long>(sql, _param).Single();
4449

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+
}
4655
}
4756

4857
return _overallCount.Value;
@@ -73,40 +82,46 @@ protected string BuildSortClause(SortField[] sortFields)
7382
// TODO: return IEnumerable so we don't have to .ToList()
7483
IList<T> ExecuteQuery(string sql, object param)
7584
{
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;
7889

79-
if (overallCount != null)
80-
_overallCount = overallCount.OverallCount;
90+
if (overallCount != null)
91+
_overallCount = overallCount.OverallCount;
8192

82-
IList<T> results = result.ToList();
93+
IList<T> results = result.ToList();
8394

84-
if (_massage != null)
85-
_massage(_getConnection(), results);
95+
if (_massage != null)
96+
_massage(_getConnection(), results);
8697

87-
return results;
98+
return results;
99+
}
100+
catch (Exception ex)
101+
{
102+
throw new DapperGriddlyException("Error executing list query.", sql, param, ex);
103+
}
88104
}
89105

90106
protected IEnumerable<T> DefaultMap(IDbConnection cn, string sql, object param)
91107
{
92108
IEnumerable<T> result = cn.Query<T>(sql, param);
93109

94-
if(typeof(IHasOverallCount).IsAssignableFrom(typeof(T))){
110+
if (typeof(IHasOverallCount).IsAssignableFrom(typeof(T)))
111+
{
95112
IHasOverallCount firstRow = result.FirstOrDefault() as IHasOverallCount;
113+
ListPage<T> lp = new ListPage<T>();
114+
96115
if (firstRow != null)
97116
{
98-
var lp = new ListPage<T>();
99117
lp.OverallCount = firstRow.OverallCount;
100118
lp.AddRange(result);
101-
result = lp;
102-
}
103-
else
104-
{
105-
return new ListPage<T>();
106119
}
120+
121+
result = lp;
107122
}
108-
return result;
109123

124+
return result;
110125
}
111126
}
112127
}

Griddly.Mvc/Griddly.Mvc.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
<Compile Include="..\Build\CommonAssemblyInfo.cs">
8989
<Link>Properties\CommonAssemblyInfo.cs</Link>
9090
</Compile>
91+
<Compile Include="DapperGriddlyException.cs" />
9192
<Compile Include="DapperGriddlyResult.cs" />
9293
<Compile Include="GriddlyFilterExtensions.cs" />
9394
<Compile Include="InternalExtensions.cs" />

0 commit comments

Comments
 (0)