forked from mirror/gorm
Cache generated model structs
This commit is contained in:
parent
0b32041135
commit
73a0401678
3
main.go
3
main.go
|
@ -23,6 +23,7 @@ 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
|
||||||
|
@ -67,6 +68,7 @@ func Open(dialect string, args ...interface{}) (DB, error) {
|
||||||
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
|
||||||
}
|
}
|
||||||
|
@ -114,6 +116,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{}
|
||||||
s.parent.singularTable = enable
|
s.parent.singularTable = enable
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,7 +3,7 @@ package gorm
|
||||||
import "time"
|
import "time"
|
||||||
|
|
||||||
func (s *DB) clone() *DB {
|
func (s *DB) clone() *DB {
|
||||||
db := DB{db: s.db, parent: s.parent, logMode: s.logMode, Value: s.Value, Error: s.Error, values: map[string]interface{}{}}
|
db := DB{db: s.db, parent: s.parent, logMode: s.logMode, values: map[string]interface{}{}, Value: s.Value, Error: s.Error}
|
||||||
|
|
||||||
for key, value := range s.values {
|
for key, value := range s.values {
|
||||||
db.values[key] = value
|
db.values[key] = value
|
||||||
|
|
|
@ -33,6 +33,24 @@ type StructField struct {
|
||||||
Relationship *Relationship
|
Relationship *Relationship
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (structField *StructField) clone() *StructField {
|
||||||
|
return &StructField{
|
||||||
|
DBName: structField.DBName,
|
||||||
|
Name: structField.Name,
|
||||||
|
Names: structField.Names,
|
||||||
|
IsPrimaryKey: structField.IsPrimaryKey,
|
||||||
|
IsScanner: structField.IsScanner,
|
||||||
|
IsTime: structField.IsTime,
|
||||||
|
IsNormal: structField.IsNormal,
|
||||||
|
IsIgnored: structField.IsIgnored,
|
||||||
|
DefaultValue: structField.DefaultValue,
|
||||||
|
SqlTag: structField.SqlTag,
|
||||||
|
Tag: structField.Tag,
|
||||||
|
Struct: structField.Struct,
|
||||||
|
Relationship: structField.Relationship,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
type Relationship struct {
|
type Relationship struct {
|
||||||
Kind string
|
Kind string
|
||||||
ForeignType string
|
ForeignType string
|
||||||
|
@ -105,6 +123,14 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
|
||||||
}
|
}
|
||||||
|
|
||||||
scopeType := reflectValue.Type()
|
scopeType := reflectValue.Type()
|
||||||
|
if scope.db != nil {
|
||||||
|
if value, ok := scope.db.parent.ModelStructs[scopeType]; ok {
|
||||||
|
return value
|
||||||
|
} else {
|
||||||
|
scope.db.parent.ModelStructs[scopeType] = &modelStruct
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if scopeType.Kind() == reflect.Ptr {
|
if scopeType.Kind() == reflect.Ptr {
|
||||||
scopeType = scopeType.Elem()
|
scopeType = scopeType.Elem()
|
||||||
}
|
}
|
||||||
|
@ -223,6 +249,7 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
|
||||||
case reflect.Struct:
|
case reflect.Struct:
|
||||||
if _, ok := gormSettings["EMBEDDED"]; ok || fieldStruct.Anonymous {
|
if _, ok := gormSettings["EMBEDDED"]; ok || fieldStruct.Anonymous {
|
||||||
for _, field := range scope.New(reflect.New(indirectType).Interface()).GetStructFields() {
|
for _, field := range scope.New(reflect.New(indirectType).Interface()).GetStructFields() {
|
||||||
|
field = field.clone()
|
||||||
field.Names = append([]string{fieldStruct.Name}, field.Names...)
|
field.Names = append([]string{fieldStruct.Name}, field.Names...)
|
||||||
modelStruct.StructFields = append(modelStruct.StructFields, field)
|
modelStruct.StructFields = append(modelStruct.StructFields, field)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue