Skip to content

Commit c6c8180

Browse files
committed
Custom CreateCommand method. Resolves DapperLib#2127
This allows the user to provide their own IDbCommand to be used by Dapper internally.
1 parent 00a3808 commit c6c8180

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed

Dapper/CommandDefinition.cs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ internal void OnCompleted()
6666
/// </summary>
6767
public CommandFlags Flags { get; }
6868

69+
/// <summary>
70+
/// The create Func that will be used to create the IDbCommand. When null the connection.CreateCommand() will be used.
71+
/// </summary>
72+
internal Func<IDbConnection, IDbCommand>? CreateCommand { get; }
73+
6974
/// <summary>
7075
/// Can async queries be pipelined?
7176
/// </summary>
@@ -95,6 +100,26 @@ public CommandDefinition(string commandText, object? parameters = null, IDbTrans
95100
CancellationToken = cancellationToken;
96101
}
97102

103+
/// <summary>
104+
/// Initialize the command definition
105+
/// </summary>
106+
/// <param name="commandText">The text for this command.</param>
107+
/// <param name="createCommand">The a custom way to create a IDbCommand object. This is to override the default connection.CreateCommand() behavior.</param>
108+
/// <param name="parameters">The parameters for this command.</param>
109+
/// <param name="transaction">The transaction for this command to participate in.</param>
110+
/// <param name="commandTimeout">The timeout (in seconds) for this command.</param>
111+
/// <param name="commandType">The <see cref="CommandType"/> for this command.</param>
112+
/// <param name="flags">The behavior flags for this command.</param>
113+
/// <param name="cancellationToken">The cancellation token for this command.</param>
114+
public CommandDefinition(string commandText, Func<IDbConnection, IDbCommand> createCommand, object? parameters = null, IDbTransaction? transaction = null,
115+
int? commandTimeout = null, CommandType? commandType = null, CommandFlags flags = CommandFlags.Buffered
116+
, CancellationToken cancellationToken = default
117+
)
118+
: this(commandText, parameters, transaction, commandTimeout, commandType, flags, cancellationToken)
119+
{
120+
CreateCommand = createCommand;
121+
}
122+
98123
internal static CommandType InferCommandType(string sql)
99124
{
100125
// if the sql contains any whitespace character (space/tab/cr/lf/etc - via unicode),
@@ -120,7 +145,12 @@ private CommandDefinition(object? parameters, CommandFlags flags) : this()
120145

121146
internal IDbCommand SetupCommand(IDbConnection cnn, Action<IDbCommand, object?>? paramReader)
122147
{
123-
var cmd = cnn.CreateCommand();
148+
IDbCommand cmd;
149+
if (CreateCommand == null)
150+
cmd = cnn.CreateCommand();
151+
else
152+
cmd = CreateCommand(cnn);
153+
124154
var init = GetInit(cmd.GetType());
125155
init?.Invoke(cmd);
126156
if (Transaction is not null)

Dapper/PublicAPI.Shipped.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Dapper.CommandDefinition.Buffered.get -> bool
99
Dapper.CommandDefinition.CancellationToken.get -> System.Threading.CancellationToken
1010
Dapper.CommandDefinition.CommandDefinition() -> void
1111
Dapper.CommandDefinition.CommandDefinition(string! commandText, object? parameters = null, System.Data.IDbTransaction? transaction = null, int? commandTimeout = null, System.Data.CommandType? commandType = null, Dapper.CommandFlags flags = Dapper.CommandFlags.Buffered, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> void
12+
Dapper.CommandDefinition.CommandDefinition(string! commandText, System.Func<System.Data.IDbConnection!, System.Data.IDbCommand!>! createCommand, object? parameters = null, System.Data.IDbTransaction? transaction = null, int? commandTimeout = null, System.Data.CommandType? commandType = null, Dapper.CommandFlags flags = Dapper.CommandFlags.Buffered, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> void
1213
Dapper.CommandDefinition.CommandText.get -> string!
1314
Dapper.CommandDefinition.CommandTimeout.get -> int?
1415
Dapper.CommandDefinition.CommandType.get -> System.Data.CommandType?

tests/Dapper.Tests/MiscTests.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1394,5 +1394,22 @@ public void SetDynamicProperty_WithValueType_Succeeds()
13941394
obj.NewProperty = true;
13951395
Assert.True(obj.NewProperty);
13961396
}
1397+
1398+
[Fact]
1399+
public void TestCreateCommandCalled()
1400+
{
1401+
var customCreateCommandCalled = false;
1402+
var createCommand = new Func<IDbConnection, IDbCommand>(conn =>
1403+
{
1404+
customCreateCommandCalled = true;
1405+
return conn.CreateCommand();
1406+
});
1407+
1408+
var definition = new CommandDefinition("select 1", createCommand);
1409+
1410+
connection.Query<int>(definition);
1411+
1412+
Assert.True(customCreateCommandCalled);
1413+
}
13971414
}
13981415
}

0 commit comments

Comments
 (0)