schema

package
v0.0.0-...-267bd09 Latest Latest
Warning

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

Go to latest
Published: Feb 9, 2016 License: BSD-3-Clause Imports: 3 Imported by: 0

Documentation

Index

Constants

View Source
const (
	OnDeleteSetNull  = "SET NULL"
	OnDeleteCascade  = "CASCADE"
	OnDeleteRestrict = "RESTRICT"

	OnUpdateSetNull  = "SET NULL"
	OnUpdateCascade  = "CASCADE"
	OnUpdateNoAction = "NO ACTION"
)

different types of constraints

Variables

This section is empty.

Functions

func ForeignKeyName

func ForeignKeyName(tableName string, field Field) string

func PrimaryKeyName

func PrimaryKeyName(tableName string, field Field) string

func UniqueName

func UniqueName(tableName string, tuple FieldTuple) string

UniqueName computes the name of a Unique constraint

Types

type Aliases

type Aliases map[string]string

Aliases is a map of all table names to their aliases in a selectable context

type Constraint

type Constraint interface {
	SqlDefinition(factory types.Factory) (string, error) // represents the constraint as an SQL statement
}

Constraint represents a table constraint. As such it must implement all the methods below to be useable in Penguon

type Field

type Field struct {
	Name       string
	TypeSpec   types.TypeSpec
	IsNullable bool
	TableName  string
}

func (*Field) SqlDefinition

func (f *Field) SqlDefinition(factory types.Factory) (string, error)

type FieldTuple

type FieldTuple []Field

type ForeignKey

type ForeignKey struct {
	Name            string
	ReferencedTable string
	TargetField     Field
	ReferencedField Field
	OnUpdate        string
	OnDelete        string
}

func NamedForeignKey

func NamedForeignKey(tableName string, refTable string, tgtField Field, refField Field, onUpdate string, onDelete string) *ForeignKey

NamedForeignKey produces a ForeignKey object given it parameters tablename is the tablename containing the said FK, reftable is the referenced table pointed at by this FK, tgtField is the field storing the FK in the containing table refField if the referenced field from the targeted table onUpdate & onDelete are strings for the corresponding SQL elements

func (*ForeignKey) SqlDefinition

func (c *ForeignKey) SqlDefinition(factory types.Factory) (string, error)

type PrimaryKey

type PrimaryKey struct {
	Name  string
	Field Field
}

func NamedPrimaryKey

func NamedPrimaryKey(tableName string, field Field) *PrimaryKey

func (*PrimaryKey) SqlDefinition

func (c *PrimaryKey) SqlDefinition(factory types.Factory) (string, error)

type Table

type Table struct {
	Name          string
	Fields        []Field
	FKConstraints []Constraint
	Constraints   []Constraint
	Indices       []TableIndex
}

func (*Table) FKDefinitions

func (t *Table) FKDefinitions(factory types.Factory) ([]string, error)

FKDefinitions returns a list of SQL query strings that creates the table's foreign keys.

func (*Table) FieldFromName

func (t *Table) FieldFromName(name string) (Field, bool)

FieldFromName returns a field that matches the given name TODO optimize ?

func (*Table) FieldNames

func (t *Table) FieldNames() []string

FieldNames returns a slice of field names

func (*Table) FieldsByName

func (t *Table) FieldsByName() map[string]Field

Creates a map that associates field names to Field structs

func (*Table) SqlAddConstraint

func (t *Table) SqlAddConstraint(c Constraint, factory types.Factory) (string, error)

Produces an ALTER TABLE statement that adds a new constraints on a given table.

func (*Table) SqlDefinitionWithFks

func (t *Table) SqlDefinitionWithFks(factory types.Factory) (string, error)

SqlDefinitionWithFks returns the SQL query string that creates the given table including the foreign key constraints.

func (*Table) SqlDefinitionWithoutFks

func (t *Table) SqlDefinitionWithoutFks(factory types.Factory) (string, error)

SqlDefinitionWithoutFks outputs a CREATE TABLE statement for the table while ommitting the foreign key constraints, thus permitting the definition of circular relationships between tables. It is assumed that all foreign key constraints are all in the FKContraints field (ie. the Constraints contains no foreign key constraints).

type TableBuilder

type TableBuilder struct {
	Fields             map[string]Field
	ForeignKeys        []ForeignKey
	Constraints        []Constraint
	IndexedFieldTuples []FieldTuple
}

func NewTableBuilder

func NewTableBuilder() TableBuilder

func (*TableBuilder) SetConstraints

func (b *TableBuilder) SetConstraints(constraints ...Constraint)

SetConstraints adds constraints (Not idempotent)

func (*TableBuilder) SetFields

func (b *TableBuilder) SetFields(fields ...Field)

SetFields adds multiple fields at the same time in a table TODO: keep fields order

func (*TableBuilder) SetForeignKeys

func (b *TableBuilder) SetForeignKeys(fks ...ForeignKey)

SetForeignKeys adds foreign key constraints (Not idempotent)

func (*TableBuilder) SetIndices

func (b *TableBuilder) SetIndices(tuples ...FieldTuple)

SetIndices adds indices (Not idempotent)

func (*TableBuilder) Table

func (b *TableBuilder) Table(tableName string) Table

type TableIndex

type TableIndex struct {
	Name       string
	Table      string
	FieldTuple []Field
}

func NamedIndex

func NamedIndex(tableName string, tuple FieldTuple) TableIndex

func (*TableIndex) SqlDefinition

func (i *TableIndex) SqlDefinition() string

type Unique

type Unique struct {
	Name       string
	FieldTuple FieldTuple
}

func NamedUnique

func NamedUnique(tableName string, tuple FieldTuple) *Unique

func (*Unique) SqlDefinition

func (u *Unique) SqlDefinition(factory types.Factory) (string, error)

Jump to

Keyboard shortcuts

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