transformSchemas converts decoded metadata into the response shape for the given include level. Applies sorting and per-schema truncation.
(metadata *databaseMetadata, include string)
| 520 | // transformSchemas converts decoded metadata into the response shape for the |
| 521 | // given include level. Applies sorting and per-schema truncation. |
| 522 | func transformSchemas(metadata *databaseMetadata, include string) []SchemaSection { |
| 523 | // Only bulk-detail modes ask the server for limit+1 and need the "trim to |
| 524 | // schemaTableLimit and flag Truncated" dance. Summary mode is designed to |
| 525 | // return every table because the per-table payload is tiny — applying the |
| 526 | // cap there would silently drop tables from agents' view. |
| 527 | applyTruncation := include == schemaIncludeColumns || include == schemaIncludeDetails |
| 528 | |
| 529 | sections := make([]SchemaSection, 0, len(metadata.Schemas)) |
| 530 | for i := range metadata.Schemas { |
| 531 | schema := &metadata.Schemas[i] |
| 532 | |
| 533 | // Sort tables alphabetically by name. |
| 534 | slices.SortStableFunc(schema.Tables, func(a, b tableMetadata) int { |
| 535 | return strings.Compare(a.Name, b.Name) |
| 536 | }) |
| 537 | |
| 538 | truncated := false |
| 539 | tablesShown := 0 |
| 540 | if applyTruncation && len(schema.Tables) > schemaTableLimit { |
| 541 | // Drop the sentinel 201st entry. |
| 542 | schema.Tables = schema.Tables[:schemaTableLimit] |
| 543 | truncated = true |
| 544 | tablesShown = schemaTableLimit |
| 545 | } |
| 546 | |
| 547 | tables := make([]TableEntry, 0, len(schema.Tables)) |
| 548 | for j := range schema.Tables { |
| 549 | tables = append(tables, buildTableEntry(&schema.Tables[j], include)) |
| 550 | } |
| 551 | |
| 552 | views := make([]string, 0, len(schema.Views)) |
| 553 | for _, v := range schema.Views { |
| 554 | views = append(views, v.Name) |
| 555 | } |
| 556 | slices.Sort(views) |
| 557 | |
| 558 | sections = append(sections, SchemaSection{ |
| 559 | Name: schema.Name, |
| 560 | Tables: tables, |
| 561 | Views: views, |
| 562 | FunctionCount: len(schema.Functions), |
| 563 | ProcedureCount: len(schema.Procedures), |
| 564 | Truncated: truncated, |
| 565 | TablesShown: tablesShown, |
| 566 | }) |
| 567 | } |
| 568 | |
| 569 | // Sort schemas alphabetically by name (empty string sorts first → MySQL friendly). |
| 570 | slices.SortStableFunc(sections, func(a, b SchemaSection) int { |
| 571 | return strings.Compare(a.Name, b.Name) |
| 572 | }) |
| 573 | return sections |
| 574 | } |
| 575 | |
| 576 | // buildTableEntry converts a single tableMetadata into the response shape at the |
| 577 | // requested detail level. |
no test coverage detected