diff --git a/Source/Core/ExpressiveCommand.cs b/Source/Core/ExpressiveCommand.cs index 88034c81..b0ef851d 100644 --- a/Source/Core/ExpressiveCommand.cs +++ b/Source/Core/ExpressiveCommand.cs @@ -125,6 +125,36 @@ public ExpressiveCommand( protected override ImmutableArray GetProcedureParameterNames(IDbConnection connection, string procedure) => connection is DbConnection db ? ProcedureSchemaNames.Get(db, procedure) : default; + /// + /// Asynchronously gets the parameter names a stored procedure declares + /// through the ADO.NET schema collection ProcedureParameters when the + /// connection is a . The lookup is asynchronous + /// on net10.0 and later (GetSchemaAsync). The netstandard targets have + /// no such method: there the lookup blocks the calling thread and returns a + /// task that is already complete. + /// + /// An open connection to run the lookup on. + /// The procedure name as given to the command. + /// + /// On net10.0 and later the token is passed to GetSchemaAsync and can + /// cancel the lookup; on the netstandard targets it is only checked before + /// the lookup starts. + /// + /// + /// is canceled. + /// + /// + protected override ValueTask> + GetProcedureParameterNamesAsync( + IDbConnection connection, + string procedure, + CancellationToken cancellationToken) + => connection is DbConnection db + ? ProcedureSchemaNames.GetAsync(db, procedure, cancellationToken) + : default; + /// protected override void AddParams(IDbCommand command) { diff --git a/Source/Core/PublicAPI.Unshipped.txt b/Source/Core/PublicAPI.Unshipped.txt index 11d05c30..ff19961a 100644 --- a/Source/Core/PublicAPI.Unshipped.txt +++ b/Source/Core/PublicAPI.Unshipped.txt @@ -112,6 +112,7 @@ Open.Database.Extensions.MappedQueryWithTake.WithOptions(Open.Database.Extens Open.Database.Extensions.MappedCommand.WithOptions(Open.Database.Extensions.QueryOptions options) -> Open.Database.Extensions.MappedCommand Open.Database.Extensions.MappedDbQuery.WithOptions(Open.Database.Extensions.QueryOptions options) -> Open.Database.Extensions.MappedDbQuery! Open.Database.Extensions.MappedDbQuery.WithOptions(Open.Database.Extensions.QueryOptions options) -> Open.Database.Extensions.MappedDbQuery! +override Open.Database.Extensions.ExpressiveCommand.GetProcedureParameterNamesAsync(System.Data.IDbConnection! connection, string! procedure, System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.ValueTask> Open.Database.Extensions.OutputDbNonQuery Open.Database.Extensions.OutputDbNonQuery.AddParam(string! name, string? value) -> Open.Database.Extensions.PendingCall> Open.Database.Extensions.OutputDbNonQuery.AddParam(string! name, TValue value) -> Open.Database.Extensions.PendingCall> diff --git a/Tests/Open.Database.Extensions.Core.Tests/ProcedureSchemaNamesTests.cs b/Tests/Open.Database.Extensions.Core.Tests/ProcedureSchemaNamesTests.cs index c546b68d..f1f51121 100644 --- a/Tests/Open.Database.Extensions.Core.Tests/ProcedureSchemaNamesTests.cs +++ b/Tests/Open.Database.Extensions.Core.Tests/ProcedureSchemaNamesTests.cs @@ -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; } @@ -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 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. @@ -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> 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 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 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() {