rawsql-ts 0.11.2-beta → 0.11.3-beta

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. package/README.md +735 -682
  2. package/dist/esm/index.js +1 -0
  3. package/dist/esm/index.js.map +1 -1
  4. package/dist/esm/index.min.js +13 -13
  5. package/dist/esm/index.min.js.map +4 -4
  6. package/dist/esm/src/index.js +1 -0
  7. package/dist/esm/src/index.js.map +1 -1
  8. package/dist/esm/src/models/BinarySelectQuery.js +9 -0
  9. package/dist/esm/src/models/BinarySelectQuery.js.map +1 -1
  10. package/dist/esm/src/models/CTEError.js +31 -0
  11. package/dist/esm/src/models/CTEError.js.map +1 -0
  12. package/dist/esm/src/models/SelectQuery.js +1 -0
  13. package/dist/esm/src/models/SelectQuery.js.map +1 -1
  14. package/dist/esm/src/models/SimpleSelectQuery.js +229 -0
  15. package/dist/esm/src/models/SimpleSelectQuery.js.map +1 -1
  16. package/dist/esm/src/models/ValuesQuery.js +1 -1
  17. package/dist/esm/src/models/ValuesQuery.js.map +1 -1
  18. package/dist/esm/src/transformers/QueryBuilder.js +2 -0
  19. package/dist/esm/src/transformers/QueryBuilder.js.map +1 -1
  20. package/dist/esm/tsconfig.browser.tsbuildinfo +1 -1
  21. package/dist/esm/types/src/index.d.ts +1 -0
  22. package/dist/esm/types/src/models/BinarySelectQuery.d.ts +7 -0
  23. package/dist/esm/types/src/models/CTEError.d.ts +21 -0
  24. package/dist/esm/types/src/models/SelectQuery.d.ts +12 -0
  25. package/dist/esm/types/src/models/SimpleSelectQuery.d.ts +166 -2
  26. package/dist/esm/types/src/models/ValuesQuery.d.ts +1 -1
  27. package/dist/index.min.js +13 -13
  28. package/dist/index.min.js.map +4 -4
  29. package/dist/src/index.d.ts +1 -0
  30. package/dist/src/index.js +1 -0
  31. package/dist/src/index.js.map +1 -1
  32. package/dist/src/models/BinarySelectQuery.d.ts +7 -0
  33. package/dist/src/models/BinarySelectQuery.js +9 -0
  34. package/dist/src/models/BinarySelectQuery.js.map +1 -1
  35. package/dist/src/models/CTEError.d.ts +21 -0
  36. package/dist/src/models/CTEError.js +37 -0
  37. package/dist/src/models/CTEError.js.map +1 -0
  38. package/dist/src/models/SelectQuery.d.ts +12 -0
  39. package/dist/src/models/SelectQuery.js +5 -1
  40. package/dist/src/models/SelectQuery.js.map +1 -1
  41. package/dist/src/models/SimpleSelectQuery.d.ts +166 -2
  42. package/dist/src/models/SimpleSelectQuery.js +229 -0
  43. package/dist/src/models/SimpleSelectQuery.js.map +1 -1
  44. package/dist/src/models/ValuesQuery.d.ts +1 -1
  45. package/dist/src/models/ValuesQuery.js +1 -1
  46. package/dist/src/models/ValuesQuery.js.map +1 -1
  47. package/dist/src/transformers/QueryBuilder.js +2 -0
  48. package/dist/src/transformers/QueryBuilder.js.map +1 -1
  49. package/dist/tsconfig.tsbuildinfo +1 -1
  50. package/package.json +2 -2
@@ -4,6 +4,7 @@ export * from './models/BinarySelectQuery';
4
4
  export * from './models/SelectQuery';
5
5
  export * from './models/ValueComponent';
6
6
  export * from './models/ValuesQuery';
7
+ export * from './models/CTEError';
7
8
  export * from './transformers/CTECollector';
8
9
  export * from './transformers/CTENormalizer';
9
10
  export * from './transformers/Formatter';
package/dist/src/index.js CHANGED
@@ -22,6 +22,7 @@ __exportStar(require("./models/BinarySelectQuery"), exports);
22
22
  __exportStar(require("./models/SelectQuery"), exports);
23
23
  __exportStar(require("./models/ValueComponent"), exports);
24
24
  __exportStar(require("./models/ValuesQuery"), exports);
25
+ __exportStar(require("./models/CTEError"), exports);
25
26
  __exportStar(require("./transformers/CTECollector"), exports);
26
27
  __exportStar(require("./transformers/CTENormalizer"), exports);
27
28
  __exportStar(require("./transformers/Formatter"), exports);
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,oCAAoC;AACpC,8DAA4C;AAC5C,8DAA4C;A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AAA;AACnB,qHAAA,eAAe,OAAA;AACf,oHAAA,cAAc,OAAA;AAGlB,qEAAqE;AACrE,wEAAqE;AAA5D,sHAAA,gBAAgB,OAAA;AACzB,2EAAyD;AACzD,iFAA+D;AAE/D,iEAA+C;AAC/C,kEAAgD;AAChD,iEAA+C;AAC/C,uEAAqD;AACrD,qEAAmD;AAEnD,6DAA2C;AAC3C,8DAA4C;AAC5C,wDAAsC;AACtC,oEAAoE"}
@@ -2,6 +2,7 @@ import { SourceExpression } from "./Clause";
2
2
  import type { SelectQuery } from "./SelectQuery";
3
3
  import { SqlComponent } from "./SqlComponent";
4
4
  import { RawString } from "./ValueComponent";
5
+ import { SimpleSelectQuery } from "./SimpleSelectQuery";
5
6
  /**
6
7
  * Represents a binary SELECT query (e.g., UNION, INTERSECT, EXCEPT).
7
8
  */
@@ -94,4 +95,10 @@ export declare class BinarySelectQuery extends SqlComponent implements SelectQue
94
95
  * @param value Value to set
95
96
  */
96
97
  setParameter(name: string, value: any): this;
98
+ /**
99
+ * Converts this BinarySelectQuery to a SimpleSelectQuery using QueryBuilder.
100
+ * This enables CTE management on binary queries by wrapping them as subqueries.
101
+ * @returns A SimpleSelectQuery representation of this binary query
102
+ */
103
+ toSimpleQuery(): SimpleSelectQuery;
97
104
  }
@@ -7,6 +7,7 @@ const ValueComponent_1 = require("./ValueComponent");
7
7
  const CTENormalizer_1 = require("../transformers/CTENormalizer");
8
8
  const SelectQueryParser_1 = require("../parsers/SelectQueryParser");
9
9
  const ParameterHelper_1 = require("../utils/ParameterHelper");
10
+ const QueryBuilder_1 = require("../transformers/QueryBuilder");
10
11
  /**
11
12
  * Represents a binary SELECT query (e.g., UNION, INTERSECT, EXCEPT).
12
13
  */
@@ -143,6 +144,14 @@ class BinarySelectQuery extends SqlComponent_1.SqlComponent {
143
144
  ParameterHelper_1.ParameterHelper.set(this, name, value);
144
145
  return this;
145
146
  }
147
+ /**
148
+ * Converts this BinarySelectQuery to a SimpleSelectQuery using QueryBuilder.
149
+ * This enables CTE management on binary queries by wrapping them as subqueries.
150
+ * @returns A SimpleSelectQuery representation of this binary query
151
+ */
152
+ toSimpleQuery() {
153
+ return QueryBuilder_1.QueryBuilder.buildSimpleQuery(this);
154
+ }
146
155
  }
147
156
  exports.BinarySelectQuery = BinarySelectQuery;
148
157
  BinarySelectQuery.kind = Symbol("BinarySelectQuery");
@@ -1 +1 @@
1
- {"version":3,"file":"BinarySelectQuery.js","sourceRoot":"","sources":["../../../src/models/BinarySelectQuery.ts"],"names":[],"mappings":";;;AAAA,qCAAmF;AAEnF,iDAA8C;AAC9C,qDAA6C;AAC7C,iEAA8D;AAC9D,oEAAiE;AAEjE,8DAA2D;AAE3D;;GAEG;AACH,MAAa,iBAAkB,SAAQ,2BAAY;IAM/C,YAAY,IAAiB,EAAE,QAAgB,EAAE,KAAkB;QAC/D,KAAK,EAAE,CAAC;QACR,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,QAAQ,GAAG,IAAI,0BAAS,CAAC,QAAQ,CAAC,CAAC;QACxC,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;IACvB,CAAC;IAED;;;;;;;OAOG;IACI,KAAK,CAAC,KAAkB;QAC3B,OAAO,IAAI,CAAC,iBAAiB,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;IAClD,CAAC;IAED;;;;;;;OAOG;IACI,QAAQ,CAAC,KAAkB;QAC9B,OAAO,IAAI,CAAC,iBAAiB,CAAC,WAAW,EAAE,KAAK,CAAC,CAAC;IACtD,CAAC;IAED;;;;;;;OAOG;IACI,SAAS,CAAC,KAAkB;QAC/B,OAAO,IAAI,CAAC,iBAAiB,CAAC,WAAW,EAAE,KAAK,CAAC,CAAC;IACtD,CAAC;IAED;;;;;;;OAOG;IACI,YAAY,CAAC,KAAkB;QAClC,OAAO,IAAI,CAAC,iBAAiB,CAAC,eAAe,EAAE,KAAK,CAAC,CAAC;IAC1D,CAAC;IAED;;;;;;;OAOG;IACI,MAAM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K,CAAC,CAAC;QACvC,OAAO,IAAI,CAAC;IAChB,CAAC;IAED;;;;OAIG;IACI,aAAa;QAChB,OAAO,2BAAY,CAAC,gBAAgB,CAAC,IAAI,CAAC,CAAC;IAC/C,CAAC;;AAjKL,8CAkKC;AAjKU,sBAAI,GAAG,MAAM,CAAC,mBAAmB,CAAC,CAAC"}
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Error thrown when a CTE with the same name already exists
3
+ */
4
+ export declare class DuplicateCTEError extends Error {
5
+ cteName: string;
6
+ constructor(cteName: string);
7
+ }
8
+ /**
9
+ * Error thrown when a CTE name is invalid
10
+ */
11
+ export declare class InvalidCTENameError extends Error {
12
+ cteName: string;
13
+ constructor(cteName: string, reason: string);
14
+ }
15
+ /**
16
+ * Error thrown when trying to operate on a non-existent CTE
17
+ */
18
+ export declare class CTENotFoundError extends Error {
19
+ cteName: string;
20
+ constructor(cteName: string);
21
+ }
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CTENotFoundError = exports.InvalidCTENameError = exports.DuplicateCTEError = void 0;
4
+ /**
5
+ * Error thrown when a CTE with the same name already exists
6
+ */
7
+ class DuplicateCTEError extends Error {
8
+ constructor(cteName) {
9
+ super(`CTE '${cteName}' already exists in the query`);
10
+ this.cteName = cteName;
11
+ this.name = 'DuplicateCTEError';
12
+ }
13
+ }
14
+ exports.DuplicateCTEError = DuplicateCTEError;
15
+ /**
16
+ * Error thrown when a CTE name is invalid
17
+ */
18
+ class InvalidCTENameError extends Error {
19
+ constructor(cteName, reason) {
20
+ super(`Invalid CTE name '${cteName}': ${reason}`);
21
+ this.cteName = cteName;
22
+ this.name = 'InvalidCTENameError';
23
+ }
24
+ }
25
+ exports.InvalidCTENameError = InvalidCTENameError;
26
+ /**
27
+ * Error thrown when trying to operate on a non-existent CTE
28
+ */
29
+ class CTENotFoundError extends Error {
30
+ constructor(cteName) {
31
+ super(`CTE '${cteName}' not found in the query`);
32
+ this.cteName = cteName;
33
+ this.name = 'CTENotFoundError';
34
+ }
35
+ }
36
+ exports.CTENotFoundError = CTENotFoundError;
37
+ //# sourceMappingURL=CTEError.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"CTEError.js","sourceRoot":"","sources":["../../../src/models/CTEError.ts"],"names":[],"mappings":";;;AAAA;;GAEG;AACH,MAAa,iBAAkB,SAAQ,KAAK;IACxC,YAAmB,OAAe;QAC9B,KAAK,CAAC,QAAQ,OAAO,+BAA+B,CAAC,CAAC;QADvC,YAAO,GAAP,OAAO,CAAQ;QAE9B,IAAI,CAAC,IAAI,GAAG,mBAAmB,CAAC;IACpC,CAAC;CACJ;AALD,8CAKC;AAED;;GAEG;AACH,MAAa,mBAAoB,SAAQ,KAAK;IAC1C,YAAmB,OAAe,EAAE,MAAc;QAC9C,KAAK,CAAC,qBAAqB,OAAO,MAAM,MAAM,EAAE,CAAC,CAAC;QADnC,YAAO,GAAP,OAAO,CAAQ;QAE9B,IAAI,CAAC,IAAI,GAAG,qBAAqB,CAAC;IACtC,CAAC;CACJ;AALD,kDAKC;AAED;;GAEG;AACH,MAAa,gBAAiB,SAAQ,KAAK;IACvC,YAAmB,OAAe;QAC9B,KAAK,CAAC,QAAQ,OAAO,0BAA0B,CAAC,CAAC;QADlC,YAAO,GAAP,OAAO,CAAQ;QAE9B,IAAI,CAAC,IAAI,GAAG,kBAAkB,CAAC;IACnC,CAAC;CACJ;AALD,4CAKC"}
@@ -3,7 +3,19 @@ import { InsertQuery } from "./InsertQuery";
3
3
  import { SimpleSelectQuery } from "./SimpleSelectQuery";
4
4
  import { BinarySelectQuery } from "./BinarySelectQuery";
5
5
  import { ValuesQuery } from "./ValuesQuery";
6
+ export interface CTEOptions {
7
+ materialized?: boolean | null;
8
+ }
9
+ export { DuplicateCTEError, InvalidCTENameError, CTENotFoundError } from './CTEError';
10
+ export interface CTEManagement {
11
+ addCTE(name: string, query: SelectQuery, options?: CTEOptions): this;
12
+ removeCTE(name: string): this;
13
+ hasCTE(name: string): boolean;
14
+ getCTENames(): string[];
15
+ replaceCTE(name: string, query: SelectQuery, options?: CTEOptions): this;
16
+ }
6
17
  export interface SelectQuery extends SqlComponent {
7
18
  setParameter(name: string, value: any): this;
19
+ toSimpleQuery(): SimpleSelectQuery;
8
20
  }
9
21
  export { SimpleSelectQuery, BinarySelectQuery, ValuesQuery, InsertQuery };
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.InsertQuery = exports.ValuesQuery = exports.BinarySelectQuery = exports.SimpleSelectQuery = void 0;
3
+ exports.InsertQuery = exports.ValuesQuery = exports.BinarySelectQuery = exports.SimpleSelectQuery = exports.CTENotFoundError = exports.InvalidCTENameError = exports.DuplicateCTEError = void 0;
4
4
  const InsertQuery_1 = require("./InsertQuery");
5
5
  Object.defineProperty(exports, "InsertQuery", { enumerable: true, get: function () { return InsertQuery_1.InsertQuery; } });
6
6
  const SimpleSelectQuery_1 = require("./SimpleSelectQuery");
@@ -9,4 +9,8 @@ const BinarySelectQuery_1 = require("./BinarySelectQuery");
9
9
  Object.defineProperty(exports, "BinarySelectQuery", { enumerable: true, get: function () { return BinarySelectQuery_1.BinarySelectQuery; } });
10
10
  const ValuesQuery_1 = require("./ValuesQuery");
11
11
  Object.defineProperty(exports, "ValuesQuery", { enumerable: true, get: function () { return ValuesQuery_1.ValuesQuery; } });
12
+ var CTEError_1 = require("./CTEError");
13
+ Object.defineProperty(exports, "DuplicateCTEError", { enumerable: true, get: function () { return CTEError_1.DuplicateCTEError; } });
14
+ Object.defineProperty(exports, "InvalidCTENameError", { enumerable: true, get: function () { return CTEError_1.InvalidCTENameError; } });
15
+ Object.defineProperty(exports, "CTENotFoundError", { enumerable: true, get: function () { return CTEError_1.CTENotFoundError; } });
12
16
  //# sourceMappingURL=SelectQuery.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"SelectQuery.js","sourceRoot":"","sources":["../../../src/models/SelectQuery.ts"],"names":[],"mappings":";;;AACA,+CAA4C;AAQgB,4FARnD,yBAAW,OAQmD;AAPvE,2DAAwD;AAO/C,kGAPA,qCAAiB,OAOA;AAN1B,2DAAwD;AAM5B,kGANnB,qCAAiB,OAMmB;AAL7C,+CAA4C;AAKG,4FALtC,yBAAW,OAKsC"}
1
+ {"version":3,"file":"SelectQuery.js","sourceRoot":"","sources":["../../../src/models/SelectQuery.ts"],"names":[],"mappings":";;;AACA,+CAA4C;AAuBgB,4FAvBnD,yBAAW,OAuBmD;AAtBvE,2DAAwD;AAsB/C,kGAtBA,qCAAiB,OAsBA;AArB1B,2DAAwD;AAqB5B,kGArBnB,qCAAiB,OAqBmB;AApB7C,+CAA4C;AAoBG,4FApBtC,yBAAW,OAoBsC;AAd1D,uCAAsF;AAA7E,6GAAA,iBAAiB,OAAA;AAAE,+GAAA,mBAAmB,OAAA;AAAE,4GAAA,gBAAgB,OAAA"}
@@ -2,12 +2,12 @@ import { SqlComponent } from "./SqlComponent";
2
2
  import { ForClause, FromClause, GroupByClause, HavingClause, LimitClause, OrderByClause, SelectClause, SourceExpression, WhereClause, WindowsClause as WindowClause, WithClause, CommonTable, OffsetClause, FetchClause } from "./Clause";
3
3
  import { ValueComponent } from "./ValueComponent";
4
4
  import { BinarySelectQuery } from "./BinarySelectQuery";
5
- import type { SelectQuery } from "./SelectQuery";
5
+ import type { SelectQuery, CTEOptions, CTEManagement } from "./SelectQuery";
6
6
  import { TableColumnResolver } from "../transformers/TableColumnResolver";
7
7
  /**
8
8
  * Represents a simple SELECT query in SQL.
9
9
  */
10
- export declare class SimpleSelectQuery extends SqlComponent implements SelectQuery {
10
+ export declare class SimpleSelectQuery extends SqlComponent implements SelectQuery, CTEManagement {
11
11
  static kind: symbol;
12
12
  withClause: WithClause | null;
13
13
  selectClause: SelectClause;
@@ -21,6 +21,7 @@ export declare class SimpleSelectQuery extends SqlComponent implements SelectQue
21
21
  offsetClause: OffsetClause | null;
22
22
  fetchClause: FetchClause | null;
23
23
  forClause: ForClause | null;
24
+ private cteNameCache;
24
25
  constructor(params: {
25
26
  selectClause: SelectClause;
26
27
  fromClause?: FromClause | null;
@@ -35,6 +36,12 @@ export declare class SimpleSelectQuery extends SqlComponent implements SelectQue
35
36
  forClause?: ForClause | null;
36
37
  withClause?: WithClause | null;
37
38
  });
39
+ /**
40
+ * Initializes the CTE name cache from existing withClause.
41
+ * Called during construction and when withClause is modified externally.
42
+ * @private
43
+ */
44
+ private initializeCTECache;
38
45
  /**
39
46
  * Creates a new BinarySelectQuery with this query as the left side and the provided query as the right side,
40
47
  * using UNION as the operator.
@@ -216,4 +223,161 @@ export declare class SimpleSelectQuery extends SqlComponent implements SelectQue
216
223
  * @param value Value to set
217
224
  */
218
225
  setParameter(name: string, value: any): this;
226
+ /**
227
+ * Returns this SimpleSelectQuery instance (identity function).
228
+ * @returns This SimpleSelectQuery instance
229
+ */
230
+ toSimpleQuery(): SimpleSelectQuery;
231
+ /**
232
+ * Adds a CTE (Common Table Expression) to the query.
233
+ *
234
+ * @param name CTE name/alias (must be non-empty)
235
+ * @param query SelectQuery to use as CTE
236
+ * @param options Optional configuration
237
+ * @param options.materialized PostgreSQL-specific: true = MATERIALIZED, false = NOT MATERIALIZED, null/undefined = no hint
238
+ *
239
+ * @throws {InvalidCTENameError} When name is empty or whitespace-only
240
+ * @throws {DuplicateCTEError} When CTE with same name already exists
241
+ *
242
+ * @example
243
+ * ```typescript
244
+ * // Basic CTE
245
+ * query.addCTE('active_users',
246
+ * SelectQueryParser.parse('SELECT * FROM users WHERE active = true')
247
+ * );
248
+ *
249
+ * // PostgreSQL MATERIALIZED CTE (forces materialization)
250
+ * query.addCTE('expensive_calc', expensiveQuery, { materialized: true });
251
+ *
252
+ * // PostgreSQL NOT MATERIALIZED CTE (prevents materialization)
253
+ * query.addCTE('simple_view', simpleQuery, { materialized: false });
254
+ * ```
255
+ *
256
+ * @remarks
257
+ * - MATERIALIZED/NOT MATERIALIZED is PostgreSQL-specific syntax
258
+ * - Other databases will ignore the materialized hint
259
+ * - CTE names must be unique within the query
260
+ * - Method supports fluent chaining
261
+ */
262
+ addCTE(name: string, query: SelectQuery, options?: CTEOptions): this;
263
+ /**
264
+ * Removes a CTE by name from the query.
265
+ *
266
+ * @param name CTE name to remove
267
+ *
268
+ * @throws {CTENotFoundError} When CTE with specified name doesn't exist
269
+ *
270
+ * @example
271
+ * ```typescript
272
+ * query.addCTE('temp_data', tempQuery);
273
+ * query.removeCTE('temp_data'); // Removes the CTE
274
+ *
275
+ * // Throws CTENotFoundError
276
+ * query.removeCTE('non_existent');
277
+ * ```
278
+ *
279
+ * @remarks
280
+ * - Throws error if CTE doesn't exist (strict mode for safety)
281
+ * - Use hasCTE() to check existence before removal if needed
282
+ * - Method supports fluent chaining
283
+ */
284
+ removeCTE(name: string): this;
285
+ /**
286
+ * Checks if a CTE with the given name exists in the query.
287
+ * Optimized with O(1) lookup using internal cache.
288
+ *
289
+ * @param name CTE name to check
290
+ * @returns true if CTE exists, false otherwise
291
+ *
292
+ * @example
293
+ * ```typescript
294
+ * query.addCTE('user_stats', statsQuery);
295
+ *
296
+ * if (query.hasCTE('user_stats')) {
297
+ * console.log('CTE exists');
298
+ * }
299
+ *
300
+ * query.removeCTE('user_stats');
301
+ * console.log(query.hasCTE('user_stats')); // false
302
+ * ```
303
+ *
304
+ * @remarks
305
+ * - Performs case-sensitive name matching
306
+ * - Returns false for queries without any CTEs
307
+ * - Useful for conditional CTE operations
308
+ * - O(1) performance using internal cache
309
+ */
310
+ hasCTE(name: string): boolean;
311
+ /**
312
+ * Returns an array of all CTE names in the query.
313
+ *
314
+ * @returns Array of CTE names in the order they were defined
315
+ *
316
+ * @example
317
+ * ```typescript
318
+ * const query = SelectQueryParser.parse('SELECT * FROM data').toSimpleQuery();
319
+ *
320
+ * // Empty query
321
+ * console.log(query.getCTENames()); // []
322
+ *
323
+ * // Add CTEs
324
+ * query.addCTE('users', userQuery);
325
+ * query.addCTE('orders', orderQuery);
326
+ *
327
+ * console.log(query.getCTENames()); // ['users', 'orders']
328
+ *
329
+ * // Use for validation
330
+ * const expectedCTEs = ['users', 'orders', 'products'];
331
+ * const actualCTEs = query.getCTENames();
332
+ * const missingCTEs = expectedCTEs.filter(name => !actualCTEs.includes(name));
333
+ * ```
334
+ *
335
+ * @remarks
336
+ * - Returns empty array for queries without CTEs
337
+ * - Names are returned in definition order
338
+ * - Useful for debugging and validation
339
+ * - Names reflect actual CTE aliases, not table references
340
+ * - Performance: O(n) but avoids redundant array mapping
341
+ */
342
+ getCTENames(): string[];
343
+ /**
344
+ * Replaces an existing CTE or adds a new one with the given name.
345
+ *
346
+ * @param name CTE name to replace/add (must be non-empty)
347
+ * @param query SelectQuery to use as CTE
348
+ * @param options Optional configuration
349
+ * @param options.materialized PostgreSQL-specific: true = MATERIALIZED, false = NOT MATERIALIZED, null/undefined = no hint
350
+ *
351
+ * @throws {InvalidCTENameError} When name is empty or whitespace-only
352
+ *
353
+ * @example
354
+ * ```typescript
355
+ * const query = SelectQueryParser.parse('SELECT * FROM final_data').toSimpleQuery();
356
+ * const oldQuery = SelectQueryParser.parse('SELECT id FROM old_table');
357
+ * const newQuery = SelectQueryParser.parse('SELECT id, status FROM new_table WHERE active = true');
358
+ *
359
+ * // Add initial CTE
360
+ * query.addCTE('data_source', oldQuery);
361
+ *
362
+ * // Replace with improved version
363
+ * query.replaceCTE('data_source', newQuery, { materialized: true });
364
+ *
365
+ * // Safe replacement - adds if doesn't exist
366
+ * query.replaceCTE('new_cte', newQuery); // Won't throw error
367
+ *
368
+ * // Chaining replacements
369
+ * query
370
+ * .replaceCTE('cte1', query1, { materialized: false })
371
+ * .replaceCTE('cte2', query2, { materialized: true });
372
+ * ```
373
+ *
374
+ * @remarks
375
+ * - Unlike addCTE(), this method won't throw error if CTE already exists
376
+ * - Unlike removeCTE(), this method won't throw error if CTE doesn't exist
377
+ * - Useful for upsert-style CTE operations
378
+ * - MATERIALIZED/NOT MATERIALIZED is PostgreSQL-specific
379
+ * - Method supports fluent chaining
380
+ * - Maintains CTE order when replacing existing CTEs
381
+ */
382
+ replaceCTE(name: string, query: SelectQuery, options?: CTEOptions): this;
219
383
  }
@@ -8,6 +8,7 @@ const ValueParser_1 = require("../parsers/ValueParser");
8
8
  const CTENormalizer_1 = require("../transformers/CTENormalizer");
9
9
  const SelectableColumnCollector_1 = require("../transformers/SelectableColumnCollector");
10
10
  const SourceParser_1 = require("../parsers/SourceParser");
11
+ const CTEError_1 = require("./CTEError");
11
12
  const SelectQueryParser_1 = require("../parsers/SelectQueryParser");
12
13
  const Formatter_1 = require("../transformers/Formatter");
13
14
  const UpstreamSelectQueryFinder_1 = require("../transformers/UpstreamSelectQueryFinder");
@@ -20,6 +21,8 @@ class SimpleSelectQuery extends SqlComponent_1.SqlComponent {
20
21
  constructor(params) {
21
22
  var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
22
23
  super();
24
+ // Performance optimization: O(1) CTE name lookups
25
+ this.cteNameCache = new Set();
23
26
  this.withClause = (_a = params.withClause) !== null && _a !== void 0 ? _a : null;
24
27
  this.selectClause = params.selectClause;
25
28
  this.fromClause = (_b = params.fromClause) !== null && _b !== void 0 ? _b : null;
@@ -32,6 +35,22 @@ class SimpleSelectQuery extends SqlComponent_1.SqlComponent {
32
35
  this.offsetClause = (_j = params.offsetClause) !== null && _j !== void 0 ? _j : null;
33
36
  this.fetchClause = (_k = params.fetchClause) !== null && _k !== void 0 ? _k : null;
34
37
  this.forClause = (_l = params.forClause) !== null && _l !== void 0 ? _l : null;
38
+ // Initialize CTE name cache from existing withClause
39
+ this.initializeCTECache();
40
+ }
41
+ /**
42
+ * Initializes the CTE name cache from existing withClause.
43
+ * Called during construction and when withClause is modified externally.
44
+ * @private
45
+ */
46
+ initializeCTECache() {
47
+ var _a;
48
+ this.cteNameCache.clear();
49
+ if ((_a = this.withClause) === null || _a === void 0 ? void 0 : _a.tables) {
50
+ for (const table of this.withClause.tables) {
51
+ this.cteNameCache.add(table.aliasExpression.table.name);
52
+ }
53
+ }
35
54
  }
36
55
  /**
37
56
  * Creates a new BinarySelectQuery with this query as the left side and the provided query as the right side,
@@ -368,6 +387,216 @@ class SimpleSelectQuery extends SqlComponent_1.SqlComponent {
368
387
  ParameterHelper_1.ParameterHelper.set(this, name, value);
369
388
  return this;
370
389
  }
390
+ /**
391
+ * Returns this SimpleSelectQuery instance (identity function).
392
+ * @returns This SimpleSelectQuery instance
393
+ */
394
+ toSimpleQuery() {
395
+ return this;
396
+ }
397
+ /**
398
+ * Adds a CTE (Common Table Expression) to the query.
399
+ *
400
+ * @param name CTE name/alias (must be non-empty)
401
+ * @param query SelectQuery to use as CTE
402
+ * @param options Optional configuration
403
+ * @param options.materialized PostgreSQL-specific: true = MATERIALIZED, false = NOT MATERIALIZED, null/undefined = no hint
404
+ *
405
+ * @throws {InvalidCTENameError} When name is empty or whitespace-only
406
+ * @throws {DuplicateCTEError} When CTE with same name already exists
407
+ *
408
+ * @example
409
+ * ```typescript
410
+ * // Basic CTE
411
+ * query.addCTE('active_users',
412
+ * SelectQueryParser.parse('SELECT * FROM users WHERE active = true')
413
+ * );
414
+ *
415
+ * // PostgreSQL MATERIALIZED CTE (forces materialization)
416
+ * query.addCTE('expensive_calc', expensiveQuery, { materialized: true });
417
+ *
418
+ * // PostgreSQL NOT MATERIALIZED CTE (prevents materialization)
419
+ * query.addCTE('simple_view', simpleQuery, { materialized: false });
420
+ * ```
421
+ *
422
+ * @remarks
423
+ * - MATERIALIZED/NOT MATERIALIZED is PostgreSQL-specific syntax
424
+ * - Other databases will ignore the materialized hint
425
+ * - CTE names must be unique within the query
426
+ * - Method supports fluent chaining
427
+ */
428
+ addCTE(name, query, options) {
429
+ var _a;
430
+ // Validate CTE name
431
+ if (!name || name.trim() === '') {
432
+ throw new CTEError_1.InvalidCTENameError(name, 'name cannot be empty or whitespace-only');
433
+ }
434
+ // Check for duplicate CTE name
435
+ if (this.hasCTE(name)) {
436
+ throw new CTEError_1.DuplicateCTEError(name);
437
+ }
438
+ const materialized = (_a = options === null || options === void 0 ? void 0 : options.materialized) !== null && _a !== void 0 ? _a : null;
439
+ const commonTable = new Clause_1.CommonTable(query, name, materialized);
440
+ this.appendWith(commonTable);
441
+ // Update cache for O(1) future lookups
442
+ this.cteNameCache.add(name);
443
+ return this;
444
+ }
445
+ /**
446
+ * Removes a CTE by name from the query.
447
+ *
448
+ * @param name CTE name to remove
449
+ *
450
+ * @throws {CTENotFoundError} When CTE with specified name doesn't exist
451
+ *
452
+ * @example
453
+ * ```typescript
454
+ * query.addCTE('temp_data', tempQuery);
455
+ * query.removeCTE('temp_data'); // Removes the CTE
456
+ *
457
+ * // Throws CTENotFoundError
458
+ * query.removeCTE('non_existent');
459
+ * ```
460
+ *
461
+ * @remarks
462
+ * - Throws error if CTE doesn't exist (strict mode for safety)
463
+ * - Use hasCTE() to check existence before removal if needed
464
+ * - Method supports fluent chaining
465
+ */
466
+ removeCTE(name) {
467
+ if (!this.hasCTE(name)) {
468
+ throw new CTEError_1.CTENotFoundError(name);
469
+ }
470
+ if (this.withClause) {
471
+ this.withClause.tables = this.withClause.tables.filter(table => table.aliasExpression.table.name !== name);
472
+ if (this.withClause.tables.length === 0) {
473
+ this.withClause = null;
474
+ }
475
+ }
476
+ // Update cache for O(1) future lookups
477
+ this.cteNameCache.delete(name);
478
+ return this;
479
+ }
480
+ /**
481
+ * Checks if a CTE with the given name exists in the query.
482
+ * Optimized with O(1) lookup using internal cache.
483
+ *
484
+ * @param name CTE name to check
485
+ * @returns true if CTE exists, false otherwise
486
+ *
487
+ * @example
488
+ * ```typescript
489
+ * query.addCTE('user_stats', statsQuery);
490
+ *
491
+ * if (query.hasCTE('user_stats')) {
492
+ * console.log('CTE exists');
493
+ * }
494
+ *
495
+ * query.removeCTE('user_stats');
496
+ * console.log(query.hasCTE('user_stats')); // false
497
+ * ```
498
+ *
499
+ * @remarks
500
+ * - Performs case-sensitive name matching
501
+ * - Returns false for queries without any CTEs
502
+ * - Useful for conditional CTE operations
503
+ * - O(1) performance using internal cache
504
+ */
505
+ hasCTE(name) {
506
+ return this.cteNameCache.has(name);
507
+ }
508
+ /**
509
+ * Returns an array of all CTE names in the query.
510
+ *
511
+ * @returns Array of CTE names in the order they were defined
512
+ *
513
+ * @example
514
+ * ```typescript
515
+ * const query = SelectQueryParser.parse('SELECT * FROM data').toSimpleQuery();
516
+ *
517
+ * // Empty query
518
+ * console.log(query.getCTENames()); // []
519
+ *
520
+ * // Add CTEs
521
+ * query.addCTE('users', userQuery);
522
+ * query.addCTE('orders', orderQuery);
523
+ *
524
+ * console.log(query.getCTENames()); // ['users', 'orders']
525
+ *
526
+ * // Use for validation
527
+ * const expectedCTEs = ['users', 'orders', 'products'];
528
+ * const actualCTEs = query.getCTENames();
529
+ * const missingCTEs = expectedCTEs.filter(name => !actualCTEs.includes(name));
530
+ * ```
531
+ *
532
+ * @remarks
533
+ * - Returns empty array for queries without CTEs
534
+ * - Names are returned in definition order
535
+ * - Useful for debugging and validation
536
+ * - Names reflect actual CTE aliases, not table references
537
+ * - Performance: O(n) but avoids redundant array mapping
538
+ */
539
+ getCTENames() {
540
+ var _a, _b;
541
+ return (_b = (_a = this.withClause) === null || _a === void 0 ? void 0 : _a.tables.map(table => table.aliasExpression.table.name)) !== null && _b !== void 0 ? _b : [];
542
+ }
543
+ /**
544
+ * Replaces an existing CTE or adds a new one with the given name.
545
+ *
546
+ * @param name CTE name to replace/add (must be non-empty)
547
+ * @param query SelectQuery to use as CTE
548
+ * @param options Optional configuration
549
+ * @param options.materialized PostgreSQL-specific: true = MATERIALIZED, false = NOT MATERIALIZED, null/undefined = no hint
550
+ *
551
+ * @throws {InvalidCTENameError} When name is empty or whitespace-only
552
+ *
553
+ * @example
554
+ * ```typescript
555
+ * const query = SelectQueryParser.parse('SELECT * FROM final_data').toSimpleQuery();
556
+ * const oldQuery = SelectQueryParser.parse('SELECT id FROM old_table');
557
+ * const newQuery = SelectQueryParser.parse('SELECT id, status FROM new_table WHERE active = true');
558
+ *
559
+ * // Add initial CTE
560
+ * query.addCTE('data_source', oldQuery);
561
+ *
562
+ * // Replace with improved version
563
+ * query.replaceCTE('data_source', newQuery, { materialized: true });
564
+ *
565
+ * // Safe replacement - adds if doesn't exist
566
+ * query.replaceCTE('new_cte', newQuery); // Won't throw error
567
+ *
568
+ * // Chaining replacements
569
+ * query
570
+ * .replaceCTE('cte1', query1, { materialized: false })
571
+ * .replaceCTE('cte2', query2, { materialized: true });
572
+ * ```
573
+ *
574
+ * @remarks
575
+ * - Unlike addCTE(), this method won't throw error if CTE already exists
576
+ * - Unlike removeCTE(), this method won't throw error if CTE doesn't exist
577
+ * - Useful for upsert-style CTE operations
578
+ * - MATERIALIZED/NOT MATERIALIZED is PostgreSQL-specific
579
+ * - Method supports fluent chaining
580
+ * - Maintains CTE order when replacing existing CTEs
581
+ */
582
+ replaceCTE(name, query, options) {
583
+ var _a;
584
+ // Validate CTE name
585
+ if (!name || name.trim() === '') {
586
+ throw new CTEError_1.InvalidCTENameError(name, 'name cannot be empty or whitespace-only');
587
+ }
588
+ // Remove existing CTE if it exists (don't throw error if not found)
589
+ if (this.hasCTE(name)) {
590
+ this.removeCTE(name);
591
+ }
592
+ // Add new CTE (but skip duplicate check since we just removed it)
593
+ const materialized = (_a = options === null || options === void 0 ? void 0 : options.materialized) !== null && _a !== void 0 ? _a : null;
594
+ const commonTable = new Clause_1.CommonTable(query, name, materialized);
595
+ this.appendWith(commonTable);
596
+ // Update cache for O(1) future lookups
597
+ this.cteNameCache.add(name);
598
+ return this;
599
+ }
371
600
  }
372
601
  exports.SimpleSelectQuery = SimpleSelectQuery;
373
602
  SimpleSelectQuery.kind = Symbol("SelectQuery");