Skip to content

Commit

Permalink
Added base command builder extensions to unify the common command exe…
Browse files Browse the repository at this point in the history
…cution usage
  • Loading branch information
oskardudycz committed Dec 8, 2023
1 parent 4d1911a commit 9a07569
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 110 deletions.
124 changes: 124 additions & 0 deletions src/Weasel.Core/CommandBuilderBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
/// <summary>
/// Compile and execute the command against the user supplied connection
/// </summary>
/// <param name="connection"></param>
/// <param name="commandBuilder"></param>
/// <param name="ct"></param>
/// <returns></returns>
public static Task<int> ExecuteNonQueryAsync<T>(
DbConnection connection,
ICommandBuilder<T> commandBuilder,
CancellationToken ct = default
) where T : DbCommand =>
ExecuteNonQueryAsync(connection, commandBuilder, null, ct);

/// <summary>
/// Compile and execute the command against the user supplied connection
/// </summary>
/// <param name="connection"></param>
/// <param name="commandBuilder"></param>
/// <param name="tx"></param>
/// <param name="ct"></param>
/// <returns></returns>
public static Task<int> ExecuteNonQueryAsync<T>(
DbConnection connection,
ICommandBuilder<T> commandBuilder,
DbTransaction? tx,
CancellationToken ct = default
) where T : DbCommand
{
var cmd = commandBuilder.Compile();

cmd.Connection = connection;
cmd.Transaction = tx;

return cmd.ExecuteNonQueryAsync(ct);
}

/// <summary>
/// Compile and execute the command against the user supplied connection and
/// return a data reader for the results
/// </summary>
/// <param name="connection"></param>
/// <param name="commandBuilder"></param>
/// <param name="ct"></param>
/// <returns></returns>
public static Task<DbDataReader> ExecuteReaderAsync<T>(
DbConnection connection,
ICommandBuilder<T> commandBuilder,
CancellationToken ct = default
) where T : DbCommand =>
ExecuteReaderAsync(connection, commandBuilder, null, ct);

/// <summary>
/// Compile and execute the command against the user supplied connection and
/// return a data reader for the results
/// </summary>
/// <param name="connection"></param>
/// <param name="commandBuilder"></param>
/// <param name="tx"></param>
/// <param name="ct"></param>
/// <returns></returns>
public static Task<DbDataReader> ExecuteReaderAsync<T>(
DbConnection connection,
ICommandBuilder<T> 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<IReadOnlyList<T>> FetchListAsync<T, TCommand>(
DbConnection connection,
ICommandBuilder<TCommand> commandBuilder,
Func<DbDataReader, CancellationToken, Task<T>> transform,
CancellationToken ct = default
) where TCommand : DbCommand =>
FetchListAsync(connection, commandBuilder, transform, null, ct);

/// <summary>
/// Compile and execute the query and returns the results transformed from the raw database reader
/// </summary>
/// <param name="connection"></param>
/// <param name="commandBuilder"></param>
/// <param name="transform"></param>
/// <param name="tx"></param>
/// <param name="ct"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static async Task<IReadOnlyList<T>> FetchListAsync<T, TCommand>(
DbConnection connection,
ICommandBuilder<TCommand> commandBuilder,
Func<DbDataReader, CancellationToken, Task<T>> transform,
DbTransaction? tx,
CancellationToken ct = default
) where TCommand : DbCommand
{
var cmd = commandBuilder.Compile();

cmd.Connection = connection;
cmd.Transaction = tx;

var list = new List<T>();

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;
}
}
40 changes: 4 additions & 36 deletions src/Weasel.Core/DbCommandBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,7 @@ public static Task<int> 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);

/// <summary>
/// Compile and execute the command against the user supplied connection and
Expand Down Expand Up @@ -85,15 +77,7 @@ public static Task<DbDataReader> 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);

/// <summary>
/// Compile and execute the query and returns the results transformed from the raw database reader
Expand Down Expand Up @@ -121,29 +105,13 @@ public static Task<IReadOnlyList<T>> FetchListAsync<T>(
/// <param name="ct"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static async Task<IReadOnlyList<T>> FetchListAsync<T>(
public static Task<IReadOnlyList<T>> FetchListAsync<T>(
this DbConnection connection,
DbCommandBuilder commandBuilder,
Func<DbDataReader, CancellationToken, Task<T>> transform,
DbTransaction? tx,
CancellationToken ct = default
)
{
var cmd = commandBuilder.Compile();

cmd.Connection = connection;
cmd.Transaction = tx;

var list = new List<T>();

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<DbCommand, DbParameter, DbType>
Expand Down
45 changes: 7 additions & 38 deletions src/Weasel.Postgresql/CommandBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ public void AppendParameter(string[] values)

public static class CommandBuilderExtensions
{

/// <summary>
/// Compile and execute the command against the user supplied connection
/// </summary>
Expand All @@ -60,15 +59,7 @@ public static Task<int> 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);

/// <summary>
/// Compile and execute the command against the user supplied connection and
Expand All @@ -93,20 +84,14 @@ public static Task<NpgsqlDataReader> ExecuteReaderAsync(
/// <param name="tx"></param>
/// <param name="ct"></param>
/// <returns></returns>
public static Task<NpgsqlDataReader> ExecuteReaderAsync(
public static async Task<NpgsqlDataReader> 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<IReadOnlyList<T>> FetchListAsync<T>(
this NpgsqlConnection connection,
Expand All @@ -125,27 +110,11 @@ public static Task<IReadOnlyList<T>> FetchListAsync<T>(
/// <param name="ct"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static async Task<IReadOnlyList<T>> FetchListAsync<T>(
public static Task<IReadOnlyList<T>> FetchListAsync<T>(
this NpgsqlConnection connection,
CommandBuilder commandBuilder,
Func<DbDataReader, CancellationToken, Task<T>> transform,
NpgsqlTransaction? tx,
CancellationToken ct = default
)
{
var cmd = commandBuilder.Compile();

cmd.Connection = connection;
cmd.Transaction = tx;

var list = new List<T>();

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);
}
42 changes: 6 additions & 36 deletions src/Weasel.SqlServer/CommandBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,7 @@ public static Task<int> 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);

/// <summary>
/// Compile and execute the command against the user supplied connection and
Expand Down Expand Up @@ -83,15 +75,9 @@ public static async Task<SqlDataReader> 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);


/// <summary>
Expand Down Expand Up @@ -121,27 +107,11 @@ public static Task<IReadOnlyList<T>> FetchListAsync<T>(
/// <param name="tx"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static async Task<IReadOnlyList<T>> FetchListAsync<T>(
public static Task<IReadOnlyList<T>> FetchListAsync<T>(
this SqlConnection connection,
CommandBuilder commandBuilder,
Func<DbDataReader, CancellationToken, Task<T>> transform,
SqlTransaction? tx,
CancellationToken ct = default
)
{
var cmd = commandBuilder.Compile();

cmd.Connection = connection;
cmd.Transaction = tx;

var list = new List<T>();

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);
}

0 comments on commit 9a07569

Please sign in to comment.