Skip to content

Defer query deserialization until the first row - #2285

Open
tuncaypeker wants to merge 1 commit into
DapperLib:mainfrom
tuncaypeker:fix/empty-query-deserializer
Open

tuncaypeker wants to merge 1 commit into
DapperLib:mainfrom
tuncaypeker:fix/empty-query-deserializer

Conversation

@tuncaypeker

Copy link
Copy Markdown
Contributor

Fixes #1836.

Query and QueryAsync create a deserializer even when the result is empty. SQLite can report a BLOB column as byte[] for an empty result while returning string values when rows exist. A model with a matching string constructor therefore throws a missing-constructor exception only when there are no rows.

Read the first row before building or validating the deserializer in Query, QueryAsync, and QueryUnbufferedAsync. Process that row with a do/while loop, keep draining subsequent results, and preserve completion callbacks for empty and zero-column results. QueryAsync's unbuffered iterator owns its command as well as its reader until enumeration finishes, so providers such as SQLite do not close the reader before it can be consumed. GridReader mapping is outside this change.

Validation:

  • Reproduced on unmodified upstream main b5e17f9 on net10.0 using a real Microsoft.Data.Sqlite in-memory database: sync and async buffered queries throw for an empty BLOB result; two populated-result controls pass.
  • New SQLite cases cover empty/single/multiple/empty/populated sequences with the same query and model, buffered and unbuffered sync/async APIs, QueryUnbufferedAsync on modern targets, initially open/closed connections, and early disposal. Populated BLOB values incompatible with the string constructor still throw.
  • SQL Server checks with both SqlClient providers verify that an empty first result does not suppress a later SQL exception.
  • The final new cases comprise 22 cases on net481 and 28 each on net8.0 and net10.0; all pass.
  • The new tests plus existing AsyncTests, ProcedureTests (including zero-column and empty-result output callbacks), SQLite, ConstructorTests, MiscTests, TypeMappingRegressionTests, and ColumnErrorDiagnosticTests pass across net481, net8.0, and net10.0. The net8.0/net10.0 selections each have 496 passed / 14 skipped. Skips are existing timeout/performance cases and the known Microsoft.Data.SqlClient pipelining issue Async Pipelined execution does not work with Microsoft.Data.SqlClient. #2028. No failures.
  • The full repository test suite was not run.
  • Release library build succeeds for net461, netstandard2.0, net8.0, and net10.0 with zero warnings or errors.

This branch has not been deployed

No deployments
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.

Empty vs. non-empty query results leads to a lookup of different constructors

1 participant