writeonce/tests/corpus/run/db-query-relations/fixture.wo
shoney.arickathil 8817fdec2a feat(compiler): ref + backlink navigation in queries (9b cont.)
- `backlink C.f` field type: parsed, typed as `multi C`, and VIRTUAL —
  filtered out of the stored row layout (no column, omittable in
  ctor/insert), collected in clsrec.cr_backlinks
- reading a backlink (`d.staff`) lowers to DB_PROBE on the source
  class's index for the backing column (backlink_target resolves the
  (source class, index number); a backlink with no backing index has
  no efficient read)
- `ref C` navigation (`e.dept.name`) chains: a ref value is the target
  row's id, so a `Ref C` base navigates into C's fields exactly like a
  table-class value, routing to DB_GET_FIELD both in typecheck and emit
- query navigation source `from s in d.staff`: emit_query evaluates the
  nav expr to get its id-list instead of DB_SCAN; QNav typechecks with
  the range var bound to the navigation's element class
- fixture run/db-query-relations proves both directions; oop-e2e 75/0,
  woc-test 566/0, log-watcher 7/0
- still ahead for employee: order/take, group-by aggregates, projection
  records, delete + update-through-row

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-16 05:10:13 +02:00

30 lines
875 B
Text

-- iteration 9b: ref + backlink navigation. `e.dept.name` chains a ref to
-- its target row (a point read); `d.staff` reads a backlink by probing the
-- source class's index — both execute in the engine, no join written.
@table(name: "dept", index: [name])
class Dept {
name: Text
staff: backlink Emp.dept
}
@table(name: "emp", index: [dept])
class Emp {
name: Text
dept: ref Dept
}
fn main() {
let eng = insert Dept { name: "eng" }
let ops = insert Dept { name: "ops" }
insert Emp { name: "asha", dept: eng }
insert Emp { name: "bram", dept: eng }
insert Emp { name: "dora", dept: ops }
print("-- forward (e.dept.name):")
for e in from x in Emp select x {
print(e.dept.name)
}
print("-- backward (d.staff):")
for d in from x in Dept where x.name == "eng" select x {
for s in from y in d.staff select y {
print(s.name)
}
}
}