UNPKG

rawsql-ts

Version:

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

83 lines 3.52 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CTEInjector = void 0; const SelectQuery_1 = require("../models/SelectQuery"); const CTECollector_1 = require("./CTECollector"); const CTEBuilder_1 = require("./CTEBuilder"); /** * CTEInjector accepts a SelectQuery object and an array of CommonTables, * and inserts Common Table Expressions into the query. * For BinarySelectQuery, CTEs are inserted into the left query. * * Uses CTENameConflictResolver to resolve naming conflicts between CTEs. */ class CTEInjector { constructor() { this.nameConflictResolver = new CTEBuilder_1.CTEBuilder(); this.cteCollector = new CTECollector_1.CTECollector(); } /** * Inserts Common Table Expressions into a SelectQuery object. * * @param query The query to inject CTEs into * @param commonTables Array of CommonTables to be inserted * @returns A new query with the injected CTEs */ inject(query, commonTables) { // If the array is empty, return the query as is if (commonTables.length === 0) { return query; } // Collect CTEs from the query commonTables.push(...this.cteCollector.collect(query)); // Use CTENameConflictResolver to resolve duplicates and sort in appropriate order const resolvedWithCaluse = this.nameConflictResolver.build(commonTables); // Process based on query type if (query instanceof SelectQuery_1.SimpleSelectQuery) { return this.injectIntoSimpleQuery(query, resolvedWithCaluse); } else if (query instanceof SelectQuery_1.BinarySelectQuery) { return this.injectIntoBinaryQuery(query, resolvedWithCaluse); } // Unsupported query type throw new Error("Unsupported query type"); } /** * Inserts Common Table Expressions into a SimpleSelectQuery. * * @param query The SimpleSelectQuery to inject CTEs into * @param commonTables Array of CommonTables to be inserted * @param needRecursive Boolean indicating if recursive WITH clause is needed * @returns A new SimpleSelectQuery with the injected CTEs */ injectIntoSimpleQuery(query, withClause) { if (query.withClause) { throw new Error("The query already has a WITH clause. Please remove it before injecting new CTEs."); } // If the query doesn't have a WITH clause, set the new one query.withClause = withClause; return query; } /** * Inserts Common Table Expressions into the left query of a BinarySelectQuery. * * @param query The BinarySelectQuery to inject CTEs into * @param commonTables Array of CommonTables to be inserted * @param needRecursive Boolean indicating if recursive WITH clause is needed * @returns A new BinarySelectQuery with the injected CTEs */ injectIntoBinaryQuery(query, withClause) { // Insert CTEs into the left query if (query.left instanceof SelectQuery_1.SimpleSelectQuery) { this.injectIntoSimpleQuery(query.left, withClause); return query; } else if (query.left instanceof SelectQuery_1.BinarySelectQuery) { this.injectIntoBinaryQuery(query.left, withClause); return query; } throw new Error("Unsupported query type for BinarySelectQuery left side"); } } exports.CTEInjector = CTEInjector; //# sourceMappingURL=CTEInjector.js.map