2013-07-30 12:32:38 +00:00
|
|
|
package orm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2013-09-09 14:33:41 +00:00
|
|
|
od_CASCADE = "cascade"
|
|
|
|
od_SET_NULL = "set_null"
|
|
|
|
od_SET_DEFAULT = "set_default"
|
|
|
|
od_DO_NOTHING = "do_nothing"
|
|
|
|
defaultStructTagName = "orm"
|
|
|
|
defaultStructTagDelim = ";"
|
2013-07-30 12:32:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2013-08-08 14:34:18 +00:00
|
|
|
modelCache = &_modelCache{
|
|
|
|
cache: make(map[string]*modelInfo),
|
|
|
|
cacheByFN: make(map[string]*modelInfo),
|
|
|
|
}
|
2013-07-30 12:32:38 +00:00
|
|
|
supportTag = map[string]int{
|
2013-08-09 12:14:18 +00:00
|
|
|
"-": 1,
|
2013-07-30 12:32:38 +00:00
|
|
|
"null": 1,
|
|
|
|
"index": 1,
|
|
|
|
"unique": 1,
|
|
|
|
"pk": 1,
|
|
|
|
"auto": 1,
|
|
|
|
"auto_now": 1,
|
|
|
|
"auto_now_add": 1,
|
2013-07-31 14:11:22 +00:00
|
|
|
"size": 2,
|
2013-07-30 12:32:38 +00:00
|
|
|
"column": 2,
|
|
|
|
"default": 2,
|
|
|
|
"rel": 2,
|
|
|
|
"reverse": 2,
|
|
|
|
"rel_table": 2,
|
|
|
|
"rel_through": 2,
|
|
|
|
"digits": 2,
|
|
|
|
"decimals": 2,
|
|
|
|
"on_delete": 2,
|
2013-07-31 14:11:22 +00:00
|
|
|
"type": 2,
|
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 {
|
|
|
|
sync.RWMutex
|
2013-08-08 14:34:18 +00:00
|
|
|
orders []string
|
|
|
|
cache map[string]*modelInfo
|
|
|
|
cacheByFN 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
|
|
|
|
}
|
|
|
|
|
2014-01-17 15:28:54 +00:00
|
|
|
// get orderd 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]
|
2013-08-09 12:14:18 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-01-17 15:28:54 +00:00
|
|
|
// get model info by field name
|
2013-08-09 12:14:18 +00:00
|
|
|
func (mc *_modelCache) getByFN(name string) (mi *modelInfo, ok bool) {
|
|
|
|
mi, ok = mc.cacheByFN[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
|
2013-08-08 14:34:18 +00:00
|
|
|
mc.cacheByFN[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)
|
|
|
|
mc.cacheByFN = make(map[string]*modelInfo)
|
|
|
|
mc.done = false
|
|
|
|
}
|