github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/depends/kit/sqlx/scanner/scan.go (about)

     1  package scanner
     2  
     3  import (
     4  	"context"
     5  	"database/sql"
     6  
     7  	"github.com/pkg/errors"
     8  )
     9  
    10  func Scan(ctx context.Context, rows *sql.Rows, v interface{}) error {
    11  	if rows == nil {
    12  		return nil
    13  	}
    14  	defer rows.Close()
    15  
    16  	iter, err := ScanIteratorFor(v)
    17  	if err != nil {
    18  		return err
    19  	}
    20  
    21  	for rows.Next() {
    22  		item := iter.New()
    23  		if err := ScanRows(ctx, rows, item); err != nil {
    24  			return err
    25  		}
    26  		if err := iter.Next(item); err != nil {
    27  			return err
    28  		}
    29  	}
    30  
    31  	if hasRecord, ok := iter.(interface{ HasRecord() bool }); ok {
    32  		if !hasRecord.HasRecord() {
    33  			return RecordNotFound
    34  		}
    35  	}
    36  	if err := rows.Err(); err != nil {
    37  		return err
    38  	}
    39  	if err := rows.Close(); err != nil {
    40  		return err
    41  	}
    42  	return nil
    43  }
    44  
    45  var RecordNotFound = errors.Errorf("record is not found")