Documentation
¶
Overview ¶
Package verify refines static query analysis using engine-native query metadata. A Verifier inspects each query against a real (per-dialect) engine and reports the metadata needed to correct and enrich the static analyzer's result. Implementations must be deterministic: same catalog + same query set must yield identical results.
Index ¶
- func ApplyCache(analyses []analyzer.Result, c *Cache, currentSchemaHash string, ...) (out []analyzer.Result, stale []string)
- func MarshalCache(c *Cache) ([]byte, error)
- func Overlay(analyses []analyzer.Result, verified map[string]Result, opts OverlayOptions) []analyzer.Result
- func QueryHash(sql string) string
- func SchemaHash(c *model.Catalog) string
- type Cache
- type CacheMeta
- type CachedColumn
- type CachedParam
- type CachedQuery
- type Column
- type OverlayOptions
- type Query
- type Result
- type Verifier
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ApplyCache ¶
func ApplyCache( analyses []analyzer.Result, c *Cache, currentSchemaHash string, queryHash func(string) string, ) (out []analyzer.Result, stale []string)
ApplyCache overlays cached column types onto static analyses. It returns a new slice of results (input is never mutated) and the names of queries that were stale (cache miss, schema change, SQL change, or column count mismatch).
Rules:
- c == nil → all queries are stale; return analyses unchanged.
- c.SchemaHash != currentSchemaHash → all queries are stale.
- Per query: if the query is missing from the cache, the SQL hash changed, or the column count differs → that query is stale (static types kept).
- Stale names are returned in the same order as the input analyses slice.
func MarshalCache ¶
MarshalCache encodes c deterministically (encoding/json sorts map keys; a fixed indent keeps diffs minimal and the output byte-stable across runs).
func Overlay ¶
func Overlay(analyses []analyzer.Result, verified map[string]Result, opts OverlayOptions) []analyzer.Result
Overlay refines static analyses using verified metadata. For each query whose name appears in verified, origin-resolved result columns are corrected to the catalog's precise Go type and nullability; expressions and annotation-overridden columns are left untouched. Param types are never changed (a count/name mismatch is recorded as a diagnostic). A result-column count mismatch leaves that query's static columns unchanged.
func QueryHash ¶
QueryHash returns a deterministic sha256 hex digest of the given SQL string. The format is "sha256:<hex>" so it is self-describing and collision-safe.
func SchemaHash ¶
SchemaHash returns a deterministic sha256 hex digest of the catalog's tables. It is stable across runs and changes whenever a table name, column name, column type, or column nullability changes. Returns an empty string when c is nil.
Types ¶
type Cache ¶
type Cache struct {
Version int `json:"version"`
BinaryVersion string `json:"binaryVersion"`
Dialect string `json:"dialect"`
SchemaHash string `json:"schemaHash"`
Queries map[string]CachedQuery `json:"queries"`
}
Cache is the committable offline record of verified types (db-catalyst.verified.json).
func BuildCache ¶
func BuildCache( analyses []analyzer.Result, verified map[string]Result, meta CacheMeta, queryHash func(string) string, ) *Cache
BuildCache constructs a Cache from a set of (overlaid) analyses and the corresponding verified metadata. The queryHash function converts a raw SQL string to a stable hash string; it must be deterministic and collision-free within any single run.
Source is set to "origin" for columns where the matching verified result supplies both TableName and OriginName (i.e. the engine resolved the column back to a schema column), otherwise "static".
func UnmarshalCache ¶
UnmarshalCache decodes a cache file.
type CachedColumn ¶
type CachedColumn struct {
Name string `json:"name"`
GoType string `json:"goType"`
Nullable bool `json:"nullable"`
Import string `json:"import,omitempty"`
Package string `json:"package,omitempty"`
Source string `json:"source"` // "origin" | "static" | "annotation"
}
CachedColumn records one verified result column and which tier decided it.
type CachedParam ¶
type CachedParam struct {
Name string `json:"name"`
GoType string `json:"goType"`
Source string `json:"source"`
}
CachedParam records one parameter (type from static analysis; verify only counts).
type CachedQuery ¶
type CachedQuery struct {
QueryHash string `json:"queryHash"`
Columns []CachedColumn `json:"columns"`
Params []CachedParam `json:"params,omitempty"`
}
CachedQuery is the verified result of one query.
type Column ¶
type Column struct {
Name string // result column name (sqlite3_column_name)
DeclType string // declared type; "" for expressions/functions/constants
TableName string // origin table; "" if not a single-column reference
OriginName string // origin column; "" if not a single-column reference
}
Column is engine-reported metadata for one result column, in query order.
type OverlayOptions ¶
type OverlayOptions struct {
Catalog *model.Catalog
// Resolve converts a SQL column type to Go type info (same mapping the
// analyzer uses; wrap engine.TypeMapper().SQLToGo at the call site).
Resolve func(sqlType string, nullable bool) analyzer.TypeInfo
// ColumnOverrides are configured annotation overrides, keyed by UPPERCASE
// column name. A column whose name is present here is never touched by verify.
ColumnOverrides map[string]config.ColumnOverride
// UseDeclType enables a secondary refinement path: when a verified Column
// has no origin (TableName/OriginName are empty) but carries a non-empty
// DeclType (a real DB-reported type), the Go type is resolved from DeclType
// via Resolve. Only GoType/Import/Package are updated; nullability is NOT
// changed from this path.
//
// This is intentionally opt-in and defaults to false. SQLite's DeclType is
// declared by the schema author (not the query engine) and is unreliable for
// expression columns, so SQLite keeps the origin-only refinement path.
// PG/MySQL verifiers set UseDeclType=true because the DB-reported type is
// authoritative.
UseDeclType bool
}
OverlayOptions carries the collaborators the overlay needs to refine columns.
type Query ¶
type Query struct {
Name string // query identity, e.g. "GetUser"
SQL string // raw SQL, db-catalyst annotations already stripped
}
Query is one named query to verify.
type Result ¶
type Result struct {
Name string
Columns []Column // empty if the query has no result columns
ParamCount int // bind-parameter count
ParamNames []string // index-ordered; "" entry for positional binds
Diagnostics []analyzer.Diagnostic // prepare/bind errors surfaced to the user
}
Result is the verification of one query.
type Verifier ¶
type Verifier interface {
// Verify prepares each query against the catalog's schema and returns one
// Result per input query, in the same order. Implementations must not mutate
// the catalog or the queries.
Verify(ctx context.Context, catalog *model.Catalog, queries []Query) ([]Result, error)
// Dialect reports the engine dialect this verifier serves (e.g. "sqlite").
Dialect() string
}
Verifier inspects queries against a dialect engine and returns per-query metadata.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package livedb implements a verify.Verifier backed by a real database via database/sql.
|
Package livedb implements a verify.Verifier backed by a real database via database/sql. |
|
Package mysql implements a verify.Verifier for MySQL using the go-sql-driver/mysql driver via database/sql.
|
Package mysql implements a verify.Verifier for MySQL using the go-sql-driver/mysql driver via database/sql. |
|
Package postgres implements a verify.Verifier for PostgreSQL using the pgx native driver (github.com/jackc/pgx/v5).
|
Package postgres implements a verify.Verifier for PostgreSQL using the pgx native driver (github.com/jackc/pgx/v5). |
|
Package provision defines the Provisioner abstraction for spinning up ephemeral database instances during verify runs, and provides:
|
Package provision defines the Provisioner abstraction for spinning up ephemeral database instances during verify runs, and provides: |
|
Package sqlite implements a verify.Verifier backed by an in-memory modernc.org/sqlite database.
|
Package sqlite implements a verify.Verifier backed by an in-memory modernc.org/sqlite database. |