feat(etl): add parameters support to SqlScriptRunner
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
using Dapper;
|
||||||
using JdeScoping.DataAccess.Interfaces;
|
using JdeScoping.DataAccess.Interfaces;
|
||||||
using JdeScoping.DataSync.Etl.Contracts;
|
using JdeScoping.DataSync.Etl.Contracts;
|
||||||
|
|
||||||
@@ -7,6 +8,7 @@ public class SqlScriptRunner : IScriptRunner
|
|||||||
{
|
{
|
||||||
private readonly IDbConnectionFactory _connectionFactory;
|
private readonly IDbConnectionFactory _connectionFactory;
|
||||||
private readonly string _sql;
|
private readonly string _sql;
|
||||||
|
private readonly object? _parameters;
|
||||||
private readonly int _timeoutSeconds;
|
private readonly int _timeoutSeconds;
|
||||||
|
|
||||||
public string ScriptName { get; }
|
public string ScriptName { get; }
|
||||||
@@ -15,6 +17,7 @@ public class SqlScriptRunner : IScriptRunner
|
|||||||
IDbConnectionFactory connectionFactory,
|
IDbConnectionFactory connectionFactory,
|
||||||
string sql,
|
string sql,
|
||||||
string? name = null,
|
string? name = null,
|
||||||
|
object? parameters = null,
|
||||||
int timeoutSeconds = 3600)
|
int timeoutSeconds = 3600)
|
||||||
{
|
{
|
||||||
ArgumentNullException.ThrowIfNull(connectionFactory);
|
ArgumentNullException.ThrowIfNull(connectionFactory);
|
||||||
@@ -22,6 +25,7 @@ public class SqlScriptRunner : IScriptRunner
|
|||||||
|
|
||||||
_connectionFactory = connectionFactory;
|
_connectionFactory = connectionFactory;
|
||||||
_sql = sql;
|
_sql = sql;
|
||||||
|
_parameters = parameters;
|
||||||
_timeoutSeconds = timeoutSeconds;
|
_timeoutSeconds = timeoutSeconds;
|
||||||
ScriptName = name ?? "SqlScript";
|
ScriptName = name ?? "SqlScript";
|
||||||
}
|
}
|
||||||
@@ -29,9 +33,11 @@ public class SqlScriptRunner : IScriptRunner
|
|||||||
public async Task ExecuteAsync(CancellationToken cancellationToken = default)
|
public async Task ExecuteAsync(CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
await using var connection = await _connectionFactory.CreateLotFinderConnectionAsync(cancellationToken);
|
await using var connection = await _connectionFactory.CreateLotFinderConnectionAsync(cancellationToken);
|
||||||
await using var command = connection.CreateCommand();
|
await connection.ExecuteAsync(
|
||||||
command.CommandText = _sql;
|
new CommandDefinition(
|
||||||
command.CommandTimeout = _timeoutSeconds;
|
_sql,
|
||||||
await command.ExecuteNonQueryAsync(cancellationToken);
|
_parameters,
|
||||||
|
commandTimeout: _timeoutSeconds,
|
||||||
|
cancellationToken: cancellationToken));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -41,4 +41,23 @@ public class SqlScriptRunnerTests
|
|||||||
var factory = Substitute.For<IDbConnectionFactory>();
|
var factory = Substitute.For<IDbConnectionFactory>();
|
||||||
Assert.Throws<ArgumentException>(() => new SqlScriptRunner(factory, ""));
|
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