mirror of https://github.com/go-gorm/gorm.git
Save cached model structs globally
This commit is contained in:
parent
b0cf9a9e4b
commit
df33db6ff9
16
main.go
16
main.go
|
@ -23,7 +23,6 @@ type DB struct {
|
||||||
Value interface{}
|
Value interface{}
|
||||||
Error error
|
Error error
|
||||||
RowsAffected int64
|
RowsAffected int64
|
||||||
ModelStructs map[reflect.Type]*ModelStruct
|
|
||||||
callback *callback
|
callback *callback
|
||||||
db sqlCommon
|
db sqlCommon
|
||||||
parent *DB
|
parent *DB
|
||||||
|
@ -63,13 +62,12 @@ func Open(dialect string, args ...interface{}) (DB, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
db = DB{
|
db = DB{
|
||||||
dialect: NewDialect(dialect),
|
dialect: NewDialect(dialect),
|
||||||
logger: defaultLogger,
|
logger: defaultLogger,
|
||||||
callback: DefaultCallback,
|
callback: DefaultCallback,
|
||||||
source: source,
|
source: source,
|
||||||
values: map[string]interface{}{},
|
values: map[string]interface{}{},
|
||||||
db: dbSql,
|
db: dbSql,
|
||||||
ModelStructs: map[reflect.Type]*ModelStruct{},
|
|
||||||
}
|
}
|
||||||
db.parent = &db
|
db.parent = &db
|
||||||
}
|
}
|
||||||
|
@ -125,7 +123,7 @@ func (s *DB) LogMode(enable bool) *DB {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *DB) SingularTable(enable bool) {
|
func (s *DB) SingularTable(enable bool) {
|
||||||
s.parent.ModelStructs = map[reflect.Type]*ModelStruct{}
|
modelStructs = map[reflect.Type]*ModelStruct{}
|
||||||
s.parent.singularTable = enable
|
s.parent.singularTable = enable
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -11,6 +11,8 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var modelStructs = map[reflect.Type]*ModelStruct{}
|
||||||
|
|
||||||
type ModelStruct struct {
|
type ModelStruct struct {
|
||||||
PrimaryFields []*StructField
|
PrimaryFields []*StructField
|
||||||
StructFields []*StructField
|
StructFields []*StructField
|
||||||
|
@ -82,10 +84,8 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
|
||||||
scopeType = scopeType.Elem()
|
scopeType = scopeType.Elem()
|
||||||
}
|
}
|
||||||
|
|
||||||
if scope.db != nil {
|
if value, ok := modelStructs[scopeType]; ok {
|
||||||
if value, ok := scope.db.parent.ModelStructs[scopeType]; ok {
|
return value
|
||||||
return value
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
modelStruct.ModelType = scopeType
|
modelStruct.ModelType = scopeType
|
||||||
|
@ -284,9 +284,7 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
|
||||||
modelStruct.StructFields = append(modelStruct.StructFields, field)
|
modelStruct.StructFields = append(modelStruct.StructFields, field)
|
||||||
}
|
}
|
||||||
|
|
||||||
if scope.db != nil {
|
modelStructs[scopeType] = &modelStruct
|
||||||
scope.db.parent.ModelStructs[scopeType] = &modelStruct
|
|
||||||
}
|
|
||||||
|
|
||||||
return &modelStruct
|
return &modelStruct
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue