From 198f5093e81806ff1d49d5d9cfc88942db691ace Mon Sep 17 00:00:00 2001 From: Maxim Ivanov Date: Fri, 10 Apr 2020 18:31:09 +0100 Subject: [PATCH] Clarify row type handling example --- doc.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/doc.go b/doc.go index 7b73c70e..bb5350ec 100644 --- a/doc.go +++ b/doc.go @@ -167,8 +167,8 @@ pgtype.Record first can simplify process by avoiding dealing with raw protocol d For example: type MyType struct { - a int - b *string + a int // NULL will cause decoding error + b *string // there can be NULL in this position in SQL } func (t *MyType) DecodeBinary(ci *pgtype.ConnInfo, src []byte) error { @@ -181,14 +181,14 @@ For example: } if r.Status != pgtype.Present { - t = nil - return nil + return errors.New("BUG: decoding should not be called on NULL value") } a := r.Fields[0].(*pgtype.Int4) b := r.Fields[1].(*pgtype.Text) - // types compatibility are checked by AssignTo + // type compatibility is checked by AssignTo + // only lossless assignments will succeed if err := a.AssignTo(&t.a); err != nil { return err } @@ -201,7 +201,7 @@ For example: } result := MyType{} - err := conn.QueryRow(context.Background(), "select row(1, 'foo'::text)").Scan(&r) + err := conn.QueryRow(context.Background(), "select row(1, 'foo'::text)", pgx.QueryResultFormats{pgx.BinaryFormatCode}).Scan(&r) Raw Bytes Mapping