connect connects to a database. connect takes ownership of config. The caller must not use or access it again.
(ctx context.Context, config *ConnConfig)
| 241 | |
| 242 | // connect connects to a database. connect takes ownership of config. The caller must not use or access it again. |
| 243 | func connect(ctx context.Context, config *ConnConfig) (c *Conn, err error) { |
| 244 | if connectTracer, ok := config.Tracer.(ConnectTracer); ok { |
| 245 | ctx = connectTracer.TraceConnectStart(ctx, TraceConnectStartData{ConnConfig: config}) |
| 246 | defer func() { |
| 247 | connectTracer.TraceConnectEnd(ctx, TraceConnectEndData{Conn: c, Err: err}) |
| 248 | }() |
| 249 | } |
| 250 | |
| 251 | // Default values are set in ParseConfig. Enforce initial creation by ParseConfig rather than setting defaults from |
| 252 | // zero values. |
| 253 | if !config.createdByParseConfig { |
| 254 | panic("config must be created by ParseConfig") |
| 255 | } |
| 256 | |
| 257 | c = &Conn{ |
| 258 | config: config, |
| 259 | typeMap: pgtype.NewMap(), |
| 260 | queryTracer: config.Tracer, |
| 261 | } |
| 262 | |
| 263 | if t, ok := c.queryTracer.(BatchTracer); ok { |
| 264 | c.batchTracer = t |
| 265 | } |
| 266 | if t, ok := c.queryTracer.(CopyFromTracer); ok { |
| 267 | c.copyFromTracer = t |
| 268 | } |
| 269 | if t, ok := c.queryTracer.(PrepareTracer); ok { |
| 270 | c.prepareTracer = t |
| 271 | } |
| 272 | |
| 273 | // Only install pgx notification system if no other callback handler is present. |
| 274 | if config.Config.OnNotification == nil { |
| 275 | config.Config.OnNotification = c.bufferNotifications |
| 276 | } |
| 277 | |
| 278 | c.pgConn, err = pgconn.ConnectConfig(ctx, &config.Config) |
| 279 | if err != nil { |
| 280 | return nil, err |
| 281 | } |
| 282 | |
| 283 | c.preparedStatements = make(map[string]*pgconn.StatementDescription) |
| 284 | c.doneChan = make(chan struct{}) |
| 285 | c.closedChan = make(chan error) |
| 286 | c.wbuf = make([]byte, 0, 1024) |
| 287 | |
| 288 | if c.config.StatementCacheCapacity > 0 { |
| 289 | c.statementCache = stmtcache.NewLRUCache(c.config.StatementCacheCapacity) |
| 290 | } |
| 291 | |
| 292 | if c.config.DescriptionCacheCapacity > 0 { |
| 293 | c.descriptionCache = stmtcache.NewLRUCache(c.config.DescriptionCacheCapacity) |
| 294 | } |
| 295 | |
| 296 | return c, nil |
| 297 | } |
| 298 | |
| 299 | // Close closes a connection. It is safe to call Close on an already closed |
| 300 | // connection. |
no test coverage detected