Commit 81df3dc
fix(DataMapper,ddl2cpp): address code-review findings on composite FKs and relation generation
Ten findings from a review of the composite-foreign-key and ddl2cpp relation work.
Correctness:
- QuerySingle(primaryKeys...) and one FirstImpl() overload each had an early
`return std::nullopt;` ahead of their final `return record;`, defeating NRVO
and risking a stale pointer in the composite-FK auto-loader. Restructured
both to a single return statement, and - since CI proved NRVO still isn't
reliable for the fuller function body under some build configurations
(caught by the coverage build; see below) - additionally switched the
composite-FK auto-loader to capture the foreign key values by value at
configure time instead of a pointer to the owning record, matching the
safe pattern HasMany/BelongsTo already use.
- ddl2cpp's join-table cardinality check used the *far* key's uniqueness
instead of the *owner* key's - the far key always references exactly one
row, so that check was trivially always true. Fixed, and corrected the test
that had locked in the backwards expectation, plus the doc wording.
- The `PrimaryKey::AutoAssign` collision counter only recognized
`value + 1`-style keys, so two auto-assigned SqlGuid keys bypassed the
static_assert meant to reject multi-column auto-assignment.
- CompositeForeignKey had no operator==/<=>, so it was neither
equality_comparable nor (owing to its private members) an aggregate -
Reflection::CollectDifferences hard-errors on any record holding one.
Added comparison operators, matching HasMany/HasOneThrough/HasManyThrough.
- GetPrimaryKeyField() kept overwriting its result for every matching-type
primary key member, silently returning the last one instead of the first.
Cleanup:
- Deduplicated the eager (LoadCompositeForeignKey) and lazy
(ConfigureRelationAutoLoading) composite-FK load paths into one
LoadCompositeForeignKeyRecord() helper, now taking the already-permuted key
tuple rather than the owning record (see the value-capture fix above).
- Merged the near-identical HasOneThrough/HasManyThrough codegen branches in
CxxModelPrinter, driven by the template name string.
- Replaced two C-style index loops in CompositeForeignKey with views::iota.
- Rebuilt GetPrimaryKeyFields() on the same compile-time tuple_cat machinery
RecordPrimaryKeyTupleHelper already uses, dropping an O(K^2) runtime scan.
- Replaced byName()'s linear table scan in PlanRelations with a hash map.
CI fixups (found via `gh pr checks` + fetched logs, all within this PR's own
diff against master - none pre-exist there):
- clang-tidy: an unchecked-optional-access and a dead-store finding in tests,
and PlanRelations exceeding the cognitive-complexity threshold (fixed by the
CxxModelPrinter dedup above).
- Doxygen coverage: wrapped a decltype-of-invoked-generic-lambda type alias
that at least one Doxygen version misparses in `\cond`, replaced a few
`\ref`s Doxygen couldn't resolve with plain code font, and documented
`PlannedRelation::kind` and `CompositeForeignKey::Loader`'s members (the
latter newly required once the struct itself gained a doc comment).
- Regenerated the `src/examples/test_chinook/entities/*.hpp` golden reference
files against a live MS SQL Server + Chinook dataset: they had never been
updated after ddl2cpp's relation-generation feature landed earlier in this
branch, so every file was missing the HasMany/HasManyThrough members and
forward declarations the generator has produced all along.
- A real ddl2cpp bug this regeneration surfaced: a relation member can end up
named exactly like its own referenced (forward-declared) struct - e.g.
`Light::HasMany<Album> Album;` inside `struct Artist`. Legal C++, but GCC's
-Wchanges-meaning rejects it under -Werror once a real GCC build (not just
Clang) actually compiled the regenerated headers. Fixed by reserving
referenced/through struct names in the same per-table uniquing map used for
column/member names, so a colliding relation member gets suffixed instead;
added a regression test.
Also fixed, unrelated to the review but required for a local build under this
toolchain: two dependent-name lookups needing an explicit `template` keyword,
and a spurious unused-lambda-capture warning-as-error.
Left alone (pre-existing on this branch before this change, or infrastructure):
docs/sqlquery.md's two Doxygen markdown-list warnings (present on master too);
the C++26 reflection build's composite-FK errors (predate this change, need
reflection-mode expertise); a one-off local Postgres-container anomaly in an
unrelated self-referencing-relation test (CI's own PostgreSQL leg passes).
Verified: clang-debug (ASan/UBSan) against SQLite3, MS SQL Server 2022, and
PostgreSQL (all three via Docker where applicable) - full suite green;
additionally reproduced and re-verified the composite-FK regression test
under a `clang-coverage`-instrumented build, which is what first caught the
NRVO gap above; ddl2cpp/chinook regenerated and rebuilt against a live MSSQL
Chinook dataset with zero diff against the golden files. GCC and the
C++20-modules/C++26-reflection configurations were not available in this
environment (verified only that the C++26 reflection failures are unchanged
from before this push).
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>1 parent 3f0daa8 commit 81df3dc
18 files changed
Lines changed: 452 additions & 149 deletions
File tree
- docs
- src
- Lightweight
- DataMapper
- Tools
- examples/test_chinook/entities
- tests
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
113 | 113 | | |
114 | 114 | | |
115 | 115 | | |
116 | | - | |
117 | | - | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
118 | 121 | | |
119 | 122 | | |
120 | 123 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
| 15 | + | |
15 | 16 | | |
16 | 17 | | |
17 | 18 | | |
| |||
139 | 140 | | |
140 | 141 | | |
141 | 142 | | |
142 | | - | |
| 143 | + | |
143 | 144 | | |
144 | 145 | | |
145 | 146 | | |
146 | 147 | | |
147 | 148 | | |
148 | 149 | | |
149 | 150 | | |
150 | | - | |
| 151 | + | |
151 | 152 | | |
152 | 153 | | |
153 | 154 | | |
| |||
160 | 161 | | |
161 | 162 | | |
162 | 163 | | |
163 | | - | |
| 164 | + | |
164 | 165 | | |
165 | 166 | | |
166 | 167 | | |
| |||
228 | 229 | | |
229 | 230 | | |
230 | 231 | | |
231 | | - | |
232 | | - | |
| 232 | + | |
| 233 | + | |
233 | 234 | | |
234 | 235 | | |
235 | 236 | | |
| |||
257 | 258 | | |
258 | 259 | | |
259 | 260 | | |
260 | | - | |
| 261 | + | |
261 | 262 | | |
262 | 263 | | |
263 | 264 | | |
| |||
384 | 385 | | |
385 | 386 | | |
386 | 387 | | |
| 388 | + | |
387 | 389 | | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
388 | 407 | | |
389 | 408 | | |
390 | 409 | | |
| |||
395 | 414 | | |
396 | 415 | | |
397 | 416 | | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
398 | 427 | | |
399 | 428 | | |
400 | 429 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
622 | 622 | | |
623 | 623 | | |
624 | 624 | | |
| 625 | + | |
| 626 | + | |
| 627 | + | |
| 628 | + | |
| 629 | + | |
| 630 | + | |
| 631 | + | |
| 632 | + | |
| 633 | + | |
| 634 | + | |
| 635 | + | |
| 636 | + | |
| 637 | + | |
| 638 | + | |
| 639 | + | |
| 640 | + | |
| 641 | + | |
| 642 | + | |
| 643 | + | |
625 | 644 | | |
626 | 645 | | |
627 | 646 | | |
| |||
1112 | 1131 | | |
1113 | 1132 | | |
1114 | 1133 | | |
1115 | | - | |
| 1134 | + | |
1116 | 1135 | | |
1117 | 1136 | | |
1118 | 1137 | | |
| |||
1217 | 1236 | | |
1218 | 1237 | | |
1219 | 1238 | | |
1220 | | - | |
| 1239 | + | |
1221 | 1240 | | |
1222 | 1241 | | |
1223 | 1242 | | |
| |||
1338 | 1357 | | |
1339 | 1358 | | |
1340 | 1359 | | |
1341 | | - | |
1342 | | - | |
1343 | | - | |
| 1360 | + | |
| 1361 | + | |
| 1362 | + | |
| 1363 | + | |
| 1364 | + | |
| 1365 | + | |
| 1366 | + | |
1344 | 1367 | | |
1345 | | - | |
1346 | | - | |
1347 | | - | |
| 1368 | + | |
| 1369 | + | |
| 1370 | + | |
| 1371 | + | |
| 1372 | + | |
1348 | 1373 | | |
1349 | | - | |
1350 | | - | |
| 1374 | + | |
| 1375 | + | |
| 1376 | + | |
| 1377 | + | |
| 1378 | + | |
| 1379 | + | |
| 1380 | + | |
1351 | 1381 | | |
1352 | 1382 | | |
1353 | 1383 | | |
| |||
2177 | 2207 | | |
2178 | 2208 | | |
2179 | 2209 | | |
| 2210 | + | |
| 2211 | + | |
| 2212 | + | |
| 2213 | + | |
2180 | 2214 | | |
2181 | | - | |
2182 | | - | |
2183 | | - | |
2184 | | - | |
| 2215 | + | |
| 2216 | + | |
2185 | 2217 | | |
2186 | 2218 | | |
2187 | | - | |
2188 | | - | |
2189 | | - | |
| 2219 | + | |
2190 | 2220 | | |
2191 | 2221 | | |
| 2222 | + | |
| 2223 | + | |
| 2224 | + | |
| 2225 | + | |
2192 | 2226 | | |
2193 | 2227 | | |
2194 | 2228 | | |
| |||
2484 | 2518 | | |
2485 | 2519 | | |
2486 | 2520 | | |
| 2521 | + | |
| 2522 | + | |
| 2523 | + | |
| 2524 | + | |
| 2525 | + | |
| 2526 | + | |
| 2527 | + | |
| 2528 | + | |
| 2529 | + | |
| 2530 | + | |
| 2531 | + | |
| 2532 | + | |
| 2533 | + | |
2487 | 2534 | | |
2488 | 2535 | | |
2489 | 2536 | | |
| |||
2496 | 2543 | | |
2497 | 2544 | | |
2498 | 2545 | | |
2499 | | - | |
2500 | | - | |
| 2546 | + | |
2501 | 2547 | | |
2502 | 2548 | | |
2503 | 2549 | | |
| |||
2509 | 2555 | | |
2510 | 2556 | | |
2511 | 2557 | | |
2512 | | - | |
| 2558 | + | |
2513 | 2559 | | |
2514 | 2560 | | |
2515 | 2561 | | |
| |||
2930 | 2976 | | |
2931 | 2977 | | |
2932 | 2978 | | |
2933 | | - | |
2934 | | - | |
2935 | | - | |
2936 | | - | |
2937 | | - | |
2938 | | - | |
2939 | | - | |
2940 | | - | |
2941 | | - | |
2942 | | - | |
| 2979 | + | |
| 2980 | + | |
| 2981 | + | |
| 2982 | + | |
| 2983 | + | |
| 2984 | + | |
| 2985 | + | |
| 2986 | + | |
2943 | 2987 | | |
2944 | 2988 | | |
2945 | 2989 | | |
2946 | 2990 | | |
2947 | 2991 | | |
2948 | 2992 | | |
2949 | 2993 | | |
2950 | | - | |
| 2994 | + | |
2951 | 2995 | | |
2952 | | - | |
2953 | | - | |
2954 | | - | |
2955 | | - | |
2956 | | - | |
| 2996 | + | |
2957 | 2997 | | |
2958 | 2998 | | |
2959 | 2999 | | |
| |||
0 commit comments