rawsql-ts
Version:
[beta]High-performance SQL parser and AST analyzer written in TypeScript. Provides fast parsing and advanced transformation capabilities.
126 lines (125 loc) • 4.19 kB
TypeScript
import { CommonTable } from "../models/Clause";
import { SimpleSelectQuery } from "../models/SimpleSelectQuery";
/**
* Node type for distinguishing between CTE and main query nodes
*/
export type NodeType = 'CTE' | 'ROOT';
/**
* Interface representing a dependency relationship between nodes
*/
export interface CTEEdge {
from: string;
to: string;
}
/**
* Interface representing a node in the dependency graph (either CTE or main query)
*/
export interface CTENode {
name: string;
type: NodeType;
cte: CommonTable | null;
dependencies: string[];
dependents: string[];
}
/**
* Interface representing the complete CTE dependency graph including main query
*/
export interface CTEDependencyGraph {
nodes: CTENode[];
edges: CTEEdge[];
}
/**
* Analyzer for CTE dependencies in SQL queries.
* Provides functionality to analyze dependencies, detect circular references,
* and generate topological ordering of CTEs.
*/
export declare class CTEDependencyAnalyzer {
private static readonly ERROR_MESSAGES;
private static readonly MAIN_QUERY_NAME;
private readonly sourceCollector;
private readonly cteReferenceCollector;
private readonly cteCollector;
private dependencyGraph;
private cteMap;
constructor();
/**
* Analyzes the dependencies between CTEs in the given query
* @param query The query to analyze
* @returns The dependency graph
*/
analyzeDependencies(query: SimpleSelectQuery): CTEDependencyGraph;
/**
* Gets the list of CTEs that the specified CTE depends on
* @param cteName The name of the CTE
* @returns Array of CTE names this CTE depends on
*/
getDependencies(cteName: string): string[];
/**
* Gets the list of CTEs that depend on the specified CTE
* @param cteName The name of the CTE
* @returns Array of CTE names that depend on this CTE
*/
getDependents(cteName: string): string[];
/**
* Gets the list of CTEs that are directly referenced by the main query
* @returns Array of CTE names referenced by the main query
*/
getMainQueryDependencies(): string[];
/**
* Gets nodes by type (CTE or ROOT)
* @param nodeType The type of nodes to retrieve
* @returns Array of nodes of the specified type
*/
getNodesByType(nodeType: NodeType): CTENode[];
/**
* Gets the main query node
* @returns The main query node or undefined if not found
*/
getMainQueryNode(): CTENode | undefined;
/**
* Checks if there are any circular dependencies in the CTE graph
* @returns true if circular dependencies exist, false otherwise
*/
hasCircularDependency(): boolean;
/**
* Gets the topological sort order for CTE execution
* @returns Array of CTE names in execution order
* @throws Error if circular dependencies are detected
*/
getExecutionOrder(): string[];
/**
* Builds the dependency graph from the given CTEs and main query
* @param ctes Array of CommonTable objects
* @param mainQuery The main query that may reference CTEs
* @returns The constructed dependency graph
*/
private buildDependencyGraph;
/**
* Ensures that dependency analysis has been performed
* @throws Error if analyzeDependencies has not been called
*/
private ensureAnalyzed;
/**
* Builds the CTE name-to-object mapping for quick lookups
* @param ctes Array of CommonTable objects
*/
private buildCTEMap;
/**
* Finds a node in the dependency graph by CTE name
* @param cteName The name of the CTE to find
* @returns The CTENode if found, undefined otherwise
*/
private findNodeByName;
/**
* Gets the main query without the WITH clause for analyzing main query dependencies
* @param query The complete query with WITH clause
* @returns A query without WITH clause, or null if no main query exists
*/
private getMainQueryWithoutCTE;
/**
* Extracts the name from a CommonTable
* @param cte The CommonTable object
* @returns The name of the CTE
*/
private static getCTEName;
}