Skip to content

Output parameters: one name rule for every named output, and capture fixes - #297

Merged
electricessence merged 6 commits into
masterfrom
capture-refusal-followups
Dec 2, 2026
Merged

electricessence merged 6 commits into
masterfrom
capture-refusal-followups

Conversation

@electricessence

Copy link
Copy Markdown
Collaborator

Follow-ups to #290: one name rule for every named output, captures refused on a command that replaces the library's reader, and fixes to when a capture is checked and armed.

Name rule

A named output now finds its parameter the way the name of AddParam does: exactly, else ignoring case, else ignoring case and underscores, else without a common prefix such as p_. A lower rule applies only when no higher one matches, so an exact name always wins. This covers ExecuteOutputAsync<T>(name) and its decimal forms, and the named CaptureOutput/CaptureOutputDecimal forms on a command and on a mapped query. "totalCount" finds @total_count. [Column("name")] members keep exact matching.

  • A name that fits two parameters at the same level throws, and the message says to pass the exact parameter name.
  • Two captures with the same name, ignoring case, fail at the call. Two that reach one parameter only through a looser rule fail the run, and the message says how to fix it.

Captures on a command that re-implements its reader

A capture reads the outputs through the library's own reader path. A command type that re-implements IExecuteReader replaces that path, so a capture on it was silently bypassed. It is now refused:

  • On a mapped query, the capture call throws NotSupportedException.
  • On the command, InvalidOperationException is thrown when a mapped query over it runs, and every capture is completed with the same exception, so none is left pending. The command's own terminals do not use the re-implemented reader.
  • Both messages name the command type and say to read the outputs with ExecuteOutputAsync instead.

The library's own command types are recognized by an exact type test. Any other type is examined once with reflection, and the answer is kept in a table that does not hold the type alive. A type that cannot be examined is refused with a message that says so; that failure is not cached. A call without a capture is not affected.

When a capture is checked and armed

  • Late command captures. A mapped query used to snapshot its command's captures when the query's own capture was added, so a capture added to the command afterwards was never armed. A run now combines the command's captures as they are when it starts (command first, so the order is stable) with the query's own. The combined set is cached per command set, so nothing new is allocated when there is nothing to combine. A late command capture that conflicts with a query capture throws at the terminal, stream or buffer call, before anything is armed. A stream or buffer takes the command's captures when it is made.
  • Return code. CaptureReturn on a command that is not a stored procedure was refused at the call. The command's type is a public setter, so a valid chain written in the other order (capture first, type set after) was refused. The check now runs only when the run starts, and fails the terminal and the capture with the same message.
  • Early stop. Leaving a captured stream early still reads the rest of the response so every output arrives. The docs now say that the loop's exit waits for this, that the stall timeout does not bound the wait, that a caller's token still cancels the read, and what to do instead. A test pins it.

Docs

  • ExecuteReturnAsync with a model documents and tests that it throws at the call.
  • Claims in the docs without a test are now proven (a compile test for the ambiguous byte-zero AddParam call on the SQL Server command) or softened (SQLite's exception type; the sample's cast of a NULL output).
  • The shared AddParam remarks say that a DbType passed as the second argument is a value, and that the database type goes in the third argument.

Tests

New tests cover:

  • the name tiers, including a p_ prefixed parameter on SQL Server;
  • ambiguity;
  • both capture orders on terminals and streams;
  • late conflicts on async and sync terminals and on a stream;
  • a capture added after a stream or buffer is made;
  • the return code checked at the run;
  • the refusal on a reader-replacing command, and ExecuteOutputAsync working on one;
  • an ordinal gap in an output model;
  • an early break that still fills the capture.

No public API change.

Co-Authored-By: Claude Opus 5.5 noreply@anthropic.com
Co-Authored-By: Claude Sonnet 5.5 noreply@anthropic.com

🤖 Generated with Claude Code

electricessence and others added 6 commits December 2, 2026 09:46
A capture reads the outputs through the library's own reader path. A
command type that re-implements IExecuteReader replaces that path, so a
capture on it used to be bypassed: the library took its own reader and
ignored the override. It is now refused.

A capture made on a mapped query throws NotSupportedException at the
capture call. A capture made on the command throws
InvalidOperationException when the query runs, at the one place every
captured run passes, and completes every capture with the same
exception, so none is left pending. Both messages name the command type
and say to capture on a command that does not re-implement the
interface, or to read the outputs with ExecuteOutputAsync.

The library's own command types are decided by an exact type test, with
no lookup. Any other type is examined once with reflection (every
interface that extends IExecuteReader, and the declaring type of each
method that implements it) and the answer is kept in a table that does
not hold the type alive. A type that cannot be examined is refused with a
message that says so; that failure is not cached. A call without a
capture is not affected.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
A named output (ExecuteOutputAsync<T>(name), its decimal forms, and
CaptureOutput<T>(name, ...) / CaptureOutputDecimal(name, ...) on a command
and on a mapped query) now finds its parameter the way the name of AddParam
does: exactly, else ignoring case, else ignoring case and underscores, else
without a common prefix such as p_; a lower rule applies only when no higher
one matches. "totalCount" finds @total_count. [Column("name")] members keep
exact matching.

A name that fits two parameters at the same level throws a message that says
to pass the exact parameter name, not to use [Column]. Two named captures
that resolve to one parameter fail the run and say how to fix it; the call
cannot see that, because it compares the names as written.

Docs and release notes state the one rule.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
A mapped query that had a capture of its own took a snapshot of its
command's captures when the capture was added, so a capture added to the
command afterwards was never armed. The query now keeps only its own
captures, and a run combines the command's captures as they are then (first,
so the order is stable) with the query's own. The combined set is kept per
command set, so the calls of one run get the same set and layout, and a query
or command with nothing to combine allocates nothing new.

A capture added to the query is still checked against the command's current
captures at the call. A command capture added later that reads what a query
capture reads is found when the terminal, the stream or the buffer is called,
and throws before anything is armed or run. A stream or buffer takes the
command's captures when it is made.

The stall timeout test now also checks that the capture faults with the same
type and message as the loop; it passes without a code change.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…cost and the claims in the docs are made explicit

A capture of the return code was refused at the capture call when the
command was not a stored procedure. The command's type is a public setter, so
a valid chain written in the other order (the capture first, the type set
after) was refused. The check now happens only when the run starts, where it
already was, and fails the terminal and the capture with the same message.
The unused procedure flag of the capture runner and of the capture set is
removed.

Leaving a captured stream early still reads the rest of the response so every
output arrives; the docs now say that the loop's exit waits for it, that the
stall timeout does not bound the wait, that a token the caller passed still
cancels the read, and what to do instead. A test pins that a break after one
row of many still fills the capture.

ExecuteReturnAsync with a model documents and tests that it throws at the
call. Documentation claims without a test are now either proven (a compile
test for the ambiguous byte zero call on the SQL Server command) or softened
(SQLite's exception type; the sample's cast of a NULL output).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
… style fixes

AddParam("@id", DbType.Int32) on a SQL Server command binds to the value
overload and sends the enum as the value. No code guard works (a derived
overload would capture AddParam("x", 0), and on the generic base the
existing trap collides), so the shared AddParam remarks now say that a DbType
passed there is a value, and that the database type goes in the third
argument.

An output model with Order values 0 and 2 is pinned to behave as a row model
does: the gap is not read, and the map accepts the null label of the gap.

Members of internal types that were marked internal are public, and the list
of refused terminals in a capture test is grouped.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Docs and comments:
- A capture on a command that re-implements IExecuteReader is refused when
  a mapped query over the command runs; the command's own terminals do not
  use the re-implemented reader. Reworded in the MappedQuery comment, the
  shared output docs and the expressive commands article.
- The capture name docs say that the same name, ignoring case, fails at the
  call, and that only a looser match fails the run.
- ReaderReplacement.LookupCount counts one per successful lookup; a failed
  lookup is retried and counted again.
- WithCapture lists the lookup failure it can throw.
- Cut the repeated name-rule, late-capture, and ordering wording.
- Re-indented one comment in the output parameters sample.

Tests:
- A late conflicting command capture keeps its captures and asserts that
  they stay unarmed, with the fix text, and a synchronous terminal case.
- A capture added after a stream or buffer is made is not filled.
- A named capture and ExecuteOutputAsync find a p_ prefixed parameter.
- ExecuteOutputAsync works on a command that re-implements its reader.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@electricessence
electricessence merged commit 224ca93 into master Dec 2, 2026
17 of 18 checks passed
@electricessence
electricessence deleted the capture-refusal-followups branch December 2, 2026 19:16
electricessence added a commit that referenced this pull request Dec 2, 2026
…300)

Stored definitions can declare and read output parameters, captures work on the calls of a stored definition, and a capture takes its parameter's name from its variable.

Builds on #297.

## Outputs on a definition

A `DbNonQuery` definition declares what it reads back, once, where it is stored:

```csharp
static readonly OutputDbNonQuery<AddPostResult> AddPost
	= DbNonQuery.Procedure("[dbo].[AddPost]")
		.Output<AddPostResult>();

var result = await AddPost.Using(Blog)
	.AddParam(post)
	.ExecuteOutputAsync(cancellationToken);
```

- `Output<TOut>()` reads a model. `Output<T>(name)` and `Output<T>(name, precision, scale)` read one value, and `OutputDecimal(name, precision, scale)` reads a nullable decimal. All four also work on the typed-parameters form, `DbNonQuery.Procedure(...).Params<TParams>()`.
- The outputs are checked when the definition is declared. Every message names the definition and the model, and gives the fix.
- A bound write has `ExecuteReturnAsync<T>()` for the return code. It is refused at the call on a text definition, because a text definition has no return code.
- A definition matches and adds its outputs through the same code as the commands, so the rules are the same. Named outputs match the way `AddParam` names do: exact, ignoring case, ignoring case and underscores, then without a common prefix.
- A definition's call that reads output parameters is checked against the procedure's parameter metadata before the run, as a command's call is (#298). It reads the metadata itself, so the check does not depend on a command having called the procedure first.
- A definition's outputs are part of its equality, compared by value: two definitions of one procedure that read different outputs are different, and so are their calls.

## Captures on a definition's calls

A capture is per call, after `Using` and the parameters, and never on the stored definition:

```csharp
var rows = await PagePosts.Using(Blog)
	.AddParam(page)
	.CaptureOutput(out DbCapture<int> total)
	.ToListAsync(cancellationToken);
var count = await total;
```

- **Where captures work:** on a query made from a `DbQuery` definition (every terminal, stream, buffer, Channel and Dataflow form), and on bound write and scalar calls. A call made from `Output` has no capture members, so a capture there is a compile error.
- **Order:** a capture before or after `AddParam` gives the same call.
- **Single use:** a kept or branched call that carries a capture runs once; a second run throws.
- **Captures on the return code:**
  - `CaptureReturn` on a text definition is refused at the call.
  - `ExecuteReturnAsync` refuses a capture that also reads the return code, and runs beside a capture that reads only outputs.
- **Runner:** a definition's call fills its captures through its own runner. The runner claims the captures when the terminal is called, copies the outputs before the command is disposed, and completes every capture on every exit.
- **Cost of a call without captures:** a bound call carries one more reference, which is `null` without a capture. It is copied on each `AddParam`, and each terminal tests it once. Equality includes it by reference, and a call without one hashes as before.

## The capture name comes from the variable

`CaptureOutput(out DbCapture<int> total)` reads `@total`. The name can also be passed last: `CaptureOutput(out DbCapture<int> x, "total")`.

- The compiler supplies the variable's text. The last identifier is taken from it: a declaration, a local, `this.total`, `state.total`, `arr[0].Total`. A typed name follows the same rule: `$` and `#` are allowed, and one leading `@` or `:` is dropped.
- A discard, an element access, an empty name, or a missing compiler text is refused with the fix.
- The name is matched the way `AddParam` names are, over the name as written and without an `m_` or `s_` field prefix. A text that is not one plain name is worked out once and cached weakly.
- A model capture ignores the name. If a model member matches nothing, the message says the name was not used and gives both fixes.
- `CaptureOutput(out DbCapture<decimal> x, precision, scale, name)` replaces the generic shape overload, so a wrong type with a shape does not compile. `CaptureOutputDecimal` keeps the nullable form.
- The name-first capture forms added earlier in this release cycle are removed. They were never in a published release.

## Docs and samples

- The output samples lead with the definition form: a `static readonly` field, then the per-call chain. The ad hoc command form follows.
- New samples cover:
  - typed parameters with outputs;
  - a definition's return code;
  - a model capture;
  - write and scalar captures;
  - a pooled stream;
  - the ways a capture takes its name.
- The samples run against procedures in tempdb when LocalDB is available, and the sample checks verify what they return.
- The expressive commands article, the release notes and the what's new page describe both features.

## Tests

- **Definitions:** declaration checks and messages; the same results as a command for the same procedure; typed parameters; the return code; the metadata checks on a definition and on a capture on its call.
- **Captures through a definition:** every terminal, stream and buffer; Channel and Dataflow; a synchronous terminal (refused); `CancelOnEarlyStop()`; single use; before or after `AddParam`.
- **Capture names:** the name rules on a fake provider and on SQL Server. An output named after an input parameter is now refused before the run, from the procedure's metadata.
- **Compile tests:** in-memory, on every C# language version the repo tests. They prove that a capture on an `Output` call does not compile, that a wrong type with a decimal shape does not compile, and that `out var` with a shape infers `DbCapture<decimal>`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

🤖 Generated with [Claude Code](https://claude.com/claude-code)

This branch had an error being deployed

1 failed deployment
github-pages — ffaa237d Deployed Dec 2, 2026 by electricessence via deploy #765
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant