Skip to content

Commit

Permalink
Fix #2113 (#2118)
Browse files Browse the repository at this point in the history
  • Loading branch information
goerch authored Oct 7, 2024
1 parent 70e57f8 commit 38df7bb
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 12 deletions.
14 changes: 4 additions & 10 deletions Dapper/CommandDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,9 @@ namespace Dapper
/// </summary>
public readonly struct CommandDefinition
{
internal static CommandDefinition ForCallback(object? parameters)
internal static CommandDefinition ForCallback(object? parameters, CommandFlags flags)
{
if (parameters is DynamicParameters)
{
return new CommandDefinition(parameters);
}
else
{
return default;
}
return new CommandDefinition(parameters is DynamicParameters ? parameters : null, flags);
}

internal void OnCompleted()
Expand Down Expand Up @@ -113,9 +106,10 @@ internal static CommandType InferCommandType(string sql)
return System.Data.CommandType.StoredProcedure;
}

private CommandDefinition(object? parameters) : this()
private CommandDefinition(object? parameters, CommandFlags flags) : this()
{
Parameters = parameters;
Flags = flags;
CommandText = "";
}

Expand Down
2 changes: 1 addition & 1 deletion Dapper/SqlMapper.Async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -939,7 +939,7 @@ private static async Task<IEnumerable<TReturn>> MultiMapAsync<TFirst, TSecond, T
using var cmd = command.TrySetupAsyncCommand(cnn, info.ParamReader);
using var reader = await ExecuteReaderWithFlagsFallbackAsync(cmd, wasClosed, CommandBehavior.SequentialAccess | CommandBehavior.SingleResult, command.CancellationToken).ConfigureAwait(false);
if (!command.Buffered) wasClosed = false; // handing back open reader; rely on command-behavior
var results = MultiMapImpl<TFirst, TSecond, TThird, TFourth, TFifth, TSixth, TSeventh, TReturn>(null, CommandDefinition.ForCallback(command.Parameters), map, splitOn, reader, identity, true);
var results = MultiMapImpl<TFirst, TSecond, TThird, TFourth, TFifth, TSixth, TSeventh, TReturn>(null, CommandDefinition.ForCallback(command.Parameters, command.Flags), map, splitOn, reader, identity, true);
return command.Buffered ? results.ToList() : results;
}
finally
Expand Down
2 changes: 1 addition & 1 deletion Dapper/SqlMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1658,7 +1658,7 @@ private static IEnumerable<TReturn> MultiMapImpl<TReturn>(this IDbConnection? cn
var deserializers = GenerateDeserializers(identity, splitOn, reader);
deserializer = cinfo.Deserializer = new DeserializerState(hash, deserializers[0]);
otherDeserializers = cinfo.OtherDeserializers = deserializers.Skip(1).ToArray();
SetQueryCache(identity, cinfo);
if (command.AddToCache) SetQueryCache(identity, cinfo);
}

Func<DbDataReader, TReturn> mapIt = GenerateMapper(types.Length, deserializer.Func, otherDeserializers, map);
Expand Down
36 changes: 36 additions & 0 deletions tests/Dapper.Tests/AsyncTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -995,5 +995,41 @@ public void AssertNoCacheWorksForQueryMultiple()
Assert.Equal(123, c);
Assert.Equal(456, d);
}

[Fact]
public async Task AssertNoCacheWorksForMultiMap()
{
const int a = 123, b = 456;
var cmdDef = new CommandDefinition("select @a as a, @b as b;", new
{
a,
b
}, commandType: CommandType.Text, flags: CommandFlags.NoCache | CommandFlags.Buffered);

SqlMapper.PurgeQueryCache();
var before = SqlMapper.GetCachedSQLCount();
Assert.Equal(0, before);

await MarsConnection.QueryAsync<int, int, (int, int)>(cmdDef, splitOn: "b", map: (a, b) => (a, b));
Assert.Equal(0, SqlMapper.GetCachedSQLCount());
}

[Fact]
public async Task AssertNoCacheWorksForQueryAsync()
{
const int a = 123, b = 456;
var cmdDef = new CommandDefinition("select @a as a, @b as b;", new
{
a,
b
}, commandType: CommandType.Text, flags: CommandFlags.NoCache | CommandFlags.Buffered);

SqlMapper.PurgeQueryCache();
var before = SqlMapper.GetCachedSQLCount();
Assert.Equal(0, before);

await MarsConnection.QueryAsync<(int, int)>(cmdDef);
Assert.Equal(0, SqlMapper.GetCachedSQLCount());
}
}
}

0 comments on commit 38df7bb

Please sign in to comment.