Fix custom type example

refs #283
pull/285/head
Jack Christensen 2017-06-24 11:37:50 -05:00
parent cd16be9308
commit c4efaf30a2
1 changed files with 12 additions and 3 deletions

View File

@ -3,9 +3,10 @@ package pgx_test
import (
"errors"
"fmt"
"github.com/jackc/pgx"
"regexp"
"strconv"
"github.com/jackc/pgx"
)
var pointRegexp *regexp.Regexp = regexp.MustCompile(`^\((.*),(.*)\)$`)
@ -55,7 +56,7 @@ func (p *NullPoint) ScanPgx(vr *pgx.ValueReader) error {
return vr.Err()
}
func (p NullPoint) FormatCode() int16 { return pgx.BinaryFormatCode }
func (p NullPoint) FormatCode() int16 { return pgx.TextFormatCode }
func (p NullPoint) Encode(w *pgx.WriteBuf, oid pgx.Oid) error {
if !p.Valid {
@ -63,7 +64,7 @@ func (p NullPoint) Encode(w *pgx.WriteBuf, oid pgx.Oid) error {
return nil
}
s := fmt.Sprintf("point(%v,%v)", p.X, p.Y)
s := fmt.Sprintf("(%v,%v)", p.X, p.Y)
w.WriteInt32(int32(len(s)))
w.WriteBytes([]byte(s))
@ -98,7 +99,15 @@ func Example_CustomType() {
return
}
fmt.Println(p)
err = conn.QueryRow("select $1::point", &NullPoint{X: 0.5, Y: 0.75, Valid: true}).Scan(&p)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(p)
// Output:
// null point
// 1.5, 2.5
// 0.5, 0.75
}