Skip to content

Commit 600c1ac

Browse files
author
Nick Craver
committed
Add test cases and cleanup last call site.
This merges in the work in #1663 and cleans up the convert a bit further.
1 parent d7ad382 commit 600c1ac

File tree

4 files changed

+29
-9
lines changed

4 files changed

+29
-9
lines changed

Dapper/SqlMapper.GridReader.Async.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ private async Task<IEnumerable<T>> ReadBufferedAsync<T>(int index, Func<IDataRea
233233
var buffer = new List<T>();
234234
while (index == gridIndex && await reader.ReadAsync(cancel).ConfigureAwait(false))
235235
{
236-
buffer.Add((T)deserializer(reader));
236+
buffer.Add(ConvertTo<T>(deserializer(reader)));
237237
}
238238
return buffer;
239239
}

Dapper/SqlMapper.GridReader.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
using System.Data;
44
using System.Linq;
55
using System.Globalization;
6+
using System.Runtime.CompilerServices;
7+
68
namespace Dapper
79
{
810
public static partial class SqlMapper
@@ -418,15 +420,13 @@ public void Dispose()
418420
GC.SuppressFinalize(this);
419421
}
420422

421-
private static T ConvertTo<T>(object value)
423+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
424+
private static T ConvertTo<T>(object value) => value switch
422425
{
423-
if (value is null or DBNull)
424-
return default;
425-
else if (value is T t)
426-
return t;
427-
else
428-
return (T)Convert.ChangeType(value, Nullable.GetUnderlyingType(typeof(T)) ?? typeof(T), CultureInfo.InvariantCulture);
429-
}
426+
T typed => typed,
427+
null or DBNull => default,
428+
_ => (T)Convert.ChangeType(value, Nullable.GetUnderlyingType(typeof(T)) ?? typeof(T), CultureInfo.InvariantCulture),
429+
};
430430
}
431431
}
432432
}

tests/Dapper.Tests/AsyncTests.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,16 @@ public async Task TestMultiAsync()
240240
}
241241
}
242242

243+
[Fact]
244+
public async Task TestMultiConversionAsync()
245+
{
246+
using (SqlMapper.GridReader multi = await connection.QueryMultipleAsync("select Cast(1 as BigInt) Col1; select Cast(2 as BigInt) Col2").ConfigureAwait(false))
247+
{
248+
Assert.Equal(1, multi.ReadAsync<int>().Result.Single());
249+
Assert.Equal(2, multi.ReadAsync<int>().Result.Single());
250+
}
251+
}
252+
243253
[Fact]
244254
public async Task TestMultiAsyncViaFirstOrDefault()
245255
{

tests/Dapper.Tests/QueryMultipleTests.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@ public void TestQueryMultipleBuffered()
3131
}
3232
}
3333

34+
[Fact]
35+
public void TestMultiConversion()
36+
{
37+
using (SqlMapper.GridReader multi = connection.QueryMultiple("select Cast(1 as BigInt) Col1; select Cast(2 as BigInt) Col2"))
38+
{
39+
Assert.Equal(1, multi.Read<int>().Single());
40+
Assert.Equal(2, multi.Read<int>().Single());
41+
}
42+
}
43+
3444
[Fact]
3545
public void TestQueryMultipleNonBufferedIncorrectOrder()
3646
{

0 commit comments

Comments
 (0)