From 2e7fb81348635b6cba4658d5813f321d5f6cb3ce Mon Sep 17 00:00:00 2001 From: Ming Deng Date: Mon, 27 Jul 2020 21:19:34 +0800 Subject: [PATCH 1/4] deprecated orm.go and add NewOrmUsingDB method --- orm/db_alias.go | 23 ++++++++++++++++++++++- pkg/orm/db_alias_test.go | 10 ++++++++++ pkg/orm/orm.go | 10 ++++++---- 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/orm/db_alias.go b/orm/db_alias.go index fe6abeb5..d3dbc595 100644 --- a/orm/db_alias.go +++ b/orm/db_alias.go @@ -41,12 +41,14 @@ const ( type driver string // get type constant int of current driver.. +// Deprecated: using pkg/orm. We will remove this method in v2.1.0 func (d driver) Type() DriverType { a, _ := dataBaseCache.get(string(d)) return a.Driver } // get name of current driver +// Deprecated: using pkg/orm. We will remove this method in v2.1.0 func (d driver) Name() string { return string(d) } @@ -111,15 +113,19 @@ type DB struct { stmtDecorators *lru.Cache } +// Begin start a transaction +// Deprecated: using pkg/orm. We will remove this method in v2.1.0 func (d *DB) Begin() (*sql.Tx, error) { return d.DB.Begin() } +// BeginTx start a transaction with context and those options +// Deprecated: using pkg/orm. We will remove this method in v2.1.0 func (d *DB) BeginTx(ctx context.Context, opts *sql.TxOptions) (*sql.Tx, error) { return d.DB.BeginTx(ctx, opts) } -//su must call release to release *sql.Stmt after using +// su must call release to release *sql.Stmt after using func (d *DB) getStmtDecorator(query string) (*stmtDecorator, error) { d.RLock() c, ok := d.stmtDecorators.Get(query) @@ -151,14 +157,17 @@ func (d *DB) getStmtDecorator(query string) (*stmtDecorator, error) { return sd, nil } +// Deprecated: using pkg/orm. We will remove this method in v2.1.0 func (d *DB) Prepare(query string) (*sql.Stmt, error) { return d.DB.Prepare(query) } +// Deprecated: using pkg/orm. We will remove this method in v2.1.0 func (d *DB) PrepareContext(ctx context.Context, query string) (*sql.Stmt, error) { return d.DB.PrepareContext(ctx, query) } +// Deprecated: using pkg/orm. We will remove this method in v2.1.0 func (d *DB) Exec(query string, args ...interface{}) (sql.Result, error) { sd, err := d.getStmtDecorator(query) if err != nil { @@ -169,6 +178,7 @@ func (d *DB) Exec(query string, args ...interface{}) (sql.Result, error) { return stmt.Exec(args...) } +// Deprecated: using pkg/orm. We will remove this method in v2.1.0 func (d *DB) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error) { sd, err := d.getStmtDecorator(query) if err != nil { @@ -179,6 +189,7 @@ func (d *DB) ExecContext(ctx context.Context, query string, args ...interface{}) return stmt.ExecContext(ctx, args...) } +// Deprecated: using pkg/orm. We will remove this method in v2.1.0 func (d *DB) Query(query string, args ...interface{}) (*sql.Rows, error) { sd, err := d.getStmtDecorator(query) if err != nil { @@ -189,6 +200,7 @@ func (d *DB) Query(query string, args ...interface{}) (*sql.Rows, error) { return stmt.Query(args...) } +// Deprecated: using pkg/orm. We will remove this method in v2.1.0 func (d *DB) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error) { sd, err := d.getStmtDecorator(query) if err != nil { @@ -199,6 +211,7 @@ func (d *DB) QueryContext(ctx context.Context, query string, args ...interface{} return stmt.QueryContext(ctx, args...) } +// Deprecated: using pkg/orm. We will remove this method in v2.1.0 func (d *DB) QueryRow(query string, args ...interface{}) *sql.Row { sd, err := d.getStmtDecorator(query) if err != nil { @@ -210,6 +223,7 @@ func (d *DB) QueryRow(query string, args ...interface{}) *sql.Row { } +// Deprecated: using pkg/orm. We will remove this method in v2.1.0 func (d *DB) QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row { sd, err := d.getStmtDecorator(query) if err != nil { @@ -319,12 +333,14 @@ func addAliasWthDB(aliasName, driverName string, db *sql.DB) (*alias, error) { } // AddAliasWthDB add a aliasName for the drivename +// Deprecated: using pkg/orm. We will remove this method in v2.1.0 func AddAliasWthDB(aliasName, driverName string, db *sql.DB) error { _, err := addAliasWthDB(aliasName, driverName, db) return err } // RegisterDataBase Setting the database connect params. Use the database driver self dataSource args. +// Deprecated: using pkg/orm. We will remove this method in v2.1.0 func RegisterDataBase(aliasName, driverName, dataSource string, params ...int) error { var ( err error @@ -368,6 +384,7 @@ end: } // RegisterDriver Register a database driver use specify driver name, this can be definition the driver is which database type. +// Deprecated: using pkg/orm. We will remove this method in v2.1.0 func RegisterDriver(driverName string, typ DriverType) error { if t, ok := drivers[driverName]; !ok { drivers[driverName] = typ @@ -380,6 +397,7 @@ func RegisterDriver(driverName string, typ DriverType) error { } // SetDataBaseTZ Change the database default used timezone +// Deprecated: using pkg/orm. We will remove this method in v2.1.0 func SetDataBaseTZ(aliasName string, tz *time.Location) error { if al, ok := dataBaseCache.get(aliasName); ok { al.TZ = tz @@ -390,6 +408,7 @@ func SetDataBaseTZ(aliasName string, tz *time.Location) error { } // SetMaxIdleConns Change the max idle conns for *sql.DB, use specify database alias name +// Deprecated: using pkg/orm. We will remove this method in v2.1.0 func SetMaxIdleConns(aliasName string, maxIdleConns int) { al := getDbAlias(aliasName) al.MaxIdleConns = maxIdleConns @@ -397,6 +416,7 @@ func SetMaxIdleConns(aliasName string, maxIdleConns int) { } // SetMaxOpenConns Change the max open conns for *sql.DB, use specify database alias name +// Deprecated: using pkg/orm. We will remove this method in v2.1.0 func SetMaxOpenConns(aliasName string, maxOpenConns int) { al := getDbAlias(aliasName) al.MaxOpenConns = maxOpenConns @@ -409,6 +429,7 @@ func SetMaxOpenConns(aliasName string, maxOpenConns int) { // GetDB Get *sql.DB from registered database by db alias name. // Use "default" as alias name if you not set. +// Deprecated: using pkg/orm. We will remove this method in v2.1.0 func GetDB(aliasNames ...string) (*sql.DB, error) { var name string if len(aliasNames) > 0 { diff --git a/pkg/orm/db_alias_test.go b/pkg/orm/db_alias_test.go index a0cdcd44..81b623c8 100644 --- a/pkg/orm/db_alias_test.go +++ b/pkg/orm/db_alias_test.go @@ -42,3 +42,13 @@ func TestRegisterDataBase(t *testing.T) { assert.Equal(t, al.MaxOpenConns, 300) assert.Equal(t, al.ConnMaxLifetime, time.Minute) } + +func TestDBCache(t *testing.T) { + dataBaseCache.add("test1", &alias{}) + dataBaseCache.add("default", &alias{}) + al := dataBaseCache.getDefault() + assert.NotNil(t, al) + al, ok := dataBaseCache.get("test1") + assert.NotNil(t, al) + assert.True(t, ok) +} diff --git a/pkg/orm/orm.go b/pkg/orm/orm.go index 8ef761f4..07084577 100644 --- a/pkg/orm/orm.go +++ b/pkg/orm/orm.go @@ -591,10 +591,13 @@ func (t *txOrm) Rollback() error { // NewOrm create new orm func NewOrm() Ormer { BootStrap() // execute only once + return NewOrmUsingDB(`default`) +} +// NewOrm create new orm with the name +func NewOrmUsingDB(aliasName string) Ormer { o := new(orm) - name := `default` - if al, ok := dataBaseCache.get(name); ok { + if al, ok := dataBaseCache.get(aliasName); ok { o.alias = al if Debug { o.db = newDbQueryLog(al, al.DB) @@ -602,9 +605,8 @@ func NewOrm() Ormer { o.db = al.DB } } else { - panic(fmt.Errorf(" unknown db alias name `%s`", name)) + panic(fmt.Errorf(" unknown db alias name `%s`", aliasName)) } - return o } From 21f281655d25c9add1aac0bde05bed1b13e25b06 Mon Sep 17 00:00:00 2001 From: Ming Deng Date: Mon, 27 Jul 2020 21:22:40 +0800 Subject: [PATCH 2/4] remove QueryRelated and QueryRelatedCtx --- pkg/orm/orm.go | 16 +--------------- 1 file changed, 1 insertion(+), 15 deletions(-) diff --git a/pkg/orm/orm.go b/pkg/orm/orm.go index 07084577..3b94ab6c 100644 --- a/pkg/orm/orm.go +++ b/pkg/orm/orm.go @@ -305,6 +305,7 @@ func (o *ormBase) QueryM2MWithCtx(ctx context.Context, md interface{}, name stri func (o *ormBase) LoadRelated(md interface{}, name string, args ...interface{}) (int64, error) { return o.LoadRelatedWithCtx(context.Background(), md, name, args...) } + func (o *ormBase) LoadRelatedWithCtx(ctx context.Context, md interface{}, name string, args ...interface{}) (int64, error) { _, fi, ind, qseter := o.queryRelated(md, name) @@ -366,21 +367,6 @@ func (o *ormBase) LoadRelatedWithCtx(ctx context.Context, md interface{}, name s return nums, err } -// return a QuerySeter for related models to md model. -// it can do all, update, delete in QuerySeter. -// example: -// qs := orm.QueryRelated(post,"Tag") -// qs.All(&[]*Tag{}) -// -func (o *ormBase) QueryRelated(md interface{}, name string) QuerySeter { - return o.QueryRelatedWithCtx(context.Background(), md, name) -} -func (o *ormBase) QueryRelatedWithCtx(ctx context.Context, md interface{}, name string) QuerySeter { - // is this api needed ? - _, _, _, qs := o.queryRelated(md, name) - return qs -} - // get QuerySeter for related models to md model func (o *ormBase) queryRelated(md interface{}, name string) (*modelInfo, *fieldInfo, reflect.Value, QuerySeter) { mi, ind := o.getMiInd(md, true) From 54ef4766002facfb8342695aafc44857a0a3652b Mon Sep 17 00:00:00 2001 From: Ming Deng Date: Tue, 28 Jul 2020 06:28:51 +0000 Subject: [PATCH 3/4] add tag interfaces and remove log.go --- log.go | 127 ------------------------------------ orm/orm.go | 23 +++++++ orm/types.go | 1 + pkg/log.go | 127 ------------------------------------ pkg/orm/model_utils_test.go | 62 ++++++++++++++++++ pkg/orm/models_info_m.go | 2 +- pkg/orm/orm_test.go | 48 +++++--------- pkg/orm/types.go | 55 +++++++++++++++- scripts/test.sh | 6 +- 9 files changed, 160 insertions(+), 291 deletions(-) delete mode 100644 log.go delete mode 100644 pkg/log.go create mode 100644 pkg/orm/model_utils_test.go diff --git a/log.go b/log.go deleted file mode 100644 index cc4c0f81..00000000 --- a/log.go +++ /dev/null @@ -1,127 +0,0 @@ -// Copyright 2014 beego Author. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package beego - -import ( - "strings" - - "github.com/astaxie/beego/logs" -) - -// Log levels to control the logging output. -// Deprecated: use github.com/astaxie/beego/logs instead. -const ( - LevelEmergency = iota - LevelAlert - LevelCritical - LevelError - LevelWarning - LevelNotice - LevelInformational - LevelDebug -) - -// BeeLogger references the used application logger. -// Deprecated: use github.com/astaxie/beego/logs instead. -var BeeLogger = logs.GetBeeLogger() - -// SetLevel sets the global log level used by the simple logger. -// Deprecated: use github.com/astaxie/beego/logs instead. -func SetLevel(l int) { - logs.SetLevel(l) -} - -// SetLogFuncCall set the CallDepth, default is 3 -// Deprecated: use github.com/astaxie/beego/logs instead. -func SetLogFuncCall(b bool) { - logs.SetLogFuncCall(b) -} - -// SetLogger sets a new logger. -// Deprecated: use github.com/astaxie/beego/logs instead. -func SetLogger(adaptername string, config string) error { - return logs.SetLogger(adaptername, config) -} - -// Emergency logs a message at emergency level. -// Deprecated: use github.com/astaxie/beego/logs instead. -func Emergency(v ...interface{}) { - logs.Emergency(generateFmtStr(len(v)), v...) -} - -// Alert logs a message at alert level. -// Deprecated: use github.com/astaxie/beego/logs instead. -func Alert(v ...interface{}) { - logs.Alert(generateFmtStr(len(v)), v...) -} - -// Critical logs a message at critical level. -// Deprecated: use github.com/astaxie/beego/logs instead. -func Critical(v ...interface{}) { - logs.Critical(generateFmtStr(len(v)), v...) -} - -// Error logs a message at error level. -// Deprecated: use github.com/astaxie/beego/logs instead. -func Error(v ...interface{}) { - logs.Error(generateFmtStr(len(v)), v...) -} - -// Warning logs a message at warning level. -// Deprecated: use github.com/astaxie/beego/logs instead. -func Warning(v ...interface{}) { - logs.Warning(generateFmtStr(len(v)), v...) -} - -// Warn compatibility alias for Warning() -// Deprecated: use github.com/astaxie/beego/logs instead. -func Warn(v ...interface{}) { - logs.Warn(generateFmtStr(len(v)), v...) -} - -// Notice logs a message at notice level. -// Deprecated: use github.com/astaxie/beego/logs instead. -func Notice(v ...interface{}) { - logs.Notice(generateFmtStr(len(v)), v...) -} - -// Informational logs a message at info level. -// Deprecated: use github.com/astaxie/beego/logs instead. -func Informational(v ...interface{}) { - logs.Informational(generateFmtStr(len(v)), v...) -} - -// Info compatibility alias for Warning() -// Deprecated: use github.com/astaxie/beego/logs instead. -func Info(v ...interface{}) { - logs.Info(generateFmtStr(len(v)), v...) -} - -// Debug logs a message at debug level. -// Deprecated: use github.com/astaxie/beego/logs instead. -func Debug(v ...interface{}) { - logs.Debug(generateFmtStr(len(v)), v...) -} - -// Trace logs a message at trace level. -// compatibility alias for Warning() -// Deprecated: use github.com/astaxie/beego/logs instead. -func Trace(v ...interface{}) { - logs.Trace(generateFmtStr(len(v)), v...) -} - -func generateFmtStr(n int) string { - return strings.Repeat("%v ", n) -} diff --git a/orm/orm.go b/orm/orm.go index 0551b1cd..c7566b9a 100644 --- a/orm/orm.go +++ b/orm/orm.go @@ -124,18 +124,21 @@ func (o *orm) getFieldInfo(mi *modelInfo, name string) *fieldInfo { } // read data to model +// Deprecated: using pkg/orm. We will remove this method in v2.1.0 func (o *orm) Read(md interface{}, cols ...string) error { mi, ind := o.getMiInd(md, true) return o.alias.DbBaser.Read(o.db, mi, ind, o.alias.TZ, cols, false) } // read data to model, like Read(), but use "SELECT FOR UPDATE" form +// Deprecated: using pkg/orm. We will remove this method in v2.1.0 func (o *orm) ReadForUpdate(md interface{}, cols ...string) error { mi, ind := o.getMiInd(md, true) return o.alias.DbBaser.Read(o.db, mi, ind, o.alias.TZ, cols, true) } // Try to read a row from the database, or insert one if it doesn't exist +// Deprecated: using pkg/orm. We will remove this method in v2.1.0 func (o *orm) ReadOrCreate(md interface{}, col1 string, cols ...string) (bool, int64, error) { cols = append([]string{col1}, cols...) mi, ind := o.getMiInd(md, true) @@ -159,6 +162,7 @@ func (o *orm) ReadOrCreate(md interface{}, col1 string, cols ...string) (bool, i } // insert model data to database +// Deprecated: using pkg/orm. We will remove this method in v2.1.0 func (o *orm) Insert(md interface{}) (int64, error) { mi, ind := o.getMiInd(md, true) id, err := o.alias.DbBaser.Insert(o.db, mi, ind, o.alias.TZ) @@ -183,6 +187,7 @@ func (o *orm) setPk(mi *modelInfo, ind reflect.Value, id int64) { } // insert some models to database +// Deprecated: using pkg/orm. We will remove this method in v2.1.0 func (o *orm) InsertMulti(bulk int, mds interface{}) (int64, error) { var cnt int64 @@ -218,6 +223,7 @@ func (o *orm) InsertMulti(bulk int, mds interface{}) (int64, error) { } // InsertOrUpdate data to database +// Deprecated: using pkg/orm. We will remove this method in v2.1.0 func (o *orm) InsertOrUpdate(md interface{}, colConflitAndArgs ...string) (int64, error) { mi, ind := o.getMiInd(md, true) id, err := o.alias.DbBaser.InsertOrUpdate(o.db, mi, ind, o.alias, colConflitAndArgs...) @@ -232,6 +238,7 @@ func (o *orm) InsertOrUpdate(md interface{}, colConflitAndArgs ...string) (int64 // update model to database. // cols set the columns those want to update. +// Deprecated: using pkg/orm. We will remove this method in v2.1.0 func (o *orm) Update(md interface{}, cols ...string) (int64, error) { mi, ind := o.getMiInd(md, true) return o.alias.DbBaser.Update(o.db, mi, ind, o.alias.TZ, cols) @@ -239,6 +246,7 @@ func (o *orm) Update(md interface{}, cols ...string) (int64, error) { // delete model in database // cols shows the delete conditions values read from. default is pk +// Deprecated: using pkg/orm. We will remove this method in v2.1.0 func (o *orm) Delete(md interface{}, cols ...string) (int64, error) { mi, ind := o.getMiInd(md, true) num, err := o.alias.DbBaser.Delete(o.db, mi, ind, o.alias.TZ, cols) @@ -252,6 +260,7 @@ func (o *orm) Delete(md interface{}, cols ...string) (int64, error) { } // create a models to models queryer +// Deprecated: using pkg/orm. We will remove this method in v2.1.0 func (o *orm) QueryM2M(md interface{}, name string) QueryM2Mer { mi, ind := o.getMiInd(md, true) fi := o.getFieldInfo(mi, name) @@ -274,6 +283,7 @@ func (o *orm) QueryM2M(md interface{}, name string) QueryM2Mer { // for _,tag := range post.Tags{...} // // make sure the relation is defined in model struct tags. +// Deprecated: using pkg/orm. We will remove this method in v2.1.0 func (o *orm) LoadRelated(md interface{}, name string, args ...interface{}) (int64, error) { _, fi, ind, qseter := o.queryRelated(md, name) @@ -341,6 +351,7 @@ func (o *orm) LoadRelated(md interface{}, name string, args ...interface{}) (int // qs := orm.QueryRelated(post,"Tag") // qs.All(&[]*Tag{}) // +// Deprecated: using pkg/orm. We will remove this method in v2.1.0 func (o *orm) QueryRelated(md interface{}, name string) QuerySeter { // is this api needed ? _, _, _, qs := o.queryRelated(md, name) @@ -423,6 +434,7 @@ func (o *orm) getRelQs(md interface{}, mi *modelInfo, fi *fieldInfo) *querySet { // return a QuerySeter for table operations. // table name can be string or struct. // e.g. QueryTable("user"), QueryTable(&user{}) or QueryTable((*User)(nil)), +// Deprecated: using pkg/orm. We will remove this method in v2.1.0 func (o *orm) QueryTable(ptrStructOrTableName interface{}) (qs QuerySeter) { var name string if table, ok := ptrStructOrTableName.(string); ok { @@ -443,6 +455,8 @@ func (o *orm) QueryTable(ptrStructOrTableName interface{}) (qs QuerySeter) { } // switch to another registered database driver by given name. +// Deprecated: using pkg/orm. We will remove this method in v2.1.0 +// Using NewOrmUsingDB(name) func (o *orm) Using(name string) error { if o.isTx { panic(fmt.Errorf(" transaction has been start, cannot change db")) @@ -461,10 +475,12 @@ func (o *orm) Using(name string) error { } // begin transaction +// Deprecated: using pkg/orm. We will remove this method in v2.1.0 func (o *orm) Begin() error { return o.BeginTx(context.Background(), nil) } +// Deprecated: using pkg/orm. We will remove this method in v2.1.0 func (o *orm) BeginTx(ctx context.Context, opts *sql.TxOptions) error { if o.isTx { return ErrTxHasBegan @@ -484,6 +500,7 @@ func (o *orm) BeginTx(ctx context.Context, opts *sql.TxOptions) error { } // commit transaction +// Deprecated: using pkg/orm. We will remove this method in v2.1.0 func (o *orm) Commit() error { if !o.isTx { return ErrTxDone @@ -499,6 +516,7 @@ func (o *orm) Commit() error { } // rollback transaction +// Deprecated: using pkg/orm. We will remove this method in v2.1.0 func (o *orm) Rollback() error { if !o.isTx { return ErrTxDone @@ -514,16 +532,19 @@ func (o *orm) Rollback() error { } // return a raw query seter for raw sql string. +// Deprecated: using pkg/orm. We will remove this method in v2.1.0 func (o *orm) Raw(query string, args ...interface{}) RawSeter { return newRawSet(o, query, args) } // return current using database Driver +// Deprecated: using pkg/orm. We will remove this method in v2.1.0 func (o *orm) Driver() Driver { return driver(o.alias.Name) } // return sql.DBStats for current database +// Deprecated: using pkg/orm. We will remove this method in v2.1.0 func (o *orm) DBStats() *sql.DBStats { if o.alias != nil && o.alias.DB != nil { stats := o.alias.DB.DB.Stats() @@ -533,6 +554,7 @@ func (o *orm) DBStats() *sql.DBStats { } // NewOrm create new orm +// Deprecated: using pkg/orm. We will remove this method in v2.1.0 func NewOrm() Ormer { BootStrap() // execute only once @@ -545,6 +567,7 @@ func NewOrm() Ormer { } // NewOrmWithDB create a new ormer object with specify *sql.DB for query +// Deprecated: using pkg/orm. We will remove this method in v2.1.0 func NewOrmWithDB(driverName, aliasName string, db *sql.DB) (Ormer, error) { var al *alias diff --git a/orm/types.go b/orm/types.go index 2fd10774..75af7149 100644 --- a/orm/types.go +++ b/orm/types.go @@ -22,6 +22,7 @@ import ( ) // Driver define database driver + type Driver interface { Name() string Type() DriverType diff --git a/pkg/log.go b/pkg/log.go deleted file mode 100644 index cc4c0f81..00000000 --- a/pkg/log.go +++ /dev/null @@ -1,127 +0,0 @@ -// Copyright 2014 beego Author. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package beego - -import ( - "strings" - - "github.com/astaxie/beego/logs" -) - -// Log levels to control the logging output. -// Deprecated: use github.com/astaxie/beego/logs instead. -const ( - LevelEmergency = iota - LevelAlert - LevelCritical - LevelError - LevelWarning - LevelNotice - LevelInformational - LevelDebug -) - -// BeeLogger references the used application logger. -// Deprecated: use github.com/astaxie/beego/logs instead. -var BeeLogger = logs.GetBeeLogger() - -// SetLevel sets the global log level used by the simple logger. -// Deprecated: use github.com/astaxie/beego/logs instead. -func SetLevel(l int) { - logs.SetLevel(l) -} - -// SetLogFuncCall set the CallDepth, default is 3 -// Deprecated: use github.com/astaxie/beego/logs instead. -func SetLogFuncCall(b bool) { - logs.SetLogFuncCall(b) -} - -// SetLogger sets a new logger. -// Deprecated: use github.com/astaxie/beego/logs instead. -func SetLogger(adaptername string, config string) error { - return logs.SetLogger(adaptername, config) -} - -// Emergency logs a message at emergency level. -// Deprecated: use github.com/astaxie/beego/logs instead. -func Emergency(v ...interface{}) { - logs.Emergency(generateFmtStr(len(v)), v...) -} - -// Alert logs a message at alert level. -// Deprecated: use github.com/astaxie/beego/logs instead. -func Alert(v ...interface{}) { - logs.Alert(generateFmtStr(len(v)), v...) -} - -// Critical logs a message at critical level. -// Deprecated: use github.com/astaxie/beego/logs instead. -func Critical(v ...interface{}) { - logs.Critical(generateFmtStr(len(v)), v...) -} - -// Error logs a message at error level. -// Deprecated: use github.com/astaxie/beego/logs instead. -func Error(v ...interface{}) { - logs.Error(generateFmtStr(len(v)), v...) -} - -// Warning logs a message at warning level. -// Deprecated: use github.com/astaxie/beego/logs instead. -func Warning(v ...interface{}) { - logs.Warning(generateFmtStr(len(v)), v...) -} - -// Warn compatibility alias for Warning() -// Deprecated: use github.com/astaxie/beego/logs instead. -func Warn(v ...interface{}) { - logs.Warn(generateFmtStr(len(v)), v...) -} - -// Notice logs a message at notice level. -// Deprecated: use github.com/astaxie/beego/logs instead. -func Notice(v ...interface{}) { - logs.Notice(generateFmtStr(len(v)), v...) -} - -// Informational logs a message at info level. -// Deprecated: use github.com/astaxie/beego/logs instead. -func Informational(v ...interface{}) { - logs.Informational(generateFmtStr(len(v)), v...) -} - -// Info compatibility alias for Warning() -// Deprecated: use github.com/astaxie/beego/logs instead. -func Info(v ...interface{}) { - logs.Info(generateFmtStr(len(v)), v...) -} - -// Debug logs a message at debug level. -// Deprecated: use github.com/astaxie/beego/logs instead. -func Debug(v ...interface{}) { - logs.Debug(generateFmtStr(len(v)), v...) -} - -// Trace logs a message at trace level. -// compatibility alias for Warning() -// Deprecated: use github.com/astaxie/beego/logs instead. -func Trace(v ...interface{}) { - logs.Trace(generateFmtStr(len(v)), v...) -} - -func generateFmtStr(n int) string { - return strings.Repeat("%v ", n) -} diff --git a/pkg/orm/model_utils_test.go b/pkg/orm/model_utils_test.go new file mode 100644 index 00000000..ea38d90a --- /dev/null +++ b/pkg/orm/model_utils_test.go @@ -0,0 +1,62 @@ +// Copyright 2020 +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package orm + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +type Interface struct { + Id int + Name string + + Index1 string + Index2 string + + Unique1 string + Unique2 string +} + +func (i *Interface) TableIndex() [][]string { + return [][]string{{"index1"}, {"index2"}} +} + +func (i *Interface) TableUnique() [][]string { + return [][]string{{"unique1"}, {"unique2"}} +} + +func (i *Interface) TableName() string { + return "INTERFACE_" +} + +func (i *Interface) TableEngine() string { + return "innodb" +} + +func TestDbBase_GetTables(t *testing.T) { + RegisterModel(&Interface{}) + mi, ok := modelCache.get("INTERFACE_") + assert.True(t, ok) + assert.NotNil(t, mi) + + engine := getTableEngine(mi.addrField) + assert.Equal(t, "innodb", engine) + uniques := getTableUnique(mi.addrField) + assert.Equal(t, [][]string{{"unique1"}, {"unique2"}}, uniques) + indexes := getTableIndex(mi.addrField) + assert.Equal(t, [][]string{{"index1"}, {"index2"}}, indexes) +} diff --git a/pkg/orm/models_info_m.go b/pkg/orm/models_info_m.go index a4d733b6..c450239c 100644 --- a/pkg/orm/models_info_m.go +++ b/pkg/orm/models_info_m.go @@ -29,7 +29,7 @@ type modelInfo struct { model interface{} fields *fields manual bool - addrField reflect.Value //store the original struct value + addrField reflect.Value // store the original struct value uniques []string isThrough bool } diff --git a/pkg/orm/orm_test.go b/pkg/orm/orm_test.go index 54ecc0fd..e3dafecd 100644 --- a/pkg/orm/orm_test.go +++ b/pkg/orm/orm_test.go @@ -297,16 +297,13 @@ func TestDataTypes(t *testing.T) { vu := e.Interface() switch name { case "Date": - vu = vu.(time.Time).In(DefaultTimeLoc).Format(testDate) - value = value.(time.Time).In(DefaultTimeLoc).Format(testDate) case "DateTime": - vu = vu.(time.Time).In(DefaultTimeLoc).Format(testDateTime) - value = value.(time.Time).In(DefaultTimeLoc).Format(testDateTime) case "Time": - vu = vu.(time.Time).In(DefaultTimeLoc).Format(testTime) - value = value.(time.Time).In(DefaultTimeLoc).Format(testTime) + assert.True(t, vu.(time.Time).In(DefaultTimeLoc).Sub(value.(time.Time).In(DefaultTimeLoc)) <= time.Second) + break + default: + assert.Equal(t, value, vu) } - throwFail(t, AssertIs(vu == value, true), value, vu) } } @@ -1662,18 +1659,14 @@ func TestRawQueryRow(t *testing.T) { switch col { case "id": throwFail(t, AssertIs(id, 1)) + break case "time": - v = v.(time.Time).In(DefaultTimeLoc) - value := dataValues[col].(time.Time).In(DefaultTimeLoc) - throwFail(t, AssertIs(v, value, testTime)) case "date": - v = v.(time.Time).In(DefaultTimeLoc) - value := dataValues[col].(time.Time).In(DefaultTimeLoc) - throwFail(t, AssertIs(v, value, testDate)) case "datetime": v = v.(time.Time).In(DefaultTimeLoc) value := dataValues[col].(time.Time).In(DefaultTimeLoc) - throwFail(t, AssertIs(v, value, testDateTime)) + assert.True(t, v.(time.Time).Sub(value) <= time.Second) + break default: throwFail(t, AssertIs(v, dataValues[col])) } @@ -1746,16 +1739,13 @@ func TestQueryRows(t *testing.T) { vu := e.Interface() switch name { case "Time": - vu = vu.(time.Time).In(DefaultTimeLoc).Format(testTime) - value = value.(time.Time).In(DefaultTimeLoc).Format(testTime) case "Date": - vu = vu.(time.Time).In(DefaultTimeLoc).Format(testDate) - value = value.(time.Time).In(DefaultTimeLoc).Format(testDate) case "DateTime": - vu = vu.(time.Time).In(DefaultTimeLoc).Format(testDateTime) - value = value.(time.Time).In(DefaultTimeLoc).Format(testDateTime) + assert.True(t, vu.(time.Time).In(DefaultTimeLoc).Sub(value.(time.Time).In(DefaultTimeLoc)) <= time.Second) + break + default: + assert.Equal(t, value, vu) } - throwFail(t, AssertIs(vu == value, true), value, vu) } var datas2 []Data @@ -1773,16 +1763,14 @@ func TestQueryRows(t *testing.T) { vu := e.Interface() switch name { case "Time": - vu = vu.(time.Time).In(DefaultTimeLoc).Format(testTime) - value = value.(time.Time).In(DefaultTimeLoc).Format(testTime) case "Date": - vu = vu.(time.Time).In(DefaultTimeLoc).Format(testDate) - value = value.(time.Time).In(DefaultTimeLoc).Format(testDate) case "DateTime": - vu = vu.(time.Time).In(DefaultTimeLoc).Format(testDateTime) - value = value.(time.Time).In(DefaultTimeLoc).Format(testDateTime) + assert.True(t, vu.(time.Time).In(DefaultTimeLoc).Sub(value.(time.Time).In(DefaultTimeLoc)) <= time.Second) + break + default: + assert.Equal(t, value, vu) } - throwFail(t, AssertIs(vu == value, true), value, vu) + } var ids []int @@ -2193,8 +2181,8 @@ func TestInLine(t *testing.T) { throwFail(t, AssertIs(il.Name, name)) throwFail(t, AssertIs(il.Email, email)) - throwFail(t, AssertIs(il.Created.In(DefaultTimeLoc), inline.Created.In(DefaultTimeLoc), testDate)) - throwFail(t, AssertIs(il.Updated.In(DefaultTimeLoc), inline.Updated.In(DefaultTimeLoc), testDateTime)) + assert.True(t, il.Created.In(DefaultTimeLoc).Sub(inline.Created.In(DefaultTimeLoc)) <= time.Second) + assert.True(t, il.Updated.In(DefaultTimeLoc).Sub(inline.Updated.In(DefaultTimeLoc)) <= time.Second) } func TestInLineOneToOne(t *testing.T) { diff --git a/pkg/orm/types.go b/pkg/orm/types.go index 8255d93e..cb0f97cc 100644 --- a/pkg/orm/types.go +++ b/pkg/orm/types.go @@ -21,6 +21,58 @@ import ( "time" ) +// TableNaming is usually used by model +// when you custom your table name, please implement this interfaces +// for example: +// type User struct { +// ... +// } +// func (u *User) TableName() string { +// return "USER_TABLE" +// } +type TableNameI interface { + TableName() string +} + +// TableEngineI is usually used by model +// when you want to use specific engine, like myisam, you can implement this interface +// for example: +// type User struct { +// ... +// } +// func (u *User) TableEngine() string { +// return "myisam" +// } +type TableEngineI interface { + TableEngine() string +} + +// TableIndexI is usually used by model +// when you want to create indexes, you can implement this interface +// for example: +// type User struct { +// ... +// } +// func (u *User) TableIndex() [][]string { +// return [][]string{{"Name"}} +// } +type TableIndexI interface { + TableIndex() [][]string +} + +// TableUniqueI is usually used by model +// when you want to create unique indexes, you can implement this interface +// for example: +// type User struct { +// ... +// } +// func (u *User) TableUnique() [][]string { +// return [][]string{{"Email"}} +// } +type TableUniqueI interface { + TableUnique() [][]string +} + // Driver define database driver type Driver interface { Name() string @@ -145,9 +197,6 @@ type DQL interface { QueryTable(ptrStructOrTableName interface{}) QuerySeter QueryTableWithCtx(ctx context.Context, ptrStructOrTableName interface{}) QuerySeter - // switch to another registered database driver by given name. - // Using(name string) error - DBStats() *sql.DBStats } diff --git a/scripts/test.sh b/scripts/test.sh index d626d24b..473a7066 100644 --- a/scripts/test.sh +++ b/scripts/test.sh @@ -1,14 +1,14 @@ #!/bin/bash -docker-compose -f test_docker_compose.yaml up -d +docker-compose -f "$(pwd)/scripts/test_docker_compose.yaml" up -d export ORM_DRIVER=mysql export TZ=UTC export ORM_SOURCE="beego:test@tcp(localhost:13306)/orm_test?charset=utf8" -go test ../... +go test "$(pwd)/..." # clear all container -docker-compose -f test_docker_compose.yaml down +docker-compose -f "$(pwd)/scripts/test_docker_compose.yaml" down From 22b8cae73be75d009151222bc2788139374074d9 Mon Sep 17 00:00:00 2001 From: wangle <285273592@qq.com> Date: Wed, 29 Jul 2020 23:23:02 +0800 Subject: [PATCH 4/4] Add the operator(>,>=,<,<=,=,!=) of orm eg: qs.Filter("counts__>=","20") qs.Filter("counts__!=","20") --- orm/db.go | 6 ++++++ orm/db_mysql.go | 6 ++++++ orm/db_oracle.go | 5 +++++ orm/db_postgres.go | 6 ++++++ orm/db_sqlite.go | 6 ++++++ 5 files changed, 29 insertions(+) diff --git a/orm/db.go b/orm/db.go index 9a1827e8..5d175bf1 100644 --- a/orm/db.go +++ b/orm/db.go @@ -49,6 +49,12 @@ var ( "eq": true, "nq": true, "ne": true, + ">": true, + ">=": true, + "<": true, + "<=": true, + "=": true, + "!=": true, "startswith": true, "endswith": true, "istartswith": true, diff --git a/orm/db_mysql.go b/orm/db_mysql.go index 6e99058e..36f6f566 100644 --- a/orm/db_mysql.go +++ b/orm/db_mysql.go @@ -29,11 +29,17 @@ var mysqlOperators = map[string]string{ // "regex": "REGEXP BINARY ?", // "iregex": "REGEXP ?", "gt": "> ?", + ">": "> ?", "gte": ">= ?", + ">=": ">= ?", "lt": "< ?", + "<": "< ?", "lte": "<= ?", + "<=": "<= ?", "eq": "= ?", + "=": "= ?", "ne": "!= ?", + "!=": "!= ?", "startswith": "LIKE BINARY ?", "endswith": "LIKE BINARY ?", "istartswith": "LIKE ?", diff --git a/orm/db_oracle.go b/orm/db_oracle.go index 5d121f83..ed2ec74c 100644 --- a/orm/db_oracle.go +++ b/orm/db_oracle.go @@ -22,10 +22,15 @@ import ( // oracle operators. var oracleOperators = map[string]string{ "exact": "= ?", + "=": "= ?", "gt": "> ?", + ">": "> ?", "gte": ">= ?", + ">=": ">= ?", "lt": "< ?", + "<": "< ?", "lte": "<= ?", + "<=": "<= ?", "//iendswith": "LIKE ?", } diff --git a/orm/db_postgres.go b/orm/db_postgres.go index c488fb38..7eb88d7a 100644 --- a/orm/db_postgres.go +++ b/orm/db_postgres.go @@ -26,11 +26,17 @@ var postgresOperators = map[string]string{ "contains": "LIKE ?", "icontains": "LIKE UPPER(?)", "gt": "> ?", + ">": "> ?", "gte": ">= ?", + ">=": ">= ?", "lt": "< ?", + "<": "< ?", "lte": "<= ?", + "<=": "<= ?", "eq": "= ?", + "=": "= ?", "ne": "!= ?", + "!=": "!= ?", "startswith": "LIKE ?", "endswith": "LIKE ?", "istartswith": "LIKE UPPER(?)", diff --git a/orm/db_sqlite.go b/orm/db_sqlite.go index 1d62ee34..bd9f5d3b 100644 --- a/orm/db_sqlite.go +++ b/orm/db_sqlite.go @@ -28,11 +28,17 @@ var sqliteOperators = map[string]string{ "contains": "LIKE ? ESCAPE '\\'", "icontains": "LIKE ? ESCAPE '\\'", "gt": "> ?", + ">": "> ?", "gte": ">= ?", + ">=": ">= ?", "lt": "< ?", + "<": "< ?", "lte": "<= ?", + "<=": "<= ?", "eq": "= ?", + "=": "= ?", "ne": "!= ?", + "!=": "!= ?", "startswith": "LIKE ? ESCAPE '\\'", "endswith": "LIKE ? ESCAPE '\\'", "istartswith": "LIKE ? ESCAPE '\\'",