mirror of
https://github.com/jackc/pgx.git
synced 2025-05-31 11:42:24 +00:00
Extract scan value to pgtype
This commit is contained in:
parent
59003afe8c
commit
f779b05f36
54
pgtype.go
54
pgtype.go
@ -1,6 +1,7 @@
|
|||||||
package pgtype
|
package pgtype
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"database/sql"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
@ -84,6 +85,12 @@ func (im InfinityModifier) String() string {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PostgreSQL format codes
|
||||||
|
const (
|
||||||
|
TextFormatCode = 0
|
||||||
|
BinaryFormatCode = 1
|
||||||
|
)
|
||||||
|
|
||||||
type Value interface {
|
type Value interface {
|
||||||
// Set converts and assigns src to itself.
|
// Set converts and assigns src to itself.
|
||||||
Set(src interface{}) error
|
Set(src interface{}) error
|
||||||
@ -207,6 +214,53 @@ func (ci *ConnInfo) DeepCopy() *ConnInfo {
|
|||||||
return ci2
|
return ci2
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (ci *ConnInfo) Scan(oid OID, formatCode int16, buf []byte, dest interface{}) error {
|
||||||
|
if dest, ok := dest.(BinaryDecoder); ok && formatCode == BinaryFormatCode {
|
||||||
|
return dest.DecodeBinary(ci, buf)
|
||||||
|
}
|
||||||
|
|
||||||
|
if dest, ok := dest.(TextDecoder); ok && formatCode == TextFormatCode {
|
||||||
|
return dest.DecodeText(ci, buf)
|
||||||
|
}
|
||||||
|
|
||||||
|
if dt, ok := ci.DataTypeForOID(oid); ok {
|
||||||
|
value := dt.Value
|
||||||
|
switch formatCode {
|
||||||
|
case TextFormatCode:
|
||||||
|
if textDecoder, ok := value.(TextDecoder); ok {
|
||||||
|
err := textDecoder.DecodeText(ci, buf)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return errors.Errorf("%T is not a pgtype.TextDecoder", value)
|
||||||
|
}
|
||||||
|
case BinaryFormatCode:
|
||||||
|
if binaryDecoder, ok := value.(BinaryDecoder); ok {
|
||||||
|
err := binaryDecoder.DecodeBinary(ci, buf)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return errors.Errorf("%T is not a pgtype.BinaryDecoder", value)
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
return errors.Errorf("unknown format code: %v", formatCode)
|
||||||
|
}
|
||||||
|
|
||||||
|
if scanner, ok := dest.(sql.Scanner); ok {
|
||||||
|
sqlSrc, err := DatabaseSQLValue(ci, value)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return scanner.Scan(sqlSrc)
|
||||||
|
} else {
|
||||||
|
return value.AssignTo(dest)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return errors.Errorf("unknown oid: %v", oid)
|
||||||
|
}
|
||||||
|
|
||||||
var nameValues map[string]Value
|
var nameValues map[string]Value
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user