using Dapper; using JdeScoping.DataAccess.Interfaces; using JdeScoping.DataSync.Etl.Contracts; namespace JdeScoping.DataSync.Etl.Scripts; /// /// SQL script runner that executes SQL commands against the database. /// public class SqlScriptRunner : IScriptRunner { private readonly IDbConnectionFactory _connectionFactory; private readonly string _sql; private readonly object? _parameters; private readonly int _timeoutSeconds; /// /// The name of this script. /// public string ScriptName { get; } /// /// Initializes a new instance of the class. /// /// The database connection factory. /// The SQL command to execute. /// The optional name of the script. /// The optional parameters for the SQL command. /// The command timeout in seconds. public SqlScriptRunner( IDbConnectionFactory connectionFactory, string sql, string? name = null, object? parameters = null, int timeoutSeconds = 3600) { ArgumentNullException.ThrowIfNull(connectionFactory); ArgumentException.ThrowIfNullOrWhiteSpace(sql); _connectionFactory = connectionFactory; _sql = sql; _parameters = parameters; _timeoutSeconds = timeoutSeconds; ScriptName = name ?? "SqlScript"; } /// /// Executes the SQL script asynchronously. /// /// The cancellation token. public async Task ExecuteAsync(CancellationToken cancellationToken = default) { await using var connection = await _connectionFactory.CreateLotFinderConnectionAsync(cancellationToken); await connection.ExecuteAsync( new CommandDefinition( _sql, _parameters, commandTimeout: _timeoutSeconds, cancellationToken: cancellationToken)); } }