Skip to content

Commit 0754a47

Browse files
committed
add CommandTimeout to Merge
1 parent e5ace3c commit 0754a47

File tree

2 files changed

+31
-8
lines changed

2 files changed

+31
-8
lines changed

src/FluentCommand.SqlServer/Merge/DataMerge.cs

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ public class DataMerge : DisposableBase, IDataMerge
1616
private readonly IDataSession _dataSession;
1717
private readonly DataMergeDefinition _mergeDefinition;
1818

19+
private int _commandTimeout = 0;
20+
1921
/// <summary>
2022
/// Initializes a new instance of the <see cref="DataMerge"/> class.
2123
/// </summary>
@@ -137,6 +139,16 @@ public IDataMerge Mode(DataMergeMode mergeMode)
137139
return this;
138140
}
139141

142+
/// <summary>
143+
/// Sets the wait time before terminating the attempt to execute a command and generating an error.
144+
/// </summary>
145+
/// <param name="timeout">TThe time in seconds to wait for the command to execute.</param>
146+
/// A fluent <see langword="interface" /> to a <see cref="DataMerge " /> operation.
147+
public IDataMerge CommandTimeout(int timeout)
148+
{
149+
_commandTimeout = timeout;
150+
return this;
151+
}
140152

141153
/// <summary>
142154
/// Merges the specified <paramref name="data"/> into the <see cref="TargetTable"/>.
@@ -531,15 +543,17 @@ private void Merge(IDataReader reader, int rows, Action<DbCommand> executeFactor
531543
}
532544

533545
// run merge statement
534-
using (var mergeCommand = _dataSession.Connection.CreateCommand())
535-
{
536-
mergeCommand.CommandText = mergeSql;
537-
mergeCommand.CommandType = CommandType.Text;
538-
mergeCommand.Transaction = sqlTransaction;
546+
using var mergeCommand = _dataSession.Connection.CreateCommand();
539547

540-
// run merge with factory
541-
executeFactory(mergeCommand);
542-
}
548+
mergeCommand.CommandText = mergeSql;
549+
mergeCommand.CommandType = CommandType.Text;
550+
mergeCommand.Transaction = sqlTransaction;
551+
552+
if (_commandTimeout > 0)
553+
mergeCommand.CommandTimeout = _commandTimeout;
554+
555+
// run merge with factory
556+
executeFactory(mergeCommand);
543557
}
544558
finally
545559
{
@@ -614,6 +628,9 @@ await bulkCopy
614628
mergeCommand.CommandType = CommandType.Text;
615629
mergeCommand.Transaction = sqlTransaction;
616630

631+
if (_commandTimeout > 0)
632+
mergeCommand.CommandTimeout = _commandTimeout;
633+
617634
// run merge with factory
618635
await executeFactory(mergeCommand, cancellationToken)
619636
.ConfigureAwait(false);

src/FluentCommand.SqlServer/Merge/IDataMerge.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,12 @@ public interface IDataMerge
8080
/// </returns>
8181
IDataMerge Mode(DataMergeMode mergeMode);
8282

83+
/// <summary>
84+
/// Sets the wait time before terminating the attempt to execute a command and generating an error.
85+
/// </summary>
86+
/// <param name="timeout">TThe time in seconds to wait for the command to execute.</param>
87+
/// A fluent <see langword="interface" /> to a <see cref="DataMerge " /> operation.
88+
IDataMerge CommandTimeout(int timeout);
8389

8490
/// <summary>
8591
/// Merges the specified <paramref name="data"/> into the <see cref="TargetTable"/>.

0 commit comments

Comments
 (0)