Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #2113 #2118

Merged
merged 1 commit into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On a second thought: is it better to mask out some of the command 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());
}
}
}