Skip to content

Commit c76a650

Browse files
committed
Improve documentation for QueryExecModes
#2157
1 parent f57b285 commit c76a650

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

conn.go

+21-10
Original file line numberDiff line numberDiff line change
@@ -650,21 +650,32 @@ const (
650650
// registered with pgtype.Map.RegisterDefaultPgType. Queries will be rejected that have arguments that are
651651
// unregistered or ambiguous. e.g. A map[string]string may have the PostgreSQL type json or hstore. Modes that know
652652
// the PostgreSQL type can use a map[string]string directly as an argument. This mode cannot.
653+
//
654+
// On rare occasions user defined types may behave differently when encoded in the text format instead of the binary
655+
// format. For example, this could happen if a "type RomanNumeral int32" implements fmt.Stringer to format integers as
656+
// Roman numerals (e.g. 7 is VII). The binary format would properly encode the integer 7 as the binary value for 7.
657+
// But the text format would encode the integer 7 as the string "VII". As QueryExecModeExec uses the text format, it
658+
// is possible that changing query mode from another mode to QueryExecModeExec could change the behavior of the query.
659+
// This should not occur with types pgx supports directly and can be avoided by registering the types with
660+
// pgtype.Map.RegisterDefaultPgType and implementing the appropriate type interfaces. In the cas of RomanNumeral, it
661+
// should implement pgtype.Int64Valuer.
653662
QueryExecModeExec
654663

655-
// Use the simple protocol. Assume the PostgreSQL query parameter types based on the Go type of the arguments.
656-
// Queries are executed in a single round trip. Type mappings can be registered with
657-
// pgtype.Map.RegisterDefaultPgType. Queries will be rejected that have arguments that are unregistered or ambiguous.
658-
// e.g. A map[string]string may have the PostgreSQL type json or hstore. Modes that know the PostgreSQL type can use
659-
// a map[string]string directly as an argument. This mode cannot.
664+
// Use the simple protocol. Assume the PostgreSQL query parameter types based on the Go type of the arguments. Queries
665+
// are executed in a single round trip. Type mappings can be registered with pgtype.Map.RegisterDefaultPgType. Queries
666+
// will be rejected that have arguments that are unregistered or ambiguous. e.g. A map[string]string may have the
667+
// PostgreSQL type json or hstore. Modes that know the PostgreSQL type can use a map[string]string directly as an
668+
// argument. This mode cannot.
660669
//
661-
// QueryExecModeSimpleProtocol should have the user application visible behavior as QueryExecModeExec with minor
662-
// exceptions such as behavior when multiple result returning queries are erroneously sent in a single string.
670+
// QueryExecModeSimpleProtocol should have the user application visible behavior as QueryExecModeExec. This includes
671+
// the warning regarding differences in text format and binary format encoding with user defined types. There may be
672+
// other minor exceptions such as behavior when multiple result returning queries are erroneously sent in a single
673+
// string.
663674
//
664675
// QueryExecModeSimpleProtocol uses client side parameter interpolation. All values are quoted and escaped. Prefer
665-
// QueryExecModeExec over QueryExecModeSimpleProtocol whenever possible. In general QueryExecModeSimpleProtocol
666-
// should only be used if connecting to a proxy server, connection pool server, or non-PostgreSQL server that does
667-
// not support the extended protocol.
676+
// QueryExecModeExec over QueryExecModeSimpleProtocol whenever possible. In general QueryExecModeSimpleProtocol should
677+
// only be used if connecting to a proxy server, connection pool server, or non-PostgreSQL server that does not
678+
// support the extended protocol.
668679
QueryExecModeSimpleProtocol
669680
)
670681

0 commit comments

Comments
 (0)