Skip to content

Commit

Permalink
Merge pull request #60 from seesharper/do-not-call-convert-on-null
Browse files Browse the repository at this point in the history
Don't call ArgumentProcessor when value is null
  • Loading branch information
seesharper committed Sep 1, 2022
2 parents 9a3aee6 + c67e93e commit 68d1bc5
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
12 changes: 12 additions & 0 deletions src/DbReader.Tests/IntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,18 @@ public async Task ShouldHandlePassingNullableCustomValueTypeWithValue()
}
}

[Fact]
public async Task ShouldHandlePassingNullableCustomValueTypeWithNull()
{
CustomValueType? shipVia = null;

using (var connection = CreateConnection())
{
var count = connection.ExecuteScalar<long>("SELECT COUNT(*) FROM Orders WHERE ShipVia = @ShipVia", new { ShipVia = shipVia });
count.ShouldBe(0);
}
}

[Fact]
public void ShouldNotSetParameterValueWhenPassingCustomType()
{
Expand Down
9 changes: 8 additions & 1 deletion src/DbReader/ArgumentProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,14 @@ public static void RegisterProcessDelegate<TArgument>(Action<IDataParameter, TAr

public static void Process(Type argumentType, IDataParameter dataParameter, object argument)
{
ProcessDelegates[argumentType.GetUnderlyingType()](dataParameter, argument);
if (argument != null)
{
ProcessDelegates[argumentType.GetUnderlyingType()](dataParameter, argument);
}
else
{
dataParameter.Value = DBNull.Value;
}
}

public static bool CanProcess(Type type)
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.3</Version>
<Version>2.5.4</Version>
<Authors>Bernhard Richter</Authors>
<PackageProjectUrl>https://github.com/seesharper/DbReader</PackageProjectUrl>
<RepositoryType>git</RepositoryType>
Expand Down

0 comments on commit 68d1bc5

Please sign in to comment.