From 44de49ffa1adacf3b2a30f81877e84755453ec14 Mon Sep 17 00:00:00 2001 From: Jack Christensen Date: Fri, 28 Dec 2018 12:25:59 -0600 Subject: [PATCH] Rename runtime params to parameter status --- base/conn.go | 22 ++++++++++++++-------- conn.go | 4 +++- query.go | 4 ++-- v4.md | 4 ++-- 4 files changed, 21 insertions(+), 13 deletions(-) diff --git a/base/conn.go b/base/conn.go index 99278839..b8867c18 100644 --- a/base/conn.go +++ b/base/conn.go @@ -103,12 +103,12 @@ func (cc *ConnConfig) assignDefaults() error { // PgConn is a low-level PostgreSQL connection handle. It is not safe for concurrent usage. type PgConn struct { - NetConn net.Conn // the underlying TCP or unix domain socket connection - PID uint32 // backend pid - SecretKey uint32 // key to use to send a cancel query message to the server - RuntimeParams map[string]string // parameters that have been reported by the server - TxStatus byte - Frontend *pgproto3.Frontend + NetConn net.Conn // the underlying TCP or unix domain socket connection + PID uint32 // backend pid + SecretKey uint32 // key to use to send a cancel query message to the server + parameterStatuses map[string]string // parameters that have been reported by the server + TxStatus byte + Frontend *pgproto3.Frontend Config ConnConfig } @@ -127,7 +127,7 @@ func Connect(cc ConnConfig) (*PgConn, error) { return nil, err } - pgConn.RuntimeParams = make(map[string]string) + pgConn.parameterStatuses = make(map[string]string) if cc.TLSConfig != nil { if err := pgConn.startTLS(cc.TLSConfig); err != nil { @@ -260,8 +260,14 @@ func (pgConn *PgConn) ReceiveMessage() (pgproto3.BackendMessage, error) { case *pgproto3.ReadyForQuery: pgConn.TxStatus = msg.TxStatus case *pgproto3.ParameterStatus: - pgConn.RuntimeParams[msg.Name] = msg.Value + pgConn.parameterStatuses[msg.Name] = msg.Value } return msg, nil } + +// ParameterStatus returns the value of a parameter reported by the server (e.g. +// server_version). Returns an empty string for unknown parameters. +func (pgConn *PgConn) ParameterStatus(key string) string { + return pgConn.parameterStatuses[key] +} diff --git a/conn.go b/conn.go index a202fa4b..e550be98 100644 --- a/conn.go +++ b/conn.go @@ -933,8 +933,10 @@ func configTLS(args configTLSArgs, cc *ConnConfig) error { return nil } +// ParameterStatus returns the value of a parameter reported by the server (e.g. +// server_version). Returns an empty string for unknown parameters. func (c *Conn) ParameterStatus(key string) string { - return c.pgConn.RuntimeParams[key] + return c.pgConn.ParameterStatus(key) } // Prepare creates a prepared statement with name and sql. sql can contain placeholders diff --git a/query.go b/query.go index b3bb56e3..0f1152c1 100644 --- a/query.go +++ b/query.go @@ -519,11 +519,11 @@ func (c *Conn) readUntilRowDescription() ([]FieldDescription, error) { } func (c *Conn) sanitizeAndSendSimpleQuery(sql string, args ...interface{}) (err error) { - if c.pgConn.RuntimeParams["standard_conforming_strings"] != "on" { + if c.pgConn.ParameterStatus("standard_conforming_strings") != "on" { return errors.New("simple protocol queries must be run with standard_conforming_strings=on") } - if c.pgConn.RuntimeParams["client_encoding"] != "UTF8" { + if c.pgConn.ParameterStatus("client_encoding") != "UTF8" { return errors.New("simple protocol queries must be run with client_encoding=UTF8") } diff --git a/v4.md b/v4.md index 50f76d56..63f5202b 100644 --- a/v4.md +++ b/v4.md @@ -34,8 +34,8 @@ Minor Potential Changes: ## Changes -`base.Conn` now contains core PostgreSQL connection functionality. +`base.PgConn` now contains core PostgreSQL connection functionality. ### Incompatible Changes -* `RuntimeParams` removed from `pgx.Conn` and added to `base.Conn` +* `RuntimeParams` `pgx.Conn`. Server reported status can now be queried with the `ParameterStatus` method. The rename aligns with the PostgreSQL protocol and standard libpq naming. Access via a method instead of direct access to the map protects against outside modification.