From f7e23d746bbf78f3f025142466ba4919abb8e9b9 Mon Sep 17 00:00:00 2001 From: Terry Aney Date: Sun, 10 Mar 2024 12:31:15 -0600 Subject: [PATCH 1/3] Added CommandDefinition overload for Task> QueryAsync --- Dapper/SqlMapper.Async.cs | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/Dapper/SqlMapper.Async.cs b/Dapper/SqlMapper.Async.cs index 6fb8ca4c..7f054bc5 100644 --- a/Dapper/SqlMapper.Async.cs +++ b/Dapper/SqlMapper.Async.cs @@ -947,6 +947,7 @@ private static async Task> MultiMapAsync /// Perform an asynchronous multi-mapping query with an arbitrary number of input types. @@ -965,11 +966,24 @@ private static async Task> MultiMapAsyncIs it a stored proc or a batch? /// An enumerable of . [System.Diagnostics.CodeAnalysis.SuppressMessage("ApiDesign", "RS0026:Do not add multiple public overloads with optional parameters", Justification = "Grandfathered")] - public static Task> QueryAsync(this IDbConnection cnn, string sql, Type[] types, Func map, object? param = null, IDbTransaction? transaction = null, bool buffered = true, string splitOn = "Id", int? commandTimeout = null, CommandType? commandType = null) - { - var command = new CommandDefinition(sql, param, transaction, commandTimeout, commandType, buffered ? CommandFlags.Buffered : CommandFlags.None, default); - return MultiMapAsync(cnn, command, types, map, splitOn); - } + public static Task> QueryAsync(this IDbConnection cnn, string sql, Type[] types, Func map, object? param = null, IDbTransaction? transaction = null, bool buffered = true, string splitOn = "Id", int? commandTimeout = null, CommandType? commandType = null) => + MultiMapAsync(cnn, + new CommandDefinition(sql, param, transaction, commandTimeout, commandType, buffered ? CommandFlags.Buffered : CommandFlags.None, default), types, map, splitOn); + + /// + /// Perform an asynchronous multi-mapping query with an arbitrary number of input types. + /// This returns a single type, combined from the raw types via . + /// + /// The combined type to return. + /// The connection to query on. + /// Array of types in the recordset. + /// The field we should split and read the second object from (default: "Id"). + /// The command to execute. + /// The function to map row types to the return type. + /// An enumerable of . + [System.Diagnostics.CodeAnalysis.SuppressMessage("ApiDesign", "RS0026:Do not add multiple public overloads with optional parameters", Justification = "Grandfathered")] + public static Task> QueryAsync(this IDbConnection cnn, CommandDefinition command, Type[] types, Func map, string splitOn = "Id") => + MultiMapAsync(cnn, command, types, map, splitOn); private static async Task> MultiMapAsync(this IDbConnection cnn, CommandDefinition command, Type[] types, Func map, string splitOn) { From eefbc4eb6bf439587e87be638d4dc465d742ba70 Mon Sep 17 00:00:00 2001 From: Terry Aney Date: Sun, 10 Mar 2024 12:33:33 -0600 Subject: [PATCH 2/3] Code format --- Dapper/SqlMapper.Async.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dapper/SqlMapper.Async.cs b/Dapper/SqlMapper.Async.cs index 7f054bc5..e35f28f6 100644 --- a/Dapper/SqlMapper.Async.cs +++ b/Dapper/SqlMapper.Async.cs @@ -968,7 +968,7 @@ private static async Task> MultiMapAsync> QueryAsync(this IDbConnection cnn, string sql, Type[] types, Func map, object? param = null, IDbTransaction? transaction = null, bool buffered = true, string splitOn = "Id", int? commandTimeout = null, CommandType? commandType = null) => MultiMapAsync(cnn, - new CommandDefinition(sql, param, transaction, commandTimeout, commandType, buffered ? CommandFlags.Buffered : CommandFlags.None, default), types, map, splitOn); + new CommandDefinition(sql, param, transaction, commandTimeout, commandType, buffered ? CommandFlags.Buffered : CommandFlags.None, default), types, map, splitOn); /// /// Perform an asynchronous multi-mapping query with an arbitrary number of input types. From 3437bb7ac6b23ce3c4410109761030ba6aa77ccc Mon Sep 17 00:00:00 2001 From: Terry Aney Date: Mon, 11 Mar 2024 07:48:38 -0600 Subject: [PATCH 3/3] Included QueryAsync with Type[]/CommandDefinition overload in test --- tests/Dapper.Tests/AsyncTests.cs | 68 ++++++++++++++++++-------------- 1 file changed, 38 insertions(+), 30 deletions(-) diff --git a/tests/Dapper.Tests/AsyncTests.cs b/tests/Dapper.Tests/AsyncTests.cs index 7a9abf2d..2c8fdfed 100644 --- a/tests/Dapper.Tests/AsyncTests.cs +++ b/tests/Dapper.Tests/AsyncTests.cs @@ -866,38 +866,46 @@ public async Task TestMultiMapArbitraryMapsAsync() return board; }; + Action assertResult = p => + { + Assert.Equal(1, p.Id); + Assert.Equal("Review Board 1", p.Name); + Assert.NotNull(p.User1); + Assert.NotNull(p.User2); + Assert.NotNull(p.User3); + Assert.NotNull(p.User4); + Assert.NotNull(p.User5); + Assert.NotNull(p.User6); + Assert.NotNull(p.User7); + Assert.NotNull(p.User8); + Assert.NotNull(p.User9); + Assert.Equal(1, p.User1.Id); + Assert.Equal(2, p.User2.Id); + Assert.Equal(3, p.User3.Id); + Assert.Equal(4, p.User4.Id); + Assert.Equal(5, p.User5.Id); + Assert.Equal(6, p.User6.Id); + Assert.Equal(7, p.User7.Id); + Assert.Equal(8, p.User8.Id); + Assert.Equal(9, p.User9.Id); + Assert.Equal("User 1", p.User1.Name); + Assert.Equal("User 2", p.User2.Name); + Assert.Equal("User 3", p.User3.Name); + Assert.Equal("User 4", p.User4.Name); + Assert.Equal("User 5", p.User5.Name); + Assert.Equal("User 6", p.User6.Name); + Assert.Equal("User 7", p.User7.Name); + Assert.Equal("User 8", p.User8.Name); + Assert.Equal("User 9", p.User9.Name); + }; + var data = (await connection.QueryAsync(sql, types, mapper).ConfigureAwait(false)).ToList(); - var p = data[0]; - Assert.Equal(1, p.Id); - Assert.Equal("Review Board 1", p.Name); - Assert.NotNull(p.User1); - Assert.NotNull(p.User2); - Assert.NotNull(p.User3); - Assert.NotNull(p.User4); - Assert.NotNull(p.User5); - Assert.NotNull(p.User6); - Assert.NotNull(p.User7); - Assert.NotNull(p.User8); - Assert.NotNull(p.User9); - Assert.Equal(1, p.User1.Id); - Assert.Equal(2, p.User2.Id); - Assert.Equal(3, p.User3.Id); - Assert.Equal(4, p.User4.Id); - Assert.Equal(5, p.User5.Id); - Assert.Equal(6, p.User6.Id); - Assert.Equal(7, p.User7.Id); - Assert.Equal(8, p.User8.Id); - Assert.Equal(9, p.User9.Id); - Assert.Equal("User 1", p.User1.Name); - Assert.Equal("User 2", p.User2.Name); - Assert.Equal("User 3", p.User3.Name); - Assert.Equal("User 4", p.User4.Name); - Assert.Equal("User 5", p.User5.Name); - Assert.Equal("User 6", p.User6.Name); - Assert.Equal("User 7", p.User7.Name); - Assert.Equal("User 8", p.User8.Name); - Assert.Equal("User 9", p.User9.Name); + assertResult( data[0] ); + + data = (await connection.QueryAsync(new CommandDefinition(sql, flags: CommandFlags.None), types, mapper).ConfigureAwait(false)).ToList(); + + assertResult( data[0] ); } finally {