From 7574b91760309df810c6b506ff1d9fda877736d4 Mon Sep 17 00:00:00 2001 From: Anker Jam Date: Wed, 2 Sep 2020 00:26:25 +0800 Subject: [PATCH 1/4] add type modelRegister interface into Ormer --- pkg/client/orm/do_nothing_orm.go | 12 ++++++++++++ pkg/client/orm/filter_orm_decorator.go | 13 +++++++++++++ pkg/client/orm/models.go | 23 +++++++++++++++++++++++ pkg/client/orm/models_boot.go | 7 ------- pkg/client/orm/orm.go | 13 +++++++++++++ pkg/client/orm/types.go | 1 + 6 files changed, 62 insertions(+), 7 deletions(-) diff --git a/pkg/client/orm/do_nothing_orm.go b/pkg/client/orm/do_nothing_orm.go index e27e7f3a..07c7fd74 100644 --- a/pkg/client/orm/do_nothing_orm.go +++ b/pkg/client/orm/do_nothing_orm.go @@ -30,6 +30,18 @@ var _ Ormer = new(DoNothingOrm) type DoNothingOrm struct { } +func (d *DoNothingOrm) RegisterModels(models ...interface{}) (err error) { + return nil +} + +func (d *DoNothingOrm) RegisterModelsWithPrefix(prefix string, models ...interface{}) (err error) { + return nil +} + +func (d *DoNothingOrm) RegisterModelsWithSuffix(suffix string, models ...interface{}) (err error) { + return nil +} + func (d *DoNothingOrm) Read(md interface{}, cols ...string) error { return nil } diff --git a/pkg/client/orm/filter_orm_decorator.go b/pkg/client/orm/filter_orm_decorator.go index d0c5c537..095c8485 100644 --- a/pkg/client/orm/filter_orm_decorator.go +++ b/pkg/client/orm/filter_orm_decorator.go @@ -32,6 +32,7 @@ var _ TxOrmer = new(filterOrmDecorator) type filterOrmDecorator struct { ormer + modelRegister TxBeginner TxCommitter @@ -42,6 +43,18 @@ type filterOrmDecorator struct { txName string } +func (f *filterOrmDecorator) RegisterModels(models ...interface{}) (err error) { + return f.modelRegister.RegisterModels(models...) +} + +func (f *filterOrmDecorator) RegisterModelsWithPrefix(prefix string, models ...interface{}) (err error) { + return f.modelRegister.RegisterModelsWithPrefix(prefix, models...) +} + +func (f *filterOrmDecorator) RegisterModelsWithSuffix(suffix string, models ...interface{}) (err error) { + return f.modelRegister.RegisterModelsWithSuffix(suffix, models...) +} + func NewFilterOrmDecorator(delegate Ormer, filterChains ...FilterChain) Ormer { res := &filterOrmDecorator{ ormer: delegate, diff --git a/pkg/client/orm/models.go b/pkg/client/orm/models.go index a7de10f7..97faa00a 100644 --- a/pkg/client/orm/models.go +++ b/pkg/client/orm/models.go @@ -39,6 +39,15 @@ var ( } ) +type modelRegister interface { + //RegisterModels register models without prefix or suffix + RegisterModels(models ...interface{}) (err error) + //RegisterModelsWithPrefix register models with prefix + RegisterModelsWithPrefix(prefix string, models ...interface{}) (err error) + //RegisterModelsWithSuffix register models with suffix + RegisterModelsWithSuffix(suffix string, models ...interface{}) (err error) +} + // model info collection type _modelCache struct { sync.RWMutex // only used outsite for bootStrap @@ -48,6 +57,20 @@ type _modelCache struct { done bool } +var _ modelRegister = new(_modelCache) + +func (mc *_modelCache) RegisterModels(models ...interface{}) (err error) { + return mc.register(``, true, models...) +} + +func (mc *_modelCache) RegisterModelsWithPrefix(prefix string, models ...interface{}) (err error) { + return mc.register(prefix, true, models...) +} + +func (mc *_modelCache) RegisterModelsWithSuffix(suffix string, models ...interface{}) (err error) { + return mc.register(suffix, false, models...) +} + // get all model info func (mc *_modelCache) all() map[string]*modelInfo { m := make(map[string]*modelInfo, len(mc.cache)) diff --git a/pkg/client/orm/models_boot.go b/pkg/client/orm/models_boot.go index 407cf536..9a0ce893 100644 --- a/pkg/client/orm/models_boot.go +++ b/pkg/client/orm/models_boot.go @@ -14,15 +14,8 @@ package orm -import ( - "fmt" -) - // RegisterModel register models func RegisterModel(models ...interface{}) { - if modelCache.done { - panic(fmt.Errorf("RegisterModel must be run before BootStrap")) - } RegisterModelWithPrefix("", models...) } diff --git a/pkg/client/orm/orm.go b/pkg/client/orm/orm.go index 634b1892..d82f7e05 100644 --- a/pkg/client/orm/orm.go +++ b/pkg/client/orm/orm.go @@ -498,10 +498,23 @@ func (o *ormBase) DBStats() *sql.DBStats { type orm struct { ormBase + modelRegister } var _ Ormer = new(orm) +func (o *orm) RegisterModels(models ...interface{}) (err error) { + return o.modelRegister.RegisterModels(models) +} + +func (o *orm) RegisterModelsWithPrefix(prefix string, models ...interface{}) (err error) { + return o.modelRegister.RegisterModelsWithPrefix(prefix, models...) +} + +func (o *orm) RegisterModelsWithSuffix(suffix string, models ...interface{}) (err error) { + return o.modelRegister.RegisterModelsWithSuffix(suffix, models...) +} + func (o *orm) Begin() (TxOrmer, error) { return o.BeginWithCtx(context.Background()) } diff --git a/pkg/client/orm/types.go b/pkg/client/orm/types.go index eb34e759..584f0f8a 100644 --- a/pkg/client/orm/types.go +++ b/pkg/client/orm/types.go @@ -214,6 +214,7 @@ type ormer interface { type Ormer interface { ormer + modelRegister TxBeginner } From 7a53baaf9b4badb2572abb4e081210a17cee1d68 Mon Sep 17 00:00:00 2001 From: Anker Jam Date: Wed, 2 Sep 2020 00:33:46 +0800 Subject: [PATCH 2/4] 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 } From 00e44952ffda640ee6f1918522dfa2f6939ef939 Mon Sep 17 00:00:00 2001 From: jianzhiyao Date: Wed, 9 Sep 2020 19:04:34 +0800 Subject: [PATCH 3/4] optimize modelCache --- pkg/client/orm/filter_orm_decorator.go | 8 ++++---- pkg/client/orm/models.go | 10 +++++----- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/pkg/client/orm/filter_orm_decorator.go b/pkg/client/orm/filter_orm_decorator.go index 5a49e395..3271c520 100644 --- a/pkg/client/orm/filter_orm_decorator.go +++ b/pkg/client/orm/filter_orm_decorator.go @@ -17,6 +17,7 @@ package orm import ( "context" "database/sql" + "errors" "reflect" "time" @@ -32,7 +33,6 @@ var _ TxOrmer = new(filterOrmDecorator) type filterOrmDecorator struct { ormer - modelCacheHandler TxBeginner TxCommitter @@ -44,15 +44,15 @@ type filterOrmDecorator struct { } func (f *filterOrmDecorator) RegisterModels(models ...interface{}) (err error) { - return f.modelCacheHandler.RegisterModels(models...) + return errors.New(`not callable`) } func (f *filterOrmDecorator) RegisterModelsWithPrefix(prefix string, models ...interface{}) (err error) { - return f.modelCacheHandler.RegisterModelsWithPrefix(prefix, models...) + return errors.New(`not callable`) } func (f *filterOrmDecorator) RegisterModelsWithSuffix(suffix string, models ...interface{}) (err error) { - return f.modelCacheHandler.RegisterModelsWithSuffix(suffix, models...) + return errors.New(`not callable`) } func NewFilterOrmDecorator(delegate Ormer, filterChains ...FilterChain) Ormer { diff --git a/pkg/client/orm/models.go b/pkg/client/orm/models.go index 55ba5a73..b38ea9e5 100644 --- a/pkg/client/orm/models.go +++ b/pkg/client/orm/models.go @@ -349,7 +349,7 @@ end: fmt.Println(err) debug.PrintStack() } - modelCache.done = true + mc.done = true return } @@ -432,14 +432,14 @@ func (mc *_modelCache) register(prefixOrSuffixStr string, prefixOrSuffix bool, m //getDbDropSQL get database scheme drop sql queries func (mc *_modelCache) getDbDropSQL(al *alias) (queries []string, err error) { - if len(modelCache.cache) == 0 { + if len(mc.cache) == 0 { err = errors.New("no Model found, need register your model") return } Q := al.DbBaser.TableQuote() - for _, mi := range modelCache.allOrdered() { + for _, mi := range mc.allOrdered() { queries = append(queries, fmt.Sprintf(`DROP TABLE IF EXISTS %s%s%s`, Q, mi.table, Q)) } return queries,nil @@ -447,7 +447,7 @@ func (mc *_modelCache) getDbDropSQL(al *alias) (queries []string, err error) { //getDbCreateSQL get database scheme creation sql queries func (mc *_modelCache) getDbCreateSQL(al *alias) (queries []string, tableIndexes map[string][]dbIndex, err error) { - if len(modelCache.cache) == 0 { + if len(mc.cache) == 0 { err = errors.New("no Model found, need register your model") return } @@ -458,7 +458,7 @@ func (mc *_modelCache) getDbCreateSQL(al *alias) (queries []string, tableIndexes tableIndexes = make(map[string][]dbIndex) - for _, mi := range modelCache.allOrdered() { + for _, mi := range mc.allOrdered() { sql := fmt.Sprintf("-- %s\n", strings.Repeat("-", 50)) sql += fmt.Sprintf("-- Table Structure for `%s`\n", mi.fullName) sql += fmt.Sprintf("-- %s\n", strings.Repeat("-", 50)) From b7bc57c4d155f4dc41ebae59ecafc1f4a89f8021 Mon Sep 17 00:00:00 2001 From: jianzhiyao Date: Wed, 16 Sep 2020 19:46:14 +0800 Subject: [PATCH 4/4] delete interface --- pkg/client/orm/do_nothing_orm.go | 12 ------------ pkg/client/orm/filter_orm_decorator.go | 13 ------------- pkg/client/orm/models.go | 23 ----------------------- pkg/client/orm/orm.go | 15 --------------- pkg/client/orm/types.go | 1 - 5 files changed, 64 deletions(-) diff --git a/pkg/client/orm/do_nothing_orm.go b/pkg/client/orm/do_nothing_orm.go index 07c7fd74..e27e7f3a 100644 --- a/pkg/client/orm/do_nothing_orm.go +++ b/pkg/client/orm/do_nothing_orm.go @@ -30,18 +30,6 @@ var _ Ormer = new(DoNothingOrm) type DoNothingOrm struct { } -func (d *DoNothingOrm) RegisterModels(models ...interface{}) (err error) { - return nil -} - -func (d *DoNothingOrm) RegisterModelsWithPrefix(prefix string, models ...interface{}) (err error) { - return nil -} - -func (d *DoNothingOrm) RegisterModelsWithSuffix(suffix string, models ...interface{}) (err error) { - return nil -} - func (d *DoNothingOrm) Read(md interface{}, cols ...string) error { return nil } diff --git a/pkg/client/orm/filter_orm_decorator.go b/pkg/client/orm/filter_orm_decorator.go index 3271c520..d0c5c537 100644 --- a/pkg/client/orm/filter_orm_decorator.go +++ b/pkg/client/orm/filter_orm_decorator.go @@ -17,7 +17,6 @@ package orm import ( "context" "database/sql" - "errors" "reflect" "time" @@ -43,18 +42,6 @@ type filterOrmDecorator struct { txName string } -func (f *filterOrmDecorator) RegisterModels(models ...interface{}) (err error) { - return errors.New(`not callable`) -} - -func (f *filterOrmDecorator) RegisterModelsWithPrefix(prefix string, models ...interface{}) (err error) { - return errors.New(`not callable`) -} - -func (f *filterOrmDecorator) RegisterModelsWithSuffix(suffix string, models ...interface{}) (err error) { - return errors.New(`not callable`) -} - func NewFilterOrmDecorator(delegate Ormer, filterChains ...FilterChain) Ormer { res := &filterOrmDecorator{ ormer: delegate, diff --git a/pkg/client/orm/models.go b/pkg/client/orm/models.go index ea315c2b..19941d2e 100644 --- a/pkg/client/orm/models.go +++ b/pkg/client/orm/models.go @@ -36,15 +36,6 @@ var ( modelCache = NewModelCacheHandler() ) -type modelCacheHandler interface { - //RegisterModels register models without prefix or suffix - RegisterModels(models ...interface{}) (err error) - //RegisterModelsWithPrefix register models with prefix - RegisterModelsWithPrefix(prefix string, models ...interface{}) (err error) - //RegisterModelsWithSuffix register models with suffix - RegisterModelsWithSuffix(suffix string, models ...interface{}) (err error) -} - // model info collection type _modelCache struct { sync.RWMutex // only used outsite for bootStrap @@ -62,20 +53,6 @@ func NewModelCacheHandler() *_modelCache { } } -var _ modelCacheHandler = new(_modelCache) - -func (mc *_modelCache) RegisterModels(models ...interface{}) (err error) { - return mc.register(``, true, models...) -} - -func (mc *_modelCache) RegisterModelsWithPrefix(prefix string, models ...interface{}) (err error) { - return mc.register(prefix, true, models...) -} - -func (mc *_modelCache) RegisterModelsWithSuffix(suffix string, models ...interface{}) (err error) { - return mc.register(suffix, false, models...) -} - // get all model info func (mc *_modelCache) all() map[string]*modelInfo { m := make(map[string]*modelInfo, len(mc.cache)) diff --git a/pkg/client/orm/orm.go b/pkg/client/orm/orm.go index 557c788c..bfb710d1 100644 --- a/pkg/client/orm/orm.go +++ b/pkg/client/orm/orm.go @@ -496,23 +496,10 @@ func (o *ormBase) DBStats() *sql.DBStats { type orm struct { ormBase - modelCacheHandler } var _ Ormer = new(orm) -func (o *orm) RegisterModels(models ...interface{}) (err error) { - return o.modelCacheHandler.RegisterModels(models) -} - -func (o *orm) RegisterModelsWithPrefix(prefix string, models ...interface{}) (err error) { - return o.modelCacheHandler.RegisterModelsWithPrefix(prefix, models...) -} - -func (o *orm) RegisterModelsWithSuffix(suffix string, models ...interface{}) (err error) { - return o.modelCacheHandler.RegisterModelsWithSuffix(suffix, models...) -} - func (o *orm) Begin() (TxOrmer, error) { return o.BeginWithCtx(context.Background()) } @@ -633,8 +620,6 @@ 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 b9c444eb..b0c793b7 100644 --- a/pkg/client/orm/types.go +++ b/pkg/client/orm/types.go @@ -219,7 +219,6 @@ type ormer interface { type Ormer interface { ormer - modelCacheHandler TxBeginner }