Skip to content

Commit

Permalink
Moved command execution out of builder and added dedicated extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
oskardudycz committed Dec 6, 2023
1 parent 7219abe commit 8138006
Show file tree
Hide file tree
Showing 6 changed files with 319 additions and 86 deletions.
115 changes: 63 additions & 52 deletions src/Weasel.Core/CommandBuilderBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,7 @@

namespace Weasel.Core;

/// <summary>
/// Base class for batch queries against a relational database
/// </summary>
/// <typeparam name="TCommand"></typeparam>
/// <typeparam name="TParameter"></typeparam>
/// <typeparam name="TConnection"></typeparam>
/// <typeparam name="TTransaction"></typeparam>
/// <typeparam name="TParameterType"></typeparam>
/// <typeparam name="TDataReader"></typeparam>
public class CommandBuilderBase<TCommand, TParameter, TConnection, TTransaction, TParameterType, TDataReader>
: CommandBuilderBase<TCommand, TParameter, TTransaction, TParameterType, TDataReader>
where TCommand : DbCommand
where TParameter : DbParameter
where TConnection : DbConnection
where TTransaction : DbTransaction
where TDataReader : DbDataReader
where TParameterType : struct
public static class CommandBuilderExtensions
{
/// <summary>
/// Compile and execute the batched command against the user supplied connection
Expand All @@ -29,16 +13,24 @@ public class CommandBuilderBase<TCommand, TParameter, TConnection, TTransaction,
/// <param name="cancellation"></param>
/// <param name="tx"></param>
/// <returns></returns>
public Task<int> ExecuteNonQueryAsync(
public static Task<int> ExecuteNonQueryAsync<TCommand, TConnection, TTransaction>(
this ICommandBuilder<TCommand> commandBuilder,
TConnection conn,
CancellationToken cancellation = default,
TTransaction? tx = null
) =>
base.ExecuteNonQueryAsync(cmd =>
{
cmd.Connection = conn;
cmd.Transaction = tx;
}, cancellation);
)
where TCommand : DbCommand
where TConnection : DbConnection
where TTransaction : DbTransaction
{

var cmd = commandBuilder.Compile();

cmd.Connection = conn;
cmd.Transaction = tx;

return cmd.ExecuteNonQueryAsync(cancellation);
}


/// <summary>
Expand All @@ -49,16 +41,24 @@ public Task<int> ExecuteNonQueryAsync(
/// <param name="cancellation"></param>
/// <param name="tx"></param>
/// <returns></returns>
public Task<TDataReader> ExecuteReaderAsync(
public static async Task<TDataReader> ExecuteReaderAsync<TCommand, TConnection, TTransaction, TDataReader>(
this ICommandBuilder<TCommand> commandBuilder,
TConnection conn,
CancellationToken cancellation = default,
TTransaction? tx = null
) =>
base.ExecuteReaderAsync(cmd =>
{
cmd.Connection = conn;
cmd.Transaction = tx;
}, cancellation);
)
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);
}

/// <summary>
/// Compile and execute the query and returns the results transformed from the raw database reader
Expand All @@ -69,27 +69,40 @@ public Task<TDataReader> ExecuteReaderAsync(
/// <param name="tx"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public Task<IReadOnlyList<T>> FetchListAsync<T>(
public static async Task<IReadOnlyList<T>> FetchListAsync<T, TCommand, TConnection, TTransaction>(

Check warning on line 72 in src/Weasel.Core/CommandBuilderBase.cs

View workflow job for this annotation

GitHub Actions / MSSQL mcr.microsoft.com/mssql/server:2022-latest

Type parameter 'TCommand' has no matching typeparam tag in the XML comment on 'CommandBuilderExtensions.FetchListAsync<T, TCommand, TConnection, TTransaction>(ICommandBuilder<TCommand>, TConnection, Func<DbDataReader, CancellationToken, Task<T>>, CancellationToken, TTransaction?)' (but other type parameters do)

Check warning on line 72 in src/Weasel.Core/CommandBuilderBase.cs

View workflow job for this annotation

GitHub Actions / MSSQL mcr.microsoft.com/mssql/server:2022-latest

Type parameter 'TConnection' has no matching typeparam tag in the XML comment on 'CommandBuilderExtensions.FetchListAsync<T, TCommand, TConnection, TTransaction>(ICommandBuilder<TCommand>, TConnection, Func<DbDataReader, CancellationToken, Task<T>>, CancellationToken, TTransaction?)' (but other type parameters do)

Check warning on line 72 in src/Weasel.Core/CommandBuilderBase.cs

View workflow job for this annotation

GitHub Actions / MSSQL mcr.microsoft.com/mssql/server:2022-latest

Type parameter 'TTransaction' has no matching typeparam tag in the XML comment on 'CommandBuilderExtensions.FetchListAsync<T, TCommand, TConnection, TTransaction>(ICommandBuilder<TCommand>, TConnection, Func<DbDataReader, CancellationToken, Task<T>>, CancellationToken, TTransaction?)' (but other type parameters do)

Check warning on line 72 in src/Weasel.Core/CommandBuilderBase.cs

View workflow job for this annotation

GitHub Actions / MSSQL mcr.microsoft.com/mssql/server:2022-latest

Type parameter 'TCommand' has no matching typeparam tag in the XML comment on 'CommandBuilderExtensions.FetchListAsync<T, TCommand, TConnection, TTransaction>(ICommandBuilder<TCommand>, TConnection, Func<DbDataReader, CancellationToken, Task<T>>, CancellationToken, TTransaction?)' (but other type parameters do)

Check warning on line 72 in src/Weasel.Core/CommandBuilderBase.cs

View workflow job for this annotation

GitHub Actions / MSSQL mcr.microsoft.com/mssql/server:2022-latest

Type parameter 'TConnection' has no matching typeparam tag in the XML comment on 'CommandBuilderExtensions.FetchListAsync<T, TCommand, TConnection, TTransaction>(ICommandBuilder<TCommand>, TConnection, Func<DbDataReader, CancellationToken, Task<T>>, CancellationToken, TTransaction?)' (but other type parameters do)

Check warning on line 72 in src/Weasel.Core/CommandBuilderBase.cs

View workflow job for this annotation

GitHub Actions / MSSQL mcr.microsoft.com/mssql/server:2022-latest

Type parameter 'TTransaction' has no matching typeparam tag in the XML comment on 'CommandBuilderExtensions.FetchListAsync<T, TCommand, TConnection, TTransaction>(ICommandBuilder<TCommand>, TConnection, Func<DbDataReader, CancellationToken, Task<T>>, CancellationToken, TTransaction?)' (but other type parameters do)

Check warning on line 72 in src/Weasel.Core/CommandBuilderBase.cs

View workflow job for this annotation

GitHub Actions / MSSQL mcr.microsoft.com/mssql/server:2022-latest

Type parameter 'TCommand' has no matching typeparam tag in the XML comment on 'CommandBuilderExtensions.FetchListAsync<T, TCommand, TConnection, TTransaction>(ICommandBuilder<TCommand>, TConnection, Func<DbDataReader, CancellationToken, Task<T>>, CancellationToken, TTransaction?)' (but other type parameters do)

Check warning on line 72 in src/Weasel.Core/CommandBuilderBase.cs

View workflow job for this annotation

GitHub Actions / MSSQL mcr.microsoft.com/mssql/server:2019-latest

Type parameter 'TCommand' has no matching typeparam tag in the XML comment on 'CommandBuilderExtensions.FetchListAsync<T, TCommand, TConnection, TTransaction>(ICommandBuilder<TCommand>, TConnection, Func<DbDataReader, CancellationToken, Task<T>>, CancellationToken, TTransaction?)' (but other type parameters do)

Check warning on line 72 in src/Weasel.Core/CommandBuilderBase.cs

View workflow job for this annotation

GitHub Actions / MSSQL mcr.microsoft.com/mssql/server:2019-latest

Type parameter 'TConnection' has no matching typeparam tag in the XML comment on 'CommandBuilderExtensions.FetchListAsync<T, TCommand, TConnection, TTransaction>(ICommandBuilder<TCommand>, TConnection, Func<DbDataReader, CancellationToken, Task<T>>, CancellationToken, TTransaction?)' (but other type parameters do)

Check warning on line 72 in src/Weasel.Core/CommandBuilderBase.cs

View workflow job for this annotation

GitHub Actions / MSSQL mcr.microsoft.com/mssql/server:2019-latest

Type parameter 'TTransaction' has no matching typeparam tag in the XML comment on 'CommandBuilderExtensions.FetchListAsync<T, TCommand, TConnection, TTransaction>(ICommandBuilder<TCommand>, TConnection, Func<DbDataReader, CancellationToken, Task<T>>, CancellationToken, TTransaction?)' (but other type parameters do)

Check warning on line 72 in src/Weasel.Core/CommandBuilderBase.cs

View workflow job for this annotation

GitHub Actions / MSSQL mcr.microsoft.com/mssql/server:2019-latest

Type parameter 'TCommand' has no matching typeparam tag in the XML comment on 'CommandBuilderExtensions.FetchListAsync<T, TCommand, TConnection, TTransaction>(ICommandBuilder<TCommand>, TConnection, Func<DbDataReader, CancellationToken, Task<T>>, CancellationToken, TTransaction?)' (but other type parameters do)

Check warning on line 72 in src/Weasel.Core/CommandBuilderBase.cs

View workflow job for this annotation

GitHub Actions / MSSQL mcr.microsoft.com/mssql/server:2019-latest

Type parameter 'TConnection' has no matching typeparam tag in the XML comment on 'CommandBuilderExtensions.FetchListAsync<T, TCommand, TConnection, TTransaction>(ICommandBuilder<TCommand>, TConnection, Func<DbDataReader, CancellationToken, Task<T>>, CancellationToken, TTransaction?)' (but other type parameters do)

Check warning on line 72 in src/Weasel.Core/CommandBuilderBase.cs

View workflow job for this annotation

GitHub Actions / MSSQL mcr.microsoft.com/mssql/server:2019-latest

Type parameter 'TTransaction' has no matching typeparam tag in the XML comment on 'CommandBuilderExtensions.FetchListAsync<T, TCommand, TConnection, TTransaction>(ICommandBuilder<TCommand>, TConnection, Func<DbDataReader, CancellationToken, Task<T>>, CancellationToken, TTransaction?)' (but other type parameters do)

Check warning on line 72 in src/Weasel.Core/CommandBuilderBase.cs

View workflow job for this annotation

GitHub Actions / MSSQL mcr.microsoft.com/mssql/server:2019-latest

Type parameter 'TCommand' has no matching typeparam tag in the XML comment on 'CommandBuilderExtensions.FetchListAsync<T, TCommand, TConnection, TTransaction>(ICommandBuilder<TCommand>, TConnection, Func<DbDataReader, CancellationToken, Task<T>>, CancellationToken, TTransaction?)' (but other type parameters do)

Check warning on line 72 in src/Weasel.Core/CommandBuilderBase.cs

View workflow job for this annotation

GitHub Actions / Postgres ionx/postgres-plv8:12.2 Case Sensitive false

Type parameter 'TCommand' has no matching typeparam tag in the XML comment on 'CommandBuilderExtensions.FetchListAsync<T, TCommand, TConnection, TTransaction>(ICommandBuilder<TCommand>, TConnection, Func<DbDataReader, CancellationToken, Task<T>>, CancellationToken, TTransaction?)' (but other type parameters do)

Check warning on line 72 in src/Weasel.Core/CommandBuilderBase.cs

View workflow job for this annotation

GitHub Actions / Postgres ionx/postgres-plv8:12.2 Case Sensitive false

Type parameter 'TConnection' has no matching typeparam tag in the XML comment on 'CommandBuilderExtensions.FetchListAsync<T, TCommand, TConnection, TTransaction>(ICommandBuilder<TCommand>, TConnection, Func<DbDataReader, CancellationToken, Task<T>>, CancellationToken, TTransaction?)' (but other type parameters do)

Check warning on line 72 in src/Weasel.Core/CommandBuilderBase.cs

View workflow job for this annotation

GitHub Actions / Postgres ionx/postgres-plv8:12.2 Case Sensitive false

Type parameter 'TTransaction' has no matching typeparam tag in the XML comment on 'CommandBuilderExtensions.FetchListAsync<T, TCommand, TConnection, TTransaction>(ICommandBuilder<TCommand>, TConnection, Func<DbDataReader, CancellationToken, Task<T>>, CancellationToken, TTransaction?)' (but other type parameters do)

Check warning on line 72 in src/Weasel.Core/CommandBuilderBase.cs

View workflow job for this annotation

GitHub Actions / Postgres ionx/postgres-plv8:12.2 Case Sensitive false

Type parameter 'TCommand' has no matching typeparam tag in the XML comment on 'CommandBuilderExtensions.FetchListAsync<T, TCommand, TConnection, TTransaction>(ICommandBuilder<TCommand>, TConnection, Func<DbDataReader, CancellationToken, Task<T>>, CancellationToken, TTransaction?)' (but other type parameters do)

Check warning on line 72 in src/Weasel.Core/CommandBuilderBase.cs

View workflow job for this annotation

GitHub Actions / Postgres ionx/postgres-plv8:12.2 Case Sensitive false

Type parameter 'TConnection' has no matching typeparam tag in the XML comment on 'CommandBuilderExtensions.FetchListAsync<T, TCommand, TConnection, TTransaction>(ICommandBuilder<TCommand>, TConnection, Func<DbDataReader, CancellationToken, Task<T>>, CancellationToken, TTransaction?)' (but other type parameters do)

Check warning on line 72 in src/Weasel.Core/CommandBuilderBase.cs

View workflow job for this annotation

GitHub Actions / Postgres ionx/postgres-plv8:12.2 Case Sensitive false

Type parameter 'TTransaction' has no matching typeparam tag in the XML comment on 'CommandBuilderExtensions.FetchListAsync<T, TCommand, TConnection, TTransaction>(ICommandBuilder<TCommand>, TConnection, Func<DbDataReader, CancellationToken, Task<T>>, CancellationToken, TTransaction?)' (but other type parameters do)

Check warning on line 72 in src/Weasel.Core/CommandBuilderBase.cs

View workflow job for this annotation

GitHub Actions / Postgres ionx/postgres-plv8:12.2 Case Sensitive false

Type parameter 'TCommand' has no matching typeparam tag in the XML comment on 'CommandBuilderExtensions.FetchListAsync<T, TCommand, TConnection, TTransaction>(ICommandBuilder<TCommand>, TConnection, Func<DbDataReader, CancellationToken, Task<T>>, CancellationToken, TTransaction?)' (but other type parameters do)

Check warning on line 72 in src/Weasel.Core/CommandBuilderBase.cs

View workflow job for this annotation

GitHub Actions / Postgres postgres:15.3-alpine Case Sensitive false

Type parameter 'TCommand' has no matching typeparam tag in the XML comment on 'CommandBuilderExtensions.FetchListAsync<T, TCommand, TConnection, TTransaction>(ICommandBuilder<TCommand>, TConnection, Func<DbDataReader, CancellationToken, Task<T>>, CancellationToken, TTransaction?)' (but other type parameters do)

Check warning on line 72 in src/Weasel.Core/CommandBuilderBase.cs

View workflow job for this annotation

GitHub Actions / Postgres postgres:15.3-alpine Case Sensitive false

Type parameter 'TConnection' has no matching typeparam tag in the XML comment on 'CommandBuilderExtensions.FetchListAsync<T, TCommand, TConnection, TTransaction>(ICommandBuilder<TCommand>, TConnection, Func<DbDataReader, CancellationToken, Task<T>>, CancellationToken, TTransaction?)' (but other type parameters do)

Check warning on line 72 in src/Weasel.Core/CommandBuilderBase.cs

View workflow job for this annotation

GitHub Actions / Postgres postgres:15.3-alpine Case Sensitive false

Type parameter 'TTransaction' has no matching typeparam tag in the XML comment on 'CommandBuilderExtensions.FetchListAsync<T, TCommand, TConnection, TTransaction>(ICommandBuilder<TCommand>, TConnection, Func<DbDataReader, CancellationToken, Task<T>>, CancellationToken, TTransaction?)' (but other type parameters do)

Check warning on line 72 in src/Weasel.Core/CommandBuilderBase.cs

View workflow job for this annotation

GitHub Actions / Postgres postgres:15.3-alpine Case Sensitive false

Type parameter 'TCommand' has no matching typeparam tag in the XML comment on 'CommandBuilderExtensions.FetchListAsync<T, TCommand, TConnection, TTransaction>(ICommandBuilder<TCommand>, TConnection, Func<DbDataReader, CancellationToken, Task<T>>, CancellationToken, TTransaction?)' (but other type parameters do)

Check warning on line 72 in src/Weasel.Core/CommandBuilderBase.cs

View workflow job for this annotation

GitHub Actions / Postgres postgres:15.3-alpine Case Sensitive false

Type parameter 'TConnection' has no matching typeparam tag in the XML comment on 'CommandBuilderExtensions.FetchListAsync<T, TCommand, TConnection, TTransaction>(ICommandBuilder<TCommand>, TConnection, Func<DbDataReader, CancellationToken, Task<T>>, CancellationToken, TTransaction?)' (but other type parameters do)

Check warning on line 72 in src/Weasel.Core/CommandBuilderBase.cs

View workflow job for this annotation

GitHub Actions / Postgres postgres:15.3-alpine Case Sensitive false

Type parameter 'TTransaction' has no matching typeparam tag in the XML comment on 'CommandBuilderExtensions.FetchListAsync<T, TCommand, TConnection, TTransaction>(ICommandBuilder<TCommand>, TConnection, Func<DbDataReader, CancellationToken, Task<T>>, CancellationToken, TTransaction?)' (but other type parameters do)

Check warning on line 72 in src/Weasel.Core/CommandBuilderBase.cs

View workflow job for this annotation

GitHub Actions / Postgres postgres:15.3-alpine Case Sensitive false

Type parameter 'TCommand' has no matching typeparam tag in the XML comment on 'CommandBuilderExtensions.FetchListAsync<T, TCommand, TConnection, TTransaction>(ICommandBuilder<TCommand>, TConnection, Func<DbDataReader, CancellationToken, Task<T>>, CancellationToken, TTransaction?)' (but other type parameters do)

Check warning on line 72 in src/Weasel.Core/CommandBuilderBase.cs

View workflow job for this annotation

GitHub Actions / Postgres ionx/postgres-plv8:12.2 Case Sensitive true

Type parameter 'TCommand' has no matching typeparam tag in the XML comment on 'CommandBuilderExtensions.FetchListAsync<T, TCommand, TConnection, TTransaction>(ICommandBuilder<TCommand>, TConnection, Func<DbDataReader, CancellationToken, Task<T>>, CancellationToken, TTransaction?)' (but other type parameters do)

Check warning on line 72 in src/Weasel.Core/CommandBuilderBase.cs

View workflow job for this annotation

GitHub Actions / Postgres ionx/postgres-plv8:12.2 Case Sensitive true

Type parameter 'TConnection' has no matching typeparam tag in the XML comment on 'CommandBuilderExtensions.FetchListAsync<T, TCommand, TConnection, TTransaction>(ICommandBuilder<TCommand>, TConnection, Func<DbDataReader, CancellationToken, Task<T>>, CancellationToken, TTransaction?)' (but other type parameters do)

Check warning on line 72 in src/Weasel.Core/CommandBuilderBase.cs

View workflow job for this annotation

GitHub Actions / Postgres ionx/postgres-plv8:12.2 Case Sensitive true

Type parameter 'TTransaction' has no matching typeparam tag in the XML comment on 'CommandBuilderExtensions.FetchListAsync<T, TCommand, TConnection, TTransaction>(ICommandBuilder<TCommand>, TConnection, Func<DbDataReader, CancellationToken, Task<T>>, CancellationToken, TTransaction?)' (but other type parameters do)

Check warning on line 72 in src/Weasel.Core/CommandBuilderBase.cs

View workflow job for this annotation

GitHub Actions / Postgres ionx/postgres-plv8:12.2 Case Sensitive true

Type parameter 'TCommand' has no matching typeparam tag in the XML comment on 'CommandBuilderExtensions.FetchListAsync<T, TCommand, TConnection, TTransaction>(ICommandBuilder<TCommand>, TConnection, Func<DbDataReader, CancellationToken, Task<T>>, CancellationToken, TTransaction?)' (but other type parameters do)

Check warning on line 72 in src/Weasel.Core/CommandBuilderBase.cs

View workflow job for this annotation

GitHub Actions / Postgres ionx/postgres-plv8:12.2 Case Sensitive true

Type parameter 'TConnection' has no matching typeparam tag in the XML comment on 'CommandBuilderExtensions.FetchListAsync<T, TCommand, TConnection, TTransaction>(ICommandBuilder<TCommand>, TConnection, Func<DbDataReader, CancellationToken, Task<T>>, CancellationToken, TTransaction?)' (but other type parameters do)

Check warning on line 72 in src/Weasel.Core/CommandBuilderBase.cs

View workflow job for this annotation

GitHub Actions / Postgres ionx/postgres-plv8:12.2 Case Sensitive true

Type parameter 'TTransaction' has no matching typeparam tag in the XML comment on 'CommandBuilderExtensions.FetchListAsync<T, TCommand, TConnection, TTransaction>(ICommandBuilder<TCommand>, TConnection, Func<DbDataReader, CancellationToken, Task<T>>, CancellationToken, TTransaction?)' (but other type parameters do)

Check warning on line 72 in src/Weasel.Core/CommandBuilderBase.cs

View workflow job for this annotation

GitHub Actions / Postgres ionx/postgres-plv8:12.2 Case Sensitive true

Type parameter 'TCommand' has no matching typeparam tag in the XML comment on 'CommandBuilderExtensions.FetchListAsync<T, TCommand, TConnection, TTransaction>(ICommandBuilder<TCommand>, TConnection, Func<DbDataReader, CancellationToken, Task<T>>, CancellationToken, TTransaction?)' (but other type parameters do)

Check warning on line 72 in src/Weasel.Core/CommandBuilderBase.cs

View workflow job for this annotation

GitHub Actions / Postgres postgres:15.3-alpine Case Sensitive true

Type parameter 'TCommand' has no matching typeparam tag in the XML comment on 'CommandBuilderExtensions.FetchListAsync<T, TCommand, TConnection, TTransaction>(ICommandBuilder<TCommand>, TConnection, Func<DbDataReader, CancellationToken, Task<T>>, CancellationToken, TTransaction?)' (but other type parameters do)

Check warning on line 72 in src/Weasel.Core/CommandBuilderBase.cs

View workflow job for this annotation

GitHub Actions / Postgres postgres:15.3-alpine Case Sensitive true

Type parameter 'TConnection' has no matching typeparam tag in the XML comment on 'CommandBuilderExtensions.FetchListAsync<T, TCommand, TConnection, TTransaction>(ICommandBuilder<TCommand>, TConnection, Func<DbDataReader, CancellationToken, Task<T>>, CancellationToken, TTransaction?)' (but other type parameters do)

Check warning on line 72 in src/Weasel.Core/CommandBuilderBase.cs

View workflow job for this annotation

GitHub Actions / Postgres postgres:15.3-alpine Case Sensitive true

Type parameter 'TTransaction' has no matching typeparam tag in the XML comment on 'CommandBuilderExtensions.FetchListAsync<T, TCommand, TConnection, TTransaction>(ICommandBuilder<TCommand>, TConnection, Func<DbDataReader, CancellationToken, Task<T>>, CancellationToken, TTransaction?)' (but other type parameters do)

Check warning on line 72 in src/Weasel.Core/CommandBuilderBase.cs

View workflow job for this annotation

GitHub Actions / Postgres postgres:15.3-alpine Case Sensitive true

Type parameter 'TCommand' has no matching typeparam tag in the XML comment on 'CommandBuilderExtensions.FetchListAsync<T, TCommand, TConnection, TTransaction>(ICommandBuilder<TCommand>, TConnection, Func<DbDataReader, CancellationToken, Task<T>>, CancellationToken, TTransaction?)' (but other type parameters do)

Check warning on line 72 in src/Weasel.Core/CommandBuilderBase.cs

View workflow job for this annotation

GitHub Actions / Postgres postgres:15.3-alpine Case Sensitive true

Type parameter 'TConnection' has no matching typeparam tag in the XML comment on 'CommandBuilderExtensions.FetchListAsync<T, TCommand, TConnection, TTransaction>(ICommandBuilder<TCommand>, TConnection, Func<DbDataReader, CancellationToken, Task<T>>, CancellationToken, TTransaction?)' (but other type parameters do)

Check warning on line 72 in src/Weasel.Core/CommandBuilderBase.cs

View workflow job for this annotation

GitHub Actions / Postgres postgres:15.3-alpine Case Sensitive true

Type parameter 'TTransaction' has no matching typeparam tag in the XML comment on 'CommandBuilderExtensions.FetchListAsync<T, TCommand, TConnection, TTransaction>(ICommandBuilder<TCommand>, TConnection, Func<DbDataReader, CancellationToken, Task<T>>, CancellationToken, TTransaction?)' (but other type parameters do)

Check warning on line 72 in src/Weasel.Core/CommandBuilderBase.cs

View workflow job for this annotation

GitHub Actions / Postgres postgres:15.3-alpine Case Sensitive true

Type parameter 'TCommand' has no matching typeparam tag in the XML comment on 'CommandBuilderExtensions.FetchListAsync<T, TCommand, TConnection, TTransaction>(ICommandBuilder<TCommand>, TConnection, Func<DbDataReader, CancellationToken, Task<T>>, CancellationToken, TTransaction?)' (but other type parameters do)
this ICommandBuilder<TCommand> commandBuilder,
TConnection conn,
Func<DbDataReader, CancellationToken, Task<T>> transform,
CancellationToken ct = default,
TTransaction? tx = null
) =>
base.FetchListAsync(cmd =>
)
where TCommand : DbCommand
where TConnection : DbConnection
where TTransaction : DbTransaction
{
var cmd = commandBuilder.Compile();

cmd.Connection = conn;
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))
{
cmd.Connection = conn;
cmd.Transaction = tx;
}, transform, ct);
list.Add(await transform(reader, ct).ConfigureAwait(false));
}

protected CommandBuilderBase(
IDatabaseProvider<TCommand, TParameter, TTransaction, TParameterType, TDataReader> provider,
char parameterPrefix,
TCommand command
): base(provider, parameterPrefix, command)
{
return list;
}
}

public interface ICommandBuilder<out TCommand>
where TCommand : DbCommand
{
public TCommand Compile();
}

/// <summary>
/// Base class for batch queries against a relational database
/// </summary>
Expand All @@ -98,24 +111,22 @@ TCommand command
/// <typeparam name="TTransaction"></typeparam>
/// <typeparam name="TParameterType"></typeparam>
/// <typeparam name="TDataReader"></typeparam>
public class CommandBuilderBase<TCommand, TParameter, TTransaction, TParameterType, TDataReader>
public class CommandBuilderBase<TCommand, TParameter, TParameterType>: ICommandBuilder<TCommand>
where TCommand : DbCommand
where TParameter : DbParameter
where TTransaction : DbTransaction
where TDataReader : DbDataReader
where TParameterType : struct
{
private readonly TCommand _command;
private readonly char _parameterPrefix;

private readonly IDatabaseProvider<TCommand, TParameter, TTransaction, TParameterType, TDataReader>
private readonly IDatabaseProvider<TCommand, TParameter, TParameterType>
_provider;

// TEMP -- will shift this to being pooled later
private readonly StringBuilder _sql = new();

protected CommandBuilderBase(
IDatabaseProvider<TCommand, TParameter, TTransaction, TParameterType, TDataReader> provider,
IDatabaseProvider<TCommand, TParameter, TParameterType> provider,
char parameterPrefix,
TCommand command
)
Expand Down Expand Up @@ -208,11 +219,11 @@ protected Task<int> ExecuteNonQueryAsync(Action<TCommand> define, CancellationTo
/// <param name="cancellation"></param>
/// <param name="tx"></param>
/// <returns></returns>
public async Task<TDataReader> ExecuteReaderAsync(CancellationToken cancellation = default)
public Task<DbDataReader> ExecuteReaderAsync(CancellationToken cancellation = default)
{
var cmd = Compile();

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

/// <summary>
Expand All @@ -223,12 +234,12 @@ public async Task<TDataReader> ExecuteReaderAsync(CancellationToken cancellation
/// <param name="cancellation"></param>
/// <param name="tx"></param>
/// <returns></returns>
protected async Task<TDataReader> ExecuteReaderAsync(Action<TCommand> define, CancellationToken cancellation = default)
protected Task<DbDataReader> ExecuteReaderAsync(Action<TCommand> define, CancellationToken cancellation = default)
{
var cmd = Compile();
define(cmd);

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

/// <summary>
Expand Down
29 changes: 3 additions & 26 deletions src/Weasel.Core/DatabaseProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,9 @@ public DbObjectName Parse(string qualifiedName)
/// <typeparam name="TParameterType"></typeparam>
/// <typeparam name="TDataReader"></typeparam>
public interface
IDatabaseProvider<in TCommand, in TParameter, TTransaction, TParameterType, TDataReader>: IDatabaseProvider
IDatabaseProvider<in TCommand, in TParameter, TParameterType>: IDatabaseProvider
where TCommand : DbCommand
where TParameter : DbParameter
where TTransaction : DbTransaction
where TDataReader : DbDataReader
where TParameterType : struct
{
TParameterType StringParameterType { get; }
Expand All @@ -61,26 +59,6 @@ public interface
void SetParameterType(TParameter parameter, TParameterType dbType);
}

/// <summary>
/// Primarily responsible for handling .Net to database engine type mappings
/// </summary>
/// <typeparam name="TCommand"></typeparam>
/// <typeparam name="TParameter"></typeparam>
/// <typeparam name="TConnection"></typeparam>
/// <typeparam name="TTransaction"></typeparam>
/// <typeparam name="TParameterType"></typeparam>
/// <typeparam name="TDataReader"></typeparam>
public interface IDatabaseProvider<TCommand, TParameter, TConnection, TTransaction, TParameterType, TDataReader>: IDatabaseProvider
<TCommand, TParameter, TTransaction, TParameterType, TDataReader>
where TCommand : DbCommand
where TParameter : DbParameter
where TConnection : DbConnection
where TTransaction : DbTransaction
where TDataReader : DbDataReader
where TParameterType : struct
{
}

/// <summary>
/// Base type for database providers. Primarily responsible for handling .Net to database engine type mappings
/// </summary>
Expand All @@ -91,7 +69,7 @@ public interface IDatabaseProvider<TCommand, TParameter, TConnection, TTransacti
/// <typeparam name="TParameterType"></typeparam>
/// <typeparam name="TDataReader"></typeparam>
public abstract class DatabaseProvider<TCommand, TParameter, TTransaction, TParameterType, TDataReader>
: IDatabaseProvider<TCommand, TParameter, TTransaction, TParameterType, TDataReader>
: IDatabaseProvider<TCommand, TParameter, TParameterType>
where TCommand : DbCommand
where TParameter : DbParameter
where TTransaction : DbTransaction
Expand Down Expand Up @@ -296,8 +274,7 @@ public string ToQualifiedName(string schemaName, string objectName) =>
/// <typeparam name="TParameterType"></typeparam>
/// <typeparam name="TDataReader"></typeparam>
public abstract class DatabaseProvider<TCommand, TParameter, TConnection, TTransaction, TParameterType, TDataReader>
: DatabaseProvider<TCommand, TParameter, TTransaction, TParameterType, TDataReader>,
IDatabaseProvider<TCommand, TParameter, TConnection, TTransaction, TParameterType, TDataReader>
: DatabaseProvider<TCommand, TParameter, TTransaction, TParameterType, TDataReader>
where TCommand : DbCommand
where TParameter : DbParameter
where TConnection : DbConnection
Expand Down
86 changes: 84 additions & 2 deletions src/Weasel.Core/DbCommandBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ namespace Weasel.Core;
/// <summary>
/// CommandBuilder for generic DbCommand or DbConnection commands
/// </summary>
public class DbCommandBuilder
: CommandBuilderBase<DbCommand, DbParameter, DbConnection, DbTransaction, DbType, DbDataReader>
public class
DbCommandBuilder: CommandBuilderBase<DbCommand, DbParameter, DbType>
{
public DbCommandBuilder(DbCommand command): base(DbDatabaseProvider.Instance, '@', command)
{
Expand All @@ -20,6 +20,88 @@ public DbCommandBuilder(DbConnection connection): base(DbDatabaseProvider.Instan
}
}

public static class DbCommandBuilderExtensions
{
/// <summary>
/// Compile and execute the batched command against the user supplied connection
/// </summary>
/// <param name="conn"></param>
/// <param name="cancellation"></param>
/// <param name="tx"></param>
/// <returns></returns>
public static Task<int> ExecuteNonQueryAsync(
this DbCommandBuilder commandBuilder,
DbConnection conn,
CancellationToken cancellation = default,
DbTransaction? tx = null
)
{
var cmd = commandBuilder.Compile();

cmd.Connection = conn;
cmd.Transaction = tx;

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 static Task<DbDataReader> ExecuteReaderAsync(
this DbCommandBuilder commandBuilder,
DbConnection conn,
CancellationToken cancellation = default,
DbTransaction? tx = null
)
{
var cmd = commandBuilder.Compile();

cmd.Connection = conn;
cmd.Transaction = tx;

return cmd.ExecuteReaderAsync(cancellation);
}

/// <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>
public static async Task<IReadOnlyList<T>> FetchListAsync<T>(
this DbCommandBuilder commandBuilder,
DbConnection conn,
Func<DbDataReader, CancellationToken, Task<T>> transform,
CancellationToken ct = default,
DbTransaction? tx = null
)
{
var cmd = commandBuilder.Compile();

cmd.Connection = conn;
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;
}
}

internal class DbDatabaseProvider: DatabaseProvider<DbCommand, DbParameter, DbConnection, DbTransaction, DbType,
DbDataReader>
{
Expand Down
Loading

0 comments on commit 8138006

Please sign in to comment.