Help us shape the future of Go! Take the 2026 Go Developer Survey

postgres

package
v0.0.0-...-552dc35 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 2, 2026 License: Apache-2.0 Imports: 24 Imported by: 0

Documentation

Index

Constants

View Source
const DriverName = "pgx/v5"
View Source
const (
	TypeEnum = "enum"
)

Variables

View Source
var (
	// AllColumnsIdentifier is used to represent '*' in a 'SELECT' projection.
	AllColumnsIdentifier = ColumnIdentifier{
		Name: "*",
	}
)
View Source
var (
	DefaultSchema = "public"
)

Functions

func CmpFloat32

func CmpFloat32(l, r any) int

func CmpFloat64

func CmpFloat64(l, r any) int

func CmpIntN

func CmpIntN(l, r any, bits int) int

func CmpTimes

func CmpTimes(l, r any) int

func ExecScript

func ExecScript(ctx context.Context, conn *sql.DB, script string) error

func Parse

func Parse(ctx context.Context, getTableF tableGetter, query string) (ir.Expression, error)

func ParseTest

func ParseTest(ctx context.Context, query string) (ir.Expression, error)

ParseTest must be used for tests only.

Types

type Bool

type Bool bool

func (*Bool) Scan

func (b *Bool) Scan(src any) error

Scan implements the sql.Scanner interface. Converts src string from Postgres into a bool to store in *b.

func (*Bool) Value

func (b *Bool) Value() (driver.Value, error)

Value implements the driver.Valuer interface.

type CTypeMeta

type CTypeMeta struct {
	// OID of the type, not of the enum in 'pg_enum'
	TypeOID OID `db:"type_oid"`
	// Type is b for a base type, c for a composite type (e.g., a table's row type), d for a domain, e for an enum
	// type, p for a pseudo-type, r for a range type, or m for a multi-range type
	Type     string `db:"typtype"`
	TypeName string `db:"typname"`
	// EnumSortOrder is the index of the enum label. Nil for other types
	EnumSortOrder sql.NullFloat64 `db:"enumsortorder"`
	// EnumLabel is null for other types
	EnumLabel sql.NullString `db:"enumlabel"`
}

CTypeMeta is a definition for custom types. For now the only custom type supported is ENUM.

type CheckConstraintMeta

type CheckConstraintMeta struct {
	Name string `db:"index_name"`
	// Definition is the SQL statement to create this index
	Definition string `db:"index_def"`
	// ConCols is the list of constrained columns
	ConCols []*ColumnMeta
}

type ColumnIdentifier

type ColumnIdentifier struct {
	Schema       string
	TableOrAlias string
	Name         string
}

type ColumnMeta

type ColumnMeta struct {
	Name      string `db:"column_name"`
	TableName string `db:"table_name"`
	// Position of the column in the table
	Position int `db:"ordinal_position"`
	// Check PostgreSQL and Golang type affinity to know what type to expect here
	DefaultVal any  `db:"column_default"`
	Nullable   Bool `db:"is_nullable"`
	// See UserDefinedTypeName.
	TypeName string `db:"data_type"`
	TypeOID  OID    `db:"type_oid"`
	// When Type is a custom (user-defined) type holds the type name
	UserDefinedTypeName string `db:"udt_name"`
	// Size of VARCHAR and similar types. No populated otherwise
	MaxLen sql.NullInt32 `db:"character_maximum_length"`
	// If data_type identifies a numeric type, this column contains the (declared or implicit) precision of the type
	// for this column. The precision indicates the number of significant digits. It can be expressed in decimal
	// (base 10) or binary (base 2) terms, as specified in the column numeric_precision_radix. For all other data
	// types, this column is null.
	NumericPrecision sql.NullInt32 `db:"numeric_precision"`
	// If data_type identifies an exact numeric type, this column contains the (declared or implicit) scale of the
	// type for this column. The scale indicates the number of significant digits to the right of the decimal point.
	// It can be expressed in decimal (base 10) or binary (base 2) terms, as specified in the column
	// numeric_precision_radix. For all other data types, this column is null.
	NumericScale sql.NullInt32 `db:"numeric_scale"`
	// Set when processing primary keys. Nil if the table has no PK
	PKConstraint *ConstraintMeta
	// Set when processing TABLE unique indexes. This column and table defined unique constraints as well as
	// explicitly created as well as unique indexes to support PKs.
	// A column is unique (simple or composed constraint) if this list is not empty.
	UniqueIndexes []UniqueIndexMeta
	// 'CHECK' constraints on this column if any
	// TODO: Not in use that the moment, maybe remove if it turns out to not be useful
	CheckConstraints []*ConstraintMeta
	TypeDef          baseType
}

func (*ColumnMeta) GenerateVal

func (c *ColumnMeta) GenerateVal() *Value

func (*ColumnMeta) NewDomain

func (c *ColumnMeta) NewDomain() Domain

NewDomain returns a new domain spanning every value of the column's type. Panics if the type of the column does not support domains, see SupportsDomain.

func (*ColumnMeta) SupportsDomain

func (c *ColumnMeta) SupportsDomain() bool

SupportsDomain returns true if the type of the column supports domains, false otherwise.

func (*ColumnMeta) WrapValue

func (c *ColumnMeta) WrapValue(val any) *Value

type ColumnMetaList

type ColumnMetaList []*ColumnMeta

func (ColumnMetaList) Names

func (c ColumnMetaList) Names() []string

type ConstraintMeta

type ConstraintMeta struct {
	// Constrained table's oid
	OID OID `db:"oid"`
	// Useful for debugging
	Name string `db:"conname"`
	// SQL expression of the constraint, e.g.:
	//   - Check const.: CHECK (c5 > 0::numeric)
	//   - PK const.: PRIMARY KEY (c1)
	//   - Unique const.: UNIQUE (c3, c4)
	//   - FK const.: FOREIGN KEY (c8) REFERENCES child_t1(id)
	ConstraintExpr string `db:"conexpr"`
	// c = check constraint, f = foreign key constraint, n = not-null constraint (domains only), p = primary key
	// constraint, u = unique constraint, t = constraint trigger, x = exclusion constraint
	Type string `db:"contype"`
	// oid of the index supporting this constraint, if it's a unique, primary key, foreign key, or exclusion
	// constraint; else zero. (references pg_class.oid)
	// For FKs this is the PK index in the referenced table.
	IndexOID OID `db:"index_oid"`
	// If a foreign key, the referenced table; else zero. (references pg_class.oid)
	FKTargetTableOID OID `db:"fk_target_table_oid"`
	// If a foreign key, list of the referenced columns. (references pg_attribute.attnum or
	// information_schema.columns.ordinal_position)
	FKReferencedColumns SliceInt `db:"fk_ref_columns"`
	// If a table constraint (including foreign keys, but not constraint triggers), list of the constrained columns.
	// (references pg_attribute.attnum or information_schema.columns.ordinal_position)
	ConstrainedColumns SliceInt `db:"con_columns_oid"`
}

type DefValWrapper

type DefValWrapper struct {
	V any
}

DefValWrapper wraps default values that are stored as SQL expressions in psqlColumnMeta, so that these are never quoted when encoded. Type encoders should use type assertion to look for this type and avoid quoting values when applicable.

func (DefValWrapper) MarshalJSON

func (v DefValWrapper) MarshalJSON() ([]byte, error)

type Domain

type Domain interface {
	// Above constrains the domain to values greater than constant or, greater or equal when inclusive is true.
	Above(constant string, inclusive bool)
	// Below constrains the domain to values lower than constant or, lower or equal when inclusive is true.
	Below(constant string, inclusive bool)
	// Exclude removes constant from the domain.
	Exclude(constant string)
	// Gen returns a random value from the domain. Never returns nil.
	Gen() any
}

Domain is the set of values a column can take given a list of range comparisons against constants, e.g.: 'c1 > 42 AND c1 <> 50 AND c1 <= 123'. Constants are passed as they come from the SQL query and each comparison narrows the domain.

type FKMeta

type FKMeta struct {
	Name string
	// Constrained columns
	ConCols []*ColumnMeta
	// Referenced table
	RefTable *TableMeta
	// Referenced columns
	RefCols []*ColumnMeta
}

type Inspector

type Inspector struct {
	// contains filtered or unexported fields
}

func NewInspector

func NewInspector(db *sql.DB) *Inspector

New creates a new Postgres Inspector to inspect db.

func (*Inspector) Table

func (i *Inspector) Table(ctx context.Context, name string) (TableMeta, error)

Table returns a table metadata for a table with name in the default schema "public".

func (*Inspector) TableWithSchema

func (i *Inspector) TableWithSchema(ctx context.Context, schema, name string) (TableMeta, error)

TableWithSchema is the same as Table but allows to specify the schema.

type OID

type OID uint32

type SliceInt

type SliceInt []int

func (*SliceInt) Scan

func (s *SliceInt) Scan(src any) error

Scan implements the sql.Scanner interface. Converts src string '{1, 2, n}' from Postgres into a SliceInt to store in *s.

func (*SliceInt) Value

func (s *SliceInt) Value() (driver.Value, error)

Value implements the driver.Valuer interface.

type TableMeta

type TableMeta struct {
	// contains filtered or unexported fields
}

func (*TableMeta) CheckConstraints

func (t *TableMeta) CheckConstraints() []CheckConstraintMeta

func (*TableMeta) ColumnByName

func (t *TableMeta) ColumnByName(name string) *ColumnMeta

func (*TableMeta) Columns

func (t *TableMeta) Columns() []*ColumnMeta

func (*TableMeta) ForeignKeys

func (t *TableMeta) ForeignKeys() []FKMeta

func (*TableMeta) Name

func (t *TableMeta) Name() TableName

func (*TableMeta) PrimaryKey

func (t *TableMeta) PrimaryKey() []*ColumnMeta

func (*TableMeta) UniqueIndexes

func (t *TableMeta) UniqueIndexes() []UniqueIndexMeta

type TableName

type TableName struct {
	// contains filtered or unexported fields
}

func (TableName) FQ

func (t TableName) FQ() string

FQ returns the fully-qualified name of the table.

func (TableName) Schema

func (t TableName) Schema() string

Schema returns the schema the table belongs to.

func (TableName) Simple

func (t TableName) Simple() string

Simple returns the table name without schema.

func (TableName) String

func (t TableName) String() string

type Type

type Type interface {
	Encode(any) ([]byte, error)
	// Cmp compares l and r. l and r can be of two types, the type implementing the interface or string.
	// It can be a string when the value comes from a parsed SQL string which at the moment is loaded to the
	// type as is. In the future this may change and both are always of the type implementing the interface.
	// This also creates weird cases when using ordering operators on values that are not ordered like 'nil' or
	// 'boolean'. To make this right this needs to be split into tow base operations, comparison and order.
	Cmp(l, r any) int
}

type UniqueIndexMeta

type UniqueIndexMeta struct {
	Name string `db:"index_name"`
	// Definition is the SQL statement to create this index
	Definition string `db:"index_def"`
	Partial    bool   `db:"is_partial"`
	Unique     bool   `db:"is_unique"`
	// PK is tru eif this unique index is the Primary Key
	PK bool `db:"is_pk"`
	// ConColsPosition is a list of the constrained columns position in the table as returned by Postgres, e.g.: "1 2"
	ConColsPosition string `db:"con_columns"`
	// ConCols is the list of constrained columns
	ConCols []*ColumnMeta
}

type Value

type Value struct {
	// contains filtered or unexported fields
}

Value is a generated value that can be compared and parsed to string.

func (Value) Cmp

func (v Value) Cmp(l, r any) int

func (Value) MarshalJSON

func (v Value) MarshalJSON() ([]byte, error)

func (Value) String

func (v Value) String() string

func (Value) Val

func (v Value) Val() any

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL