From 78f09760af2d042bf31f1bcffd67eb52c9972bf6 Mon Sep 17 00:00:00 2001 From: Oskar Dudycz Date: Thu, 7 Dec 2023 10:31:11 +0100 Subject: [PATCH] Removed methods executing SQL from the command builder Changed order of the extension methods parameters to extend connection, instead of builder. That should allow next extensions for data source. It'll be more clear to see responsibilities. --- src/Weasel.Core/CommandBuilderBase.cs | 207 ------------------ src/Weasel.Core/DbCommandBuilder.cs | 94 +++++--- src/Weasel.Core/SchemaMigration.cs | 2 +- .../CommandBuilderIntegrationTests.cs | 11 +- .../IntegrationContext.cs | 2 +- src/Weasel.Postgresql/CommandBuilder.cs | 87 +++++--- src/Weasel.Postgresql/Functions/Function.cs | 2 +- .../SchemaObjectsExtensions.cs | 4 +- src/Weasel.Postgresql/Sequence.cs | 2 +- .../Tables/Table.FetchExisting.cs | 2 +- .../CommandBuilderIntegrationTests.cs | 16 +- src/Weasel.SqlServer/CommandBuilder.cs | 92 ++++++-- src/Weasel.SqlServer/Functions/Function.cs | 2 +- .../Procedures/StoredProcedure.cs | 2 +- .../SchemaObjectsExtensions.cs | 4 +- src/Weasel.SqlServer/Sequence.cs | 2 +- .../Tables/Table.FetchExisting.cs | 2 +- src/Weasel.SqlServer/Tables/TableType.cs | 2 +- 18 files changed, 230 insertions(+), 305 deletions(-) diff --git a/src/Weasel.Core/CommandBuilderBase.cs b/src/Weasel.Core/CommandBuilderBase.cs index 3d654d4b..e70dc413 100644 --- a/src/Weasel.Core/CommandBuilderBase.cs +++ b/src/Weasel.Core/CommandBuilderBase.cs @@ -4,99 +4,6 @@ namespace Weasel.Core; -public static class CommandBuilderExtensions -{ - /// - /// Compile and execute the batched command against the user supplied connection - /// - /// - /// - /// - /// - public static Task ExecuteNonQueryAsync( - this ICommandBuilder commandBuilder, - TConnection conn, - CancellationToken cancellation = default, - TTransaction? tx = null - ) - where TCommand : DbCommand - where TConnection : DbConnection - where TTransaction : DbTransaction - { - - var cmd = commandBuilder.Compile(); - - cmd.Connection = conn; - cmd.Transaction = tx; - - return cmd.ExecuteNonQueryAsync(cancellation); - } - - - /// - /// Compile and execute the command against the user supplied connection and - /// return a data reader for the results - /// - /// - /// - /// - /// - public static async Task ExecuteReaderAsync( - this ICommandBuilder commandBuilder, - TConnection conn, - CancellationToken cancellation = default, - TTransaction? tx = null - ) - where TCommand : DbCommand - where TConnection : DbConnection - where TTransaction : DbTransaction - where TDataReader : DbDataReader - { - var cmd = commandBuilder.Compile(); - - cmd.Connection = conn; - cmd.Transaction = tx; - - return (TDataReader)await cmd.ExecuteReaderAsync(cancellation).ConfigureAwait(false); - } - - /// - /// Compile and execute the query and returns the results transformed from the raw database reader - /// - /// - /// - /// - /// - /// - /// - public static async Task> FetchListAsync( - this ICommandBuilder commandBuilder, - TConnection conn, - Func> transform, - CancellationToken ct = default, - TTransaction? tx = null - ) - where TCommand : DbCommand - where TConnection : DbConnection - where TTransaction : DbTransaction - { - var cmd = commandBuilder.Compile(); - - cmd.Connection = conn; - cmd.Transaction = tx; - - var list = new List(); - - await using var reader = await cmd.ExecuteReaderAsync(ct).ConfigureAwait(false); - while (await reader.ReadAsync(ct).ConfigureAwait(false)) - { - list.Add(await transform(reader, ct).ConfigureAwait(false)); - } - - return list; - } -} - public interface ICommandBuilder where TCommand : DbCommand { @@ -185,120 +92,6 @@ public TCommand Compile() return _command; } - /// - /// Compile and execute the batched command against the user supplied connection - /// - /// - /// - public Task ExecuteNonQueryAsync(CancellationToken cancellation = default) - { - var cmd = Compile(); - - return cmd.ExecuteNonQueryAsync(cancellation); - } - - /// - /// Compile and execute the batched command against the user supplied connection - /// - /// - /// - /// - protected Task ExecuteNonQueryAsync(Action define, CancellationToken cancellation = default) - { - var cmd = Compile(); - define(cmd); - - return cmd.ExecuteNonQueryAsync(cancellation); - } - - /// - /// Compile and execute the command against the user supplied connection and - /// return a data reader for the results - /// - /// - /// - /// - /// - public Task ExecuteReaderAsync(CancellationToken cancellation = default) - { - var cmd = Compile(); - - return cmd.ExecuteReaderAsync(cancellation); - } - - /// - /// Compile and execute the command against the user supplied connection and - /// return a data reader for the results - /// - /// - /// - /// - /// - protected Task ExecuteReaderAsync(Action define, CancellationToken cancellation = default) - { - var cmd = Compile(); - define(cmd); - - return cmd.ExecuteReaderAsync(cancellation); - } - - /// - /// Compile and execute the query and returns the results transformed from the raw database reader - /// - /// - /// - /// - /// - /// - /// - public async Task> FetchListAsync( - Func> transform, - CancellationToken ct = default - ) - { - var cmd = Compile(); - - var list = new List(); - - await using var reader = await cmd.ExecuteReaderAsync(ct).ConfigureAwait(false); - while (await reader.ReadAsync(ct).ConfigureAwait(false)) - { - list.Add(await transform(reader, ct).ConfigureAwait(false)); - } - - return list; - } - - - /// - /// Compile and execute the query and returns the results transformed from the raw database reader - /// - /// - /// - /// - /// - /// - /// - protected async Task> FetchListAsync( - Action define, - Func> transform, - CancellationToken ct = default - ) - { - var cmd = Compile(); - define(cmd); - - var list = new List(); - - await using var reader = await cmd.ExecuteReaderAsync(ct).ConfigureAwait(false); - while (await reader.ReadAsync(ct).ConfigureAwait(false)) - { - list.Add(await transform(reader, ct).ConfigureAwait(false)); - } - - return list; - } - /// /// Adds a parameter to the underlying command, but does NOT add the /// parameter usage to the command text diff --git a/src/Weasel.Core/DbCommandBuilder.cs b/src/Weasel.Core/DbCommandBuilder.cs index 19058a72..77a9caaa 100644 --- a/src/Weasel.Core/DbCommandBuilder.cs +++ b/src/Weasel.Core/DbCommandBuilder.cs @@ -8,8 +8,7 @@ namespace Weasel.Core; /// /// CommandBuilder for generic DbCommand or DbConnection commands /// -public class - DbCommandBuilder: CommandBuilderBase +public class DbCommandBuilder: CommandBuilderBase { public DbCommandBuilder(DbCommand command): base(DbDatabaseProvider.Instance, '@', command) { @@ -25,69 +24,114 @@ public static class DbCommandBuilderExtensions /// /// Compile and execute the batched command against the user supplied connection /// - /// - /// + /// + /// + /// + /// + public static Task ExecuteNonQueryAsync( + this DbConnection connection, + DbCommandBuilder commandBuilder, + CancellationToken ct = default + ) => connection.ExecuteNonQueryAsync(commandBuilder, null, ct); + + /// + /// Compile and execute the batched command against the user supplied connection + /// + /// + /// /// + /// /// public static Task ExecuteNonQueryAsync( - this DbCommandBuilder commandBuilder, - DbConnection conn, - CancellationToken cancellation = default, - DbTransaction? tx = null + this DbConnection connection, + DbCommandBuilder commandBuilder, + DbTransaction? tx, + CancellationToken ct = default ) { var cmd = commandBuilder.Compile(); - cmd.Connection = conn; + cmd.Connection = connection; cmd.Transaction = tx; - return cmd.ExecuteNonQueryAsync(cancellation); + return cmd.ExecuteNonQueryAsync(ct); } + /// + /// Compile and execute the command against the user supplied connection and + /// return a data reader for the results + /// + /// + /// + /// + /// + public static Task ExecuteReaderAsync( + this DbConnection connection, + DbCommandBuilder commandBuilder, + CancellationToken ct = default + ) => connection.ExecuteReaderAsync(commandBuilder, null, ct); /// /// Compile and execute the command against the user supplied connection and /// return a data reader for the results /// - /// - /// + /// + /// /// + /// /// public static Task ExecuteReaderAsync( - this DbCommandBuilder commandBuilder, - DbConnection conn, - CancellationToken cancellation = default, - DbTransaction? tx = null + this DbConnection connection, + DbCommandBuilder commandBuilder, + DbTransaction? tx, + CancellationToken ct = default ) { var cmd = commandBuilder.Compile(); - cmd.Connection = conn; + cmd.Connection = connection; cmd.Transaction = tx; - return cmd.ExecuteReaderAsync(cancellation); + return cmd.ExecuteReaderAsync(ct); } /// /// Compile and execute the query and returns the results transformed from the raw database reader /// - /// + /// + /// /// - /// /// + /// + /// + /// + public static Task> FetchListAsync( + this DbConnection connection, + DbCommandBuilder commandBuilder, + Func> transform, + CancellationToken ct = default + ) => connection.FetchListAsync(commandBuilder, transform, null, ct); + + /// + /// Compile and execute the query and returns the results transformed from the raw database reader + /// + /// + /// + /// + /// /// /// public static async Task> FetchListAsync( - this DbCommandBuilder commandBuilder, - DbConnection conn, + this DbConnection connection, + DbCommandBuilder commandBuilder, Func> transform, - CancellationToken ct = default, - DbTransaction? tx = null + DbTransaction? tx, + CancellationToken ct = default ) { var cmd = commandBuilder.Compile(); - cmd.Connection = conn; + cmd.Connection = connection; cmd.Transaction = tx; var list = new List(); diff --git a/src/Weasel.Core/SchemaMigration.cs b/src/Weasel.Core/SchemaMigration.cs index 7e5da3a5..dfe40647 100644 --- a/src/Weasel.Core/SchemaMigration.cs +++ b/src/Weasel.Core/SchemaMigration.cs @@ -79,7 +79,7 @@ params ISchemaObject[] schemaObjects foreach (var schemaObject in schemaObjects) schemaObject.ConfigureQueryCommand(builder); - await using var reader = await builder.ExecuteReaderAsync(conn, ct).ConfigureAwait(false); + await using var reader = await conn.ExecuteReaderAsync(builder, ct).ConfigureAwait(false); deltas.Add(await schemaObjects[0].CreateDeltaAsync(reader, ct).ConfigureAwait(false)); diff --git a/src/Weasel.Postgresql.Tests/CommandBuilderIntegrationTests.cs b/src/Weasel.Postgresql.Tests/CommandBuilderIntegrationTests.cs index 72654453..95fe1a6f 100644 --- a/src/Weasel.Postgresql.Tests/CommandBuilderIntegrationTests.cs +++ b/src/Weasel.Postgresql.Tests/CommandBuilderIntegrationTests.cs @@ -28,7 +28,7 @@ public async Task use_parameters_to_query_by_anonymous_type() builder.Append("insert into integration.thing (id, tag, age) values (:id, :tag, :age)"); builder.AddParameters(new { id = 3, tag = "Toodles", age = 5 }); - await builder.ExecuteNonQueryAsync(theConnection); + await theConnection.ExecuteNonQueryAsync(builder); await using var reader = await theConnection.CreateCommand("select id, tag, age from integration.thing") .ExecuteReaderAsync(); @@ -56,7 +56,7 @@ public async Task use_parameters_to_query_by_anonymous_type_by_generic_command_b builder.Append("insert into integration.thing (id, tag, age) values (:id, :tag, :age)"); builder.AddParameters(new { id = 3, tag = "Toodles", age = 5 }); - await builder.ExecuteNonQueryAsync(theConnection); + await theConnection.ExecuteNonQueryAsync(builder); await using var reader = await theConnection.CreateCommand("select id, tag, age from integration.thing") .ExecuteReaderAsync(); @@ -98,11 +98,12 @@ await theConnection.CreateCommand("insert into integration.thing (id, tag) value var builder = new CommandBuilder(); builder.Append("select id, tag from integration.thing order by id"); - var things = await builder.FetchListAsync(theConnection, async (r, ct) => + var things = await theConnection.FetchListAsync(builder, async (r, ct) => { var thing = new Thing { - id = await r.GetFieldValueAsync(0), tag = await r.GetFieldValueAsync(1) + id = await r.GetFieldValueAsync(0, ct), + tag = await r.GetFieldValueAsync(1, ct) }; return thing; @@ -147,7 +148,7 @@ public async Task add_named_parameter() builder.AddNamedParameter("done", true); - await builder.ExecuteNonQueryAsync(theConnection); + await theConnection.ExecuteNonQueryAsync(builder); await using var reader = await theConnection .CreateCommand("select id, tag, age, rate, sequence, is_done from integration.thing") diff --git a/src/Weasel.Postgresql.Tests/IntegrationContext.cs b/src/Weasel.Postgresql.Tests/IntegrationContext.cs index afa5037d..2bfeaa17 100644 --- a/src/Weasel.Postgresql.Tests/IntegrationContext.cs +++ b/src/Weasel.Postgresql.Tests/IntegrationContext.cs @@ -46,7 +46,7 @@ protected async Task CreateSchemaObjectInDatabase(ISchemaObject schemaObject) var rules = new PostgresqlMigrator(); var builder = new DbCommandBuilder(theConnection); schemaObject.ConfigureQueryCommand(builder); - await using var reader = await builder.ExecuteReaderAsync(theConnection); + await using var reader = await theConnection.ExecuteReaderAsync(builder); var schemaMigration = new SchemaMigration(await schemaObject.CreateDeltaAsync(reader)); await reader.CloseAsync(); diff --git a/src/Weasel.Postgresql/CommandBuilder.cs b/src/Weasel.Postgresql/CommandBuilder.cs index fa174e17..a7a565ee 100644 --- a/src/Weasel.Postgresql/CommandBuilder.cs +++ b/src/Weasel.Postgresql/CommandBuilder.cs @@ -33,72 +33,109 @@ public void AppendParameter(string[] values) public static class CommandBuilderExtensions { + + /// + /// Compile and execute the command against the user supplied connection + /// + /// + /// + /// + /// + public static Task ExecuteNonQueryAsync( + this NpgsqlConnection connection, + CommandBuilder commandBuilder, + CancellationToken ct = default + ) => connection.ExecuteNonQueryAsync(commandBuilder, null, ct); + /// - /// Compile and execute the batched command against the user supplied connection + /// Compile and execute the command against the user supplied connection /// - /// - /// + /// + /// /// + /// /// public static Task ExecuteNonQueryAsync( - this CommandBuilder commandBuilder, - NpgsqlConnection conn, - CancellationToken cancellation = default, - NpgsqlTransaction? tx = null + this NpgsqlConnection connection, + CommandBuilder commandBuilder, + NpgsqlTransaction? tx, + CancellationToken ct = default ) { var cmd = commandBuilder.Compile(); - cmd.Connection = conn; + cmd.Connection = connection; cmd.Transaction = tx; - return cmd.ExecuteNonQueryAsync(cancellation); + return cmd.ExecuteNonQueryAsync(ct); } + /// + /// Compile and execute the command against the user supplied connection and + /// return a data reader for the results + /// + /// + /// + /// + /// + public static Task ExecuteReaderAsync( + this NpgsqlConnection connection, + CommandBuilder commandBuilder, + CancellationToken ct = default + ) => connection.ExecuteReaderAsync(commandBuilder, null, ct); /// /// Compile and execute the command against the user supplied connection and /// return a data reader for the results /// - /// - /// + /// + /// /// + /// /// - public static async Task ExecuteReaderAsync( - this CommandBuilder commandBuilder, - NpgsqlConnection conn, - CancellationToken cancellation = default, - NpgsqlTransaction? tx = null + public static Task ExecuteReaderAsync( + this NpgsqlConnection connection, + CommandBuilder commandBuilder, + NpgsqlTransaction? tx, + CancellationToken ct = default ) { var cmd = commandBuilder.Compile(); - cmd.Connection = conn; + cmd.Connection = connection; cmd.Transaction = tx; - return (NpgsqlDataReader)await cmd.ExecuteReaderAsync(cancellation).ConfigureAwait(false); + return cmd.ExecuteReaderAsync(ct); } + public static Task> FetchListAsync( + this NpgsqlConnection connection, + CommandBuilder commandBuilder, + Func> transform, + CancellationToken ct = default + ) => connection.FetchListAsync(commandBuilder, transform, null, ct); + /// /// Compile and execute the query and returns the results transformed from the raw database reader /// - /// + /// + /// /// - /// /// + /// /// /// public static async Task> FetchListAsync( - this CommandBuilder commandBuilder, - NpgsqlConnection conn, + this NpgsqlConnection connection, + CommandBuilder commandBuilder, Func> transform, - CancellationToken ct = default, - NpgsqlTransaction? tx = null + NpgsqlTransaction? tx, + CancellationToken ct = default ) { var cmd = commandBuilder.Compile(); - cmd.Connection = conn; + cmd.Connection = connection; cmd.Transaction = tx; var list = new List(); diff --git a/src/Weasel.Postgresql/Functions/Function.cs b/src/Weasel.Postgresql/Functions/Function.cs index 93cc1864..cc918e39 100644 --- a/src/Weasel.Postgresql/Functions/Function.cs +++ b/src/Weasel.Postgresql/Functions/Function.cs @@ -108,7 +108,7 @@ public static DbObjectName ParseIdentifier(string functionSql) ConfigureQueryCommand(builder); - await using var reader = await builder.ExecuteReaderAsync(conn, ct).ConfigureAwait(false); + await using var reader = await conn.ExecuteReaderAsync(builder, ct).ConfigureAwait(false); return await readExistingAsync(reader, ct).ConfigureAwait(false); } diff --git a/src/Weasel.Postgresql/SchemaObjectsExtensions.cs b/src/Weasel.Postgresql/SchemaObjectsExtensions.cs index 32883e20..c3cad409 100644 --- a/src/Weasel.Postgresql/SchemaObjectsExtensions.cs +++ b/src/Weasel.Postgresql/SchemaObjectsExtensions.cs @@ -166,7 +166,7 @@ public static async Task> ExistingTablesAsync( builder.Append(";"); - return await builder.FetchListAsync(conn, ReadDbObjectNameAsync, ct).ConfigureAwait(false); + return await conn.FetchListAsync(builder, ReadDbObjectNameAsync, ct: ct).ConfigureAwait(false); } public static async Task> ExistingFunctionsAsync( @@ -194,7 +194,7 @@ public static async Task> ExistingFunctionsAsync( builder.Append(";"); - return await builder.FetchListAsync(conn, ReadDbObjectNameAsync, ct).ConfigureAwait(false); + return await conn.FetchListAsync(builder, ReadDbObjectNameAsync, ct: ct).ConfigureAwait(false); } private static async Task ReadDbObjectNameAsync(DbDataReader reader, CancellationToken ct = default) diff --git a/src/Weasel.Postgresql/Sequence.cs b/src/Weasel.Postgresql/Sequence.cs index 47a3f823..0172c9d8 100644 --- a/src/Weasel.Postgresql/Sequence.cs +++ b/src/Weasel.Postgresql/Sequence.cs @@ -75,7 +75,7 @@ public async Task FindDeltaAsync(NpgsqlConnection conn, Canc ConfigureQueryCommand(builder); - await using var reader = await builder.ExecuteReaderAsync(conn, ct).ConfigureAwait(false); + await using var reader = await conn.ExecuteReaderAsync(builder, ct).ConfigureAwait(false); return await CreateDeltaAsync(reader, ct).ConfigureAwait(false); } diff --git a/src/Weasel.Postgresql/Tables/Table.FetchExisting.cs b/src/Weasel.Postgresql/Tables/Table.FetchExisting.cs index 0dab1f2c..ebf5b7ec 100644 --- a/src/Weasel.Postgresql/Tables/Table.FetchExisting.cs +++ b/src/Weasel.Postgresql/Tables/Table.FetchExisting.cs @@ -121,7 +121,7 @@ information_schema.columns col ConfigureQueryCommand(builder); - await using var reader = await builder.ExecuteReaderAsync(conn, ct).ConfigureAwait(false); + await using var reader = await conn.ExecuteReaderAsync(builder, ct).ConfigureAwait(false); return await readExistingAsync(reader, ct).ConfigureAwait(false); } diff --git a/src/Weasel.SqlServer.Tests/CommandBuilderIntegrationTests.cs b/src/Weasel.SqlServer.Tests/CommandBuilderIntegrationTests.cs index 96e572fe..dfcae949 100644 --- a/src/Weasel.SqlServer.Tests/CommandBuilderIntegrationTests.cs +++ b/src/Weasel.SqlServer.Tests/CommandBuilderIntegrationTests.cs @@ -27,7 +27,7 @@ public async Task use_parameters_to_query_by_anonymous_type() builder.Append("insert into integration.thing (id, tag, age) values (@id, @tag, @age)"); builder.AddParameters(new { id = 3, tag = "Toodles", age = 5 }); - await builder.ExecuteNonQueryAsync(theConnection); + await theConnection.ExecuteNonQueryAsync(builder); await using var reader = await theConnection.CreateCommand("select id, tag, age from integration.thing") .ExecuteReaderAsync(); @@ -55,7 +55,7 @@ public async Task use_parameters_to_query_by_anonymous_type_generic_db_builder() builder.Append("insert into integration.thing (id, tag, age) values (@id, @tag, @age)"); builder.AddParameters(new { id = 3, tag = "Toodles", age = 5 }); - await builder.ExecuteNonQueryAsync(theConnection); + await theConnection.ExecuteNonQueryAsync(builder); await using var reader = await theConnection.CreateCommand("select id, tag, age from integration.thing") .ExecuteReaderAsync(); @@ -97,11 +97,12 @@ await theConnection.CreateCommand("insert into integration.thing (id, tag) value var builder = new CommandBuilder(); builder.Append("select id, tag from integration.thing order by id"); - var things = await builder.FetchListAsync(theConnection, async (r, ct) => + var things = await theConnection.FetchListAsync(builder, async (r, ct) => { var thing = new Thing { - id = await r.GetFieldValueAsync(0), tag = await r.GetFieldValueAsync(1) + id = await r.GetFieldValueAsync(0, ct), + tag = await r.GetFieldValueAsync(1, ct) }; return thing; @@ -144,11 +145,12 @@ await theConnection.CreateCommand("insert into integration.thing (id, tag) value var builder = new DbCommandBuilder(theConnection); builder.Append("select id, tag from integration.thing order by id"); - var things = await builder.FetchListAsync(theConnection, async (r, ct) => + var things = await theConnection.FetchListAsync(builder, async (r, ct) => { var thing = new Thing { - id = await r.GetFieldValueAsync(0), tag = await r.GetFieldValueAsync(1) + id = await r.GetFieldValueAsync(0, ct), + tag = await r.GetFieldValueAsync(1, ct) }; return thing; @@ -193,7 +195,7 @@ public async Task add_named_parameter() builder.AddNamedParameter("done", true); - await builder.ExecuteNonQueryAsync(theConnection); + await theConnection.ExecuteNonQueryAsync(builder); await using var reader = await theConnection .CreateCommand("select id, tag, age, rate, sequence, is_done from integration.thing") diff --git a/src/Weasel.SqlServer/CommandBuilder.cs b/src/Weasel.SqlServer/CommandBuilder.cs index 8afe2c4b..91c50979 100644 --- a/src/Weasel.SqlServer/CommandBuilder.cs +++ b/src/Weasel.SqlServer/CommandBuilder.cs @@ -18,72 +18,120 @@ public CommandBuilder(SqlCommand command): base(SqlServerProvider.Instance, ':', public static class CommandBuilderExtensions { + + /// + /// Compile and execute the batched command against the user supplied connection + /// + /// + /// + /// + public static Task ExecuteNonQueryAsync( + this SqlConnection connection, + CommandBuilder commandBuilder, + CancellationToken ct = default + ) => connection.ExecuteNonQueryAsync(commandBuilder, null, ct); + /// /// Compile and execute the batched command against the user supplied connection /// - /// - /// + /// + /// + /// /// /// public static Task ExecuteNonQueryAsync( - this CommandBuilder commandBuilder, - SqlConnection conn, - CancellationToken cancellation = default, - SqlTransaction? tx = null + this SqlConnection connection, + CommandBuilder commandBuilder, + SqlTransaction? tx, + CancellationToken ct = default ) { var cmd = commandBuilder.Compile(); - cmd.Connection = conn; + cmd.Connection = connection; cmd.Transaction = tx; - return cmd.ExecuteNonQueryAsync(cancellation); + return cmd.ExecuteNonQueryAsync(ct); } + /// + /// Compile and execute the command against the user supplied connection and + /// return a data reader for the results + /// + /// + /// + /// + /// + public static Task ExecuteReaderAsync( + this SqlConnection connection, + CommandBuilder commandBuilder, + CancellationToken ct = default + ) => connection.ExecuteReaderAsync(commandBuilder, null, ct); + /// /// Compile and execute the command against the user supplied connection and /// return a data reader for the results /// - /// - /// + /// + /// /// + /// /// public static async Task ExecuteReaderAsync( - this CommandBuilder commandBuilder, - SqlConnection conn, - CancellationToken cancellation = default, - SqlTransaction? tx = null + this SqlConnection connection, + CommandBuilder commandBuilder, + SqlTransaction? tx, + CancellationToken ct = default ) { var cmd = commandBuilder.Compile(); - cmd.Connection = conn; + cmd.Connection = connection; cmd.Transaction = tx; - return (SqlDataReader)await cmd.ExecuteReaderAsync(cancellation).ConfigureAwait(false); + return (SqlDataReader)await cmd.ExecuteReaderAsync(ct).ConfigureAwait(false); } + + /// + /// Compile and execute the query and returns the results transformed from the raw database reader + /// + /// + /// + /// + /// + /// + /// + /// + public static Task> FetchListAsync( + this SqlConnection connection, + CommandBuilder commandBuilder, + Func> transform, + CancellationToken ct = default + ) => connection.FetchListAsync(commandBuilder, transform, null, ct); + /// /// Compile and execute the query and returns the results transformed from the raw database reader /// - /// + /// + /// /// /// /// /// /// public static async Task> FetchListAsync( - this CommandBuilder commandBuilder, - SqlConnection conn, + this SqlConnection connection, + CommandBuilder commandBuilder, Func> transform, - CancellationToken ct = default, - SqlTransaction? tx = null + SqlTransaction? tx, + CancellationToken ct = default ) { var cmd = commandBuilder.Compile(); - cmd.Connection = conn; + cmd.Connection = connection; cmd.Transaction = tx; var list = new List(); diff --git a/src/Weasel.SqlServer/Functions/Function.cs b/src/Weasel.SqlServer/Functions/Function.cs index a0e567c5..18c7700d 100644 --- a/src/Weasel.SqlServer/Functions/Function.cs +++ b/src/Weasel.SqlServer/Functions/Function.cs @@ -77,7 +77,7 @@ public static DbObjectName ParseIdentifier(string functionSql) ConfigureQueryCommand(builder); - await using var reader = await builder.ExecuteReaderAsync(conn, ct).ConfigureAwait(false); + await using var reader = await conn.ExecuteReaderAsync(builder, ct).ConfigureAwait(false); return await readExistingAsync(reader, ct).ConfigureAwait(false); } diff --git a/src/Weasel.SqlServer/Procedures/StoredProcedure.cs b/src/Weasel.SqlServer/Procedures/StoredProcedure.cs index b3484ac9..5c4b94d4 100644 --- a/src/Weasel.SqlServer/Procedures/StoredProcedure.cs +++ b/src/Weasel.SqlServer/Procedures/StoredProcedure.cs @@ -122,7 +122,7 @@ public string CanonicizeSql() ConfigureQueryCommand(builder); - await using var reader = await builder.ExecuteReaderAsync(conn, ct).ConfigureAwait(false); + await using var reader = await conn.ExecuteReaderAsync(builder, ct).ConfigureAwait(false); return await readExistingAsync(reader, ct).ConfigureAwait(false); } diff --git a/src/Weasel.SqlServer/SchemaObjectsExtensions.cs b/src/Weasel.SqlServer/SchemaObjectsExtensions.cs index 1a27a7fb..afe146d4 100644 --- a/src/Weasel.SqlServer/SchemaObjectsExtensions.cs +++ b/src/Weasel.SqlServer/SchemaObjectsExtensions.cs @@ -202,7 +202,7 @@ public static async Task> ExistingTables( builder.Append(";"); - return await builder.FetchListAsync(conn, ReadDbObjectNameAsync, ct).ConfigureAwait(false); + return await conn.FetchListAsync(builder, ReadDbObjectNameAsync, ct).ConfigureAwait(false); } public static async Task> ExistingFunctionsAsync( @@ -230,7 +230,7 @@ public static async Task> ExistingFunctionsAsync( builder.Append(";"); - return await builder.FetchListAsync(conn, ReadDbObjectNameAsync, ct).ConfigureAwait(false); + return await conn.FetchListAsync(builder, ReadDbObjectNameAsync, ct).ConfigureAwait(false); } private static async Task ReadDbObjectNameAsync(DbDataReader reader, CancellationToken ct = default) diff --git a/src/Weasel.SqlServer/Sequence.cs b/src/Weasel.SqlServer/Sequence.cs index 5544505f..29f3a93b 100644 --- a/src/Weasel.SqlServer/Sequence.cs +++ b/src/Weasel.SqlServer/Sequence.cs @@ -77,7 +77,7 @@ public async Task FindDeltaAsync(SqlConnection conn, Cancell ConfigureQueryCommand(builder); - await using var reader = await builder.ExecuteReaderAsync(conn, ct).ConfigureAwait(false); + await using var reader = await conn.ExecuteReaderAsync(builder, ct).ConfigureAwait(false); return await CreateDeltaAsync(reader, ct).ConfigureAwait(false); } diff --git a/src/Weasel.SqlServer/Tables/Table.FetchExisting.cs b/src/Weasel.SqlServer/Tables/Table.FetchExisting.cs index e23fca01..a45749f5 100644 --- a/src/Weasel.SqlServer/Tables/Table.FetchExisting.cs +++ b/src/Weasel.SqlServer/Tables/Table.FetchExisting.cs @@ -97,7 +97,7 @@ order by ConfigureQueryCommand(builder); - await using var reader = await builder.ExecuteReaderAsync(conn, ct).ConfigureAwait(false); + await using var reader = await conn.ExecuteReaderAsync(builder, ct).ConfigureAwait(false); return await readExistingAsync(reader, ct).ConfigureAwait(false); } diff --git a/src/Weasel.SqlServer/Tables/TableType.cs b/src/Weasel.SqlServer/Tables/TableType.cs index 20276a6e..1b74ebeb 100644 --- a/src/Weasel.SqlServer/Tables/TableType.cs +++ b/src/Weasel.SqlServer/Tables/TableType.cs @@ -91,7 +91,7 @@ public ITableTypeColumn AddColumn(string columnName, string databaseType) ConfigureQueryCommand(builder); - await using var reader = await builder.ExecuteReaderAsync(conn, ct).ConfigureAwait(false); + await using var reader = await conn.ExecuteReaderAsync(builder, ct).ConfigureAwait(false); return await readExistingAsync(reader, ct).ConfigureAwait(false); }