Query model.db from your own tool
model.db is the workbench: a SQLite cache of one model.jsonl, indexed so you
can ask your own questions without writing a program. This guide gets you to a
useful query with codegraph query, then with any other SQLite client, and
states the four rules a query must respect.
Before you start: a model.jsonl. Nothing else for codegraph query; for
another client, anything that speaks SQLite.
Ask with codegraph query
Give it the model and one statement:
$ codegraph query fixtures/java/expected/model.jsonl \
"SELECT n.id, count(*) AS fan_in FROM dep d JOIN node n ON n.ref = d.to_ref
GROUP BY d.to_ref ORDER BY fan_in DESC, n.id" --limit 3
cache: fixtures/java/expected/model.db
id fan_in
------------------------- ------
java:com.acme.order/Money 16
java:java.lang/String 16
java:com.acme.order/Order 7
3 of 69 rows shown — --all prints every row, --limit N another count.
It builds model.db beside the model when it is missing or stale, and the two
views of the next section, node and dep, are already there: entities by
their ids, edges with their kinds, provenance and anchors. A value goes in as a
bound parameter, never pasted into the text, so an id needs no quoting:
$ codegraph query fixtures/java/expected/model.jsonl \
"SELECT d.kind, d.provenance, d.file, d.line FROM dep d JOIN node n ON n.ref = d.to_ref
WHERE n.id = :id ORDER BY d.file, d.line" --param id='java:com.acme.order/Money' --limit 3
cache: fixtures/java/expected/model.db
kind provenance file line
--------- ---------- --------------------------------- ----
reference declared com/acme/order/AbstractOrder.java 6
reference declared com/acme/order/AbstractOrder.java 20
reference declared com/acme/order/Batch.java 24
3 of 16 rows shown — --all prints every row, --limit N another count.
--file q.sql reads the statement from a file, --csv and --json print the
rows for another program, and --param-file rows=saved.json hands one query’s
saved --json result to the next. It never writes: the store is opened
read-only and the text must be one statement that reads, so a DELETE, a
VACUUM INTO or a second statement after a ; stops with exit 2. Every option
is on the query reference.
From another SQLite client
codegraph import model.jsonl # writes model.db beside it
sqlite3 model.db
datasette model.db
duckdb -c "ATTACH 'model.db' AS cg (TYPE sqlite); SELECT count(*) FROM cg.entity;"Nothing you do here can hurt anything: delete the file and the next command
rebuilds it. --out FILE puts the store somewhere else; --no-cache on an
analysis command reads the JSONL directly and never touches a store.
Rows reference entities by integer surrogate and vocabularies by id. These two
temp views turn both into the ids and names you recognise, and every recipe below
assumes them. codegraph query --views prints them, to paste or to pipe into
sqlite3:
CREATE TEMP VIEW node AS
SELECT e.id AS ref,
(SELECT value FROM meta WHERE key = 'lang') || ':' || m.symbol
-- no '/' for a module (it names itself) nor for an empty symbol (an anonymous function)
|| CASE WHEN e.id = e.module_id OR e.symbol = '' THEN '' ELSE '/' || e.symbol END
|| CASE WHEN e.disambiguator IS NULL THEN '' ELSE '#' || e.disambiguator END
AS id,
k.name AS kind,
e.name AS name,
-- `IS 1`, not `= 1`: a non-stubbable entity has NULL here, and `= 1`
-- would make every comparison NULL and quietly drop every method.
e.is_stub IS 1 AS is_stub,
e.module_id AS module_ref,
e.parent_id AS parent_ref,
e.trait_set_id AS trait_set_id
FROM entity e
JOIN entity m ON m.id = e.module_id
JOIN kind k ON k.id = e.kind_id;CREATE TEMP VIEW dep AS
SELECT x.id AS ref,
x.from_id AS from_ref,
x.to_id AS to_ref,
ek.name AS kind,
p.name AS provenance,
f.path AS file,
x.anchor_start AS line,
x.member AS member
FROM edge x
JOIN edge_kind ek ON ek.id = x.kind_id
JOIN provenance p ON p.id = x.provenance_id
JOIN file f ON f.id = x.anchor_file_id;The tables to start from
| Table | Holds |
|---|---|
entity | one row per entity: kind_id, trait_set_id, module_id, symbol, disambiguator, parent_id, declared_type_id, is_stub, the anchor |
edge | one row per edge, in the direction the model states: from_id, to_id, kind_id, provenance_id, the anchor, and member: the member of a stub target the edge reaches, as written (insert/2, save), NULL when it names none |
kind, trait, edge_kind, provenance, file | the interned vocabularies; ids are the header dictionary’s own indices |
trait_set, trait_set_member | trait sets, interned — an entity points at a set, not at a list of traits |
entity_metric | one row per (entity, measure), the values the extractor measured |
meta | lang, dbVersion, and the store’s own bookkeeping |
Four queries that pay for themselves
The corpus at a glance:
SELECT (SELECT value FROM meta WHERE key = 'lang') AS lang,
(SELECT count(*) FROM entity) AS entities,
(SELECT count(*) FROM entity WHERE is_stub = 1) AS stubs,
(SELECT count(*) FROM edge) AS edges,
(SELECT count(*) FROM file) AS files;| lang | entities | stubs | edges | files |
|---|---|---|---|---|
| java | 179 | 27 | 190 | 16 |
Fan-in — what everything depends on. edge_to is an index over the stored
outgoing facts, so this is a GROUP BY and it can never disagree with the
outgoing direction:
SELECT n.id, count(*) AS fan_in
FROM dep d JOIN node n ON n.ref = d.to_ref
GROUP BY d.to_ref
ORDER BY fan_in DESC, n.id
LIMIT 5;| id | fan_in |
|---|---|
java:com.acme.order/Money | 16 |
java:java.lang/String | 16 |
java:com.megacorp.ledger/LedgerClient | 8 |
java:java.util/List | 8 |
java:com.acme.order/Order | 7 |
How much of the graph is inference — worth running once on any corpus:
SELECT d.provenance, d.kind, count(*) AS n
FROM dep d
WHERE d.provenance <> 'declared'
GROUP BY d.provenance, d.kind
ORDER BY n DESC, d.provenance, d.kind;| provenance | kind | n |
|---|---|---|
| derived | import | 3 |
| derived | invocation | 1 |
Three derived imports and one derived call (super.post in LedgerAdapter,
charged to the one library ancestor that could answer) out of 190 edges: on this
corpus almost everything is a fact the extractor read.
The most complex invocables:
SELECT n.id, max(CASE WHEN em.key = 'cyclomatic' THEN em.value END) AS cc
FROM entity_metric em
JOIN node n ON n.ref = em.entity_id
GROUP BY em.entity_id
HAVING cc IS NOT NULL
ORDER BY cc DESC, n.id
LIMIT 5;| id | cc |
|---|---|
java:com.acme.order/Reporting.max(java.util.List) | 4 |
java:com.acme.order/StockGuard.ensure(int) | 3 |
java:com.acme.order.legacy/List.length() | 2 |
Absent is not zero. An entity with no row for a key was not measured — a
stub, or a kind the extractor does not measure. sum() over a missing key
returns NULL, and turning that into 0 would state a fact about code nobody
read.
Four rules
Sorting belongs to the model, never to the engine. Codegraph orders ids by
UTF-16 code unit; SQLite’s BINARY collation orders by UTF-8 byte. They agree
across the Basic Multilingual Plane and invert above it. If your query feeds
something that cares about order, sort it in your own code with the same
comparison, or ORDER BY the surrogate, which the importer wrote in canonical
order.
Never store a surrogate. entity.id is a position in one file. It is not
stable across runs, it means nothing in another model, and writing one into a
spreadsheet is how a number outlives its meaning. Identity is the natural key —
(lang, module, symbol, disambiguator), with lang in meta and the rest on
entity.
Provenance or it did not happen. A count that mixes declared with derived
is not a measurement of the code, it is a measurement of the code plus the
extractor’s guesses. Say which you meant.
The store is a cache, not a source. Nothing but the importer writes to it;
codegraph query cannot. If you want a derived table in another client, make it
a TEMP one — the next codegraph run may
regenerate the file underneath you, and it will not ask.
When to ask codegraph instead
Use the CLI when you need the fold exactly (the parent-chain walk and its view
semantics), ranked strongly connected components with the edges closing each
cycle, coupling tables that state their level and view, or anything you intend to
commit. A query result is a look; the .jsonl and the analyzer’s exports are the
reproducible artefacts.