forked from mirror/gorm
Add ParameterizedQueries option support for logger, close #5288
This commit is contained in:
parent
794edad60e
commit
ddd3cc2502
|
@ -132,7 +132,11 @@ func (p *processor) Execute(db *DB) *DB {
|
||||||
|
|
||||||
if stmt.SQL.Len() > 0 {
|
if stmt.SQL.Len() > 0 {
|
||||||
db.Logger.Trace(stmt.Context, curTime, func() (string, int64) {
|
db.Logger.Trace(stmt.Context, curTime, func() (string, int64) {
|
||||||
return db.Dialector.Explain(stmt.SQL.String(), stmt.Vars...), db.RowsAffected
|
sql, vars := stmt.SQL.String(), stmt.Vars
|
||||||
|
if filter, ok := db.Logger.(ParamsFilter); ok {
|
||||||
|
sql, vars = filter.ParamsFilter(stmt.Context, stmt.SQL.String(), stmt.Vars...)
|
||||||
|
}
|
||||||
|
return db.Dialector.Explain(sql, vars...), db.RowsAffected
|
||||||
}, db.Error)
|
}, db.Error)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
12
gorm.go
12
gorm.go
|
@ -464,12 +464,12 @@ func (db *DB) Use(plugin Plugin) error {
|
||||||
|
|
||||||
// ToSQL for generate SQL string.
|
// ToSQL for generate SQL string.
|
||||||
//
|
//
|
||||||
// db.ToSQL(func(tx *gorm.DB) *gorm.DB {
|
// db.ToSQL(func(tx *gorm.DB) *gorm.DB {
|
||||||
// return tx.Model(&User{}).Where(&User{Name: "foo", Age: 20})
|
// return tx.Model(&User{}).Where(&User{Name: "foo", Age: 20})
|
||||||
// .Limit(10).Offset(5)
|
// .Limit(10).Offset(5)
|
||||||
// .Order("name ASC")
|
// .Order("name ASC")
|
||||||
// .First(&User{})
|
// .First(&User{})
|
||||||
// })
|
// })
|
||||||
func (db *DB) ToSQL(queryFn func(tx *DB) *DB) string {
|
func (db *DB) ToSQL(queryFn func(tx *DB) *DB) string {
|
||||||
tx := queryFn(db.Session(&Session{DryRun: true, SkipDefaultTransaction: true}))
|
tx := queryFn(db.Session(&Session{DryRun: true, SkipDefaultTransaction: true}))
|
||||||
stmt := tx.Statement
|
stmt := tx.Statement
|
||||||
|
|
|
@ -26,6 +26,10 @@ type Plugin interface {
|
||||||
Initialize(*DB) error
|
Initialize(*DB) error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ParamsFilter interface {
|
||||||
|
ParamsFilter(ctx context.Context, sql string, params ...interface{}) (string, []interface{})
|
||||||
|
}
|
||||||
|
|
||||||
// ConnPool db conns pool interface
|
// ConnPool db conns pool interface
|
||||||
type ConnPool interface {
|
type ConnPool interface {
|
||||||
PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)
|
PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)
|
||||||
|
|
|
@ -55,6 +55,7 @@ type Config struct {
|
||||||
SlowThreshold time.Duration
|
SlowThreshold time.Duration
|
||||||
Colorful bool
|
Colorful bool
|
||||||
IgnoreRecordNotFoundError bool
|
IgnoreRecordNotFoundError bool
|
||||||
|
ParameterizedQueries bool
|
||||||
LogLevel LogLevel
|
LogLevel LogLevel
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -75,6 +76,7 @@ var (
|
||||||
SlowThreshold: 200 * time.Millisecond,
|
SlowThreshold: 200 * time.Millisecond,
|
||||||
LogLevel: Warn,
|
LogLevel: Warn,
|
||||||
IgnoreRecordNotFoundError: false,
|
IgnoreRecordNotFoundError: false,
|
||||||
|
ParameterizedQueries: true,
|
||||||
Colorful: true,
|
Colorful: true,
|
||||||
})
|
})
|
||||||
// Recorder Recorder logger records running SQL into a recorder instance
|
// Recorder Recorder logger records running SQL into a recorder instance
|
||||||
|
@ -181,6 +183,14 @@ func (l logger) Trace(ctx context.Context, begin time.Time, fc func() (string, i
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Trace print sql message
|
||||||
|
func (l logger) ParamsFilter(ctx context.Context, sql string, params ...interface{}) (string, []interface{}) {
|
||||||
|
if l.Config.ParameterizedQueries {
|
||||||
|
return sql, nil
|
||||||
|
}
|
||||||
|
return sql, params
|
||||||
|
}
|
||||||
|
|
||||||
type traceRecorder struct {
|
type traceRecorder struct {
|
||||||
Interface
|
Interface
|
||||||
BeginAt time.Time
|
BeginAt time.Time
|
||||||
|
|
|
@ -3,11 +3,14 @@ module gorm.io/gorm/tests
|
||||||
go 1.16
|
go 1.16
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
github.com/go-sql-driver/mysql v1.7.0 // indirect
|
||||||
github.com/google/uuid v1.3.0
|
github.com/google/uuid v1.3.0
|
||||||
|
github.com/jackc/pgtype v1.13.0 // indirect
|
||||||
github.com/jinzhu/now v1.1.5
|
github.com/jinzhu/now v1.1.5
|
||||||
github.com/lib/pq v1.10.7
|
github.com/lib/pq v1.10.7
|
||||||
github.com/mattn/go-sqlite3 v1.14.16 // indirect
|
github.com/mattn/go-sqlite3 v1.14.16 // indirect
|
||||||
golang.org/x/crypto v0.3.0 // indirect
|
github.com/microsoft/go-mssqldb v0.19.0 // indirect
|
||||||
|
golang.org/x/crypto v0.4.0 // indirect
|
||||||
gorm.io/driver/mysql v1.4.4
|
gorm.io/driver/mysql v1.4.4
|
||||||
gorm.io/driver/postgres v1.4.5
|
gorm.io/driver/postgres v1.4.5
|
||||||
gorm.io/driver/sqlite v1.4.3
|
gorm.io/driver/sqlite v1.4.3
|
||||||
|
|
Loading…
Reference in New Issue