Repository navigation
Commit 2a391a6
authored
Output parameters on stored definitions, and captures on their calls (#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)1 parent eb30290 commit 2a391a6
36 files changed
Lines changed: 5194 additions & 793 deletions
File tree
- Source/Core
- Tests
- Open.Database.Extensions.Core.Tests
- Open.Database.Extensions.LegacyCompiler.Tests
- docsite
- articles
- samples
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
14 | 21 | | |
15 | 22 | | |
16 | 23 | | |
| |||
27 | 34 | | |
28 | 35 | | |
29 | 36 | | |
30 | | - | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
31 | 47 | | |
32 | 48 | | |
33 | 49 | | |
| |||
39 | 55 | | |
40 | 56 | | |
41 | 57 | | |
42 | | - | |
| 58 | + | |
43 | 59 | | |
44 | 60 | | |
45 | 61 | | |
46 | | - | |
| 62 | + | |
47 | 63 | | |
48 | 64 | | |
49 | 65 | | |
50 | | - | |
| 66 | + | |
51 | 67 | | |
52 | 68 | | |
53 | 69 | | |
54 | 70 | | |
55 | 71 | | |
56 | 72 | | |
57 | | - | |
| 73 | + | |
58 | 74 | | |
59 | 75 | | |
60 | 76 | | |
61 | | - | |
| 77 | + | |
| 78 | + | |
62 | 79 | | |
63 | 80 | | |
64 | 81 | | |
| |||
83 | 100 | | |
84 | 101 | | |
85 | 102 | | |
86 | | - | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
87 | 109 | | |
88 | 110 | | |
89 | 111 | | |
| |||
262 | 284 | | |
263 | 285 | | |
264 | 286 | | |
265 | | - | |
266 | | - | |
267 | | - | |
268 | | - | |
269 | | - | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
270 | 294 | | |
271 | 295 | | |
272 | 296 | | |
| |||
282 | 306 | | |
283 | 307 | | |
284 | 308 | | |
285 | | - | |
286 | | - | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
287 | 379 | | |
288 | 380 | | |
| 381 | + | |
289 | 382 | | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
| 512 | + | |
| 513 | + | |
| 514 | + | |
| 515 | + | |
| 516 | + | |
| 517 | + | |
| 518 | + | |
| 519 | + | |
| 520 | + | |
| 521 | + | |
| 522 | + | |
| 523 | + | |
| 524 | + | |
| 525 | + | |
| 526 | + | |
| 527 | + | |
| 528 | + | |
| 529 | + | |
| 530 | + | |
| 531 | + | |
| 532 | + | |
| 533 | + | |
| 534 | + | |
| 535 | + | |
| 536 | + | |
| 537 | + | |
| 538 | + | |
| 539 | + | |
| 540 | + | |
| 541 | + | |
| 542 | + | |
| 543 | + | |
| 544 | + | |
| 545 | + | |
| 546 | + | |
| 547 | + | |
| 548 | + | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
290 | 560 | | |
0 commit comments