Commit f22447df authored by Marc Gravell's avatar Marc Gravell

Give more context when the GridReader is disposed; see http://stackoverflow.com/questions/12809907/

parent 24225931
...@@ -2280,7 +2280,7 @@ public IEnumerable<dynamic> Read() ...@@ -2280,7 +2280,7 @@ public IEnumerable<dynamic> Read()
/// </summary> /// </summary>
public IEnumerable<T> Read<T>() public IEnumerable<T> Read<T>()
{ {
if (reader == null) throw new ObjectDisposedException(GetType().Name); if (reader == null) throw new ObjectDisposedException(GetType().FullName, "The reader has been disposed; this can happen after all data has been consumed");
if (consumed) throw new InvalidOperationException("Each grid can only be iterated once"); if (consumed) throw new InvalidOperationException("Each grid can only be iterated once");
var typedIdentity = identity.ForGrid(typeof(T), gridIndex); var typedIdentity = identity.ForGrid(typeof(T), gridIndex);
CacheInfo cache = GetCacheInfo(typedIdentity); CacheInfo cache = GetCacheInfo(typedIdentity);
...@@ -2298,7 +2298,6 @@ public IEnumerable<T> Read<T>() ...@@ -2298,7 +2298,6 @@ public IEnumerable<T> Read<T>()
private IEnumerable<TReturn> MultiReadInternal<TFirst, TSecond, TThird, TFourth, TFifth, TReturn>(object func, string splitOn) private IEnumerable<TReturn> MultiReadInternal<TFirst, TSecond, TThird, TFourth, TFifth, TReturn>(object func, string splitOn)
{ {
var identity = this.identity.ForGrid(typeof(TReturn), new Type[] { var identity = this.identity.ForGrid(typeof(TReturn), new Type[] {
typeof(TFirst), typeof(TFirst),
typeof(TSecond), typeof(TSecond),
...@@ -2413,12 +2412,13 @@ private IEnumerable<T> ReadDeferred<T>(int index, Func<IDataReader, object> dese ...@@ -2413,12 +2412,13 @@ private IEnumerable<T> ReadDeferred<T>(int index, Func<IDataReader, object> dese
} }
} }
} }
private int gridIndex; private int gridIndex, readCount;
private bool consumed; private bool consumed;
private void NextResult() private void NextResult()
{ {
if (reader.NextResult()) if (reader.NextResult())
{ {
readCount++;
gridIndex++; gridIndex++;
consumed = false; consumed = false;
} }
......
...@@ -2096,6 +2096,31 @@ public void QueryMultipleInvalidFromClosed() ...@@ -2096,6 +2096,31 @@ public void QueryMultipleInvalidFromClosed()
} }
} }
public void TestMultiSelectWithSomeEmptyGrids()
{
using (var reader = connection.QueryMultiple("select 1; select 2 where 1 = 0; select 3 where 1 = 0; select 4;"))
{
var one = reader.Read<int>().ToArray();
var two = reader.Read<int>().ToArray();
var three = reader.Read<int>().ToArray();
var four = reader.Read<int>().ToArray();
try { // only returned four grids; expect a fifth read to fail
reader.Read<int>();
throw new InvalidOperationException("this should not have worked!");
}
catch (ObjectDisposedException ex) { // expected; success
ex.Message.IsEqualTo("The reader has been disposed; this can happen after all data has been consumed\r\nObject name: 'Dapper.SqlMapper+GridReader'.");
}
one.Length.IsEqualTo(1);
one[0].IsEqualTo(1);
two.Length.IsEqualTo(0);
three.Length.IsEqualTo(0);
four.Length.IsEqualTo(1);
four[0].IsEqualTo(4);
}
}
class TransactedConnection : IDbConnection class TransactedConnection : IDbConnection
{ {
IDbConnection _conn; IDbConnection _conn;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment