feat(etl): add parameters support to SqlScriptRunner
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
using Dapper;
|
||||
using JdeScoping.DataAccess.Interfaces;
|
||||
using JdeScoping.DataSync.Etl.Contracts;
|
||||
|
||||
@@ -7,6 +8,7 @@ public class SqlScriptRunner : IScriptRunner
|
||||
{
|
||||
private readonly IDbConnectionFactory _connectionFactory;
|
||||
private readonly string _sql;
|
||||
private readonly object? _parameters;
|
||||
private readonly int _timeoutSeconds;
|
||||
|
||||
public string ScriptName { get; }
|
||||
@@ -15,6 +17,7 @@ public class SqlScriptRunner : IScriptRunner
|
||||
IDbConnectionFactory connectionFactory,
|
||||
string sql,
|
||||
string? name = null,
|
||||
object? parameters = null,
|
||||
int timeoutSeconds = 3600)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(connectionFactory);
|
||||
@@ -22,6 +25,7 @@ public class SqlScriptRunner : IScriptRunner
|
||||
|
||||
_connectionFactory = connectionFactory;
|
||||
_sql = sql;
|
||||
_parameters = parameters;
|
||||
_timeoutSeconds = timeoutSeconds;
|
||||
ScriptName = name ?? "SqlScript";
|
||||
}
|
||||
@@ -29,9 +33,11 @@ public class SqlScriptRunner : IScriptRunner
|
||||
public async Task ExecuteAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
await using var connection = await _connectionFactory.CreateLotFinderConnectionAsync(cancellationToken);
|
||||
await using var command = connection.CreateCommand();
|
||||
command.CommandText = _sql;
|
||||
command.CommandTimeout = _timeoutSeconds;
|
||||
await command.ExecuteNonQueryAsync(cancellationToken);
|
||||
await connection.ExecuteAsync(
|
||||
new CommandDefinition(
|
||||
_sql,
|
||||
_parameters,
|
||||
commandTimeout: _timeoutSeconds,
|
||||
cancellationToken: cancellationToken));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,4 +41,23 @@ public class SqlScriptRunnerTests
|
||||
var factory = Substitute.For<IDbConnectionFactory>();
|
||||
Assert.Throws<ArgumentException>(() => new SqlScriptRunner(factory, ""));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_WithParameters_AcceptsParameters()
|
||||
{
|
||||
// Arrange
|
||||
var factory = Substitute.For<IDbConnectionFactory>();
|
||||
var parameters = new { tableName = "WorkOrder", schemaName = "dbo" };
|
||||
|
||||
// Act
|
||||
var runner = new SqlScriptRunner(
|
||||
factory,
|
||||
"SELECT @tableName, @schemaName",
|
||||
"Test",
|
||||
parameters: parameters);
|
||||
|
||||
// Assert - constructor should accept parameters without error
|
||||
Assert.NotNull(runner);
|
||||
Assert.Equal("Test", runner.ScriptName);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user