Path: redb-route/tests/redb.Route.Tests.Sql/Batch/Fakes/FakeBatchConnection.cs Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route Page: https://redbase.app/src/dev-2026-12-06/redb-route/tests/redb.Route.Tests.Sql/Batch/Fakes/FakeBatchConnection.cs ---- using System.Data; using System.Data.Common; using System.Diagnostics.CodeAnalysis; namespace redb.Route.Tests.Sql.Batch.Fakes; /// /// A connection whose command executions are scripted per execution index, and which counts what the connector /// asks of it. Lets batch algorithms be tested at the exact point where a real provider would fail or be cancelled. /// internal sealed class FakeBatchConnection : DbConnection { private string _connectionString = ""; private int _executions; /// Called for every execution with its zero-based index; returns rows affected or throws. public Func? OnExecute { get; init; } /// How the connection's transactions answer savepoint calls. public SavepointBehavior Savepoints { get; init; } = SavepointBehavior.Unsupported; /// Rolling back a transaction of this connection throws. public bool FailRollback { get; init; } /// The connection was disposed. public bool IsDisposed { get; private set; } /// Commands created through . public int CommandsCreated { get; private set; } /// Statements executed, successful or not. public int Executions => _executions; /// The last transaction begun on this connection. public FakeBatchTransaction? Transaction { get; private set; } /// The connection can create a (). public bool SupportsBatch { get; init; } /// Batch commands create their own parameters (). public bool BatchCommandsCanCreateParameter { get; init; } = true; /// Called for every batch execution with its zero-based index and the batch; returns rows affected or throws. public Func? OnExecuteBatch { get; init; } /// Number of commands in each executed batch, in order. public List BatchSizes { get; } = []; /// Parameters created through . public int BatchParametersCreated { get; internal set; } public override bool CanCreateBatch => SupportsBatch; protected override DbBatch CreateDbBatch() => SupportsBatch ? new FakeDbBatch(this) : throw new NotSupportedException("fake: this connection has no DbBatch"); internal int ExecuteBatch(FakeDbBatch batch, CancellationToken ct) { ct.ThrowIfCancellationRequested(); var index = BatchSizes.Count; BatchSizes.Add(batch.BatchCommands.Count); return OnExecuteBatch?.Invoke(index, batch) ?? batch.BatchCommands.Count; } /// /// Called for every batch executed for its rows, with its zero-based index and the batch; returns a reader over one /// result set per command, or throws. /// public Func? OnExecuteBatchReader { get; init; } internal DbDataReader ExecuteBatchReader(FakeDbBatch batch, CancellationToken ct) { ct.ThrowIfCancellationRequested(); var index = BatchSizes.Count; BatchSizes.Add(batch.BatchCommands.Count); return OnExecuteBatchReader?.Invoke(index, batch) ?? throw new NotSupportedException("fake: no OnExecuteBatchReader script for a batch executed for its rows"); } [AllowNull] public override string ConnectionString { get => _connectionString; set => _connectionString = value ?? ""; } public override string Database => "fake"; public override string DataSource => "fake"; public override string ServerVersion => "0"; public override ConnectionState State => ConnectionState.Open; public override void ChangeDatabase(string databaseName) { } public override void Close() { } public override void Open() { } /// The text of every command execution, in order. public List ExecutedCommandTexts { get; } = []; internal int Execute(string commandText, CancellationToken ct) { ExecutedCommandTexts.Add(commandText); var index = _executions++; return OnExecute?.Invoke(index, ct) ?? 1; } protected override DbTransaction BeginDbTransaction(IsolationLevel isolationLevel) => Transaction = new FakeBatchTransaction(this, isolationLevel); protected override DbCommand CreateDbCommand() { CommandsCreated++; return new FakeBatchCommand(this); } protected override void Dispose(bool disposing) { IsDisposed = true; base.Dispose(disposing); } }