UNPKG

rawsql-ts

Version:

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

138 lines 4.15 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DataFlowGraph = void 0; const DataFlowNode_1 = require("./DataFlowNode"); const DataFlowEdge_1 = require("./DataFlowEdge"); /** * Represents the complete data flow graph for a SQL query */ class DataFlowGraph { constructor() { this.nodes = new Map(); this.edges = new DataFlowEdge_1.DataFlowEdgeCollection(); } addNode(node) { this.nodes.set(node.id, node); } addEdge(edge) { this.edges.add(edge); } addConnection(from, to, label) { this.edges.addConnection(from, to, label); } hasNode(nodeId) { return this.nodes.has(nodeId); } hasConnection(from, to) { return this.edges.hasConnection(from, to); } getNode(nodeId) { return this.nodes.get(nodeId); } getAllNodes() { return Array.from(this.nodes.values()); } getAllEdges() { return this.edges.getAll(); } /** * Generates the complete Mermaid flowchart syntax */ generateMermaid(direction = 'TD', title) { let mermaid = `flowchart ${direction}\n`; // Add title if provided if (title) { mermaid += ` %% ${title}\n`; } // Add nodes const nodeLines = Array.from(this.nodes.values()) .map(node => ` ${node.getMermaidRepresentation()}`) .join('\n'); if (nodeLines) { mermaid += nodeLines + '\n'; } // Add blank line between nodes and edges if both exist if (this.nodes.size > 0 && this.edges.getAll().length > 0) { mermaid += '\n'; } // Add edges const edgeRepresentation = this.edges.getMermaidRepresentation(); if (edgeRepresentation) { mermaid += ` ${edgeRepresentation}\n`; } return mermaid; } /** * Creates or gets a table node */ getOrCreateTable(tableName) { const nodeId = `table_${tableName}`; let node = this.nodes.get(nodeId); if (!node) { node = DataFlowNode_1.DataSourceNode.createTable(tableName); this.addNode(node); } return node; } /** * Creates or gets a CTE node */ getOrCreateCTE(cteName) { const nodeId = `cte_${cteName}`; let node = this.nodes.get(nodeId); if (!node) { node = DataFlowNode_1.DataSourceNode.createCTE(cteName); this.addNode(node); } return node; } /** * Creates or gets a subquery node */ getOrCreateSubquery(alias) { const nodeId = `subquery_${alias}`; let node = this.nodes.get(nodeId); if (!node) { node = DataFlowNode_1.DataSourceNode.createSubquery(alias); this.addNode(node); } return node; } /** * Creates a process node (deprecated - kept for backward compatibility) * Note: Process nodes are no longer used in data flow diagrams * as we focus only on data sources, joins, and unions */ createProcessNode(type, context) { // Create a generic process node for backward compatibility const node = new DataFlowNode_1.ProcessNode(context, type); this.addNode(node); return node; } /** * Creates a JOIN operation node */ createJoinNode(joinId, joinType) { const node = DataFlowNode_1.OperationNode.createJoin(joinId, joinType); this.addNode(node); return node; } /** * Creates a set operation node (UNION, EXCEPT, etc.) */ createSetOperationNode(operationId, operation) { const node = DataFlowNode_1.OperationNode.createSetOperation(operationId, operation); this.addNode(node); return node; } /** * Creates an output node */ createOutputNode(context = 'main') { const node = new DataFlowNode_1.OutputNode(context); this.addNode(node); return node; } } exports.DataFlowGraph = DataFlowGraph; //# sourceMappingURL=DataFlowGraph.js.map