Add AssignTo to pgtype.Timestamptz

Also handle infinity for pgtype.Date
pgxtype-experiment2
Jack Christensen 2017-03-03 15:24:47 -06:00
parent 7329933610
commit c19454582b
3 changed files with 42 additions and 7 deletions

13
conn.go
View File

@ -280,12 +280,13 @@ func (c *Conn) connect(config ConnConfig, network, address string, tlsConfig *tl
c.closedChan = make(chan error)
c.oidPgtypeValues = map[OID]pgtype.Value{
BoolOID: &pgtype.Bool{},
DateOID: &pgtype.Date{},
Int2OID: &pgtype.Int2{},
Int2ArrayOID: &pgtype.Int2Array{},
Int4OID: &pgtype.Int4{},
Int8OID: &pgtype.Int8{},
BoolOID: &pgtype.Bool{},
DateOID: &pgtype.Date{},
Int2OID: &pgtype.Int2{},
Int2ArrayOID: &pgtype.Int2Array{},
Int4OID: &pgtype.Int4{},
Int8OID: &pgtype.Int8{},
TimestampTzOID: &pgtype.Timestamptz{},
}
if tlsConfig != nil {

View File

@ -39,7 +39,7 @@ func (d *Date) ConvertFrom(src interface{}) error {
func (d *Date) AssignTo(dst interface{}) error {
switch v := dst.(type) {
case *time.Time:
if d.Status != Present {
if d.Status != Present || d.InfinityModifier != None {
return fmt.Errorf("cannot assign %v to %T", d, dst)
}
*v = d.Time

View File

@ -3,6 +3,7 @@ package pgtype
import (
"fmt"
"io"
"reflect"
"time"
"github.com/jackc/pgx/pgio"
@ -40,6 +41,39 @@ func (t *Timestamptz) ConvertFrom(src interface{}) error {
return nil
}
func (t *Timestamptz) AssignTo(dst interface{}) error {
switch v := dst.(type) {
case *time.Time:
if t.Status != Present || t.InfinityModifier != None {
return fmt.Errorf("cannot assign %v to %T", t, dst)
}
*v = t.Time
default:
if v := reflect.ValueOf(dst); v.Kind() == reflect.Ptr {
el := v.Elem()
switch el.Kind() {
// if dst is a pointer to pointer, strip the pointer and try again
case reflect.Ptr:
if t.Status == Null {
if !el.IsNil() {
// if the destination pointer is not nil, nil it out
el.Set(reflect.Zero(el.Type()))
}
return nil
}
if el.IsNil() {
// allocate destination
el.Set(reflect.New(el.Type().Elem()))
}
return t.AssignTo(el.Interface())
}
}
return fmt.Errorf("cannot assign %v into %T", t, dst)
}
return nil
}
func (t *Timestamptz) DecodeText(r io.Reader) error {
size, err := pgio.ReadInt32(r)
if err != nil {