Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 70 additions & 56 deletions Source/Core/Extensions/Command._.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,56 @@ internal static IDataReader OpenAndExecuteReader(this IDbCommand command, Comman
static ValueTask<DbDataReader> OpenAndExecuteReaderAsync(this DbCommand command, CommandBehavior behavior, CancellationToken cancellationToken)
=> OpenAndExecuteReaderAsync(command, behavior, static (c, b, ct) => c.ExecuteReaderAsync(b, ct), cancellationToken);

// Every handler and transform overload ends up in one of these two cores,
// which own opening the connection and disposing the reader.
// There are two because a ValueTask cannot wrap a ValueTask<T>
// without a second state machine.
// The overload passes its handler as the argument together with a static
// lambda that calls it, so there is no closure.
// The null checks are here, inside the async method,
// so a null is still reported through the returned task.
static async ValueTask ExecuteReaderCoreAsync<TArg>(
DbCommand command,
TArg arg,
string argName,
Func<DbDataReader, TArg, ValueTask> handle,
CommandBehavior behavior,
CancellationToken cancellationToken)
{
if (command is null) throw new ArgumentNullException(nameof(command));
if (arg is null) throw new ArgumentNullException(argName);
Contract.EndContractBlock();

#if NETSTANDARD2_0
using DbDataReader reader = await command.OpenAndExecuteReaderAsync(behavior, cancellationToken).ConfigureAwait(false);
#else
DbDataReader reader = await command.OpenAndExecuteReaderAsync(behavior, cancellationToken).ConfigureAwait(false);
await using var readerDisposal = reader.ConfigureAwait(false);
#endif
await handle(reader, arg).ConfigureAwait(false);
}

static async ValueTask<TResult> ExecuteReaderCoreAsync<TArg, TResult>(
DbCommand command,
TArg arg,
string argName,
Func<DbDataReader, TArg, ValueTask<TResult>> handle,
CommandBehavior behavior,
CancellationToken cancellationToken)
{
if (command is null) throw new ArgumentNullException(nameof(command));
if (arg is null) throw new ArgumentNullException(argName);
Contract.EndContractBlock();

#if NETSTANDARD2_0
using DbDataReader reader = await command.OpenAndExecuteReaderAsync(behavior, cancellationToken).ConfigureAwait(false);
#else
DbDataReader reader = await command.OpenAndExecuteReaderAsync(behavior, cancellationToken).ConfigureAwait(false);
await using var readerDisposal = reader.ConfigureAwait(false);
#endif
return await handle(reader, arg).ConfigureAwait(false);
}

#endregion

/// <inheritdoc cref="ExecuteReaderAsync(IDbCommand, CommandBehavior, CancellationToken)" path="/*[not(self::param[@name='behavior'])]"/>
Expand Down Expand Up @@ -157,23 +207,17 @@ public static ValueTask ExecuteReaderAsync(this DbCommand command,
/// <include file="Readers.docs.xml" path="docs/members[@name='cancellationToken']/*" />
/// <returns>A task that represents the asynchronous operation.</returns>
/// <include file="Readers.docs.xml" path="docs/members[@name='null-command-handler']/*" />
public static async ValueTask ExecuteReaderAsync(this DbCommand command,
public static ValueTask ExecuteReaderAsync(this DbCommand command,
Action<DbDataReader> handler,
CommandBehavior behavior,
CancellationToken cancellationToken = default)
{
if (command is null) throw new ArgumentNullException(nameof(command));
if (handler is null) throw new ArgumentNullException(nameof(handler));
Contract.EndContractBlock();

#if NETSTANDARD2_0
using DbDataReader reader = await command.OpenAndExecuteReaderAsync(behavior, cancellationToken).ConfigureAwait(false);
#else
DbDataReader reader = await command.OpenAndExecuteReaderAsync(behavior, cancellationToken).ConfigureAwait(false);
await using var readerDisposal = reader.ConfigureAwait(false);
#endif
handler(reader);
}
=> ExecuteReaderCoreAsync(command, handler, nameof(handler),
static (reader, h) =>
{
h(reader);
return default;
},
behavior, cancellationToken);

/// <inheritdoc cref="ExecuteReaderAsync(IDbCommand, Func{IDataReader, ValueTask}, CommandBehavior, CancellationToken)" path="/*[not(self::param[@name='behavior'])]"/>
public static ValueTask ExecuteReaderAsync(this IDbCommand command,
Expand Down Expand Up @@ -230,23 +274,13 @@ public static ValueTask ExecuteReaderAsync(this DbCommand command,
/// <include file="Readers.docs.xml" path="docs/members[@name='cancellationToken']/*" />
/// <returns>A task that represents the asynchronous operation.</returns>
/// <include file="Readers.docs.xml" path="docs/members[@name='null-command-handler']/*" />
public static async ValueTask ExecuteReaderAsync(this DbCommand command,
public static ValueTask ExecuteReaderAsync(this DbCommand command,
Func<DbDataReader, ValueTask> handler,
CommandBehavior behavior,
CancellationToken cancellationToken = default)
{
if (command is null) throw new ArgumentNullException(nameof(command));
if (handler is null) throw new ArgumentNullException(nameof(handler));
Contract.EndContractBlock();

#if NETSTANDARD2_0
using DbDataReader reader = await command.OpenAndExecuteReaderAsync(behavior, cancellationToken).ConfigureAwait(false);
#else
DbDataReader reader = await command.OpenAndExecuteReaderAsync(behavior, cancellationToken).ConfigureAwait(false);
await using var readerDisposal = reader.ConfigureAwait(false);
#endif
await handler(reader).ConfigureAwait(false);
}
=> ExecuteReaderCoreAsync(command, handler, nameof(handler),
static (reader, h) => h(reader),
behavior, cancellationToken);

/// <inheritdoc cref="ExecuteReaderAsync{T}(DbCommand, Func{DbDataReader, T}, CommandBehavior, CancellationToken)" path="/*[not(self::param[@name='behavior'])]"/>
public static ValueTask<T> ExecuteReaderAsync<T>(this DbCommand command,
Expand All @@ -270,23 +304,13 @@ public static ValueTask<T> ExecuteReaderAsync<T>(this DbCommand command,
/// Its result is the result of the transform.
/// </returns>
/// <include file="Readers.docs.xml" path="docs/members[@name='null-command-transform']/*" />
public static async ValueTask<T> ExecuteReaderAsync<T>(this DbCommand command,
public static ValueTask<T> ExecuteReaderAsync<T>(this DbCommand command,
Func<DbDataReader, T> transform,
CommandBehavior behavior,
CancellationToken cancellationToken = default)
{
if (command is null) throw new ArgumentNullException(nameof(command));
if (transform is null) throw new ArgumentNullException(nameof(transform));
Contract.EndContractBlock();

#if NETSTANDARD2_0
using DbDataReader reader = await command.OpenAndExecuteReaderAsync(behavior, cancellationToken).ConfigureAwait(false);
#else
DbDataReader reader = await command.OpenAndExecuteReaderAsync(behavior, cancellationToken).ConfigureAwait(false);
await using var readerDisposal = reader.ConfigureAwait(false);
#endif
return transform(reader);
}
=> ExecuteReaderCoreAsync(command, transform, nameof(transform),
static (reader, t) => new ValueTask<T>(t(reader)),
behavior, cancellationToken);

/// <inheritdoc cref="ExecuteReaderAsync{T}(IDbCommand, Func{IDataReader, ValueTask{T}}, CommandBehavior, CancellationToken)" path="/*[not(self::param[@name='behavior'])]"/>
public static ValueTask<T> ExecuteReaderAsync<T>(this IDbCommand command,
Expand Down Expand Up @@ -326,21 +350,11 @@ public static ValueTask<T> ExecuteReaderAsync<T>(this DbCommand command,
=> ExecuteReaderAsync<T>(command, transform, CommandBehavior.Default, cancellationToken);

/// <inheritdoc cref="ExecuteReaderAsync{T}(DbCommand, Func{DbDataReader, T}, CommandBehavior, CancellationToken)"/>
public static async ValueTask<T> ExecuteReaderAsync<T>(this DbCommand command,
public static ValueTask<T> ExecuteReaderAsync<T>(this DbCommand command,
Func<DbDataReader, ValueTask<T>> transform,
CommandBehavior behavior,
CancellationToken cancellationToken = default)
{
if (command is null) throw new ArgumentNullException(nameof(command));
if (transform is null) throw new ArgumentNullException(nameof(transform));
Contract.EndContractBlock();

#if NETSTANDARD2_0
using DbDataReader reader = await command.OpenAndExecuteReaderAsync(behavior, cancellationToken).ConfigureAwait(false);
#else
DbDataReader reader = await command.OpenAndExecuteReaderAsync(behavior, cancellationToken).ConfigureAwait(false);
await using var readerDisposal = reader.ConfigureAwait(false);
#endif
return await transform(reader).ConfigureAwait(false);
}
=> ExecuteReaderCoreAsync(command, transform, nameof(transform),
static (reader, t) => t(reader),
behavior, cancellationToken);
}
2 changes: 1 addition & 1 deletion Source/Core/Extensions/Transaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ internal static void RestoreConnection(DbConnection connection, ConnectionState
/// Asynchronously closes the connection as <see cref="RestoreConnection(DbConnection, ConnectionState)"/> does.
/// </summary>
/// <inheritdoc cref="RestoreConnection(DbConnection, ConnectionState)"/>
static ValueTask RestoreConnectionAsync(DbConnection connection, ConnectionState priorState)
internal static ValueTask RestoreConnectionAsync(DbConnection connection, ConnectionState priorState)
=> ConnectionExtensions.NeedsOpen(priorState)
? ConnectionExtensions.CloseQuietlyAsync(connection)
: default;
Expand Down
126 changes: 70 additions & 56 deletions Source/MSSqlClient/Extensions/Command.ExecuteReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,56 @@ public static partial class SqlCommandExtensions
/// <returns>A task whose result is the reader.</returns>
static ValueTask<SqlDataReader> OpenAndExecuteReaderAsync(this SqlCommand command, CommandBehavior behavior, CancellationToken cancellationToken)
=> CommandExtensions.OpenAndExecuteReaderAsync(command, behavior, static (c, b, ct) => c.ExecuteReaderAsync(b, ct), cancellationToken);

// Every handler and transform overload ends up in one of these two cores,
// which own opening the connection and disposing the reader.
// There are two because a ValueTask cannot wrap a ValueTask<T>
// without a second state machine.
// The overload passes its handler as the argument together with a static
// lambda that calls it, so there is no closure.
// The null checks are here, inside the async method,
// so a null is still reported through the returned task.
static async ValueTask ExecuteReaderCoreAsync<TArg>(
SqlCommand command,
TArg arg,
string argName,
Func<SqlDataReader, TArg, ValueTask> handle,
CommandBehavior behavior,
CancellationToken cancellationToken)
{
if (command is null) throw new ArgumentNullException(nameof(command));
if (arg is null) throw new ArgumentNullException(argName);
Contract.EndContractBlock();

#if NET472
using SqlDataReader reader = await command.OpenAndExecuteReaderAsync(behavior, cancellationToken).ConfigureAwait(false);
#else
SqlDataReader reader = await command.OpenAndExecuteReaderAsync(behavior, cancellationToken).ConfigureAwait(false);
await using var readerDisposal = reader.ConfigureAwait(false);
#endif
await handle(reader, arg).ConfigureAwait(false);
}

static async ValueTask<TResult> ExecuteReaderCoreAsync<TArg, TResult>(
SqlCommand command,
TArg arg,
string argName,
Func<SqlDataReader, TArg, ValueTask<TResult>> handle,
CommandBehavior behavior,
CancellationToken cancellationToken)
{
if (command is null) throw new ArgumentNullException(nameof(command));
if (arg is null) throw new ArgumentNullException(argName);
Contract.EndContractBlock();

#if NET472
using SqlDataReader reader = await command.OpenAndExecuteReaderAsync(behavior, cancellationToken).ConfigureAwait(false);
#else
SqlDataReader reader = await command.OpenAndExecuteReaderAsync(behavior, cancellationToken).ConfigureAwait(false);
await using var readerDisposal = reader.ConfigureAwait(false);
#endif
return await handle(reader, arg).ConfigureAwait(false);
}
#endregion

/// <inheritdoc cref="ExecuteReaderAsync(SqlCommand, Func{SqlDataReader, ValueTask}, CommandBehavior, CancellationToken)" path="/*[not(self::param[@name='behavior'])]"/>
Expand All @@ -37,23 +87,13 @@ public static ValueTask ExecuteReaderAsync(this SqlCommand command,
/// <include file="MSSqlClient.docs.xml" path="docs/members[@name='reader-remarks']/*" />
/// <exception cref="ArgumentNullException"><paramref name="command"/> or <paramref name="handler"/> is <see langword="null"/>. The exception is reported through the returned task.</exception>
/// <exception cref="OperationCanceledException"><paramref name="cancellationToken"/> was canceled before or while the connection was being opened or the command executed.</exception>
public static async ValueTask ExecuteReaderAsync(this SqlCommand command,
public static ValueTask ExecuteReaderAsync(this SqlCommand command,
Func<SqlDataReader, ValueTask> handler,
CommandBehavior behavior,
CancellationToken cancellationToken = default)
{
if (command is null) throw new ArgumentNullException(nameof(command));
if (handler is null) throw new ArgumentNullException(nameof(handler));
Contract.EndContractBlock();

#if NET472
using SqlDataReader reader = await command.OpenAndExecuteReaderAsync(behavior, cancellationToken).ConfigureAwait(false);
#else
SqlDataReader reader = await command.OpenAndExecuteReaderAsync(behavior, cancellationToken).ConfigureAwait(false);
await using var readerDisposal = reader.ConfigureAwait(false);
#endif
await handler(reader).ConfigureAwait(false);
}
=> ExecuteReaderCoreAsync(command, handler, nameof(handler),
static (reader, h) => h(reader),
behavior, cancellationToken);

/// <inheritdoc cref="ExecuteReaderAsync(SqlCommand, Action{SqlDataReader}, CommandBehavior, CancellationToken)" path="/*[not(self::param[@name='behavior'])]"/>
public static ValueTask ExecuteReaderAsync(this SqlCommand command,
Expand All @@ -66,23 +106,17 @@ public static ValueTask ExecuteReaderAsync(this SqlCommand command,
/// </summary>
/// <returns>A task that represents the asynchronous operation. It completes after <paramref name="handler"/> has completed and the reader has been disposed.</returns>
/// <inheritdoc cref="ExecuteReaderAsync(SqlCommand, Func{SqlDataReader, ValueTask}, CommandBehavior, CancellationToken)" path="/*[not(self::summary) and not(self::returns)]"/>
public static async ValueTask ExecuteReaderAsync(this SqlCommand command,
public static ValueTask ExecuteReaderAsync(this SqlCommand command,
Action<SqlDataReader> handler,
CommandBehavior behavior,
CancellationToken cancellationToken = default)
{
if (command is null) throw new ArgumentNullException(nameof(command));
if (handler is null) throw new ArgumentNullException(nameof(handler));
Contract.EndContractBlock();

#if NET472
using SqlDataReader reader = await command.OpenAndExecuteReaderAsync(behavior, cancellationToken).ConfigureAwait(false);
#else
SqlDataReader reader = await command.OpenAndExecuteReaderAsync(behavior, cancellationToken).ConfigureAwait(false);
await using var readerDisposal = reader.ConfigureAwait(false);
#endif
handler(reader);
}
=> ExecuteReaderCoreAsync(command, handler, nameof(handler),
static (reader, h) =>
{
h(reader);
return default;
},
behavior, cancellationToken);

/// <inheritdoc cref="ExecuteReaderAsync{T}(SqlCommand, Func{SqlDataReader, T}, CommandBehavior, CancellationToken)" path="/*[not(self::param[@name='behavior'])]"/>
public static ValueTask<T> ExecuteReaderAsync<T>(this SqlCommand command,
Expand All @@ -102,23 +136,13 @@ public static ValueTask<T> ExecuteReaderAsync<T>(this SqlCommand command,
/// <include file="MSSqlClient.docs.xml" path="docs/members[@name='reader-remarks']/*" />
/// <exception cref="ArgumentNullException"><paramref name="command"/> or <paramref name="transform"/> is <see langword="null"/>. The exception is reported through the returned task.</exception>
/// <exception cref="OperationCanceledException"><paramref name="cancellationToken"/> was canceled before or while the connection was being opened or the command executed.</exception>
public static async ValueTask<T> ExecuteReaderAsync<T>(this SqlCommand command,
public static ValueTask<T> ExecuteReaderAsync<T>(this SqlCommand command,
Func<SqlDataReader, T> transform,
CommandBehavior behavior,
CancellationToken cancellationToken = default)
{
if (command is null) throw new ArgumentNullException(nameof(command));
if (transform is null) throw new ArgumentNullException(nameof(transform));
Contract.EndContractBlock();

#if NET472
using SqlDataReader reader = await command.OpenAndExecuteReaderAsync(behavior, cancellationToken).ConfigureAwait(false);
#else
SqlDataReader reader = await command.OpenAndExecuteReaderAsync(behavior, cancellationToken).ConfigureAwait(false);
await using var readerDisposal = reader.ConfigureAwait(false);
#endif
return transform(reader);
}
=> ExecuteReaderCoreAsync(command, transform, nameof(transform),
static (reader, t) => new ValueTask<T>(t(reader)),
behavior, cancellationToken);

/// <inheritdoc cref="ExecuteReaderAsync{T}(SqlCommand, Func{SqlDataReader, ValueTask{T}}, CommandBehavior, CancellationToken)" path="/*[not(self::param[@name='behavior'])]"/>
public static ValueTask<T> ExecuteReaderAsync<T>(this SqlCommand command,
Expand All @@ -131,21 +155,11 @@ public static ValueTask<T> ExecuteReaderAsync<T>(this SqlCommand command,
/// </summary>
/// <returns>A task that represents the asynchronous operation; its result is the value produced by <paramref name="transform"/>.</returns>
/// <inheritdoc cref="ExecuteReaderAsync{T}(SqlCommand, Func{SqlDataReader, T}, CommandBehavior, CancellationToken)" path="/*[not(self::summary) and not(self::returns)]"/>
public static async ValueTask<T> ExecuteReaderAsync<T>(this SqlCommand command,
public static ValueTask<T> ExecuteReaderAsync<T>(this SqlCommand command,
Func<SqlDataReader, ValueTask<T>> transform,
CommandBehavior behavior,
CancellationToken cancellationToken = default)
{
if (command is null) throw new ArgumentNullException(nameof(command));
if (transform is null) throw new ArgumentNullException(nameof(transform));
Contract.EndContractBlock();

#if NET472
using SqlDataReader reader = await command.OpenAndExecuteReaderAsync(behavior, cancellationToken).ConfigureAwait(false);
#else
SqlDataReader reader = await command.OpenAndExecuteReaderAsync(behavior, cancellationToken).ConfigureAwait(false);
await using var readerDisposal = reader.ConfigureAwait(false);
#endif
return await transform(reader).ConfigureAwait(false);
}
=> ExecuteReaderCoreAsync(command, transform, nameof(transform),
static (reader, t) => t(reader),
behavior, cancellationToken);
}
Loading