mirror of https://github.com/jackc/pgx.git
parent
4b843c0a26
commit
94052ea940
8
query.go
8
query.go
|
@ -233,8 +233,8 @@ func (e scanArgError) Error() string {
|
||||||
|
|
||||||
// Scan reads the values from the current row into dest values positionally.
|
// Scan reads the values from the current row into dest values positionally.
|
||||||
// dest can include pointers to core types, values implementing the Scanner
|
// dest can include pointers to core types, values implementing the Scanner
|
||||||
// interface, and []byte. []byte will skip the decoding process and directly
|
// interface, []byte, and nil. []byte will skip the decoding process and directly
|
||||||
// copy the raw bytes received from PostgreSQL.
|
// copy the raw bytes received from PostgreSQL. nil will skip the value entirely.
|
||||||
func (rows *Rows) Scan(dest ...interface{}) (err error) {
|
func (rows *Rows) Scan(dest ...interface{}) (err error) {
|
||||||
if len(rows.fields) != len(dest) {
|
if len(rows.fields) != len(dest) {
|
||||||
err = fmt.Errorf("Scan received wrong number of arguments, got %d but expected %d", len(dest), len(rows.fields))
|
err = fmt.Errorf("Scan received wrong number of arguments, got %d but expected %d", len(dest), len(rows.fields))
|
||||||
|
@ -245,6 +245,10 @@ func (rows *Rows) Scan(dest ...interface{}) (err error) {
|
||||||
for i, d := range dest {
|
for i, d := range dest {
|
||||||
vr, _ := rows.nextColumn()
|
vr, _ := rows.nextColumn()
|
||||||
|
|
||||||
|
if d == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
// Check for []byte first as we allow sidestepping the decoding process and retrieving the raw bytes
|
// Check for []byte first as we allow sidestepping the decoding process and retrieving the raw bytes
|
||||||
if b, ok := d.(*[]byte); ok {
|
if b, ok := d.(*[]byte); ok {
|
||||||
// If it actually is a bytea then pass it through decodeBytea (so it can be decoded if it is in text format)
|
// If it actually is a bytea then pass it through decodeBytea (so it can be decoded if it is in text format)
|
||||||
|
|
|
@ -218,6 +218,40 @@ func TestConnQueryReadTooManyValues(t *testing.T) {
|
||||||
ensureConnValid(t, conn)
|
ensureConnValid(t, conn)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestConnQueryScanIgnoreColumn(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
conn := mustConnect(t, *defaultConnConfig)
|
||||||
|
defer closeConn(t, conn)
|
||||||
|
|
||||||
|
rows, err := conn.Query("select 1::int8, 2::int8, 3::int8")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("conn.Query failed: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
ok := rows.Next()
|
||||||
|
if !ok {
|
||||||
|
t.Fatal("rows.Next terminated early")
|
||||||
|
}
|
||||||
|
|
||||||
|
var n, m int64
|
||||||
|
err = rows.Scan(&n, nil, &m)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("rows.Scan failed: %v", err)
|
||||||
|
}
|
||||||
|
rows.Close()
|
||||||
|
|
||||||
|
if n != 1 {
|
||||||
|
t.Errorf("Expected n to equal 1, but it was %d", n)
|
||||||
|
}
|
||||||
|
|
||||||
|
if m != 3 {
|
||||||
|
t.Errorf("Expected n to equal 3, but it was %d", m)
|
||||||
|
}
|
||||||
|
|
||||||
|
ensureConnValid(t, conn)
|
||||||
|
}
|
||||||
|
|
||||||
func TestConnQueryScanner(t *testing.T) {
|
func TestConnQueryScanner(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue