2
0

Clean up old Go 1.10 build tags

pgx requires Go modules which requires at least Go 1.11 so there is no
use in build tags to support older Go versions.
This commit is contained in:
Jack Christensen
2020-05-08 12:18:09 -05:00
parent c03ac1519e
commit c44cda4bb4
5 changed files with 62 additions and 104 deletions
+53
View File
@@ -114,6 +114,59 @@ var (
fakeTxConns map[*pgx.Conn]*sql.Tx
)
// OptionOpenDB options for configuring the driver when opening a new db pool.
type OptionOpenDB func(*connector)
// OptionAfterConnect provide a callback for after connect.
func OptionAfterConnect(ac func(context.Context, *pgx.Conn) error) OptionOpenDB {
return func(dc *connector) {
dc.AfterConnect = ac
}
}
func OpenDB(config pgx.ConnConfig, opts ...OptionOpenDB) *sql.DB {
c := connector{
ConnConfig: config,
AfterConnect: func(context.Context, *pgx.Conn) error { return nil }, // noop after connect by default
driver: pgxDriver,
}
for _, opt := range opts {
opt(&c)
}
return sql.OpenDB(c)
}
type connector struct {
pgx.ConnConfig
AfterConnect func(context.Context, *pgx.Conn) error // function to call on every new connection
driver *Driver
}
// Connect implement driver.Connector interface
func (c connector) Connect(ctx context.Context) (driver.Conn, error) {
var (
err error
conn *pgx.Conn
)
if conn, err = pgx.ConnectConfig(ctx, &c.ConnConfig); err != nil {
return nil, err
}
if err = c.AfterConnect(ctx, conn); err != nil {
return nil, err
}
return &Conn{conn: conn, driver: c.driver, connConfig: c.ConnConfig}, nil
}
// Driver implement driver.Connector interface
func (c connector) Driver() driver.Driver {
return c.driver
}
// GetDefaultDriver returns the driver initialized in the init function
// and used when the pgx driver is registered.
func GetDefaultDriver() driver.Driver {