sqlc
sqlc copied to clipboard
feat(postgresql): add accurate analyzer mode for database-only analysis
Summary
Adds an optional analyzer.accurate: true mode for PostgreSQL that bypasses the internal catalog and uses only database-backed analysis for more accurate type information.
Key Features
- Database-only type resolution: Uses PostgreSQL
PREPAREstatements for all column and parameter type inference - Database-backed star expansion: Uses the expander package (from #4203) with database queries for
SELECT *andRETURNING *expansion - Schema introspection: Queries
pg_catalogto build catalog structures needed for code generation (tables, columns, enums) - No internal catalog: Skips building the internal catalog from schema files in accurate mode
Configuration
version: "2"
sql:
- engine: postgresql
schema: "schema.sql"
queries: "queries/"
database:
uri: "postgres://localhost/mydb" # or managed: true
analyzer:
accurate: true
gen:
go:
package: "db"
out: "db"
Requirements
- Accurate mode requires a database connection (
database.uriordatabase.managed: true) - The schema must exist in the database before running sqlc generate
- Only supported for PostgreSQL engine
Files Changed
| File | Changes |
|---|---|
internal/config/config.go |
Added Accurate *bool to Analyzer struct |
internal/config/v_one.json, v_two.json |
Added accurate boolean to analyzer schema |
internal/engine/postgresql/analyzer/analyze.go |
Added IntrospectSchema(), EnsurePool(), GetColumnNames() methods |
internal/compiler/engine.go |
Added accurate mode detection and expander setup |
internal/compiler/compile.go |
Skip catalog building, introspect schema after parsing |
internal/compiler/parse.go |
Use expander for star expansion in accurate mode |
Test plan
- [x] All existing tests pass
- [x] Code compiles without errors
- [ ] Manual testing with local PostgreSQL database
🤖 Generated with Claude Code