Skip to content

Commit

Permalink
Add test cases and cleanup last call site.
Browse files Browse the repository at this point in the history
This merges in the work in #1663 and cleans up the convert a bit further.
  • Loading branch information
Nick Craver committed May 10, 2021
1 parent d7ad382 commit 600c1ac
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Dapper/SqlMapper.GridReader.Async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ private async Task<IEnumerable<T>> ReadBufferedAsync<T>(int index, Func<IDataRea
var buffer = new List<T>();
while (index == gridIndex && await reader.ReadAsync(cancel).ConfigureAwait(false))
{
buffer.Add((T)deserializer(reader));
buffer.Add(ConvertTo<T>(deserializer(reader)));
}
return buffer;
}
Expand Down
16 changes: 8 additions & 8 deletions Dapper/SqlMapper.GridReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using System.Data;
using System.Linq;
using System.Globalization;
using System.Runtime.CompilerServices;

namespace Dapper
{
public static partial class SqlMapper
Expand Down Expand Up @@ -418,15 +420,13 @@ public void Dispose()
GC.SuppressFinalize(this);
}

private static T ConvertTo<T>(object value)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static T ConvertTo<T>(object value) => value switch
{
if (value is null or DBNull)
return default;
else if (value is T t)
return t;
else
return (T)Convert.ChangeType(value, Nullable.GetUnderlyingType(typeof(T)) ?? typeof(T), CultureInfo.InvariantCulture);
}
T typed => typed,
null or DBNull => default,
_ => (T)Convert.ChangeType(value, Nullable.GetUnderlyingType(typeof(T)) ?? typeof(T), CultureInfo.InvariantCulture),
};
}
}
}
10 changes: 10 additions & 0 deletions tests/Dapper.Tests/AsyncTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,16 @@ public async Task TestMultiAsync()
}
}

[Fact]
public async Task TestMultiConversionAsync()
{
using (SqlMapper.GridReader multi = await connection.QueryMultipleAsync("select Cast(1 as BigInt) Col1; select Cast(2 as BigInt) Col2").ConfigureAwait(false))
{
Assert.Equal(1, multi.ReadAsync<int>().Result.Single());
Assert.Equal(2, multi.ReadAsync<int>().Result.Single());
}
}

[Fact]
public async Task TestMultiAsyncViaFirstOrDefault()
{
Expand Down
10 changes: 10 additions & 0 deletions tests/Dapper.Tests/QueryMultipleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ public void TestQueryMultipleBuffered()
}
}

[Fact]
public void TestMultiConversion()
{
using (SqlMapper.GridReader multi = connection.QueryMultiple("select Cast(1 as BigInt) Col1; select Cast(2 as BigInt) Col2"))
{
Assert.Equal(1, multi.Read<int>().Single());
Assert.Equal(2, multi.Read<int>().Single());
}
}

[Fact]
public void TestQueryMultipleNonBufferedIncorrectOrder()
{
Expand Down

0 comments on commit 600c1ac

Please sign in to comment.