Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions Dapper/SqlMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2180,9 +2180,10 @@ public static void PackListParameters(IDbCommand command, string namePrefix, obj
bool byPosition = ShouldPassByPosition(command.CommandText);
var list = value as IEnumerable;
var count = 0;
bool isString = value is IEnumerable<string>;
bool isDbString = value is IEnumerable<DbString>;
DbType? dbType = null;
bool isString = value is IEnumerable<string>;
bool isDbString = value is IEnumerable<DbString>;
DbType? dbType = null;
ITypeHandler? handler = null;

int splitAt = SqlMapper.Settings.InListStringSplitCount;
bool viaSplit = splitAt >= 0
Expand All @@ -2199,9 +2200,9 @@ public static void PackListParameters(IDbCommand command, string namePrefix, obj
{
throw new NotSupportedException("The first item in a list-expansion cannot be null");
}
if (!isDbString)
{
dbType = LookupDbType(item.GetType(), "", true, out var handler);
if (!isDbString)
{
dbType = LookupDbType(item.GetType(), "", true, out handler);
}
}
var nextName = namePrefix + count.ToString();
Expand All @@ -2222,7 +2223,16 @@ public static void PackListParameters(IDbCommand command, string namePrefix, obj
}
}

var tmp = listParam.Value = SanitizeParameterValue(item);
object? tmp;
if (handler is null)
{
tmp = listParam.Value = SanitizeParameterValue(item);
}
else
{
handler.SetValue(listParam, item!);
tmp = listParam.Value;
}
if (tmp is not null && tmp is not DBNull)
lastValue = tmp; // only interested in non-trivial values for padding

Expand Down
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
<PackageVersion Include="Norm.net" Version="5.4.0" />
<PackageVersion Include="Npgsql" Version="10.0.2" />
<PackageVersion Include="PetaPoco" Version="5.1.306" />
<PackageVersion Include="pengdows.crud.fakeDb" Version="2.0.5" />
<PackageVersion Include="RepoDb.SqlServer" Version="1.13.1" />
<PackageVersion Include="ServiceStack.OrmLite.SqlServer" Version="10.0.6" />
<PackageVersion Include="Snowflake.Data" Version="5.5.0" />
Expand Down
5 changes: 3 additions & 2 deletions tests/Dapper.Tests/Dapper.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@
<PackageReference Include="log4net" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' != 'net481'">
<PackageReference Include="Npgsql" />
<ItemGroup Condition="'$(TargetFramework)' != 'net481'">
<PackageReference Include="pengdows.crud.fakeDb" />
<PackageReference Include="Npgsql" />
<PackageReference Include="Snowflake.Data" />
</ItemGroup>

Expand Down
58 changes: 58 additions & 0 deletions tests/Dapper.Tests/FakeDbTests.Issue1649.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#if !NET481
using System;
using System.Collections.Generic;
using System.Data;
using pengdows.crud.fakeDb;
using Xunit;

namespace Dapper.Tests
{
public sealed class FakeDbIssue1649Tests
{
[Fact]
public void TypeHandlerIsUsedForEachListExpansionParameter()
{
var handler = new EncodedValueHandler();
SqlMapper.AddTypeHandler(handler);
try
{
using var conn = new fakeDbConnection(new FakeDataStore());
conn.EnqueueNonQueryResult(2);
conn.Open();

var count = conn.Execute("select 1 where Id in @ids", new
{
ids = new[] { new EncodedValue(7), new EncodedValue(8) }
});

Assert.Equal(2, count);
Assert.Equal(new[] { "encoded:7", "encoded:8" }, handler.Values);
}
finally
{
SqlMapper.ResetTypeHandlers();
}
}

private readonly struct EncodedValue
{
public EncodedValue(int value) => Value = value;
public int Value { get; }
}

private sealed class EncodedValueHandler : SqlMapper.TypeHandler<EncodedValue>
{
public List<string> Values { get; } = new();

public override EncodedValue Parse(object value) => new(Convert.ToInt32(value));

public override void SetValue(IDbDataParameter parameter, EncodedValue value)
{
var encoded = $"encoded:{value.Value}";
Values.Add(encoded);
parameter.Value = encoded;
}
}
}
}
#endif