Skip to content

Commit

Permalink
Merge pull request #58 from seesharper/fix-nullables-with-scalarvalues
Browse files Browse the repository at this point in the history
Fix nullables with scalarvalues
  • Loading branch information
seesharper committed Sep 1, 2022
2 parents e5889d8 + eaa8cb6 commit 67c5f9d
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 11 deletions.
18 changes: 15 additions & 3 deletions src/DbReader.Tests/IntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ static IntegrationTests()

DbReaderOptions.WhenPassing<CustomValueType>()
.Use((parameter, argument) => parameter.Value = argument.Value);

DbReaderOptions.WhenReading<CustomScalarValue>().Use((datarecord, i) => new CustomScalarValue(datarecord.GetInt64(i)));
}


Expand Down Expand Up @@ -375,7 +377,6 @@ public void ShouldConvertScalarValue()
[Fact]
public async Task ShouldConvertScalarValueUsingValueConverter()
{
DbReaderOptions.WhenReading<CustomScalarValue>().Use((datarecord, i) => new CustomScalarValue(datarecord.GetInt64(i)));
using (var connection = CreateConnection())
{
var scalar = await connection.ExecuteScalarAsync<CustomScalarValue>("SELECT COUNT(*) FROM Customers");
Expand All @@ -384,16 +385,27 @@ public async Task ShouldConvertScalarValueUsingValueConverter()
}

[Fact]
public async Task ShouldHandleNullScalarValueUsingValueConverter()
public async Task ShouldHandleReturningNullIntoNullableTypeWithValueConverter()
{
DbReaderOptions.WhenReading<CustomScalarValue?>().Use((datarecord, i) => new CustomScalarValue(datarecord.GetInt64(i)));
using (var connection = CreateConnection())
{
var scalar = await connection.ExecuteScalarAsync<CustomScalarValue?>("SELECT NULL FROM Customers WHERE CustomerID = 'ALFKI'");
scalar.ShouldBe(null);
}
}

[Fact]
public async Task ShouldHandleReturningValueIntoNullableTypeWithValueConverter()
{
using (var connection = CreateConnection())
{
var scalar = await connection.ExecuteScalarAsync<CustomScalarValue?>("SELECT 1 FROM Customers WHERE CustomerID = 'ALFKI'");
scalar.Value.Value.ShouldBe(1);
}
}

//ShouldHandleReturningNullIntoNullableTypeWithValueConverter()


[Fact]
public async Task ShouldGetScalarValueAsync()
Expand Down
8 changes: 5 additions & 3 deletions src/DbReader/DbConnectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -242,12 +242,14 @@ private static T ReadScalarValue<T>(IDataReader reader)
{
if (!reader.Read() || reader.IsDBNull(0))
{
return default(T);
return default;
}

if (ValueConverter.CanConvert(typeof(T)))
var underlyingType = typeof(T).GetUnderlyingType();

if (ValueConverter.CanConvert(underlyingType))
{
return ValueConverter.Convert<T>(reader, 0);
return (T)ValueConverter.GetConvertMethod(underlyingType).Invoke(null, new object[] { reader, 0 });
}

return ConvertFromDbValue<T>(reader.GetValue(0));
Expand Down
2 changes: 1 addition & 1 deletion src/DbReader/DbReader.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netcoreapp3.1;netstandard2.0;net462</TargetFrameworks>
<Version>2.5.1</Version>
<Version>2.5.2</Version>
<Authors>Bernhard Richter</Authors>
<PackageProjectUrl>https://github.com/seesharper/DbReader</PackageProjectUrl>
<RepositoryType>git</RepositoryType>
Expand Down
8 changes: 4 additions & 4 deletions src/DbReader/ValueConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ namespace DbReader
using System.Collections.Concurrent;
using System.Data;
using System.Reflection;

/// <summary>
/// Used to register a custom conversion from a <see cref="IDataRecord"/> to an instance of a given <see cref="Type"/>.
/// </summary>
public static class ValueConverter
{
private static readonly ConcurrentDictionary<Type, Delegate> ReadDelegates =
new ConcurrentDictionary<Type, Delegate>();

/// <summary>
/// Registers a function delegate that creates a value of <typeparamref name="T"/> from an <see cref="IDataRecord"/>
/// at the specified ordinal (column index).
Expand All @@ -46,7 +46,7 @@ public static void RegisterReadDelegate<T>(Func<IDataRecord, int, T> convertFunc
{
ReadDelegates.AddOrUpdate(typeof(T), type => convertFunction, (type, del) => convertFunction);
}

/// <summary>
/// Determines if the given <paramref name="type"/> can be converted.
/// </summary>
Expand Down Expand Up @@ -79,6 +79,6 @@ internal static MethodInfo GetConvertMethod(Type type)
{
var openGenericConvertMethod = typeof(ValueConverter).GetMethod("Convert", BindingFlags.Static | BindingFlags.Public);
return openGenericConvertMethod.MakeGenericMethod(type);
}
}
}
}

0 comments on commit 67c5f9d

Please sign in to comment.