UNPKG

rawsql-ts

Version:

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

145 lines (144 loc) 4.75 kB
import { SelectQuery } from "../models/SelectQuery"; /** * A utility class for renaming Common Table Expressions (CTEs) in SQL queries. * * This class provides functionality to safely rename CTEs while automatically updating * all column references and table references throughout the query, including within * nested CTE definitions and subqueries. * * @example * ```typescript * import { CTERenamer, SelectQueryParser } from 'rawsql-ts'; * * const sql = ` * WITH user_data AS ( * SELECT id, name FROM users * ), * order_summary AS ( * SELECT user_data.id, COUNT(*) as order_count * FROM user_data * JOIN orders ON user_data.id = orders.user_id * GROUP BY user_data.id * ) * SELECT * FROM order_summary * `; * * const query = SelectQueryParser.parse(sql); * const renamer = new CTERenamer(); * * // Rename 'user_data' to 'customer_data' * renamer.renameCTE(query, 'user_data', 'customer_data'); * * // All references are automatically updated: * // - CTE definition: WITH customer_data AS (...) * // - Column references: customer_data.id * // - Table references: FROM customer_data * ``` * * @example * ```typescript * // Error handling * try { * renamer.renameCTE(query, 'nonexistent_cte', 'new_name'); * } catch (error) { * console.error(error.message); // "CTE 'nonexistent_cte' does not exist" * } * * try { * renamer.renameCTE(query, 'existing_cte', 'already_exists'); * } catch (error) { * console.error(error.message); // "CTE 'already_exists' already exists" * } * ``` * * @since 0.11.16 */ export declare class CTERenamer { private dependencyAnalyzer; private columnReferenceCollector; private tableSourceCollector; /** * Creates a new instance of CTERenamer. * * The constructor initializes internal collectors and analyzers needed for * comprehensive CTE renaming operations. */ constructor(); /** * Renames a Common Table Expression (CTE) and updates all references to it. * * This method performs a comprehensive rename operation that includes: * - Updating the CTE definition name in the WITH clause * - Updating all column references (e.g., `old_name.column` → `new_name.column`) * - Updating all table references in FROM and JOIN clauses * - Processing references within nested CTEs and subqueries * * @param query - The SQL query containing the CTE to rename. Can be either SimpleSelectQuery or BinarySelectQuery (UNION/INTERSECT/EXCEPT). * @param oldName - The current name of the CTE to rename. * @param newName - The new name for the CTE. * * @throws {Error} When the specified CTE does not exist in the query. * @throws {Error} When a CTE with the new name already exists. * @throws {Error} When the query type is not supported (not a SelectQuery). * * @example * ```typescript * const renamer = new CTERenamer(); * * // Basic usage * renamer.renameCTE(query, 'old_cte_name', 'new_cte_name'); * * // With error handling * try { * renamer.renameCTE(query, 'user_data', 'customer_data'); * } catch (error) { * if (error.message.includes('does not exist')) { * console.log('CTE not found'); * } else if (error.message.includes('already exists')) { * console.log('Name conflict'); * } * } * ``` * * @since 0.11.16 */ renameCTE(query: SelectQuery, oldName: string, newName: string): void; /** * Validates input parameters for CTE renaming. */ private validateInputs; /** * Handles CTE renaming for SimpleSelectQuery. */ private renameInSimpleQuery; /** * Handles CTE renaming for BinarySelectQuery. */ private renameInBinaryQuery; /** * Recursively handles CTE renaming for any SelectQuery type. */ private renameInSelectQuery; /** * Renames the CTE definition in the WITH clause. */ private renameCTEDefinition; /** * Updates all references to the old CTE name (column references and table sources). */ private updateAllReferences; /** * Updates table sources within CTE definitions that reference the old CTE name. * This method manually traverses CTE internals to avoid infinite recursion * that occurs when using TableSourceCollector with selectableOnly=false. */ private updateTableSourcesInCTEs; /** * Updates table sources in a specific query (used for CTE internals). */ private updateTableSourcesInQuery; /** * Updates a specific table source if it matches the old CTE name. */ private updateTableSource; }