Commit bd6ee1e
committed
fix(ogar-adapter-surrealql): quote non-bare identifiers in emitted DDL
Closes Codex P2 on PR #33 (now merged).
# The bug
When `surrealql-hint` is enabled (PR #33), `register_class_knowable_from`
renders the schema DDL for every registered Class via
`emit_surrealql_ddl`. For Odoo-style class names — which legitimately
use the dotted form (`sale.order`, `account.move.line`,
`res.partner`) per `ogar-ontology::class_identity` — the emitter was
producing raw `DEFINE TABLE sale.order …`. That's invalid SurrealQL:
the dot terminates the bare identifier, so the parser either rejects
the DDL or mis-parses the dot as a path operator.
Codex flagged this on PR #33 line 272: "enabling this feature for
Odoo-style classes stores an invalid schema hint instead of a self-
describing registry entry".
# The fix
New helper in `ogar-adapter-surrealql`:
fn surrealql_ident(name: &str) -> String
Returns the bare identifier if `name` matches `[A-Za-z_][A-Za-z0-9_]*`,
or backtick-quoted otherwise (with embedded backticks doubled per
SurrealQL convention). Examples:
surrealql_ident("widget") -> "widget"
surrealql_ident("sale.order") -> "`sale.order`"
surrealql_ident("account.move.line") -> "`account.move.line`"
surrealql_ident("kebab-case") -> "`kebab-case`"
surrealql_ident("weird`name") -> "`weird``name`"
Applied at every identifier-emission site:
- `emit_class` — DEFINE TABLE <name> (once at the top; `class.name`
pre-quoted into `table_ident` and reused as `table` parameter for
the field emitters).
- `emit_field_attr` — DEFINE FIELD <name> ON <table>; quotes
`attr.name` locally.
- `emit_field_assoc` — DEFINE FIELD <name> ON <table> TYPE
record<<target>>; quotes `assoc.name` + record target locally
(so `record<\`res.partner\`>` lands correctly).
- `emit_field_enum` — DEFINE FIELD <column> ON <table> TYPE …;
quotes `enum_decl.column` locally.
The comment-marker line for non-owning sides (`-- {table} HasMany …`)
is left un-quoted — it's not parsed; readable text is more useful.
# Walker side is automatically correct
`parse_surrealql_ddl` extracts identifiers via
`Expr::Path { start: Ident { text }, parts: None }` -> `ast[ident.text]`.
The SurrealDB parser unwraps backtick-quoting at the lexer level, so
the walker yields `"sale.order"` (unquoted text) whether the DDL had
backticks or not. Round-trip works without walker changes — verified
by the new round-trip tests.
# Tests added (8 new; 20 default + 33 with parser feature)
Helper unit tests:
- `bare_identifier_passes_through_unquoted`
- `non_bare_identifier_gets_backtick_quoted` (covers `sale.order`,
`account.move.line`, `res.partner`, kebab-case, leading digit,
space)
- `embedded_backticks_get_doubled`
- `empty_string_is_not_a_bare_identifier`
Emit-site tests:
- `emit_class_with_odoo_dotted_name_quotes_the_table` — asserts
both DEFINE TABLE and DEFINE FIELD ON clauses use the quoted form.
- `emit_class_with_odoo_belongs_to_quotes_record_target` — asserts
`record<\`res.partner\`>` lands correctly.
Round-trip (feature-on) tests — the load-bearing ones:
- `round_trip_odoo_dotted_table_name` — builds IR with
`sale.order` + belongs_to → `res.partner`, emits, parses,
asserts the IR survives the trip with names byte-identical.
- `round_trip_three_segment_odoo_name` — same for
`account.move.line`.
# Verification
cargo test -p ogar-adapter-surrealql -> 20/20
cargo test -p ogar-adapter-surrealql --features surrealdb-parser -> 33/33
cargo test -p ogar-knowable-from --features surrealql-hint -> 10/10
cargo check --workspace --all-targets -> clean
PII abort-guard (word-boundary): CLEAN.
# Note on scope
This is a fix for the merged PR #33 — adding the missing identifier
quoting at the emit layer. Same change-policy as ADR-023 (the IR is
fixed; adapter emit logic is where source-dialect concerns get
absorbed). No `Class` IR change; no adapter signature change;
existing call sites unaffected (all the bare identifiers they use
pass through `surrealql_ident` unchanged).
https://claude.ai/code/session_01PBTGaPCSnnt6u3pjXpbLwY1 parent 3506751 commit bd6ee1e
1 file changed
Lines changed: 179 additions & 14 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
499 | 499 | | |
500 | 500 | | |
501 | 501 | | |
502 | | - | |
| 502 | + | |
| 503 | + | |
503 | 504 | | |
504 | 505 | | |
505 | 506 | | |
506 | 507 | | |
507 | 508 | | |
508 | 509 | | |
509 | 510 | | |
510 | | - | |
| 511 | + | |
511 | 512 | | |
512 | 513 | | |
513 | 514 | | |
514 | | - | |
| 515 | + | |
515 | 516 | | |
516 | 517 | | |
517 | 518 | | |
518 | | - | |
| 519 | + | |
519 | 520 | | |
520 | 521 | | |
521 | 522 | | |
| 523 | + | |
| 524 | + | |
| 525 | + | |
| 526 | + | |
522 | 527 | | |
523 | 528 | | |
524 | 529 | | |
| |||
537 | 542 | | |
538 | 543 | | |
539 | 544 | | |
540 | | - | |
| 545 | + | |
| 546 | + | |
| 547 | + | |
541 | 548 | | |
542 | 549 | | |
543 | 550 | | |
| |||
552 | 559 | | |
553 | 560 | | |
554 | 561 | | |
| 562 | + | |
| 563 | + | |
| 564 | + | |
555 | 565 | | |
556 | | - | |
| 566 | + | |
557 | 567 | | |
558 | | - | |
| 568 | + | |
559 | 569 | | |
560 | 570 | | |
561 | 571 | | |
562 | | - | |
| 572 | + | |
| 573 | + | |
| 574 | + | |
563 | 575 | | |
564 | 576 | | |
565 | 577 | | |
566 | 578 | | |
567 | 579 | | |
568 | 580 | | |
| 581 | + | |
| 582 | + | |
569 | 583 | | |
570 | 584 | | |
571 | 585 | | |
| |||
582 | 596 | | |
583 | 597 | | |
584 | 598 | | |
| 599 | + | |
585 | 600 | | |
586 | 601 | | |
587 | 602 | | |
| |||
590 | 605 | | |
591 | 606 | | |
592 | 607 | | |
593 | | - | |
594 | | - | |
| 608 | + | |
595 | 609 | | |
596 | 610 | | |
597 | 611 | | |
| |||
603 | 617 | | |
604 | 618 | | |
605 | 619 | | |
606 | | - | |
607 | | - | |
| 620 | + | |
608 | 621 | | |
609 | 622 | | |
610 | 623 | | |
| |||
613 | 626 | | |
614 | 627 | | |
615 | 628 | | |
616 | | - | |
617 | | - | |
| 629 | + | |
618 | 630 | | |
619 | 631 | | |
620 | 632 | | |
| |||
655 | 667 | | |
656 | 668 | | |
657 | 669 | | |
| 670 | + | |
| 671 | + | |
| 672 | + | |
| 673 | + | |
| 674 | + | |
| 675 | + | |
| 676 | + | |
| 677 | + | |
| 678 | + | |
| 679 | + | |
| 680 | + | |
| 681 | + | |
| 682 | + | |
| 683 | + | |
| 684 | + | |
| 685 | + | |
| 686 | + | |
| 687 | + | |
| 688 | + | |
| 689 | + | |
| 690 | + | |
| 691 | + | |
| 692 | + | |
| 693 | + | |
| 694 | + | |
| 695 | + | |
| 696 | + | |
| 697 | + | |
| 698 | + | |
| 699 | + | |
| 700 | + | |
| 701 | + | |
| 702 | + | |
658 | 703 | | |
659 | 704 | | |
660 | 705 | | |
| |||
1083 | 1128 | | |
1084 | 1129 | | |
1085 | 1130 | | |
| 1131 | + | |
| 1132 | + | |
| 1133 | + | |
| 1134 | + | |
| 1135 | + | |
| 1136 | + | |
| 1137 | + | |
| 1138 | + | |
| 1139 | + | |
| 1140 | + | |
| 1141 | + | |
| 1142 | + | |
| 1143 | + | |
| 1144 | + | |
| 1145 | + | |
| 1146 | + | |
| 1147 | + | |
| 1148 | + | |
| 1149 | + | |
| 1150 | + | |
| 1151 | + | |
| 1152 | + | |
| 1153 | + | |
| 1154 | + | |
| 1155 | + | |
| 1156 | + | |
| 1157 | + | |
| 1158 | + | |
| 1159 | + | |
| 1160 | + | |
| 1161 | + | |
| 1162 | + | |
| 1163 | + | |
| 1164 | + | |
| 1165 | + | |
| 1166 | + | |
| 1167 | + | |
| 1168 | + | |
| 1169 | + | |
| 1170 | + | |
| 1171 | + | |
| 1172 | + | |
| 1173 | + | |
| 1174 | + | |
| 1175 | + | |
| 1176 | + | |
| 1177 | + | |
| 1178 | + | |
| 1179 | + | |
| 1180 | + | |
| 1181 | + | |
| 1182 | + | |
| 1183 | + | |
| 1184 | + | |
| 1185 | + | |
| 1186 | + | |
| 1187 | + | |
| 1188 | + | |
| 1189 | + | |
| 1190 | + | |
| 1191 | + | |
| 1192 | + | |
| 1193 | + | |
| 1194 | + | |
| 1195 | + | |
| 1196 | + | |
| 1197 | + | |
| 1198 | + | |
| 1199 | + | |
| 1200 | + | |
| 1201 | + | |
| 1202 | + | |
| 1203 | + | |
| 1204 | + | |
| 1205 | + | |
| 1206 | + | |
| 1207 | + | |
| 1208 | + | |
| 1209 | + | |
| 1210 | + | |
| 1211 | + | |
| 1212 | + | |
| 1213 | + | |
| 1214 | + | |
| 1215 | + | |
| 1216 | + | |
| 1217 | + | |
| 1218 | + | |
| 1219 | + | |
| 1220 | + | |
| 1221 | + | |
| 1222 | + | |
| 1223 | + | |
| 1224 | + | |
| 1225 | + | |
| 1226 | + | |
| 1227 | + | |
| 1228 | + | |
| 1229 | + | |
| 1230 | + | |
| 1231 | + | |
| 1232 | + | |
| 1233 | + | |
| 1234 | + | |
| 1235 | + | |
| 1236 | + | |
| 1237 | + | |
| 1238 | + | |
| 1239 | + | |
| 1240 | + | |
| 1241 | + | |
| 1242 | + | |
| 1243 | + | |
| 1244 | + | |
| 1245 | + | |
| 1246 | + | |
| 1247 | + | |
| 1248 | + | |
| 1249 | + | |
| 1250 | + | |
1086 | 1251 | | |
0 commit comments