Skip to content

Commit

Permalink
Adjusted command builder base to not have methods with connections
Browse files Browse the repository at this point in the history
Added CommandBuilder that takes NpgsqlDataSource
  • Loading branch information
oskardudycz committed Dec 5, 2023
1 parent 466e676 commit 8c3efac
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 17 deletions.
94 changes: 77 additions & 17 deletions src/Weasel.Core/CommandBuilderBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ public Task<int> ExecuteNonQueryAsync(
CancellationToken cancellation = default,
TTransaction? tx = null
) =>
base.ExecuteNonQueryAsync(conn, cancellation, tx);
base.ExecuteNonQueryAsync(cmd =>
{
cmd.Connection = conn;
cmd.Transaction = tx;
}, cancellation);


/// <summary>
Expand All @@ -50,7 +54,11 @@ public Task<TDataReader> ExecuteReaderAsync(
CancellationToken cancellation = default,
TTransaction? tx = null
) =>
base.ExecuteReaderAsync(conn, cancellation, tx);
base.ExecuteReaderAsync(cmd =>
{
cmd.Connection = conn;
cmd.Transaction = tx;
}, cancellation);

/// <summary>
/// Compile and execute the query and returns the results transformed from the raw database reader
Expand All @@ -67,7 +75,11 @@ public Task<IReadOnlyList<T>> FetchListAsync<T>(
CancellationToken ct = default,
TTransaction? tx = null
) =>
base.FetchListAsync(conn, transform, ct, tx);
base.FetchListAsync(cmd =>
{
cmd.Connection = conn;
cmd.Transaction = tx;
}, transform, ct);

protected CommandBuilderBase(
IDatabaseProvider<TCommand, TParameter, TTransaction, TParameterType, TDataReader> provider,
Expand Down Expand Up @@ -165,18 +177,42 @@ public TCommand Compile()
/// <summary>
/// Compile and execute the batched command against the user supplied connection
/// </summary>
/// <param name="cancellation"></param>
/// <returns></returns>
public Task<int> ExecuteNonQueryAsync(CancellationToken cancellation = default)
{
var cmd = Compile();

return cmd.ExecuteNonQueryAsync(cancellation);
}

/// <summary>
/// Compile and execute the batched command against the user supplied connection
/// </summary>
/// <param name="define"></param>
/// <param name="cancellation"></param>
/// <returns></returns>
protected Task<int> ExecuteNonQueryAsync(Action<TCommand> define, CancellationToken cancellation = default)
{
var cmd = Compile();
define(cmd);

return cmd.ExecuteNonQueryAsync(cancellation);
}

/// <summary>
/// Compile and execute the command against the user supplied connection and
/// return a data reader for the results
/// </summary>
/// <param name="conn"></param>
/// <param name="cancellation"></param>
/// <param name="tx"></param>
/// <returns></returns>
public Task<int> ExecuteNonQueryAsync(DbConnection conn, CancellationToken cancellation = default,
TTransaction? tx = null)
public async Task<TDataReader> ExecuteReaderAsync(CancellationToken cancellation = default)
{
var cmd = Compile();
cmd.Connection = conn;
cmd.Transaction = tx;

return cmd.ExecuteNonQueryAsync(cancellation);
return (TDataReader)await cmd.ExecuteReaderAsync(cancellation).ConfigureAwait(false);
}

/// <summary>
Expand All @@ -187,12 +223,10 @@ public Task<int> ExecuteNonQueryAsync(DbConnection conn, CancellationToken cance
/// <param name="cancellation"></param>
/// <param name="tx"></param>
/// <returns></returns>
public async Task<TDataReader> ExecuteReaderAsync(DbConnection conn,
CancellationToken cancellation = default, TTransaction? tx = null)
protected async Task<TDataReader> ExecuteReaderAsync(Action<TCommand> define, CancellationToken cancellation = default)
{
var cmd = Compile();
cmd.Connection = conn;
cmd.Transaction = tx;
define(cmd);

return (TDataReader)await cmd.ExecuteReaderAsync(cancellation).ConfigureAwait(false);
}
Expand All @@ -207,15 +241,41 @@ public async Task<TDataReader> ExecuteReaderAsync(DbConnection conn,
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public async Task<IReadOnlyList<T>> FetchListAsync<T>(
DbConnection conn,
Func<DbDataReader, CancellationToken, Task<T>> transform,
CancellationToken ct = default,
TTransaction? tx = null
CancellationToken ct = default
)
{
var cmd = Compile();

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


/// <summary>
/// Compile and execute the query and returns the results transformed from the raw database reader
/// </summary>
/// <param name="conn"></param>
/// <param name="transform"></param>
/// <param name="ct"></param>
/// <param name="tx"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
protected async Task<IReadOnlyList<T>> FetchListAsync<T>(
Action<TCommand> define,
Func<DbDataReader, CancellationToken, Task<T>> transform,
CancellationToken ct = default
)
{
var cmd = Compile();
cmd.Connection = conn;
cmd.Transaction = tx;
define(cmd);

var list = new List<T>();

Expand Down
4 changes: 4 additions & 0 deletions src/Weasel.Postgresql/CommandBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ namespace Weasel.Postgresql;
public class CommandBuilder: CommandBuilderBase<NpgsqlCommand, NpgsqlParameter, NpgsqlConnection, NpgsqlTransaction,
NpgsqlDbType, NpgsqlDataReader>
{
public CommandBuilder(NpgsqlDataSource dataSource): this(dataSource.CreateCommand())
{
}

public CommandBuilder(): this(new NpgsqlCommand())
{
}
Expand Down

0 comments on commit 8c3efac

Please sign in to comment.