Connect implement driver.Connector interface
(ctx context.Context)
| 253 | |
| 254 | // Connect implement driver.Connector interface |
| 255 | func (c connector) Connect(ctx context.Context) (driver.Conn, error) { |
| 256 | var ( |
| 257 | connConfig pgx.ConnConfig |
| 258 | conn *pgx.Conn |
| 259 | close func(context.Context) error |
| 260 | err error |
| 261 | ) |
| 262 | |
| 263 | if c.pool == nil { |
| 264 | // Create a shallow copy of the config, so that BeforeConnect can safely modify it |
| 265 | connConfig = c.ConnConfig |
| 266 | |
| 267 | if err = c.BeforeConnect(ctx, &connConfig); err != nil { |
| 268 | return nil, err |
| 269 | } |
| 270 | |
| 271 | if conn, err = pgx.ConnectConfig(ctx, &connConfig); err != nil { |
| 272 | return nil, err |
| 273 | } |
| 274 | |
| 275 | if err = c.AfterConnect(ctx, conn); err != nil { |
| 276 | return nil, err |
| 277 | } |
| 278 | |
| 279 | close = conn.Close |
| 280 | } else { |
| 281 | var pconn *pgxpool.Conn |
| 282 | |
| 283 | pconn, err = c.pool.Acquire(ctx) |
| 284 | if err != nil { |
| 285 | return nil, err |
| 286 | } |
| 287 | |
| 288 | conn = pconn.Conn() |
| 289 | |
| 290 | close = func(_ context.Context) error { |
| 291 | pconn.Release() |
| 292 | return nil |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | return &Conn{ |
| 297 | conn: conn, |
| 298 | close: close, |
| 299 | driver: c.driver, |
| 300 | connConfig: connConfig, |
| 301 | resetSessionFunc: c.ResetSession, |
| 302 | shouldPing: c.ShouldPing, |
| 303 | psRefCounts: make(map[*pgconn.StatementDescription]int), |
| 304 | }, nil |
| 305 | } |
| 306 | |
| 307 | // Driver implement driver.Connector interface |
| 308 | func (c connector) Driver() driver.Driver { |