github.com/acoshift/pgsql@v0.15.3/scan.go (about)

     1  package pgsql
     2  
     3  import (
     4  	"database/sql"
     5  	"reflect"
     6  	"time"
     7  
     8  	"github.com/lib/pq"
     9  )
    10  
    11  // Scan wraps scanner with custom scanner
    12  func Scan(scan Scanner) Scanner {
    13  	return func(dest ...any) error {
    14  		for i, d := range dest {
    15  			// skip known type
    16  			switch d.(type) {
    17  			case sql.Scanner,
    18  				*time.Time,
    19  				*[]byte,
    20  				*int, *int8, *int16, *int32, *int64,
    21  				*uint, *uint8, *uint16, *uint32, *uint64,
    22  				*bool,
    23  				*float32, *float64,
    24  				*sql.RawBytes,
    25  				*sql.Rows:
    26  				continue
    27  			}
    28  
    29  			dt := reflect.TypeOf(d)
    30  			if dt.Kind() == reflect.Ptr {
    31  				dt = dt.Elem()
    32  			}
    33  			switch dt.Kind() {
    34  			case reflect.Slice:
    35  				dest[i] = pq.Array(d)
    36  			case reflect.Struct:
    37  				dest[i] = JSON(d)
    38  			}
    39  		}
    40  		return scan(dest...)
    41  	}
    42  }
    43  
    44  type Row struct {
    45  	*sql.Row
    46  }
    47  
    48  func (r *Row) Scan(dest ...any) error {
    49  	return Scan(r.Row.Scan)(dest...)
    50  }
    51  
    52  type Rows struct {
    53  	*sql.Rows
    54  }
    55  
    56  func (r *Rows) Scan(dest ...any) error {
    57  	return Scan(r.Rows.Scan)(dest...)
    58  }