1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-24 03:00:54 +00:00
Beego/orm/models.go

100 lines
2.5 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 (
"sync"
)
const (
2015-09-12 13:46:43 +00:00
odCascade = "cascade"
odSetNULL = "set_null"
odSetDefault = "set_default"
odDoNothing = "do_nothing"
defaultStructTagName = "orm"
defaultStructTagDelim = ";"
2013-07-30 12:32:38 +00:00
)
var (
modelCache = &_modelCache{
2016-08-30 16:07:19 +00:00
cache: make(map[string]*modelInfo),
cacheByFullName: make(map[string]*modelInfo),
2013-07-30 12:32:38 +00:00
}
)
2014-01-17 15:28:54 +00:00
// model info collection
2013-07-30 12:32:38 +00:00
type _modelCache struct {
2016-08-30 16:07:19 +00:00
sync.RWMutex // only used outsite for bootStrap
orders []string
cache map[string]*modelInfo
cacheByFullName map[string]*modelInfo
done bool
2013-07-30 12:32:38 +00:00
}
2014-01-17 15:28:54 +00:00
// get all model info
2013-07-30 12:32:38 +00:00
func (mc *_modelCache) all() map[string]*modelInfo {
m := make(map[string]*modelInfo, len(mc.cache))
for k, v := range mc.cache {
m[k] = v
}
return m
}
2017-10-17 09:27:03 +00:00
// get ordered model info
2013-07-30 12:32:38 +00:00
func (mc *_modelCache) allOrdered() []*modelInfo {
m := make([]*modelInfo, 0, len(mc.orders))
2013-08-25 03:31:07 +00:00
for _, table := range mc.orders {
m = append(m, mc.cache[table])
2013-07-30 12:32:38 +00:00
}
return m
}
2014-01-17 15:28:54 +00:00
// get model info by table name
2013-07-30 12:32:38 +00:00
func (mc *_modelCache) get(table string) (mi *modelInfo, ok bool) {
mi, ok = mc.cache[table]
return
}
2016-08-30 16:07:19 +00:00
// get model info by full name
func (mc *_modelCache) getByFullName(name string) (mi *modelInfo, ok bool) {
mi, ok = mc.cacheByFullName[name]
2013-07-30 12:32:38 +00:00
return
}
2014-01-17 15:28:54 +00:00
// set model info to collection
2013-07-30 12:32:38 +00:00
func (mc *_modelCache) set(table string, mi *modelInfo) *modelInfo {
mii := mc.cache[table]
mc.cache[table] = mi
2016-08-30 16:07:19 +00:00
mc.cacheByFullName[mi.fullName] = mi
2013-07-30 12:32:38 +00:00
if mii == nil {
mc.orders = append(mc.orders, table)
}
return mii
}
2013-08-19 14:37:39 +00:00
2014-01-17 15:28:54 +00:00
// clean all model info.
2013-08-19 14:37:39 +00:00
func (mc *_modelCache) clean() {
mc.orders = make([]string, 0)
mc.cache = make(map[string]*modelInfo)
2016-08-30 16:07:19 +00:00
mc.cacheByFullName = make(map[string]*modelInfo)
2013-08-19 14:37:39 +00:00
mc.done = false
}
2015-09-12 13:46:43 +00:00
// ResetModelCache Clean model cache. Then you can re-RegisterModel.
// Common use this api for test case.
func ResetModelCache() {
modelCache.clean()
}