Documentation
¶
Overview ¶
Package metaquery provides runtime metadata types and a CTE-based query builder for queries generated by sqlc-go-codegen-metaquery.
A generated Query value captures the column/arg shape of a sqlc query plus its original SQL text. Wrap one with Wrap() to compose additional filters, ordering, pagination, grouping, and aggregations on top without rewriting the original SQL — the original is embedded verbatim as a CTE.
Index ¶
- func Validate[T any](b *Builder) error
- func ValidateFilter(q *Query, f Filter) error
- type AnyCol
- func (c AnyCol) Asc() OrderBy
- func (c AnyCol) Col() string
- func (c AnyCol) Desc() OrderBy
- func (c AnyCol) Eq(v any) Filter
- func (c AnyCol) Ge(v any) Filter
- func (c AnyCol) Gt(v any) Filter
- func (c AnyCol) IsNotNull() Filter
- func (c AnyCol) IsNull() Filter
- func (c AnyCol) Le(v any) Filter
- func (c AnyCol) Lt(v any) Filter
- func (c AnyCol) Ne(v any) Filter
- type Arg
- type BoolCol
- type Builder
- func (b *Builder) Agg(alias, expr, goType string) *Builder
- func (b *Builder) ApplyFilter(f Filter) *Builder
- func (b *Builder) ApplyFilters(fs []Filter) *Builder
- func (b *Builder) ApplyHaving(f Filter) *Builder
- func (b *Builder) ApplyOrder(o OrderBy) *Builder
- func (b *Builder) ApplyOrders(os []OrderBy) *Builder
- func (b *Builder) ApplyPagination(r PageRequest) *Builder
- func (b *Builder) Avg(alias, col string) *Builder
- func (b *Builder) Build() (string, []any, error)
- func (b *Builder) BuildCount() (string, []any, error)
- func (b *Builder) Count(alias string) *Builder
- func (b *Builder) CountOf(alias, col string) *Builder
- func (b *Builder) GroupBy(cols ...string) *Builder
- func (b *Builder) GroupByExpr(alias, expr, goType string, args ...any) *Builder
- func (b *Builder) Having(expr string, args ...any) *Builder
- func (b *Builder) Limit(n int) *Builder
- func (b *Builder) Max(alias, col string) *Builder
- func (b *Builder) Meta() Meta
- func (b *Builder) Min(alias, col string) *Builder
- func (b *Builder) Offset(n int) *Builder
- func (b *Builder) OrderBy(col string, dir Direction) *Builder
- func (b *Builder) OrderByExpr(expr string) *Builder
- func (b *Builder) OutputColumns() []Column
- func (b *Builder) Paginate(page, size int) *Builder
- func (b *Builder) Select(cols ...string) *Builder
- func (b *Builder) Sum(alias, col string) *Builder
- func (b *Builder) WantsTotal() bool
- func (b *Builder) Where(col string, op Op, val any) *Builder
- func (b *Builder) WhereExpr(expr string, args ...any) *Builder
- func (b *Builder) WithTotal() *Builder
- type BytesCol
- type Column
- type Dialect
- type Direction
- type Filter
- type FloatCol
- func (c FloatCol) Asc() OrderBy
- func (c FloatCol) Between(lo, hi float64) Filter
- func (c FloatCol) Col() string
- func (c FloatCol) Desc() OrderBy
- func (c FloatCol) Eq(v float64) Filter
- func (c FloatCol) Ge(v float64) Filter
- func (c FloatCol) Gt(v float64) Filter
- func (c FloatCol) IsNotNull() Filter
- func (c FloatCol) IsNull() Filter
- func (c FloatCol) Le(v float64) Filter
- func (c FloatCol) Lt(v float64) Filter
- func (c FloatCol) Ne(v float64) Filter
- type IntCol
- func (c IntCol) Asc() OrderBy
- func (c IntCol) Between(lo, hi int64) Filter
- func (c IntCol) Col() string
- func (c IntCol) Desc() OrderBy
- func (c IntCol) Eq(v int64) Filter
- func (c IntCol) Ge(v int64) Filter
- func (c IntCol) Gt(v int64) Filter
- func (c IntCol) In(vs ...int64) Filter
- func (c IntCol) IsNotNull() Filter
- func (c IntCol) IsNull() Filter
- func (c IntCol) Le(v int64) Filter
- func (c IntCol) Lt(v int64) Filter
- func (c IntCol) Ne(v int64) Filter
- type Meta
- type Op
- type OrderBy
- type PageRequest
- type Pagination
- type Query
- type Result
- type Table
- type TextCol
- func (c TextCol) Asc() OrderBy
- func (c TextCol) Col() string
- func (c TextCol) Desc() OrderBy
- func (c TextCol) Eq(v string) Filter
- func (c TextCol) ILike(v string) Filter
- func (c TextCol) In(vs ...string) Filter
- func (c TextCol) IsNotNull() Filter
- func (c TextCol) IsNull() Filter
- func (c TextCol) Like(v string) Filter
- func (c TextCol) Ne(v string) Filter
- type TimeCol
- func (c TimeCol) After(v time.Time) Filter
- func (c TimeCol) Asc() OrderBy
- func (c TimeCol) Before(v time.Time) Filter
- func (c TimeCol) Between(lo, hi time.Time) Filter
- func (c TimeCol) Col() string
- func (c TimeCol) Desc() OrderBy
- func (c TimeCol) Eq(v time.Time) Filter
- func (c TimeCol) IsNotNull() Filter
- func (c TimeCol) IsNull() Filter
- type TypedResult
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Validate ¶
Validate checks that T's exported fields map cleanly onto b.OutputColumns(). A field matches a column by its `db` tag, falling back to snake_case of the field name. Drift in either direction (field with no column, column with no field) is an error.
Called automatically by mqpgx.Scan[T]; call directly if you want to fail fast without issuing a query.
func ValidateFilter ¶
ValidateFilter checks that f is a sensible filter for q: the column exists, the op is permitted for the column's Go type, and the value is type- compatible (with JSON-friendly leniency — float64 for int columns, RFC3339 strings for time columns).
Raw-expression filters (f.IsExpr()) are passed through unchecked.
Called automatically by Builder.ApplyFilter; call directly when you want to validate a filter you just deserialized from JSON without mutating a builder.
Types ¶
type AnyCol ¶
type AnyCol struct {
// contains filtered or unexported fields
}
AnyCol is the fallback for column kinds we don't have dedicated typing for (arrays, enums, pgtype.UUID, pgtype.Numeric, domain types, ...). Ops take `any` — use sparingly.
func (AnyCol) Asc ¶
func (c AnyCol) Asc() OrderBy
Asc / Desc produce OrderBy entries for the builder's ApplyOrder method.
func (AnyCol) Col ¶
func (c AnyCol) Col() string
Col returns the column name this reference points at. Useful for forwarding to GroupBy/Select which take plain strings:
b.GroupBy(db.ListAuthorsCols.Name.Col())
type Arg ¶
type Arg struct {
Position int `json:"position"`
Name string `json:"name,omitempty"`
GoType string `json:"go_type,omitempty"`
DBType string `json:"db_type,omitempty"`
NotNull bool `json:"not_null,omitempty"`
IsArray bool `json:"is_array,omitempty"`
IsSqlcSlice bool `json:"is_sqlc_slice,omitempty"`
}
Arg describes one positional argument of the underlying query.
type BoolCol ¶
type BoolCol struct {
// contains filtered or unexported fields
}
BoolCol wraps bool / pgtype.Bool. IsTrue / IsFalse read nicer than Eq(true).
func NewBoolCol ¶
func (BoolCol) Asc ¶
func (c BoolCol) Asc() OrderBy
Asc / Desc produce OrderBy entries for the builder's ApplyOrder method.
func (BoolCol) Col ¶
func (c BoolCol) Col() string
Col returns the column name this reference points at. Useful for forwarding to GroupBy/Select which take plain strings:
b.GroupBy(db.ListAuthorsCols.Name.Col())
type Builder ¶
type Builder struct {
// contains filtered or unexported fields
}
Builder composes filters, projection, grouping, aggregations, ordering, and pagination on top of a Query. Build returns the final SQL wrapping the original query as a CTE, plus the full positional argument slice.
Zero value is not usable; construct with Wrap.
func Wrap ¶
Wrap creates a Builder over q. baseArgs are the positional arguments for the original query ($1..$N); the Builder's own added arguments renumber to $N+1 and up.
func (*Builder) Agg ¶
Agg adds an aggregate to the projection. alias is the output column name, expr is raw SQL, goType is the Go type of the result (used when building Meta.Columns and validating Into[T] shapes).
func (*Builder) ApplyFilter ¶
ApplyFilter appends a Filter to the WHERE clause. See Filter for the two valid shapes. Validates via ValidateFilter; accumulated errors surface at Build time.
func (*Builder) ApplyFilters ¶
ApplyFilters is a convenience for applying a slice of filters.
func (*Builder) ApplyHaving ¶
ApplyHaving appends a Filter to the HAVING clause. Aggregate expressions are common in HAVING so raw-expr filters are typical here; structured filters go through the same ValidateFilter check as WHERE.
func (*Builder) ApplyOrder ¶
ApplyOrder appends an OrderBy. Exactly one of Column/Expr must be set.
func (*Builder) ApplyOrders ¶
ApplyOrders is a convenience for a slice of OrderBy entries.
func (*Builder) ApplyPagination ¶
func (b *Builder) ApplyPagination(r PageRequest) *Builder
ApplyPagination drives pagination from a structured request.
func (*Builder) BuildCount ¶
BuildCount returns a SELECT count(*) query over the same CTE + WHERE filters (but without GROUP BY / ORDER BY / LIMIT / OFFSET). Aggregation builders (groupBy non-empty) wrap the whole projection so distinct groups are counted.
func (*Builder) CountOf ¶
CountOf adds `count(<col>) AS <alias>` projecting int64 NOT NULL. col is whitelisted.
func (*Builder) GroupByExpr ¶ added in v1.0.0
GroupByExpr appends a raw SQL expression to the GROUP BY clause. The result is projected as `<expr> AS <alias>` in the SELECT and appears in OutputColumns with the given goType. Use ? placeholders for bound args. This is the escape hatch for grouping by computed expressions (e.g. JSON field extraction: `GroupByExpr("group_value", "context_json->>?", "string", key)`).
func (*Builder) Meta ¶
Meta returns the envelope describing the builder's applied state. Meta does NOT include Pagination.Total — that's populated by the adapter after the count query runs.
func (*Builder) OrderByExpr ¶
OrderByExpr appends a raw ORDER BY fragment (no whitelist).
func (*Builder) OutputColumns ¶
OutputColumns returns the ordered set of columns the builder's final SELECT will produce. Used for Meta and for Into[T] validation.
func (*Builder) Paginate ¶
Paginate is a convenience for Limit + Offset (and WithTotal when req.Total is set). Page is 0-indexed.
func (*Builder) Select ¶
Select restricts the projection to the given columns, whitelisted. Without a Select (or GroupBy/Agg), projection defaults to *.
func (*Builder) Sum ¶
Sum, Avg, Min, Max project the same GoType as the source column but nullable (aggregates over empty groups return NULL). Use the Agg escape hatch with coalesce(...) to force NOT NULL.
func (*Builder) WantsTotal ¶
WantsTotal reports whether the caller requested a total-row count (see WithTotal / ApplyPagination). Adapters use this to run a follow-up COUNT.
func (*Builder) Where ¶
Where appends `<col> <op> ?`, whitelisting col against q.Columns. Prefer passing a typed Op constant (OpEq, OpILike, ...) — raw string literals still work because they're untyped constants.
type BytesCol ¶
type BytesCol struct {
// contains filtered or unexported fields
}
BytesCol wraps []byte columns.
func NewBytesCol ¶
func (BytesCol) Asc ¶
func (c BytesCol) Asc() OrderBy
Asc / Desc produce OrderBy entries for the builder's ApplyOrder method.
func (BytesCol) Col ¶
func (c BytesCol) Col() string
Col returns the column name this reference points at. Useful for forwarding to GroupBy/Select which take plain strings:
b.GroupBy(db.ListAuthorsCols.Name.Col())
type Column ¶
type Column struct {
Name string `json:"name"`
OriginalName string `json:"original_name,omitempty"`
GoType string `json:"go_type,omitempty"`
DBType string `json:"db_type,omitempty"`
NotNull bool `json:"not_null,omitempty"`
IsArray bool `json:"is_array,omitempty"`
Table string `json:"table,omitempty"`
TableAlias string `json:"table_alias,omitempty"`
}
Column describes one result column of a query or builder projection.
type Dialect ¶ added in v1.1.0
type Dialect string
Dialect selects the placeholder syntax the Builder emits for filters, aggregations, and other appended SQL fragments. The zero value (DialectPostgres) preserves prior behavior; DialectSQLite is required when the wrapped query targets SQLite via database/sql, where Postgres-style $N is not the conventional placeholder form.
type Filter ¶
type Filter struct {
Column string `json:"column,omitempty"`
Op Op `json:"op,omitempty"`
Value any `json:"value,omitempty"`
Expr string `json:"expr,omitempty"`
Args []any `json:"args,omitempty"`
}
Filter is a predicate applied to a wrapped query. It's bidirectional: pass it into the builder via ApplyFilter to add WHERE/HAVING clauses, and read it back from Meta.Where/Meta.Having after execution to inspect what was applied (e.g. for rendering active filters in a UI).
Use exactly one of:
- (Column, Op, Value) — structured; Column is whitelisted against the query's output columns.
- (Expr, Args) — raw SQL fragment with ? placeholders (renumbered at Build time).
type FloatCol ¶
type FloatCol struct {
// contains filtered or unexported fields
}
FloatCol wraps float32/float64 and pgtype.Float4/8.
func NewFloatCol ¶
func (FloatCol) Asc ¶
func (c FloatCol) Asc() OrderBy
Asc / Desc produce OrderBy entries for the builder's ApplyOrder method.
func (FloatCol) Col ¶
func (c FloatCol) Col() string
Col returns the column name this reference points at. Useful for forwarding to GroupBy/Select which take plain strings:
b.GroupBy(db.ListAuthorsCols.Name.Col())
type IntCol ¶
type IntCol struct {
// contains filtered or unexported fields
}
IntCol wraps int/int16/int32/int64 and pgtype.Int2/4/8. Values are taken as int64; Go's untyped integer constants make callsites ergonomic (c.Id.Eq(42) compiles because 42 is untyped).
func (IntCol) Asc ¶
func (c IntCol) Asc() OrderBy
Asc / Desc produce OrderBy entries for the builder's ApplyOrder method.
func (IntCol) Col ¶
func (c IntCol) Col() string
Col returns the column name this reference points at. Useful for forwarding to GroupBy/Select which take plain strings:
b.GroupBy(db.ListAuthorsCols.Name.Col())
type Meta ¶
type Meta struct {
Columns []Column `json:"columns"`
Pagination *Pagination `json:"pagination,omitempty"`
Where []Filter `json:"where,omitempty"`
Having []Filter `json:"having,omitempty"`
GroupBy []string `json:"group_by,omitempty"`
OrderBy []OrderBy `json:"order_by,omitempty"`
}
Meta is the envelope describing a builder's output shape and the filters that were applied. Safe to JSON-serialize as the body of an API response.
type Op ¶
type Op string
Op is a named string type for structured filter operators. Predefined constants (OpEq, OpILike, ...) are preferred over raw strings; the typed parameter catches op typos at compile time when callers use constants. Raw string literals still work due to untyped-constant assignability.
type OrderBy ¶
type OrderBy struct {
Column string `json:"column,omitempty"`
Dir Direction `json:"dir,omitempty"`
Expr string `json:"expr,omitempty"`
}
OrderBy is one ORDER BY entry. Structured form uses Column+Dir; raw form uses Expr.
type PageRequest ¶
type PageRequest struct {
Page int `json:"page,omitempty"`
Size int `json:"size,omitempty"`
Total bool `json:"total,omitempty"`
}
PageRequest describes a pagination + total-count request. Size of 0 means no LIMIT. Total=true causes a second COUNT(*) query to populate Pagination.Total when the builder is executed.
type Pagination ¶
type Pagination struct {
Limit int `json:"limit,omitempty"`
Offset int `json:"offset,omitempty"`
Total int64 `json:"total,omitempty"` // -1 if not computed
}
Pagination is the applied pagination state returned in Meta.
type Query ¶
type Query struct {
Name string `json:"name"`
Cmd string `json:"cmd"`
SQL string `json:"sql"`
Source string `json:"source,omitempty"`
Dialect Dialect `json:"dialect,omitempty"`
Columns []Column `json:"columns,omitempty"`
Args []Arg `json:"args,omitempty"`
Table *Table `json:"table,omitempty"`
}
Query is the per-query metadata emitted alongside sqlc's generated code.
type Result ¶
Result is the untyped envelope from mqpgx.Run — each row is a positional []any matching Meta.Columns.
type Table ¶
type Table struct {
Catalog string `json:"catalog,omitempty"`
Schema string `json:"schema,omitempty"`
Name string `json:"name"`
}
Table identifies the insert target for :copyfrom queries.
type TextCol ¶
type TextCol struct {
// contains filtered or unexported fields
}
TextCol wraps string / pgtype.Text / pgtype.Varchar columns.
func NewTextCol ¶
func (TextCol) Asc ¶
func (c TextCol) Asc() OrderBy
Asc / Desc produce OrderBy entries for the builder's ApplyOrder method.
func (TextCol) Col ¶
func (c TextCol) Col() string
Col returns the column name this reference points at. Useful for forwarding to GroupBy/Select which take plain strings:
b.GroupBy(db.ListAuthorsCols.Name.Col())
type TimeCol ¶
type TimeCol struct {
// contains filtered or unexported fields
}
TimeCol wraps time.Time and pgtype.Timestamp/Timestamptz/Date/Time.
func NewTimeCol ¶
func (TimeCol) Asc ¶
func (c TimeCol) Asc() OrderBy
Asc / Desc produce OrderBy entries for the builder's ApplyOrder method.
type TypedResult ¶
TypedResult is the typed envelope from mqpgx.Scan[T].
Directories
¶
| Path | Synopsis |
|---|---|
|
Package mqpgx is the pgx/v5 adapter for metaquery builders.
|
Package mqpgx is the pgx/v5 adapter for metaquery builders. |
|
Package mqsqlite is the database/sql adapter for metaquery builders targeting SQLite.
|
Package mqsqlite is the database/sql adapter for metaquery builders targeting SQLite. |