github.com/astaxie/beego@v1.12.3/orm/models.go (about)

     1  // Copyright 2014 beego Author. All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package orm
    16  
    17  import (
    18  	"sync"
    19  )
    20  
    21  const (
    22  	odCascade             = "cascade"
    23  	odSetNULL             = "set_null"
    24  	odSetDefault          = "set_default"
    25  	odDoNothing           = "do_nothing"
    26  	defaultStructTagName  = "orm"
    27  	defaultStructTagDelim = ";"
    28  )
    29  
    30  var (
    31  	modelCache = &_modelCache{
    32  		cache:           make(map[string]*modelInfo),
    33  		cacheByFullName: make(map[string]*modelInfo),
    34  	}
    35  )
    36  
    37  // model info collection
    38  type _modelCache struct {
    39  	sync.RWMutex    // only used outsite for bootStrap
    40  	orders          []string
    41  	cache           map[string]*modelInfo
    42  	cacheByFullName map[string]*modelInfo
    43  	done            bool
    44  }
    45  
    46  // get all model info
    47  func (mc *_modelCache) all() map[string]*modelInfo {
    48  	m := make(map[string]*modelInfo, len(mc.cache))
    49  	for k, v := range mc.cache {
    50  		m[k] = v
    51  	}
    52  	return m
    53  }
    54  
    55  // get ordered model info
    56  func (mc *_modelCache) allOrdered() []*modelInfo {
    57  	m := make([]*modelInfo, 0, len(mc.orders))
    58  	for _, table := range mc.orders {
    59  		m = append(m, mc.cache[table])
    60  	}
    61  	return m
    62  }
    63  
    64  // get model info by table name
    65  func (mc *_modelCache) get(table string) (mi *modelInfo, ok bool) {
    66  	mi, ok = mc.cache[table]
    67  	return
    68  }
    69  
    70  // get model info by full name
    71  func (mc *_modelCache) getByFullName(name string) (mi *modelInfo, ok bool) {
    72  	mi, ok = mc.cacheByFullName[name]
    73  	return
    74  }
    75  
    76  // set model info to collection
    77  func (mc *_modelCache) set(table string, mi *modelInfo) *modelInfo {
    78  	mii := mc.cache[table]
    79  	mc.cache[table] = mi
    80  	mc.cacheByFullName[mi.fullName] = mi
    81  	if mii == nil {
    82  		mc.orders = append(mc.orders, table)
    83  	}
    84  	return mii
    85  }
    86  
    87  // clean all model info.
    88  func (mc *_modelCache) clean() {
    89  	mc.orders = make([]string, 0)
    90  	mc.cache = make(map[string]*modelInfo)
    91  	mc.cacheByFullName = make(map[string]*modelInfo)
    92  	mc.done = false
    93  }
    94  
    95  // ResetModelCache Clean model cache. Then you can re-RegisterModel.
    96  // Common use this api for test case.
    97  func ResetModelCache() {
    98  	modelCache.clean()
    99  }