From 9dd6ed9c65bcf95e4a4298bcdf1f26670778ba76 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Tue, 29 Mar 2022 18:14:29 +0800 Subject: [PATCH] Scan with Rows interface --- interfaces.go | 10 ++++++++++ scan.go | 4 ++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/interfaces.go b/interfaces.go index 84dc94bb..32d49605 100644 --- a/interfaces.go +++ b/interfaces.go @@ -72,3 +72,13 @@ type Valuer interface { type GetDBConnector interface { GetDBConn() (*sql.DB, error) } + +// Rows rows interface +type Rows interface { + Columns() ([]string, error) + ColumnTypes() ([]*sql.ColumnType, error) + Next() bool + Scan(dest ...interface{}) error + Err() error + Close() error +} diff --git a/scan.go b/scan.go index 42642ec6..c8da13da 100644 --- a/scan.go +++ b/scan.go @@ -50,7 +50,7 @@ func scanIntoMap(mapValue map[string]interface{}, values []interface{}, columns } } -func (db *DB) scanIntoStruct(rows *sql.Rows, reflectValue reflect.Value, values []interface{}, fields []*schema.Field, joinFields [][2]*schema.Field) { +func (db *DB) scanIntoStruct(rows Rows, reflectValue reflect.Value, values []interface{}, fields []*schema.Field, joinFields [][2]*schema.Field) { for idx, field := range fields { if field != nil { values[idx] = field.NewValuePool.Get() @@ -99,7 +99,7 @@ const ( ) // Scan scan rows into db statement -func Scan(rows *sql.Rows, db *DB, mode ScanMode) { +func Scan(rows Rows, db *DB, mode ScanMode) { var ( columns, _ = rows.Columns() values = make([]interface{}, len(columns))