Skip to content
2 changes: 0 additions & 2 deletions Source/Core/ChunkSizing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,6 @@ public ChunkSizes(
LastStreamDoubling = ChunkSizing.Log2(StreamMaxRows / FirstRows);
}

// The value when it is a power of two no larger than the maximum;
// otherwise the default.
static int Fit(int value, int max, int fallback)
=> ChunkSizing.IsPowerOfTwo(value) && value <= max
? value
Expand Down
1 change: 0 additions & 1 deletion Source/Core/ChunkStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,6 @@ async IAsyncEnumerator<ReadOnlyMemory<T>> Read(
!cancellationToken.IsCancellationRequested
&& reader.TryRead(out ArrayLease<T> chunk))
{
// The array goes back at the next step.
using (chunk)
yield return chunk.Memory;
}
Expand Down
6 changes: 2 additions & 4 deletions Source/Core/Core/RowMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -456,8 +456,6 @@ RowMap<T> Converting<TColumn, TMember>(
/// <summary>The number of layouts a map keeps readers for before its cache is cleared.</summary>
const int MaxLayouts = 128;

// The statics below are one immutable copy per model type: see TypeShape.

// Never throws: an unmappable T holds its error, so no static initializer here can fail with it.
// The per-model-type statics (TypeShape, MappedSlots, ModelSlotIndex, Default) are one immutable copy per T, built once.
// Any cache of compiled readers keyed by model type lives as long and costs a lookup per call; a static on the generic type is the cheapest place for it.
Expand Down Expand Up @@ -1277,7 +1275,7 @@ public bool Matches(IDataRecord r)
}
}

#region Array rows (the route generated mappers and the Channel and Dataflow pipelines use)
#region Array rows (the route the output and capture reads use)

/// <summary>The layout of the <c>object[]</c> rows most recently used.</summary>
/// <remarks>
Expand All @@ -1303,7 +1301,7 @@ public bool Matches(ImmutableArray<string> candidate)
}

/// <summary>Returns the mapper from <c>object[]</c> rows whose columns are, in order, <paramref name="names"/>.</summary>
/// <remarks>For generated code.</remarks>
/// <remarks>For the output and capture reads, whose caller owns the loop.</remarks>
/// <param name="names">The names of the columns, in order.</param>
/// <returns>The delegate that maps a row to a <typeparamref name="T"/>.</returns>
/// <exception cref="ValueMappingException">A required property has no column among <paramref name="names"/>.</exception>
Expand Down
10 changes: 2 additions & 8 deletions Source/Core/DbNonQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,29 +134,26 @@ public OutputDbNonQuery<T> Output<T>(
/// <inheritdoc cref="DbQuery.Using(DbConnection)"/>
/// <returns>The write, bound.</returns>
public BoundCall<NonQueryKind> Using(DbConnection connection)
// DbQuery.Using throws ArgumentNullException for a null source.
// Every Using overload of this type: DbQuery.Using throws ArgumentNullException for a null source.
=> new(_definition.Using(connection).ToCall());

/// <inheritdoc cref="DbQuery.Using(DbTransaction)"/>
/// <returns>The write, bound.</returns>
public BoundCall<NonQueryKind> Using(DbTransaction transaction)
// DbQuery.Using throws ArgumentNullException for a null source.
=> new(_definition.Using(transaction).ToCall());

/// <inheritdoc cref="DbQuery.Using{TConnection}(IDbConnectionPool{TConnection})"/>
/// <returns>The write, bound.</returns>
public BoundCall<NonQueryKind> Using<TConnection>(
IDbConnectionPool<TConnection> pool)
where TConnection : DbConnection
// DbQuery.Using throws ArgumentNullException for a null source.
=> new(_definition.Using(pool).ToCall());

/// <inheritdoc cref="DbQuery.Using{TConnection}(IDbConnectionFactory{TConnection})"/>
/// <returns>The write, bound.</returns>
public BoundCall<NonQueryKind> Using<TConnection>(
IDbConnectionFactory<TConnection> factory)
where TConnection : DbConnection
// DbQuery.Using throws ArgumentNullException for a null source.
=> new(_definition.Using(factory).ToCall());

/// <inheritdoc cref="DbQueryUsing.AddParam{T}(string, T)"/>
Expand Down Expand Up @@ -219,29 +216,26 @@ public PendingParams<NonQueryKind, TParams> AddParams(TParams values)
/// <inheritdoc cref="DbQuery.Using(DbConnection)"/>
/// <returns>The write, bound.</returns>
public BoundParams<NonQueryKind, TParams> Using(DbConnection connection)
// DbQuery.Using throws ArgumentNullException for a null source.
// Every Using overload of this type: DbQuery.Using throws ArgumentNullException for a null source.
=> new(_definition.Using(connection));

/// <inheritdoc cref="DbQuery.Using(DbTransaction)"/>
/// <returns>The write, bound.</returns>
public BoundParams<NonQueryKind, TParams> Using(DbTransaction transaction)
// DbQuery.Using throws ArgumentNullException for a null source.
=> new(_definition.Using(transaction));

/// <inheritdoc cref="DbQuery.Using{TConnection}(IDbConnectionPool{TConnection})"/>
/// <returns>The write, bound.</returns>
public BoundParams<NonQueryKind, TParams> Using<TConnection>(
IDbConnectionPool<TConnection> pool)
where TConnection : DbConnection
// DbQuery.Using throws ArgumentNullException for a null source.
=> new(_definition.Using(pool));

/// <inheritdoc cref="DbQuery.Using{TConnection}(IDbConnectionFactory{TConnection})"/>
/// <returns>The write, bound.</returns>
public BoundParams<NonQueryKind, TParams> Using<TConnection>(
IDbConnectionFactory<TConnection> factory)
where TConnection : DbConnection
// DbQuery.Using throws ArgumentNullException for a null source.
=> new(_definition.Using(factory));

/// <summary>
Expand Down
6 changes: 1 addition & 5 deletions Source/Core/DbQueryRunner.Output.cs
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,6 @@ static async ValueTask<TResult> ExecuteWithOutputAsync<TValue, TResult>(
{
// Cancelled awaited tasks throw, so this does too.
cancellationToken.ThrowIfCancellationRequested();
output.Token = cancellationToken;

TValue value;
// The run is a block of its own, so everything is released at its end.
Expand Down Expand Up @@ -348,10 +347,7 @@ public static async ValueTask<TResult> RunCapturedAsync<TState, TResult>(
{
// Cancelled awaited tasks throw, so this does too.
cancellationToken.ThrowIfCancellationRequested();
var output = new OutputRun(run.Set.Layout)
{
Token = cancellationToken,
};
var output = new OutputRun(run.Set.Layout);

TResult result;
{
Expand Down
2 changes: 1 addition & 1 deletion Source/Core/DirectStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public DirectStream(
}

// Whether the watchdog timer exists: it does not before the first row, nor after the stream is over.
internal bool HasTimer => Volatile.Read(ref _timer) is not null;
public bool HasTimer => Volatile.Read(ref _timer) is not null;

TimeoutException ToException()
=> StallStream.TimeoutFor(_stall);
Expand Down
7 changes: 5 additions & 2 deletions Source/Core/ExpressiveCommandBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -994,7 +994,9 @@ await ProcedureParameterMetadata.EnsureAsync(

// The @names the text declares, when it declares any.
NameSet? TextNames()
=> Type == CommandType.Text && Command is { Length: > 0 } text && TextNameCache.Get(text) is { Names.Length: > 0 } names
=> Type == CommandType.Text
&& Command is { Length: > 0 } text
&& TextNameCache.Get(text) is { Names.Length: > 0 } names
? names
: null;

Expand Down Expand Up @@ -1230,7 +1232,8 @@ InvalidOperationException Collision(string name, Param first, Param second, Immu
+ " Name one of them explicitly: AddParam(\"name\", value).");

InvalidOperationException NoMatch(string expression, ImmutableArray<string> real)
=> DbQueryBinding.NoMatch(expression, Command, "The procedure declares", real);
=> DbQueryBinding.NoMatch(
expression, Command, "The procedure declares", real);

#endregion

Expand Down
2 changes: 1 addition & 1 deletion Source/Core/ExpressiveDbCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public ExpressiveDbCommand(
CommandType type,
string command,
IEnumerable<Param>? @params = null)
: base(connFactory, type, command, @params)
: base(connFactory, type, command, @params)
{
}

Expand Down
17 changes: 3 additions & 14 deletions Source/Core/Extensions/Command._.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,6 @@ namespace Open.Database.Extensions;
public static partial class CommandExtensions
{

#region Connection.EnsureOpen Shortcuts.
// A command with no connection is refused by the connection extension: ArgumentNullException named "connection".
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static ConnectionState EnsureOpen(this IDbCommand command)
=> command.Connection!.EnsureOpen();

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static ValueTask<ConnectionState> EnsureOpenAsync(this IDbCommand command, CancellationToken cancellationToken)
=> command.Connection!.EnsureOpenAsync(cancellationToken);

/// <summary>
/// Gets the scope of the command's own connection:
/// it is opened if needed, and closed again if the scope opened it.
Expand Down Expand Up @@ -60,7 +50,8 @@ internal static async ValueTask<ConnectionScope<IDbConnection>> EnsureOpenScopeA
internal static TReader OpenAndExecuteReader<TCommand, TReader>(TCommand command, CommandBehavior behavior, Func<TCommand, CommandBehavior, TReader> execute)
where TCommand : IDbCommand
{
if (!ConnectionExtensions.NeedsOpen(command.EnsureOpen()))
// A command with no connection is refused by the connection extension: ArgumentNullException named "connection".
if (!ConnectionExtensions.NeedsOpen(command.Connection!.EnsureOpen()))
return execute(command, behavior);

// The connection this call opened is the one it closes if the execute fails.
Expand All @@ -80,7 +71,7 @@ internal static TReader OpenAndExecuteReader<TCommand, TReader>(TCommand command
internal static async ValueTask<TReader> OpenAndExecuteReaderAsync<TCommand, TReader>(TCommand command, CommandBehavior behavior, Func<TCommand, CommandBehavior, CancellationToken, Task<TReader>> execute, CancellationToken cancellationToken)
where TCommand : IDbCommand
{
ConnectionState prior = await command.EnsureOpenAsync(cancellationToken).ConfigureAwait(false);
ConnectionState prior = await command.Connection!.EnsureOpenAsync(cancellationToken).ConfigureAwait(false);
if (!ConnectionExtensions.NeedsOpen(prior))
return await execute(command, behavior, cancellationToken).ConfigureAwait(false);

Expand Down Expand Up @@ -153,8 +144,6 @@ static async ValueTask<TResult> ExecuteReaderCoreAsync<TArg, TResult>(
return await handle(reader, arg).ConfigureAwait(false);
}

#endregion

/// <inheritdoc cref="ExecuteReaderAsync(IDbCommand, CommandBehavior, CancellationToken)" path="/*[not(self::param[@name='behavior'])]"/>
public static ValueTask<IDataReader> ExecuteReaderAsync(this IDbCommand command,
CancellationToken cancellationToken = default)
Expand Down
4 changes: 0 additions & 4 deletions Source/Core/Extensions/DataReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -929,10 +929,6 @@ public static async ValueTask IterateWhileAsync(this DbDataReader reader, Func<I

static async ValueTask IterateWhileAsyncInternal(IDataReader reader, Func<IDataRecord, ValueTask<bool>> predicate, CancellationToken cancellationToken)
{
if (reader is null) throw new ArgumentNullException(nameof(reader));
if (predicate is null) throw new ArgumentNullException(nameof(predicate));
Contract.EndContractBlock();

while (ReadNext(reader, cancellationToken))
{
if (!await predicate(reader).ConfigureAwait(false)) break;
Expand Down
18 changes: 18 additions & 0 deletions Source/Core/Mapped.docs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -313,4 +313,22 @@
A cancel or a dispose stops the read promptly only as far as the provider honors the cancellation.
</para>
</members>

<members name="single-result-opt-out">
<para>
A <c>Map</c> terminal returns the rows of the first result set
(a <em>result</em> here is one whole result set, what <see cref="IDataReader.NextResult"/> moves between; never a row)
and then reads through the later sets without reading their rows,
so a failure in a later statement is thrown as the provider raised it.
This is the one explicit opt-out of that.
It asserts nothing: it does not check that there is only one result set.
</para>
<para>
Call it before <c>Map</c>, as in <c>command.SingleResult().Map&lt;Post&gt;().ToListAsync(cancellationToken)</c>.
</para>
<para>
A later failure can follow a write that already committed,
so a caller that retries can repeat it, and the opt-out hides that failure.
</para>
</members>
</docs>
13 changes: 7 additions & 6 deletions Source/Core/MappedViews.cs
Original file line number Diff line number Diff line change
Expand Up @@ -371,12 +371,13 @@ internal static MappedQueryCore<T> ReaderCore<T>(
internal static MappedQueryCore<T> CommandCore<T>(
IDbCommand command,
IRowSource<T> source)
{
var single = command as SingleResultDbCommand;
return NewCore(
new CommandExecutor(single?.Command ?? command), default, source,
single is null ? QueryOptions.None : QueryOptions.SingleResult);
}
=> command is SingleResultDbCommand single
? NewCore(
new CommandExecutor(single.Command), default, source,
QueryOptions.SingleResult)
: NewCore(
new CommandExecutor(command), default, source,
QueryOptions.None);

internal static MappedQueryCore<T> TableCore<T>(
DataTable table,
Expand Down
6 changes: 2 additions & 4 deletions Source/Core/ProcedureParameterMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ public static async ValueTask EnsureAsync(
{
#if NET10_0_OR_GREATER
using DataTable table = await db.GetSchemaAsync(
ProcedureSchemaNames.Collection, restrictions, cancellationToken)
ProcedureSchemaNames.Collection, restrictions,
cancellationToken)
.ConfigureAwait(false);
#else
// DbConnection.GetSchemaAsync does not exist on the netstandard
Expand All @@ -100,8 +101,6 @@ public static async ValueTask EnsureAsync(
using DataTable table = db.GetSchema(
ProcedureSchemaNames.Collection, restrictions);
#endif
// A table that tells nothing usable, or that spans schemas for a
// name given without one (ambiguous), leaves nothing known.
bool schemaGiven = restrictions[1] is not null;
metadata = table is null
|| (!schemaGiven && ProcedureSchemaNames.SpansSchemas(table))
Expand Down Expand Up @@ -203,7 +202,6 @@ static int ScaleOf(object value)
? scale
: ParameterMetadata.NoScale;

// A value the database reports that is not a number is "not known".
static bool TryInt(object value, out int result)
{
try
Expand Down
14 changes: 11 additions & 3 deletions Source/Core/ProcedureSchemaNames.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ internal static ImmutableArray<string> Get(DbConnection connection, string proce

try
{
using DataTable table = connection.GetSchema(Collection, restrictions);
using DataTable table
= connection.GetSchema(Collection, restrictions);
return Names(table, schemaGiven: restrictions[1] is not null);
}
catch (Exception ex) when (ex is NotSupportedException or NotImplementedException or ArgumentException)
Expand Down Expand Up @@ -78,7 +79,9 @@ internal static async ValueTask<ImmutableArray<string>> GetAsync(DbConnection co

try
{
using DataTable table = await connection.GetSchemaAsync(Collection, restrictions, cancellationToken).ConfigureAwait(false);
using DataTable table = await connection.GetSchemaAsync(
Collection, restrictions, cancellationToken)
.ConfigureAwait(false);
return Names(table, schemaGiven: restrictions[1] is not null);
}
catch (Exception ex) when (ex is NotSupportedException or NotImplementedException or ArgumentException)
Expand Down Expand Up @@ -206,7 +209,12 @@ public static bool SpansSchemas(DataTable table)
{
string current = row["SPECIFIC_SCHEMA"] as string ?? "";
schema ??= current;
if (!string.Equals(schema, current, StringComparison.OrdinalIgnoreCase)) return true;
if (!string.Equals(
schema, current,
StringComparison.OrdinalIgnoreCase))
{
return true;
}
}

return false;
Expand Down
10 changes: 2 additions & 8 deletions Source/Core/ScalarDbQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,26 +41,23 @@ public ScalarDbQuery<TParams, T> Params<TParams>()

/// <inheritdoc cref="DbQuery.Using(DbConnection)"/>
public BoundCall<ScalarKind<T>> Using(DbConnection connection)
// DbQuery.Using throws ArgumentNullException for a null source.
// Every Using overload of this type: DbQuery.Using throws ArgumentNullException for a null source.
=> new(_definition.Using(connection).ToCall());

/// <inheritdoc cref="DbQuery.Using(DbTransaction)"/>
public BoundCall<ScalarKind<T>> Using(DbTransaction transaction)
// DbQuery.Using throws ArgumentNullException for a null source.
=> new(_definition.Using(transaction).ToCall());

/// <inheritdoc cref="DbQuery.Using{TConnection}(IDbConnectionPool{TConnection})"/>
public BoundCall<ScalarKind<T>> Using<TConnection>(
IDbConnectionPool<TConnection> pool)
where TConnection : DbConnection
// DbQuery.Using throws ArgumentNullException for a null source.
=> new(_definition.Using(pool).ToCall());

/// <inheritdoc cref="DbQuery.Using{TConnection}(IDbConnectionFactory{TConnection})"/>
public BoundCall<ScalarKind<T>> Using<TConnection>(
IDbConnectionFactory<TConnection> factory)
where TConnection : DbConnection
// DbQuery.Using throws ArgumentNullException for a null source.
=> new(_definition.Using(factory).ToCall());

/// <inheritdoc cref="DbQueryUsing.AddParam{T}(string, T)"/>
Expand Down Expand Up @@ -109,25 +106,22 @@ public PendingParams<ScalarKind<T>, TParams> AddParams(TParams values)

/// <inheritdoc cref="DbQuery.Using(DbConnection)"/>
public BoundParams<ScalarKind<T>, TParams> Using(DbConnection connection)
// DbQuery.Using throws ArgumentNullException for a null source.
// Every Using overload of this type: DbQuery.Using throws ArgumentNullException for a null source.
=> new(_definition.Using(connection));

/// <inheritdoc cref="DbQuery.Using(DbTransaction)"/>
public BoundParams<ScalarKind<T>, TParams> Using(DbTransaction transaction)
// DbQuery.Using throws ArgumentNullException for a null source.
=> new(_definition.Using(transaction));

/// <inheritdoc cref="DbQuery.Using{TConnection}(IDbConnectionPool{TConnection})"/>
public BoundParams<ScalarKind<T>, TParams> Using<TConnection>(
IDbConnectionPool<TConnection> pool)
where TConnection : DbConnection
// DbQuery.Using throws ArgumentNullException for a null source.
=> new(_definition.Using(pool));

/// <inheritdoc cref="DbQuery.Using{TConnection}(IDbConnectionFactory{TConnection})"/>
public BoundParams<ScalarKind<T>, TParams> Using<TConnection>(
IDbConnectionFactory<TConnection> factory)
where TConnection : DbConnection
// DbQuery.Using throws ArgumentNullException for a null source.
=> new(_definition.Using(factory));
}
Loading