Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
30 changes: 30 additions & 0 deletions Source/Core/ExpressiveCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,36 @@ public ExpressiveCommand(
protected override ImmutableArray<string> GetProcedureParameterNames(IDbConnection connection, string procedure)
=> connection is DbConnection db ? ProcedureSchemaNames.Get(db, procedure) : default;

/// <summary>
/// Asynchronously gets the parameter names a stored procedure declares
/// through the ADO.NET schema collection <c>ProcedureParameters</c> when the
/// connection is a <see cref="DbConnection"/>. The lookup is asynchronous
/// on net10.0 and later (<c>GetSchemaAsync</c>). The netstandard targets have
/// no such method: there the lookup blocks the calling thread and returns a
/// task that is already complete.
/// </summary>
/// <param name="connection">An open connection to run the lookup on.</param>
/// <param name="procedure">The procedure name as given to the command.</param>
/// <param name="cancellationToken">
/// On net10.0 and later the token is passed to <c>GetSchemaAsync</c> and can
/// cancel the lookup; on the netstandard targets it is only checked before
/// the lookup starts.
/// </param>
/// <exception cref="OperationCanceledException">
/// <paramref name="cancellationToken"/> is canceled.
/// </exception>
/// <inheritdoc
/// cref="GetProcedureParameterNames(IDbConnection, string)"
/// path="/*[not(self::param) and not(self::summary)]"/>
protected override ValueTask<ImmutableArray<string>>
GetProcedureParameterNamesAsync(
IDbConnection connection,
string procedure,
CancellationToken cancellationToken)
=> connection is DbConnection db
? ProcedureSchemaNames.GetAsync(db, procedure, cancellationToken)
: default;

/// <inheritdoc />
protected override void AddParams(IDbCommand command)
{
Expand Down
1 change: 1 addition & 0 deletions Source/Core/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ Open.Database.Extensions.MappedQueryWithTake<T>.WithOptions(Open.Database.Extens
Open.Database.Extensions.MappedCommand<T>.WithOptions(Open.Database.Extensions.QueryOptions options) -> Open.Database.Extensions.MappedCommand<T>
Open.Database.Extensions.MappedDbQuery<T>.WithOptions(Open.Database.Extensions.QueryOptions options) -> Open.Database.Extensions.MappedDbQuery<T>!
Open.Database.Extensions.MappedDbQuery<TParams, T>.WithOptions(Open.Database.Extensions.QueryOptions options) -> Open.Database.Extensions.MappedDbQuery<TParams, T>!
override Open.Database.Extensions.ExpressiveCommand.GetProcedureParameterNamesAsync(System.Data.IDbConnection! connection, string! procedure, System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.ValueTask<System.Collections.Immutable.ImmutableArray<string!>>
Open.Database.Extensions.OutputDbNonQuery<TOut>
Open.Database.Extensions.OutputDbNonQuery<TOut>.AddParam(string! name, string? value) -> Open.Database.Extensions.PendingCall<Open.Database.Extensions.OutputKind<TOut>>
Open.Database.Extensions.OutputDbNonQuery<TOut>.AddParam<TValue>(string! name, TValue value) -> Open.Database.Extensions.PendingCall<Open.Database.Extensions.OutputKind<TOut>>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ sealed class SchemaConnection(DataTable? table, Exception? failure) : DbConnecti
{
public DerivedParameterNameTests.RecordingDbCommand Command { get; } = new();
public int SchemaCalls { get; private set; }
public int AsyncSchemaCalls { get; private set; }
public CancellationToken SchemaToken { get; private set; }
public string? Collection { get; private set; }
public string?[]? Restrictions { get; private set; }

Expand All @@ -40,6 +42,20 @@ public override DataTable GetSchema(string collectionName, string?[] restriction
if (failure is not null) throw failure;
return table ?? throw new NotSupportedException();
}

// The default GetSchemaAsync may call GetSchema, so SchemaCalls can count both.
public override Task<DataTable> GetSchemaAsync(
string collectionName,
string?[] restrictionValues,
CancellationToken cancellationToken)
{
AsyncSchemaCalls++;
SchemaToken = cancellationToken;
return base.GetSchemaAsync(
collectionName,
restrictionValues,
cancellationToken);
}
}

// The columns of INFORMATION_SCHEMA.PARAMETERS that matter: a row per parameter, ordinal 0 is the return value.
Expand Down Expand Up @@ -318,6 +334,87 @@ public static void ExpressiveCommand_OverADbConnection_LooksTheNamesUpToo()
Assert.Equal(1, connection.SchemaCalls);
}

[Fact]
public static async Task ExpressiveCommand_OverADbConnection_LooksTheNamesUpAsynchronously_OnTheAsyncPath()
{
const int orderId = 5;
var connection = new SchemaConnection(Table(Row(1, "@order_id")), null);
IDbConnection asInterface = connection;

connection.Command.Rows = new DataTable();

await new ExpressiveCommand(asInterface, CommandType.StoredProcedure, Proc())
.AddParam(orderId)
.ExecuteReaderAsync(
static _ => { },
TestContext.Current.CancellationToken);

Assert.Equal(["@order_id"], Sent(connection));
Assert.Equal(1, connection.AsyncSchemaCalls);
}

[Fact]
public static void ExpressiveCommand_OverADbConnection_LooksTheNamesUpSynchronously_OnTheSyncPath()
{
const int orderId = 5;
var connection = new SchemaConnection(Table(Row(1, "@order_id")), null);
IDbConnection asInterface = connection;

new ExpressiveCommand(asInterface, CommandType.StoredProcedure, Proc())
.AddParam(orderId)
.ExecuteNonQuery();

Assert.Equal(["@order_id"], Sent(connection));
Assert.Equal(1, connection.SchemaCalls);
Assert.Equal(0, connection.AsyncSchemaCalls);
}

// Exposes the protected lookup, to call it without a run around it.
sealed class LookupCommand()
: ExpressiveCommand(
new ConnectionProbe(ConnectionState.Open).Connection,
CommandType.StoredProcedure,
"dbo.p")
{
public ValueTask<ImmutableArray<string>> NamesAsync(
IDbConnection connection,
string procedure,
CancellationToken cancellationToken)
=> GetProcedureParameterNamesAsync(
connection,
procedure,
cancellationToken);
}

[Fact]
public static async Task ExpressiveCommand_AsyncLookup_OverAConnectionThatIsNotADbConnection_GivesNoNames()
{
var probe = new ConnectionProbe(ConnectionState.Open);

ImmutableArray<string> names = await new LookupCommand().NamesAsync(
probe.Connection,
Proc(),
TestContext.Current.CancellationToken);

Assert.True(names.IsDefault);
Assert.Empty(probe.Calls);
}

[Fact]
public static async Task ExpressiveCommand_AsyncLookup_PassesTheCallersTokenToGetSchemaAsync()
{
var connection = new SchemaConnection(Table(Row(1, "@order_id")), null);
using var source = new CancellationTokenSource();

ImmutableArray<string> names = await new LookupCommand().NamesAsync(
connection,
Proc(),
source.Token);

Assert.Equal(["@order_id"], names);
Assert.Equal(source.Token, connection.SchemaToken);
}

[Fact]
public static void ExpressiveCommand_OverAConnectionThatIsNotADbConnection_UsesTheNameAsWritten()
{
Expand Down