Documentation ¶
Index ¶
- Constants
- func ForeignKeyName(tableName string, field Field) string
- func PrimaryKeyName(tableName string, field Field) string
- func UniqueName(tableName string, tuple FieldTuple) string
- type Aliases
- type Constraint
- type Field
- type FieldTuple
- type ForeignKey
- type PrimaryKey
- type Table
- func (t *Table) FKDefinitions(factory types.Factory) ([]string, error)
- func (t *Table) FieldFromName(name string) (Field, bool)
- func (t *Table) FieldNames() []string
- func (t *Table) FieldsByName() map[string]Field
- func (t *Table) SqlAddConstraint(c Constraint, factory types.Factory) (string, error)
- func (t *Table) SqlDefinitionWithFks(factory types.Factory) (string, error)
- func (t *Table) SqlDefinitionWithoutFks(factory types.Factory) (string, error)
- type TableBuilder
- type TableIndex
- type Unique
Constants ¶
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 PrimaryKeyName ¶
func UniqueName ¶
func UniqueName(tableName string, tuple FieldTuple) string
UniqueName computes the name of a Unique constraint
Types ¶
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 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 ¶
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 ¶
FKDefinitions returns a list of SQL query strings that creates the table's foreign keys.
func (*Table) FieldFromName ¶
FieldFromName returns a field that matches the given name TODO optimize ?
func (*Table) FieldNames ¶
FieldNames returns a slice of field names
func (*Table) FieldsByName ¶
Creates a map that associates field names to Field structs
func (*Table) SqlAddConstraint ¶
Produces an ALTER TABLE statement that adds a new constraints on a given table.
func (*Table) SqlDefinitionWithFks ¶
SqlDefinitionWithFks returns the SQL query string that creates the given table including the foreign key constraints.
func (*Table) SqlDefinitionWithoutFks ¶
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 ¶
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