mirror of
https://github.com/astaxie/beego.git
synced 2024-11-22 01:00:54 +00:00
Merge pull request #4203 from jianzhiyao/frt/proposal_4105
warpping for global modelCache
This commit is contained in:
commit
fbaf3380c6
@ -33,10 +33,7 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
modelCache = &_modelCache{
|
modelCache = NewModelCacheHandler()
|
||||||
cache: make(map[string]*modelInfo),
|
|
||||||
cacheByFullName: make(map[string]*modelInfo),
|
|
||||||
}
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// model info collection
|
// model info collection
|
||||||
@ -48,6 +45,14 @@ type _modelCache struct {
|
|||||||
done bool
|
done bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//NewModelCacheHandler generator of _modelCache
|
||||||
|
func NewModelCacheHandler() *_modelCache {
|
||||||
|
return &_modelCache{
|
||||||
|
cache: make(map[string]*modelInfo),
|
||||||
|
cacheByFullName: make(map[string]*modelInfo),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// get all model info
|
// get all model info
|
||||||
func (mc *_modelCache) all() map[string]*modelInfo {
|
func (mc *_modelCache) all() map[string]*modelInfo {
|
||||||
m := make(map[string]*modelInfo, len(mc.cache))
|
m := make(map[string]*modelInfo, len(mc.cache))
|
||||||
@ -321,7 +326,7 @@ end:
|
|||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
debug.PrintStack()
|
debug.PrintStack()
|
||||||
}
|
}
|
||||||
modelCache.done = true
|
mc.done = true
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -404,14 +409,14 @@ func (mc *_modelCache) register(prefixOrSuffixStr string, prefixOrSuffix bool, m
|
|||||||
|
|
||||||
//getDbDropSQL get database scheme drop sql queries
|
//getDbDropSQL get database scheme drop sql queries
|
||||||
func (mc *_modelCache) getDbDropSQL(al *alias) (queries []string, err error) {
|
func (mc *_modelCache) getDbDropSQL(al *alias) (queries []string, err error) {
|
||||||
if len(modelCache.cache) == 0 {
|
if len(mc.cache) == 0 {
|
||||||
err = errors.New("no Model found, need register your model")
|
err = errors.New("no Model found, need register your model")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
Q := al.DbBaser.TableQuote()
|
Q := al.DbBaser.TableQuote()
|
||||||
|
|
||||||
for _, mi := range modelCache.allOrdered() {
|
for _, mi := range mc.allOrdered() {
|
||||||
queries = append(queries, fmt.Sprintf(`DROP TABLE IF EXISTS %s%s%s`, Q, mi.table, Q))
|
queries = append(queries, fmt.Sprintf(`DROP TABLE IF EXISTS %s%s%s`, Q, mi.table, Q))
|
||||||
}
|
}
|
||||||
return queries, nil
|
return queries, nil
|
||||||
@ -419,7 +424,7 @@ func (mc *_modelCache) getDbDropSQL(al *alias) (queries []string, err error) {
|
|||||||
|
|
||||||
//getDbCreateSQL get database scheme creation sql queries
|
//getDbCreateSQL get database scheme creation sql queries
|
||||||
func (mc *_modelCache) getDbCreateSQL(al *alias) (queries []string, tableIndexes map[string][]dbIndex, err error) {
|
func (mc *_modelCache) getDbCreateSQL(al *alias) (queries []string, tableIndexes map[string][]dbIndex, err error) {
|
||||||
if len(modelCache.cache) == 0 {
|
if len(mc.cache) == 0 {
|
||||||
err = errors.New("no Model found, need register your model")
|
err = errors.New("no Model found, need register your model")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -430,7 +435,7 @@ func (mc *_modelCache) getDbCreateSQL(al *alias) (queries []string, tableIndexes
|
|||||||
|
|
||||||
tableIndexes = make(map[string][]dbIndex)
|
tableIndexes = make(map[string][]dbIndex)
|
||||||
|
|
||||||
for _, mi := range modelCache.allOrdered() {
|
for _, mi := range mc.allOrdered() {
|
||||||
sql := fmt.Sprintf("-- %s\n", strings.Repeat("-", 50))
|
sql := fmt.Sprintf("-- %s\n", strings.Repeat("-", 50))
|
||||||
sql += fmt.Sprintf("-- Table Structure for `%s`\n", mi.fullName)
|
sql += fmt.Sprintf("-- Table Structure for `%s`\n", mi.fullName)
|
||||||
sql += fmt.Sprintf("-- %s\n", strings.Repeat("-", 50))
|
sql += fmt.Sprintf("-- %s\n", strings.Repeat("-", 50))
|
||||||
|
@ -14,15 +14,8 @@
|
|||||||
|
|
||||||
package orm
|
package orm
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
)
|
|
||||||
|
|
||||||
// RegisterModel register models
|
// RegisterModel register models
|
||||||
func RegisterModel(models ...interface{}) {
|
func RegisterModel(models ...interface{}) {
|
||||||
if modelCache.done {
|
|
||||||
panic(fmt.Errorf("RegisterModel must be run before BootStrap"))
|
|
||||||
}
|
|
||||||
RegisterModelWithPrefix("", models...)
|
RegisterModelWithPrefix("", models...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user