2
0

Add database/sql support to pgtype

This commit is contained in:
Jack Christensen
2017-03-18 21:11:43 -05:00
parent 5572c002dc
commit bec9bd261b
55 changed files with 1459 additions and 201 deletions
+38
View File
@@ -1,6 +1,7 @@
package pgtype
import (
"database/sql/driver"
"encoding/hex"
"fmt"
"io"
@@ -12,6 +13,11 @@ type Bytea struct {
}
func (dst *Bytea) Set(src interface{}) error {
if src == nil {
*dst = Bytea{Status: Null}
return nil
}
switch value := src.(type) {
case []byte:
if value != nil {
@@ -124,3 +130,35 @@ func (src Bytea) EncodeBinary(ci *ConnInfo, w io.Writer) (bool, error) {
_, err := w.Write(src.Bytes)
return false, err
}
// Scan implements the database/sql Scanner interface.
func (dst *Bytea) Scan(src interface{}) error {
if src == nil {
*dst = Bytea{Status: Null}
return nil
}
switch src := src.(type) {
case string:
return dst.DecodeText(nil, []byte(src))
case []byte:
buf := make([]byte, len(src))
copy(buf, src)
*dst = Bytea{Bytes: buf, Status: Present}
return nil
}
return fmt.Errorf("cannot scan %T", src)
}
// Value implements the database/sql/driver Valuer interface.
func (src Bytea) Value() (driver.Value, error) {
switch src.Status {
case Present:
return src.Bytes, nil
case Null:
return nil, nil
default:
return nil, errUndefined
}
}