Skip to content

Commit 7634877

Browse files
committed
Make Conn.ConnInfo private
1 parent a8691a7 commit 7634877

9 files changed

+25
-58
lines changed

Diff for: conn.go

+13-10
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ type Conn struct {
6262
doneChan chan struct{}
6363
closedChan chan error
6464

65-
ConnInfo *pgtype.ConnInfo
65+
connInfo *pgtype.ConnInfo
6666

6767
wbuf []byte
6868
preallocatedRows []connRows
@@ -171,7 +171,7 @@ func connect(ctx context.Context, config *ConnConfig) (c *Conn, err error) {
171171

172172
c = &Conn{
173173
config: config,
174-
ConnInfo: pgtype.NewConnInfo(),
174+
connInfo: pgtype.NewConnInfo(),
175175
logLevel: config.LogLevel,
176176
logger: config.Logger,
177177
}
@@ -408,6 +408,9 @@ func (c *Conn) PgConn() *pgconn.PgConn { return c.pgConn }
408408
// StatementCache returns the statement cache used for this connection.
409409
func (c *Conn) StatementCache() stmtcache.Cache { return c.stmtcache }
410410

411+
// ConnInfo returns the connection info used for this connection.
412+
func (c *Conn) ConnInfo() *pgtype.ConnInfo { return c.connInfo }
413+
411414
// Exec executes sql. sql can be either a prepared statement name or an SQL string. arguments should be referenced
412415
// positionally from the sql string as $1, $2, etc.
413416
func (c *Conn) Exec(ctx context.Context, sql string, arguments ...interface{}) (pgconn.CommandTag, error) {
@@ -499,14 +502,14 @@ func (c *Conn) execParamsAndPreparedPrefix(sd *pgconn.StatementDescription, argu
499502
}
500503

501504
for i := range args {
502-
err = c.eqb.AppendParam(c.ConnInfo, sd.ParamOIDs[i], args[i])
505+
err = c.eqb.AppendParam(c.connInfo, sd.ParamOIDs[i], args[i])
503506
if err != nil {
504507
return err
505508
}
506509
}
507510

508511
for i := range sd.Fields {
509-
c.eqb.AppendResultFormat(c.ConnInfo.ResultFormatCodeForOID(sd.Fields[i].DataTypeOID))
512+
c.eqb.AppendResultFormat(c.ConnInfo().ResultFormatCodeForOID(sd.Fields[i].DataTypeOID))
510513
}
511514

512515
return nil
@@ -542,7 +545,7 @@ func (c *Conn) getRows(ctx context.Context, sql string, args []interface{}) *con
542545

543546
r.ctx = ctx
544547
r.logger = c
545-
r.connInfo = c.ConnInfo
548+
r.connInfo = c.connInfo
546549
r.startTime = time.Now()
547550
r.sql = sql
548551
r.args = args
@@ -642,7 +645,7 @@ optionLoop:
642645
}
643646

644647
for i := range args {
645-
err = c.eqb.AppendParam(c.ConnInfo, sd.ParamOIDs[i], args[i])
648+
err = c.eqb.AppendParam(c.connInfo, sd.ParamOIDs[i], args[i])
646649
if err != nil {
647650
rows.fatal(err)
648651
return rows, rows.err
@@ -658,7 +661,7 @@ optionLoop:
658661

659662
if resultFormats == nil {
660663
for i := range sd.Fields {
661-
c.eqb.AppendResultFormat(c.ConnInfo.ResultFormatCodeForOID(sd.Fields[i].DataTypeOID))
664+
c.eqb.AppendResultFormat(c.ConnInfo().ResultFormatCodeForOID(sd.Fields[i].DataTypeOID))
662665
}
663666

664667
resultFormats = c.eqb.resultFormats
@@ -732,14 +735,14 @@ func (c *Conn) SendBatch(ctx context.Context, b *Batch) BatchResults {
732735
}
733736

734737
for i := range args {
735-
err = c.eqb.AppendParam(c.ConnInfo, sd.ParamOIDs[i], args[i])
738+
err = c.eqb.AppendParam(c.connInfo, sd.ParamOIDs[i], args[i])
736739
if err != nil {
737740
return &batchResults{ctx: ctx, conn: c, err: err}
738741
}
739742
}
740743

741744
for i := range sd.Fields {
742-
c.eqb.AppendResultFormat(c.ConnInfo.ResultFormatCodeForOID(sd.Fields[i].DataTypeOID))
745+
c.eqb.AppendResultFormat(c.ConnInfo().ResultFormatCodeForOID(sd.Fields[i].DataTypeOID))
743746
}
744747

745748
if sd.Name == "" {
@@ -770,7 +773,7 @@ func (c *Conn) sanitizeForSimpleQuery(sql string, args ...interface{}) (string,
770773
var err error
771774
valueArgs := make([]interface{}, len(args))
772775
for i, a := range args {
773-
valueArgs[i], err = convertSimpleArgument(c.ConnInfo, a)
776+
valueArgs[i], err = convertSimpleArgument(c.connInfo, a)
774777
if err != nil {
775778
return "", err
776779
}

Diff for: conn_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -803,11 +803,11 @@ func TestConnInitConnInfo(t *testing.T) {
803803
"text": pgtype.TextOID,
804804
}
805805
for name, oid := range nameOIDs {
806-
dtByName, ok := conn.ConnInfo.DataTypeForName(name)
806+
dtByName, ok := conn.ConnInfo().DataTypeForName(name)
807807
if !ok {
808808
t.Fatalf("Expected type named %v to be present", name)
809809
}
810-
dtByOID, ok := conn.ConnInfo.DataTypeForOID(oid)
810+
dtByOID, ok := conn.ConnInfo().DataTypeForOID(oid)
811811
if !ok {
812812
t.Fatalf("Expected type OID %v to be present", oid)
813813
}
@@ -866,7 +866,7 @@ func TestDomainType(t *testing.T) {
866866
if err != nil {
867867
t.Fatalf("did not find uint64 OID, %v", err)
868868
}
869-
conn.ConnInfo.RegisterDataType(pgtype.DataType{Value: &pgtype.Numeric{}, Name: "uint64", OID: uint64OID})
869+
conn.ConnInfo().RegisterDataType(pgtype.DataType{Value: &pgtype.Numeric{}, Name: "uint64", OID: uint64OID})
870870

871871
// String is still an acceptable argument after registration
872872
err = conn.QueryRow(context.Background(), "select $1::uint64", "7").Scan(&n)

Diff for: copy_from.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ func (ct *copyFrom) buildCopyBuf(buf []byte, sd *pgconn.StatementDescription) (b
130130

131131
buf = pgio.AppendInt16(buf, int16(len(ct.columnNames)))
132132
for i, val := range values {
133-
buf, err = encodePreparedStatementArgument(ct.conn.ConnInfo, buf, sd.Fields[i].DataTypeOID, val)
133+
buf, err = encodePreparedStatementArgument(ct.conn.connInfo, buf, sd.Fields[i].DataTypeOID, val)
134134
if err != nil {
135135
return false, nil, err
136136
}

Diff for: copy_from_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ func TestConnCopyFromJSON(t *testing.T) {
133133
defer closeConn(t, conn)
134134

135135
for _, typeName := range []string{"json", "jsonb"} {
136-
if _, ok := conn.ConnInfo.DataTypeForName(typeName); !ok {
136+
if _, ok := conn.ConnInfo().DataTypeForName(typeName); !ok {
137137
return // No JSON/JSONB type -- must be running against old PostgreSQL
138138
}
139139
}

Diff for: example_custom_type_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func Example_CustomType() {
8181
}
8282

8383
// Override registered handler for point
84-
conn.ConnInfo.RegisterDataType(pgtype.DataType{
84+
conn.ConnInfo().RegisterDataType(pgtype.DataType{
8585
Value: &Point{},
8686
Name: "point",
8787
OID: 600,

Diff for: query_test.go

+2-38
Original file line numberDiff line numberDiff line change
@@ -965,42 +965,6 @@ func TestQueryRowCoreByteSlice(t *testing.T) {
965965
}
966966
}
967967

968-
func TestQueryRowUnknownType(t *testing.T) {
969-
t.Parallel()
970-
971-
conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE"))
972-
defer closeConn(t, conn)
973-
974-
// Clear existing type mappings
975-
conn.ConnInfo = pgtype.NewConnInfo()
976-
conn.ConnInfo.RegisterDataType(pgtype.DataType{
977-
Value: &pgtype.GenericText{},
978-
Name: "point",
979-
OID: 600,
980-
})
981-
conn.ConnInfo.RegisterDataType(pgtype.DataType{
982-
Value: &pgtype.Int4{},
983-
Name: "int4",
984-
OID: pgtype.Int4OID,
985-
})
986-
987-
sql := "select $1::point"
988-
expected := "(1,0)"
989-
var actual string
990-
991-
err := conn.QueryRow(context.Background(), sql, expected).Scan(&actual)
992-
if err != nil {
993-
t.Errorf("Unexpected failure: %v (sql -> %v)", err, sql)
994-
}
995-
996-
if actual != expected {
997-
t.Errorf(`Expected "%v", got "%v" (sql -> %v)`, expected, actual, sql)
998-
999-
}
1000-
1001-
ensureConnValid(t, conn)
1002-
}
1003-
1004968
func TestQueryRowErrors(t *testing.T) {
1005969
t.Parallel()
1006970

@@ -1197,7 +1161,7 @@ func TestConnQueryDatabaseSQLDriverValuerWithBinaryPgTypeThatAcceptsSameType(t *
11971161
conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE"))
11981162
defer closeConn(t, conn)
11991163

1200-
conn.ConnInfo.RegisterDataType(pgtype.DataType{
1164+
conn.ConnInfo().RegisterDataType(pgtype.DataType{
12011165
Value: &satori.UUID{},
12021166
Name: "uuid",
12031167
OID: 2950,
@@ -1413,7 +1377,7 @@ func TestRowsFromResultReader(t *testing.T) {
14131377

14141378
var sum, rowCount int32
14151379

1416-
rows := pgx.RowsFromResultReader(conn.ConnInfo, resultReader)
1380+
rows := pgx.RowsFromResultReader(conn.ConnInfo(), resultReader)
14171381
defer rows.Close()
14181382

14191383
for rows.Next() {

Diff for: rows.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ func (rows *connRows) Scan(dest ...interface{}) error {
188188
continue
189189
}
190190

191-
err := rows.connInfo.Scan(uint32(fd.DataTypeOID), fd.Format, buf, d)
191+
err := rows.connInfo.Scan(fd.DataTypeOID, fd.Format, buf, d)
192192
if err != nil {
193193
rows.fatal(scanArgError{col: i, err: err})
194194
return err
@@ -214,7 +214,7 @@ func (rows *connRows) Values() ([]interface{}, error) {
214214
continue
215215
}
216216

217-
if dt, ok := rows.connInfo.DataTypeForOID(uint32(fd.DataTypeOID)); ok {
217+
if dt, ok := rows.connInfo.DataTypeForOID(fd.DataTypeOID); ok {
218218
value := reflect.New(reflect.ValueOf(dt.Value).Elem().Type()).Interface().(pgtype.Value)
219219

220220
switch fd.Format {

Diff for: stdlib/sql.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ func (r *Rows) Columns() []string {
310310

311311
// ColumnTypeDatabaseTypeName return the database system type name.
312312
func (r *Rows) ColumnTypeDatabaseTypeName(index int) string {
313-
if dt, ok := r.conn.conn.ConnInfo.DataTypeForOID(r.rows.FieldDescriptions()[index].DataTypeOID); ok {
313+
if dt, ok := r.conn.conn.ConnInfo().DataTypeForOID(r.rows.FieldDescriptions()[index].DataTypeOID); ok {
314314
return strings.ToUpper(dt.Name)
315315
}
316316

Diff for: values_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func TestJSONAndJSONBTranscode(t *testing.T) {
8080
defer closeConn(t, conn)
8181

8282
for _, typename := range []string{"json", "jsonb"} {
83-
if _, ok := conn.ConnInfo.DataTypeForName(typename); !ok {
83+
if _, ok := conn.ConnInfo().DataTypeForName(typename); !ok {
8484
continue // No JSON/JSONB type -- must be running against old PostgreSQL
8585
}
8686

0 commit comments

Comments
 (0)