MCPcopy Index your code
hub / github.com/sqlc-dev/sqlc / IntrospectSchema

Method IntrospectSchema

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

IntrospectSchema queries the database to build a catalog containing tables, columns, and enum types for the specified schemas.

(ctx context.Context, schemas []string)

Source from the content-addressed store, hash-verified

390// IntrospectSchema queries the database to build a catalog containing
391// tables, columns, and enum types for the specified schemas.
392func (a *Analyzer) IntrospectSchema(ctx context.Context, schemas []string) (*catalog.Catalog, error) {
393 if a.pool == nil {
394 return nil, fmt.Errorf("database connection not initialized")
395 }
396
397 c, err := a.pool.Acquire(ctx)
398 if err != nil {
399 return nil, err
400 }
401 defer c.Release()
402
403 // Query tables and columns
404 rows, err := c.Query(ctx, introspectTablesQuery, schemas)
405 if err != nil {
406 return nil, fmt.Errorf("introspect tables: %w", err)
407 }
408 columns, err := pgx.CollectRows(rows, pgx.RowToStructByName[introspectedColumn])
409 if err != nil {
410 return nil, fmt.Errorf("collect table rows: %w", err)
411 }
412
413 // Query enums
414 enumRows, err := c.Query(ctx, introspectEnumsQuery, schemas)
415 if err != nil {
416 return nil, fmt.Errorf("introspect enums: %w", err)
417 }
418 enums, err := pgx.CollectRows(enumRows, pgx.RowToStructByName[introspectedEnum])
419 if err != nil {
420 return nil, fmt.Errorf("collect enum rows: %w", err)
421 }
422
423 // Build catalog
424 cat := &catalog.Catalog{
425 DefaultSchema: "public",
426 SearchPath: schemas,
427 }
428
429 // Create schema map for quick lookup
430 schemaMap := make(map[string]*catalog.Schema)
431 for _, schemaName := range schemas {
432 schema := &catalog.Schema{Name: schemaName}
433 cat.Schemas = append(cat.Schemas, schema)
434 schemaMap[schemaName] = schema
435 }
436
437 // Group columns by table
438 tableMap := make(map[string]*catalog.Table)
439 for _, col := range columns {
440 key := col.SchemaName + "." + col.TableName
441 tbl, exists := tableMap[key]
442 if !exists {
443 tbl = &catalog.Table{
444 Rel: &ast.TableName{
445 Schema: col.SchemaName,
446 Name: col.TableName,
447 },
448 }
449 tableMap[key] = tbl

Callers

nothing calls this directly

Implementers 3

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

Calls 2

parseTypeFunction · 0.85
QueryMethod · 0.65

Tested by

no test coverage detected