From 95a8a61d2b8b05bade28f3da3130645c741c23ff Mon Sep 17 00:00:00 2001 From: Ming Deng Date: Mon, 27 Nov 2023 23:43:59 +0800 Subject: [PATCH] fix 5530: use orm.DebugLog (#5532) * fix 5500: querySet should not be changed when constructing SQL * fix 5530: use orm.DebugLog --- client/orm/db.go | 8 +++----- client/orm/db_alias.go | 8 +++----- client/orm/db_mysql.go | 4 +--- client/orm/db_oracle.go | 6 ++---- client/orm/db_postgres.go | 4 +--- client/orm/db_sqlite.go | 6 ++---- client/orm/db_test.go | 8 +++++--- client/orm/internal/logs/log.go | 3 --- client/orm/internal/models/models_utils.go | 4 ---- client/orm/orm.go | 4 ++-- client/orm/orm_log.go | 7 ++----- client/orm/orm_test.go | 6 ++---- 12 files changed, 23 insertions(+), 45 deletions(-) diff --git a/client/orm/db.go b/client/orm/db.go index d7ea52f6..48cc2b37 100644 --- a/client/orm/db.go +++ b/client/orm/db.go @@ -25,8 +25,6 @@ import ( "github.com/beego/beego/v2/client/orm/internal/buffers" - "github.com/beego/beego/v2/client/orm/internal/logs" - "github.com/beego/beego/v2/client/orm/internal/utils" "github.com/beego/beego/v2/client/orm/internal/models" @@ -463,7 +461,7 @@ func (d *dbBase) InsertValue(ctx context.Context, q dbQuerier, mi *models.ModelI lastInsertId, err := res.LastInsertId() if err != nil { - logs.DebugLog.Println(ErrLastInsertIdUnavailable, ':', err) + DebugLog.Println(ErrLastInsertIdUnavailable, ':', err) return lastInsertId, ErrLastInsertIdUnavailable } else { return lastInsertId, nil @@ -547,7 +545,7 @@ func (d *dbBase) InsertOrUpdate(ctx context.Context, q dbQuerier, mi *models.Mod if err == nil { lastInsertId, err := res.LastInsertId() if err != nil { - logs.DebugLog.Println(ErrLastInsertIdUnavailable, ':', err) + DebugLog.Println(ErrLastInsertIdUnavailable, ':', err) return lastInsertId, ErrLastInsertIdUnavailable } else { return lastInsertId, nil @@ -2166,7 +2164,7 @@ func (d *dbBase) GenerateSpecifyIndex(tableName string, useIndex int, indexes [] case hints.KeyIgnoreIndex: useWay = `IGNORE` default: - logs.DebugLog.Println("[WARN] Not a valid specifying action, so that action is ignored") + DebugLog.Println("[WARN] Not a valid specifying action, so that action is ignored") return `` } diff --git a/client/orm/db_alias.go b/client/orm/db_alias.go index d7874166..beabc267 100644 --- a/client/orm/db_alias.go +++ b/client/orm/db_alias.go @@ -21,8 +21,6 @@ import ( "sync" "time" - "github.com/beego/beego/v2/client/orm/internal/logs" - lru "github.com/hashicorp/golang-lru" ) @@ -322,7 +320,7 @@ func detectTZ(al *alias) { al.TZ = t.Location() } } else { - logs.DebugLog.Printf("Detect DB timezone: %s %s\n", tz, err.Error()) + DebugLog.Printf("Detect DB timezone: %s %s\n", tz, err.Error()) } } @@ -349,7 +347,7 @@ func detectTZ(al *alias) { if err == nil { al.TZ = loc } else { - logs.DebugLog.Printf("Detect DB timezone: %s %s\n", tz, err.Error()) + DebugLog.Printf("Detect DB timezone: %s %s\n", tz, err.Error()) } } } @@ -481,7 +479,7 @@ end: if db != nil { db.Close() } - logs.DebugLog.Println(err.Error()) + DebugLog.Println(err.Error()) } return err diff --git a/client/orm/db_mysql.go b/client/orm/db_mysql.go index e253f92a..add87502 100644 --- a/client/orm/db_mysql.go +++ b/client/orm/db_mysql.go @@ -20,8 +20,6 @@ import ( "reflect" "strings" - "github.com/beego/beego/v2/client/orm/internal/logs" - "github.com/beego/beego/v2/client/orm/internal/models" ) @@ -163,7 +161,7 @@ func (d *dbBaseMysql) InsertOrUpdate(ctx context.Context, q dbQuerier, mi *model if err == nil { lastInsertId, err := res.LastInsertId() if err != nil { - logs.DebugLog.Println(ErrLastInsertIdUnavailable, ':', err) + DebugLog.Println(ErrLastInsertIdUnavailable, ':', err) return lastInsertId, ErrLastInsertIdUnavailable } else { return lastInsertId, nil diff --git a/client/orm/db_oracle.go b/client/orm/db_oracle.go index 247959df..8776f23a 100644 --- a/client/orm/db_oracle.go +++ b/client/orm/db_oracle.go @@ -19,8 +19,6 @@ import ( "fmt" "strings" - "github.com/beego/beego/v2/client/orm/internal/logs" - "github.com/beego/beego/v2/client/orm/internal/models" "github.com/beego/beego/v2/client/orm/hints" @@ -120,7 +118,7 @@ func (d *dbBaseOracle) GenerateSpecifyIndex(tableName string, useIndex int, inde case hints.KeyIgnoreIndex: hint = `NO_INDEX` default: - logs.DebugLog.Println("[WARN] Not a valid specifying action, so that action is ignored") + DebugLog.Println("[WARN] Not a valid specifying action, so that action is ignored") return `` } @@ -160,7 +158,7 @@ func (d *dbBaseOracle) InsertValue(ctx context.Context, q dbQuerier, mi *models. lastInsertId, err := res.LastInsertId() if err != nil { - logs.DebugLog.Println(ErrLastInsertIdUnavailable, ':', err) + DebugLog.Println(ErrLastInsertIdUnavailable, ':', err) return lastInsertId, ErrLastInsertIdUnavailable } else { return lastInsertId, nil diff --git a/client/orm/db_postgres.go b/client/orm/db_postgres.go index e7fa6aea..e16b87ba 100644 --- a/client/orm/db_postgres.go +++ b/client/orm/db_postgres.go @@ -19,8 +19,6 @@ import ( "fmt" "strconv" - "github.com/beego/beego/v2/client/orm/internal/logs" - "github.com/beego/beego/v2/client/orm/internal/models" ) @@ -189,7 +187,7 @@ func (d *dbBasePostgres) IndexExists(ctx context.Context, db dbQuerier, table st // GenerateSpecifyIndex return a specifying index clause func (d *dbBasePostgres) GenerateSpecifyIndex(tableName string, useIndex int, indexes []string) string { - logs.DebugLog.Println("[WARN] Not support any specifying index action, so that action is ignored") + DebugLog.Println("[WARN] Not support any specifying index action, so that action is ignored") return `` } diff --git a/client/orm/db_sqlite.go b/client/orm/db_sqlite.go index 0e84d4df..42282592 100644 --- a/client/orm/db_sqlite.go +++ b/client/orm/db_sqlite.go @@ -22,8 +22,6 @@ import ( "strings" "time" - "github.com/beego/beego/v2/client/orm/internal/logs" - "github.com/beego/beego/v2/client/orm/internal/models" "github.com/beego/beego/v2/client/orm/hints" @@ -80,7 +78,7 @@ var _ dbBaser = new(dbBaseSqlite) // override base db read for update behavior as SQlite does not support syntax func (d *dbBaseSqlite) Read(ctx context.Context, q dbQuerier, mi *models.ModelInfo, ind reflect.Value, tz *time.Location, cols []string, isForUpdate bool) error { if isForUpdate { - logs.DebugLog.Println("[WARN] SQLite does not support SELECT FOR UPDATE query, isForUpdate param is ignored and always as false to do the work") + DebugLog.Println("[WARN] SQLite does not support SELECT FOR UPDATE query, isForUpdate param is ignored and always as false to do the work") } return d.dbBase.Read(ctx, q, mi, ind, tz, cols, false) } @@ -175,7 +173,7 @@ func (d *dbBaseSqlite) GenerateSpecifyIndex(tableName string, useIndex int, inde case hints.KeyUseIndex, hints.KeyForceIndex: return fmt.Sprintf(` INDEXED BY %s `, strings.Join(s, `,`)) default: - logs.DebugLog.Println("[WARN] Not a valid specifying action, so that action is ignored") + DebugLog.Println("[WARN] Not a valid specifying action, so that action is ignored") return `` } } diff --git a/client/orm/db_test.go b/client/orm/db_test.go index d8ceca39..1157bc07 100644 --- a/client/orm/db_test.go +++ b/client/orm/db_test.go @@ -16,12 +16,14 @@ package orm import ( "errors" - "github.com/beego/beego/v2/client/orm/clauses/order_clause" - "github.com/beego/beego/v2/client/orm/internal/buffers" - "github.com/stretchr/testify/assert" "testing" "time" + "github.com/stretchr/testify/assert" + + "github.com/beego/beego/v2/client/orm/clauses/order_clause" + "github.com/beego/beego/v2/client/orm/internal/buffers" + "github.com/beego/beego/v2/client/orm/internal/models" ) diff --git a/client/orm/internal/logs/log.go b/client/orm/internal/logs/log.go index 3ddde3ad..bbee9b70 100644 --- a/client/orm/internal/logs/log.go +++ b/client/orm/internal/logs/log.go @@ -3,11 +3,8 @@ package logs import ( "io" "log" - "os" ) -var DebugLog = NewLog(os.Stdout) - // Log implement the log.Logger type Log struct { *log.Logger diff --git a/client/orm/internal/models/models_utils.go b/client/orm/internal/models/models_utils.go index 9e950abb..3a96d232 100644 --- a/client/orm/internal/models/models_utils.go +++ b/client/orm/internal/models/models_utils.go @@ -20,8 +20,6 @@ import ( "reflect" "strings" "time" - - "github.com/beego/beego/v2/client/orm/internal/logs" ) // 1 is attr @@ -252,8 +250,6 @@ func ParseStructTag(data string) (attrs map[string]bool, tags map[string]string) v = v[i+1 : len(v)-1] tags[name] = v } - } else { - logs.DebugLog.Println("unsupport orm tag", v) } } return diff --git a/client/orm/orm.go b/client/orm/orm.go index 2f91eab6..5b1a27d0 100644 --- a/client/orm/orm.go +++ b/client/orm/orm.go @@ -54,9 +54,9 @@ import ( "database/sql" "errors" "fmt" + "os" "reflect" - ilogs "github.com/beego/beego/v2/client/orm/internal/logs" iutils "github.com/beego/beego/v2/client/orm/internal/utils" "github.com/beego/beego/v2/client/orm/internal/models" @@ -75,7 +75,7 @@ const ( // Define common vars var ( Debug = false - DebugLog = ilogs.DebugLog + DebugLog = NewLog(os.Stdout) DefaultRowsLimit = -1 DefaultRelsDepth = 2 DefaultTimeLoc = iutils.DefaultTimeLoc diff --git a/client/orm/orm_log.go b/client/orm/orm_log.go index b1476b7b..da1e26cf 100644 --- a/client/orm/orm_log.go +++ b/client/orm/orm_log.go @@ -19,7 +19,6 @@ import ( "database/sql" "fmt" "io" - "log" "strings" "time" @@ -30,9 +29,7 @@ type Log = logs.Log // NewLog Set io.Writer to create a Logger. func NewLog(out io.Writer) *logs.Log { - d := new(logs.Log) - d.Logger = log.New(out, "[ORM]", log.LstdFlags) - return d + return logs.NewLog(out) } // LogFunc costomer log func @@ -63,7 +60,7 @@ func debugLogQueies(alias *alias, operaton, query string, t time.Time, err error if LogFunc != nil { LogFunc(logMap) } - logs.DebugLog.Println(con) + DebugLog.Println(con) } // statement query logger struct. diff --git a/client/orm/orm_test.go b/client/orm/orm_test.go index a371f193..0bcc86cc 100644 --- a/client/orm/orm_test.go +++ b/client/orm/orm_test.go @@ -28,8 +28,6 @@ import ( "testing" "time" - "github.com/beego/beego/v2/client/orm/internal/logs" - "github.com/beego/beego/v2/client/orm/internal/utils" "github.com/beego/beego/v2/client/orm/internal/models" @@ -2994,9 +2992,9 @@ func TestDebugLog(t *testing.T) { func captureDebugLogOutput(f func()) string { var buf bytes.Buffer - logs.DebugLog.SetOutput(&buf) + DebugLog.SetOutput(&buf) defer func() { - logs.DebugLog.SetOutput(os.Stderr) + DebugLog.SetOutput(os.Stderr) }() f() return buf.String()