fix 5530: use orm.DebugLog (#5532)

* fix 5500: querySet should not be changed when constructing SQL

* fix 5530: use orm.DebugLog
This commit is contained in:
Ming Deng 2023-11-27 23:43:59 +08:00 committed by GitHub
parent e4b67e86ee
commit 95a8a61d2b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 23 additions and 45 deletions

View File

@ -25,8 +25,6 @@ import (
"github.com/beego/beego/v2/client/orm/internal/buffers" "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/utils"
"github.com/beego/beego/v2/client/orm/internal/models" "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() lastInsertId, err := res.LastInsertId()
if err != nil { if err != nil {
logs.DebugLog.Println(ErrLastInsertIdUnavailable, ':', err) DebugLog.Println(ErrLastInsertIdUnavailable, ':', err)
return lastInsertId, ErrLastInsertIdUnavailable return lastInsertId, ErrLastInsertIdUnavailable
} else { } else {
return lastInsertId, nil return lastInsertId, nil
@ -547,7 +545,7 @@ func (d *dbBase) InsertOrUpdate(ctx context.Context, q dbQuerier, mi *models.Mod
if err == nil { if err == nil {
lastInsertId, err := res.LastInsertId() lastInsertId, err := res.LastInsertId()
if err != nil { if err != nil {
logs.DebugLog.Println(ErrLastInsertIdUnavailable, ':', err) DebugLog.Println(ErrLastInsertIdUnavailable, ':', err)
return lastInsertId, ErrLastInsertIdUnavailable return lastInsertId, ErrLastInsertIdUnavailable
} else { } else {
return lastInsertId, nil return lastInsertId, nil
@ -2166,7 +2164,7 @@ func (d *dbBase) GenerateSpecifyIndex(tableName string, useIndex int, indexes []
case hints.KeyIgnoreIndex: case hints.KeyIgnoreIndex:
useWay = `IGNORE` useWay = `IGNORE`
default: 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 `` return ``
} }

View File

@ -21,8 +21,6 @@ import (
"sync" "sync"
"time" "time"
"github.com/beego/beego/v2/client/orm/internal/logs"
lru "github.com/hashicorp/golang-lru" lru "github.com/hashicorp/golang-lru"
) )
@ -322,7 +320,7 @@ func detectTZ(al *alias) {
al.TZ = t.Location() al.TZ = t.Location()
} }
} else { } 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 { if err == nil {
al.TZ = loc al.TZ = loc
} else { } 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 { if db != nil {
db.Close() db.Close()
} }
logs.DebugLog.Println(err.Error()) DebugLog.Println(err.Error())
} }
return err return err

View File

@ -20,8 +20,6 @@ import (
"reflect" "reflect"
"strings" "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/internal/models"
) )
@ -163,7 +161,7 @@ func (d *dbBaseMysql) InsertOrUpdate(ctx context.Context, q dbQuerier, mi *model
if err == nil { if err == nil {
lastInsertId, err := res.LastInsertId() lastInsertId, err := res.LastInsertId()
if err != nil { if err != nil {
logs.DebugLog.Println(ErrLastInsertIdUnavailable, ':', err) DebugLog.Println(ErrLastInsertIdUnavailable, ':', err)
return lastInsertId, ErrLastInsertIdUnavailable return lastInsertId, ErrLastInsertIdUnavailable
} else { } else {
return lastInsertId, nil return lastInsertId, nil

View File

@ -19,8 +19,6 @@ import (
"fmt" "fmt"
"strings" "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/internal/models"
"github.com/beego/beego/v2/client/orm/hints" "github.com/beego/beego/v2/client/orm/hints"
@ -120,7 +118,7 @@ func (d *dbBaseOracle) GenerateSpecifyIndex(tableName string, useIndex int, inde
case hints.KeyIgnoreIndex: case hints.KeyIgnoreIndex:
hint = `NO_INDEX` hint = `NO_INDEX`
default: 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 `` return ``
} }
@ -160,7 +158,7 @@ func (d *dbBaseOracle) InsertValue(ctx context.Context, q dbQuerier, mi *models.
lastInsertId, err := res.LastInsertId() lastInsertId, err := res.LastInsertId()
if err != nil { if err != nil {
logs.DebugLog.Println(ErrLastInsertIdUnavailable, ':', err) DebugLog.Println(ErrLastInsertIdUnavailable, ':', err)
return lastInsertId, ErrLastInsertIdUnavailable return lastInsertId, ErrLastInsertIdUnavailable
} else { } else {
return lastInsertId, nil return lastInsertId, nil

View File

@ -19,8 +19,6 @@ import (
"fmt" "fmt"
"strconv" "strconv"
"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/internal/models"
) )
@ -189,7 +187,7 @@ func (d *dbBasePostgres) IndexExists(ctx context.Context, db dbQuerier, table st
// GenerateSpecifyIndex return a specifying index clause // GenerateSpecifyIndex return a specifying index clause
func (d *dbBasePostgres) GenerateSpecifyIndex(tableName string, useIndex int, indexes []string) string { 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 `` return ``
} }

View File

@ -22,8 +22,6 @@ import (
"strings" "strings"
"time" "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/internal/models"
"github.com/beego/beego/v2/client/orm/hints" "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 // 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 { 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 { 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) 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: case hints.KeyUseIndex, hints.KeyForceIndex:
return fmt.Sprintf(` INDEXED BY %s `, strings.Join(s, `,`)) return fmt.Sprintf(` INDEXED BY %s `, strings.Join(s, `,`))
default: 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 `` return ``
} }
} }

View File

@ -16,12 +16,14 @@ package orm
import ( import (
"errors" "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" "testing"
"time" "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" "github.com/beego/beego/v2/client/orm/internal/models"
) )

View File

@ -3,11 +3,8 @@ package logs
import ( import (
"io" "io"
"log" "log"
"os"
) )
var DebugLog = NewLog(os.Stdout)
// Log implement the log.Logger // Log implement the log.Logger
type Log struct { type Log struct {
*log.Logger *log.Logger

View File

@ -20,8 +20,6 @@ import (
"reflect" "reflect"
"strings" "strings"
"time" "time"
"github.com/beego/beego/v2/client/orm/internal/logs"
) )
// 1 is attr // 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] v = v[i+1 : len(v)-1]
tags[name] = v tags[name] = v
} }
} else {
logs.DebugLog.Println("unsupport orm tag", v)
} }
} }
return return

View File

@ -54,9 +54,9 @@ import (
"database/sql" "database/sql"
"errors" "errors"
"fmt" "fmt"
"os"
"reflect" "reflect"
ilogs "github.com/beego/beego/v2/client/orm/internal/logs"
iutils "github.com/beego/beego/v2/client/orm/internal/utils" iutils "github.com/beego/beego/v2/client/orm/internal/utils"
"github.com/beego/beego/v2/client/orm/internal/models" "github.com/beego/beego/v2/client/orm/internal/models"
@ -75,7 +75,7 @@ const (
// Define common vars // Define common vars
var ( var (
Debug = false Debug = false
DebugLog = ilogs.DebugLog DebugLog = NewLog(os.Stdout)
DefaultRowsLimit = -1 DefaultRowsLimit = -1
DefaultRelsDepth = 2 DefaultRelsDepth = 2
DefaultTimeLoc = iutils.DefaultTimeLoc DefaultTimeLoc = iutils.DefaultTimeLoc

View File

@ -19,7 +19,6 @@ import (
"database/sql" "database/sql"
"fmt" "fmt"
"io" "io"
"log"
"strings" "strings"
"time" "time"
@ -30,9 +29,7 @@ type Log = logs.Log
// NewLog Set io.Writer to create a Logger. // NewLog Set io.Writer to create a Logger.
func NewLog(out io.Writer) *logs.Log { func NewLog(out io.Writer) *logs.Log {
d := new(logs.Log) return logs.NewLog(out)
d.Logger = log.New(out, "[ORM]", log.LstdFlags)
return d
} }
// LogFunc costomer log func // LogFunc costomer log func
@ -63,7 +60,7 @@ func debugLogQueies(alias *alias, operaton, query string, t time.Time, err error
if LogFunc != nil { if LogFunc != nil {
LogFunc(logMap) LogFunc(logMap)
} }
logs.DebugLog.Println(con) DebugLog.Println(con)
} }
// statement query logger struct. // statement query logger struct.

View File

@ -28,8 +28,6 @@ import (
"testing" "testing"
"time" "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/utils"
"github.com/beego/beego/v2/client/orm/internal/models" "github.com/beego/beego/v2/client/orm/internal/models"
@ -2994,9 +2992,9 @@ func TestDebugLog(t *testing.T) {
func captureDebugLogOutput(f func()) string { func captureDebugLogOutput(f func()) string {
var buf bytes.Buffer var buf bytes.Buffer
logs.DebugLog.SetOutput(&buf) DebugLog.SetOutput(&buf)
defer func() { defer func() {
logs.DebugLog.SetOutput(os.Stderr) DebugLog.SetOutput(os.Stderr)
}() }()
f() f()
return buf.String() return buf.String()