2
0

Add OpenFromConnPool

This commit is contained in:
Jack Christensen
2014-06-21 11:34:56 -05:00
parent eb85aad21f
commit 09b9964ca7
2 changed files with 67 additions and 2 deletions
+28 -1
View File
@@ -8,12 +8,16 @@ import (
"io"
)
var openFromConnPoolCount int
func init() {
d := &Driver{}
sql.Register("pgx", d)
}
type Driver struct{}
type Driver struct {
Pool *pgx.ConnPool
}
func (d *Driver) Open(name string) (driver.Conn, error) {
connConfig, err := pgx.ParseURI(name)
@@ -30,6 +34,29 @@ func (d *Driver) Open(name string) (driver.Conn, error) {
return c, nil
}
// OpenFromConnPool takes the existing *pgx.ConnPool pool and returns a *sql.DB
// with pool as the backend. This enables full control over the connection
// process and configuration while maintaining compatibility with the
// database/sql interface. In addition, by calling Driver() on the returned
// *sql.DB and typecasting to *stdlib.Driver a reference to the pgx.ConnPool can
// be reaquired later. This allows fast paths targeting pgx to be used while
// still maintaining compatibility with other databases and drivers.
func OpenFromConnPool(pool *pgx.ConnPool) (*sql.DB, error) {
d := &Driver{Pool: pool}
name := fmt.Sprintf("pgx-%d", openFromConnPoolCount)
openFromConnPoolCount++
sql.Register(name, d)
db, err := sql.Open(name, "")
if err != nil {
return nil, err
}
db.SetMaxIdleConns(0)
db.SetMaxOpenConns(pool.MaxConnectionCount())
return db, nil
}
type Conn struct {
conn *pgx.Conn
psCount int64 // Counter used for creating unique prepared statement names