2
0

pgtype.Encode(Binary|Text) do not write length

To aid in composability, these methods no longer write their own length. This
is especially useful for text formatted arrays and may be useful for future
database/sql compatibility. It also makes the code a little simpler as the
types no longer have to compute their own size.

Along with this, these methods cannot encode NULL. They now return a boolean
if they are NULL. This also benefits text array encoding as numeric arrays
require NULL to be exactly `NULL` while string arrays require NULL to be
`"NULL"`.
This commit is contained in:
Jack Christensen
2017-03-11 12:32:33 -06:00
parent 6c26c3a4a3
commit 1f3e484ca1
38 changed files with 1271 additions and 1319 deletions
+18 -26
View File
@@ -5,8 +5,6 @@ import (
"fmt"
"io"
"reflect"
"github.com/jackc/pgx/pgio"
)
type Bytea struct {
@@ -101,37 +99,31 @@ func (dst *Bytea) DecodeBinary(src []byte) error {
return nil
}
func (src Bytea) EncodeText(w io.Writer) error {
if done, err := encodeNotPresent(w, src.Status); done {
return err
func (src Bytea) EncodeText(w io.Writer) (bool, error) {
switch src.Status {
case Null:
return true, nil
case Undefined:
return false, errUndefined
}
str := hex.EncodeToString(src.Bytes)
_, err := pgio.WriteInt32(w, int32(len(str)+2))
_, err := io.WriteString(w, `\x`)
if err != nil {
return nil
return false, err
}
_, err = io.WriteString(w, `\x`)
if err != nil {
return nil
}
_, err = io.WriteString(w, str)
return err
_, err = io.WriteString(w, hex.EncodeToString(src.Bytes))
return false, err
}
func (src Bytea) EncodeBinary(w io.Writer) error {
if done, err := encodeNotPresent(w, src.Status); done {
return err
func (src Bytea) EncodeBinary(w io.Writer) (bool, error) {
switch src.Status {
case Null:
return true, nil
case Undefined:
return false, errUndefined
}
_, err := pgio.WriteInt32(w, int32(len(src.Bytes)))
if err != nil {
return nil
}
_, err = w.Write(src.Bytes)
return err
_, err := w.Write(src.Bytes)
return false, err
}