IntrospectSchema queries the database to build a catalog containing tables, columns, and enum types for the specified schemas.
(ctx context.Context, schemas []string)
| 390 | // IntrospectSchema queries the database to build a catalog containing |
| 391 | // tables, columns, and enum types for the specified schemas. |
| 392 | func (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 |
nothing calls this directly
no test coverage detected