Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
b5abd05
Definitions can declare and read output parameters
electricessence Dec 2, 2026
2bdc77c
Captures work on the calls of a stored definition
electricessence Dec 2, 2026
47f4a42
Captures take their name last, from the variable
electricessence Dec 2, 2026
20546c4
Samples use the name-last capture forms
electricessence Dec 2, 2026
55f274e
Captures always match over their own candidates
electricessence Dec 2, 2026
21cf1e2
Count only a test's own fault as unobserved; fix capture comments
electricessence Dec 2, 2026
02a05ac
Show outputs and captures on stored definitions first
electricessence Dec 2, 2026
36fa94d
Test captures through a definition on channels and blocks
electricessence Dec 2, 2026
023224f
Fix review findings on output definitions and captures
electricessence Dec 2, 2026
7febd4d
Tidy the output parameter samples and docs
electricessence Dec 2, 2026
63aee0d
Say "empty or only white space" for a typed capture name with no char…
electricessence Dec 2, 2026
67aa214
Pin the capture types in the binding tests without unused locals
electricessence Dec 2, 2026
c3ec445
Pass the layout step to the captured run instead of a bool
electricessence Dec 2, 2026
0d75e5e
Merge master: check output parameters against the procedure's metadata
electricessence Dec 2, 2026
b7dd471
Check a definition's outputs against the procedure's metadata
electricessence Dec 2, 2026
0fcbf9a
Expect the metadata refusal in the capture name tests
electricessence Dec 2, 2026
2cfb7e0
Merge master: allocation probe fix
electricessence Dec 2, 2026
d9b6c6b
Include a definition's outputs in its equality
electricessence Dec 2, 2026
b839db1
Format the ExecuteOutputAsync refusal text only when it is thrown
electricessence Dec 2, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
298 changes: 284 additions & 14 deletions Source/Core/BoundCall.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ namespace Open.Database.Extensions;
/// </summary>
public readonly struct NonQueryKind;

/// <summary>
/// Marks a chain that writes and reads output parameters: it ends in
/// <c>ExecuteOutputAsync</c>.
/// </summary>
/// <typeparam name="TOut">What the outputs are read as.</typeparam>
public readonly struct OutputKind<TOut>;

/// <summary>
/// A scalar query or a write bound to where its connections come from, with
/// the values added so far.
Expand All @@ -27,7 +34,16 @@ namespace Open.Database.Extensions;
/// </typeparam>
public readonly struct BoundCall<TKind> : IEquatable<BoundCall<TKind>>
{
internal BoundCall(DbQueryCall call) => Call = call;
internal BoundCall(DbQueryCall call, CaptureSet? captures = null)
{
Call = call;
Captures = captures;
}

// The captures the call carries, or null. Added by the capture extensions
// of BoundCallExtensions after Using and the parameters, and carried by
// every AddParam; compared by reference.
internal CaptureSet? Captures { get; }

// The same value a rows query holds: the definition, the source and the
// values.
Expand All @@ -39,26 +55,27 @@ namespace Open.Database.Extensions;
/// <inheritdoc cref="DbQueryUsing.AddParams{T}(T)"/>
public BoundCall<TKind> AddParams<TValues>(TValues values)
where TValues : notnull
=> new(Call.AddParams(values));
=> new(Call.AddParams(values), Captures);

/// <inheritdoc cref="DbQueryUsing.AddParam{T}(string, T)"/>
public BoundCall<TKind> AddParam<TValue>(string name, TValue value)
=> new(Call.AddParam(name, value));
=> new(Call.AddParam(name, value), Captures);

/// <inheritdoc cref="DbQueryUsing.AddParam(string, string?)"/>
public BoundCall<TKind> AddParam(string name, string? value)
=> new(Call.AddParam(name, value));
=> new(Call.AddParam(name, value), Captures);

/// <inheritdoc cref="DbQueryUsing.AddParam{T}(T, string?)"/>
[OverloadResolutionPriority(-1)]
public BoundCall<TKind> AddParam<TValue>(
TValue value,
[CallerArgumentExpression(nameof(value))] string? expression = null)
=> new(Call.AddParam(value, expression));
=> new(Call.AddParam(value, expression), Captures);

/// <inheritdoc />
public bool Equals(BoundCall<TKind> other)
=> Call.Equals(other.Call);
=> Call.Equals(other.Call)
&& ReferenceEquals(Captures, other.Captures);

/// <inheritdoc />
public override bool Equals(object? obj)
Expand All @@ -83,7 +100,12 @@ public override int GetHashCode()
{
unchecked
{
return Call.GetHashCode() * 31 + typeof(TKind).GetHashCode();
int hash = Call.GetHashCode() * 31 + typeof(TKind).GetHashCode();

// Without a capture the hash is what it always was.
return Captures is null
? hash
: hash * 31 + RuntimeHelpers.GetHashCode(Captures);
}
}
}
Expand Down Expand Up @@ -262,11 +284,13 @@ public static class BoundCallExtensions
public static ValueTask<T> ExecuteScalarAsync<T>(
this BoundCall<ScalarKind<T>> call,
CancellationToken cancellationToken = default)
=> DbQueryRunner.ScalarAsync<T>(
call.Call.Definition,
call.Call.SourceCore,
call.Call.Params,
cancellationToken);
=> call.Captures is { } captures
? DbQueryRunner.ScalarCapturedAsync<T>(
call.Call.Definition, call.Call.SourceCore, call.Call.Params,
captures, cancellationToken)
: DbQueryRunner.ScalarAsync<T>(
call.Call.Definition, call.Call.SourceCore,
call.Call.Params, cancellationToken);

/// <summary>Runs the write and returns the number of rows affected.</summary>
/// <remarks>
Expand All @@ -282,9 +306,255 @@ public static ValueTask<T> ExecuteScalarAsync<T>(
public static ValueTask<int> ExecuteNonQueryAsync(
this BoundCall<NonQueryKind> call,
CancellationToken cancellationToken = default)
=> DbQueryRunner.NonQueryAsync(
call.Call.Definition,
=> call.Captures is { } captures
? DbQueryRunner.NonQueryCapturedAsync(
call.Call.Definition, call.Call.SourceCore, call.Call.Params,
captures, cancellationToken)
: DbQueryRunner.NonQueryAsync(
call.Call.Definition, call.Call.SourceCore,
call.Call.Params, cancellationToken);

/// <summary>
/// Runs the write and reads its output parameters (and return code) as the
/// definition declared with <c>Output</c>.
/// </summary>
/// <remarks>
/// <include file="Output.docs.xml" path="docs/members[@name='def-run']/*" />
/// </remarks>
/// <typeparam name="TOut">What the outputs are read as.</typeparam>
/// <param name="call">The bound call.</param>
/// <include file="Mapped.docs.xml" path="docs/members[@name='token-call']/*" />
/// <returns>A task that represents the asynchronous operation; its result is the model, or the one value.</returns>
/// <exception cref="InvalidOperationException">Before the command executes: an output matches no parameter, or two match one, or <paramref name="call"/> is a <see langword="default"/> value, which is bound to nothing.</exception>
/// <exception cref="InvalidOperationException">
/// <include file="Output.docs.xml" path="docs/members[@name='out-metadata']/*" />
/// </exception>
/// <exception cref="ValueMappingException">A value does not convert, or is NULL and the type does not take it.</exception>
/// <exception cref="OperationCanceledException"><paramref name="cancellationToken"/> is canceled.</exception>
public static ValueTask<TOut> ExecuteOutputAsync<TOut>(
this BoundCall<OutputKind<TOut>> call,
CancellationToken cancellationToken = default)
=> DbQueryRunner.OutputAsync<TOut>(
call.Call.Definition, call.Call.SourceCore,
call.Call.Params, cancellationToken);

/// <summary>
/// Runs the write and returns the stored procedure's return code, converted
/// to <typeparamref name="T"/>.
/// </summary>
/// <remarks>
/// <include file="Output.docs.xml" path="docs/members[@name='def-return']/*" />
/// </remarks>
/// <typeparam name="T">The type of the return code: a single value such as <see cref="int"/>.</typeparam>
/// <param name="call">The bound call.</param>
/// <include file="Mapped.docs.xml" path="docs/members[@name='token-call']/*" />
/// <returns>A task that represents the asynchronous operation; its result is the return code.</returns>
/// <exception cref="InvalidOperationException">Before any connection is used: <typeparamref name="T"/> is a model, the call's definition is text (it has no return code), <paramref name="call"/> is a <see langword="default"/> value, or a capture on the call reads the return code too.</exception>
/// <exception cref="OperationCanceledException"><paramref name="cancellationToken"/> is canceled.</exception>
public static ValueTask<T> ExecuteReturnAsync<T>(
this BoundCall<NonQueryKind> call,
CancellationToken cancellationToken = default)
{
// All checked at the call, before a task exists or a connection is used.
ReturnCode<T>.ThrowUnlessScalar();
DbQuery definition = call.Call.Definition;
if (definition.Kind != CommandType.StoredProcedure)
{
throw new InvalidOperationException(
$"ExecuteReturnAsync<{Shape.TypeName(typeof(T))}>() reads a stored procedure's return code, but this call's definition is {OutputDefinitions.Describe(definition)}, and a text definition has no return code: the provider reports 0. "
+ "Declare the definition with DbNonQuery.Procedure(name) to read the return code.");
}

// A capture of the return code and this would read it twice.
if (call.Captures?.ReturnReader is { } returnReader)
{
throw call.Captures.Refuse(
new InvalidOperationException(
CaptureMessages.ReturnBeside(returnReader)),
cancellationToken);
}

return DbQueryRunner.ReturnAsync<T>(
definition,
call.Call.SourceCore,
call.Call.Params,
call.Captures,
cancellationToken);
}

// ---- captures: per kind, so a call made from Output has none ----

// Returns the call with one more capture, checked at the call. A
// definition's kind never changes, so the return code is checked here.
static BoundCall<TKind> WithCapture<TKind>(
BoundCall<TKind> call,
ICaptureSource source)
=> new(
call.Call,
CaptureSet.WithOnDefinition(
call.Captures, source, call.Call.Definition.Kind));

/// <summary>
/// Reads the output parameters into a <typeparamref name="TOut"/> when the
/// write runs: a model by its members, or one value by the name of the
/// variable.
/// </summary>
/// <include file="Output.docs.xml" path="docs/members[@name='capture-model']/*" />
/// <include file="Output.docs.xml" path="docs/members[@name='out-name-param']/*" />
/// <include file="Output.docs.xml" path="docs/members[@name='capture-call']/*" />
/// <param name="call">The bound write.</param>
public static BoundCall<NonQueryKind> CaptureOutput<TOut>(
this BoundCall<NonQueryKind> call,
out DbCapture<TOut> output,
[CallerArgumentExpression(nameof(output))] string? name = null)
{
CaptureSource<TOut> source = Captures.Output<TOut>(name);
BoundCall<NonQueryKind> captured = WithCapture(call, source);
output = new(source);
return captured;
}

/// <summary>
/// Reads one <see cref="decimal"/> output parameter of the given precision
/// and scale when the write runs. A NULL faults the capture;
/// <c>CaptureOutputDecimal</c> reads it as <see langword="null"/>.
/// </summary>
/// <include file="Output.docs.xml" path="docs/members[@name='capture-decimal']/*" />
/// <include file="Output.docs.xml" path="docs/members[@name='out-name-param']/*" />
/// <include file="Output.docs.xml" path="docs/members[@name='capture-call']/*" />
/// <param name="call">The bound write.</param>
public static BoundCall<NonQueryKind> CaptureOutput(
this BoundCall<NonQueryKind> call,
out DbCapture<decimal> value,
byte precision,
byte scale,
[CallerArgumentExpression(nameof(value))] string? name = null)
{
CaptureSource<decimal> source = Captures.Shaped(name, precision, scale);
BoundCall<NonQueryKind> captured = WithCapture(call, source);
value = new(source);
return captured;
}

/// <summary>
/// Reads one <see cref="decimal"/> output parameter of the given precision
/// and scale when the write runs.
/// </summary>
/// <include file="Output.docs.xml" path="docs/members[@name='capture-decimal']/*" />
/// <include file="Output.docs.xml" path="docs/members[@name='out-name-param']/*" />
/// <include file="Output.docs.xml" path="docs/members[@name='capture-call']/*" />
/// <param name="call">The bound write.</param>
public static BoundCall<NonQueryKind> CaptureOutputDecimal(
this BoundCall<NonQueryKind> call,
out DbCapture<decimal?> value,
byte precision,
byte scale,
[CallerArgumentExpression(nameof(value))] string? name = null)
{
CaptureSource<decimal?> source = Captures.Decimal(name, precision, scale);
BoundCall<NonQueryKind> captured = WithCapture(call, source);
value = new(source);
return captured;
}

/// <summary>
/// Reads the stored procedure's return code, converted to
/// <typeparamref name="TValue"/>, when the write runs.
/// </summary>
/// <include file="Output.docs.xml" path="docs/members[@name='capture-return']/*" />
/// <include file="Output.docs.xml" path="docs/members[@name='capture-call']/*" />
/// <param name="call">The bound write.</param>
public static BoundCall<NonQueryKind> CaptureReturn<TValue>(
this BoundCall<NonQueryKind> call,
out DbCapture<TValue> returnCode)
{
CaptureSource<TValue> source = Captures.Return<TValue>();
BoundCall<NonQueryKind> captured = WithCapture(call, source);
returnCode = new(source);
return captured;
}

/// <summary>
/// Reads the output parameters into a <typeparamref name="TOut"/> when the
/// query runs: a model by its members, or one value by the name of the
/// variable.
/// </summary>
/// <typeparam name="T">The type of the scalar, from the call.</typeparam>
/// <include file="Output.docs.xml" path="docs/members[@name='capture-model']/*" />
/// <include file="Output.docs.xml" path="docs/members[@name='out-name-param']/*" />
/// <include file="Output.docs.xml" path="docs/members[@name='capture-call']/*" />
/// <param name="call">The bound scalar query.</param>
public static BoundCall<ScalarKind<T>> CaptureOutput<T, TOut>(
this BoundCall<ScalarKind<T>> call,
out DbCapture<TOut> output,
[CallerArgumentExpression(nameof(output))] string? name = null)
{
CaptureSource<TOut> source = Captures.Output<TOut>(name);
BoundCall<ScalarKind<T>> captured = WithCapture(call, source);
output = new(source);
return captured;
}

/// <summary>
/// Reads one <see cref="decimal"/> output parameter of the given precision
/// and scale when the query runs. A NULL faults the capture;
/// <c>CaptureOutputDecimal</c> reads it as <see langword="null"/>.
/// </summary>
/// <typeparam name="T">The type of the scalar, from the call.</typeparam>
/// <include file="Output.docs.xml" path="docs/members[@name='capture-decimal']/*" />
/// <include file="Output.docs.xml" path="docs/members[@name='out-name-param']/*" />
/// <include file="Output.docs.xml" path="docs/members[@name='capture-call']/*" />
/// <param name="call">The bound scalar query.</param>
public static BoundCall<ScalarKind<T>> CaptureOutput<T>(
this BoundCall<ScalarKind<T>> call,
out DbCapture<decimal> value,
byte precision,
byte scale,
[CallerArgumentExpression(nameof(value))] string? name = null)
{
CaptureSource<decimal> source = Captures.Shaped(name, precision, scale);
BoundCall<ScalarKind<T>> captured = WithCapture(call, source);
value = new(source);
return captured;
}

/// <summary>
/// Reads one <see cref="decimal"/> output parameter of the given precision
/// and scale when the query runs.
/// </summary>
/// <typeparam name="T">The type of the scalar, from the call.</typeparam>
/// <include file="Output.docs.xml" path="docs/members[@name='capture-decimal']/*" />
/// <include file="Output.docs.xml" path="docs/members[@name='out-name-param']/*" />
/// <include file="Output.docs.xml" path="docs/members[@name='capture-call']/*" />
/// <param name="call">The bound scalar query.</param>
public static BoundCall<ScalarKind<T>> CaptureOutputDecimal<T>(
this BoundCall<ScalarKind<T>> call,
out DbCapture<decimal?> value,
byte precision,
byte scale,
[CallerArgumentExpression(nameof(value))] string? name = null)
{
CaptureSource<decimal?> source = Captures.Decimal(name, precision, scale);
BoundCall<ScalarKind<T>> captured = WithCapture(call, source);
value = new(source);
return captured;
}

/// <summary>
/// Reads the stored procedure's return code, converted to
/// <typeparamref name="TValue"/>, when the query runs.
/// </summary>
/// <typeparam name="T">The type of the scalar, from the call.</typeparam>
/// <include file="Output.docs.xml" path="docs/members[@name='capture-return']/*" />
/// <include file="Output.docs.xml" path="docs/members[@name='capture-call']/*" />
/// <param name="call">The bound scalar query.</param>
public static BoundCall<ScalarKind<T>> CaptureReturn<T, TValue>(
this BoundCall<ScalarKind<T>> call,
out DbCapture<TValue> returnCode)
{
CaptureSource<TValue> source = Captures.Return<TValue>();
BoundCall<ScalarKind<T>> captured = WithCapture(call, source);
returnCode = new(source);
return captured;
}
}
Loading