using System.Data.Common;
using ScadaLink.Commons.Types;
namespace ScadaLink.Commons.Interfaces.Services;
///
/// Interface for database access from scripts.
/// Implemented by ExternalSystemGateway, consumed by ScriptRuntimeContext.
///
public interface IDatabaseGateway
{
///
/// Returns an ADO.NET DbConnection (typically SqlConnection) from the named connection.
/// Connection pooling is managed by the underlying provider.
/// Caller is responsible for disposing.
///
Task GetConnectionAsync(
string connectionName,
CancellationToken cancellationToken = default);
///
/// Submits a SQL write to the store-and-forward engine for reliable delivery.
///
///
/// Audit Log #23 (M3): caller-supplied tracking id used as the
/// store-and-forward message id so the S&F retry loop can read it
/// back via StoreAndForwardMessage.Id and emit per-attempt /
/// terminal cached-write telemetry under the same id. Defaults to
/// null — when omitted the S&F engine mints a fresh GUID and no
/// M3 telemetry is correlated (pre-M3 caller behaviour).
///
///
/// Audit Log #23 (ExecutionId Task 4): the originating script execution's
/// per-run correlation id. When the write is buffered on a transient
/// failure this is threaded onto the S&F message so the retry-loop
/// cached-write audit rows carry it. null when not threaded.
///
///
/// Audit Log #23 (ExecutionId Task 4): the originating script identifier,
/// threaded onto the buffered S&F message alongside
/// . null when not known.
///
///
/// Audit Log #23 (ParentExecutionId Task 6): the ExecutionId of the
/// inbound-API request that spawned the originating script execution.
/// When the write is buffered on a transient failure this is threaded onto
/// the S&F message alongside so the
/// retry-loop cached-write audit rows carry it. null for a
/// non-routed run.
///
Task CachedWriteAsync(
string connectionName,
string sql,
IReadOnlyDictionary? parameters = null,
string? originInstanceName = null,
CancellationToken cancellationToken = default,
TrackedOperationId? trackedOperationId = null,
Guid? executionId = null,
string? sourceScript = null,
Guid? parentExecutionId = null);
}