Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1798 - Added Support for loosely coupled custom query parameters. #1799

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
10 changes: 10 additions & 0 deletions Dapper/SqlMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2535,6 +2535,16 @@ internal static IList<LiteralToken> GetLiteralTokens(string sql)
il.EmitCall(OpCodes.Callvirt, prop.PropertyType.GetMethod(nameof(ICustomQueryParameter.AddParameter)), null); // stack is now [parameters]
continue;
}
var addParameterMethod = prop.PropertyType.GetMethod(nameof(ICustomQueryParameter.AddParameter), BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic, Type.DefaultBinder, new[] { typeof(IDbCommand), typeof(string) }, null);
if (addParameterMethod is not null)
{
il.Emit(OpCodes.Ldloc, typedParameterLocal); // stack is now [parameters] [typed-param]
il.Emit(callOpCode, prop.GetGetMethod()); // stack is [parameters] [custom]
il.Emit(OpCodes.Ldarg_0); // stack is now [parameters] [custom] [command]
il.Emit(OpCodes.Ldstr, prop.Name); // stack is now [parameters] [custom] [command] [name]
il.EmitCall(OpCodes.Callvirt, addParameterMethod, null); // stack is now [parameters]
continue;
}
#pragma warning disable 618
DbType? dbType = LookupDbType(prop.PropertyType, prop.Name, true, out ITypeHandler handler);
#pragma warning restore 618
Expand Down
2 changes: 2 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ Note: to get the latest pre-release build, add ` -Pre` to the end of the command

(note: new PRs will not be merged until they add release note wording here)

- Added Support for loosely coupled custom query parameters (#1798 via robert-io)

### 2.0.123

- Parameters can now be re-used on subsequent commands (#952 via jamescrowley)
Expand Down
34 changes: 32 additions & 2 deletions tests/Dapper.Tests/ParameterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,21 @@ public void AddParameter(IDbCommand command, string name)
}
}

public class DbLooselyCoupledCustomParam
{
private readonly IDbDataParameter _sqlParameter;

public DbLooselyCoupledCustomParam(IDbDataParameter sqlParameter)
{
_sqlParameter = sqlParameter;
}

private void AddParameter(IDbCommand command, string name)
{
command.Parameters.Add(_sqlParameter);
}
}

private static IEnumerable<IDataRecord> CreateSqlDataRecordList(IDbCommand command, IEnumerable<int> numbers)
{
if (command is System.Data.SqlClient.SqlCommand) return CreateSqlDataRecordList_SD(numbers);
Expand Down Expand Up @@ -871,8 +886,23 @@ public void TestCustomParameterReuse()
Assert.Equal(123, result2.Foo);
Assert.Equal("abc", result2.Bar);
}



[Fact]
public void TestLooselyCoupledCustomParameter()
{
var args = new
{
foo = new DbLooselyCoupledCustomParam(Provider.CreateRawParameter("foo", 123)),
bar = "abc"
};
var result = connection.Query("select Foo=@foo, Bar=@bar", args).Single();
int foo = result.Foo;
string bar = result.Bar;
Assert.Equal(123, foo);
Assert.Equal("abc", bar);
}


[Fact]
public void TestDynamicParamNullSupport()
{
Expand Down