Expose stdlib.Conn.Conn() to enable database/sql.Conn.Raw()

pull/768/head
Jack Christensen 2020-05-28 17:36:29 -05:00
parent e6c101413b
commit 2bd26ec7fa
2 changed files with 20 additions and 0 deletions

View File

@ -257,6 +257,11 @@ type Conn struct {
connConfig pgx.ConnConfig
}
// Conn returns the underlying *pgx.Conn
func (c *Conn) Conn() *pgx.Conn {
return c.conn
}
func (c *Conn) Prepare(query string) (driver.Stmt, error) {
return c.PrepareContext(context.Background(), query)
}

View File

@ -561,6 +561,21 @@ func TestAcquireConn(t *testing.T) {
})
}
func TestConnRaw(t *testing.T) {
testWithAndWithoutPreferSimpleProtocol(t, func(t *testing.T, db *sql.DB) {
conn, err := db.Conn(context.Background())
require.NoError(t, err)
var n int
err = conn.Raw(func(driverConn interface{}) error {
conn := driverConn.(*stdlib.Conn).Conn()
return conn.QueryRow(context.Background(), "select 42").Scan(&n)
})
require.NoError(t, err)
assert.EqualValues(t, 42, n)
})
}
// https://github.com/jackc/pgx/issues/673
func TestReleaseConnWithTxInProgress(t *testing.T) {
testWithAndWithoutPreferSimpleProtocol(t, func(t *testing.T, db *sql.DB) {