MCPcopy
hub / github.com/chartdb/chartdb / convertToChartDBDiagram

Function convertToChartDBDiagram

src/lib/data/sql-import/common.ts:622–1074  ·  view source on GitHub ↗
(
    parserResult: SQLParserResult,
    sourceDatabaseType: DatabaseType,
    targetDatabaseType: DatabaseType
)

Source from the content-addressed store, hash-verified

620
621// Convert SQLParserResult to ChartDB Diagram structure
622export function convertToChartDBDiagram(
623 parserResult: SQLParserResult,
624 sourceDatabaseType: DatabaseType,
625 targetDatabaseType: DatabaseType
626): Diagram {
627 // Create a mapping of old table IDs to new ones
628 const tableIdMapping = new Map<string, string>();
629
630 // Convert SQL tables to ChartDB tables
631 const tables: DBTable[] = parserResult.tables.map((table, index) => {
632 const row = Math.floor(index / 4);
633 const col = index % 4;
634 const tableSpacing = 300;
635 const newId = generateId();
636 tableIdMapping.set(table.id, newId);
637
638 // Create fields from columns
639 const fields: DBField[] = table.columns.map((column) => {
640 // Use special case handling for specific database types to ensure correct mapping
641 let mappedType: DataType;
642
643 // Detect and handle array types (e.g., int[], text[], varchar[])
644 const isArrayType = column.type.endsWith('[]');
645 const baseColumnType = isArrayType
646 ? column.type.slice(0, -2)
647 : column.type;
648
649 // Create a modified column object with the base type for mapping
650 const columnForMapping = { ...column, type: baseColumnType };
651
652 // SQLite-specific handling for numeric types
653 if (sourceDatabaseType === DatabaseType.SQLITE) {
654 const normalizedType = columnForMapping.type.toLowerCase();
655
656 if (normalizedType === 'integer' || normalizedType === 'int') {
657 // Ensure integer types are preserved
658 mappedType = { id: 'integer', name: 'integer' };
659 } else if (
660 normalizedType === 'real' ||
661 normalizedType === 'float' ||
662 normalizedType === 'double' ||
663 normalizedType === 'numeric' ||
664 normalizedType === 'decimal'
665 ) {
666 // Ensure real types are preserved
667 mappedType = { id: 'real', name: 'real' };
668 } else if (normalizedType === 'blob') {
669 // Ensure blob types are preserved
670 mappedType = { id: 'blob', name: 'blob' };
671 } else {
672 // Use the standard mapping for other types
673 mappedType = mapSQLTypeToGenericType(
674 columnForMapping.type,
675 sourceDatabaseType
676 );
677 }
678 }
679 // Handle MySQL/MariaDB integer types specifically

Calls 5

generateIdFunction · 0.90
supportsCustomTypesFunction · 0.90
getPreferredSynonymFunction · 0.90
generateDiagramIdFunction · 0.90
mapSQLTypeToGenericTypeFunction · 0.85

Tested by

no test coverage detected