Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/cteMaker.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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,
Expand Down
35 changes: 24 additions & 11 deletions src/queryKinds/dml/union.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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,
};
}
}

/**
Expand Down