Skip to content

Seven fixes for a production consumer's rc.1 feedback: a late SQL error was swallowed, generic commands dropped a parameter's direction, AddParams(object) sent nothing, Map<Dictionary> returned an empty row, the DbType overload trap, parameter names through IDbConnectionFactory<DbConnection> and on SQL text - #84

Closed
electricessence wants to merge 20 commits into
masterfrom
fix/consumer-feedback-rc1-fixes

Conversation

@electricessence

Copy link
Copy Markdown
Collaborator

Seven fixes for a production consumer's rc.1 feedback (a late SQL error was swallowed, generic commands dropped a parameter's direction, AddParams(object) sent nothing, Map<Dictionary> returned an empty row, the DbType overload trap, parameter names through an IDbConnectionFactory<DbConnection> and on SQL text), each with tests that fail first, and the release notes updated.

A production SQL Server app is moving from Dapper to ODE 11 and measured rc.1 against LocalDB with Dapper side by side. Five of its ten findings were defects and two were gaps that matter for its setup; you asked me to fix everything that needed it. The work was split over three agents on three branches (a branch per area, tests first, one commit pair per item); I merged them (no conflicts), reran everything on the merge, and fixed what the gate found. Branch integration/rc2 on master 41af17c.

What was wrong and what changed

# (the consumer's list) what the consumer saw what I found what changed tests
1 SELECT 1; RAISERROR('late', 16, 1); read with Map<T>() completes with the row; Dapper throws SqlException Real. The loops stop at the last row and nothing asks the provider for the rest. On LocalDB, raw SqlClient throws such an error only with the default reader behavior: with SingleResult or SingleRow NextResult completes silently after the natural end of the rows every Map<T>() terminal, Results, Retrieve and the Channel and Dataflow pipelines call NextResult once (the error faults the target when complete is true); FirstOrDefault, SingleOrDefault and Take(n) read one more row to find the end and stop without reading more when there is one; a handler that stops early is not drained; the mapped terminals no longer request SingleResult (or SingleRow) 140 (101 failed before)
2a an output parameter through IDbConnectionFactory<DbConnection> fails ("expects parameter ... which was not supplied") Real: ExpressiveCommand and ExpressiveDbCommand never set Direction both send the direction 15
5 AddParam("@i", DbType.Int32, ParameterDirection.Output) binds the enum as the value Real, on the SQL Server commands (their type is SqlDbType) AddParam(name, DbType, direction) and AddParamIf in both SQL packages (no default for direction, which would hijack AddParam("@count", 0)) 7
3 AddParams(x) with x typed object sends nothing; the procedure runs with every parameter NULL Real, and silent the runtime type's members are sent (as Dapper does), cached per type; members of a ref struct or pointer type are skipped on that path new
4 Map<Dictionary<string, object?>>() and Map<ExpandoObject>() return one empty row Real: not row models InvalidOperationException at the call, like Map<int>() 297 unmappable-type cases pass
2b, 2c through the interface AddParam(orderId) is sent as orderId; AddParams sends members the procedure does not declare A gap: only the SQL Server commands looked the names up the lookup runs for any DbConnection through the ADO.NET ProcedureParameters schema collection (SQL Server supports it; other providers keep "as written"); it works inside a transaction and is cached like the SQL packages' lookup 35
6 AddParam(regionId) on ... WHERE region_id = @region_id fails at the server The gap you asked about the @name markers of the SQL text are the declared names, with the same four tiers; no marker keeps the name as written 94

Items 7 to 10 of the consumer's list (output values after the call, a column-name selector, tokens on the scalar terminals plus AnyAsync/FirstAsync/SingleAsync, width conversion in ExecuteScalar<T>) are additive API and not defects; they are for later, the last one is documented as an exact cast.

Verification (gentle: BelowNormal, 2 cores)

The gate (verify-gate.ps1) on the merge, then the scans:

  • Solution build, no-incremental, EnforceCodeStyleInBuild=true: Release 0 warnings 0 errors; Debug 0 warnings 0 errors.
  • Tests: Release 9092 passed, Debug 9092 passed, 0 failed (master 8730, so 362 new).
  • Samples: 86 checks passed. DocFX: 0 warnings, 0 errors.
  • dotnet format style and analyzers, --severity info: 0 findings in all nine projects after one style commit. The gate found 9 in RuntimeParamSource (three field names that need a _ prefix, counted per target framework) and 2 in a test file; the three fields are properties now, and the rescan confirms 0 and 0 on Core and Core.Tests.
  • Sarif scan: 7 actionable, the same documented leftovers as master (six IDE0370 where the ! is needed in Release, one IDE0078 sample pattern); the merge had 7 more in the new code (naming, an unused using, a local function, a new() simplification, an xUnit serialization warning), all fixed. Roslynator: 0 findings in all ten projects (the merge had 25 in the new tests: 22 locals that never change are const now, two empty class bodies became ;, and one local stays a variable on purpose, with the reason next to it: a literal 0 converts to any enum, a variable does not).
  • LocalDB probe (all seven items against real SQL Server): 22 of 22 passed (before the fixes the late-error lines, 2a, 2b, 2c, 3, 4, 5 and 6 failed); the probe also confirms raw SqlClient throws a late error only with the default reader behavior.
  • Pre-push scan on the diff and this text: clean.

Not done / decisions for the owner

  • The older transform-based terminals (reader.Select(transform): First, Take, Skip, ToList(transform), IterateReader, FirstOrdinalResults, LoadTable, ...) are unchanged and some request SingleRow | SingleResult, which hides the late error from SqlClient. They need the same treatment if you want them covered; the notes say so.
  • The reader-level ToTargetBlock(reader) and ToChannel(reader) move the caller's reader to its end (the command-level calls are built on them); a later NextResult by the caller would skip a result set.
  • A Take(n) or First that stops with rows left does not drain them, so an error that follows many rows is not seen by those calls.
  • A typeless output parameter (AddParam("@o", ParameterDirection.Output)) fails in SqlClient ("invalid size of 0"), through the SQL package too: an output parameter needs a type (and a size for a string); the notes already say so. The two-argument AddParam("@i", DbType.Int32) still binds a value.
  • Name-lookup details to confirm: "a provider that does not implement the collection" is detected as NotSupportedException, ArgumentException, NotImplementedException, or an InvalidOperationException whose message mentions ProcedureParameters; the tokenizer treats a declare @x local like any @name, keeps the first spelling of a name that appears in two cases, and reads @a@b as two names; for a text command the no-match messages still say "the procedure declares".
  • PublicAPI.Unshipped.txt has three new override lines (the lookup on the two generic commands).
  • Three existing tests changed: they pinned "a text command whose SQL contains @Name sends the name as written", which is exactly what item 6 changes.
  • Memory per command (the consumer's second report) and the timing check of the new reader behavior are separate work: an allocation attribution agent is running; I will run the timing comparison (your yes needed) before this ships.

🤖 Generated with Claude Code

electricessence and others added 20 commits November 8, 2026 15:39
… source

SELECT 1; RAISERROR('late', 16, 1); completes with the row today: the read loops stop at the last row, and a provider reports an error that follows the rows only when the reader moves on.

LateErrorTests uses a reader whose NextResult (and NextResultAsync) throws, like a provider does for such an error, and asserts for every MappedQuery terminal (sync and async), the command-level Results and Retrieve, and the Channel and Dataflow pipelines that the error reaches the caller (and faults the target when complete is true); that First, Single and Take(n) read one more row to find the end of the rows and stop there when there is one; that a streaming handler that stops early is not drained; and that the reader-level helpers on a reader the caller holds leave it where it stopped. TypedReader counts NextResult calls and can be told to throw from it. Two RowMapperTests that counted the rows read now expect the one extra row.

101 of the 140 tests in LateErrorTests and RowMapperTests fail on the unfixed source.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…m 2 of the consumer feedback)

ExpressiveCommand and ExpressiveDbCommand never set the Direction of the provider parameter, so Output, InputOutput and ReturnValue parameters reach the database as inputs. These tests read the direction on the provider's parameter for all four directions, sync and async, a NULL-valued output parameter, and a mixed call; they fail until the fix.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…direction (item 2 of the consumer feedback)

AddParams on the generic commands never set the Direction of the provider parameter, so an Output, InputOutput or ReturnValue parameter reached the database as an input and a procedure with an output parameter failed with "expects parameter '@o', which was not supplied". The direction is now set, as the SQL Server command already did. Passes the tests added in the previous commit.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…failing: item 3 of the consumer feedback)

On the SQL Server commands the database type is SqlDbType, so AddParam("@o", DbType.Int32, ParameterDirection.Output) binds to the object overload and the enum becomes the VALUE of an output parameter. These tests, written for both packages through an extern alias, expect a typed parameter with no value; the four that need the new overload fail, and the three that pin the calls which must keep their meaning (a literal 0 above all) pass.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…parameter names through the ProcedureParameters schema collection

ExpressiveDbCommand, and ExpressiveCommand over a DbConnection, should find the real names (AddParam(orderId) as the declared order_id, a member the procedure
does not declare dropped from AddParams) through GetSchema("ProcedureParameters"): the restrictions asked for one-, two- and three-part names, the ordinal-0 return
value skipped, rows in ordinal order, an ambiguous name across schemas, a provider without the collection, a database error carrying the procedure name, and
the names remembered like the other lookups. The fake provider answers with a DataTable that has the columns of INFORMATION_SCHEMA.PARAMETERS. 24 of the 35 fail
until the lookup exists; the rest pin today's "not known" outcomes.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…L Server commands (item 3 of the consumer feedback)

ExpressiveSqlCommand in both SQL Server packages takes AddParam(string, DbType, ParameterDirection) and the AddParamIf twin, so AddParam("@o", DbType.Int32, ParameterDirection.Output) adds an Int output parameter with no value instead of an output parameter whose value is the enum. The DbType is converted to the SqlDbType the provider maps it to.

The direction has no default on purpose: an overload declared on the command hides the inherited ones wherever it applies, and a literal 0 converts to any enum, so with a default AddParam("@count", 0) would have bound to it and added a typed NULL. The tests of the previous commit pin that call, and the alias on the SQL Server package reference lets the test project reach the real commands of both packages.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…eader terminal

After the natural end of the rows (a Read returned false) every terminal now calls NextResult once (NextResultAsync when it reads asynchronously), which is what makes a provider raise an error that follows the rows. This covers the Map<T>() terminals (list, array, immutable array, dictionary, hash set, first, single, and the Read and ReadAsync handlers that enumerate to the end), Results and Retrieve on commands, and the Channel and Dataflow pipelines, where the error faults the target when complete is true. First, single and Take(n) read one more row after the wanted rows: no row is the end (NextResult), a row means stop there without reading the rest. A handler that stops early is not drained. Reader-level helpers on a reader the caller holds (Results(reader), ResultsBuffered(reader), ...) leave it where it stopped, so a caller may still read the next result set.

Measured with the consumer probe against SQL Server LocalDB: raw SqlClient with the default behavior and NextResult throws the error; with SingleResult|SequentialAccess, NextResult and Dispose complete without it; with SingleRow it is not raised either. The terminals therefore no longer request SingleResult (Map<T>() keeps SequentialAccess for generated mappings) or SingleRow. With that, every late-error line of the probe passes.

Cost: one NextResult call per query and, for first and take, one more Read; no allocation (the allocation tests are unchanged). mapping.md states the new behavior.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…em 4 of the consumer feedback)

A variable or a Func<T, object> result typed object has no members of its own, so AddParams(x) sent nothing and the procedure ran with every parameter NULL. These tests expect the members of the runtime type: an anonymous object, a Func result read at every call, different runtime types in a row, a struct, a type with no members, a null source, a throwing getter, and a ref-struct member skipped on that path. The static-type rule for every other type is pinned beside them.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…e type (item 4 of the consumer feedback)

A Func<T, object> result or an object variable has no members of its own, so AddParams sent nothing and the procedure ran with every parameter NULL, with no error. When the static type is exactly object, the public readable properties and fields of the runtime type are used (as Dapper does); every other static type keeps its own members. A non-generic table keeps one immutable entry per runtime type, with a compiled getter per member that takes the object, so no reflection runs after the first time a type is seen; the table is weak, so an unloaded type takes its entry with it. A null source still throws ArgumentNullException.

A member whose type cannot be boxed (a ref struct such as ReadOnlySpan<char>, or a pointer) made the static-type path throw TypeInitializationException for that type; the new path skips such members. The static-type path is unchanged. The AddParams remarks and the expressive-commands page say so.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…es through the ADO.NET schema collection

ExpressiveDbCommand (and ExpressiveCommand when the connection is a DbConnection) now override GetProcedureParameterNames and its async twin with a shared helper
that calls GetSchema("ProcedureParameters"): AddParam(orderId) finds @order_id and AddParams(new { ... }) drops the members the procedure does not declare on a
command made from an IDbConnectionFactory<DbConnection>, which SQL Server answers. The name is split into schema and name (brackets and quotes removed); a name with
a database part, an empty part, a provider without the collection, and a name found in more than one schema when none was given are "not known" (used as written).
A database error is thrown as it is with the procedure's name in Exception.Data["Procedure"]. The result goes through the same remembered-names path as the SQL
packages' lookup, which is unchanged. Checked on SQL Server LocalDB, also inside a transaction: it neither throws nor commits.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@name tokens

The tokenizer table first (strings and '' escapes, "" and [] identifiers with their escapes, line and block comments, @@System variables, names with digits, _, $ and #,
an @ with no name, an address-looking text inside a string, repeated names, names in another case), then derived names on a CommandType.Text command through
ExpressiveCommand, ExpressiveDbCommand and its async path: the four tiers, ambiguity, no match, a name only in a string or comment, a collision, no token (kept as written),
TableDirect and stored procedures untouched, AddParams dropping undeclared members, an explicit name winning. 85 of them fail until the tokenizer exists.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…em 5 of the consumer feedback)

Map<Dictionary<string, object?>>() and Map<ExpandoObject>() return one empty row and say nothing, because the types have a public parameterless constructor and no properties to set. These tests expect the InvalidOperationException a type that cannot be mapped gets, at the call, even for an empty result, on every entry point and for every way a type is a dictionary (IDictionary, IDictionary<,>, IReadOnlyDictionary<,>) or a dynamic object (IDynamicMetaObjectProvider); an interface keeps its own reason and a model that merely holds a dictionary still maps.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…() (item 5 of the consumer feedback)

Map<Dictionary<string, object?>>() and Map<ExpandoObject>() returned one empty row with no error: the types have a public parameterless constructor and no properties to set. They are not row models, so Shape.Judge now rejects any type assignable to IDictionary, to a closed IDictionary<,> or IReadOnlyDictionary<,>, or to IDynamicMetaObjectProvider, with an InvalidOperationException at the call, even for an empty result. An interface or an abstract type keeps its own reason, and a model that merely holds a dictionary still maps. The RowMap<T> remarks and the mapping page list the new case.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…its SQL text

For a command of CommandType.Text the "declared names" are now the @name tokens in the command text, matched with the same four tiers as the parameters of a stored
procedure (on ExpressiveCommand, ExpressiveDbCommand and ExpressiveSqlCommand, sync and async), so AddParam(regionId) on "... WHERE region_id = @region_id" sends
@region_id and AddParams(new { ... }) sends only the members the text uses. The internal tokenizer (SqlTextParameters) returns the distinct tokens in order of first
appearance (the same name in another case once) and skips single-quoted strings, double-quoted and [bracketed] identifiers, -- and /* */ comments and @@System
variables; only @ is recognised. The result is remembered per command text (at most 256 texts, then the cache starts again). A text with no @name token keeps the
argument's name as written, so nothing that works today changes; ambiguity and no match throw the same InvalidOperationException as for a procedure.
Three existing tests that pinned "a text command with @ in it uses the name as written" now use a text without a marker (or expect the marker). Docs: the
expressive-commands article, the AddParam/AddParams XML remarks and the MSSqlClient README line.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…nge instead of Substring, CA2249 Contains where the target has it)

The full gate listed both at --severity info after the two items; no behavior change.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
… the new code (naming, an unused using, a local function, const locals, class bodies, one dismissal with its reason)

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…unmappable list and the limits

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@electricessence

Copy link
Copy Markdown
Collaborator Author

Included in #85, which now targets master and contains these commits plus the later fixes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant