Merge branch 'master' into v3-experimental
This commit is contained in:
@@ -3,6 +3,7 @@ package pgx_test
|
||||
import (
|
||||
"bytes"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -291,6 +292,67 @@ func TestConnQueryScanner(t *testing.T) {
|
||||
ensureConnValid(t, conn)
|
||||
}
|
||||
|
||||
type pgxNullInt64 struct {
|
||||
Int64 int64
|
||||
Valid bool // Valid is true if Int64 is not NULL
|
||||
}
|
||||
|
||||
func (n *pgxNullInt64) ScanPgx(vr *pgx.ValueReader) error {
|
||||
if vr.Type().DataType != pgx.Int8Oid {
|
||||
return pgx.SerializationError(fmt.Sprintf("pgxNullInt64.Scan cannot decode OID %d", vr.Type().DataType))
|
||||
}
|
||||
|
||||
if vr.Len() == -1 {
|
||||
n.Int64, n.Valid = 0, false
|
||||
return nil
|
||||
}
|
||||
n.Valid = true
|
||||
|
||||
err := pgx.Decode(vr, &n.Int64)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return vr.Err()
|
||||
}
|
||||
|
||||
func TestConnQueryPgxScanner(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
conn := mustConnect(t, *defaultConnConfig)
|
||||
defer closeConn(t, conn)
|
||||
|
||||
rows, err := conn.Query("select null::int8, 1::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 pgxNullInt64
|
||||
err = rows.Scan(&n, &m)
|
||||
if err != nil {
|
||||
t.Fatalf("rows.Scan failed: %v", err)
|
||||
}
|
||||
rows.Close()
|
||||
|
||||
if n.Valid {
|
||||
t.Error("Null should not be valid, but it was")
|
||||
}
|
||||
|
||||
if !m.Valid {
|
||||
t.Error("1 should be valid, but it wasn't")
|
||||
}
|
||||
|
||||
if m.Int64 != 1 {
|
||||
t.Errorf("m.Int64 should have been 1, but it was %v", m.Int64)
|
||||
}
|
||||
|
||||
ensureConnValid(t, conn)
|
||||
}
|
||||
|
||||
func TestConnQueryErrorWhileReturningRows(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user