models.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. package orm
  15. import (
  16. "sync"
  17. )
  18. const (
  19. odCascade = "cascade"
  20. odSetNULL = "set_null"
  21. odSetDefault = "set_default"
  22. odDoNothing = "do_nothing"
  23. defaultStructTagName = "orm"
  24. defaultStructTagDelim = ";"
  25. )
  26. var (
  27. modelCache = &_modelCache{
  28. cache: make(map[string]*modelInfo),
  29. cacheByFullName: make(map[string]*modelInfo),
  30. }
  31. )
  32. // model info collection
  33. type _modelCache struct {
  34. sync.RWMutex // only used outsite for bootStrap
  35. orders []string
  36. cache map[string]*modelInfo
  37. cacheByFullName map[string]*modelInfo
  38. done bool
  39. }
  40. // get all model info
  41. func (mc *_modelCache) all() map[string]*modelInfo {
  42. m := make(map[string]*modelInfo, len(mc.cache))
  43. for k, v := range mc.cache {
  44. m[k] = v
  45. }
  46. return m
  47. }
  48. // get orderd model info
  49. func (mc *_modelCache) allOrdered() []*modelInfo {
  50. m := make([]*modelInfo, 0, len(mc.orders))
  51. for _, table := range mc.orders {
  52. m = append(m, mc.cache[table])
  53. }
  54. return m
  55. }
  56. // get model info by table name
  57. func (mc *_modelCache) get(table string) (mi *modelInfo, ok bool) {
  58. mi, ok = mc.cache[table]
  59. return
  60. }
  61. // get model info by full name
  62. func (mc *_modelCache) getByFullName(name string) (mi *modelInfo, ok bool) {
  63. mi, ok = mc.cacheByFullName[name]
  64. return
  65. }
  66. // set model info to collection
  67. func (mc *_modelCache) set(table string, mi *modelInfo) *modelInfo {
  68. mii := mc.cache[table]
  69. mc.cache[table] = mi
  70. mc.cacheByFullName[mi.fullName] = mi
  71. if mii == nil {
  72. mc.orders = append(mc.orders, table)
  73. }
  74. return mii
  75. }
  76. // clean all model info.
  77. func (mc *_modelCache) clean() {
  78. mc.orders = make([]string, 0)
  79. mc.cache = make(map[string]*modelInfo)
  80. mc.cacheByFullName = make(map[string]*modelInfo)
  81. mc.done = false
  82. }
  83. // ResetModelCache Clean model cache. Then you can re-RegisterModel.
  84. // Common use this api for test case.
  85. func ResetModelCache() {
  86. modelCache.clean()
  87. }