rawsql-ts
Version:
[beta]High-performance SQL parser and AST analyzer written in TypeScript. Provides fast parsing and advanced transformation capabilities.
46 lines • 1.89 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.CTENormalizer = void 0;
const CTECollector_1 = require("./CTECollector");
const CTEDisabler_1 = require("./CTEDisabler");
const CTEInjector_1 = require("./CTEInjector");
/**
* CTENormalizer is responsible for normalizing Common Table Expressions (CTEs) within SQL queries.
* It collects all CTEs from various parts of the query and consolidates them into a single WITH clause
* at the root level of the query.
*
* This implementation uses:
* 1. CommonTableCollector - to gather all CTEs from the query structure
* 2. WithClauseDisabler - to remove all original WITH clauses from the query
* 3. CTENameConflictResolver - to resolve name conflicts among CTEs and sort them properly
*/
class CTENormalizer {
/**
* Private constructor to prevent instantiation of this utility class.
*/
constructor() {
// This class is not meant to be instantiated.
}
/**
* Normalizes a SQL query by consolidating all CTEs into a single WITH clause
* at the root level of the query.
*
* @param query The query to normalize
* @returns A new normalized query with all CTEs at the root level
*/
static normalize(query) {
// No need to normalize if the query doesn't have any CTEs
const cteCollector = new CTECollector_1.CTECollector();
const allCommonTables = cteCollector.collect(query);
if (allCommonTables.length === 0) {
return query;
}
// Remove all WITH clauses from the original query
const cteDisabler = new CTEDisabler_1.CTEDisabler();
cteDisabler.execute(query);
const injector = new CTEInjector_1.CTEInjector();
return injector.inject(query, allCommonTables);
}
}
exports.CTENormalizer = CTENormalizer;
//# sourceMappingURL=CTENormalizer.js.map
;