mirror of https://github.com/jackc/pgx.git
Support new pgtype format preferences
parent
aabe5538a8
commit
38dd42de4b
|
@ -21,7 +21,7 @@ func (eqb *extendedQueryBuilder) AppendParam(ci *pgtype.ConnInfo, oid uint32, ar
|
||||||
f := chooseParameterFormatCode(ci, oid, arg)
|
f := chooseParameterFormatCode(ci, oid, arg)
|
||||||
eqb.paramFormats = append(eqb.paramFormats, f)
|
eqb.paramFormats = append(eqb.paramFormats, f)
|
||||||
|
|
||||||
v, err := eqb.encodeExtendedParamValue(ci, oid, arg)
|
v, err := eqb.encodeExtendedParamValue(ci, oid, f, arg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -62,7 +62,7 @@ func (eqb *extendedQueryBuilder) Reset() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (eqb *extendedQueryBuilder) encodeExtendedParamValue(ci *pgtype.ConnInfo, oid uint32, arg interface{}) ([]byte, error) {
|
func (eqb *extendedQueryBuilder) encodeExtendedParamValue(ci *pgtype.ConnInfo, oid uint32, formatCode int16, arg interface{}) ([]byte, error) {
|
||||||
if arg == nil {
|
if arg == nil {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
@ -82,36 +82,41 @@ func (eqb *extendedQueryBuilder) encodeExtendedParamValue(ci *pgtype.ConnInfo, o
|
||||||
var buf []byte
|
var buf []byte
|
||||||
pos := len(eqb.paramValueBytes)
|
pos := len(eqb.paramValueBytes)
|
||||||
|
|
||||||
switch arg := arg.(type) {
|
if arg, ok := arg.(string); ok {
|
||||||
case pgtype.BinaryEncoder:
|
|
||||||
buf, err = arg.EncodeBinary(ci, eqb.paramValueBytes)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if buf == nil {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
eqb.paramValueBytes = buf
|
|
||||||
return eqb.paramValueBytes[pos:], nil
|
|
||||||
case pgtype.TextEncoder:
|
|
||||||
buf, err = arg.EncodeText(ci, eqb.paramValueBytes)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if buf == nil {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
eqb.paramValueBytes = buf
|
|
||||||
return eqb.paramValueBytes[pos:], nil
|
|
||||||
case string:
|
|
||||||
return []byte(arg), nil
|
return []byte(arg), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if formatCode == TextFormatCode {
|
||||||
|
if arg, ok := arg.(pgtype.TextEncoder); ok {
|
||||||
|
buf, err = arg.EncodeText(ci, eqb.paramValueBytes)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if buf == nil {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
eqb.paramValueBytes = buf
|
||||||
|
return eqb.paramValueBytes[pos:], nil
|
||||||
|
}
|
||||||
|
} else if formatCode == BinaryFormatCode {
|
||||||
|
if arg, ok := arg.(pgtype.BinaryEncoder); ok {
|
||||||
|
buf, err = arg.EncodeBinary(ci, eqb.paramValueBytes)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if buf == nil {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
eqb.paramValueBytes = buf
|
||||||
|
return eqb.paramValueBytes[pos:], nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if argIsPtr {
|
if argIsPtr {
|
||||||
// We have already checked that arg is not pointing to nil,
|
// We have already checked that arg is not pointing to nil,
|
||||||
// so it is safe to dereference here.
|
// so it is safe to dereference here.
|
||||||
arg = refVal.Elem().Interface()
|
arg = refVal.Elem().Interface()
|
||||||
return eqb.encodeExtendedParamValue(ci, oid, arg)
|
return eqb.encodeExtendedParamValue(ci, oid, formatCode, arg)
|
||||||
}
|
}
|
||||||
|
|
||||||
if dt, ok := ci.DataTypeForOID(oid); ok {
|
if dt, ok := ci.DataTypeForOID(oid); ok {
|
||||||
|
@ -124,14 +129,14 @@ func (eqb *extendedQueryBuilder) encodeExtendedParamValue(ci *pgtype.ConnInfo, o
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return eqb.encodeExtendedParamValue(ci, oid, v)
|
return eqb.encodeExtendedParamValue(ci, oid, formatCode, v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return eqb.encodeExtendedParamValue(ci, oid, value)
|
return eqb.encodeExtendedParamValue(ci, oid, formatCode, value)
|
||||||
}
|
}
|
||||||
|
|
||||||
// There is no data type registered for the destination OID, but maybe there is data type registered for the arg
|
// There is no data type registered for the destination OID, but maybe there is data type registered for the arg
|
||||||
|
@ -157,7 +162,7 @@ func (eqb *extendedQueryBuilder) encodeExtendedParamValue(ci *pgtype.ConnInfo, o
|
||||||
}
|
}
|
||||||
|
|
||||||
if strippedArg, ok := stripNamedType(&refVal); ok {
|
if strippedArg, ok := stripNamedType(&refVal); ok {
|
||||||
return eqb.encodeExtendedParamValue(ci, oid, strippedArg)
|
return eqb.encodeExtendedParamValue(ci, oid, formatCode, strippedArg)
|
||||||
}
|
}
|
||||||
return nil, SerializationError(fmt.Sprintf("Cannot encode %T into oid %v - %T must implement Encoder or be converted to a string", arg, oid, arg))
|
return nil, SerializationError(fmt.Sprintf("Cannot encode %T into oid %v - %T must implement Encoder or be converted to a string", arg, oid, arg))
|
||||||
}
|
}
|
||||||
|
|
2
go.mod
2
go.mod
|
@ -9,7 +9,7 @@ require (
|
||||||
github.com/jackc/pgconn v1.5.0
|
github.com/jackc/pgconn v1.5.0
|
||||||
github.com/jackc/pgio v1.0.0
|
github.com/jackc/pgio v1.0.0
|
||||||
github.com/jackc/pgproto3/v2 v2.0.1
|
github.com/jackc/pgproto3/v2 v2.0.1
|
||||||
github.com/jackc/pgtype v1.3.1-0.20200510045248-7e66ab1e146c
|
github.com/jackc/pgtype v1.3.1-0.20200510190516-8cd94a14c75a
|
||||||
github.com/jackc/puddle v1.1.1
|
github.com/jackc/puddle v1.1.1
|
||||||
github.com/rs/zerolog v1.15.0
|
github.com/rs/zerolog v1.15.0
|
||||||
github.com/shopspring/decimal v0.0.0-20200227202807-02e2044944cc
|
github.com/shopspring/decimal v0.0.0-20200227202807-02e2044944cc
|
||||||
|
|
2
go.sum
2
go.sum
|
@ -59,6 +59,8 @@ github.com/jackc/pgtype v1.3.1-0.20200508211315-97bbe6ae20e2 h1:Y6cErz3hUojOwnjU
|
||||||
github.com/jackc/pgtype v1.3.1-0.20200508211315-97bbe6ae20e2/go.mod h1:vaogEUkALtxZMCH411K+tKzNpwzCKU+AnPzBKZ+I+Po=
|
github.com/jackc/pgtype v1.3.1-0.20200508211315-97bbe6ae20e2/go.mod h1:vaogEUkALtxZMCH411K+tKzNpwzCKU+AnPzBKZ+I+Po=
|
||||||
github.com/jackc/pgtype v1.3.1-0.20200510045248-7e66ab1e146c h1:id5j6vOwHhbR7BYdGyb0sDMQjNsKTO+mXWaJxiwKu5M=
|
github.com/jackc/pgtype v1.3.1-0.20200510045248-7e66ab1e146c h1:id5j6vOwHhbR7BYdGyb0sDMQjNsKTO+mXWaJxiwKu5M=
|
||||||
github.com/jackc/pgtype v1.3.1-0.20200510045248-7e66ab1e146c/go.mod h1:vaogEUkALtxZMCH411K+tKzNpwzCKU+AnPzBKZ+I+Po=
|
github.com/jackc/pgtype v1.3.1-0.20200510045248-7e66ab1e146c/go.mod h1:vaogEUkALtxZMCH411K+tKzNpwzCKU+AnPzBKZ+I+Po=
|
||||||
|
github.com/jackc/pgtype v1.3.1-0.20200510190516-8cd94a14c75a h1:XUNeoL8E15IgWouQ8gfA6EPHOfTqVetdxBhAKMYKNGo=
|
||||||
|
github.com/jackc/pgtype v1.3.1-0.20200510190516-8cd94a14c75a/go.mod h1:vaogEUkALtxZMCH411K+tKzNpwzCKU+AnPzBKZ+I+Po=
|
||||||
github.com/jackc/pgx/v4 v4.0.0-20190420224344-cc3461e65d96/go.mod h1:mdxmSJJuR08CZQyj1PVQBHy9XOp5p8/SHH6a0psbY9Y=
|
github.com/jackc/pgx/v4 v4.0.0-20190420224344-cc3461e65d96/go.mod h1:mdxmSJJuR08CZQyj1PVQBHy9XOp5p8/SHH6a0psbY9Y=
|
||||||
github.com/jackc/pgx/v4 v4.0.0-20190421002000-1b8f0016e912/go.mod h1:no/Y67Jkk/9WuGR0JG/JseM9irFbnEPbuWV2EELPNuM=
|
github.com/jackc/pgx/v4 v4.0.0-20190421002000-1b8f0016e912/go.mod h1:no/Y67Jkk/9WuGR0JG/JseM9irFbnEPbuWV2EELPNuM=
|
||||||
github.com/jackc/pgx/v4 v4.0.0-pre1.0.20190824185557-6972a5742186/go.mod h1:X+GQnOEnf1dqHGpw7JmHqHc1NxDoalibchSk9/RWuDc=
|
github.com/jackc/pgx/v4 v4.0.0-pre1.0.20190824185557-6972a5742186/go.mod h1:X+GQnOEnf1dqHGpw7JmHqHc1NxDoalibchSk9/RWuDc=
|
||||||
|
|
|
@ -226,7 +226,9 @@ func encodePreparedStatementArgument(ci *pgtype.ConnInfo, buf []byte, oid uint32
|
||||||
// argument to a prepared statement. It defaults to TextFormatCode if no
|
// argument to a prepared statement. It defaults to TextFormatCode if no
|
||||||
// determination can be made.
|
// determination can be made.
|
||||||
func chooseParameterFormatCode(ci *pgtype.ConnInfo, oid uint32, arg interface{}) int16 {
|
func chooseParameterFormatCode(ci *pgtype.ConnInfo, oid uint32, arg interface{}) int16 {
|
||||||
switch arg.(type) {
|
switch arg := arg.(type) {
|
||||||
|
case pgtype.ParamFormatPreferrer:
|
||||||
|
return arg.PreferredParamFormat()
|
||||||
case pgtype.BinaryEncoder:
|
case pgtype.BinaryEncoder:
|
||||||
return BinaryFormatCode
|
return BinaryFormatCode
|
||||||
case string, *string, pgtype.TextEncoder:
|
case string, *string, pgtype.TextEncoder:
|
||||||
|
|
Loading…
Reference in New Issue