diff --git a/orm/db_alias.go b/orm/db_alias.go index b6c833a7..c95d49c9 100644 --- a/orm/db_alias.go +++ b/orm/db_alias.go @@ -80,7 +80,7 @@ type _dbCache struct { func (ac *_dbCache) add(name string, al *alias) (added bool) { ac.mux.Lock() defer ac.mux.Unlock() - if _, ok := ac.cache[name]; ok == false { + if _, ok := ac.cache[name]; !ok { ac.cache[name] = al added = true } diff --git a/orm/models_boot.go b/orm/models_boot.go index 7d9746d0..d48d2677 100644 --- a/orm/models_boot.go +++ b/orm/models_boot.go @@ -15,7 +15,6 @@ package orm import ( - "errors" "fmt" "os" "reflect" @@ -55,34 +54,34 @@ func registerModel(prefix string, model interface{}) { os.Exit(2) } - info := newModelInfo(val) - if info.fields.pk == nil { + mi := newModelInfo(val) + if mi.fields.pk == nil { outFor: - for _, fi := range info.fields.fieldsDB { + for _, fi := range mi.fields.fieldsDB { if strings.ToLower(fi.name) == "id" { switch fi.addrValue.Elem().Kind() { case reflect.Int, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint32, reflect.Uint64: fi.auto = true fi.pk = true - info.fields.pk = fi + mi.fields.pk = fi break outFor } } } - if info.fields.pk == nil { + if mi.fields.pk == nil { fmt.Printf(" `%s` need a primary key field, default use 'id' if not set\n", name) os.Exit(2) } } - info.table = table - info.pkg = typ.PkgPath() - info.model = model - info.manual = true + mi.table = table + mi.pkg = typ.PkgPath() + mi.model = model + mi.manual = true - modelCache.set(table, info) + modelCache.set(table, mi) } // boostrap models @@ -90,12 +89,10 @@ func bootStrap() { if modelCache.done { return } - var ( err error models map[string]*modelInfo ) - if dataBaseCache.getDefault() == nil { err = fmt.Errorf("must have one register DataBase alias named `default`") goto end @@ -106,14 +103,13 @@ func bootStrap() { for _, fi := range mi.fields.columns { if fi.rel || fi.reverse { elm := fi.addrValue.Type().Elem() - switch fi.fieldType { - case RelReverseMany, RelManyToMany: + if fi.fieldType == RelReverseMany || fi.fieldType == RelManyToMany { elm = elm.Elem() } - + // check the rel or reverse model already register name := getFullName(elm) mii, ok := modelCache.getByFullName(name) - if ok == false || mii.pkg != elm.PkgPath() { + if !ok || mii.pkg != elm.PkgPath() { err = fmt.Errorf("can not found rel in field `%s`, `%s` may be miss register", fi.fullName, elm.String()) goto end } @@ -122,20 +118,17 @@ func bootStrap() { switch fi.fieldType { case RelManyToMany: if fi.relThrough != "" { - msg := fmt.Sprintf("field `%s` wrong rel_through value `%s`", fi.fullName, fi.relThrough) if i := strings.LastIndex(fi.relThrough, "."); i != -1 && len(fi.relThrough) > (i+1) { pn := fi.relThrough[:i] rmi, ok := modelCache.getByFullName(fi.relThrough) if ok == false || pn != rmi.pkg { - err = errors.New(msg + " cannot find table") + err = fmt.Errorf("field `%s` wrong rel_through value `%s` cannot find table", fi.fullName, fi.relThrough) goto end } - fi.relThroughModelInfo = rmi fi.relTable = rmi.table - } else { - err = errors.New(msg) + err = fmt.Errorf("field `%s` wrong rel_through value `%s`", fi.fullName, fi.relThrough) goto end } } else { @@ -143,7 +136,6 @@ func bootStrap() { if fi.relTable != "" { i.table = fi.relTable } - if v := modelCache.set(i.table, i); v != nil { err = fmt.Errorf("the rel table name `%s` already registered, cannot be use, please change one", fi.relTable) goto end diff --git a/orm/models_info_f.go b/orm/models_info_f.go index ba25f744..4b3d3e27 100644 --- a/orm/models_info_f.go +++ b/orm/models_info_f.go @@ -104,7 +104,7 @@ type fieldInfo struct { mi *modelInfo fieldIndex []int fieldType int - dbcol bool + dbcol bool // table column fk and onetoone inModel bool name string fullName string @@ -116,13 +116,13 @@ type fieldInfo struct { null bool index bool unique bool - colDefault bool - initial StrTo + colDefault bool // whether has default tag + initial StrTo // store the default value size int toText bool autoNow bool autoNowAdd bool - rel bool + rel bool // if type equal to RelForeignKey, RelOneToOne, RelManyToMany then true reverse bool reverseField string reverseFieldInfo *fieldInfo @@ -134,7 +134,7 @@ type fieldInfo struct { relModelInfo *modelInfo digits int decimals int - isFielder bool + isFielder bool // implement Fielder interface onDelete string } @@ -265,6 +265,9 @@ checkType: } } + // check the rel and reverse type + // rel should Ptr + // reverse should slice []*struct switch fieldType { case RelForeignKey, RelOneToOne, RelReverseOne: if field.Kind() != reflect.Ptr { @@ -403,14 +406,12 @@ checkType: 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 } - fi.pk = true } fi.null = false @@ -422,8 +423,8 @@ checkType: fi.index = false } + // can not set default for these type if fi.auto || fi.pk || fi.unique || fieldType == TypeTimeField || fieldType == TypeDateField || fieldType == TypeDateTimeField { - // can not set default initial.Clear() } diff --git a/orm/models_info_m.go b/orm/models_info_m.go index 2e0905ec..d6ba1dca 100644 --- a/orm/models_info_m.go +++ b/orm/models_info_m.go @@ -107,9 +107,9 @@ func newM2MModelInfo(m1, m2 *modelInfo) (mi *modelInfo) { mi.name = camelString(mi.table) mi.fullName = m1.pkg + "." + mi.name - fa := new(fieldInfo) - f1 := new(fieldInfo) - f2 := new(fieldInfo) + fa := new(fieldInfo) // pk + f1 := new(fieldInfo) // m1 table RelForeignKey + f2 := new(fieldInfo) // m2 table RelForeignKey fa.fieldType = TypeBigIntegerField fa.auto = true fa.pk = true