Skip to content

Commit 8098d37

Browse files
kyleconroyclaude
andcommitted
core: generate catalog accessors with sqlc (sqlc building sqlc)
Introduce internal/core/catalogdb, a sqlc-generated SQLite query layer over the core catalog's own sql_* tables — sqlc analyzing sqlc's catalog. The catalog's schema.sql is the sqlc schema; catalogdb/query.sql holds the queries; //go:generate runs sqlc against them. Generation is offline and the output is committed, so there is no build-time cycle, and because the SQLite engine runs on the legacy compiler (not the core catalog) there is no analysis recursion. Vertical slice: types.go (CreateType/TypeOID/TypeName/LookupType) now delegates to the generated Queries. FindProcs is split into two generated queries — FindProcsAnyNamespace and FindProcsInNamespaces (sqlc.slice) — with Go choosing based on whether namespaces were supplied, replacing the hand-built IN-list. Remaining core files still use raw SQL and will be swept over incrementally. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XTGxNHW6v1S1YyC9FDSgrK
1 parent 123979e commit 8098d37

8 files changed

Lines changed: 504 additions & 72 deletions

File tree

‎internal/core/catalog.go‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,30 @@ import (
1111
_ "embed"
1212
"fmt"
1313

14+
"github.com/sqlc-dev/sqlc/internal/core/catalogdb"
15+
1416
_ "modernc.org/sqlite"
1517
)
1618

19+
// The catalogdb package is generated by running sqlc against this
20+
// package's own schema.sql and catalogdb/query.sql — sqlc analyzing
21+
// sqlc's catalog. Regenerate with `go generate ./internal/core/...`.
22+
//
23+
//go:generate go run github.com/sqlc-dev/sqlc/cmd/sqlc generate
24+
1725
//go:embed schema.sql
1826
var ddl string
1927

2028
// Catalog is an in-memory SQLite database that stores schema metadata in
2129
// sql_* tables modeled after PostgreSQL's system catalogs.
30+
//
31+
// Queries against the sql_* tables are increasingly served by the
32+
// sqlc-generated catalogdb.Queries (see catalogdb/query.sql) rather than
33+
// hand-written SQL — sqlc analyzing sqlc's own catalog. Methods not yet
34+
// migrated fall back to the raw db handle.
2235
type Catalog struct {
2336
db *sql.DB
37+
q *catalogdb.Queries
2438
}
2539

2640
// Option configures a Catalog at creation time. The most common use is
@@ -45,7 +59,7 @@ func New(opts ...Option) (*Catalog, error) {
4559
db.Close()
4660
return nil, fmt.Errorf("core: init schema: %w", err)
4761
}
48-
c := &Catalog{db: db}
62+
c := &Catalog{db: db, q: catalogdb.New(db)}
4963
if err := c.bootstrap(); err != nil {
5064
db.Close()
5165
return nil, fmt.Errorf("core: bootstrap: %w", err)

‎internal/core/catalogdb/db.go‎

Lines changed: 31 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎internal/core/catalogdb/models.go‎

Lines changed: 111 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎internal/core/catalogdb/query.sql‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
-- name: CreateType :execlastid
2+
INSERT INTO sql_type
3+
(name, size, typtype, category, preferred, namespace_oid, dialect_oid, element_oid)
4+
VALUES (?, ?, ?, ?, ?, ?, ?, ?);
5+
6+
-- name: TypeOIDByName :one
7+
SELECT t.oid
8+
FROM sql_type t
9+
JOIN sql_namespace ns ON ns.oid = t.namespace_oid
10+
WHERE t.name = sqlc.arg(name)
11+
ORDER BY
12+
CASE ns.name
13+
WHEN 'pg_catalog' THEN 0
14+
WHEN 'public' THEN 1
15+
ELSE 2
16+
END,
17+
ns.name
18+
LIMIT 1;
19+
20+
-- name: TypeNameByOID :one
21+
SELECT name FROM sql_type WHERE oid = ?;
22+
23+
-- name: LookupType :one
24+
SELECT oid, name, category, typtype, preferred
25+
FROM sql_type
26+
WHERE oid = ?;
27+
28+
-- name: FindProcsAnyNamespace :many
29+
SELECT oid, name, kind, return_type_oid, return_nullable
30+
FROM sql_proc
31+
WHERE name = ?;
32+
33+
-- name: FindProcsInNamespaces :many
34+
SELECT oid, name, kind, return_type_oid, return_nullable
35+
FROM sql_proc
36+
WHERE name = sqlc.arg(name)
37+
AND namespace_oid IN (sqlc.slice(namespace_oids));

0 commit comments

Comments
 (0)