refactor: fix signiture

This commit is contained in:
warren 2024-09-01 22:38:16 +09:00
parent 0daaf1747c
commit 6728918242

View File

@ -1,6 +1,7 @@
package gorm package gorm
import ( import (
"context"
"database/sql" "database/sql"
"errors" "errors"
"fmt" "fmt"
@ -522,7 +523,7 @@ func (db *DB) Rows() (*sql.Rows, error) {
} }
// Scan scans selected value to the struct dest // Scan scans selected value to the struct dest
func (db *DB) Scan(dest interface{}) (tx *DB) { func (db *DB) Scan(ctx context.Context, dest interface{}) (tx *DB) {
config := *db.Config config := *db.Config
currentLogger, newLogger := config.Logger, logger.Recorder.New() currentLogger, newLogger := config.Logger, logger.Recorder.New()
config.Logger = newLogger config.Logger = newLogger
@ -530,6 +531,10 @@ func (db *DB) Scan(dest interface{}) (tx *DB) {
tx = db.getInstance() tx = db.getInstance()
tx.Config = &config tx.Config = &config
if ctx != nil {
tx = tx.WithContext(ctx)
}
if rows, err := tx.Rows(); err == nil { if rows, err := tx.Rows(); err == nil {
if rows.Next() { if rows.Next() {
tx.ScanRows(rows, dest) tx.ScanRows(rows, dest)
@ -544,7 +549,7 @@ func (db *DB) Scan(dest interface{}) (tx *DB) {
return newLogger.SQL, tx.RowsAffected return newLogger.SQL, tx.RowsAffected
}, tx.Error) }, tx.Error)
tx.Logger = currentLogger tx.Logger = currentLogger
return return tx
} }
// Pluck queries a single column from a model, returning in the slice dest. E.g.: // Pluck queries a single column from a model, returning in the slice dest. E.g.: