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
4 changes: 1 addition & 3 deletions Source/Core/DbQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,7 @@ public DbQuery SetTimeout(ushort seconds)
// Any other type is read through RowMap<T>.Default, shared, so its compiled
// reader is built once.
public MappedDbQuery<T> Map<T>()
=> FirstColumn<T>.Instance is { } firstColumn
? new(this, firstColumn)
: new(this, RowMap<T>.Default);
=> new(this, Views.DefaultSource<T>());

/// <summary>Maps each row to a <typeparamref name="T"/> with a map the caller declared, and returns the mapped definition.</summary>
/// <typeparam name="T">The type each row is mapped to.</typeparam>
Expand Down
48 changes: 38 additions & 10 deletions Source/Core/DbQueryCall.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,27 @@ public DbQueryCall AddParam<T>(
SourceCore,
Params.AddDerived(Definition, CheckedExpression(expression), value));

/// <inheritdoc cref="DbQueryUsing.Map{T}()"/>
/// <summary>
/// Maps each row to a <typeparamref name="T"/> by column name (a single
/// value such as <see cref="int"/> is read from the first column), and
/// returns the mapped query.
/// </summary>
/// <remarks>
/// The same as <see cref="CoreExtensions.Map{T}(IExecuteReader)"/>.
/// </remarks>
/// <include file="Mapped.docs.xml" path="docs/members[@name='row-type']/*" />
/// <include file="Mapped.docs.xml" path="docs/members[@name='returns-mapped-query']/*" />
/// <include file="Mapped.docs.xml" path="docs/members[@name='default-bound']/*" />
// The query holds this call as it is; the value is not boxed.
public MappedQuery<T> Map<T>()
=> FirstColumn<T>.Instance is { } firstColumn
? new(Views.QueryCore(Bound(), firstColumn))
: new(Views.QueryCore(Bound(), RowMap<T>.Default));
=> new(Views.QueryCore(Bound(), Views.DefaultSource<T>()));

/// <inheritdoc cref="DbQueryUsing.Map{T}(RowMap{T})"/>
/// <summary>
/// Maps each row to a <typeparamref name="T"/> with a map the caller
/// declared, and returns the mapped query.
/// </summary>
/// <include file="Mapped.docs.xml" path="docs/members[@name='map-overload']/*" />
/// <include file="Mapped.docs.xml" path="docs/members[@name='default-bound']/*" />
// A bare null does not compile here (it also fits the selector overload):
// write (RowMap<T>?)null.
public MappedQuery<T> Map<T>(RowMap<T>? map)
Expand All @@ -100,11 +113,11 @@ public MappedQuery<T> Map<T>(RowMap<T>? map)
: new(Views.QueryCore(Bound(), map));

/// <summary>Maps each row with a selector, and returns the mapped query.</summary>
/// <typeparam name="T">The type each row is mapped to.</typeparam>
/// <include file="Mapped.docs.xml" path="docs/members[@name='row-type']/*" />
/// <param name="selector">Called for each row, from any thread and by overlapping calls, so it must be safe for concurrent use.</param>
/// <returns>The mapped query, to be read with a terminal.</returns>
/// <include file="Mapped.docs.xml" path="docs/members[@name='returns-mapped-query']/*" />
/// <exception cref="ArgumentNullException"><paramref name="selector"/> is <see langword="null"/>.</exception>
/// <exception cref="InvalidOperationException">This is a <see langword="default"/> value, which is bound to nothing.</exception>
/// <include file="Mapped.docs.xml" path="docs/members[@name='default-bound']/*" />
public MappedQuery<T> Map<T>(Func<IDataRecord, T> selector)
{
if (selector is null) throw new ArgumentNullException(nameof(selector));
Expand All @@ -113,11 +126,26 @@ public MappedQuery<T> Map<T>(Func<IDataRecord, T> selector)
return new(Views.QueryCore(Bound(), new SelectorSource<T>(selector)));
}

/// <inheritdoc cref="DbQueryUsing.AsObjectArrays()"/>
/// <summary>
/// Reads each row without a model, as an array of its values.
/// </summary>
/// <remarks>
/// The same as <see cref="CoreExtensions.AsObjectArrays(IExecuteReader)"/>.
/// </remarks>
/// <include file="Mapped.docs.xml" path="docs/members[@name='returns-mapped-query']/*" />
/// <include file="Mapped.docs.xml" path="docs/members[@name='default-bound']/*" />
public MappedQuery<object?[]> AsObjectArrays()
=> new(Views.QueryCore(Bound(), ObjectArraySource.Instance));

/// <inheritdoc cref="DbQueryUsing.AsDictionaryRows()"/>
/// <summary>
/// Reads each row without a model, as a <see cref="DictionaryRow"/>.
/// </summary>
/// <remarks>
/// The same as
/// <see cref="CoreExtensions.AsDictionaryRows(IExecuteReader)"/>.
/// </remarks>
/// <include file="Mapped.docs.xml" path="docs/members[@name='returns-mapped-query']/*" />
/// <include file="Mapped.docs.xml" path="docs/members[@name='default-bound']/*" />
public MappedQuery<DictionaryRow> AsDictionaryRows()
=> new(Views.QueryCore(Bound(), DictionaryRowSource.Instance));

Expand Down
55 changes: 15 additions & 40 deletions Source/Core/DbQueryUsing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,61 +44,36 @@ object SourceOrThrow
/// <exception cref="InvalidOperationException">This is a <see langword="default"/> value, which is bound to nothing.</exception>
public object Source => SourceOrThrow;

/// <summary>
/// Maps each row to a <typeparamref name="T"/> by column name (a single value such as
/// <see cref="int"/> is read from the first column), and returns the mapped query.
/// </summary>
/// <remarks>
/// The same as <see cref="CoreExtensions.Map{T}(IExecuteReader)"/>.
/// </remarks>
/// <typeparam name="T">The type each row is mapped to.</typeparam>
/// <returns>The mapped query, to be read with a terminal.</returns>
/// <exception cref="InvalidOperationException">This is a <see langword="default"/> value, which is bound to nothing.</exception>
/// <inheritdoc cref="DbQueryCall.Map{T}()"/>
// The query holds this value as it is; the value is not boxed.
public MappedQuery<T> Map<T>()
=> FirstColumn<T>.Instance is { } firstColumn
? new(Views.QueryCore(ToCall(), firstColumn))
: new(Views.QueryCore(ToCall(), RowMap<T>.Default));

/// <summary>Maps each row to a <typeparamref name="T"/> with a map the caller declared, and returns the mapped query.</summary>
/// <typeparam name="T">The type each row is mapped to.</typeparam>
/// <param name="map">The map. A <see langword="null"/> map reads as <see cref="Map{T}()"/> does.</param>
/// <returns>The mapped query, to be read with a terminal.</returns>
/// <exception cref="InvalidOperationException">This is a <see langword="default"/> value, which is bound to nothing.</exception>
=> ToCall().Map<T>();

/// <inheritdoc cref="DbQueryCall.Map{T}(RowMap{T})"/>
// A bare null does not compile here (it also fits the selector overload):
// write (RowMap<T>?)null.
public MappedQuery<T> Map<T>(RowMap<T>? map)
=> map is null
? Map<T>()
: new(Views.QueryCore(ToCall(), map));

/// <summary>Maps each row with a selector, and returns the mapped query.</summary>
/// <typeparam name="T">The type each row is mapped to.</typeparam>
/// <param name="selector">Called for each row, from any thread and by overlapping calls, so it must be safe for concurrent use.</param>
/// <returns>The mapped query, to be read with a terminal.</returns>
/// <exception cref="ArgumentNullException"><paramref name="selector"/> is <see langword="null"/>.</exception>
/// <exception cref="InvalidOperationException">This is a <see langword="default"/> value, which is bound to nothing.</exception>
=> ToCall().Map(map);

/// <inheritdoc cref="DbQueryCall.Map{T}(Func{IDataRecord, T})"/>
public MappedQuery<T> Map<T>(Func<IDataRecord, T> selector)
{
if (selector is null) throw new ArgumentNullException(nameof(selector));
Contract.EndContractBlock();

return new(Views.QueryCore(ToCall(), new SelectorSource<T>(selector)));
// The selector is checked above, before ToCall() reads the value, so a
// default value with no selector reports the selector first, as it
// always did.
return ToCall().Map(selector);
}

/// <summary>Reads each row without a model, as an array of its values.</summary>
/// <remarks>The same as <see cref="CoreExtensions.AsObjectArrays(IExecuteReader)"/>.</remarks>
/// <returns>The mapped query, to be read with a terminal.</returns>
/// <exception cref="InvalidOperationException">This is a <see langword="default"/> value, which is bound to nothing.</exception>
/// <inheritdoc cref="DbQueryCall.AsObjectArrays()"/>
public MappedQuery<object?[]> AsObjectArrays()
=> new(Views.QueryCore(ToCall(), ObjectArraySource.Instance));
=> ToCall().AsObjectArrays();

/// <summary>Reads each row without a model, as a <see cref="DictionaryRow"/>.</summary>
/// <remarks>The same as <see cref="CoreExtensions.AsDictionaryRows(IExecuteReader)"/>.</remarks>
/// <returns>The mapped query, to be read with a terminal.</returns>
/// <exception cref="InvalidOperationException">This is a <see langword="default"/> value, which is bound to nothing.</exception>
/// <inheritdoc cref="DbQueryCall.AsDictionaryRows()"/>
public MappedQuery<DictionaryRow> AsDictionaryRows()
=> new(Views.QueryCore(ToCall(), DictionaryRowSource.Instance));
=> ToCall().AsDictionaryRows();

/// <inheritdoc cref="CoreExtensions.AnyAsync(IExecuteReader, CancellationToken)" path="/*[not(self::param[@name='command']) and not(self::exception)]"/>
/// <exception cref="InvalidOperationException">This is a <see langword="default"/> value, which is bound to nothing.</exception>
Expand Down
39 changes: 39 additions & 0 deletions Source/Core/Mapped.docs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,45 @@
<param name="query">The query to execute.</param>
</members>

<members name="options">
<remarks>
<para>
It replaces the switches, it does not combine them: any switch that is
not in <paramref name="options"/> is turned off, including one set
earlier in the chain or on the command before <c>Map</c>
(<c>SingleResult()</c>, <c>AsyncReads()</c>).
<c>WithOptions(QueryOptions.None)</c> clears all four, and is the only
way to turn a switch off.
To add one switch and keep the others, call its own method:
<c>SingleResult()</c>, <c>AsyncReads()</c>, <c>CancelOnEarlyStop()</c>
or <c>SequentialAccess()</c>.
The pool is not a switch: see <c>UsePool</c>.
</para>
</remarks>
<param name="options">
The switches the query is to have: any combination of the
<see cref="QueryOptions"/> values.
</param>
<exception cref="System.ArgumentOutOfRangeException">
<paramref name="options"/> has a bit that is not one of the four
switches.
</exception>
</members>

<members name="see-with-options">
<seealso cref="MappedQuery{T}.WithOptions(QueryOptions)"/>
</members>

<members name="default-bound">
<exception cref="System.InvalidOperationException">
This is a <see langword="default"/> value, which is bound to nothing.
</exception>
</members>

<members name="returns-mapped-query">
<returns>The mapped query, to be read with a terminal.</returns>
</members>

<members name="query-null">
<exception cref="System.ArgumentNullException">
<paramref name="query"/> is <see langword="null"/>.
Expand Down
81 changes: 55 additions & 26 deletions Source/Core/MappedDbQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,39 +30,30 @@ namespace Open.Database.Extensions;
public sealed class MappedDbQuery<T>
{
readonly IRowSource<T> _source;
readonly bool _singleResult;
readonly bool _asyncReads;
readonly QueryOptions _options;
readonly ArrayPool<T>? _pool;
readonly bool _clearOnReturn;
readonly bool _cancelOnEarlyStop;
readonly bool _sequentialAccess;

internal MappedDbQuery(DbQuery definition, IRowSource<T> source)
: this(definition, source, false, false, null, false, false, false)
: this(definition, source, default, null, false)
{
}

MappedDbQuery(
DbQuery definition,
IRowSource<T> source,
bool singleResult,
bool asyncReads,
QueryOptions options,
ArrayPool<T>? pool,
bool clearOnReturn,
bool cancelOnEarlyStop,
bool sequentialAccess)
bool clearOnReturn)
{
Debug.Assert(definition is not null);
Debug.Assert(source is not null);

Query = definition;
_source = source;
_singleResult = singleResult;
_asyncReads = asyncReads;
_options = options;
_pool = pool;
_clearOnReturn = clearOnReturn;
_cancelOnEarlyStop = cancelOnEarlyStop;
_sequentialAccess = sequentialAccess;
}

/// <summary>Gets the command definition under this mapped one.</summary>
Expand All @@ -73,33 +64,71 @@ internal MappedDbQuery(DbQuery definition, IRowSource<T> source)
/// (see <see cref="MappedQuery{T}.SingleResult"/>).
/// </summary>
/// <returns>A definition that reads only the first result set.</returns>
/// <seealso cref="WithOptions(QueryOptions)"/>

public MappedDbQuery<T> SingleResult()
=> new(Query, _source, true, _asyncReads, _pool, _clearOnReturn, _cancelOnEarlyStop, _sequentialAccess);
=> new(
Query,
_source,
_options | QueryOptions.SingleResult,
_pool,
_clearOnReturn);

/// <summary>
/// Cancels the command when a terminal stops before the last row, in every query made from this definition
/// (see <see cref="MappedQuery{T}.CancelOnEarlyStop"/> for when to use it and what it costs).
/// </summary>
/// <returns>A definition whose queries cancel their command on an early stop.</returns>
/// <seealso cref="WithOptions(QueryOptions)"/>
public MappedDbQuery<T> CancelOnEarlyStop()
=> new(Query, _source, _singleResult, _asyncReads, _pool, _clearOnReturn, true, _sequentialAccess);
=> new(
Query,
_source,
_options | QueryOptions.CancelOnEarlyStop,
_pool,
_clearOnReturn);

/// <summary>
/// Reads the rows with the asynchronous read of the provider, in every query made from this definition
/// (see <see cref="MappedQuery{T}.AsyncReads"/>).
/// </summary>
/// <returns>A definition that reads asynchronously.</returns>
/// <seealso cref="WithOptions(QueryOptions)"/>
public MappedDbQuery<T> AsyncReads()
=> new(Query, _source, _singleResult, true, _pool, _clearOnReturn, _cancelOnEarlyStop, _sequentialAccess);
=> new(
Query,
_source,
_options | QueryOptions.AsyncReads,
_pool,
_clearOnReturn);

/// <summary>
/// Asks the reader for sequential access, in every query made from this definition
/// (see <see cref="MappedQuery{T}.SequentialAccess"/> for when to use it).
/// </summary>
/// <returns>A definition whose queries read with sequential access.</returns>
/// <seealso cref="WithOptions(QueryOptions)"/>
public MappedDbQuery<T> SequentialAccess()
=> new(Query, _source, _singleResult, _asyncReads, _pool, _clearOnReturn, _cancelOnEarlyStop, true);
=> new(
Query,
_source,
_options | QueryOptions.SequentialAccess,
_pool,
_clearOnReturn);

/// <summary>
/// Returns a definition whose switches, in every query made from it, are
/// exactly <paramref name="options"/>.
/// </summary>
/// <include file="Mapped.docs.xml" path="docs/members[@name='options']/*" />
/// <returns>A definition with exactly these switches.</returns>
public MappedDbQuery<T> WithOptions(QueryOptions options)
=> new(
Query,
_source,
Views.CheckedOptions(options),
_pool,
_clearOnReturn);

/// <summary>
/// Takes the arrays that hold the rows from <paramref name="pool"/>, in every query made from this definition
Expand All @@ -114,7 +143,7 @@ public MappedDbQuery<T> UsePool(ArrayPool<T> pool, bool clearOnReturn)
if (pool is null) throw new ArgumentNullException(nameof(pool));
Contract.EndContractBlock();

return new(Query, _source, _singleResult, _asyncReads, pool, clearOnReturn, _cancelOnEarlyStop, _sequentialAccess);
return new(Query, _source, _options, pool, clearOnReturn);
}

/// <inheritdoc cref="DbQuery.Params{TParams}"/>
Expand Down Expand Up @@ -201,13 +230,8 @@ public UnboundCall<T> AddParams<TValues>(TValues values)
/// <summary>Makes the view over a command made from this definition, with the options of this definition.</summary>
internal MappedQuery<T> View(DbQueryCall call)
{
MappedQuery<T> view = new(Views.QueryCore(call, _source));
if (_singleResult) view = view.SingleResult();
if (_cancelOnEarlyStop) view = view.CancelOnEarlyStop();
if (_asyncReads) view = view.AsyncReads();
if (_sequentialAccess) view = view.SequentialAccess();
if (_pool is not null) view = view.UsePool(_pool, _clearOnReturn);
return view;
MappedQuery<T> view = new(Views.QueryCore(call, _source, _options));
return _pool is null ? view : view.UsePool(_pool, _clearOnReturn);
}
}

Expand Down Expand Up @@ -243,6 +267,11 @@ public MappedDbQuery<TParams, T> AsyncReads()
public MappedDbQuery<TParams, T> SequentialAccess()
=> new(_definition.SequentialAccess());

/// <inheritdoc cref="MappedDbQuery{T}.WithOptions(QueryOptions)"/>
public MappedDbQuery<TParams, T> WithOptions(QueryOptions options)
// The definition throws ArgumentOutOfRangeException for a bad value.
=> new(_definition.WithOptions(options));

/// <inheritdoc cref="MappedDbQuery{T}.UsePool(ArrayPool{T}, bool)"/>
public MappedDbQuery<TParams, T> UsePool(ArrayPool<T> pool, bool clearOnReturn)
// The definition throws ArgumentNullException for a null pool.
Expand Down
Loading