From 9a07569aa8ffd0ee287a89d649158169c0d63b90 Mon Sep 17 00:00:00 2001 From: Oskar Dudycz Date: Fri, 8 Dec 2023 09:35:55 +0100 Subject: [PATCH] Added base command builder extensions to unify the common command execution usage --- src/Weasel.Core/CommandBuilderBase.cs | 124 ++++++++++++++++++++++++ src/Weasel.Core/DbCommandBuilder.cs | 40 +------- src/Weasel.Postgresql/CommandBuilder.cs | 45 ++------- src/Weasel.SqlServer/CommandBuilder.cs | 42 ++------ 4 files changed, 141 insertions(+), 110 deletions(-) diff --git a/src/Weasel.Core/CommandBuilderBase.cs b/src/Weasel.Core/CommandBuilderBase.cs index e70dc413..e95e698a 100644 --- a/src/Weasel.Core/CommandBuilderBase.cs +++ b/src/Weasel.Core/CommandBuilderBase.cs @@ -328,3 +328,127 @@ public TParameter[] AppendWithParameters(string text, char separator) return parameters; } } + +// Note: Those methods are intentionally not written as extension methods +// as the preference is to use the strongly typed methods of specific databases +// instead of those generic ones +public static class CommandBuilderExtensions +{ + /// + /// Compile and execute the command against the user supplied connection + /// + /// + /// + /// + /// + public static Task ExecuteNonQueryAsync( + DbConnection connection, + ICommandBuilder commandBuilder, + CancellationToken ct = default + ) where T : DbCommand => + ExecuteNonQueryAsync(connection, commandBuilder, null, ct); + + /// + /// Compile and execute the command against the user supplied connection + /// + /// + /// + /// + /// + /// + public static Task ExecuteNonQueryAsync( + DbConnection connection, + ICommandBuilder commandBuilder, + DbTransaction? tx, + CancellationToken ct = default + ) where T : DbCommand + { + var cmd = commandBuilder.Compile(); + + cmd.Connection = connection; + cmd.Transaction = tx; + + 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( + DbConnection connection, + ICommandBuilder commandBuilder, + CancellationToken ct = default + ) where T : DbCommand => + ExecuteReaderAsync(connection, 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( + DbConnection connection, + ICommandBuilder commandBuilder, + DbTransaction? tx, + CancellationToken ct = default + ) where T : DbCommand + { + var cmd = commandBuilder.Compile(); + + cmd.Connection = connection; + cmd.Transaction = tx; + + return cmd.ExecuteReaderAsync(ct); + } + + public static Task> FetchListAsync( + DbConnection connection, + ICommandBuilder commandBuilder, + Func> transform, + CancellationToken ct = default + ) where TCommand : DbCommand => + FetchListAsync(connection, commandBuilder, transform, null, ct); + + /// + /// Compile and execute the query and returns the results transformed from the raw database reader + /// + /// + /// + /// + /// + /// + /// + /// + public static async Task> FetchListAsync( + DbConnection connection, + ICommandBuilder commandBuilder, + Func> transform, + DbTransaction? tx, + CancellationToken ct = default + ) where TCommand : DbCommand + { + var cmd = commandBuilder.Compile(); + + cmd.Connection = connection; + 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; + } +} diff --git a/src/Weasel.Core/DbCommandBuilder.cs b/src/Weasel.Core/DbCommandBuilder.cs index 1a0fec96..505395f7 100644 --- a/src/Weasel.Core/DbCommandBuilder.cs +++ b/src/Weasel.Core/DbCommandBuilder.cs @@ -47,15 +47,7 @@ public static Task ExecuteNonQueryAsync( DbCommandBuilder commandBuilder, DbTransaction? tx, CancellationToken ct = default - ) - { - var cmd = commandBuilder.Compile(); - - cmd.Connection = connection; - cmd.Transaction = tx; - - return cmd.ExecuteNonQueryAsync(ct); - } + ) => CommandBuilderExtensions.ExecuteNonQueryAsync(connection, commandBuilder, tx, ct); /// /// Compile and execute the command against the user supplied connection and @@ -85,15 +77,7 @@ public static Task ExecuteReaderAsync( DbCommandBuilder commandBuilder, DbTransaction? tx, CancellationToken ct = default - ) - { - var cmd = commandBuilder.Compile(); - - cmd.Connection = connection; - cmd.Transaction = tx; - - return cmd.ExecuteReaderAsync(ct); - } + ) => CommandBuilderExtensions.ExecuteReaderAsync(connection, commandBuilder, tx, ct); /// /// Compile and execute the query and returns the results transformed from the raw database reader @@ -121,29 +105,13 @@ public static Task> FetchListAsync( /// /// /// - public static async Task> FetchListAsync( + public static Task> FetchListAsync( this DbConnection connection, DbCommandBuilder commandBuilder, Func> transform, DbTransaction? tx, CancellationToken ct = default - ) - { - var cmd = commandBuilder.Compile(); - - cmd.Connection = connection; - 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; - } + ) => CommandBuilderExtensions.FetchListAsync(connection, commandBuilder, transform, tx, ct); } internal class DbDatabaseProvider: DatabaseProvider diff --git a/src/Weasel.Postgresql/CommandBuilder.cs b/src/Weasel.Postgresql/CommandBuilder.cs index a7a565ee..0c75a719 100644 --- a/src/Weasel.Postgresql/CommandBuilder.cs +++ b/src/Weasel.Postgresql/CommandBuilder.cs @@ -33,7 +33,6 @@ public void AppendParameter(string[] values) public static class CommandBuilderExtensions { - /// /// Compile and execute the command against the user supplied connection /// @@ -60,15 +59,7 @@ public static Task ExecuteNonQueryAsync( CommandBuilder commandBuilder, NpgsqlTransaction? tx, CancellationToken ct = default - ) - { - var cmd = commandBuilder.Compile(); - - cmd.Connection = connection; - cmd.Transaction = tx; - - return cmd.ExecuteNonQueryAsync(ct); - } + ) => Weasel.Core.CommandBuilderExtensions.ExecuteNonQueryAsync(connection, commandBuilder, tx, ct); /// /// Compile and execute the command against the user supplied connection and @@ -93,20 +84,14 @@ public static Task ExecuteReaderAsync( /// /// /// - public static Task ExecuteReaderAsync( + public static async Task ExecuteReaderAsync( this NpgsqlConnection connection, CommandBuilder commandBuilder, NpgsqlTransaction? tx, CancellationToken ct = default - ) - { - var cmd = commandBuilder.Compile(); - - cmd.Connection = connection; - cmd.Transaction = tx; - - return cmd.ExecuteReaderAsync(ct); - } + ) => + (NpgsqlDataReader)await Weasel.Core.CommandBuilderExtensions + .ExecuteReaderAsync(connection, commandBuilder, tx, ct).ConfigureAwait(false); public static Task> FetchListAsync( this NpgsqlConnection connection, @@ -125,27 +110,11 @@ public static Task> FetchListAsync( /// /// /// - public static async Task> FetchListAsync( + public static Task> FetchListAsync( this NpgsqlConnection connection, CommandBuilder commandBuilder, Func> transform, NpgsqlTransaction? tx, CancellationToken ct = default - ) - { - var cmd = commandBuilder.Compile(); - - cmd.Connection = connection; - 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; - } + ) => Weasel.Core.CommandBuilderExtensions.FetchListAsync(connection, commandBuilder, transform, tx, ct); } diff --git a/src/Weasel.SqlServer/CommandBuilder.cs b/src/Weasel.SqlServer/CommandBuilder.cs index 91c50979..cb49c5f0 100644 --- a/src/Weasel.SqlServer/CommandBuilder.cs +++ b/src/Weasel.SqlServer/CommandBuilder.cs @@ -44,15 +44,7 @@ public static Task ExecuteNonQueryAsync( CommandBuilder commandBuilder, SqlTransaction? tx, CancellationToken ct = default - ) - { - var cmd = commandBuilder.Compile(); - - cmd.Connection = connection; - cmd.Transaction = tx; - - return cmd.ExecuteNonQueryAsync(ct); - } + ) => Weasel.Core.CommandBuilderExtensions.ExecuteNonQueryAsync(connection, commandBuilder, tx, ct); /// /// Compile and execute the command against the user supplied connection and @@ -83,15 +75,9 @@ public static async Task ExecuteReaderAsync( CommandBuilder commandBuilder, SqlTransaction? tx, CancellationToken ct = default - ) - { - var cmd = commandBuilder.Compile(); - - cmd.Connection = connection; - cmd.Transaction = tx; - - return (SqlDataReader)await cmd.ExecuteReaderAsync(ct).ConfigureAwait(false); - } + ) => + (SqlDataReader)await Weasel.Core.CommandBuilderExtensions + .ExecuteReaderAsync(connection, commandBuilder, tx, ct).ConfigureAwait(false); /// @@ -121,27 +107,11 @@ public static Task> FetchListAsync( /// /// /// - public static async Task> FetchListAsync( + public static Task> FetchListAsync( this SqlConnection connection, CommandBuilder commandBuilder, Func> transform, SqlTransaction? tx, CancellationToken ct = default - ) - { - var cmd = commandBuilder.Compile(); - - cmd.Connection = connection; - 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; - } + )=> Weasel.Core.CommandBuilderExtensions.FetchListAsync(connection, commandBuilder, transform, tx, ct); }