Repository navigation
MappedQuery dedupe, and a public QueryOptions with WithOptions to set it - #289
Merged
Merged
Conversation
… on the bound query No behavior change and no public API change (the public member list and order are the same). - The four switches of a mapped query (`SingleResult`, `AsyncReads`, `CancelOnEarlyStop`, `SequentialAccess`) are one internal readonly struct, `QueryOptions` (a plain struct with `With*` copies, as the netstandard targets have no `init` accessors). The query core takes it in place of four bools (seven constructor arguments, not ten), each `With*` is one copy call, and its equality compares the value; the hash code mixes the same bits as before. `MappedDbQuery` holds the value in place of four bool fields (five constructor arguments, not eight), its setters are copies of it, and `View()` makes the core with its options in one call instead of re-applying four, then the pool. - `Views.DefaultSource<T>()` is the first-column-or-default-map choice of a plain `Map<T>()`, written seven times before (`DbQuery`, `DbQueryCall`, `DbQueryUsing`, and the four view factories). - `DbQueryUsing.Map<T>()`, `Map(RowMap)`, `Map(Func)`, `AsObjectArrays()` and `AsDictionaryRows()` forward to the call they hold (`ToCall().Map<T>()` and so on), as its `AnyAsync` and `CountAsync` already did, instead of repeating the call's bodies. `Map(Func)` keeps its own selector check first, so a default value with no selector reports the selector, as it always did. Tests: `QueryOptionsTests` pins what the refactor must keep: each switch makes another query that no other switch stands for, any order of the switches is the same query, a switch set twice is the same as once, and a definition's switches and pool reach the query made from it. They pass on the code before this change as well. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
The four switches of a mapped query are `[Flags] internal enum QueryOptions : byte` (`None`, `AsyncReads` 1, `SingleResult` 2, `CancelOnEarlyStop` 4, `SequentialAccess` 8), in place of the plain struct: the values are the bits the query's hash code has always mixed in, so the hash part is `(int)_options`, equality is `==`, a copy with a switch set is `_options | QueryOptions.X`, and a switch is read with a mask (`(_options & QueryOptions.X) != 0`, never `HasFlag`, which boxes on the older targets). The core reads them through four private properties of that mask, so the capture tuples of its terminals still pass bools, computed where they are captured, at no cost per row. `Views.NewCore`, `QueryCore` and `FromCommand` pass flag values. No behavior change; the previous tests pass, and a new one pins the flag values. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
The doc comments of the members this change touches reuse text instead of repeating it: - `DbQueryCall.Map<T>()`, `Map(RowMap)`, `Map(Func)`, `AsObjectArrays()` and `AsDictionaryRows()` now hold the text, built from the shared members of `Mapped.docs.xml` (`row-type`, `map-overload`, plus two new ones, `default-bound` for the "default value" exception and `returns-mapped-query` for the one returns line they all repeated). They inherited it from `DbQueryUsing` before. - The same five members of `DbQueryUsing`, which forward to those, are `<inheritdoc cref=.../>` of them, instead of their own copies of the summary, type parameter, returns and exceptions. (The two types were each other's source: the direction is turned round, as a cycle would resolve nothing.) - The selector parameter keeps its text that the selector is called from any thread and by overlapping calls, so the shared `map-selector` text, which lacks it, is not used there. No code change. The members of the internal `QueryOptions` keep one-line docs. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Style only, no change to what the code does: every line this change adds or touches (the query core's `With*` one-liners, the `QueryOptions` call sites in `Views` and `MappedDbQuery`, the `ExecuteReader`, `ExecuteAsync` and `RunAsync` calls whose captured switches were renamed, the touched comments and doc comments, and the new tests) is within about 80 columns: an expression body starts on its own line, a long argument list is one argument per line or in natural groups under it, a long ternary is broken at its `?` and `:`, and a comment is wrapped at its phrase breaks. The `<include>` lines of the doc comments stay on one line, as everywhere in the repo. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…ions) to set it `QueryOptions` (`None`, `AsyncReads` 1, `SingleResult` 2, `CancelOnEarlyStop` 4, `SequentialAccess` 8) is a public flags enum in the `Open.Database.Extensions` namespace: how a mapped query executes its command and reads its rows. The summary of each value is inherited from the method of the same name, and the remarks say it applies to the terminals that read rows from a command (a `Map` over a reader or a table you already have has no command to execute, so it has no switches). `WithOptions(QueryOptions options)` on `MappedQuery<T>`, `MappedQueryWithSkip<T>`, `MappedQueryWithTake<T>`, `MappedCommand<T>` and `MappedDbQuery<T>` returns a query whose switches are exactly `options`. It replaces, it does not combine: any switch not in the value is turned off, including one set earlier in the chain or on the command before `Map` (`SingleResult()`, `AsyncReads()`), so `WithOptions(QueryOptions.None)` clears all four and is the only way to turn a switch off; to add one switch, the named method is the call. Skip and take are kept. A value with a bit that is none of the switches throws `ArgumentOutOfRangeException` (parameter `options`, the value as the actual value) whose message names the valid values, before a default value is looked at. The text that says so is one shared member of `Mapped.docs.xml`, and each named switch method (and the enum) points to `WithOptions` with a `seealso`. Inside, the query core's four `With*` are one `WithOptions` (set) and one `WithOption` (add one switch); the four public switch methods call `WithOption` and are unchanged for callers. The core's switches are an auto-property (`Options`), read by mask, never with `HasFlag`. `PublicAPI.Unshipped.txt` lists the enum, its values and the five methods; the release notes have a bullet. Tests: for all 16 combinations, `WithOptions(x)` is the same query (equal, same hash) as the chained calls on a command view, a mapped query, a skipped and a taken query, and a definition (and the definition's query); it replaces earlier switches, and `WithOptions(None)` turns off what a command chose before `Map`, giving the plain query of that command; it keeps skip and take; and a bit that is no switch is refused with the message, on all five types, before a default value is looked at. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Every type that has the four named switches has `WithOptions(QueryOptions options)`: the typed definition `MappedDbQuery<TParams, T>` had them and not the setter. It forwards to the untyped definition, which checks the value (`ArgumentOutOfRangeException` for a bit that is no switch), and its docs are the untyped one's (inherited), so the four named methods' `seealso` reach it too. `PublicAPI.Unshipped.txt` and the release note list it. Tests: for all 16 combinations the typed definition's `WithOptions(x)` gives the same query (equal, same hash) as the chained calls, and as the chained calls on the query made from it with `AddParams`; the typed definition refuses a bit that is no switch like the others. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Removes repeated code in the mapped query family, and makes the four switches of a mapped query one public value with one call to set them. The refactor changes no behavior and nothing existing in the public API (the public member list and its order are the same; there is no benchmark because nothing here is on a per-call path: these are the query-build steps). The one addition to the public API is
QueryOptionsandWithOptions(below).What changed
SingleResult,AsyncReads,CancelOnEarlyStop,SequentialAccess) were four bool fields, four constructor parameters and nine hand-spelled copies. They are now one[Flags] enum QueryOptions : byte(AsyncReads1,SingleResult2,CancelOnEarlyStop4,SequentialAccess8, the bits the query's hash code has always mixed in).==and the hash part is(int)options. A switch is read with a mask ((options & QueryOptions.X) != 0), neverHasFlag, which boxes on the older targets. The terminals still pass bools into their state, at no cost per row.MappedDbQueryholds the value in place of four bool fields (five constructor arguments, not eight).View()makes the core with its options in one call instead of re-applying four switches one by one, then the pool.Views.DefaultSource<T>()is the "first column for a single value, the shared default map otherwise" choice of a plainMap<T>(), which was written seven times (DbQuery,DbQueryCall,DbQueryUsingand the four view factories). At the four extension sites the old fallback was theRowMapoverload, which for a non-null map is exactly the matching core factory, so they now call it directly.DbQueryUsing.Map<T>(),Map(RowMap),Map(Func),AsObjectArrays()andAsDictionaryRows()repeated the bodies ofDbQueryCall; they now forward to the call they hold, as itsAnyAsyncandCountAsyncalready did.Map(Func)keeps its own selector check first, so a default value with no selector still reports the selector, as it always did.DbQueryCallmembers above hold the text, built from the shared members ofMapped.docs.xml(row-type,map-overload, and two new ones:default-bound, the "default value" exception, andreturns-mapped-query). The same five members ofDbQueryUsinginherit it instead of keeping their own copies. The selector parameter keeps its "called from any thread and by overlapping calls" text. Rendered in DocFX with no warnings, each of the five keeps its summary, remarks, type parameter, parameter, returns and exceptions.=>goes on its own line, and long argument lists break one per line.Not in this PR: folding the bodies of the
First/Singleone-row terminals intoFirstOrDefault/SingleOrDefault. That is on the per-call path and waits for its benchmark, which was added separately.The public addition:
QueryOptionsandWithOptionsQueryOptionsis a public[Flags]enum (None,AsyncReads1,SingleResult2,CancelOnEarlyStop4,SequentialAccess8): how a mapped query executes its command and reads its rows. The summary of each value is inherited from the method of the same name.WithOptions(QueryOptions options)onMappedQuery<T>,MappedQueryWithSkip<T>,MappedQueryWithTake<T>,MappedCommand<T>,MappedDbQuery<T>andMappedDbQuery<TParams, T>returns a query whose switches are exactlyoptions.Map, soWithOptions(QueryOptions.None)clears all four and is the only way to turn a switch off.WithOptionsin its docs. Skip and take are kept.ArgumentOutOfRangeException(parameteroptions) whose message names the valid values, before a default value is looked at.WithOptionsonMappedReader<T>orMappedTable<T>: aMapover a reader or a table you already have has no command to execute.PublicAPI.Unshipped.txtlists the enum, its values and the six methods; the release notes have a bullet under "Changes since rc.16".Why there is no behavior change
QueryOptionsTestspin what the refactor must keep: each switch makes a query that no other switch stands for, any order of the switches is the same query, a switch set twice is the same as once, and a definition's switches and pool reach the query made from it. They pass on the code before this change as well (checked against the previous master).OptionsSetterTestspin the setter: for all 16 combinationsWithOptions(x)is the same query (equal, same hash) as the chained calls, on a command view, a mapped query, a skipped and a taken query, and a definition (typed or not). It replaces earlier switches, andWithOptions(None)turns off what a command chose beforeMap. It keeps skip and take. A bit that is no switch is refused on all six types, before a default value is looked at.Checks
dotnet format styleandanalyzersat--severity inforeport nothing on all 14 projects; the docs build is clean.🤖 Generated with Claude Code