rawsql-ts 0.1.0-beta.9 → 0.1.1-beta.2

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 (218) hide show
  1. package/README.md +214 -176
  2. package/dist/esm/index.js +17 -0
  3. package/dist/esm/index.js.map +1 -0
  4. package/dist/esm/models/BinarySelectQuery.js +137 -0
  5. package/dist/esm/models/BinarySelectQuery.js.map +1 -0
  6. package/dist/esm/models/Clause.js +289 -0
  7. package/dist/esm/models/Clause.js.map +1 -0
  8. package/dist/esm/models/KeywordTrie.js +48 -0
  9. package/dist/esm/models/KeywordTrie.js.map +1 -0
  10. package/dist/esm/models/Lexeme.js +18 -0
  11. package/dist/esm/models/Lexeme.js.map +1 -0
  12. package/dist/esm/models/SelectQuery.js +5 -0
  13. package/dist/esm/models/SelectQuery.js.map +1 -0
  14. package/dist/esm/models/SimpleSelectQuery.js +288 -0
  15. package/dist/esm/models/SimpleSelectQuery.js.map +1 -0
  16. package/dist/esm/models/SqlComponent.js +22 -0
  17. package/dist/esm/models/SqlComponent.js.map +1 -0
  18. package/dist/esm/models/ValueComponent.js +223 -0
  19. package/dist/esm/models/ValueComponent.js.map +1 -0
  20. package/dist/esm/models/ValuesQuery.js +12 -0
  21. package/dist/esm/models/ValuesQuery.js.map +1 -0
  22. package/dist/esm/parsers/CommandExpressionParser.js +120 -0
  23. package/dist/esm/parsers/CommandExpressionParser.js.map +1 -0
  24. package/dist/esm/parsers/CommonTableParser.js +58 -0
  25. package/dist/esm/parsers/CommonTableParser.js.map +1 -0
  26. package/dist/esm/parsers/ForClauseParser.js +54 -0
  27. package/dist/esm/parsers/ForClauseParser.js.map +1 -0
  28. package/dist/esm/parsers/FromClauseParser.js +43 -0
  29. package/dist/esm/parsers/FromClauseParser.js.map +1 -0
  30. package/dist/esm/parsers/FunctionExpressionParser.js +174 -0
  31. package/dist/esm/parsers/FunctionExpressionParser.js.map +1 -0
  32. package/dist/esm/parsers/GroupByParser.js +54 -0
  33. package/dist/esm/parsers/GroupByParser.js.map +1 -0
  34. package/dist/esm/parsers/HavingParser.js +32 -0
  35. package/dist/esm/parsers/HavingParser.js.map +1 -0
  36. package/dist/esm/parsers/IdentifierParser.js +35 -0
  37. package/dist/esm/parsers/IdentifierParser.js.map +1 -0
  38. package/dist/esm/parsers/JoinClauseParser.js +101 -0
  39. package/dist/esm/parsers/JoinClauseParser.js.map +1 -0
  40. package/dist/esm/parsers/KeywordParser.js +87 -0
  41. package/dist/esm/parsers/KeywordParser.js.map +1 -0
  42. package/dist/esm/parsers/LimitClauseParser.js +46 -0
  43. package/dist/esm/parsers/LimitClauseParser.js.map +1 -0
  44. package/dist/esm/parsers/LiteralParser.js +34 -0
  45. package/dist/esm/parsers/LiteralParser.js.map +1 -0
  46. package/dist/esm/parsers/OrderByClauseParser.js +73 -0
  47. package/dist/esm/parsers/OrderByClauseParser.js.map +1 -0
  48. package/dist/esm/parsers/OverExpressionParser.js +40 -0
  49. package/dist/esm/parsers/OverExpressionParser.js.map +1 -0
  50. package/dist/esm/parsers/ParameterExpressionParser.js +11 -0
  51. package/dist/esm/parsers/ParameterExpressionParser.js.map +1 -0
  52. package/dist/esm/parsers/ParenExpressionParser.js +29 -0
  53. package/dist/esm/parsers/ParenExpressionParser.js.map +1 -0
  54. package/dist/esm/parsers/PartitionByParser.js +49 -0
  55. package/dist/esm/parsers/PartitionByParser.js.map +1 -0
  56. package/dist/esm/parsers/SelectClauseParser.js +80 -0
  57. package/dist/esm/parsers/SelectClauseParser.js.map +1 -0
  58. package/dist/esm/parsers/SelectQueryParser.js +149 -0
  59. package/dist/esm/parsers/SelectQueryParser.js.map +1 -0
  60. package/dist/esm/parsers/SourceAliasExpressionParser.js +45 -0
  61. package/dist/esm/parsers/SourceAliasExpressionParser.js.map +1 -0
  62. package/dist/esm/parsers/SourceExpressionParser.js +31 -0
  63. package/dist/esm/parsers/SourceExpressionParser.js.map +1 -0
  64. package/dist/esm/parsers/SourceParser.js +115 -0
  65. package/dist/esm/parsers/SourceParser.js.map +1 -0
  66. package/dist/esm/parsers/SqlTokenizer.js +170 -0
  67. package/dist/esm/parsers/SqlTokenizer.js.map +1 -0
  68. package/dist/esm/parsers/StringSpecifierExpressionParser.js +18 -0
  69. package/dist/esm/parsers/StringSpecifierExpressionParser.js.map +1 -0
  70. package/dist/esm/parsers/UnaryExpressionParser.js +26 -0
  71. package/dist/esm/parsers/UnaryExpressionParser.js.map +1 -0
  72. package/dist/esm/parsers/ValueParser.js +132 -0
  73. package/dist/esm/parsers/ValueParser.js.map +1 -0
  74. package/dist/esm/parsers/ValuesQueryParser.js +82 -0
  75. package/dist/esm/parsers/ValuesQueryParser.js.map +1 -0
  76. package/dist/esm/parsers/WhereClauseParser.js +32 -0
  77. package/dist/esm/parsers/WhereClauseParser.js.map +1 -0
  78. package/dist/esm/parsers/WindowClauseParser.js +41 -0
  79. package/dist/esm/parsers/WindowClauseParser.js.map +1 -0
  80. package/dist/esm/parsers/WindowExpressionParser.js +159 -0
  81. package/dist/esm/parsers/WindowExpressionParser.js.map +1 -0
  82. package/dist/esm/parsers/WithClauseParser.js +53 -0
  83. package/dist/esm/parsers/WithClauseParser.js.map +1 -0
  84. package/dist/esm/tokenReaders/BaseTokenReader.js +78 -0
  85. package/dist/esm/tokenReaders/BaseTokenReader.js.map +1 -0
  86. package/dist/esm/tokenReaders/CommandTokenReader.js +141 -0
  87. package/dist/esm/tokenReaders/CommandTokenReader.js.map +1 -0
  88. package/dist/esm/tokenReaders/FunctionTokenReader.js +41 -0
  89. package/dist/esm/tokenReaders/FunctionTokenReader.js.map +1 -0
  90. package/dist/esm/tokenReaders/IdentifierTokenReader.js +66 -0
  91. package/dist/esm/tokenReaders/IdentifierTokenReader.js.map +1 -0
  92. package/dist/esm/tokenReaders/LiteralTokenReader.js +185 -0
  93. package/dist/esm/tokenReaders/LiteralTokenReader.js.map +1 -0
  94. package/dist/esm/tokenReaders/OperatorTokenReader.js +94 -0
  95. package/dist/esm/tokenReaders/OperatorTokenReader.js.map +1 -0
  96. package/dist/esm/tokenReaders/ParameterTokenReader.js +40 -0
  97. package/dist/esm/tokenReaders/ParameterTokenReader.js.map +1 -0
  98. package/dist/esm/tokenReaders/StringSpecifierTokenReader.js +27 -0
  99. package/dist/esm/tokenReaders/StringSpecifierTokenReader.js.map +1 -0
  100. package/dist/esm/tokenReaders/SymbolTokenReader.js +31 -0
  101. package/dist/esm/tokenReaders/SymbolTokenReader.js.map +1 -0
  102. package/dist/esm/tokenReaders/TokenReaderManager.js +106 -0
  103. package/dist/esm/tokenReaders/TokenReaderManager.js.map +1 -0
  104. package/dist/esm/tokenReaders/TypeTokenReader.js +55 -0
  105. package/dist/esm/tokenReaders/TypeTokenReader.js.map +1 -0
  106. package/dist/esm/transformers/CTEBuilder.js +184 -0
  107. package/dist/esm/transformers/CTEBuilder.js.map +1 -0
  108. package/dist/esm/transformers/CTECollector.js +384 -0
  109. package/dist/esm/transformers/CTECollector.js.map +1 -0
  110. package/dist/esm/transformers/CTEDisabler.js +321 -0
  111. package/dist/esm/transformers/CTEDisabler.js.map +1 -0
  112. package/dist/esm/transformers/CTEInjector.js +79 -0
  113. package/dist/esm/transformers/CTEInjector.js.map +1 -0
  114. package/dist/esm/transformers/CTENormalizer.js +42 -0
  115. package/dist/esm/transformers/CTENormalizer.js.map +1 -0
  116. package/dist/esm/transformers/Formatter.js +463 -0
  117. package/dist/esm/transformers/Formatter.js.map +1 -0
  118. package/dist/esm/transformers/QueryConverter.js +115 -0
  119. package/dist/esm/transformers/QueryConverter.js.map +1 -0
  120. package/dist/esm/transformers/SelectValueCollector.js +245 -0
  121. package/dist/esm/transformers/SelectValueCollector.js.map +1 -0
  122. package/dist/esm/transformers/SelectableColumnCollector.js +318 -0
  123. package/dist/esm/transformers/SelectableColumnCollector.js.map +1 -0
  124. package/dist/esm/transformers/TableColumnResolver.js +2 -0
  125. package/dist/esm/transformers/TableColumnResolver.js.map +1 -0
  126. package/dist/esm/transformers/TableSourceCollector.js +380 -0
  127. package/dist/esm/transformers/TableSourceCollector.js.map +1 -0
  128. package/dist/esm/transformers/UpstreamSelectQueryFinder.js +125 -0
  129. package/dist/esm/transformers/UpstreamSelectQueryFinder.js.map +1 -0
  130. package/dist/esm/types/index.d.ts +14 -0
  131. package/dist/esm/types/models/BinarySelectQuery.d.ts +91 -0
  132. package/dist/esm/types/models/Clause.d.ts +189 -0
  133. package/dist/esm/types/models/KeywordTrie.d.ts +11 -0
  134. package/dist/esm/types/models/Lexeme.d.ts +25 -0
  135. package/dist/esm/types/models/SelectQuery.d.ts +5 -0
  136. package/dist/esm/types/models/SimpleSelectQuery.d.ts +167 -0
  137. package/dist/esm/types/models/SqlComponent.d.ts +18 -0
  138. package/dist/esm/types/models/ValueComponent.d.ts +158 -0
  139. package/dist/esm/types/models/ValuesQuery.d.ts +10 -0
  140. package/dist/esm/types/parsers/CommandExpressionParser.d.ts +15 -0
  141. package/dist/esm/types/parsers/CommonTableParser.d.ts +9 -0
  142. package/dist/esm/types/parsers/ForClauseParser.d.ts +9 -0
  143. package/dist/esm/types/parsers/FromClauseParser.d.ts +9 -0
  144. package/dist/esm/types/parsers/FunctionExpressionParser.d.ts +22 -0
  145. package/dist/esm/types/parsers/GroupByParser.d.ts +10 -0
  146. package/dist/esm/types/parsers/HavingParser.d.ts +9 -0
  147. package/dist/esm/types/parsers/IdentifierParser.d.ts +8 -0
  148. package/dist/esm/types/parsers/JoinClauseParser.d.ts +14 -0
  149. package/dist/esm/types/parsers/KeywordParser.d.ts +17 -0
  150. package/dist/esm/types/parsers/LimitClauseParser.d.ts +9 -0
  151. package/dist/esm/types/parsers/LiteralParser.d.ts +8 -0
  152. package/dist/esm/types/parsers/OrderByClauseParser.d.ts +10 -0
  153. package/dist/esm/types/parsers/OverExpressionParser.d.ts +9 -0
  154. package/dist/esm/types/parsers/ParameterExpressionParser.d.ts +8 -0
  155. package/dist/esm/types/parsers/ParenExpressionParser.d.ts +8 -0
  156. package/dist/esm/types/parsers/PartitionByParser.d.ts +9 -0
  157. package/dist/esm/types/parsers/SelectClauseParser.d.ts +10 -0
  158. package/dist/esm/types/parsers/SelectQueryParser.d.ts +13 -0
  159. package/dist/esm/types/parsers/SourceAliasExpressionParser.d.ts +8 -0
  160. package/dist/esm/types/parsers/SourceExpressionParser.d.ts +8 -0
  161. package/dist/esm/types/parsers/SourceParser.d.ts +13 -0
  162. package/dist/esm/types/parsers/SqlTokenizer.d.ts +64 -0
  163. package/dist/esm/types/parsers/StringSpecifierExpressionParser.d.ts +8 -0
  164. package/dist/esm/types/parsers/UnaryExpressionParser.d.ts +8 -0
  165. package/dist/esm/types/parsers/ValueParser.d.ts +14 -0
  166. package/dist/esm/types/parsers/ValuesQueryParser.d.ts +10 -0
  167. package/dist/esm/types/parsers/WhereClauseParser.d.ts +9 -0
  168. package/dist/esm/types/parsers/WindowClauseParser.d.ts +9 -0
  169. package/dist/esm/types/parsers/WindowExpressionParser.d.ts +12 -0
  170. package/dist/esm/types/parsers/WithClauseParser.d.ts +9 -0
  171. package/dist/esm/types/tokenReaders/BaseTokenReader.d.ts +43 -0
  172. package/dist/esm/types/tokenReaders/CommandTokenReader.d.ts +7 -0
  173. package/dist/esm/types/tokenReaders/FunctionTokenReader.d.ts +11 -0
  174. package/dist/esm/types/tokenReaders/IdentifierTokenReader.d.ts +15 -0
  175. package/dist/esm/types/tokenReaders/LiteralTokenReader.d.ts +23 -0
  176. package/dist/esm/types/tokenReaders/OperatorTokenReader.d.ts +5 -0
  177. package/dist/esm/types/tokenReaders/ParameterTokenReader.d.ts +11 -0
  178. package/dist/esm/types/tokenReaders/StringSpecifierTokenReader.d.ts +8 -0
  179. package/dist/esm/types/tokenReaders/SymbolTokenReader.d.ts +12 -0
  180. package/dist/esm/types/tokenReaders/TokenReaderManager.d.ts +53 -0
  181. package/dist/esm/types/tokenReaders/TypeTokenReader.d.ts +11 -0
  182. package/dist/esm/types/transformers/CTEBuilder.d.ts +52 -0
  183. package/dist/esm/types/transformers/CTECollector.d.ts +82 -0
  184. package/dist/esm/types/transformers/CTEDisabler.d.ts +77 -0
  185. package/dist/esm/types/transformers/CTEInjector.d.ts +40 -0
  186. package/dist/esm/types/transformers/CTENormalizer.d.ts +25 -0
  187. package/dist/esm/types/transformers/Formatter.d.ts +82 -0
  188. package/dist/esm/types/transformers/QueryConverter.d.ts +41 -0
  189. package/dist/esm/types/transformers/SelectValueCollector.d.ts +60 -0
  190. package/dist/esm/types/transformers/SelectableColumnCollector.d.ts +72 -0
  191. package/dist/esm/types/transformers/TableColumnResolver.d.ts +10 -0
  192. package/dist/esm/types/transformers/TableSourceCollector.d.ts +92 -0
  193. package/dist/esm/types/transformers/UpstreamSelectQueryFinder.d.ts +27 -0
  194. package/dist/esm/types/utils/charLookupTable.d.ts +11 -0
  195. package/dist/esm/types/utils/stringUtils.d.ts +43 -0
  196. package/dist/esm/utils/charLookupTable.js +69 -0
  197. package/dist/esm/utils/charLookupTable.js.map +1 -0
  198. package/dist/esm/utils/stringUtils.js +164 -0
  199. package/dist/esm/utils/stringUtils.js.map +1 -0
  200. package/dist/index.d.ts +1 -0
  201. package/dist/index.js +1 -0
  202. package/dist/index.js.map +1 -1
  203. package/dist/transformers/CTECollector.d.ts +2 -1
  204. package/dist/transformers/CTECollector.js +5 -1
  205. package/dist/transformers/CTECollector.js.map +1 -1
  206. package/dist/transformers/CTEDisabler.js +1 -1
  207. package/dist/transformers/CTEDisabler.js.map +1 -1
  208. package/dist/transformers/Formatter.js +1 -1
  209. package/dist/transformers/Formatter.js.map +1 -1
  210. package/dist/transformers/SelectValueCollector.d.ts +2 -4
  211. package/dist/transformers/SelectValueCollector.js.map +1 -1
  212. package/dist/transformers/SelectableColumnCollector.d.ts +2 -0
  213. package/dist/transformers/SelectableColumnCollector.js +17 -3
  214. package/dist/transformers/SelectableColumnCollector.js.map +1 -1
  215. package/dist/transformers/TableColumnResolver.d.ts +10 -0
  216. package/dist/transformers/TableColumnResolver.js +3 -0
  217. package/dist/transformers/TableColumnResolver.js.map +1 -0
  218. package/package.json +15 -2
package/README.md CHANGED
@@ -1,228 +1,262 @@
1
1
  # rawsql-ts
2
2
 
3
- rawsql-ts is a TypeScript SQL parser that performs Abstract Syntax Tree (AST) analysis for advanced SQL processing and transformation.
3
+ ![No external dependencies](https://img.shields.io/badge/dependencies-none-brightgreen)
4
+ ![Browser Support](https://img.shields.io/badge/browser-%F0%9F%9A%80-brightgreen)
5
+ ![npm version](https://img.shields.io/npm/v/rawsql-ts)
6
+ ![npm downloads](https://img.shields.io/npm/dm/rawsql-ts)
7
+ ![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)
4
8
 
5
- > **Note:** This library is currently in beta. The API may change without notice until the v1.0 release.
9
+ rawsql-ts is a high-performance SQL parser and AST transformer library written in TypeScript. It is designed for extensibility and advanced SQL analysis, with initial focus on PostgreSQL syntax but not limited to it. The library enables easy SQL parsing, transformation, and analysis for a wide range of SQL dialects.
6
10
 
7
- ## Installation
11
+ > **Note:** This library is currently in beta. The API may change until the v1.0 release.
12
+
13
+ ---
14
+
15
+ 💡 **Key Advantages**
16
+
17
+ With rawsql-ts, raw SQL can be represented as objects, enabling flexible manipulation of SQL statements directly within your program. Objectified SQL can be partially transformed, decomposed into manageable components, and recombined as needed. This approach dramatically improves the maintainability and reusability of complex SQL, making even large-scale queries easy to manage and refactor.
18
+
19
+ ---
20
+
21
+ ## Features
8
22
 
9
- Install the package from npm as follows:
23
+ - Zero dependencies: fully self-contained and lightweight
24
+ - High-speed SQL parsing and AST analysis
25
+ - Rich utilities for SQL structure transformation and analysis
26
+
27
+ ## ✨ Browser & CDN Ready!
28
+
29
+ You can use rawsql-ts directly in modern browsers via CDN (unpkg/jsdelivr)!
30
+ No Node.js dependencies, no build tools required.
31
+ Just import it like this:
32
+
33
+ ```html
34
+ <!-- Always get the latest version -->
35
+ <script type="module">
36
+ import { parse } from "https://unpkg.com/rawsql-ts/dist/esm/index.js";
37
+ </script>
38
+ ```
39
+
40
+ ```html
41
+ <!-- Pin a specific version for stability -->
42
+ <script type="module">
43
+ import { parse } from "https://unpkg.com/rawsql-ts@0.1.0-beta.12/dist/esm/index.js";
44
+ </script>
45
+ ```
46
+
47
+ ---
48
+
49
+ ## Installation
10
50
 
11
51
  ```bash
12
52
  npm install rawsql-ts
13
53
  ```
14
54
 
15
- ## Usage
16
-
17
- Basic usage example:
55
+ ## Quick Start
18
56
 
19
57
  ```typescript
20
- import { SelectQueryParser } from 'rawsql-ts';
21
- import { Formatter } from 'rawsql-ts';
58
+ import { SelectQueryParser, Formatter } from 'rawsql-ts';
22
59
 
23
- const sql = `SELECT id, name FROM users WHERE active = TRUE`;
60
+ const sql = `SELECT user_id, name FROM users WHERE active = TRUE`;
24
61
  const query = SelectQueryParser.parse(sql);
25
62
  const formatter = new Formatter();
26
63
  const formattedSql = formatter.format(query);
64
+
27
65
  console.log(formattedSql);
28
- // => select "id", "name" from "users" where "active" = true
66
+ // => select "user_id", "name" from "users" where "active" = true
29
67
  ```
30
68
 
31
69
  ---
32
70
 
33
- ## 🧩 Parsing Features
34
-
35
- rawsql-ts provides the following main parser class for converting SQL text into an Abstract Syntax Tree (AST):
71
+ ## Main Parser Features
36
72
 
37
73
  - **SelectQueryParser**
38
- Parses complete SELECT and VALUES queries, including support for CTEs (WITH), UNION/INTERSECT/EXCEPT, subqueries, and all major SQL clauses. Handles PostgreSQL-specific syntax and advanced query structures.
39
-
40
- **Key methods:**
74
+ The main class for converting SELECT and VALUES statements into AST. Fully supports CTEs (WITH), UNION/INTERSECT/EXCEPT, subqueries, and PostgreSQL-specific syntax.
41
75
  - `parse(sql: string): SelectQuery`
42
- Parses a SQL string and returns the root AST node for the query. Throws an error if the SQL is invalid or contains extra tokens.
43
-
44
- **Notes:**
45
- - Only PostgreSQL syntax is supported at this time.
46
- - Only SELECT and VALUES queries are supported (INSERT/UPDATE/DELETE are not yet implemented).
47
- - All SQL comments are removed during parsing.
48
-
49
- This class is designed to handle all practical SQL parsing needs for SELECT/VALUES queries in PostgreSQL, including:
50
- - CTEs (WITH), including recursive and materialized options
51
- - UNION, INTERSECT, EXCEPT, and subqueries
52
- - Window functions and analytic clauses
53
- - Complex expressions, functions, and operators
54
- - Robust error handling with detailed messages
55
- - Accurate tokenization, including comments and special literals
76
+ Converts a SQL string to an AST. Throws an exception on error.
77
+ - Supports only PostgreSQL syntax
78
+ - Only SELECT and VALUES are supported (INSERT/UPDATE/DELETE are not yet implemented)
79
+ - SQL comments are automatically removed
80
+ - Handles CTEs (WITH), UNION/INTERSECT/EXCEPT, subqueries, window functions, complex expressions, and functions
81
+ - Provides detailed error messages
82
+ - Highly accurate tokenization
56
83
 
57
84
  ---
58
85
 
59
86
  ## Core SQL Query Classes
60
87
 
61
- The following classes are the primary building blocks for representing and manipulating SQL queries in rawsql-ts:
62
-
63
- ### SimpleSelectQuery
64
-
65
- Represents a single, standard SQL SELECT statement (not a UNION or VALUES). This class encapsulates all major clauses such as SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY, and more.
66
-
67
- **Key methods:**
68
- - `toUnion`, `toUnionAll`, `toIntersect`, `toExcept`, etc.
69
- - Combine this query with another using UNION, INTERSECT, EXCEPT, etc., returning a BinarySelectQuery.
70
- - `appendWhere`, `appendWhereRaw`
71
- - Add a new condition to the WHERE clause (as AST or raw SQL string).
72
- - `appendHaving`, `appendHavingRaw`
73
- - Add a new condition to the HAVING clause.
74
- - `innerJoin`, `leftJoin`, `rightJoin`, `innerJoinRaw`, etc.
75
- - Add JOIN clauses to the query, either as AST or from raw SQL.
76
- - `toSource`
77
- - Wrap this query as a subquery (for use in FROM/JOIN, etc.).
78
- - `appendWith`, `appendWithRaw`
79
- - Add CTEs (WITH clause) to the query.
80
-
81
- ### BinarySelectQuery
82
-
83
- Represents a binary SQL query, such as `SELECT ... UNION SELECT ...`, `INTERSECT`, or `EXCEPT`. This class holds a left and right query and the operator between them.
88
+ - **SimpleSelectQuery**
89
+ Represents a standard SELECT statement. Supports all major clauses such as WHERE, GROUP BY, JOIN, and CTE.
90
+ - `toUnion`, `toUnionAll`, ... for UNION operations
91
+ - `appendWhere`, `appendWhereRaw` to add WHERE conditions
92
+ - `innerJoin`, `leftJoin`, ... to add JOINs
93
+ - `toSource` to wrap as a subquery
94
+ - `appendWith`, `appendWithRaw` to add CTEs
84
95
 
85
- **Key methods:**
86
- - `union`, `unionAll`, `intersect`, `intersectAll`, `except`, `exceptAll`
87
- - Chain additional queries to the current binary query.
88
- - `appendSelectQuery`
89
- - Add a new query with a custom operator.
90
- - `toSource`
91
- - Wrap this binary query as a subquery (for use in FROM/JOIN, etc.).
92
- - `unionRaw`, `intersectRaw`, etc.
93
- - Add a new query by parsing a raw SQL string and combining it.
96
+ - **BinarySelectQuery**
97
+ Represents binary SQL queries such as UNION, INTERSECT, and EXCEPT.
98
+ - `union`, `intersect`, ... to combine queries
99
+ - `toSource` to wrap as a subquery
100
+ - `unionRaw`, ... to combine with raw SQL
94
101
 
95
- ### ValuesQuery
96
-
97
- Represents a SQL `VALUES` clause, such as `VALUES (1, 'a'), (2, 'b')`, which is used for inline data tables.
98
-
99
- **Key methods:**
100
- - (Primarily the constructor and tuple access)
101
- - This class can be used as a subquery source or wrapped with QueryNormalizer to convert it into a standard SELECT query.
102
+ - **ValuesQuery**
103
+ For inline tables like `VALUES (1, 'a'), (2, 'b')`.
104
+ - Can be used as a subquery or converted to SELECT with QueryNormalizer
102
105
 
103
106
  ---
104
107
 
105
- These classes are designed to be flexible and allow for robust construction, combination, and transformation of SQL queries. For further details, please refer to the source code.
106
-
107
- ---
108
+ ## AST Transformer Features
108
109
 
109
- ## 🛠️ Transformer Features (AST Transformers)
110
-
111
- rawsql-ts provides a suite of AST (Abstract Syntax Tree) transformers for advanced SQL analysis and manipulation. These utilities are intended for engineers who require programmatic extraction, analysis, or transformation of SQL query structures.
110
+ A suite of utilities for transforming and analyzing SQL ASTs.
112
111
 
113
112
  ### Main Transformers
114
113
 
115
114
  - **Formatter**
116
- Converts SQL ASTs into standardized SQL text, handling identifier escaping and formatting for all SQL components.
117
- **Note:** Output formatting is currently limited to single-line (compact) style.
118
-
115
+ Converts ASTs to formatted SQL strings. Handles identifier escaping. Output is currently single-line (compact) style.
119
116
  - **SelectValueCollector**
120
- Extracts all columns, including aliases and expressions, from SELECT clauses. Supports wildcard expansion (e.g., `*`, `table.*`) when table structure information is provided.
121
-
117
+ Extracts all columns, aliases, and expressions from SELECT clauses. Supports wildcard expansion (e.g., `*`, `table.*`) with TableColumnResolver.
122
118
  - **SelectableColumnCollector**
123
- Collects all column references in a query that can be included in a SELECT clause. Gathers all columns available from root FROM/JOIN sources.
124
-
119
+ Collects all columns available from root FROM/JOIN sources.
125
120
  - **TableSourceCollector**
126
- Collects all table and subquery sources from the FROM and JOIN clauses. This utility helps you extract all logical sources (tables, subqueries, CTEs, etc.) referenced in the root query, including their aliases. Useful for schema analysis, join logic, and query visualization.
127
-
121
+ Collects all table and subquery sources from FROM and JOIN clauses.
128
122
  - **CTECollector**
129
- Collects all Common Table Expressions (CTEs) from WITH clauses, subqueries, and UNION queries. Supports both nested and recursive CTEs.
130
-
123
+ Collects all CTEs from WITH clauses, subqueries, and UNION queries.
131
124
  - **UpstreamSelectQueryFinder**
132
- Identifies upstream SELECT queries that provide specific columns by traversing CTEs, subqueries, and UNION branches.
133
-
125
+ Finds upstream SELECT queries that provide specific columns by traversing CTEs, subqueries, and UNION branches.
134
126
  - **CTENormalizer**
135
- Consolidates all Common Table Expressions (CTEs) from any part of a query (including nested subqueries, JOINs, and UNIONs) into a single root-level WITH clause. If duplicate CTE names with different definitions are detected, an error is thrown to prevent ambiguity.
136
-
127
+ Consolidates all CTEs into a single root-level WITH clause. Throws an error if duplicate CTE names with different definitions are found.
137
128
  - **QueryNormalizer**
138
- Converts any SELECT query (including UNION, EXCEPT, or VALUES queries) into a standard SimpleSelectQuery format. For UNION or EXCEPT, the query is wrapped as a subquery with an alias (e.g., SELECT * FROM (...)). For VALUES, sequential column names (column1, column2, ...) are generated and the VALUES are wrapped in a subquery. This ensures a predictable query structure for downstream processing.
129
+ Converts any SELECT/UNION/VALUES query into a standard SimpleSelectQuery. Handles subquery wrapping and automatic column name generation.
130
+ - **TableColumnResolver**
131
+ A function type for resolving column names from a table name, mainly used for wildcard expansion (e.g., `table.*`). Used by analyzers like SelectValueCollector.
132
+ ```typescript
133
+ export type TableColumnResolver = (tableName: string) => string[];
134
+ ```
139
135
 
140
- ---
136
+ ---
141
137
 
142
- ### Example Usage
138
+ ## Usage Example
143
139
 
144
140
  ```typescript
145
- import { SelectQueryParser } from 'rawsql-ts';
146
- import { SelectableColumnCollector } from 'rawsql-ts/transformers/SelectableColumnCollector';
147
- import { SelectValueCollector } from 'rawsql-ts/transformers/SelectValueCollector';
148
- import { TableSourceCollector } from 'rawsql-ts/transformers/TableSourceCollector';
141
+ import { TableColumnResolver, SelectQueryParser, SelectableColumnCollector, SelectValueCollector, TableSourceCollector, Formatter } from 'rawsql-ts';
149
142
 
150
- const sql = `SELECT u.id, u.name FROM users u JOIN posts p ON u.id = p.user_id`;
151
- const query = SelectQueryParser.parse(sql);
143
+ // TableColumnResolver example
144
+ const resolver: TableColumnResolver = (tableName) => {
145
+ if (tableName === 'users') return ['user_id', 'user_name', 'email'];
146
+ if (tableName === 'posts') return ['post_id', 'user_id', 'title', 'content'];
147
+ return [];
148
+ };
152
149
 
153
- // Collects all selectable columns from the query (from FROM/JOIN sources)
154
- const selectableColumnCollector = new SelectableColumnCollector();
155
- const selectableColumns = selectableColumnCollector.collect(query);
156
- // ["id", "name", "user_id", ...]
157
- console.log(selectableColumns.map(col => col.name));
150
+ const sql = `SELECT u.*, p.title as post_title FROM users u INNER JOIN posts p ON u.user_id = p.user_id`;
151
+ const query = SelectQueryParser.parse(sql);
152
+ const formatter = new Formatter();
158
153
 
159
- // Collects all values and aliases from the SELECT clause
160
- const selectValueCollector = new SelectValueCollector();
154
+ // Collects information from the SELECT clause.
155
+ // To expand wildcards, you must specify a TableColumnResolver.
156
+ const selectValueCollector = new SelectValueCollector(resolver);
161
157
  const selectValues = selectValueCollector.collect(query);
162
- // ["id", "name"]
163
- console.log(selectValues.map(val => val.alias || val.expression.toString()));
164
-
165
- // Collects all table and subquery sources from the FROM/JOIN clauses
158
+ // Log the name and formatted value of each select value
159
+ console.log('Select values:');
160
+ selectValues.forEach(val => {
161
+ console.log(` name: ${val.name}, value: ${formatter.format(val.value)}`);
162
+ });
163
+ /*
164
+ Select values:
165
+ name: post_title, value: "p"."title"
166
+ name: user_id, value: "u"."user_id"
167
+ name: user_name, value: "u"."user_name"
168
+ name: email, value: "u"."email"
169
+ */
170
+
171
+ // Collects selectable columns from the FROM/JOIN clauses.
172
+ // You can get accurate information by specifying a TableColumnResolver.
173
+ // If omitted, the information will be inferred from the query content.
174
+ const selectableColumnCollector = new SelectableColumnCollector(resolver);
175
+ const selectableColumns = selectableColumnCollector.collect(query);
176
+ // Log detailed info for each selectable column
177
+ console.log('Selectable columns:');
178
+ selectableColumns.forEach(val => {
179
+ console.log(` name: ${val.name}, value: ${formatter.format(val.value)}`);
180
+ });
181
+ /*
182
+ Selectable columns:
183
+ name: post_title, value: "p"."title"
184
+ name: user_id, value: "u"."user_id"
185
+ name: user_name, value: "u"."user_name"
186
+ name: email, value: "u"."email"
187
+ name: post_id, value: "p"."post_id"
188
+ name: title, value: "p"."title"
189
+ name: content, value: "p"."content"
190
+ */
191
+
192
+ // Retrieves physical table sources.
166
193
  const tableSourceCollector = new TableSourceCollector();
167
194
  const sources = tableSourceCollector.collect(query);
168
- // ["u", "p"]
169
- console.log(sources.map(src => src.alias || src.name));
195
+ // Log detailed info for each source
196
+ console.log('Sources:');
197
+ sources.forEach(src => {
198
+ console.log(` name: ${src.getSourceName()}`);
199
+ });
200
+ /*
201
+ TableSources:
202
+ name: users
203
+ name: posts
204
+ */
170
205
  ```
171
206
 
172
207
  ---
173
208
 
174
- ## Practical Example: Table Join
209
+ ## Advanced Example: Table Join
175
210
 
176
- The following example demonstrates how to join two tables using rawsql-ts. It is not necessary to understand the internal structure of the SelectQuery class or manage alias names manually. By specifying the join key(s), the library automatically generates the ON clause and handles all aliasing and subquery details.
211
+ This example demonstrates how to join two tables using rawsql-ts. You do not need to understand the internal structure or manage aliases manually. By specifying the join key(s), the ON clause is generated automatically.
177
212
 
178
213
  ```typescript
179
- import { SelectQueryParser } from 'rawsql-ts';
180
- import { Formatter } from 'rawsql-ts';
214
+ import { SelectQueryParser, Formatter, SimpleSelectQuery } from 'rawsql-ts';
215
+
216
+ // Parse the base query
217
+ const query = SelectQueryParser.parse('SELECT u.user_id, u.name FROM users u') as SimpleSelectQuery;
181
218
 
182
- // Parse two separate queries
183
- const userQuery = SelectQueryParser.parse('SELECT user_id, user_name FROM users');
184
- const postQuery = SelectQueryParser.parse('SELECT post_id, user_id, title FROM posts');
219
+ // Add LEFT JOIN using the leftJoinRaw method (join on user_id)
220
+ query.leftJoinRaw('orders', 'o', ['user_id']);
185
221
 
186
- // Join the two queries using innerJoin
187
- // Provide the join key(s) as an array; the ON clause will be generated automatically.
188
- const joinedQuery = userQuery.innerJoin(postQuery, ['user_id']);
222
+ // Add WHERE clause
223
+ query.appendWhereRaw('o.order_id IS NULL');
189
224
 
190
- // Format the joined query back to SQL
191
225
  const formatter = new Formatter();
192
- const sql = formatter.format(joinedQuery);
193
- console.log(sql);
194
- // Output:
195
- // select "user_id", "user_name", "post_id", "title" from "users" inner join (select "post_id", "user_id", "title" from "posts") on "users"."user_id" = "posts"."user_id"
226
+ const formattedSql = formatter.format(query);
227
+
228
+ console.log(formattedSql);
229
+ // => select "u"."user_id", "u"."name" from "users" as "u" left join "orders" as "o" on "u"."user_id" = "o"."user_id" where "o"."order_id" is null
196
230
  ```
197
231
 
198
232
  **Key Points:**
199
- - It is not necessary to understand the internal implementation of SelectQuery to perform join operations.
200
- - Only the join key(s) (e.g., `['user_id']`) need to be specified. The ON clause is generated automatically.
201
- - Alias names and subquery handling are managed by the library, eliminating the need for manual intervention.
202
- - This approach enables straightforward joining of queries, even without detailed knowledge of the SQL structure or AST internals.
233
+ - No need to understand internal implementation or alias management
234
+ - Specify only the join key(s) (e.g., `['user_id']`); the ON clause is generated automatically
235
+ - Subqueries and aliases are handled automatically
236
+ - You can join queries without detailed knowledge of SQL structure or AST internals
203
237
 
204
238
  ---
205
239
 
206
- By utilizing these transformer utilities, you can perform advanced SQL analysis and manipulation with reliability and consistency.
207
-
208
240
  ## Benchmarks
209
241
 
210
- This project includes benchmarking functionality. To run benchmarks, execute:
242
+ This project includes a comprehensive benchmark suite to evaluate the performance of `rawsql-ts` in comparison with other popular libraries such as `node-sql-parser` and `sql-formatter`.
243
+
244
+ ### How to Run
211
245
 
212
246
  ```bash
213
247
  npm run benchmark
214
248
  ```
215
249
 
216
- ## Benchmark Details
250
+ ### Benchmark Details
217
251
 
218
- The benchmark suite evaluates the SQL parsing and formatting performance of `rawsql-ts` in comparison to popular libraries such as `sql-formatter` and `node-sql-parser`. Queries of varying complexity are tested:
252
+ The benchmark suite measures SQL parsing and formatting speed across queries of varying complexity:
219
253
 
220
- - **Tokens20**: Simple `SELECT` query with a basic `WHERE` condition (~20 tokens)
221
- - **Tokens70**: Medium complexity query with `JOIN`s and multiple conditions (~70 tokens)
222
- - **Tokens140**: Complex query with `CTE`s and aggregations (~140 tokens)
223
- - **Tokens230**: Highly complex query with multiple `CTE`s, subqueries, and window functions (~230 tokens)
254
+ - **Tokens20**: Simple SELECT with a basic WHERE clause (~20 tokens)
255
+ - **Tokens70**: Medium complexity query with JOINs and multiple conditions (~70 tokens)
256
+ - **Tokens140**: Complex query with CTEs and aggregations (~140 tokens)
257
+ - **Tokens230**: Highly complex query with multiple CTEs, subqueries, and window functions (~230 tokens)
224
258
 
225
- ## Benchmark Environment
259
+ ### Benchmark Environment
226
260
 
227
261
  ```
228
262
  benchmark.js v2.1.4
@@ -231,42 +265,46 @@ AMD Ryzen 7 7800X3D (8C/16T)
231
265
  Node.js v22.14.0
232
266
  ```
233
267
 
234
- ## Results
235
-
236
- ### Tokens20
237
- | Method | Mean | Error | StdDev |
238
- |---------------------------------- |-----------:|----------:|----------:|
239
- | rawsql-ts | 0.021 ms | 0.0044 ms | 0.0023 ms |
240
- | node-sql-parser | 0.169 ms | 0.0695 ms | 0.0355 ms |
241
- | sql-formatter | 0.208 ms | 0.0556 ms | 0.0284 ms |
242
-
243
- ### Tokens70
244
- | Method | Mean | Error | StdDev |
245
- |---------------------------------- |-----------:|----------:|----------:|
246
- | rawsql-ts | 0.057 ms | 0.0143 ms | 0.0073 ms |
247
- | node-sql-parser | 0.216 ms | 0.0780 ms | 0.0398 ms |
248
- | sql-formatter | 0.512 ms | 0.1251 ms | 0.0638 ms |
249
-
250
- ### Tokens140
251
- | Method | Mean | Error | StdDev |
252
- |---------------------------------- |-----------:|----------:|----------:|
253
- | rawsql-ts | 0.112 ms | 0.0236 ms | 0.0120 ms |
254
- | node-sql-parser | 0.404 ms | 0.0926 ms | 0.0472 ms |
255
- | sql-formatter | 1.004 ms | 0.3027 ms | 0.1545 ms |
256
-
257
- ### Tokens230
258
- | Method | Mean | Error | StdDev |
259
- |---------------------------------- |-----------:|----------:|----------:|
260
- | rawsql-ts | 0.182 ms | 0.0371 ms | 0.0189 ms |
261
- | node-sql-parser | 0.865 ms | 0.3325 ms | 0.1696 ms |
262
- | sql-formatter | 1.696 ms | 0.2754 ms | 0.1405 ms |
263
-
264
- ## Performance Summary
268
+ ### Results
269
+
270
+ #### Tokens20
271
+ | Method | Mean | Error | StdDev |
272
+ |------------------|----------:|----------:|----------:|
273
+ | rawsql-ts | 0.021 ms | 0.0044 ms | 0.0023 ms |
274
+ | node-sql-parser | 0.169 ms | 0.0695 ms | 0.0355 ms |
275
+ | sql-formatter | 0.208 ms | 0.0556 ms | 0.0284 ms |
276
+
277
+ #### Tokens70
278
+ | Method | Mean | Error | StdDev |
279
+ |------------------|----------:|----------:|----------:|
280
+ | rawsql-ts | 0.057 ms | 0.0143 ms | 0.0073 ms |
281
+ | node-sql-parser | 0.216 ms | 0.0780 ms | 0.0398 ms |
282
+ | sql-formatter | 0.512 ms | 0.1251 ms | 0.0638 ms |
283
+
284
+ #### Tokens140
285
+ | Method | Mean | Error | StdDev |
286
+ |------------------|----------:|----------:|----------:|
287
+ | rawsql-ts | 0.112 ms | 0.0236 ms | 0.0120 ms |
288
+ | node-sql-parser | 0.404 ms | 0.0926 ms | 0.0472 ms |
289
+ | sql-formatter | 1.004 ms | 0.3027 ms | 0.1545 ms |
290
+
291
+ #### Tokens230
292
+ | Method | Mean | Error | StdDev |
293
+ |------------------|----------:|----------:|----------:|
294
+ | rawsql-ts | 0.182 ms | 0.0371 ms | 0.0189 ms |
295
+ | node-sql-parser | 0.865 ms | 0.3325 ms | 0.1696 ms |
296
+ | sql-formatter | 1.696 ms | 0.2754 ms | 0.1405 ms |
297
+
298
+ ### Performance Summary
265
299
 
266
300
  - `rawsql-ts` consistently outperforms both `node-sql-parser` and `sql-formatter` in all tested scenarios.
267
301
  - Approximately 4x faster than `node-sql-parser`.
268
- - Approximately 9-10x faster than `sql-formatter`.
269
- - Maintains comprehensive SQL parsing capabilities while delivering significant performance improvements.
302
+ - Approximately 910x faster than `sql-formatter`.
303
+ - Maintains high performance even with complex SQL while providing comprehensive features.
304
+
305
+ > **Note:** These benchmarks are based on a specific hardware and software environment. Actual performance may vary depending on system configuration and query complexity.
306
+
307
+ ---
270
308
 
271
- > **Note:** These benchmarks are based on a specific hardware and software environment. Actual performance may vary depending on system configuration and workload.
309
+ Feel free to try rawsql-ts! Questions, requests, and bug reports are always welcome.
272
310
 
@@ -0,0 +1,17 @@
1
+ // Entry point for rawsql-ts package
2
+ export * from './parsers/SelectQueryParser';
3
+ export * from './models/BinarySelectQuery';
4
+ export * from './models/SelectQuery';
5
+ export * from './models/ValueComponent';
6
+ export * from './models/ValuesQuery';
7
+ export * from './transformers/CTECollector';
8
+ export * from './transformers/CTENormalizer';
9
+ export * from './transformers/Formatter';
10
+ export * from './transformers/QueryConverter';
11
+ export * from './transformers/SelectValueCollector';
12
+ export * from './transformers/SelectableColumnCollector';
13
+ export * from './transformers/TableColumnResolver';
14
+ export * from './transformers/TableSourceCollector';
15
+ export * from './transformers/UpstreamSelectQueryFinder';
16
+ // Add more exports here if you want to expose additional public API
17
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,oCAAoC;AACpC,cAAc,6BAA6B,CAAC;AAE5C,cAAc,4BAA4B,CAAC;AAC3C,cAAc,sBAAsB,CAAC;AACrC,cAAc,yBAAyB,CAAC;AACxC,cAAc,sBAAsB,CAAC;AAErC,cAAc,6BAA6B,CAAC;AAC5C,cAAc,8BAA8B,CAAC;AAC7C,cAAc,0BAA0B,CAAC;AACzC,cAAc,+BAA+B,CAAC;AAC9C,cAAc,qCAAqC,CAAC;AACpD,cAAc,0CAA0C,CAAC;AACzD,cAAc,oCAAoC,CAAC;AACnD,cAAc,qCAAqC,CAAC;AACpD,cAAc,0CAA0C,CAAC;AACzD,oEAAoE"}
@@ -0,0 +1,137 @@
1
+ import { SourceExpression, SubQuerySource, SourceAliasExpression } from "./Clause";
2
+ import { SqlComponent } from "./SqlComponent";
3
+ import { RawString } from "./ValueComponent";
4
+ import { CTENormalizer } from "../transformers/CTENormalizer";
5
+ import { SelectQueryParser } from "../parsers/SelectQueryParser";
6
+ /**
7
+ * Represents a binary SELECT query (e.g., UNION, INTERSECT, EXCEPT).
8
+ */
9
+ export class BinarySelectQuery extends SqlComponent {
10
+ constructor(left, operator, right) {
11
+ super();
12
+ this.left = left;
13
+ this.operator = new RawString(operator);
14
+ this.right = right;
15
+ }
16
+ /**
17
+ * Appends another query to this binary query using UNION as the operator.
18
+ * This creates a new BinarySelectQuery where the left side is this binary query
19
+ * and the right side is the provided query.
20
+ *
21
+ * @param query The query to append with UNION
22
+ * @returns A new BinarySelectQuery representing "(this) UNION query"
23
+ */
24
+ union(query) {
25
+ return this.appendSelectQuery('union', query);
26
+ }
27
+ /**
28
+ * Appends another query to this binary query using UNION ALL as the operator.
29
+ * This creates a new BinarySelectQuery where the left side is this binary query
30
+ * and the right side is the provided query.
31
+ *
32
+ * @param query The query to append with UNION ALL
33
+ * @returns A new BinarySelectQuery representing "(this) UNION ALL query"
34
+ */
35
+ unionAll(query) {
36
+ return this.appendSelectQuery('union all', query);
37
+ }
38
+ /**
39
+ * Appends another query to this binary query using INTERSECT as the operator.
40
+ * This creates a new BinarySelectQuery where the left side is this binary query
41
+ * and the right side is the provided query.
42
+ *
43
+ * @param query The query to append with INTERSECT
44
+ * @returns A new BinarySelectQuery representing "(this) INTERSECT query"
45
+ */
46
+ intersect(query) {
47
+ return this.appendSelectQuery('intersect', query);
48
+ }
49
+ /**
50
+ * Appends another query to this binary query using INTERSECT ALL as the operator.
51
+ * This creates a new BinarySelectQuery where the left side is this binary query
52
+ * and the right side is the provided query.
53
+ *
54
+ * @param query The query to append with INTERSECT ALL
55
+ * @returns A new BinarySelectQuery representing "(this) INTERSECT ALL query"
56
+ */
57
+ intersectAll(query) {
58
+ return this.appendSelectQuery('intersect all', query);
59
+ }
60
+ /**
61
+ * Appends another query to this binary query using EXCEPT as the operator.
62
+ * This creates a new BinarySelectQuery where the left side is this binary query
63
+ * and the right side is the provided query.
64
+ *
65
+ * @param query The query to append with EXCEPT
66
+ * @returns A new BinarySelectQuery representing "(this) EXCEPT query"
67
+ */
68
+ except(query) {
69
+ return this.appendSelectQuery('except', query);
70
+ }
71
+ /**
72
+ * Appends another query to this binary query using EXCEPT ALL as the operator.
73
+ * This creates a new BinarySelectQuery where the left side is this binary query
74
+ * and the right side is the provided query.
75
+ *
76
+ * @param query The query to append with EXCEPT ALL
77
+ * @returns A new BinarySelectQuery representing "(this) EXCEPT ALL query"
78
+ */
79
+ exceptAll(query) {
80
+ return this.appendSelectQuery('except all', query);
81
+ }
82
+ /**
83
+ * Appends another query to this binary query using the specified operator.
84
+ * This creates a new BinarySelectQuery where the left side is this binary query
85
+ * and the right side is the provided query.
86
+ *
87
+ * @param operator SQL operator to use (e.g. 'union', 'union all', 'intersect', 'except')
88
+ * @param query The query to append with the specified operator
89
+ * @returns A new BinarySelectQuery representing "(this) [operator] query"
90
+ */
91
+ appendSelectQuery(operator, query) {
92
+ this.left = new BinarySelectQuery(this.left, this.operator.value, this.right);
93
+ this.operator = new RawString(operator);
94
+ this.right = query;
95
+ // const normalizer = new CTENormalizer();
96
+ // normalizer.normalize(this);
97
+ CTENormalizer.normalize(this);
98
+ return this;
99
+ }
100
+ /**
101
+ * Appends another query to this binary query using UNION as the operator, accepting a raw SQL string.
102
+ * This method parses the SQL string and appends the resulting query using UNION.
103
+ * @param sql The SQL string to parse and union
104
+ * @returns A new BinarySelectQuery representing "(this) UNION (parsed query)"
105
+ */
106
+ unionRaw(sql) {
107
+ const parsedQuery = SelectQueryParser.parse(sql);
108
+ return this.union(parsedQuery);
109
+ }
110
+ unionAllRaw(sql) {
111
+ const parsedQuery = SelectQueryParser.parse(sql);
112
+ return this.unionAll(parsedQuery);
113
+ }
114
+ intersectRaw(sql) {
115
+ const parsedQuery = SelectQueryParser.parse(sql);
116
+ return this.intersect(parsedQuery);
117
+ }
118
+ intersectAllRaw(sql) {
119
+ const parsedQuery = SelectQueryParser.parse(sql);
120
+ return this.intersectAll(parsedQuery);
121
+ }
122
+ exceptRaw(sql) {
123
+ const parsedQuery = SelectQueryParser.parse(sql);
124
+ return this.except(parsedQuery);
125
+ }
126
+ exceptAllRaw(sql) {
127
+ const parsedQuery = SelectQueryParser.parse(sql);
128
+ return this.exceptAll(parsedQuery);
129
+ }
130
+ // Returns a SourceExpression wrapping this query as a subquery source.
131
+ // Optionally takes an alias name (default: "subq")
132
+ toSource(alias = "subq") {
133
+ return new SourceExpression(new SubQuerySource(this), new SourceAliasExpression(alias, null));
134
+ }
135
+ }
136
+ BinarySelectQuery.kind = Symbol("BinarySelectQuery");
137
+ //# sourceMappingURL=BinarySelectQuery.js.map