1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-10 21:40:54 +00:00
Beego/orm/models_info_f.go

470 lines
11 KiB
Go
Raw Normal View History

2014-08-18 08:41:43 +00:00
// Copyright 2014 beego Author. All Rights Reserved.
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// http://www.apache.org/licenses/LICENSE-2.0
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2013-07-30 12:32:38 +00:00
package orm
import (
"errors"
"fmt"
"reflect"
"strings"
)
var errSkipField = errors.New("skip field")
2013-07-30 12:32:38 +00:00
2014-01-17 15:28:54 +00:00
// field info collection
2013-07-30 12:32:38 +00:00
type fields struct {
2013-08-07 11:11:44 +00:00
pk *fieldInfo
2013-07-30 12:32:38 +00:00
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
}
2014-01-17 15:28:54 +00:00
// add field info
2013-07-30 12:32:38 +00:00
func (f *fields) Add(fi *fieldInfo) (added bool) {
if f.fields[fi.name] == nil && f.columns[fi.column] == nil {
f.columns[fi.column] = fi
f.fields[fi.name] = fi
f.fieldsLow[strings.ToLower(fi.name)] = fi
} else {
return
}
2017-03-17 17:24:45 +00:00
if _, ok := f.fieldsByType[fi.fieldType]; !ok {
2013-07-30 12:32:38 +00:00
f.fieldsByType[fi.fieldType] = make([]*fieldInfo, 0)
}
f.fieldsByType[fi.fieldType] = append(f.fieldsByType[fi.fieldType], fi)
f.orders = append(f.orders, fi.column)
if fi.dbcol {
f.dbcols = append(f.dbcols, fi.column)
f.fieldsDB = append(f.fieldsDB, fi)
}
if fi.rel {
f.fieldsRel = append(f.fieldsRel, fi)
}
if fi.reverse {
f.fieldsReverse = append(f.fieldsReverse, fi)
}
return true
}
2014-01-17 15:28:54 +00:00
// get field info by name
2013-07-30 12:32:38 +00:00
func (f *fields) GetByName(name string) *fieldInfo {
return f.fields[name]
}
2014-01-17 15:28:54 +00:00
// get field info by column name
2013-07-30 12:32:38 +00:00
func (f *fields) GetByColumn(column string) *fieldInfo {
return f.columns[column]
}
2014-01-17 15:28:54 +00:00
// get field info by string, name is prior
2013-07-30 12:32:38 +00:00
func (f *fields) GetByAny(name string) (*fieldInfo, bool) {
if fi, ok := f.fields[name]; ok {
return fi, ok
}
if fi, ok := f.fieldsLow[strings.ToLower(name)]; ok {
return fi, ok
}
if fi, ok := f.columns[name]; ok {
return fi, ok
}
return nil, false
}
2014-01-17 15:28:54 +00:00
// create new field info collection
2013-07-30 12:32:38 +00:00
func newFields() *fields {
f := new(fields)
f.fields = make(map[string]*fieldInfo)
f.fieldsLow = make(map[string]*fieldInfo)
f.columns = make(map[string]*fieldInfo)
f.fieldsByType = make(map[int][]*fieldInfo)
return f
}
2014-01-17 15:28:54 +00:00
// single field info
2013-07-30 12:32:38 +00:00
type fieldInfo struct {
mi *modelInfo
2016-02-24 10:46:14 +00:00
fieldIndex []int
2013-07-30 12:32:38 +00:00
fieldType int
2016-09-01 15:28:34 +00:00
dbcol bool // table column fk and onetoone
2013-07-30 12:32:38 +00:00
inModel bool
name string
fullName string
column string
addrValue reflect.Value
sf reflect.StructField
2013-07-30 12:32:38 +00:00
auto bool
pk bool
null bool
index bool
unique bool
2016-09-01 15:28:34 +00:00
colDefault bool // whether has default tag
initial StrTo // store the default value
2013-07-31 14:11:22 +00:00
size int
toText bool
2015-09-12 13:46:43 +00:00
autoNow bool
autoNowAdd bool
2016-09-01 15:28:34 +00:00
rel bool // if type equal to RelForeignKey, RelOneToOne, RelManyToMany then true
2013-07-30 12:32:38 +00:00
reverse bool
reverseField string
reverseFieldInfo *fieldInfo
reverseFieldInfoTwo *fieldInfo
reverseFieldInfoM2M *fieldInfo
2013-07-30 12:32:38 +00:00
relTable string
relThrough string
relThroughModelInfo *modelInfo
relModelInfo *modelInfo
digits int
decimals int
2016-09-01 15:28:34 +00:00
isFielder bool // implement Fielder interface
2013-07-30 12:32:38 +00:00
onDelete string
}
2014-01-17 15:28:54 +00:00
// new field info
2016-02-24 10:46:14 +00:00
func newFieldInfo(mi *modelInfo, field reflect.Value, sf reflect.StructField, mName string) (fi *fieldInfo, err error) {
2013-07-30 12:32:38 +00:00
var (
tag string
tagValue string
2016-08-31 14:47:31 +00:00
initial StrTo // store the default value
2013-07-30 12:32:38 +00:00
fieldType int
attrs map[string]bool
tags map[string]string
addrField reflect.Value
)
fi = new(fieldInfo)
2016-08-30 16:07:19 +00:00
// if field which CanAddr is the follow type
// A value is addressable if it is an element of a slice,
// an element of an addressable array, a field of an
// addressable struct, or the result of dereferencing a pointer.
2013-11-01 15:54:28 +00:00
addrField = field
if field.CanAddr() && field.Kind() != reflect.Ptr {
2013-07-30 12:32:38 +00:00
addrField = field.Addr()
2013-11-01 15:54:28 +00:00
if _, ok := addrField.Interface().(Fielder); !ok {
if field.Kind() == reflect.Slice {
addrField = field
}
}
2013-07-30 12:32:38 +00:00
}
2016-08-30 16:07:19 +00:00
attrs, tags = parseStructTag(sf.Tag.Get(defaultStructTagName))
2013-07-30 12:32:38 +00:00
if _, ok := attrs["-"]; ok {
return nil, errSkipField
}
2013-07-30 12:32:38 +00:00
digits := tags["digits"]
decimals := tags["decimals"]
2013-07-31 14:11:22 +00:00
size := tags["size"]
2013-07-30 12:32:38 +00:00
onDelete := tags["on_delete"]
initial.Clear()
if v, ok := tags["default"]; ok {
initial.Set(v)
}
2013-07-30 12:32:38 +00:00
checkType:
switch f := addrField.Interface().(type) {
case Fielder:
fi.isFielder = true
if field.Kind() == reflect.Ptr {
err = fmt.Errorf("the model Fielder can not be use ptr")
goto end
}
fieldType = f.FieldType()
if fieldType&IsRelField > 0 {
2016-08-30 16:07:19 +00:00
err = fmt.Errorf("unsupport type custom field, please refer to https://github.com/astaxie/beego/blob/master/orm/models_fields.go#L24-L42")
2013-07-30 12:32:38 +00:00
goto end
}
default:
tag = "rel"
tagValue = tags[tag]
if tagValue != "" {
switch tagValue {
case "fk":
fieldType = RelForeignKey
break checkType
case "one":
fieldType = RelOneToOne
break checkType
case "m2m":
fieldType = RelManyToMany
if tv := tags["rel_table"]; tv != "" {
fi.relTable = tv
} else if tv := tags["rel_through"]; tv != "" {
fi.relThrough = tv
}
break checkType
default:
2016-08-31 14:47:31 +00:00
err = fmt.Errorf("rel only allow these value: fk, one, m2m")
2013-07-30 12:32:38 +00:00
goto wrongTag
}
}
tag = "reverse"
tagValue = tags[tag]
if tagValue != "" {
switch tagValue {
case "one":
fieldType = RelReverseOne
break checkType
case "many":
fieldType = RelReverseMany
2015-07-08 14:42:00 +00:00
if tv := tags["rel_table"]; tv != "" {
fi.relTable = tv
} else if tv := tags["rel_through"]; tv != "" {
fi.relThrough = tv
}
2013-07-30 12:32:38 +00:00
break checkType
default:
2016-08-31 14:47:31 +00:00
err = fmt.Errorf("reverse only allow these value: one, many")
2013-07-30 12:32:38 +00:00
goto wrongTag
}
}
fieldType, err = getFieldType(addrField)
if err != nil {
goto end
}
2016-04-08 13:53:27 +00:00
if fieldType == TypeCharField {
switch tags["type"] {
case "text":
fieldType = TypeTextField
case "json":
2016-04-09 02:22:40 +00:00
fieldType = TypeJSONField
2016-04-08 13:53:27 +00:00
case "jsonb":
fieldType = TypeJsonbField
}
2013-07-30 12:32:38 +00:00
}
if fieldType == TypeFloatField && (digits != "" || decimals != "") {
fieldType = TypeDecimalField
}
2013-07-31 14:11:22 +00:00
if fieldType == TypeDateTimeField && tags["type"] == "date" {
2013-07-30 12:32:38 +00:00
fieldType = TypeDateField
}
if fieldType == TypeTimeField && tags["type"] == "time" {
fieldType = TypeTimeField
}
2013-07-30 12:32:38 +00:00
}
2016-09-01 15:28:34 +00:00
// check the rel and reverse type
// rel should Ptr
// reverse should slice []*struct
2013-07-30 12:32:38 +00:00
switch fieldType {
case RelForeignKey, RelOneToOne, RelReverseOne:
if field.Kind() != reflect.Ptr {
err = fmt.Errorf("rel/reverse:one field must be *%s", field.Type().Name())
goto end
}
case RelManyToMany, RelReverseMany:
if field.Kind() != reflect.Slice {
err = fmt.Errorf("rel/reverse:many field must be slice")
goto end
} else {
if field.Type().Elem().Kind() != reflect.Ptr {
err = fmt.Errorf("rel/reverse:many slice must be []*%s", field.Type().Elem().Name())
goto end
}
}
}
if fieldType&IsFieldType == 0 {
err = fmt.Errorf("wrong field type")
goto end
}
fi.fieldType = fieldType
fi.name = sf.Name
fi.column = getColumnName(fieldType, addrField, sf, tags["column"])
fi.addrValue = addrField
fi.sf = sf
2016-02-24 10:46:14 +00:00
fi.fullName = mi.fullName + mName + "." + sf.Name
2013-07-30 12:32:38 +00:00
fi.null = attrs["null"]
fi.index = attrs["index"]
fi.auto = attrs["auto"]
fi.pk = attrs["pk"]
fi.unique = attrs["unique"]
// Mark object property if there is attribute "default" in the orm configuration
if _, ok := tags["default"]; ok {
fi.colDefault = true
}
2013-07-30 12:32:38 +00:00
switch fieldType {
case RelManyToMany, RelReverseMany, RelReverseOne:
fi.null = false
fi.index = false
fi.auto = false
fi.pk = false
fi.unique = false
default:
fi.dbcol = true
}
switch fieldType {
case RelForeignKey, RelOneToOne, RelManyToMany:
fi.rel = true
if fieldType == RelOneToOne {
fi.unique = true
}
case RelReverseMany, RelReverseOne:
fi.reverse = true
}
if fi.rel && fi.dbcol {
switch onDelete {
2015-09-12 13:46:43 +00:00
case odCascade, odDoNothing:
case odSetDefault:
2017-03-17 17:24:45 +00:00
if !initial.Exist() {
2013-07-30 12:32:38 +00:00
err = errors.New("on_delete: set_default need set field a default value")
goto end
}
2015-09-12 13:46:43 +00:00
case odSetNULL:
2017-03-17 17:24:45 +00:00
if !fi.null {
2013-07-30 12:32:38 +00:00
err = errors.New("on_delete: set_null need set field null")
goto end
}
default:
if onDelete == "" {
2015-09-12 13:46:43 +00:00
onDelete = odCascade
2013-07-30 12:32:38 +00:00
} else {
err = fmt.Errorf("on_delete value expected choice in `cascade,set_null,set_default,do_nothing`, unknown `%s`", onDelete)
goto end
}
}
fi.onDelete = onDelete
}
switch fieldType {
case TypeBooleanField:
2016-04-09 02:22:40 +00:00
case TypeCharField, TypeJSONField, TypeJsonbField:
2013-07-31 14:11:22 +00:00
if size != "" {
v, e := StrTo(size).Int32()
2013-07-30 12:32:38 +00:00
if e != nil {
2013-07-31 14:11:22 +00:00
err = fmt.Errorf("wrong size value `%s`", size)
2013-07-30 12:32:38 +00:00
} else {
2013-07-31 14:11:22 +00:00
fi.size = int(v)
2013-07-30 12:32:38 +00:00
}
} else {
fi.size = 255
fi.toText = true
2013-07-30 12:32:38 +00:00
}
case TypeTextField:
fi.index = false
fi.unique = false
case TypeTimeField, TypeDateField, TypeDateTimeField:
2013-07-30 12:32:38 +00:00
if attrs["auto_now"] {
2015-09-12 13:46:43 +00:00
fi.autoNow = true
2013-07-30 12:32:38 +00:00
} else if attrs["auto_now_add"] {
2015-09-12 13:46:43 +00:00
fi.autoNowAdd = true
2013-07-30 12:32:38 +00:00
}
case TypeFloatField:
case TypeDecimalField:
d1 := digits
d2 := decimals
v1, er1 := StrTo(d1).Int8()
v2, er2 := StrTo(d2).Int8()
2013-07-30 12:32:38 +00:00
if er1 != nil || er2 != nil {
err = fmt.Errorf("wrong digits/decimals value %s/%s", d2, d1)
goto end
}
fi.digits = int(v1)
fi.decimals = int(v2)
default:
switch {
case fieldType&IsIntegerField > 0:
case fieldType&IsRelField > 0:
}
}
if fieldType&IsIntegerField == 0 {
if fi.auto {
err = fmt.Errorf("non-integer type cannot set auto")
goto end
}
}
if fi.auto || fi.pk {
if fi.auto {
switch addrField.Elem().Kind() {
case reflect.Int, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint32, reflect.Uint64:
default:
err = fmt.Errorf("auto primary key only support int, int32, int64, uint, uint32, uint64 but found `%s`", addrField.Elem().Kind())
goto end
}
2013-07-30 12:32:38 +00:00
fi.pk = true
}
fi.null = false
fi.index = false
fi.unique = false
}
if fi.unique {
fi.index = false
}
2016-09-01 15:28:34 +00:00
// can not set default for these type
if fi.auto || fi.pk || fi.unique || fieldType == TypeTimeField || fieldType == TypeDateField || fieldType == TypeDateTimeField {
2013-07-30 12:32:38 +00:00
initial.Clear()
}
if initial.Exist() {
v := initial
2013-07-30 12:32:38 +00:00
switch fieldType {
case TypeBooleanField:
_, err = v.Bool()
case TypeFloatField, TypeDecimalField:
_, err = v.Float64()
case TypeBitField:
_, err = v.Int8()
2013-07-30 12:32:38 +00:00
case TypeSmallIntegerField:
_, err = v.Int16()
case TypeIntegerField:
_, err = v.Int32()
case TypeBigIntegerField:
_, err = v.Int64()
2013-08-19 14:37:39 +00:00
case TypePositiveBitField:
_, err = v.Uint8()
2013-07-30 12:32:38 +00:00
case TypePositiveSmallIntegerField:
_, err = v.Uint16()
case TypePositiveIntegerField:
_, err = v.Uint32()
case TypePositiveBigIntegerField:
_, err = v.Uint64()
}
if err != nil {
tag, tagValue = "default", tags["default"]
2013-07-30 12:32:38 +00:00
goto wrongTag
}
}
fi.initial = initial
end:
if err != nil {
return nil, err
}
return
wrongTag:
return nil, fmt.Errorf("wrong tag format: `%s:\"%s\"`, %s", tag, tagValue, err)
}