Documentation
¶
Overview ¶
Package analyze is the pure-Go PostgreSQL semantic analyzer: given a Schema and one SQL statement it derives parameter types, result columns (type, nullability, provenance) and PG-compatible errors, following the rules of PostgreSQL manual chapter 10 (type conversion) without running PostgreSQL.
Index ¶
- func Load(schemaSQL string) (*schema.Schema, error)
- func LoadWith(cat *catalog.Catalog, schemaSQL string) (*schema.Schema, error)
- func Lower(s *schema.Schema, expr string, rel *schema.Relation) ([]facts.Pred, error)
- func SettingReads(pol *schema.Policy) []string
- type Column
- type Error
- type FunctionResult
- type FunctionStatement
- type Note
- type RelationRef
- type Result
- type Source
- type Use
- type Violation
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Load ¶
Analyze analyzes exactly one SQL statement against s. A statement PG would reject returns an *Error; other failures are ordinary errors. Load parses schema DDL the way schema.Load does and freezes every view's output columns as the view is created (PG fixes them at CREATE VIEW; see schema.Relation.Frozen). Use this rather than schema.Load for a schema the analyzer will read.
func Lower ¶ added in v1.1.0
Lower reads a declared predicate over rel in the facts language: the expression is parsed and type-checked against the table alone (an error is the declaration's), and each conjunct becomes a facts.Pred whose ColRef.Leaf is 0, standing for the subject. This is internal/obligation's Lowerer for PostgreSQL.
func SettingReads ¶
SettingReads lists the current_setting(name, true) calls in a policy's predicates: a setting the session never set makes the predicate NULL, which hides every row without an error.
Types ¶
type Column ¶
type Column struct {
Name string
Type schema.TypeRef
// Nullable is the analyzer's own inference (NOT NULL constraints, join shape, expression rules).
Nullable bool
// Source mirrors what PG's Describe reports (stops at views), for oracle parity.
Source *Source
// Fields describes a record / composite column (or an array of them): the columns of
// the row type, or the positional f1.. fields of an anonymous row(...). Nil otherwise.
Fields []Column
}
Column is one result column.
type FunctionResult ¶
type FunctionResult struct {
// Relations the body references directly (dependency graph through functions).
Relations []RelationRef
// Notes from the body's statements (domain mixing etc.).
Notes []Note
// Violations the body's writes may cause (PL/pgSQL bodies; SQL bodies are walked
// per call in functionViolations).
Violations []Violation
// Statements are the body's statements as facts, for the obligations the schema
// declares (internal/obligation); Line is the PL/pgSQL line, 0 for a SQL body.
Statements []FunctionStatement
}
FunctionResult is the analysis of a function body.
func AnalyzeFunction ¶
AnalyzeFunction checks a LANGUAGE sql function's body against its signature. Functions without an analyzable body return an empty result.
type FunctionStatement ¶ added in v1.1.0
FunctionStatement is one statement of a function body, as facts.
type Note ¶
Note is a finding that PostgreSQL itself would not report.
func AnalyzePolicy ¶
AnalyzePolicy type-checks a row-level security policy's USING and WITH CHECK predicates the way CREATE POLICY does: an expression over the table's columns that yields boolean, with no aggregates, window functions or set-returning functions. Notes are the analyzer's own findings inside the predicates (domain mixing, and so on).
type RelationRef ¶
type RelationRef struct {
Schema string
Name string
Kind byte // 'r' table, 'v' view, 'm' materialized view
Position int32
// Target: the relation an INSERT / UPDATE / DELETE / MERGE writes (a read elsewhere in
// the same statement, in a subquery or a USING, does not count).
Target bool
}
RelationRef is a direct reference to a relation, with its position in the SQL.
type Result ¶
type Result struct {
Params []schema.TypeRef
Columns []Column
// ParamSources, indexed like Params, is the table column a parameter was compared
// with or assigned to (nil when it met an expression). It carries "which identity"
// a value stands for, beyond its type.
ParamSources []*Source
// Notes are findings PG itself would accept, e.g. mixing domains (domain.go).
Notes []Note
// AtMostOne is whether the statement provably returns at most one row (card.go);
// ManyRowsWhy says what blocks the proof otherwise.
AtMostOne bool
ManyRowsWhy string
// Violations are the constraints a write may violate (violation.go).
Violations []Violation
// Relations are the tables and views the statement references directly (FROM, JOIN,
// DML target, subqueries; views are listed but not expanded). Each once, in order.
Relations []RelationRef
// Fixed are the table columns the statement pins to one value: by equality with a
// literal / parameter / outer reference in WHERE or ON (at any select level), or by
// assignment in an INSERT (Assigned set). Row-ownership policies check against it.
Fixed []Source
// Uses are the relation columns the statement depends on (see Use).
Uses []Use
// Facts is what the statement provably does, in the form internal/obligation judges
// (facts.go); nil for statements that are not SELECT / INSERT / UPDATE / DELETE / MERGE.
Facts *facts.Facts
}
Result is the analysis of one statement.
func AnalyzeView ¶
AnalyzeView analyzes a view's defining query as a statement of its own, so its findings (policy, domain mixing) are reported once at the view rather than at every reader.
type Source ¶
type Source struct {
Table string
Column string
NotNull bool
// Assigned is set when a parameter was stored into the column (INSERT / UPDATE), as
// opposed to compared with it.
Assigned bool
}
Source is the table column a result column comes straight from.
type Use ¶
type Use struct {
Table string // schema-qualified unless public, like Source.Table
Column string
Position int32
}
Use is a relation column the statement depends on: read by name or by *, assigned by INSERT / UPDATE / MERGE, or taken in a whole-row reference. Dropping or changing the column changes what the statement means. A view's columns count as the view's (its base tables are the view's business, and the schema knows them). Each once, at its first position; nothing is recorded inside view definitions.
type Violation ¶
type Violation struct {
Code string // SQLSTATE: 23505 unique, 23503 foreign key, 23514 check, 23502 not null
Constraint string // constraint name; "" for NOT NULL (PG 17 does not name them)
Table string // table the constraint belongs to (referencing table for FKs)
Columns []string // constrained columns
RefTable string // FK: referenced table
Param int32 // NOT NULL: the bare parameter whose NULL would violate (0 otherwise)
// Trigger / Name: a custom SQLSTATE raised by a trigger function (Constraint holds the code)
Trigger string
Name string
// Function is the user function whose body (or `-- sqlshape: error` annotation) this
// violation comes from, when the statement reaches it through a call.
Function string
}
Violation is one constraint a statement may violate.