feat(inbound): expose read-only Database helper on InboundScriptContext

This commit is contained in:
Joseph Doherty
2026-06-16 21:45:13 -04:00
parent 16fc62bfa0
commit daff1446d8
3 changed files with 128 additions and 2 deletions
@@ -12,15 +12,16 @@ namespace ZB.MOM.WW.ScadaBridge.InboundAPI;
/// </summary>
public sealed class InboundDatabaseHelper
{
private readonly IDatabaseGateway _gateway;
private readonly IDatabaseGateway? _gateway;
private readonly CancellationToken _ct;
public InboundDatabaseHelper(IDatabaseGateway gateway, CancellationToken ct)
public InboundDatabaseHelper(IDatabaseGateway? gateway, CancellationToken ct)
{ _gateway = gateway; _ct = ct; }
/// <summary>First column of the first row converted to T (default if no rows).</summary>
public T? QuerySingle<T>(string connectionName, string sql, object? parameters = null)
{
if (_gateway is null) throw new InvalidOperationException("Database is not available for this inbound method");
using var conn = _gateway.GetConnectionAsync(connectionName, _ct).GetAwaiter().GetResult();
using var cmd = conn.CreateCommand();
cmd.CommandText = sql;
@@ -35,6 +36,7 @@ public sealed class InboundDatabaseHelper
public IReadOnlyList<IReadOnlyDictionary<string, object?>> Query(
string connectionName, string sql, object? parameters = null)
{
if (_gateway is null) throw new InvalidOperationException("Database is not available for this inbound method");
using var conn = _gateway.GetConnectionAsync(connectionName, _ct).GetAwaiter().GetResult();
using var cmd = conn.CreateCommand();
cmd.CommandText = sql;