MCPcopy Index your code
hub / github.com/chartdb/chartdb / mapSQLTypeToGenericType

Function mapSQLTypeToGenericType

src/lib/data/sql-import/common.ts:345–421  ·  view source on GitHub ↗
(
    sqlType: string,
    databaseType?: DatabaseType
)

Source from the content-addressed store, hash-verified

343
344// Map SQL data type to generic data type in our system
345export function mapSQLTypeToGenericType(
346 sqlType: string,
347 databaseType?: DatabaseType
348): DataType {
349 if (!sqlType) {
350 return genericDataTypes.find((t) => t.id === 'text')!;
351 }
352
353 // Normalize the SQL type to lowercase for consistency
354 const normalizedSqlType = sqlType.toLowerCase();
355
356 // Add special case handling for SQLite INTEGER type
357 if (
358 databaseType === DatabaseType.SQLITE &&
359 (normalizedSqlType === 'integer' || normalizedSqlType === 'int')
360 ) {
361 return genericDataTypes.find((t) => t.id === 'integer')!;
362 }
363
364 // Get dialect-specific type mappings
365 const dialectAffinity =
366 (databaseType && typeAffinity[databaseType]) ||
367 typeAffinity[DatabaseType.GENERIC];
368
369 // Handle specific database dialect mappings
370 if (databaseType) {
371 // Try to find a mapping for the normalized type
372 const typeMapping = dialectAffinity[normalizedSqlType];
373 if (typeMapping) {
374 const foundType = genericDataTypes.find(
375 (t) => t.id === typeMapping
376 );
377 if (foundType) return foundType;
378 }
379 }
380
381 // Try direct mapping by normalizing the input type
382 const normalizedType = normalizedSqlType.replace(/\(.*\)/, '');
383 const matchedType = genericDataTypes.find((t) => t.id === normalizedType);
384 if (matchedType) return matchedType;
385
386 // Generic type mappings as a fallback
387 const typeMap: Record<string, string> = {
388 int: 'integer',
389 integer: 'integer',
390 smallint: 'smallint',
391 bigint: 'bigint',
392 decimal: 'decimal',
393 numeric: 'numeric',
394 float: 'float',
395 double: 'double',
396 varchar: 'varchar',
397 'character varying': 'varchar',
398 char: 'char',
399 character: 'char',
400 text: 'text',
401 boolean: 'boolean',
402 bool: 'boolean',

Callers 1

convertToChartDBDiagramFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected