You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
42 lines
827 B
42 lines
827 B
2 years ago
|
package pgtype
|
||
|
|
||
|
import (
|
||
|
"database/sql/driver"
|
||
|
"errors"
|
||
|
)
|
||
|
|
||
|
func DatabaseSQLValue(ci *ConnInfo, src Value) (interface{}, error) {
|
||
|
if valuer, ok := src.(driver.Valuer); ok {
|
||
|
return valuer.Value()
|
||
|
}
|
||
|
|
||
|
if textEncoder, ok := src.(TextEncoder); ok {
|
||
|
buf, err := textEncoder.EncodeText(ci, nil)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
return string(buf), nil
|
||
|
}
|
||
|
|
||
|
if binaryEncoder, ok := src.(BinaryEncoder); ok {
|
||
|
buf, err := binaryEncoder.EncodeBinary(ci, nil)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
return buf, nil
|
||
|
}
|
||
|
|
||
|
return nil, errors.New("cannot convert to database/sql compatible value")
|
||
|
}
|
||
|
|
||
|
func EncodeValueText(src TextEncoder) (interface{}, error) {
|
||
|
buf, err := src.EncodeText(nil, make([]byte, 0, 32))
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
if buf == nil {
|
||
|
return nil, nil
|
||
|
}
|
||
|
return string(buf), err
|
||
|
}
|