rawsql-ts
Version:
[beta]High-performance SQL parser and AST analyzer written in TypeScript. Provides fast parsing and advanced transformation capabilities.
53 lines (52 loc) • 2.33 kB
TypeScript
import { CommonTable, WithClause } from "../models/Clause";
/**
* CTENameConflictResolver is responsible for resolving name conflicts among Common Table Expressions (CTEs).
* It also sorts the tables in the proper order based on dependencies and recursiveness.
*/
export declare class CTEBuilder {
private sourceCollector;
private cteCollector;
private formatter;
constructor();
/**
* Resolves name conflicts among CommonTables.
* If there are duplicate CTE names, they must have identical definitions.
* Also sorts the tables so that:
* 1. Recursive CTEs come first (CTEs that reference themselves)
* 2. Then remaining tables are sorted so inner (deeper) CTEs come before outer CTEs
*
* @param commonTables The list of CommonTables to check for name conflicts
* @returns An object containing:
* - needRecursive: boolean indicating if any recursive CTEs are present
* - commonTables: A new list of CommonTables with resolved name conflicts and proper order
* @throws Error if there are duplicate CTE names with different definitions
*/
build(commonTables: CommonTable[]): WithClause;
/**
* Resolves duplicate CTE names by checking if they have identical definitions.
* If definitions differ, throws an error.
*
* @param commonTables The list of CTEs to check for duplicates
* @returns A list of CTEs with duplicates removed
* @throws Error if there are duplicate CTE names with different definitions
*/
private resolveDuplicateNames;
/**
* Builds a dependency graph of CTEs and identifies recursive CTEs.
*
* @param tables The list of CTEs to analyze
* @returns Object containing the table map, set of recursive CTEs, and dependency map
*/
private buildDependencyGraph;
/**
* Sorts the CTEs using topological sort, with recursive CTEs coming first.
*
* @param tables The list of CTEs to sort
* @param tableMap Map of table names to their CommonTable objects
* @param recursiveCTEs Set of table names that are recursive (self-referential)
* @param dependencies Map of table dependencies
* @returns Sorted list of CTEs
* @throws Error if a circular reference is detected
*/
private sortCommonTables;
}