2
0

Add pgtype.Record and prerequisite restructuring

Because reading a record type requires the decoder to be able to look up oid
to type mapping and types such as hstore have types that are not fixed between
different PostgreSQL servers it was necessary to restructure the pgtype system
so all encoders and decodes take a *ConnInfo that includes oid/name/type
information.
This commit is contained in:
Jack Christensen
2017-03-18 12:01:16 -05:00
parent b31d409dc2
commit 6e21cb00fe
54 changed files with 1761 additions and 320 deletions
+66
View File
@@ -0,0 +1,66 @@
package pgtype
import (
"bytes"
"errors"
)
func DatabaseSQLValue(ci *ConnInfo, src Value) (interface{}, error) {
switch src := src.(type) {
case *Bool:
return src.Bool, nil
case *Bytea:
return src.Bytes, nil
case *Date:
if src.InfinityModifier == None {
return src.Time, nil
}
case *Float4:
return float64(src.Float), nil
case *Float8:
return src.Float, nil
case *GenericBinary:
return src.Bytes, nil
case *GenericText:
return src.String, nil
case *Int2:
return int64(src.Int), nil
case *Int4:
return int64(src.Int), nil
case *Int8:
return int64(src.Int), nil
case *Text:
return src.String, nil
case *Timestamp:
if src.InfinityModifier == None {
return src.Time, nil
}
case *Timestamptz:
if src.InfinityModifier == None {
return src.Time, nil
}
case *Unknown:
return src.String, nil
case *Varchar:
return src.String, nil
}
buf := &bytes.Buffer{}
if textEncoder, ok := src.(TextEncoder); ok {
_, err := textEncoder.EncodeText(ci, buf)
if err != nil {
return nil, err
}
return buf.String(), nil
}
if binaryEncoder, ok := src.(BinaryEncoder); ok {
_, err := binaryEncoder.EncodeBinary(ci, buf)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
return nil, errors.New("cannot convert to database/sql compatible value")
}