models

package
v2.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 31, 2024 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	TypeBooleanField = 1 << iota
	TypeVarCharField
	TypeCharField
	TypeTextField
	TypeTimeField
	TypeDateField
	TypeDateTimeField
	TypeBitField
	TypeSmallIntegerField
	TypeIntegerField
	TypeBigIntegerField
	TypePositiveBitField
	TypePositiveSmallIntegerField
	TypePositiveIntegerField
	TypePositiveBigIntegerField
	TypeFloatField
	TypeDecimalField
	TypeJSONField
	TypeJsonbField
	RelForeignKey
	RelOneToOne
	RelManyToMany
	RelReverseOne
	RelReverseMany
)

Define the Type enum

View Source
const (
	IsIntegerField         = ^-TypePositiveBigIntegerField >> 6 << 7
	IsPositiveIntegerField = ^-TypePositiveBigIntegerField >> 10 << 11
	IsRelField             = ^-RelReverseMany >> 18 << 19
	IsFieldType            = ^-RelReverseMany<<1 + 1
)

Define some logic enum

View Source
const (
	OdCascade    = "cascade"
	OdSetNULL    = "set_null"
	OdSetDefault = "set_default"
	OdDoNothing  = "do_nothing"
)

Variables

View Source
var (
	NameStrategyMap = map[string]fn{
		DefaultNameStrategy:      SnakeString,
		SnakeAcronymNameStrategy: SnakeStringWithAcronym,
	}
	DefaultNameStrategy      = "snakeString"
	SnakeAcronymNameStrategy = "snakeStringWithAcronym"
	NameStrategy             = DefaultNameStrategy

	DefaultStructTagName = "orm"
)

Functions

func AddModelFields

func AddModelFields(mi *ModelInfo, ind reflect.Value, mName string, index []int)

AddModelFields index: FieldByIndex returns the nested field corresponding to index

func CamelString

func CamelString(s string) string

CamelString camel string, xx_yy to XxYy

func GetFullName

func GetFullName(typ reflect.Type) string

GetFullName get reflect.Type name with package path.

func GetTableEngine

func GetTableEngine(val reflect.Value) string

GetTableEngine get table engine, myisam or innodb.

func GetTableIndex

func GetTableIndex(val reflect.Value) [][]string

GetTableIndex get table index from method.

func GetTableName

func GetTableName(val reflect.Value) string

GetTableName get struct table name. If the struct implement the TableName, then get the result as tablename else use the struct name which will apply snakeString.

func GetTableUnique

func GetTableUnique(val reflect.Value) [][]string

GetTableUnique get table unique from method

func IsApplicableTableForDB

func IsApplicableTableForDB(val reflect.Value, db string) bool

IsApplicableTableForDB get whether the table needs to be created for the database alias

func ParseStructTag

func ParseStructTag(data string) (attrs map[string]bool, tags map[string]string)

ParseStructTag parse struct tag string

func SnakeString

func SnakeString(s string) string

SnakeString snake string, XxYy to xx_yy , XxYY to xx_y_y

func SnakeStringWithAcronym

func SnakeStringWithAcronym(s string) string

Types

type BigIntegerField

type BigIntegerField int64

BigIntegerField -9223372036854775808 to 9223372036854775807.

func (*BigIntegerField) FieldType

func (e *BigIntegerField) FieldType() int

FieldType return enum type

func (*BigIntegerField) RawValue

func (e *BigIntegerField) RawValue() interface{}

RawValue return BigIntegerField value

func (*BigIntegerField) Set

func (e *BigIntegerField) Set(d int64)

Set the BigIntegerField value

func (*BigIntegerField) SetRaw

func (e *BigIntegerField) SetRaw(value interface{}) error

SetRaw convert interface int64/string to int64

func (*BigIntegerField) String

func (e *BigIntegerField) String() string

String convert BigIntegerField to string

func (BigIntegerField) Value

func (e BigIntegerField) Value() int64

Value return int64

type BooleanField

type BooleanField bool

BooleanField A true/false field.

func (*BooleanField) FieldType

func (e *BooleanField) FieldType() int

FieldType return BooleanField the type

func (*BooleanField) RawValue

func (e *BooleanField) RawValue() interface{}

RawValue return the current value

func (*BooleanField) Set

func (e *BooleanField) Set(d bool)

Set will set the BooleanField

func (*BooleanField) SetRaw

func (e *BooleanField) SetRaw(value interface{}) error

SetRaw set the interface to bool

func (*BooleanField) String

func (e *BooleanField) String() string

String format the Bool to string

func (BooleanField) Value

func (e BooleanField) Value() bool

Value return the BooleanField

type CharField

type CharField string

CharField A string field required values tag: size The size is enforced at the database level and in models’s validation. eg: `orm:"size(120)"`

func (*CharField) FieldType

func (e *CharField) FieldType() int

FieldType return the enum type

func (*CharField) RawValue

func (e *CharField) RawValue() interface{}

RawValue return the CharField value

func (*CharField) Set

func (e *CharField) Set(d string)

Set CharField value

func (*CharField) SetRaw

func (e *CharField) SetRaw(value interface{}) error

SetRaw set the interface to string

func (*CharField) String

func (e *CharField) String() string

String return the CharField

func (CharField) Value

func (e CharField) Value() string

Value return the CharField's Value

type DateField

type DateField time.Time

DateField A date, represented in go by a time.Time instance. only date values like 2006-01-02 Has a few extra, optional attr tag:

auto_now: Automatically set the field to now every time the object is saved. Useful for “last-modified” timestamps. Note that the current date is always used; it’s not just a default value that you can override.

auto_now_add: Automatically set the field to now when the object is first created. Useful for creation of timestamps. Note that the current date is always used; it’s not just a default value that you can override.

eg: `orm:"auto_now"` or `orm:"auto_now_add"`

func (*DateField) FieldType

func (e *DateField) FieldType() int

FieldType return enum type Date

func (*DateField) RawValue

func (e *DateField) RawValue() interface{}

RawValue return Date value

func (*DateField) Set

func (e *DateField) Set(d time.Time)

Set set the DateField's value

func (*DateField) SetRaw

func (e *DateField) SetRaw(value interface{}) error

SetRaw convert the interface to time.Time. Allow string and time.Time

func (*DateField) String

func (e *DateField) String() string

String convert datetime to string

func (DateField) Value

func (e DateField) Value() time.Time

Value return the time.Time

type DateTimeField

type DateTimeField time.Time

DateTimeField A date, represented in go by a time.Time instance. datetime values like 2006-01-02 15:04:05 Takes the same extra arguments as DateField.

func (*DateTimeField) FieldType

func (e *DateTimeField) FieldType() int

FieldType return the enum TypeDateTimeField

func (*DateTimeField) RawValue

func (e *DateTimeField) RawValue() interface{}

RawValue return the datetime value

func (*DateTimeField) Set

func (e *DateTimeField) Set(d time.Time)

Set set the time.Time to datetime

func (*DateTimeField) SetRaw

func (e *DateTimeField) SetRaw(value interface{}) error

SetRaw convert the string or time.Time to DateTimeField

func (*DateTimeField) String

func (e *DateTimeField) String() string

String return the time's String

func (DateTimeField) Value

func (e DateTimeField) Value() time.Time

Value return the datetime value

type FieldInfo

type FieldInfo struct {
	DBcol               bool // table column fk and onetoone
	InModel             bool
	Auto                bool
	Pk                  bool
	Null                bool
	Index               bool
	Unique              bool
	ColDefault          bool // whether has default tag
	ToText              bool
	AutoNow             bool
	AutoNowAdd          bool
	Rel                 bool // if type equal to RelForeignKey, RelOneToOne, RelManyToMany then true
	Reverse             bool
	IsFielder           bool // implement Fielder interface
	Mi                  *ModelInfo
	FieldIndex          []int
	FieldType           int
	Name                string
	FullName            string
	Column              string
	AddrValue           reflect.Value
	Sf                  reflect.StructField
	Initial             utils.StrTo // store the default value
	Size                int
	ReverseField        string
	ReverseFieldInfo    *FieldInfo
	ReverseFieldInfoTwo *FieldInfo
	ReverseFieldInfoM2M *FieldInfo
	RelTable            string
	RelThrough          string
	RelThroughModelInfo *ModelInfo
	RelModelInfo        *ModelInfo
	Digits              int
	Decimals            int
	OnDelete            string
	Description         string
	TimePrecision       *int
	DBType              string
}

FieldInfo single field info

func NewFieldInfo

func NewFieldInfo(mi *ModelInfo, field reflect.Value, sf reflect.StructField, mName string) (fi *FieldInfo, err error)

NewFieldInfo new field info

type Fielder

type Fielder interface {
	String() string
	FieldType() int
	SetRaw(interface{}) error
	RawValue() interface{}
}

Fielder define field info

type Fields

type Fields struct {
	Pk            *FieldInfo
	Columns       map[string]*FieldInfo
	Fields        map[string]*FieldInfo
	FieldsLow     map[string]*FieldInfo
	FieldsByType  map[int][]*FieldInfo
	FieldsRel     []*FieldInfo
	FieldsReverse []*FieldInfo
	FieldsDB      []*FieldInfo
	Rels          []*FieldInfo
	Orders        []string
	DBcols        []string
}

Fields field info collection

func NewFields

func NewFields() *Fields

NewFields create new field info collection

func (*Fields) Add

func (f *Fields) Add(fi *FieldInfo) (added bool)

Add adds field info

func (*Fields) GetByAny

func (f *Fields) GetByAny(name string) (*FieldInfo, bool)

GetByAny get field info by string, name is prior

func (*Fields) GetByColumn

func (f *Fields) GetByColumn(column string) *FieldInfo

GetByColumn get field info by column name

func (*Fields) GetByName

func (f *Fields) GetByName(name string) *FieldInfo

GetByName get field info by name

type FloatField

type FloatField float64

FloatField A floating-point number represented in go by a float32 value.

func (*FloatField) FieldType

func (e *FloatField) FieldType() int

FieldType return the enum type

func (*FloatField) RawValue

func (e *FloatField) RawValue() interface{}

RawValue return the FloatField value

func (*FloatField) Set

func (e *FloatField) Set(d float64)

Set the Float64

func (*FloatField) SetRaw

func (e *FloatField) SetRaw(value interface{}) error

SetRaw converter interface Float64 float32 or string to FloatField

func (*FloatField) String

func (e *FloatField) String() string

String return the string

func (FloatField) Value

func (e FloatField) Value() float64

Value return the FloatField value

type IntegerField

type IntegerField int32

IntegerField -2147483648 to 2147483647

func (*IntegerField) FieldType

func (e *IntegerField) FieldType() int

FieldType return the enum type

func (*IntegerField) RawValue

func (e *IntegerField) RawValue() interface{}

RawValue return IntegerField value

func (*IntegerField) Set

func (e *IntegerField) Set(d int32)

Set IntegerField value

func (*IntegerField) SetRaw

func (e *IntegerField) SetRaw(value interface{}) error

SetRaw convert interface int32/string to int32

func (*IntegerField) String

func (e *IntegerField) String() string

String convert Int32 to string

func (IntegerField) Value

func (e IntegerField) Value() int32

Value return the int32

type JSONField

type JSONField string

JSONField postgres json field.

func (*JSONField) FieldType

func (j *JSONField) FieldType() int

FieldType return enum type

func (*JSONField) RawValue

func (j *JSONField) RawValue() interface{}

RawValue return JSONField value

func (*JSONField) Set

func (j *JSONField) Set(d string)

Set the JSONField value

func (*JSONField) SetRaw

func (j *JSONField) SetRaw(value interface{}) error

SetRaw convert interface string to string

func (*JSONField) String

func (j *JSONField) String() string

String convert JSONField to string

func (JSONField) Value

func (j JSONField) Value() string

Value return JSONField value

type JsonbField

type JsonbField string

JsonbField postgres json field.

func (*JsonbField) FieldType

func (j *JsonbField) FieldType() int

FieldType return enum type

func (*JsonbField) RawValue

func (j *JsonbField) RawValue() interface{}

RawValue return JsonbField value

func (*JsonbField) Set

func (j *JsonbField) Set(d string)

Set the JsonbField value

func (*JsonbField) SetRaw

func (j *JsonbField) SetRaw(value interface{}) error

SetRaw convert interface string to string

func (*JsonbField) String

func (j *JsonbField) String() string

String convert JsonbField to string

func (JsonbField) Value

func (j JsonbField) Value() string

Value return JsonbField value

type ModelCache added in v2.1.2

type ModelCache struct {
	sync.RWMutex // only used outsite for bootStrap
	// contains filtered or unexported fields
}

ModelCache info collection

func NewModelCacheHandler added in v2.1.2

func NewModelCacheHandler() *ModelCache

NewModelCacheHandler generator of ModelCache

func (*ModelCache) All added in v2.1.2

func (mc *ModelCache) All() map[string]*ModelInfo

All return all model info

func (*ModelCache) AllOrdered added in v2.1.2

func (mc *ModelCache) AllOrdered() []*ModelInfo

func (*ModelCache) Bootstrap added in v2.1.2

func (mc *ModelCache) Bootstrap()

Bootstrap Bootstrap for models

func (*ModelCache) Clean added in v2.1.2

func (mc *ModelCache) Clean()

Clean All model info.

func (*ModelCache) Empty added in v2.1.2

func (mc *ModelCache) Empty() bool

func (*ModelCache) Get added in v2.1.2

func (mc *ModelCache) Get(table string) (mi *ModelInfo, ok bool)

Get model info by table name

func (*ModelCache) GetByFullName added in v2.1.2

func (mc *ModelCache) GetByFullName(name string) (mi *ModelInfo, ok bool)

GetByFullName model info by full name

func (*ModelCache) GetByMd added in v2.1.2

func (mc *ModelCache) GetByMd(md interface{}) (*ModelInfo, bool)

func (*ModelCache) Register added in v2.1.2

func (mc *ModelCache) Register(prefixOrSuffixStr string, prefixOrSuffix bool, models ...interface{}) (err error)

Register Register models to model cache

func (*ModelCache) Set added in v2.1.2

func (mc *ModelCache) Set(table string, mi *ModelInfo) *ModelInfo

Set model info to collection

type ModelInfo

type ModelInfo struct {
	Manual    bool
	IsThrough bool
	Pkg       string
	Name      string
	FullName  string
	Table     string
	Model     interface{}
	Fields    *Fields
	AddrField reflect.Value // store the original struct value
	Uniques   []string
}

ModelInfo single model info

func NewM2MModelInfo

func NewM2MModelInfo(m1, m2 *ModelInfo) (mi *ModelInfo)

NewM2MModelInfo combine related model info to new model info. prepare for relation models query.

func NewModelInfo

func NewModelInfo(val reflect.Value) (mi *ModelInfo)

NewModelInfo new model info

type PositiveBigIntegerField

type PositiveBigIntegerField uint64

PositiveBigIntegerField 0 to 18446744073709551615

func (*PositiveBigIntegerField) FieldType

func (e *PositiveBigIntegerField) FieldType() int

FieldType return enum type

func (*PositiveBigIntegerField) RawValue

func (e *PositiveBigIntegerField) RawValue() interface{}

RawValue return PositiveBigIntegerField value

func (*PositiveBigIntegerField) Set

func (e *PositiveBigIntegerField) Set(d uint64)

Set PositiveBigIntegerField value

func (*PositiveBigIntegerField) SetRaw

func (e *PositiveBigIntegerField) SetRaw(value interface{}) error

SetRaw convert interface uint64/string to Uint64

func (*PositiveBigIntegerField) String

func (e *PositiveBigIntegerField) String() string

String convert PositiveBigIntegerField to string

func (PositiveBigIntegerField) Value

func (e PositiveBigIntegerField) Value() uint64

Value return uint64

type PositiveIntegerField

type PositiveIntegerField uint32

PositiveIntegerField 0 to 4294967295

func (*PositiveIntegerField) FieldType

func (e *PositiveIntegerField) FieldType() int

FieldType return enum type

func (*PositiveIntegerField) RawValue

func (e *PositiveIntegerField) RawValue() interface{}

RawValue return the PositiveIntegerField Value

func (*PositiveIntegerField) Set

func (e *PositiveIntegerField) Set(d uint32)

Set the PositiveIntegerField value

func (*PositiveIntegerField) SetRaw

func (e *PositiveIntegerField) SetRaw(value interface{}) error

SetRaw convert interface uint32/string to Uint32

func (*PositiveIntegerField) String

func (e *PositiveIntegerField) String() string

String convert PositiveIntegerField to string

func (PositiveIntegerField) Value

func (e PositiveIntegerField) Value() uint32

Value return PositiveIntegerField value. Uint32

type PositiveSmallIntegerField

type PositiveSmallIntegerField uint16

PositiveSmallIntegerField 0 to 65535

func (*PositiveSmallIntegerField) FieldType

func (e *PositiveSmallIntegerField) FieldType() int

FieldType return enum type

func (*PositiveSmallIntegerField) RawValue

func (e *PositiveSmallIntegerField) RawValue() interface{}

RawValue returns PositiveSmallIntegerField value

func (*PositiveSmallIntegerField) Set

Set PositiveSmallIntegerField value

func (*PositiveSmallIntegerField) SetRaw

func (e *PositiveSmallIntegerField) SetRaw(value interface{}) error

SetRaw convert Interface uint16/string to uint16

func (*PositiveSmallIntegerField) String

func (e *PositiveSmallIntegerField) String() string

String convert uint16 to string

func (PositiveSmallIntegerField) Value

Value return uint16

type SmallIntegerField

type SmallIntegerField int16

SmallIntegerField -32768 to 32767

func (*SmallIntegerField) FieldType

func (e *SmallIntegerField) FieldType() int

FieldType return enum type SmallIntegerField

func (*SmallIntegerField) RawValue

func (e *SmallIntegerField) RawValue() interface{}

RawValue return smallint value

func (*SmallIntegerField) Set

func (e *SmallIntegerField) Set(d int16)

Set the SmallIntegerField value

func (*SmallIntegerField) SetRaw

func (e *SmallIntegerField) SetRaw(value interface{}) error

SetRaw convert interface int16/string to int16

func (*SmallIntegerField) String

func (e *SmallIntegerField) String() string

String convert smallint to string

func (SmallIntegerField) Value

func (e SmallIntegerField) Value() int16

Value return int16 value

type TextField

type TextField string

TextField A large text field.

func (*TextField) FieldType

func (e *TextField) FieldType() int

FieldType return enum type

func (*TextField) RawValue

func (e *TextField) RawValue() interface{}

RawValue return TextField value

func (*TextField) Set

func (e *TextField) Set(d string)

Set the TextField value

func (*TextField) SetRaw

func (e *TextField) SetRaw(value interface{}) error

SetRaw convert interface string to string

func (*TextField) String

func (e *TextField) String() string

String convert TextField to string

func (TextField) Value

func (e TextField) Value() string

Value return TextField value

type TimeField

type TimeField time.Time

TimeField A time, represented in go by a time.Time instance. only time values like 10:00:00 Has a few extra, optional attr tag:

auto_now: Automatically set the field to now every time the object is saved. Useful for “last-modified” timestamps. Note that the current date is always used; it’s not just a default value that you can override.

auto_now_add: Automatically set the field to now when the object is first created. Useful for creation of timestamps. Note that the current date is always used; it’s not just a default value that you can override.

eg: `orm:"auto_now"` or `orm:"auto_now_add"`

func (*TimeField) FieldType

func (e *TimeField) FieldType() int

FieldType return enum type Date

func (*TimeField) RawValue

func (e *TimeField) RawValue() interface{}

RawValue return time value

func (*TimeField) Set

func (e *TimeField) Set(d time.Time)

Set set the TimeField's value

func (*TimeField) SetRaw

func (e *TimeField) SetRaw(value interface{}) error

SetRaw convert the interface to time.Time. Allow string and time.Time

func (*TimeField) String

func (e *TimeField) String() string

String convert time to string

func (TimeField) Value

func (e TimeField) Value() time.Time

Value return the time.Time

Jump to

Keyboard shortcuts

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