From 21cce8cf387bdfaabea33bd660062f88fcf204f7 Mon Sep 17 00:00:00 2001 From: Nicolas Renan Machado Dias Date: Tue, 28 Oct 2025 11:32:18 -0300 Subject: [PATCH] fix/fixed how unions and selects work in CTEs, disable analysis --- src/cteMaker.ts | 13 +++++++++++++ src/queryKinds/dml/union.ts | 35 ++++++++++++++++++++++++----------- 2 files changed, 37 insertions(+), 11 deletions(-) diff --git a/src/cteMaker.ts b/src/cteMaker.ts index c1fc40c..d7a8ad3 100644 --- a/src/cteMaker.ts +++ b/src/cteMaker.ts @@ -1,5 +1,6 @@ import type QueryDefinition from "./queryKinds/dml/dmlQueryDefinition.js"; import SelectQuery from "./queryKinds/dml/select.js"; +import Union from "./queryKinds/dml/union.js"; /** * Cte represents a Common Table Expression (CTE) in SQL. @@ -75,7 +76,19 @@ export class Cte { */ public build(): { text: string; values: any[] } { const recursiveStr = this.recursiveCte ? "RECURSIVE " : ""; + if ( + this.query instanceof SelectQuery + || this.query instanceof Union + ) { + (this.query as any).disabledAnalysis = true; + } const query = this.query.build(); + if ( + this.query instanceof SelectQuery + || this.query instanceof Union + ) { + (this.query as any).disabledAnalysis = false; + } return { text: `${recursiveStr}${this.name} AS (\n${query.text}\n)`, values: query.values, diff --git a/src/queryKinds/dml/union.ts b/src/queryKinds/dml/union.ts index ff5fba8..7f504c9 100644 --- a/src/queryKinds/dml/union.ts +++ b/src/queryKinds/dml/union.ts @@ -67,6 +67,9 @@ export default class Union extends DmlQueryDefinition { /** Having statement for the union query */ private havingStatement: Statement | null = null; + /** Where statement for the union query */ + private disabledAnalysis: boolean = false; + /** * Checks if all added SELECT queries have the same number of fields. * This is important for ensuring that the UNION operation is valid. @@ -527,19 +530,29 @@ export default class Union extends DmlQueryDefinition { const finalValues = [...values, ...whereValues, ...havingValues]; - const analyzed = this.reAnalyzeParsedQueryForDuplicateParams( - union, - finalValues, - deepAnalysis, - ); + if (!deepAnalysis) { + const analyzed = this.reAnalyzeParsedQueryForDuplicateParams( + union, + finalValues, + deepAnalysis, + ); - this.builtQuery = analyzed.text; - this.builtParams = analyzed.values; + this.builtQuery = analyzed.text; + this.builtParams = analyzed.values; - return { - text: this.builtQuery, - values: this.builtParams, - }; + return { + text: this.builtQuery, + values: this.builtParams, + }; + } else { + this.builtQuery = union; + this.builtParams = finalValues; + + return { + text: this.builtQuery, + values: this.builtParams, + }; + } } /**