MCPcopy
hub / github.com/sqlc-dev/sqlc / EnsureConn

Method EnsureConn

internal/engine/postgresql/analyzer/analyze.go:487–521  ·  view source on GitHub ↗

EnsureConn initializes the database connection pool if not already done. This is useful for database-only mode where we need to connect before analyzing queries.

(ctx context.Context, migrations []string)

Source from the content-addressed store, hash-verified

485// EnsureConn initializes the database connection pool if not already done.
486// This is useful for database-only mode where we need to connect before analyzing queries.
487func (a *Analyzer) EnsureConn(ctx context.Context, migrations []string) error {
488 if a.pool != nil {
489 return nil
490 }
491
492 var uri string
493 if a.db.Managed {
494 if a.client == nil {
495 return fmt.Errorf("client is nil")
496 }
497 edb, err := a.client.CreateDatabase(ctx, &dbmanager.CreateDatabaseRequest{
498 Engine: "postgresql",
499 Migrations: migrations,
500 })
501 if err != nil {
502 return err
503 }
504 uri = edb.Uri
505 } else if a.dbg.OnlyManagedDatabases {
506 return fmt.Errorf("database: connections disabled via SQLCDEBUG=databases=managed")
507 } else {
508 uri = a.replacer.Replace(a.db.URI)
509 }
510
511 conf, err := pgxpool.ParseConfig(uri)
512 if err != nil {
513 return err
514 }
515 pool, err := pgxpool.NewWithConfig(ctx, conf)
516 if err != nil {
517 return err
518 }
519 a.pool = pool
520 return nil
521}
522
523// GetColumnNames implements the expander.ColumnGetter interface.
524// It prepares a query and returns the column names from the result set description.

Callers

nothing calls this directly

Implementers 3

CachedAnalyzerinternal/analyzer/analyzer.go
Analyzerinternal/engine/postgresql/analyzer/an
Analyzerinternal/engine/sqlite/analyzer/analyz

Calls 2

CreateDatabaseMethod · 0.65
ReplaceMethod · 0.45

Tested by

no test coverage detected