1
0
mirror of https://github.com/astaxie/beego.git synced 2024-12-23 18:00:50 +00:00
Beego/orm/models_info_m.go

148 lines
3.4 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 (
"fmt"
"os"
"reflect"
)
2014-01-17 15:28:54 +00:00
// single model info
2013-07-30 12:32:38 +00:00
type modelInfo struct {
pkg string
name string
fullName string
table string
model interface{}
2013-07-30 12:32:38 +00:00
fields *fields
manual bool
2016-08-30 16:07:19 +00:00
addrField reflect.Value //store the original struct value
uniques []string
isThrough bool
2013-07-30 12:32:38 +00:00
}
2014-01-17 15:28:54 +00:00
// new model info
2016-08-30 16:07:19 +00:00
func newModelInfo(val reflect.Value) (mi *modelInfo) {
mi = &modelInfo{}
mi.fields = newFields()
2013-07-30 12:32:38 +00:00
ind := reflect.Indirect(val)
2016-08-30 16:07:19 +00:00
mi.addrField = val
mi.name = ind.Type().Name()
mi.fullName = getFullName(ind.Type())
addModelFields(mi, ind, "", []int{})
2016-02-24 10:46:14 +00:00
return
}
2016-08-30 16:07:19 +00:00
// index: FieldByIndex returns the nested field corresponding to index
func addModelFields(mi *modelInfo, ind reflect.Value, mName string, index []int) {
2016-02-24 10:46:14 +00:00
var (
err error
fi *fieldInfo
sf reflect.StructField
)
2013-07-30 12:32:38 +00:00
for i := 0; i < ind.NumField(); i++ {
field := ind.Field(i)
sf = ind.Type().Field(i)
2016-08-30 16:07:19 +00:00
// if the field is unexported skip
if sf.PkgPath != "" {
continue
}
2016-02-24 10:46:14 +00:00
// add anonymous struct fields
if sf.Anonymous {
2016-08-30 16:07:19 +00:00
addModelFields(mi, field, mName+"."+sf.Name, append(index, i))
2016-02-24 10:46:14 +00:00
continue
}
2016-08-30 16:07:19 +00:00
fi, err = newFieldInfo(mi, field, sf, mName)
if err == errSkipField {
err = nil
continue
} else if err != nil {
2013-07-30 12:32:38 +00:00
break
}
2016-08-30 16:07:19 +00:00
//record current field index
fi.fieldIndex = append(index, i)
fi.mi = mi
fi.inModel = true
2017-03-17 17:24:45 +00:00
if !mi.fields.Add(fi) {
2015-09-12 13:46:43 +00:00
err = fmt.Errorf("duplicate column name: %s", fi.column)
2013-07-30 12:32:38 +00:00
break
}
if fi.pk {
2016-08-30 16:07:19 +00:00
if mi.fields.pk != nil {
2015-09-12 13:46:43 +00:00
err = fmt.Errorf("one model must have one pk field only")
2013-07-30 12:32:38 +00:00
break
} else {
2016-08-30 16:07:19 +00:00
mi.fields.pk = fi
2013-07-30 12:32:38 +00:00
}
}
}
if err != nil {
fmt.Println(fmt.Errorf("field: %s.%s, %s", ind.Type(), sf.Name, err))
os.Exit(2)
}
}
2014-01-17 15:28:54 +00:00
// combine related model info to new model info.
// prepare for relation models query.
2016-08-30 16:07:19 +00:00
func newM2MModelInfo(m1, m2 *modelInfo) (mi *modelInfo) {
mi = new(modelInfo)
mi.fields = newFields()
mi.table = m1.table + "_" + m2.table + "s"
mi.name = camelString(mi.table)
mi.fullName = m1.pkg + "." + mi.name
2013-07-30 12:32:38 +00:00
2016-09-01 15:28:34 +00:00
fa := new(fieldInfo) // pk
f1 := new(fieldInfo) // m1 table RelForeignKey
f2 := new(fieldInfo) // m2 table RelForeignKey
2013-07-30 12:32:38 +00:00
fa.fieldType = TypeBigIntegerField
fa.auto = true
fa.pk = true
fa.dbcol = true
2013-08-19 14:37:39 +00:00
fa.name = "Id"
fa.column = "id"
2016-08-30 16:07:19 +00:00
fa.fullName = mi.fullName + "." + fa.name
2013-07-30 12:32:38 +00:00
f1.dbcol = true
f2.dbcol = true
f1.fieldType = RelForeignKey
f2.fieldType = RelForeignKey
f1.name = camelString(m1.table)
f2.name = camelString(m2.table)
2016-08-30 16:07:19 +00:00
f1.fullName = mi.fullName + "." + f1.name
f2.fullName = mi.fullName + "." + f2.name
2013-07-30 12:32:38 +00:00
f1.column = m1.table + "_id"
f2.column = m2.table + "_id"
f1.rel = true
f2.rel = true
f1.relTable = m1.table
f2.relTable = m2.table
f1.relModelInfo = m1
f2.relModelInfo = m2
2016-08-30 16:07:19 +00:00
f1.mi = mi
f2.mi = mi
2013-07-30 12:32:38 +00:00
2016-08-30 16:07:19 +00:00
mi.fields.Add(fa)
mi.fields.Add(f1)
mi.fields.Add(f2)
mi.fields.pk = fa
2016-08-30 16:07:19 +00:00
mi.uniques = []string{f1.column, f2.column}
2013-07-30 12:32:38 +00:00
return
}