Fix pgtype types that can Set database/sql/driver.driver.Valuer
Bug was chooseParameterFormatCode would see that type could handle binary format so binary format would be chosen. But encodePreparedStatementArgument would see driver.Valuer first and would encode with that -- which is text mode. So the server would receive a text format value when expecting a binary format value. Discovered while investigating #316
This commit is contained in:
@@ -128,12 +128,6 @@ func encodePreparedStatementArgument(ci *pgtype.ConnInfo, buf []byte, oid pgtype
|
||||
pgio.SetInt32(buf[sp:], int32(len(buf[sp:])-4))
|
||||
}
|
||||
return buf, nil
|
||||
case driver.Valuer:
|
||||
v, err := arg.Value()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return encodePreparedStatementArgument(ci, buf, oid, v)
|
||||
case string:
|
||||
buf = pgio.AppendInt32(buf, int32(len(arg)))
|
||||
buf = append(buf, arg...)
|
||||
@@ -154,6 +148,16 @@ func encodePreparedStatementArgument(ci *pgtype.ConnInfo, buf []byte, oid pgtype
|
||||
value := dt.Value
|
||||
err := value.Set(arg)
|
||||
if err != nil {
|
||||
{
|
||||
if arg, ok := arg.(driver.Valuer); ok {
|
||||
v, err := arg.Value()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return encodePreparedStatementArgument(ci, buf, oid, v)
|
||||
}
|
||||
}
|
||||
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -170,6 +174,14 @@ func encodePreparedStatementArgument(ci *pgtype.ConnInfo, buf []byte, oid pgtype
|
||||
return buf, nil
|
||||
}
|
||||
|
||||
if arg, ok := arg.(driver.Valuer); ok {
|
||||
v, err := arg.Value()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return encodePreparedStatementArgument(ci, buf, oid, v)
|
||||
}
|
||||
|
||||
if strippedArg, ok := stripNamedType(&refVal); ok {
|
||||
return encodePreparedStatementArgument(ci, buf, oid, strippedArg)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user