UNPKG

rawsql-ts

Version:

High-performance SQL parser and AST analyzer written in TypeScript. Provides fast parsing and advanced transformation capabilities.

117 lines 5.06 kB
import { normalizeTableName } from './TableNameUtils'; /** * Build a direction-aware relation graph from parsed CREATE TABLE statements. */ export function buildRelationGraphFromCreateTableQueries(queries) { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k; const relations = []; const byChildTable = new Map(); const byParentTable = new Map(); const tableNames = new Set(); const seen = new Set(); for (const query of queries) { const childTable = normalizeRelationTableName(buildQualifiedName(query.namespaces, query.tableName.name)); tableNames.add(childTable); for (const column of query.columns) { for (const constraint of column.constraints) { if (constraint.kind !== 'references' || !constraint.reference) { continue; } const edge = createEdge({ childTable, parentTable: normalizeRelationTableName(constraint.reference.targetTable.toString()), childColumns: [column.name.name], parentColumns: (_b = (_a = constraint.reference.columns) === null || _a === void 0 ? void 0 : _a.map((item) => item.name)) !== null && _b !== void 0 ? _b : [], constraintKind: 'column-reference', constraintName: (_d = (_c = constraint.constraintName) === null || _c === void 0 ? void 0 : _c.name) !== null && _d !== void 0 ? _d : null, evidenceKind: 'column-reference', confidence: 'confirmed' }); addEdge(relations, byChildTable, byParentTable, seen, tableNames, edge); } } for (const constraint of query.tableConstraints) { if (constraint.kind !== 'foreign-key' || !constraint.reference) { continue; } const edge = createEdge({ childTable, parentTable: normalizeRelationTableName(constraint.reference.targetTable.toString()), childColumns: (_f = (_e = constraint.columns) === null || _e === void 0 ? void 0 : _e.map((item) => item.name)) !== null && _f !== void 0 ? _f : [], parentColumns: (_h = (_g = constraint.reference.columns) === null || _g === void 0 ? void 0 : _g.map((item) => item.name)) !== null && _h !== void 0 ? _h : [], constraintKind: 'table-foreign-key', constraintName: (_k = (_j = constraint.constraintName) === null || _j === void 0 ? void 0 : _j.name) !== null && _k !== void 0 ? _k : null, evidenceKind: 'table-foreign-key', confidence: 'confirmed' }); addEdge(relations, byChildTable, byParentTable, seen, tableNames, edge); } } return { relations, byChildTable, byParentTable, tableNames }; } /** * Return the known parent relations for a child table. */ export function getOutgoingRelations(graph, childTable) { var _a; return [...((_a = graph.byChildTable.get(normalizeRelationTableName(childTable))) !== null && _a !== void 0 ? _a : [])]; } /** * Return the known child relations for a parent table. */ export function getIncomingRelations(graph, parentTable) { var _a; return [...((_a = graph.byParentTable.get(normalizeRelationTableName(parentTable))) !== null && _a !== void 0 ? _a : [])]; } function addEdge(relations, byChildTable, byParentTable, seen, tableNames, edge) { tableNames.add(edge.childTable); tableNames.add(edge.parentTable); const signature = [ edge.childTable, edge.parentTable, edge.childColumns.join(','), edge.parentColumns.join(','), edge.constraintKind ].join('|'); if (seen.has(signature)) { return; } seen.add(signature); relations.push(edge); pushIndexedEdge(byChildTable, edge.childTable, edge); pushIndexedEdge(byParentTable, edge.parentTable, edge); } function pushIndexedEdge(index, key, edge) { var _a; const bucket = (_a = index.get(key)) !== null && _a !== void 0 ? _a : []; bucket.push(edge); index.set(key, bucket); } function createEdge(params) { return { childTable: params.childTable, parentTable: params.parentTable, childColumns: [...params.childColumns], parentColumns: [...params.parentColumns], constraintKind: params.constraintKind, constraintName: params.constraintName, evidenceKind: params.evidenceKind, confidence: params.confidence, isSelfReference: params.childTable === params.parentTable }; } function buildQualifiedName(namespaces, name) { return [...(namespaces !== null && namespaces !== void 0 ? namespaces : []), name].join('.'); } function normalizeRelationTableName(tableName) { return normalizeTableName(tableName); } export function getQualifiedNameText(value) { return normalizeRelationTableName(value.toString()); } //# sourceMappingURL=RelationGraph.js.map