From d9ac491657fdbe09c08e07cc7abff6fe546b7d7d Mon Sep 17 00:00:00 2001 From: Jack Christensen Date: Thu, 4 Mar 2021 19:56:14 -0600 Subject: [PATCH] Add prefer_simple_protocol option to ParseConfig refs #650 --- conn.go | 11 +++++++++++ conn_test.go | 20 ++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/conn.go b/conn.go index ea3d7117..dfe5a74b 100644 --- a/conn.go +++ b/conn.go @@ -160,11 +160,22 @@ func ParseConfig(connString string) (*ConnConfig, error) { } } + preferSimpleProtocol := false + if s, ok := config.RuntimeParams["prefer_simple_protocol"]; ok { + delete(config.RuntimeParams, "prefer_simple_protocol") + if b, err := strconv.ParseBool(s); err == nil { + preferSimpleProtocol = b + } else { + return nil, errors.Errorf("invalid prefer_simple_protocol: %v", err) + } + } + connConfig := &ConnConfig{ Config: *config, createdByParseConfig: true, LogLevel: LogLevelInfo, BuildStatementCache: buildStatementCache, + PreferSimpleProtocol: preferSimpleProtocol, connString: connString, } diff --git a/conn_test.go b/conn_test.go index 984a5114..57f6bf03 100644 --- a/conn_test.go +++ b/conn_test.go @@ -169,6 +169,26 @@ func TestParseConfigExtractsStatementCacheOptions(t *testing.T) { require.Equal(t, stmtcache.ModeDescribe, c.Mode()) } +func TestParseConfigExtractsPreferSimpleProtocol(t *testing.T) { + t.Parallel() + + for _, tt := range []struct { + connString string + preferSimpleProtocol bool + }{ + {"", false}, + {"prefer_simple_protocol=false", false}, + {"prefer_simple_protocol=0", false}, + {"prefer_simple_protocol=true", true}, + {"prefer_simple_protocol=1", true}, + } { + config, err := pgx.ParseConfig(tt.connString) + require.NoError(t, err) + require.Equalf(t, tt.preferSimpleProtocol, config.PreferSimpleProtocol, "connString: `%s`", tt.connString) + require.Empty(t, config.RuntimeParams["prefer_simple_protocol"]) + } +} + func TestExec(t *testing.T) { t.Parallel()