schema

package
v0.0.0-...-acbd5a7 Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2022 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ForeignKeys = make(map[string][]*ForeignKey)

Functions

func AutoMigrate

func AutoMigrate(pool *pgxpool.Pool, driver string, models ...interface{}) error

Creates all tables, constraints and relations. NB: This does not alter existing table schema and is not recommendated as a solid migration option.

func Columns

func Columns(v interface{}, dialect string) ([]string, []string, error)

Returns a slice table columns, qualified_column_names and an error

func DeleteSchema

func DeleteSchema(v interface{}, dialect string) (string, error)

Returns the string for DELETE statement

func GetTableName

func GetTableName(v interface{}) string

func GetType

func GetType(model any) any

Initializes a pointer to the underlying model struct model must be a struct pointer e.g model := &Model{}

func InsertSchema

func InsertSchema(v interface{}, dialect string) (string, []interface{}, error)

Returns the string for the Insert query

func IsPointer

func IsPointer(v interface{}) bool

Returns true if v is a pointer

func IsPointerToArrayOfStructPointer

func IsPointerToArrayOfStructPointer(v interface{}) bool

v must be a of the form *[]*Model{}

func IsSlice

func IsSlice(v interface{}) bool

Returns true if v is a slice

func IsStruct

func IsStruct(v interface{}) bool

Returns true if v is a struct

func IsStructPointer

func IsStructPointer(v interface{}) bool

Returns true if v is a pointer to a struct

func NewStructPointer

func NewStructPointer(model interface{}) any

Creates a new struct pointer from a *[]*Model

func OrmType

func OrmType(v *reflect.Value) string

OrmType uses reflection to guess corresponding database type

func Schema

func Schema(v interface{}, dialect string) (string, error)

Calls GetTableSchema to generate the sql for creating the table and all constraints. If you are interested in the TableSchema data structure call schema.GetTableSchema

func SnakeCase

func SnakeCase(s string) string

convert strings to snake case without reflection

func UpdateSchema

func UpdateSchema(v interface{}, filter *query.QueryFilter, dialect string) (string, []interface{}, error)

Returns the string for the UpdateQuery

Types

type Constraint

type Constraint struct {
	Name  string
	Type  string
	Field *Field
}

type Field

type Field struct {
	Name            string
	Type            string
	Tags            map[string]string
	ReflectObjType  *reflect.StructField
	ReflectObjValue *reflect.Value
	Table           *TableSchema
	// contains filtered or unexported fields
}

Field is the data structure that stores data about a single struct field

func (*Field) FKExists

func (f *Field) FKExists(constraint_name string) bool

Checks if a foreign key with constraint constraint_name exists in a global map of foreign keys

func (*Field) IsAutoIncrement

func (f *Field) IsAutoIncrement() bool

func (*Field) IsConstraint

func (f *Field) IsConstraint(tagName string) bool

func (*Field) IsForeignKey

func (field *Field) IsForeignKey() bool

func (*Field) IsPrimaryKey

func (f *Field) IsPrimaryKey() bool

func (*Field) IsPrimaryKeyAndZero

func (f *Field) IsPrimaryKeyAndZero() bool

func (*Field) PrintTags

func (f *Field) PrintTags()

Print all field tags to the field buffer

func (*Field) PrintType

func (f *Field) PrintType(sqlType string, dialect string)

Writes column name and type to the buffer

func (*Field) String

func (f *Field) String() string

Returns the complete string representing the schema of a single column

e.g : name varchar(200) not null unique

func (*Field) WriteFieldConstraints

func (f *Field) WriteFieldConstraints(k, v string)

Write field tags representing constraints to the underlying field bytes.Buffer

type ForeignKey

type ForeignKey struct {
	ConstraintName string
	FK             string
	OnDelete       string
	OnUpdate       string
	TableName      string
	ParentTable    string
	ParentPkColumn string
}

func (*ForeignKey) String

func (fk *ForeignKey) String() string

type TableSchema

type TableSchema struct {
	TableName        string
	Fields           []*Field
	PrimaryKey       *Field
	ForeignKeys      map[string]*ForeignKey
	UniqueFields     []*Field
	CompositeIndexes map[string][]*Field
	Constraints      []*Constraint
	// contains filtered or unexported fields
}

func GetTableSchema

func GetTableSchema(m interface{}, dialect string) (*TableSchema, error)
Generates the table schema for struct m

Returns a pointer to TableSchema and an error if m is not a struct or pointer to a struct

func (*TableSchema) DeleteSchema

func (table *TableSchema) DeleteSchema(dialect string) string

Returns the sql string for deleting the table with a trailing empty space Warning: Does not include the where clause

func (*TableSchema) Flush

func (t *TableSchema) Flush()

func (*TableSchema) InsertSchema

func (table *TableSchema) InsertSchema(v interface{}, dialect string) (string, []interface{})

Returns the sql string for creating the table

func (*TableSchema) String

func (t *TableSchema) String(dialect string) string

Returns the sql string for creating the table

func (*TableSchema) UpdateSchema

func (table *TableSchema) UpdateSchema(v interface{}, dialect string) (string, []interface{})

Returns the sql string for updating the table

func (*TableSchema) WriteColumns

func (t *TableSchema) WriteColumns(dialect string)

func (*TableSchema) WriteCompositeUnique

func (t *TableSchema) WriteCompositeUnique()

func (*TableSchema) WriteHeader

func (t *TableSchema) WriteHeader()

func (*TableSchema) WritePrimaryKey

func (t *TableSchema) WritePrimaryKey()

func (*TableSchema) WriteUniqueFields

func (t *TableSchema) WriteUniqueFields()

Jump to

Keyboard shortcuts

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