2014-04-12 05:18:18 +00:00
|
|
|
// Beego (http://beego.me/)
|
2014-06-25 02:39:37 +00:00
|
|
|
|
2014-04-12 05:18:18 +00:00
|
|
|
// @description beego is an open-source, high-performance web framework for the Go programming language.
|
2014-06-25 02:39:37 +00:00
|
|
|
|
2014-04-12 05:18:18 +00:00
|
|
|
// @link http://github.com/astaxie/beego for the canonical source repository
|
2014-06-25 02:39:37 +00:00
|
|
|
|
2014-04-12 05:18:18 +00:00
|
|
|
// @license http://github.com/astaxie/beego/blob/master/LICENSE
|
2014-06-25 02:39:37 +00:00
|
|
|
|
|
|
|
// @authors astaxie, slene
|
2014-04-12 05:18:18 +00:00
|
|
|
|
2013-07-30 12:32:38 +00:00
|
|
|
package orm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"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
|
2013-08-09 12:14:18 +00:00
|
|
|
model interface{}
|
2013-07-30 12:32:38 +00:00
|
|
|
fields *fields
|
|
|
|
manual bool
|
|
|
|
addrField reflect.Value
|
2013-10-14 14:31:35 +00:00
|
|
|
uniques []string
|
|
|
|
isThrough bool
|
2013-07-30 12:32:38 +00:00
|
|
|
}
|
|
|
|
|
2014-01-17 15:28:54 +00:00
|
|
|
// new model info
|
2013-08-09 12:14:18 +00:00
|
|
|
func newModelInfo(val reflect.Value) (info *modelInfo) {
|
2013-07-30 12:32:38 +00:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
fi *fieldInfo
|
|
|
|
sf reflect.StructField
|
|
|
|
)
|
|
|
|
|
|
|
|
info = &modelInfo{}
|
|
|
|
info.fields = newFields()
|
|
|
|
|
|
|
|
ind := reflect.Indirect(val)
|
|
|
|
typ := ind.Type()
|
|
|
|
|
2013-08-22 13:19:58 +00:00
|
|
|
info.addrField = val
|
2013-07-30 12:32:38 +00:00
|
|
|
|
|
|
|
info.name = typ.Name()
|
2013-08-09 12:14:18 +00:00
|
|
|
info.fullName = getFullName(typ)
|
2013-07-30 12:32:38 +00:00
|
|
|
|
|
|
|
for i := 0; i < ind.NumField(); i++ {
|
|
|
|
field := ind.Field(i)
|
|
|
|
sf = ind.Type().Field(i)
|
2014-03-18 23:00:07 +00:00
|
|
|
if sf.PkgPath != "" {
|
|
|
|
continue
|
|
|
|
}
|
2013-07-30 12:32:38 +00:00
|
|
|
fi, err = newFieldInfo(info, field, sf)
|
2013-08-09 12:14:18 +00:00
|
|
|
|
2013-07-30 12:32:38 +00:00
|
|
|
if err != nil {
|
2013-08-09 12:14:18 +00:00
|
|
|
if err == errSkipField {
|
|
|
|
err = nil
|
|
|
|
continue
|
|
|
|
}
|
2013-07-30 12:32:38 +00:00
|
|
|
break
|
|
|
|
}
|
2013-08-07 11:11:44 +00:00
|
|
|
|
2013-07-30 12:32:38 +00:00
|
|
|
added := info.fields.Add(fi)
|
|
|
|
if added == false {
|
|
|
|
err = errors.New(fmt.Sprintf("duplicate column name: %s", fi.column))
|
|
|
|
break
|
|
|
|
}
|
2013-08-07 11:11:44 +00:00
|
|
|
|
2013-07-30 12:32:38 +00:00
|
|
|
if fi.pk {
|
|
|
|
if info.fields.pk != nil {
|
|
|
|
err = errors.New(fmt.Sprintf("one model must have one pk field only"))
|
|
|
|
break
|
|
|
|
} else {
|
2013-08-07 11:11:44 +00:00
|
|
|
info.fields.pk = fi
|
2013-07-30 12:32:38 +00:00
|
|
|
}
|
|
|
|
}
|
2013-08-07 11:11:44 +00:00
|
|
|
|
2013-07-30 12:32:38 +00:00
|
|
|
fi.fieldIndex = i
|
|
|
|
fi.mi = info
|
2013-09-22 11:20:40 +00:00
|
|
|
fi.inModel = true
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-01-17 15:28:54 +00:00
|
|
|
// combine related model info to new model info.
|
|
|
|
// prepare for relation models query.
|
2013-07-30 12:32:38 +00:00
|
|
|
func newM2MModelInfo(m1, m2 *modelInfo) (info *modelInfo) {
|
|
|
|
info = new(modelInfo)
|
|
|
|
info.fields = newFields()
|
2013-08-10 16:15:26 +00:00
|
|
|
info.table = m1.table + "_" + m2.table + "s"
|
2013-07-30 12:32:38 +00:00
|
|
|
info.name = camelString(info.table)
|
|
|
|
info.fullName = m1.pkg + "." + info.name
|
|
|
|
|
|
|
|
fa := new(fieldInfo)
|
|
|
|
f1 := new(fieldInfo)
|
|
|
|
f2 := new(fieldInfo)
|
|
|
|
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"
|
|
|
|
fa.fullName = info.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)
|
|
|
|
f1.fullName = info.fullName + "." + f1.name
|
|
|
|
f2.fullName = info.fullName + "." + f2.name
|
|
|
|
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
|
|
|
|
f1.mi = info
|
|
|
|
f2.mi = info
|
|
|
|
|
|
|
|
info.fields.Add(fa)
|
|
|
|
info.fields.Add(f1)
|
|
|
|
info.fields.Add(f2)
|
2013-08-07 11:11:44 +00:00
|
|
|
info.fields.pk = fa
|
2013-10-14 14:31:35 +00:00
|
|
|
|
|
|
|
info.uniques = []string{f1.column, f2.column}
|
2013-07-30 12:32:38 +00:00
|
|
|
return
|
|
|
|
}
|