schema

package
v1.25.0 Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2023 License: MIT Imports: 24 Imported by: 3,645

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	TimeReflectType    = reflect.TypeOf(time.Time{})
	TimePtrReflectType = reflect.TypeOf(&time.Time{})
	ByteReflectType    = reflect.TypeOf(uint8(0))
)

special types' reflect type

View Source
var ErrUnsupportedDataType = errors.New("unsupported data type")

ErrUnsupportedDataType unsupported data type

Functions

func GetIdentityFieldValuesMap

func GetIdentityFieldValuesMap(ctx context.Context, reflectValue reflect.Value, fields []*Field) (map[string][]reflect.Value, [][]interface{})

GetIdentityFieldValuesMap get identity map from fields

func GetIdentityFieldValuesMapFromValues

func GetIdentityFieldValuesMapFromValues(ctx context.Context, values []interface{}, fields []*Field) (map[string][]reflect.Value, [][]interface{})

GetIdentityFieldValuesMapFromValues get identity map from fields

func GetRelationsValues

func GetRelationsValues(ctx context.Context, reflectValue reflect.Value, rels []*Relationship) (reflectResults reflect.Value)

GetRelationsValues get relations's values from a reflect value

func ParseTagSetting

func ParseTagSetting(str string, sep string) map[string]string

func RegisterSerializer added in v1.23.0

func RegisterSerializer(name string, serializer SerializerInterface)

RegisterSerializer register serializer

func ToQueryValues

func ToQueryValues(table string, foreignKeys []string, foreignValues [][]interface{}) (interface{}, []interface{})

ToQueryValues to query values

Types

type Check

type Check struct {
	Name       string
	Constraint string // length(phone) >= 10
	*Field
}

type Constraint

type Constraint struct {
	Name            string
	Field           *Field
	Schema          *Schema
	ForeignKeys     []*Field
	ReferenceSchema *Schema
	References      []*Field
	OnDelete        string
	OnUpdate        string
}

type CreateClausesInterface

type CreateClausesInterface interface {
	CreateClauses(*Field) []clause.Interface
}

CreateClausesInterface create clauses interface

type DataType

type DataType string

DataType GORM data type

const (
	Bool   DataType = "bool"
	Int    DataType = "int"
	Uint   DataType = "uint"
	Float  DataType = "float"
	String DataType = "string"
	Time   DataType = "time"
	Bytes  DataType = "bytes"
)

GORM fields types

type DeleteClausesInterface

type DeleteClausesInterface interface {
	DeleteClauses(*Field) []clause.Interface
}

DeleteClausesInterface delete clauses interface

type Field

type Field struct {
	Name                   string
	DBName                 string
	BindNames              []string
	DataType               DataType
	GORMDataType           DataType
	PrimaryKey             bool
	AutoIncrement          bool
	AutoIncrementIncrement int64
	Creatable              bool
	Updatable              bool
	Readable               bool
	AutoCreateTime         TimeType
	AutoUpdateTime         TimeType
	HasDefaultValue        bool
	DefaultValue           string
	DefaultValueInterface  interface{}
	NotNull                bool
	Unique                 bool
	Comment                string
	Size                   int
	Precision              int
	Scale                  int
	IgnoreMigration        bool
	FieldType              reflect.Type
	IndirectFieldType      reflect.Type
	StructField            reflect.StructField
	Tag                    reflect.StructTag
	TagSettings            map[string]string
	Schema                 *Schema
	EmbeddedSchema         *Schema
	OwnerSchema            *Schema
	ReflectValueOf         func(context.Context, reflect.Value) reflect.Value
	ValueOf                func(context.Context, reflect.Value) (value interface{}, zero bool)
	Set                    func(context.Context, reflect.Value, interface{}) error
	Serializer             SerializerInterface
	NewValuePool           FieldNewValuePool
}

Field is the representation of model schema's field

func (*Field) BindName added in v1.25.0

func (field *Field) BindName() string

type FieldNewValuePool added in v1.23.0

type FieldNewValuePool interface {
	Get() interface{}
	Put(interface{})
}

FieldNewValuePool field new scan value pool

type GobSerializer added in v1.23.2

type GobSerializer struct{}

GobSerializer gob serializer

func (GobSerializer) Scan added in v1.23.2

func (GobSerializer) Scan(ctx context.Context, field *Field, dst reflect.Value, dbValue interface{}) (err error)

Scan implements serializer interface

func (GobSerializer) Value added in v1.23.2

func (GobSerializer) Value(ctx context.Context, field *Field, dst reflect.Value, fieldValue interface{}) (interface{}, error)

Value implements serializer interface

type GormDataTypeInterface added in v0.2.3

type GormDataTypeInterface interface {
	GormDataType() string
}

GormDataTypeInterface gorm data type interface

type Index

type Index struct {
	Name    string
	Class   string // UNIQUE | FULLTEXT | SPATIAL
	Type    string // btree, hash, gist, spgist, gin, and brin
	Where   string
	Comment string
	Option  string // WITH PARSER parser_name
	Fields  []IndexOption
}

type IndexOption

type IndexOption struct {
	*Field
	Expression string
	Sort       string // DESC, ASC
	Collate    string
	Length     int
	// contains filtered or unexported fields
}

type JSONSerializer added in v1.23.0

type JSONSerializer struct{}

JSONSerializer json serializer

func (JSONSerializer) Scan added in v1.23.0

func (JSONSerializer) Scan(ctx context.Context, field *Field, dst reflect.Value, dbValue interface{}) (err error)

Scan implements serializer interface

func (JSONSerializer) Value added in v1.23.0

func (JSONSerializer) Value(ctx context.Context, field *Field, dst reflect.Value, fieldValue interface{}) (interface{}, error)

Value implements serializer interface

type Namer

type Namer interface {
	TableName(table string) string
	SchemaName(table string) string
	ColumnName(table, column string) string
	JoinTableName(joinTable string) string
	RelationshipFKName(Relationship) string
	CheckerName(table, column string) string
	IndexName(table, column string) string
}

Namer namer interface

type NamingStrategy

type NamingStrategy struct {
	TablePrefix   string
	SingularTable bool
	NameReplacer  Replacer
	NoLowerCase   bool
}

NamingStrategy tables, columns naming strategy

func (NamingStrategy) CheckerName

func (ns NamingStrategy) CheckerName(table, column string) string

CheckerName generate checker name

func (NamingStrategy) ColumnName

func (ns NamingStrategy) ColumnName(table, column string) string

ColumnName convert string to column name

func (NamingStrategy) IndexName

func (ns NamingStrategy) IndexName(table, column string) string

IndexName generate index name

func (NamingStrategy) JoinTableName

func (ns NamingStrategy) JoinTableName(str string) string

JoinTableName convert string to join table name

func (NamingStrategy) RelationshipFKName

func (ns NamingStrategy) RelationshipFKName(rel Relationship) string

RelationshipFKName generate fk name for relation

func (NamingStrategy) SchemaName added in v1.21.13

func (ns NamingStrategy) SchemaName(table string) string

SchemaName generate schema name from table name, don't guarantee it is the reverse value of TableName

func (NamingStrategy) TableName

func (ns NamingStrategy) TableName(str string) string

TableName convert string to table name

type Polymorphic

type Polymorphic struct {
	PolymorphicID   *Field
	PolymorphicType *Field
	Value           string
}

type QueryClausesInterface

type QueryClausesInterface interface {
	QueryClauses(*Field) []clause.Interface
}

QueryClausesInterface query clauses interface

type Reference

type Reference struct {
	PrimaryKey    *Field
	PrimaryValue  string
	ForeignKey    *Field
	OwnPrimaryKey bool
}

type Relationship

type Relationship struct {
	Name        string
	Type        RelationshipType
	Field       *Field
	Polymorphic *Polymorphic
	References  []*Reference
	Schema      *Schema
	FieldSchema *Schema
	JoinTable   *Schema
	// contains filtered or unexported fields
}

func (*Relationship) ParseConstraint

func (rel *Relationship) ParseConstraint() *Constraint

func (*Relationship) ToQueryConditions

func (rel *Relationship) ToQueryConditions(ctx context.Context, reflectValue reflect.Value) (conds []clause.Expression)

type RelationshipType

type RelationshipType string

RelationshipType relationship type

const (
	HasOne    RelationshipType = "has_one"      // HasOneRel has one relationship
	HasMany   RelationshipType = "has_many"     // HasManyRel has many relationship
	BelongsTo RelationshipType = "belongs_to"   // BelongsToRel belongs to relationship
	Many2Many RelationshipType = "many_to_many" // Many2ManyRel many to many relationship

)

type Relationships

type Relationships struct {
	HasOne    []*Relationship
	BelongsTo []*Relationship
	HasMany   []*Relationship
	Many2Many []*Relationship
	Relations map[string]*Relationship

	EmbeddedRelations map[string]*Relationships
}

type Replacer added in v1.21.0

type Replacer interface {
	Replace(name string) string
}

Replacer replacer interface like strings.Replacer

type Schema

type Schema struct {
	Name                      string
	ModelType                 reflect.Type
	Table                     string
	PrioritizedPrimaryField   *Field
	DBNames                   []string
	PrimaryFields             []*Field
	PrimaryFieldDBNames       []string
	Fields                    []*Field
	FieldsByName              map[string]*Field
	FieldsByBindName          map[string]*Field // embedded fields is 'Embed.Field'
	FieldsByDBName            map[string]*Field
	FieldsWithDefaultDBValue  []*Field // fields with default value assigned by database
	Relationships             Relationships
	CreateClauses             []clause.Interface
	QueryClauses              []clause.Interface
	UpdateClauses             []clause.Interface
	DeleteClauses             []clause.Interface
	BeforeCreate, AfterCreate bool
	BeforeUpdate, AfterUpdate bool
	BeforeDelete, AfterDelete bool
	BeforeSave, AfterSave     bool
	AfterFind                 bool
	// contains filtered or unexported fields
}

func Parse

func Parse(dest interface{}, cacheStore *sync.Map, namer Namer) (*Schema, error)

Parse get data type from dialector

func ParseWithSpecialTableName added in v1.22.0

func ParseWithSpecialTableName(dest interface{}, cacheStore *sync.Map, namer Namer, specialTableName string) (*Schema, error)

ParseWithSpecialTableName get data type from dialector with extra schema table

func (*Schema) LookIndex

func (schema *Schema) LookIndex(name string) *Index

func (Schema) LookUpField

func (schema Schema) LookUpField(name string) *Field

func (Schema) LookUpFieldByBindName added in v1.25.0

func (schema Schema) LookUpFieldByBindName(bindNames []string, name string) *Field

LookUpFieldByBindName looks for the closest field in the embedded struct.

type Struct struct {
	Embedded struct {
		ID string // is selected by LookUpFieldByBindName([]string{"Embedded", "ID"}, "ID")
	}
	ID string // is selected by LookUpFieldByBindName([]string{"ID"}, "ID")
}

func (Schema) MakeSlice

func (schema Schema) MakeSlice() reflect.Value

func (*Schema) ParseCheckConstraints

func (schema *Schema) ParseCheckConstraints() map[string]Check

ParseCheckConstraints parse schema check constraints

func (*Schema) ParseField

func (schema *Schema) ParseField(fieldStruct reflect.StructField) *Field

ParseField parses reflect.StructField to Field

func (*Schema) ParseIndexes

func (schema *Schema) ParseIndexes() map[string]Index

ParseIndexes parse schema indexes

func (Schema) String

func (schema Schema) String() string

type SerializerInterface added in v1.23.0

type SerializerInterface interface {
	Scan(ctx context.Context, field *Field, dst reflect.Value, dbValue interface{}) error
	SerializerValuerInterface
}

SerializerInterface serializer interface

func GetSerializer added in v1.23.0

func GetSerializer(name string) (serializer SerializerInterface, ok bool)

GetSerializer get serializer

type SerializerValuerInterface added in v1.23.0

type SerializerValuerInterface interface {
	Value(ctx context.Context, field *Field, dst reflect.Value, fieldValue interface{}) (interface{}, error)
}

SerializerValuerInterface serializer valuer interface

type Tabler added in v0.2.1

type Tabler interface {
	TableName() string
}

type TablerWithNamer added in v1.24.0

type TablerWithNamer interface {
	TableName(Namer) string
}

type TimeType

type TimeType int64

TimeType GORM time type

const (
	UnixTime        TimeType = 1
	UnixSecond      TimeType = 2
	UnixMillisecond TimeType = 3
	UnixNanosecond  TimeType = 4
)

GORM time types

type UnixSecondSerializer added in v1.23.0

type UnixSecondSerializer struct{}

UnixSecondSerializer json serializer

func (UnixSecondSerializer) Scan added in v1.23.0

func (UnixSecondSerializer) Scan(ctx context.Context, field *Field, dst reflect.Value, dbValue interface{}) (err error)

Scan implements serializer interface

func (UnixSecondSerializer) Value added in v1.23.0

func (UnixSecondSerializer) Value(ctx context.Context, field *Field, dst reflect.Value, fieldValue interface{}) (result interface{}, err error)

Value implements serializer interface

type UpdateClausesInterface

type UpdateClausesInterface interface {
	UpdateClauses(*Field) []clause.Interface
}

UpdateClausesInterface update clauses interface

Jump to

Keyboard shortcuts

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