mirror of https://github.com/jackc/pgx.git
Add pgtype interfaces
parent
0f93bfc2de
commit
d984fcafab
16
conn.go
16
conn.go
|
@ -7,7 +7,6 @@ import (
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"golang.org/x/net/context"
|
|
||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
@ -20,7 +19,10 @@ import (
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"golang.org/x/net/context"
|
||||||
|
|
||||||
"github.com/jackc/pgx/chunkreader"
|
"github.com/jackc/pgx/chunkreader"
|
||||||
|
"github.com/jackc/pgx/pgtype"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -102,6 +104,8 @@ type Conn struct {
|
||||||
ctxInProgress bool
|
ctxInProgress bool
|
||||||
doneChan chan struct{}
|
doneChan chan struct{}
|
||||||
closedChan chan error
|
closedChan chan error
|
||||||
|
|
||||||
|
oidPgtypeValues map[OID]pgtype.Value
|
||||||
}
|
}
|
||||||
|
|
||||||
// PreparedStatement is a description of a prepared statement
|
// PreparedStatement is a description of a prepared statement
|
||||||
|
@ -275,6 +279,16 @@ func (c *Conn) connect(config ConnConfig, network, address string, tlsConfig *tl
|
||||||
c.doneChan = make(chan struct{})
|
c.doneChan = make(chan struct{})
|
||||||
c.closedChan = make(chan error)
|
c.closedChan = make(chan error)
|
||||||
|
|
||||||
|
i2 := pgtype.Int2(0)
|
||||||
|
i4 := pgtype.Int4(0)
|
||||||
|
i8 := pgtype.Int8(0)
|
||||||
|
|
||||||
|
c.oidPgtypeValues = map[OID]pgtype.Value{
|
||||||
|
Int2OID: &i2,
|
||||||
|
Int4OID: &i4,
|
||||||
|
Int8OID: &i8,
|
||||||
|
}
|
||||||
|
|
||||||
if tlsConfig != nil {
|
if tlsConfig != nil {
|
||||||
if c.shouldLog(LogLevelDebug) {
|
if c.shouldLog(LogLevelDebug) {
|
||||||
c.log(LogLevelDebug, "Starting TLS handshake")
|
c.log(LogLevelDebug, "Starting TLS handshake")
|
||||||
|
|
|
@ -11,7 +11,7 @@ import (
|
||||||
|
|
||||||
type Int2 int16
|
type Int2 int16
|
||||||
|
|
||||||
func (i *Int2) Convert(src interface{}) error {
|
func (i *Int2) ConvertFrom(src interface{}) error {
|
||||||
switch value := src.(type) {
|
switch value := src.(type) {
|
||||||
case Int2:
|
case Int2:
|
||||||
*i = value
|
*i = value
|
||||||
|
@ -73,7 +73,7 @@ func (i *Int2) Convert(src interface{}) error {
|
||||||
*i = Int2(num)
|
*i = Int2(num)
|
||||||
default:
|
default:
|
||||||
if originalSrc, ok := underlyingIntType(src); ok {
|
if originalSrc, ok := underlyingIntType(src); ok {
|
||||||
return i.Convert(originalSrc)
|
return i.ConvertFrom(originalSrc)
|
||||||
}
|
}
|
||||||
return fmt.Errorf("cannot convert %v to Int2", value)
|
return fmt.Errorf("cannot convert %v to Int2", value)
|
||||||
}
|
}
|
||||||
|
@ -81,6 +81,10 @@ func (i *Int2) Convert(src interface{}) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (i *Int2) AssignTo(dst interface{}) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (i *Int2) DecodeText(r io.Reader) error {
|
func (i *Int2) DecodeText(r io.Reader) error {
|
||||||
size, err := pgio.ReadInt32(r)
|
size, err := pgio.ReadInt32(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -86,7 +86,7 @@ func TestInt2Transcode(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestInt2Convert(t *testing.T) {
|
func TestInt2ConvertFrom(t *testing.T) {
|
||||||
type _int8 int8
|
type _int8 int8
|
||||||
|
|
||||||
successfulTests := []struct {
|
successfulTests := []struct {
|
||||||
|
@ -111,7 +111,7 @@ func TestInt2Convert(t *testing.T) {
|
||||||
|
|
||||||
for i, tt := range successfulTests {
|
for i, tt := range successfulTests {
|
||||||
var r pgtype.Int2
|
var r pgtype.Int2
|
||||||
err := r.Convert(tt.source)
|
err := r.ConvertFrom(tt.source)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("%d: %v", i, err)
|
t.Errorf("%d: %v", i, err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,7 +11,7 @@ import (
|
||||||
|
|
||||||
type Int4 int32
|
type Int4 int32
|
||||||
|
|
||||||
func (i *Int4) Convert(src interface{}) error {
|
func (i *Int4) ConvertFrom(src interface{}) error {
|
||||||
switch value := src.(type) {
|
switch value := src.(type) {
|
||||||
case Int4:
|
case Int4:
|
||||||
*i = value
|
*i = value
|
||||||
|
@ -64,7 +64,7 @@ func (i *Int4) Convert(src interface{}) error {
|
||||||
*i = Int4(num)
|
*i = Int4(num)
|
||||||
default:
|
default:
|
||||||
if originalSrc, ok := underlyingIntType(src); ok {
|
if originalSrc, ok := underlyingIntType(src); ok {
|
||||||
return i.Convert(originalSrc)
|
return i.ConvertFrom(originalSrc)
|
||||||
}
|
}
|
||||||
return fmt.Errorf("cannot convert %v to Int8", value)
|
return fmt.Errorf("cannot convert %v to Int8", value)
|
||||||
}
|
}
|
||||||
|
@ -72,6 +72,10 @@ func (i *Int4) Convert(src interface{}) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (i *Int4) AssignTo(dst interface{}) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (i *Int4) DecodeText(r io.Reader) error {
|
func (i *Int4) DecodeText(r io.Reader) error {
|
||||||
size, err := pgio.ReadInt32(r)
|
size, err := pgio.ReadInt32(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -86,7 +86,7 @@ func TestInt4Transcode(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestInt4Convert(t *testing.T) {
|
func TestInt4ConvertFrom(t *testing.T) {
|
||||||
type _int8 int8
|
type _int8 int8
|
||||||
|
|
||||||
successfulTests := []struct {
|
successfulTests := []struct {
|
||||||
|
@ -111,7 +111,7 @@ func TestInt4Convert(t *testing.T) {
|
||||||
|
|
||||||
for i, tt := range successfulTests {
|
for i, tt := range successfulTests {
|
||||||
var r pgtype.Int4
|
var r pgtype.Int4
|
||||||
err := r.Convert(tt.source)
|
err := r.ConvertFrom(tt.source)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("%d: %v", i, err)
|
t.Errorf("%d: %v", i, err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,7 +11,7 @@ import (
|
||||||
|
|
||||||
type Int8 int64
|
type Int8 int64
|
||||||
|
|
||||||
func (i *Int8) Convert(src interface{}) error {
|
func (i *Int8) ConvertFrom(src interface{}) error {
|
||||||
switch value := src.(type) {
|
switch value := src.(type) {
|
||||||
case Int8:
|
case Int8:
|
||||||
*i = value
|
*i = value
|
||||||
|
@ -55,7 +55,7 @@ func (i *Int8) Convert(src interface{}) error {
|
||||||
*i = Int8(num)
|
*i = Int8(num)
|
||||||
default:
|
default:
|
||||||
if originalSrc, ok := underlyingIntType(src); ok {
|
if originalSrc, ok := underlyingIntType(src); ok {
|
||||||
return i.Convert(originalSrc)
|
return i.ConvertFrom(originalSrc)
|
||||||
}
|
}
|
||||||
return fmt.Errorf("cannot convert %v to Int8", value)
|
return fmt.Errorf("cannot convert %v to Int8", value)
|
||||||
}
|
}
|
||||||
|
@ -63,6 +63,10 @@ func (i *Int8) Convert(src interface{}) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (i *Int8) AssignTo(dst interface{}) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (i *Int8) DecodeText(r io.Reader) error {
|
func (i *Int8) DecodeText(r io.Reader) error {
|
||||||
size, err := pgio.ReadInt32(r)
|
size, err := pgio.ReadInt32(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -86,7 +86,7 @@ func TestInt8Transcode(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestInt8Convert(t *testing.T) {
|
func TestInt8ConvertFrom(t *testing.T) {
|
||||||
type _int8 int8
|
type _int8 int8
|
||||||
|
|
||||||
successfulTests := []struct {
|
successfulTests := []struct {
|
||||||
|
@ -111,7 +111,7 @@ func TestInt8Convert(t *testing.T) {
|
||||||
|
|
||||||
for i, tt := range successfulTests {
|
for i, tt := range successfulTests {
|
||||||
var r pgtype.Int8
|
var r pgtype.Int8
|
||||||
err := r.Convert(tt.source)
|
err := r.ConvertFrom(tt.source)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("%d: %v", i, err)
|
t.Errorf("%d: %v", i, err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,26 @@
|
||||||
|
package pgtype
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Value interface {
|
||||||
|
ConvertFrom(src interface{}) error
|
||||||
|
AssignTo(dst interface{}) error
|
||||||
|
}
|
||||||
|
|
||||||
|
type BinaryDecoder interface {
|
||||||
|
DecodeBinary(r io.Reader) error
|
||||||
|
}
|
||||||
|
|
||||||
|
type TextDecoder interface {
|
||||||
|
DecodeText(r io.Reader) error
|
||||||
|
}
|
||||||
|
|
||||||
|
type BinaryEncoder interface {
|
||||||
|
EncodeBinary(w io.Writer) error
|
||||||
|
}
|
||||||
|
|
||||||
|
type TextEncoder interface {
|
||||||
|
EncodeText(w io.Writer) error
|
||||||
|
}
|
22
values.go
22
values.go
|
@ -1060,28 +1060,12 @@ func Encode(wbuf *WriteBuf, oid OID, arg interface{}) error {
|
||||||
return encodeJSONB(wbuf, oid, arg)
|
return encodeJSONB(wbuf, oid, arg)
|
||||||
}
|
}
|
||||||
|
|
||||||
switch oid {
|
if value, ok := wbuf.conn.oidPgtypeValues[oid]; ok {
|
||||||
case Int2OID:
|
err := value.ConvertFrom(arg)
|
||||||
var i2 pgtype.Int2
|
|
||||||
err := i2.Convert(arg)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return i2.EncodeBinary(wbuf)
|
return value.(pgtype.BinaryEncoder).EncodeBinary(wbuf)
|
||||||
case Int4OID:
|
|
||||||
var i4 pgtype.Int4
|
|
||||||
err := i4.Convert(arg)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return i4.EncodeBinary(wbuf)
|
|
||||||
case Int8OID:
|
|
||||||
var i8 pgtype.Int8
|
|
||||||
err := i8.Convert(arg)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return i8.EncodeBinary(wbuf)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
switch arg := arg.(type) {
|
switch arg := arg.(type) {
|
||||||
|
|
Loading…
Reference in New Issue