MCPcopy
hub / github.com/jackc/pgx / connect

Function connect

conn.go:243–297  ·  view source on GitHub ↗

connect connects to a database. connect takes ownership of config. The caller must not use or access it again.

(ctx context.Context, config *ConnConfig)

Source from the content-addressed store, hash-verified

241
242// connect connects to a database. connect takes ownership of config. The caller must not use or access it again.
243func 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.

Callers 3

ConnectFunction · 0.85
ConnectWithOptionsFunction · 0.85
ConnectConfigFunction · 0.85

Calls 5

NewMapFunction · 0.92
ConnectConfigFunction · 0.92
NewLRUCacheFunction · 0.92
TraceConnectStartMethod · 0.65
TraceConnectEndMethod · 0.65

Tested by

no test coverage detected