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{}
|
||||
Error error
|
||||
RowsAffected int64
|
||||
ModelStructs map[reflect.Type]*ModelStruct
|
||||
callback *callback
|
||||
db sqlCommon
|
||||
parent *DB
|
||||
|
@ -63,13 +62,12 @@ func Open(dialect string, args ...interface{}) (DB, error) {
|
|||
}
|
||||
|
||||
db = DB{
|
||||
dialect: NewDialect(dialect),
|
||||
logger: defaultLogger,
|
||||
callback: DefaultCallback,
|
||||
source: source,
|
||||
values: map[string]interface{}{},
|
||||
db: dbSql,
|
||||
ModelStructs: map[reflect.Type]*ModelStruct{},
|
||||
dialect: NewDialect(dialect),
|
||||
logger: defaultLogger,
|
||||
callback: DefaultCallback,
|
||||
source: source,
|
||||
values: map[string]interface{}{},
|
||||
db: dbSql,
|
||||
}
|
||||
db.parent = &db
|
||||
}
|
||||
|
@ -125,7 +123,7 @@ func (s *DB) LogMode(enable bool) *DB {
|
|||
}
|
||||
|
||||
func (s *DB) SingularTable(enable bool) {
|
||||
s.parent.ModelStructs = map[reflect.Type]*ModelStruct{}
|
||||
modelStructs = map[reflect.Type]*ModelStruct{}
|
||||
s.parent.singularTable = enable
|
||||
}
|
||||
|
||||
|
|
|
@ -11,6 +11,8 @@ import (
|
|||
"time"
|
||||
)
|
||||
|
||||
var modelStructs = map[reflect.Type]*ModelStruct{}
|
||||
|
||||
type ModelStruct struct {
|
||||
PrimaryFields []*StructField
|
||||
StructFields []*StructField
|
||||
|
@ -82,10 +84,8 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
|
|||
scopeType = scopeType.Elem()
|
||||
}
|
||||
|
||||
if scope.db != nil {
|
||||
if value, ok := scope.db.parent.ModelStructs[scopeType]; ok {
|
||||
return value
|
||||
}
|
||||
if value, ok := modelStructs[scopeType]; ok {
|
||||
return value
|
||||
}
|
||||
|
||||
modelStruct.ModelType = scopeType
|
||||
|
@ -284,9 +284,7 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
|
|||
modelStruct.StructFields = append(modelStruct.StructFields, field)
|
||||
}
|
||||
|
||||
if scope.db != nil {
|
||||
scope.db.parent.ModelStructs[scopeType] = &modelStruct
|
||||
}
|
||||
modelStructs[scopeType] = &modelStruct
|
||||
|
||||
return &modelStruct
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue