UNPKG

rawsql-ts

Version:

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

148 lines 5.58 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.OutputNode = exports.OperationNode = exports.ProcessNode = exports.DataSourceNode = exports.BaseDataFlowNode = void 0; /** * Base class for all data flow nodes */ class BaseDataFlowNode { constructor(id, label, type, shape, details) { this.id = id; this.label = label; this.type = type; this.shape = shape; this.details = details; } } exports.BaseDataFlowNode = BaseDataFlowNode; /** * Represents a data source (table, CTE, subquery) */ class DataSourceNode extends BaseDataFlowNode { constructor(id, label, type) { const shape = type === 'subquery' ? 'hexagon' : 'cylinder'; super(id, label, type, shape); this.annotations = new Set(); } addAnnotation(annotation) { this.annotations.add(annotation); } hasAnnotation(annotation) { return this.annotations.has(annotation); } getMermaidRepresentation() { if (this.shape === 'hexagon') { return `${this.id}{{${this.label}}}`; } return `${this.id}[(${this.label})]`; } static createTable(tableName) { return new DataSourceNode(`table_${tableName}`, tableName, 'table'); } static createCTE(cteName) { return new DataSourceNode(`cte_${cteName}`, `CTE:${cteName}`, 'cte'); } static createSubquery(alias) { return new DataSourceNode(`subquery_${alias}`, `SubQuery:${alias}`, 'subquery'); } } exports.DataSourceNode = DataSourceNode; /** * Represents a processing operation (WHERE, GROUP BY, SELECT, etc.) */ class ProcessNode extends BaseDataFlowNode { constructor(id, operation, context = '') { const nodeId = context ? `${context}_${operation.toLowerCase().replace(/\s+/g, '_')}` : operation.toLowerCase().replace(/\s+/g, '_'); super(nodeId, operation, 'process', 'hexagon'); } getMermaidRepresentation() { return `${this.id}{{${this.label}}}`; } /** @deprecated Process nodes are no longer used in data flow diagrams */ static createWhere(context) { return new ProcessNode(`${context}_where`, 'WHERE', context); } /** @deprecated Process nodes are no longer used in data flow diagrams */ static createGroupBy(context) { return new ProcessNode(`${context}_group_by`, 'GROUP BY', context); } /** @deprecated Process nodes are no longer used in data flow diagrams */ static createHaving(context) { return new ProcessNode(`${context}_having`, 'HAVING', context); } /** @deprecated Process nodes are no longer used in data flow diagrams */ static createSelect(context) { return new ProcessNode(`${context}_select`, 'SELECT', context); } /** @deprecated Process nodes are no longer used in data flow diagrams */ static createOrderBy(context) { return new ProcessNode(`${context}_order_by`, 'ORDER BY', context); } /** @deprecated Process nodes are no longer used in data flow diagrams */ static createLimit(context, hasOffset = false) { const label = hasOffset ? 'LIMIT/OFFSET' : 'LIMIT'; return new ProcessNode(`${context}_limit`, label, context); } } exports.ProcessNode = ProcessNode; /** * Represents an operation (JOIN, UNION, etc.) */ class OperationNode extends BaseDataFlowNode { constructor(id, operation, shape = 'diamond') { super(id, operation, 'operation', shape); } getMermaidRepresentation() { switch (this.shape) { case 'rounded': return `${this.id}(${this.label})`; case 'rectangle': return `${this.id}[${this.label}]`; case 'hexagon': return `${this.id}{{${this.label}}}`; case 'stadium': return `${this.id}([${this.label}])`; case 'diamond': default: return `${this.id}{${this.label}}`; } } static createJoin(joinId, joinType) { let label; const normalizedType = joinType.trim().toLowerCase(); if (normalizedType === 'join') { label = 'INNER JOIN'; } else if (normalizedType.endsWith(' join')) { label = normalizedType.toUpperCase(); } else { label = normalizedType.toUpperCase() + ' JOIN'; } // Use rectangle shape for JOIN operations (standard flowchart) return new OperationNode(`join_${joinId}`, label, 'rectangle'); } static createUnion(unionId, unionType = 'UNION ALL') { return new OperationNode(`${unionType.toLowerCase().replace(/\s+/g, '_')}_${unionId}`, unionType.toUpperCase(), 'rectangle'); } static createSetOperation(operationId, operation) { const normalizedOp = operation.toUpperCase(); const id = `${normalizedOp.toLowerCase().replace(/\s+/g, '_')}_${operationId}`; // Use rectangle shape for set operations like UNION (smaller than diamond) return new OperationNode(id, normalizedOp, 'rectangle'); } } exports.OperationNode = OperationNode; /** * Represents the final output */ class OutputNode extends BaseDataFlowNode { constructor(context = 'main') { const label = context === 'main' ? 'Final Result' : `${context} Result`; super(`${context}_output`, label, 'output', 'stadium'); } getMermaidRepresentation() { return `${this.id}([${this.label}])`; } } exports.OutputNode = OutputNode; //# sourceMappingURL=DataFlowNode.js.map