Commit 4997b176 authored by Marc Gravell's avatar Marc Gravell

GridReader should respect no-cache flags

parent 8115d389
...@@ -1513,7 +1513,7 @@ private static GridReader QueryMultipleImpl(this IDbConnection cnn, ref CommandD ...@@ -1513,7 +1513,7 @@ private static GridReader QueryMultipleImpl(this IDbConnection cnn, ref CommandD
cmd = command.SetupCommand(cnn, info.ParamReader); cmd = command.SetupCommand(cnn, info.ParamReader);
reader = cmd.ExecuteReader(wasClosed ? CommandBehavior.CloseConnection | CommandBehavior.SequentialAccess : CommandBehavior.SequentialAccess); reader = cmd.ExecuteReader(wasClosed ? CommandBehavior.CloseConnection | CommandBehavior.SequentialAccess : CommandBehavior.SequentialAccess);
var result = new GridReader(cmd, reader, identity, command.Parameters as DynamicParameters); var result = new GridReader(cmd, reader, identity, command.Parameters as DynamicParameters, command.AddToCache);
cmd = null; // now owned by result cmd = null; // now owned by result
wasClosed = false; // *if* the connection was closed and we got this far, then we now have a reader wasClosed = false; // *if* the connection was closed and we got this far, then we now have a reader
// with the CloseConnection flag, so the reader will deal with the connection; we // with the CloseConnection flag, so the reader will deal with the connection; we
...@@ -4099,13 +4099,15 @@ public partial class GridReader : IDisposable ...@@ -4099,13 +4099,15 @@ public partial class GridReader : IDisposable
private IDataReader reader; private IDataReader reader;
private IDbCommand command; private IDbCommand command;
private Identity identity; private Identity identity;
private bool addToCache;
internal GridReader(IDbCommand command, IDataReader reader, Identity identity, SqlMapper.IParameterCallbacks callbacks) internal GridReader(IDbCommand command, IDataReader reader, Identity identity, SqlMapper.IParameterCallbacks callbacks, bool addToCache)
{ {
this.command = command; this.command = command;
this.reader = reader; this.reader = reader;
this.identity = identity; this.identity = identity;
this.callbacks = callbacks; this.callbacks = callbacks;
this.addToCache = addToCache;
} }
#if !CSHARP30 #if !CSHARP30
...@@ -4159,7 +4161,7 @@ private IEnumerable<T> ReadImpl<T>(Type type, bool buffered) ...@@ -4159,7 +4161,7 @@ private IEnumerable<T> ReadImpl<T>(Type type, bool buffered)
if (reader == null) throw new ObjectDisposedException(GetType().FullName, "The reader has been disposed; this can happen after all data has been consumed"); 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("Query results must be consumed in the correct order, and each result can only be consumed once"); if (consumed) throw new InvalidOperationException("Query results must be consumed in the correct order, and each result can only be consumed once");
var typedIdentity = identity.ForGrid(type, gridIndex); var typedIdentity = identity.ForGrid(type, gridIndex);
CacheInfo cache = GetCacheInfo(typedIdentity, null, true); CacheInfo cache = GetCacheInfo(typedIdentity, null, addToCache);
var deserializer = cache.Deserializer; var deserializer = cache.Deserializer;
int hash = GetColumnHash(reader); int hash = GetColumnHash(reader);
......
...@@ -539,7 +539,8 @@ private static IEnumerable<T> ExecuteReaderSync<T>(IDataReader reader, Func<IDat ...@@ -539,7 +539,8 @@ private static IEnumerable<T> ExecuteReaderSync<T>(IDataReader reader, Func<IDat
partial class GridReader partial class GridReader
{ {
CancellationToken cancel; CancellationToken cancel;
internal GridReader(IDbCommand command, IDataReader reader, Identity identity, DynamicParameters dynamicParams, CancellationToken cancel) : this(command, reader, identity, dynamicParams) internal GridReader(IDbCommand command, IDataReader reader, Identity identity, DynamicParameters dynamicParams, bool addToCache, CancellationToken cancel)
: this(command, reader, identity, dynamicParams, addToCache)
{ {
this.cancel = cancel; this.cancel = cancel;
} }
...@@ -593,7 +594,7 @@ private Task<IEnumerable<T>> ReadAsyncImpl<T>(Type type, bool buffered) ...@@ -593,7 +594,7 @@ private Task<IEnumerable<T>> ReadAsyncImpl<T>(Type type, bool buffered)
if (reader == null) throw new ObjectDisposedException(GetType().FullName, "The reader has been disposed; this can happen after all data has been consumed"); 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("Query results must be consumed in the correct order, and each result can only be consumed once"); if (consumed) throw new InvalidOperationException("Query results must be consumed in the correct order, and each result can only be consumed once");
var typedIdentity = identity.ForGrid(type, gridIndex); var typedIdentity = identity.ForGrid(type, gridIndex);
CacheInfo cache = GetCacheInfo(typedIdentity, null, true); CacheInfo cache = GetCacheInfo(typedIdentity, null, addToCache);
var deserializer = cache.Deserializer; var deserializer = cache.Deserializer;
int hash = GetColumnHash(reader); int hash = GetColumnHash(reader);
...@@ -659,7 +660,7 @@ public static async Task<GridReader> QueryMultipleAsync(this IDbConnection cnn, ...@@ -659,7 +660,7 @@ public static async Task<GridReader> QueryMultipleAsync(this IDbConnection cnn,
cmd = (DbCommand)command.SetupCommand(cnn, info.ParamReader); cmd = (DbCommand)command.SetupCommand(cnn, info.ParamReader);
reader = await cmd.ExecuteReaderAsync(wasClosed ? CommandBehavior.CloseConnection | CommandBehavior.SequentialAccess : CommandBehavior.SequentialAccess, command.CancellationToken).ConfigureAwait(false); reader = await cmd.ExecuteReaderAsync(wasClosed ? CommandBehavior.CloseConnection | CommandBehavior.SequentialAccess : CommandBehavior.SequentialAccess, command.CancellationToken).ConfigureAwait(false);
var result = new GridReader(cmd, reader, identity, command.Parameters as DynamicParameters, command.CancellationToken); var result = new GridReader(cmd, reader, identity, command.Parameters as DynamicParameters, command.AddToCache, command.CancellationToken);
wasClosed = false; // *if* the connection was closed and we got this far, then we now have a reader wasClosed = false; // *if* the connection was closed and we got this far, then we now have a reader
// with the CloseConnection flag, so the reader will deal with the connection; we // with the CloseConnection flag, so the reader will deal with the connection; we
// still need something in the "finally" to ensure that broken SQL still results // still need something in the "finally" to ensure that broken SQL still results
......
...@@ -309,6 +309,34 @@ public void RunSequentialVersusParallelSync() ...@@ -309,6 +309,34 @@ public void RunSequentialVersusParallelSync()
System.Console.WriteLine("Pipeline: {0}ms", watch.ElapsedMilliseconds); System.Console.WriteLine("Pipeline: {0}ms", watch.ElapsedMilliseconds);
} }
} }
public void AssertNoCacheWorksForQueryMultiple()
{
int a = 123, b = 456;
var cmdDef = new CommandDefinition(@"select @a; select @b;", new
{
a, b
}, commandType: CommandType.Text, flags: CommandFlags.NoCache);
int c, d;
Dapper.SqlMapper.PurgeQueryCache();
int before = Dapper.SqlMapper.GetCachedSQLCount();
using (var sqlConnection = Program.GetOpenConnection(true))
{
using (var multi = sqlConnection.QueryMultiple(cmdDef))
{
c = multi.Read<int>().Single();
d = multi.Read<int>().Single();
}
}
int after = Dapper.SqlMapper.GetCachedSQLCount();
before.IsEqualTo(0);
after.IsEqualTo(0);
c.IsEqualTo(123);
d.IsEqualTo(456);
}
class Product class Product
{ {
public int Id { get; set; } public int Id { get; set; }
...@@ -373,7 +401,7 @@ public void Issue22_ExecuteScalar() ...@@ -373,7 +401,7 @@ public void Issue22_ExecuteScalar()
k.IsNull(); k.IsNull();
} }
} }
public void TestSupportForDynamicParametersOutputExpressions() public void TestSupportForDynamicParametersOutputExpressions()
{ {
using (var connection = Program.GetOpenConnection()) using (var connection = Program.GetOpenConnection())
...@@ -704,7 +732,7 @@ public void TestMultiMapArbitraryMaps() ...@@ -704,7 +732,7 @@ public void TestMultiMapArbitraryMaps()
} }
} }
} }
public void Issue157_ClosedReaderAsync() public void Issue157_ClosedReaderAsync()
{ {
using(var conn = Program.GetOpenConnection()) using(var conn = Program.GetOpenConnection())
...@@ -721,8 +749,8 @@ public void Issue157_ClosedReaderAsync() ...@@ -721,8 +749,8 @@ public void Issue157_ClosedReaderAsync()
conn.QueryAsync<SomeType>(new CommandDefinition( conn.QueryAsync<SomeType>(new CommandDefinition(
sql, args, flags: CommandFlags.None)).Result.Any().IsFalse(); sql, args, flags: CommandFlags.None)).Result.Any().IsFalse();
} }
} }
public void TestAtEscaping() public void TestAtEscaping()
{ {
using (var connection = Program.GetOpenConnection()) using (var connection = Program.GetOpenConnection())
...@@ -733,7 +761,7 @@ public void TestAtEscaping() ...@@ -733,7 +761,7 @@ public void TestAtEscaping()
select @@Name select @@Name
", new Product { Id = 1 }).Result.Single(); ", new Product { Id = 1 }).Result.Single();
id.IsEqualTo(2); id.IsEqualTo(2);
} }
} }
} }
......
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