constraint

package
v0.62.6 Latest Latest
Warning

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

Go to latest
Published: Aug 10, 2024 License: BSD-2-Clause Imports: 6 Imported by: 2

Documentation

Overview

Package constraint provides types and methods to support foreign-key relationshipd between database tables.

Only simple keys are supported, which can be integers, strings or any other suitable type. Compound keys are not supported.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CheckConstraint

type CheckConstraint struct {
	Expression string
}

CheckConstraint holds an expression that refers to table columns and is applied as a precondition whenever a table insert, update or delete is attempted. The CheckConstraint expression is in SQL.

func (CheckConstraint) ConstraintSql

func (c CheckConstraint) ConstraintSql(q quote.Quoter, name sqlapi.TableName, index int) string

ConstraintSql constructs the CONSTRAINT clause to be included in the CREATE TABLE.

func (CheckConstraint) GoString

func (c CheckConstraint) GoString() string

type Consequence

type Consequence string

Consequence is the action to be performed after updating or deleting a record constrained by foreign key.

const (
	// unspecified option is available but its semantics vary by DB vendor, so it's not included here.
	NoAction   Consequence = "no action"
	Restrict   Consequence = "restrict"
	Cascade    Consequence = "cascade"
	SetNull    Consequence = "set null"
	SetDefault Consequence = "set default"
	Delete     Consequence = "delete" // not MySQL
)

func (Consequence) Apply

func (c Consequence) Apply(pfx, action string) string

Apply constructs the SQL sub-clause for a consequence of a specified action. The prefix is typically arbitrary whitespace.

type Constraint

type Constraint interface {
	// ConstraintSql constructs the CONSTRAINT clause to be included in the CREATE TABLE.
	ConstraintSql(q quote.Quoter, name sqlapi.TableName, index int) string

	// Expresses the constraint as a constructor + literals for the API type.
	GoString() string
}

Constraint represents data that augments the data-definition SQL statements such as CREATE TABLE.

type Constraints

type Constraints []Constraint

Constraints holds constraints.

func (Constraints) FkConstraints

func (cc Constraints) FkConstraints() FkConstraints

FkConstraints returns only the foreign key constraints in the Constraints slice.

type FkConstraint

type FkConstraint struct {
	ForeignKeyColumn string // only one column is supported
	Parent           Reference
	Update, Delete   Consequence
}

FkConstraint holds a pair of references and their update/delete consequences. ForeignKeyColumn is the 'owner' of the constraint.

func FkConstraintOfField added in v0.13.0

func FkConstraintOfField(field *schema.Field) FkConstraint

FkConstraintOfField constructs a foreign key constraint from a struct field.

func FkConstraintOn

func FkConstraintOn(column string) FkConstraint

FkConstraintOn constructs a foreign key constraint in a fluent style.

func (FkConstraint) ConstraintSql

func (c FkConstraint) ConstraintSql(q quote.Quoter, name sqlapi.TableName, index int) string

ConstraintSql constructs the CONSTRAINT clause to be included in the CREATE TABLE.

func (FkConstraint) GoString

func (c FkConstraint) GoString() string

func (FkConstraint) NoCascade added in v0.12.0

func (c FkConstraint) NoCascade() FkConstraint

NoCascade changes both the Update and Delete consequences to NoAction.

func (FkConstraint) OnDelete

func (c FkConstraint) OnDelete(consequence Consequence) FkConstraint

OnDelete sets the delete consequence.

func (FkConstraint) OnUpdate

func (c FkConstraint) OnUpdate(consequence Consequence) FkConstraint

OnUpdate sets the update consequence.

func (FkConstraint) RefersTo

func (c FkConstraint) RefersTo(tableName string, column string) FkConstraint

RefersTo sets the parent reference. The column may be blank.

func (FkConstraint) RelationshipWith

func (c FkConstraint) RelationshipWith(child sqlapi.TableName) Relationship

RelationshipWith constructs the Relationship that is expressed by the parent reference in the FkConstraint and the child's foreign key.

The table names do not include any prefix.

type FkConstraints

type FkConstraints []FkConstraint

FkConstraints holds foreign key constraints.

func (FkConstraints) Find

func (cc FkConstraints) Find(fkColumn string) (FkConstraint, bool)

Find finds the FkConstraint on a specified column.

type Reference

type Reference struct {
	TableName string
	Column    string // only one column is supported
}

Reference holds a table + column reference used by constraints. The table name should not include any schema or other prefix. The column may be blank, but the functionality is then reduced (there will be insufficient metadata to use Relationship methods).

type Relationship

type Relationship struct {
	Parent, Child Reference
}

Relationship represents a parent-child relationship. Only simple keys are supported (compound keys are not supported).

func (Relationship) IdsUnusedAsForeignKeys

func (rel Relationship) IdsUnusedAsForeignKeys(tbl sqlapi.Table) (set.Of[int64], error)

IdsUnusedAsForeignKeys finds all the primary keys in the parent table that have no foreign key in the dependent (child) table. The table tbl provides the database or transaction handle; either the parent or the child table can be used for thi purpose.

func (Relationship) IdsUsedAsForeignKeys

func (rel Relationship) IdsUsedAsForeignKeys(tbl sqlapi.Table) (set.Of[int64], error)

IdsUsedAsForeignKeys finds all the primary keys in the parent table that have at least one foreign key in the dependent (child) table.

Jump to

Keyboard shortcuts

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