Skip to content

Commit

Permalink
Removed methods executing SQL from the command builder
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
oskardudycz committed Dec 7, 2023
1 parent 640a257 commit 78f0976
Show file tree
Hide file tree
Showing 18 changed files with 230 additions and 305 deletions.
207 changes: 0 additions & 207 deletions src/Weasel.Core/CommandBuilderBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,99 +4,6 @@

namespace Weasel.Core;

public static class CommandBuilderExtensions
{
/// <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<TCommand, TConnection, TTransaction>(
this ICommandBuilder<TCommand> 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);
}


/// <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 async Task<TDataReader> ExecuteReaderAsync<TCommand, TConnection, TTransaction, TDataReader>(
this ICommandBuilder<TCommand> 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);
}

/// <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, TCommand, TConnection, TTransaction>(
this ICommandBuilder<TCommand> commandBuilder,
TConnection conn,
Func<DbDataReader, CancellationToken, Task<T>> 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<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;
}
}

public interface ICommandBuilder<out TCommand>
where TCommand : DbCommand
{
Expand Down Expand Up @@ -185,120 +92,6 @@ public TCommand Compile()
return _command;
}

/// <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<DbDataReader> ExecuteReaderAsync(CancellationToken cancellation = default)
{
var cmd = Compile();

return cmd.ExecuteReaderAsync(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>
protected Task<DbDataReader> ExecuteReaderAsync(Action<TCommand> define, CancellationToken cancellation = default)
{
var cmd = Compile();
define(cmd);

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 async Task<IReadOnlyList<T>> FetchListAsync<T>(
Func<DbDataReader, CancellationToken, Task<T>> transform,
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();
define(cmd);

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>
/// Adds a parameter to the underlying command, but does NOT add the
/// parameter usage to the command text
Expand Down
94 changes: 69 additions & 25 deletions src/Weasel.Core/DbCommandBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ namespace Weasel.Core;
/// <summary>
/// CommandBuilder for generic DbCommand or DbConnection commands
/// </summary>
public class
DbCommandBuilder: CommandBuilderBase<DbCommand, DbParameter, DbType>
public class DbCommandBuilder: CommandBuilderBase<DbCommand, DbParameter, DbType>
{
public DbCommandBuilder(DbCommand command): base(DbDatabaseProvider.Instance, '@', command)
{
Expand All @@ -25,69 +24,114 @@ 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="connection"></param>
/// <param name="commandBuilder"></param>
/// <param name="ct"></param>
/// <returns></returns>
public static Task<int> ExecuteNonQueryAsync(
this DbConnection connection,
DbCommandBuilder commandBuilder,
CancellationToken ct = default
) => connection.ExecuteNonQueryAsync(commandBuilder, null, ct);

/// <summary>
/// Compile and execute the batched 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(
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);
}

/// <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(
this DbConnection connection,
DbCommandBuilder commandBuilder,
CancellationToken ct = default
) => connection.ExecuteReaderAsync(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="conn"></param>
/// <param name="cancellation"></param>
/// <param name="connection"></param>
/// <param name="commandBuilder"></param>
/// <param name="tx"></param>
/// <param name="ct"></param>
/// <returns></returns>
public static Task<DbDataReader> 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);
}

/// <summary>
/// Compile and execute the query and returns the results transformed from the raw database reader
/// </summary>
/// <param name="conn"></param>
/// <param name="connection"></param>
/// <param name="commandBuilder"></param>
/// <param name="transform"></param>
/// <param name="ct"></param>
/// <param name="tx"></param>
/// <param name="ct"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static Task<IReadOnlyList<T>> FetchListAsync<T>(
this DbConnection connection,
DbCommandBuilder commandBuilder,
Func<DbDataReader, CancellationToken, Task<T>> transform,
CancellationToken ct = default
) => connection.FetchListAsync(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="ct"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static async Task<IReadOnlyList<T>> FetchListAsync<T>(
this DbCommandBuilder commandBuilder,
DbConnection conn,
this DbConnection connection,
DbCommandBuilder commandBuilder,
Func<DbDataReader, CancellationToken, Task<T>> 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<T>();
Expand Down
2 changes: 1 addition & 1 deletion src/Weasel.Core/SchemaMigration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down
Loading

0 comments on commit 78f0976

Please sign in to comment.