Path: redb-route/redb.Route.Sql/Batch/CommandsBatchWriter.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/redb.Route.Sql/Batch/CommandsBatchWriter.cs
----
using redb.Route.Sql.Mapping;
namespace redb.Route.Sql.Batch;
///
/// Break-on-first-error batch on a connection that cannot create a DbBatch: one command, its parameters reused, runs
/// the items one after another as they are read. The first failing item ends the batch — nothing is read or sent after it,
/// and the processor rolls the whole transaction back.
///
internal sealed class CommandsBatchWriter(SqlEndpointOptions options) : IBatchWriter
{
///
public string Strategy => "Commands";
///
public async Task WriteAsync(BatchWriteRequest request, CancellationToken ct)
{
var outcome = new BatchOutcome();
await using var command = new BatchItemCommand(request, options);
while (await request.Items.ReadAsync().ConfigureAwait(false))
{
var index = request.Items.Index;
int affected;
try
{
affected = await command.ExecuteAsync(request.Items.Current, index, ct).ConfigureAwait(false);
}
// A returned row that cannot be mapped is the endpoint's configuration, not this item's failure: it goes up as is.
catch (Exception ex) when (ex is not OperationCanceledException && !ct.IsCancellationRequested && ex is not SqlRowMappingException)
{
throw new BatchItemFailedException(index, ex);
}
outcome.RowsAffected += affected;
outcome.ItemsWritten++;
}
return outcome;
}
}