Skip to content

Commit

Permalink
add CommandTimeout to Merge
Browse files Browse the repository at this point in the history
  • Loading branch information
pwelter34 committed Jul 17, 2024
1 parent e5ace3c commit 0754a47
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
33 changes: 25 additions & 8 deletions src/FluentCommand.SqlServer/Merge/DataMerge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public class DataMerge : DisposableBase, IDataMerge
private readonly IDataSession _dataSession;
private readonly DataMergeDefinition _mergeDefinition;

private int _commandTimeout = 0;

/// <summary>
/// Initializes a new instance of the <see cref="DataMerge"/> class.
/// </summary>
Expand Down Expand Up @@ -137,6 +139,16 @@ public IDataMerge Mode(DataMergeMode mergeMode)
return this;
}

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

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

// run merge statement
using (var mergeCommand = _dataSession.Connection.CreateCommand())
{
mergeCommand.CommandText = mergeSql;
mergeCommand.CommandType = CommandType.Text;
mergeCommand.Transaction = sqlTransaction;
using var mergeCommand = _dataSession.Connection.CreateCommand();

// run merge with factory
executeFactory(mergeCommand);
}
mergeCommand.CommandText = mergeSql;
mergeCommand.CommandType = CommandType.Text;
mergeCommand.Transaction = sqlTransaction;

if (_commandTimeout > 0)
mergeCommand.CommandTimeout = _commandTimeout;

// run merge with factory
executeFactory(mergeCommand);
}
finally
{
Expand Down Expand Up @@ -614,6 +628,9 @@ await bulkCopy
mergeCommand.CommandType = CommandType.Text;
mergeCommand.Transaction = sqlTransaction;

if (_commandTimeout > 0)
mergeCommand.CommandTimeout = _commandTimeout;

// run merge with factory
await executeFactory(mergeCommand, cancellationToken)
.ConfigureAwait(false);
Expand Down
6 changes: 6 additions & 0 deletions src/FluentCommand.SqlServer/Merge/IDataMerge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ public interface IDataMerge
/// </returns>
IDataMerge Mode(DataMergeMode mergeMode);

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

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

0 comments on commit 0754a47

Please sign in to comment.