From 7a53baaf9b4badb2572abb4e081210a17cee1d68 Mon Sep 17 00:00:00 2001 From: Anker Jam Date: Wed, 2 Sep 2020 00:33:46 +0800 Subject: [PATCH] rename modelRegister to modelCacheHandler --- pkg/client/orm/filter_orm_decorator.go | 8 ++++---- pkg/client/orm/models.go | 17 +++++++++++------ pkg/client/orm/orm.go | 10 ++++++---- pkg/client/orm/types.go | 2 +- 4 files changed, 22 insertions(+), 15 deletions(-) diff --git a/pkg/client/orm/filter_orm_decorator.go b/pkg/client/orm/filter_orm_decorator.go index 095c8485..5a49e395 100644 --- a/pkg/client/orm/filter_orm_decorator.go +++ b/pkg/client/orm/filter_orm_decorator.go @@ -32,7 +32,7 @@ var _ TxOrmer = new(filterOrmDecorator) type filterOrmDecorator struct { ormer - modelRegister + modelCacheHandler TxBeginner TxCommitter @@ -44,15 +44,15 @@ type filterOrmDecorator struct { } func (f *filterOrmDecorator) RegisterModels(models ...interface{}) (err error) { - return f.modelRegister.RegisterModels(models...) + return f.modelCacheHandler.RegisterModels(models...) } func (f *filterOrmDecorator) RegisterModelsWithPrefix(prefix string, models ...interface{}) (err error) { - return f.modelRegister.RegisterModelsWithPrefix(prefix, models...) + return f.modelCacheHandler.RegisterModelsWithPrefix(prefix, models...) } func (f *filterOrmDecorator) RegisterModelsWithSuffix(suffix string, models ...interface{}) (err error) { - return f.modelRegister.RegisterModelsWithSuffix(suffix, models...) + return f.modelCacheHandler.RegisterModelsWithSuffix(suffix, models...) } func NewFilterOrmDecorator(delegate Ormer, filterChains ...FilterChain) Ormer { diff --git a/pkg/client/orm/models.go b/pkg/client/orm/models.go index 97faa00a..55ba5a73 100644 --- a/pkg/client/orm/models.go +++ b/pkg/client/orm/models.go @@ -33,13 +33,10 @@ const ( ) var ( - modelCache = &_modelCache{ - cache: make(map[string]*modelInfo), - cacheByFullName: make(map[string]*modelInfo), - } + modelCache = NewModelCacheHandler() ) -type modelRegister interface { +type modelCacheHandler interface { //RegisterModels register models without prefix or suffix RegisterModels(models ...interface{}) (err error) //RegisterModelsWithPrefix register models with prefix @@ -57,7 +54,15 @@ type _modelCache struct { done bool } -var _ modelRegister = new(_modelCache) +//NewModelCacheHandler generator of _modelCache +func NewModelCacheHandler() *_modelCache { + return &_modelCache{ + cache: make(map[string]*modelInfo), + cacheByFullName: make(map[string]*modelInfo), + } +} + +var _ modelCacheHandler = new(_modelCache) func (mc *_modelCache) RegisterModels(models ...interface{}) (err error) { return mc.register(``, true, models...) diff --git a/pkg/client/orm/orm.go b/pkg/client/orm/orm.go index d82f7e05..a18dae3c 100644 --- a/pkg/client/orm/orm.go +++ b/pkg/client/orm/orm.go @@ -498,21 +498,21 @@ func (o *ormBase) DBStats() *sql.DBStats { type orm struct { ormBase - modelRegister + modelCacheHandler } var _ Ormer = new(orm) func (o *orm) RegisterModels(models ...interface{}) (err error) { - return o.modelRegister.RegisterModels(models) + return o.modelCacheHandler.RegisterModels(models) } func (o *orm) RegisterModelsWithPrefix(prefix string, models ...interface{}) (err error) { - return o.modelRegister.RegisterModelsWithPrefix(prefix, models...) + return o.modelCacheHandler.RegisterModelsWithPrefix(prefix, models...) } func (o *orm) RegisterModelsWithSuffix(suffix string, models ...interface{}) (err error) { - return o.modelRegister.RegisterModelsWithSuffix(suffix, models...) + return o.modelCacheHandler.RegisterModelsWithSuffix(suffix, models...) } func (o *orm) Begin() (TxOrmer, error) { @@ -635,6 +635,8 @@ func newDBWithAlias(al *alias) Ormer { o.db = al.DB } + o.modelCacheHandler = NewModelCacheHandler() + if len(globalFilterChains) > 0 { return NewFilterOrmDecorator(o, globalFilterChains...) } diff --git a/pkg/client/orm/types.go b/pkg/client/orm/types.go index 584f0f8a..cee570af 100644 --- a/pkg/client/orm/types.go +++ b/pkg/client/orm/types.go @@ -214,7 +214,7 @@ type ormer interface { type Ormer interface { ormer - modelRegister + modelCacheHandler TxBeginner }