mirror of https://github.com/jackc/pgx.git
parent
a8c350c77d
commit
cab445ddd2
|
@ -164,5 +164,5 @@ func (dst *Box) Scan(src interface{}) error {
|
|||
|
||||
// Value implements the database/sql/driver Valuer interface.
|
||||
func (src *Box) Value() (driver.Value, error) {
|
||||
return encodeValueText(src)
|
||||
return EncodeValueText(src)
|
||||
}
|
||||
|
|
|
@ -146,5 +146,5 @@ func (dst *Circle) Scan(src interface{}) error {
|
|||
|
||||
// Value implements the database/sql/driver Valuer interface.
|
||||
func (src *Circle) Value() (driver.Value, error) {
|
||||
return encodeValueText(src)
|
||||
return EncodeValueText(src)
|
||||
}
|
||||
|
|
|
@ -31,7 +31,7 @@ func DatabaseSQLValue(ci *ConnInfo, src Value) (interface{}, error) {
|
|||
return nil, errors.New("cannot convert to database/sql compatible value")
|
||||
}
|
||||
|
||||
func encodeValueText(src TextEncoder) (interface{}, error) {
|
||||
func EncodeValueText(src TextEncoder) (interface{}, error) {
|
||||
buf := &bytes.Buffer{}
|
||||
null, err := src.EncodeText(nil, buf)
|
||||
if err != nil {
|
||||
|
|
|
@ -264,5 +264,5 @@ func (dst *Daterange) Scan(src interface{}) error {
|
|||
|
||||
// Value implements the database/sql/driver Valuer interface.
|
||||
func (src *Daterange) Value() (driver.Value, error) {
|
||||
return encodeValueText(src)
|
||||
return EncodeValueText(src)
|
||||
}
|
||||
|
|
|
@ -0,0 +1,164 @@
|
|||
package uuid
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"github.com/jackc/pgx/pgtype"
|
||||
uuid "github.com/satori/go.uuid"
|
||||
)
|
||||
|
||||
var errUndefined = errors.New("cannot encode status undefined")
|
||||
|
||||
type Uuid struct {
|
||||
UUID uuid.UUID
|
||||
Status pgtype.Status
|
||||
}
|
||||
|
||||
func (dst *Uuid) Set(src interface{}) error {
|
||||
switch value := src.(type) {
|
||||
case uuid.UUID:
|
||||
*dst = Uuid{UUID: value, Status: pgtype.Present}
|
||||
case [16]byte:
|
||||
*dst = Uuid{UUID: uuid.UUID(value), Status: pgtype.Present}
|
||||
case []byte:
|
||||
if len(value) != 16 {
|
||||
return fmt.Errorf("[]byte must be 16 bytes to convert to Uuid: %d", len(value))
|
||||
}
|
||||
*dst = Uuid{Status: pgtype.Present}
|
||||
copy(dst.UUID[:], value)
|
||||
case string:
|
||||
uuid, err := uuid.FromString(value)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*dst = Uuid{UUID: uuid, Status: pgtype.Present}
|
||||
default:
|
||||
// If all else fails see if pgtype.Uuid can handle it. If so, translate through that.
|
||||
pgUuid := &pgtype.Uuid{}
|
||||
if err := pgUuid.Set(value); err != nil {
|
||||
return fmt.Errorf("cannot convert %v to Uuid", value)
|
||||
}
|
||||
|
||||
*dst = Uuid{UUID: uuid.UUID(pgUuid.Bytes), Status: pgUuid.Status}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (dst *Uuid) Get() interface{} {
|
||||
switch dst.Status {
|
||||
case pgtype.Present:
|
||||
return dst.UUID
|
||||
case pgtype.Null:
|
||||
return nil
|
||||
default:
|
||||
return dst.Status
|
||||
}
|
||||
}
|
||||
|
||||
func (src *Uuid) AssignTo(dst interface{}) error {
|
||||
switch src.Status {
|
||||
case pgtype.Present:
|
||||
switch v := dst.(type) {
|
||||
case *uuid.UUID:
|
||||
*v = src.UUID
|
||||
case *[16]byte:
|
||||
*v = [16]byte(src.UUID)
|
||||
return nil
|
||||
case *[]byte:
|
||||
*v = make([]byte, 16)
|
||||
copy(*v, src.UUID[:])
|
||||
return nil
|
||||
case *string:
|
||||
*v = src.UUID.String()
|
||||
return nil
|
||||
default:
|
||||
if nextDst, retry := pgtype.GetAssignToDstType(v); retry {
|
||||
return src.AssignTo(nextDst)
|
||||
}
|
||||
}
|
||||
case pgtype.Null:
|
||||
return pgtype.NullAssignTo(dst)
|
||||
}
|
||||
|
||||
return fmt.Errorf("cannot assign %v into %T", src, dst)
|
||||
}
|
||||
|
||||
func (dst *Uuid) DecodeText(ci *pgtype.ConnInfo, src []byte) error {
|
||||
if src == nil {
|
||||
*dst = Uuid{Status: pgtype.Null}
|
||||
return nil
|
||||
}
|
||||
|
||||
u, err := uuid.FromString(string(src))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*dst = Uuid{UUID: u, Status: pgtype.Present}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (dst *Uuid) DecodeBinary(ci *pgtype.ConnInfo, src []byte) error {
|
||||
if src == nil {
|
||||
*dst = Uuid{Status: pgtype.Null}
|
||||
return nil
|
||||
}
|
||||
|
||||
if len(src) != 16 {
|
||||
return fmt.Errorf("invalid length for Uuid: %v", len(src))
|
||||
}
|
||||
|
||||
*dst = Uuid{Status: pgtype.Present}
|
||||
copy(dst.UUID[:], src)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (src *Uuid) EncodeText(ci *pgtype.ConnInfo, w io.Writer) (bool, error) {
|
||||
switch src.Status {
|
||||
case pgtype.Null:
|
||||
return true, nil
|
||||
case pgtype.Undefined:
|
||||
return false, errUndefined
|
||||
}
|
||||
|
||||
_, err := io.WriteString(w, src.UUID.String())
|
||||
return false, err
|
||||
}
|
||||
|
||||
func (src *Uuid) EncodeBinary(ci *pgtype.ConnInfo, w io.Writer) (bool, error) {
|
||||
switch src.Status {
|
||||
case pgtype.Null:
|
||||
return true, nil
|
||||
case pgtype.Undefined:
|
||||
return false, errUndefined
|
||||
}
|
||||
|
||||
_, err := w.Write(src.UUID[:])
|
||||
return false, err
|
||||
}
|
||||
|
||||
// Scan implements the database/sql Scanner interface.
|
||||
func (dst *Uuid) Scan(src interface{}) error {
|
||||
if src == nil {
|
||||
*dst = Uuid{Status: pgtype.Null}
|
||||
return nil
|
||||
}
|
||||
|
||||
switch src := src.(type) {
|
||||
case string:
|
||||
return dst.DecodeText(nil, []byte(src))
|
||||
case []byte:
|
||||
return dst.DecodeText(nil, src)
|
||||
}
|
||||
|
||||
return fmt.Errorf("cannot scan %T", src)
|
||||
}
|
||||
|
||||
// Value implements the database/sql/driver Valuer interface.
|
||||
func (src *Uuid) Value() (driver.Value, error) {
|
||||
return pgtype.EncodeValueText(src)
|
||||
}
|
|
@ -0,0 +1,97 @@
|
|||
package uuid_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"testing"
|
||||
|
||||
"github.com/jackc/pgx/pgtype"
|
||||
satori "github.com/jackc/pgx/pgtype/ext/satori-uuid"
|
||||
"github.com/jackc/pgx/pgtype/testutil"
|
||||
)
|
||||
|
||||
func TestUuidTranscode(t *testing.T) {
|
||||
testutil.TestSuccessfulTranscode(t, "uuid", []interface{}{
|
||||
&satori.Uuid{UUID: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Status: pgtype.Present},
|
||||
&satori.Uuid{Status: pgtype.Null},
|
||||
})
|
||||
}
|
||||
|
||||
func TestUuidSet(t *testing.T) {
|
||||
successfulTests := []struct {
|
||||
source interface{}
|
||||
result satori.Uuid
|
||||
}{
|
||||
{
|
||||
source: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
|
||||
result: satori.Uuid{UUID: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Status: pgtype.Present},
|
||||
},
|
||||
{
|
||||
source: []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
|
||||
result: satori.Uuid{UUID: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Status: pgtype.Present},
|
||||
},
|
||||
{
|
||||
source: "00010203-0405-0607-0809-0a0b0c0d0e0f",
|
||||
result: satori.Uuid{UUID: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Status: pgtype.Present},
|
||||
},
|
||||
}
|
||||
|
||||
for i, tt := range successfulTests {
|
||||
var r satori.Uuid
|
||||
err := r.Set(tt.source)
|
||||
if err != nil {
|
||||
t.Errorf("%d: %v", i, err)
|
||||
}
|
||||
|
||||
if r != tt.result {
|
||||
t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, r)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestUuidAssignTo(t *testing.T) {
|
||||
{
|
||||
src := satori.Uuid{UUID: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Status: pgtype.Present}
|
||||
var dst [16]byte
|
||||
expected := [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
|
||||
|
||||
err := src.AssignTo(&dst)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if dst != expected {
|
||||
t.Errorf("expected %v to assign %v, but result was %v", src, expected, dst)
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
src := satori.Uuid{UUID: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Status: pgtype.Present}
|
||||
var dst []byte
|
||||
expected := []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
|
||||
|
||||
err := src.AssignTo(&dst)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if bytes.Compare(dst, expected) != 0 {
|
||||
t.Errorf("expected %v to assign %v, but result was %v", src, expected, dst)
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
src := satori.Uuid{UUID: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Status: pgtype.Present}
|
||||
var dst string
|
||||
expected := "00010203-0405-0607-0809-0a0b0c0d0e0f"
|
||||
|
||||
err := src.AssignTo(&dst)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if dst != expected {
|
||||
t.Errorf("expected %v to assign %v, but result was %v", src, expected, dst)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -463,5 +463,5 @@ func (dst *Hstore) Scan(src interface{}) error {
|
|||
|
||||
// Value implements the database/sql/driver Valuer interface.
|
||||
func (src *Hstore) Value() (driver.Value, error) {
|
||||
return encodeValueText(src)
|
||||
return EncodeValueText(src)
|
||||
}
|
||||
|
|
|
@ -221,5 +221,5 @@ func (dst *Inet) Scan(src interface{}) error {
|
|||
|
||||
// Value implements the database/sql/driver Valuer interface.
|
||||
func (src *Inet) Value() (driver.Value, error) {
|
||||
return encodeValueText(src)
|
||||
return EncodeValueText(src)
|
||||
}
|
||||
|
|
|
@ -264,5 +264,5 @@ func (dst *Int4range) Scan(src interface{}) error {
|
|||
|
||||
// Value implements the database/sql/driver Valuer interface.
|
||||
func (src *Int4range) Value() (driver.Value, error) {
|
||||
return encodeValueText(src)
|
||||
return EncodeValueText(src)
|
||||
}
|
||||
|
|
|
@ -264,5 +264,5 @@ func (dst *Int8range) Scan(src interface{}) error {
|
|||
|
||||
// Value implements the database/sql/driver Valuer interface.
|
||||
func (src *Int8range) Value() (driver.Value, error) {
|
||||
return encodeValueText(src)
|
||||
return EncodeValueText(src)
|
||||
}
|
||||
|
|
|
@ -267,5 +267,5 @@ func (dst *Interval) Scan(src interface{}) error {
|
|||
|
||||
// Value implements the database/sql/driver Valuer interface.
|
||||
func (src *Interval) Value() (driver.Value, error) {
|
||||
return encodeValueText(src)
|
||||
return EncodeValueText(src)
|
||||
}
|
||||
|
|
|
@ -144,5 +144,5 @@ func (dst *Line) Scan(src interface{}) error {
|
|||
|
||||
// Value implements the database/sql/driver Valuer interface.
|
||||
func (src *Line) Value() (driver.Value, error) {
|
||||
return encodeValueText(src)
|
||||
return EncodeValueText(src)
|
||||
}
|
||||
|
|
|
@ -164,5 +164,5 @@ func (dst *Lseg) Scan(src interface{}) error {
|
|||
|
||||
// Value implements the database/sql/driver Valuer interface.
|
||||
func (src *Lseg) Value() (driver.Value, error) {
|
||||
return encodeValueText(src)
|
||||
return EncodeValueText(src)
|
||||
}
|
||||
|
|
|
@ -150,5 +150,5 @@ func (dst *Macaddr) Scan(src interface{}) error {
|
|||
|
||||
// Value implements the database/sql/driver Valuer interface.
|
||||
func (src *Macaddr) Value() (driver.Value, error) {
|
||||
return encodeValueText(src)
|
||||
return EncodeValueText(src)
|
||||
}
|
||||
|
|
|
@ -264,5 +264,5 @@ func (dst *Numrange) Scan(src interface{}) error {
|
|||
|
||||
// Value implements the database/sql/driver Valuer interface.
|
||||
func (src *Numrange) Value() (driver.Value, error) {
|
||||
return encodeValueText(src)
|
||||
return EncodeValueText(src)
|
||||
}
|
||||
|
|
|
@ -203,5 +203,5 @@ func (dst *Path) Scan(src interface{}) error {
|
|||
|
||||
// Value implements the database/sql/driver Valuer interface.
|
||||
func (src *Path) Value() (driver.Value, error) {
|
||||
return encodeValueText(src)
|
||||
return EncodeValueText(src)
|
||||
}
|
||||
|
|
|
@ -138,5 +138,5 @@ func (dst *Point) Scan(src interface{}) error {
|
|||
|
||||
// Value implements the database/sql/driver Valuer interface.
|
||||
func (src *Point) Value() (driver.Value, error) {
|
||||
return encodeValueText(src)
|
||||
return EncodeValueText(src)
|
||||
}
|
||||
|
|
|
@ -182,5 +182,5 @@ func (dst *Polygon) Scan(src interface{}) error {
|
|||
|
||||
// Value implements the database/sql/driver Valuer interface.
|
||||
func (src *Polygon) Value() (driver.Value, error) {
|
||||
return encodeValueText(src)
|
||||
return EncodeValueText(src)
|
||||
}
|
||||
|
|
|
@ -142,5 +142,5 @@ func (dst *Tid) Scan(src interface{}) error {
|
|||
|
||||
// Value implements the database/sql/driver Valuer interface.
|
||||
func (src *Tid) Value() (driver.Value, error) {
|
||||
return encodeValueText(src)
|
||||
return EncodeValueText(src)
|
||||
}
|
||||
|
|
|
@ -264,5 +264,5 @@ func (dst *Tsrange) Scan(src interface{}) error {
|
|||
|
||||
// Value implements the database/sql/driver Valuer interface.
|
||||
func (src *Tsrange) Value() (driver.Value, error) {
|
||||
return encodeValueText(src)
|
||||
return EncodeValueText(src)
|
||||
}
|
||||
|
|
|
@ -264,5 +264,5 @@ func (dst *Tstzrange) Scan(src interface{}) error {
|
|||
|
||||
// Value implements the database/sql/driver Valuer interface.
|
||||
func (src *Tstzrange) Value() (driver.Value, error) {
|
||||
return encodeValueText(src)
|
||||
return EncodeValueText(src)
|
||||
}
|
||||
|
|
|
@ -264,5 +264,5 @@ func (dst *<%= range_type %>) Scan(src interface{}) error {
|
|||
|
||||
// Value implements the database/sql/driver Valuer interface.
|
||||
func (src <%= range_type %>) Value() (driver.Value, error) {
|
||||
return encodeValueText(src)
|
||||
return EncodeValueText(src)
|
||||
}
|
||||
|
|
|
@ -169,5 +169,5 @@ func (dst *Uuid) Scan(src interface{}) error {
|
|||
|
||||
// Value implements the database/sql/driver Valuer interface.
|
||||
func (src *Uuid) Value() (driver.Value, error) {
|
||||
return encodeValueText(src)
|
||||
return EncodeValueText(src)
|
||||
}
|
||||
|
|
|
@ -137,5 +137,5 @@ func (dst *Varbit) Scan(src interface{}) error {
|
|||
|
||||
// Value implements the database/sql/driver Valuer interface.
|
||||
func (src *Varbit) Value() (driver.Value, error) {
|
||||
return encodeValueText(src)
|
||||
return EncodeValueText(src)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue