ddl

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Jan 26, 2026 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Index

Constants

View Source
const (
	SMALLINT    datatype = "SMALLINT"
	INTEGER     datatype = "INTEGER"
	BIGINT      datatype = "BIGINT"
	SERIAL      datatype = "SERIAL"
	BIGSERIAL   datatype = "BIGSERIAL"
	REAL        datatype = "REAL"
	DOUBLE      datatype = "DOUBLE PRECISION"
	TEXT        datatype = "TEXT"
	BOOLEAN     datatype = "BOOLEAN"
	DATE        datatype = "DATE"
	TIME        datatype = "TIME"
	TIMESTAMP   datatype = "TIMESTAMP"
	TIMESTAMPTZ datatype = "TIMESTAMPTZ"
	INTERVAL    datatype = "INTERVAL"
	UUID        datatype = "UUID"
	JSON        datatype = "JSON"
	JSONB       datatype = "JSONB"
	BYTEA       datatype = "BYTEA"
)

Variables

This section is empty.

Functions

func SchemaSQL

func SchemaSQL(sqlers ...SchemaSqler) string

func SchemaSortedSQL

func SchemaSortedSQL(sqlers ...SchemaSqler) (string, error)

SchemaSortedSQL returns SQL with tables sorted by dependencies

func SchemaSortedStatements

func SchemaSortedStatements(sqlers ...SchemaSqler) ([]string, error)

SchemaSortedStatements returns SQL statements with tables sorted by dependencies. Tables without foreign keys come first, then tables that depend on them, etc.

func SchemaStatements

func SchemaStatements(sqlers ...SchemaSqler) []string

Types

type CheckConstraint added in v0.2.0

type CheckConstraint struct {
	Name string // explicit constraint name
	Expr string // CHECK expression
}

CheckConstraint holds a CHECK constraint

type ColumnDDL

type ColumnDDL[C types.ColumnAlias] struct {
	// contains filtered or unexported fields
}

ColumnDDL describes a column for CREATE/ALTER TABLE.

func NewColumnDDL

func NewColumnDDL[C types.ColumnAlias](
	fa C,
	dataType Datatype,
	opts ...ColumnOption[C],
) *ColumnDDL[C]

func (*ColumnDDL[C]) Alias

func (c *ColumnDDL[C]) Alias() C

func (*ColumnDDL[C]) Check

func (c *ColumnDDL[C]) Check(expr string) *ColumnDDL[C]

Check sets CHECK constraint (raw SQL expression).

func (*ColumnDDL[C]) CheckNamed added in v0.2.0

func (c *ColumnDDL[C]) CheckNamed(constraintName, expr string) *ColumnDDL[C]

CheckNamed sets CHECK constraint with explicit name.

func (*ColumnDDL[C]) Default

func (c *ColumnDDL[C]) Default(value string) *ColumnDDL[C]

Default sets DEFAULT value (raw SQL expression).

func (*ColumnDDL[C]) NotNull

func (c *ColumnDDL[C]) NotNull() *ColumnDDL[C]

NotNull sets NOT NULL constraint.

func (*ColumnDDL[C]) Nullable

func (c *ColumnDDL[C]) Nullable() *ColumnDDL[C]

Nullable sets explicit NULL constraint.

func (*ColumnDDL[C]) OnDelete

func (c *ColumnDDL[C]) OnDelete(action string) *ColumnDDL[C]

OnDelete sets ON DELETE action for REFERENCES.

func (*ColumnDDL[C]) OnUpdate

func (c *ColumnDDL[C]) OnUpdate(action string) *ColumnDDL[C]

OnUpdate sets ON UPDATE action for REFERENCES.

func (*ColumnDDL[C]) PrimaryKey

func (c *ColumnDDL[C]) PrimaryKey() *ColumnDDL[C]

PrimaryKey sets PRIMARY KEY constraint.

func (*ColumnDDL[C]) PrimaryKeyNamed added in v0.2.0

func (c *ColumnDDL[C]) PrimaryKeyNamed(constraintName string) *ColumnDDL[C]

PrimaryKeyNamed sets PRIMARY KEY constraint with explicit name.

func (*ColumnDDL[C]) References

func (c *ColumnDDL[C]) References(table, column string) *ColumnDDL[C]

References sets REFERENCES clause.

func (*ColumnDDL[C]) ReferencesNamed added in v0.2.0

func (c *ColumnDDL[C]) ReferencesNamed(constraintName, table, column string) *ColumnDDL[C]

ReferencesNamed sets REFERENCES clause with explicit constraint name.

func (*ColumnDDL[C]) SchemaSql

func (c *ColumnDDL[C]) SchemaSql() string

func (*ColumnDDL[C]) Unique

func (c *ColumnDDL[C]) Unique() *ColumnDDL[C]

Unique sets UNIQUE constraint.

func (*ColumnDDL[C]) UniqueNamed added in v0.2.0

func (c *ColumnDDL[C]) UniqueNamed(constraintName string) *ColumnDDL[C]

UniqueNamed sets UNIQUE constraint with explicit name.

type ColumnOption

type ColumnOption[C types.ColumnAlias] func(*ColumnDDL[C])

ColumnOption is a functional option for configuring a ColumnDDL.

func WithCheck

func WithCheck[C types.ColumnAlias](expr string) ColumnOption[C]

WithCheck sets CHECK constraint (raw SQL expression).

func WithCheckNamed added in v0.2.0

func WithCheckNamed[C types.ColumnAlias](constraintName, expr string) ColumnOption[C]

WithCheckNamed sets CHECK constraint with explicit name.

func WithDefault

func WithDefault[C types.ColumnAlias](value string) ColumnOption[C]

WithDefault sets DEFAULT value (raw SQL expression).

func WithNotNull

func WithNotNull[C types.ColumnAlias]() ColumnOption[C]

WithNotNull sets NOT NULL constraint.

func WithNullable

func WithNullable[C types.ColumnAlias]() ColumnOption[C]

WithNullable sets explicit NULL constraint.

func WithOnDelete

func WithOnDelete[C types.ColumnAlias](action string) ColumnOption[C]

WithOnDelete sets ON DELETE action for REFERENCES.

func WithOnUpdate

func WithOnUpdate[C types.ColumnAlias](action string) ColumnOption[C]

WithOnUpdate sets ON UPDATE action for REFERENCES.

func WithPrimaryKey

func WithPrimaryKey[C types.ColumnAlias]() ColumnOption[C]

WithPrimaryKey sets PRIMARY KEY constraint.

func WithPrimaryKeyNamed added in v0.2.0

func WithPrimaryKeyNamed[C types.ColumnAlias](constraintName string) ColumnOption[C]

WithPrimaryKeyNamed sets PRIMARY KEY constraint with explicit name.

func WithReferences

func WithReferences[C types.ColumnAlias](table, column string) ColumnOption[C]

WithReferences sets REFERENCES clause.

func WithReferencesNamed added in v0.2.0

func WithReferencesNamed[C types.ColumnAlias](constraintName, table, column string) ColumnOption[C]

WithReferencesNamed sets REFERENCES clause with explicit constraint name.

func WithUnique

func WithUnique[C types.ColumnAlias]() ColumnOption[C]

WithUnique sets UNIQUE constraint.

func WithUniqueNamed added in v0.2.0

func WithUniqueNamed[C types.ColumnAlias](constraintName string) ColumnOption[C]

WithUniqueNamed sets UNIQUE constraint with explicit name.

type Datatype

type Datatype fmt.Stringer

func Array

func Array(d Datatype) Datatype

func Char

func Char(length int) Datatype

func Numeric

func Numeric(precision, scale int) Datatype

func Varchar

func Varchar(length int) Datatype

type DependencySqler

type DependencySqler interface {
	SchemaSqler
	// TableName returns the name of this table
	TableName() string
	// Dependencies returns names of tables this table depends on (via foreign keys)
	Dependencies() []string
}

DependencySqler is an optional interface for tables that have foreign key dependencies

type Index

type Index[T types.TableAlias, C types.ColumnAlias] struct {
	// contains filtered or unexported fields
}

func NewIndex

func NewIndex[T types.TableAlias, C types.ColumnAlias](name string, table T) *Index[T, C]

func (*Index[T, C]) Concurrently

func (i *Index[T, C]) Concurrently() *Index[T, C]

Concurrently sets CONCURRENTLY option for index creation.

func (*Index[T, C]) OnColumns

func (i *Index[T, C]) OnColumns(columns ...C) *Index[T, C]

OnColumns sets the columns for the index.

func (*Index[T, C]) SchemaSql

func (i *Index[T, C]) SchemaSql() string

func (*Index[T, C]) Unique

func (i *Index[T, C]) Unique() *Index[T, C]

Unique sets the index as UNIQUE.

func (*Index[T, C]) Using

func (i *Index[T, C]) Using(method string) *Index[T, C]

Using sets the index method (btree, hash, gin, gist, etc.).

func (*Index[T, C]) Where

func (i *Index[T, C]) Where(predicate string) *Index[T, C]

Where sets the WHERE clause for a partial index.

type NamedConstraint added in v0.2.0

type NamedConstraint[C types.ColumnAlias] struct {
	Name    string // explicit constraint name (empty for auto-generated)
	Columns []C
}

NamedConstraint holds a constraint with optional explicit name

type Reference

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

Reference describes a REFERENCES clause with optional actions.

type SchemaSqler

type SchemaSqler interface {
	SchemaSql() []string
}

type TableDDL

type TableDDL[T types.TableAlias, C types.ColumnAlias] struct {
	// contains filtered or unexported fields
}

func NewTableDDL

func NewTableDDL[T types.TableAlias, C types.ColumnAlias](
	alias T,
	columns []*ColumnDDL[C],
	options ...TableOptions[T, C],
) *TableDDL[T, C]

func (*TableDDL[T, C]) Alias

func (c *TableDDL[T, C]) Alias() T

func (*TableDDL[T, C]) Dependencies

func (c *TableDDL[T, C]) Dependencies() []string

Dependencies returns names of tables this table depends on via foreign keys (implements DependencySqler)

func (*TableDDL[T, C]) Indexes

func (c *TableDDL[T, C]) Indexes(indexes ...*Index[T, C]) *TableDDL[T, C]

func (*TableDDL[T, C]) SchemaSql

func (c *TableDDL[T, C]) SchemaSql() []string

func (*TableDDL[T, C]) TableName

func (c *TableDDL[T, C]) TableName() string

TableName returns the name of this table (implements DependencySqler)

func (*TableDDL[T, C]) Unique

func (c *TableDDL[T, C]) Unique(columns ...[]C) *TableDDL[T, C]

type TableOptions

type TableOptions[T types.TableAlias, C types.ColumnAlias] func(*TableDDL[T, C])

func WithIndexes

func WithIndexes[T types.TableAlias, C types.ColumnAlias](indexes ...*Index[T, C]) TableOptions[T, C]

func WithPrimaryKeyColumns

func WithPrimaryKeyColumns[T types.TableAlias, C types.ColumnAlias](columns []C) TableOptions[T, C]

WithPrimaryKeyColumns sets a composite primary key for the table

func WithTableCheckConstraint added in v0.2.0

func WithTableCheckConstraint[T types.TableAlias, C types.ColumnAlias](name, expr string) TableOptions[T, C]

WithTableCheckConstraint adds a named CHECK constraint to the table

func WithTablePrimaryKeyNamed added in v0.2.0

func WithTablePrimaryKeyNamed[T types.TableAlias, C types.ColumnAlias](name string, columns []C) TableOptions[T, C]

WithTablePrimaryKeyNamed sets a named composite primary key for the table

func WithTableUniqueNamed added in v0.2.0

func WithTableUniqueNamed[T types.TableAlias, C types.ColumnAlias](name string, columns []C) TableOptions[T, C]

WithTableUniqueNamed adds a named unique constraint to the table

func WithUniqueColumns

func WithUniqueColumns[T types.TableAlias, C types.ColumnAlias](columns ...[]C) TableOptions[T, C]

Jump to

Keyboard shortcuts

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