rawsql-ts 0.1.0-beta.3 → 0.1.0-beta.5

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 (135) hide show
  1. package/README.md +143 -199
  2. package/dist/index.js +32 -0
  3. package/dist/index.js.map +1 -0
  4. package/dist/models/BinarySelectQuery.js +140 -0
  5. package/dist/models/BinarySelectQuery.js.map +1 -0
  6. package/dist/models/Clause.js +318 -0
  7. package/dist/models/Clause.js.map +1 -0
  8. package/dist/models/KeywordTrie.js +52 -0
  9. package/dist/models/KeywordTrie.js.map +1 -0
  10. package/dist/models/Lexeme.js +21 -0
  11. package/dist/models/Lexeme.js.map +1 -0
  12. package/dist/models/SelectQuery.js +10 -0
  13. package/dist/models/SelectQuery.js.map +1 -0
  14. package/dist/models/SimpleSelectQuery.js +290 -0
  15. package/dist/models/SimpleSelectQuery.js.map +1 -0
  16. package/dist/models/SqlComponent.js +27 -0
  17. package/dist/models/SqlComponent.js.map +1 -0
  18. package/dist/models/ValueComponent.js +250 -0
  19. package/dist/models/ValueComponent.js.map +1 -0
  20. package/dist/models/ValuesQuery.js +16 -0
  21. package/dist/models/ValuesQuery.js.map +1 -0
  22. package/dist/parsers/CommandExpressionParser.js +124 -0
  23. package/dist/parsers/CommandExpressionParser.js.map +1 -0
  24. package/dist/parsers/CommonTableParser.js +60 -0
  25. package/dist/parsers/CommonTableParser.js.map +1 -0
  26. package/dist/parsers/ForClauseParser.js +56 -0
  27. package/dist/parsers/ForClauseParser.js.map +1 -0
  28. package/dist/parsers/FromClauseParser.js +45 -0
  29. package/dist/parsers/FromClauseParser.js.map +1 -0
  30. package/dist/parsers/FunctionExpressionParser.js +178 -0
  31. package/dist/parsers/FunctionExpressionParser.js.map +1 -0
  32. package/dist/parsers/GroupByParser.js +56 -0
  33. package/dist/parsers/GroupByParser.js.map +1 -0
  34. package/dist/parsers/HavingParser.js +34 -0
  35. package/dist/parsers/HavingParser.js.map +1 -0
  36. package/dist/parsers/IdentifierParser.js +39 -0
  37. package/dist/parsers/IdentifierParser.js.map +1 -0
  38. package/dist/parsers/JoinClauseParser.js +105 -0
  39. package/dist/parsers/JoinClauseParser.js.map +1 -0
  40. package/dist/parsers/KeywordParser.js +91 -0
  41. package/dist/parsers/KeywordParser.js.map +1 -0
  42. package/dist/parsers/LimitClauseParser.js +48 -0
  43. package/dist/parsers/LimitClauseParser.js.map +1 -0
  44. package/dist/parsers/LiteralParser.js +38 -0
  45. package/dist/parsers/LiteralParser.js.map +1 -0
  46. package/dist/parsers/OrderByClauseParser.js +75 -0
  47. package/dist/parsers/OrderByClauseParser.js.map +1 -0
  48. package/dist/parsers/OverExpressionParser.js +44 -0
  49. package/dist/parsers/OverExpressionParser.js.map +1 -0
  50. package/dist/parsers/ParameterExpressionParser.js +15 -0
  51. package/dist/parsers/ParameterExpressionParser.js.map +1 -0
  52. package/dist/parsers/ParenExpressionParser.js +33 -0
  53. package/dist/parsers/ParenExpressionParser.js.map +1 -0
  54. package/dist/parsers/PartitionByParser.js +51 -0
  55. package/dist/parsers/PartitionByParser.js.map +1 -0
  56. package/dist/parsers/SelectClauseParser.js +82 -0
  57. package/dist/parsers/SelectClauseParser.js.map +1 -0
  58. package/dist/parsers/SelectQueryParser.js +151 -0
  59. package/dist/parsers/SelectQueryParser.js.map +1 -0
  60. package/dist/parsers/SourceAliasExpressionParser.js +48 -0
  61. package/dist/parsers/SourceAliasExpressionParser.js.map +1 -0
  62. package/dist/parsers/SourceExpressionParser.js +34 -0
  63. package/dist/parsers/SourceExpressionParser.js.map +1 -0
  64. package/dist/parsers/SourceParser.js +116 -0
  65. package/dist/parsers/SourceParser.js.map +1 -0
  66. package/dist/parsers/SqlTokenizer.js +174 -0
  67. package/dist/parsers/SqlTokenizer.js.map +1 -0
  68. package/dist/parsers/StringSpecifierExpressionParser.js +22 -0
  69. package/dist/parsers/StringSpecifierExpressionParser.js.map +1 -0
  70. package/dist/parsers/UnaryExpressionParser.js +30 -0
  71. package/dist/parsers/UnaryExpressionParser.js.map +1 -0
  72. package/dist/parsers/ValueParser.js +134 -0
  73. package/dist/parsers/ValueParser.js.map +1 -0
  74. package/dist/parsers/ValuesQueryParser.js +86 -0
  75. package/dist/parsers/ValuesQueryParser.js.map +1 -0
  76. package/dist/parsers/WhereClauseParser.js +34 -0
  77. package/dist/parsers/WhereClauseParser.js.map +1 -0
  78. package/dist/parsers/WindowClauseParser.js +43 -0
  79. package/dist/parsers/WindowClauseParser.js.map +1 -0
  80. package/dist/parsers/WindowExpressionParser.js +151 -0
  81. package/dist/parsers/WindowExpressionParser.js.map +1 -0
  82. package/dist/parsers/WithClauseParser.js +55 -0
  83. package/dist/parsers/WithClauseParser.js.map +1 -0
  84. package/dist/tokenReaders/BaseTokenReader.js +82 -0
  85. package/dist/tokenReaders/BaseTokenReader.js.map +1 -0
  86. package/dist/tokenReaders/CommandTokenReader.js +145 -0
  87. package/dist/tokenReaders/CommandTokenReader.js.map +1 -0
  88. package/dist/tokenReaders/FunctionTokenReader.js +45 -0
  89. package/dist/tokenReaders/FunctionTokenReader.js.map +1 -0
  90. package/dist/tokenReaders/IdentifierTokenReader.js +70 -0
  91. package/dist/tokenReaders/IdentifierTokenReader.js.map +1 -0
  92. package/dist/tokenReaders/LiteralTokenReader.js +189 -0
  93. package/dist/tokenReaders/LiteralTokenReader.js.map +1 -0
  94. package/dist/tokenReaders/OperatorTokenReader.js +98 -0
  95. package/dist/tokenReaders/OperatorTokenReader.js.map +1 -0
  96. package/dist/tokenReaders/ParameterTokenReader.js +44 -0
  97. package/dist/tokenReaders/ParameterTokenReader.js.map +1 -0
  98. package/dist/tokenReaders/StringSpecifierTokenReader.js +31 -0
  99. package/dist/tokenReaders/StringSpecifierTokenReader.js.map +1 -0
  100. package/dist/tokenReaders/SymbolTokenReader.js +35 -0
  101. package/dist/tokenReaders/SymbolTokenReader.js.map +1 -0
  102. package/dist/tokenReaders/TokenReaderManager.js +110 -0
  103. package/dist/tokenReaders/TokenReaderManager.js.map +1 -0
  104. package/dist/tokenReaders/TypeTokenReader.js +59 -0
  105. package/dist/tokenReaders/TypeTokenReader.js.map +1 -0
  106. package/dist/transformers/CTEBuilder.js +188 -0
  107. package/dist/transformers/CTEBuilder.js.map +1 -0
  108. package/dist/transformers/CTECollector.js +384 -0
  109. package/dist/transformers/CTECollector.js.map +1 -0
  110. package/dist/transformers/CTEDisabler.js +325 -0
  111. package/dist/transformers/CTEDisabler.js.map +1 -0
  112. package/dist/transformers/CTEInjector.js +83 -0
  113. package/dist/transformers/CTEInjector.js.map +1 -0
  114. package/dist/transformers/CTENormalizer.js +42 -0
  115. package/dist/transformers/CTENormalizer.js.map +1 -0
  116. package/dist/transformers/Formatter.js +452 -0
  117. package/dist/transformers/Formatter.js.map +1 -0
  118. package/dist/transformers/QueryNormalizer.js +114 -0
  119. package/dist/transformers/QueryNormalizer.js.map +1 -0
  120. package/dist/transformers/SelectValueCollector.js +249 -0
  121. package/dist/transformers/SelectValueCollector.js.map +1 -0
  122. package/dist/transformers/SelectableColumnCollector.js +308 -0
  123. package/dist/transformers/SelectableColumnCollector.js.map +1 -0
  124. package/dist/transformers/TableSourceCollector.js +384 -0
  125. package/dist/transformers/TableSourceCollector.js.map +1 -0
  126. package/dist/transformers/UpstreamSelectQueryFinder.js +129 -0
  127. package/dist/transformers/UpstreamSelectQueryFinder.js.map +1 -0
  128. package/dist/utils/charLookupTable.js +73 -0
  129. package/dist/utils/charLookupTable.js.map +1 -0
  130. package/dist/utils/stringUtils.js +168 -0
  131. package/dist/utils/stringUtils.js.map +1 -0
  132. package/package.json +2 -2
  133. package/dist/tsconfig.tsbuildinfo +0 -1
  134. package/dist/vitest.config.js +0 -15
  135. package/dist/vitest.config.js.map +0 -1
package/README.md CHANGED
@@ -1,250 +1,164 @@
1
1
  # rawsql-ts
2
2
 
3
- A TypeScript SQL parser project that performs AST (Abstract Syntax Tree) analysis.
3
+ rawsql-ts is a TypeScript SQL parser that performs Abstract Syntax Tree (AST) analysis for advanced SQL processing and transformation.
4
+
5
+ > **Note:** This library is currently in beta. The API may change without notice until the v1.0 release.
4
6
 
5
7
  ## Installation
6
8
 
7
- Install the main project:
9
+ Install the package from npm as follows:
8
10
 
9
11
  ```bash
10
- npm install
12
+ npm install rawsql-ts
11
13
  ```
12
14
 
13
15
  ## Usage
14
16
 
15
- Build the project:
17
+ Basic usage example:
16
18
 
17
- ```bash
18
- npm run build
19
+ ```typescript
20
+ import { SelectQueryParser } from 'rawsql-ts';
21
+ import { Formatter } from 'rawsql-ts';
22
+
23
+ const sql = `SELECT id, name FROM users WHERE active = TRUE`;
24
+ const query = SelectQueryParser.parseFromText(sql);
25
+ const formatter = new Formatter();
26
+ const formattedSql = formatter.visit(query);
27
+ console.log(formattedSql);
28
+ // => select "id", "name" from "users" where "active" = true
19
29
  ```
20
30
 
21
- Run tests:
31
+ ---
22
32
 
23
- ```bash
24
- npm test
25
- ```
33
+ ## 🧩 Parsing Features
26
34
 
27
- ## Supported Features
35
+ rawsql-ts provides the following main parser class for converting SQL text into an Abstract Syntax Tree (AST):
28
36
 
29
- **Main features included in this parser:**
37
+ - **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.
30
39
 
31
- - **CTE Support**: Full Common Table Expression parsing
32
- - PostgreSQL `MATERIALIZED`/`NOT MATERIALIZED` options
33
- - Nested and recursive CTEs
34
- - **UNION Queries**: Handles UNION, UNION ALL, INTERSECT and EXCEPT
35
- - **Complex Subqueries**: Supports subqueries and inline queries
36
- - **Window Functions**: Complete WINDOW clause and function support
37
- - **PostgreSQL Optimized**: Deep support for PostgreSQL syntax
38
- - `DISTINCT ON (columns)` expressions
39
- - Array and range operators
40
+ **Key methods:**
41
+ - `parseFromText(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
+ - `parse(lexemes: Lexeme[], index: number): { value: SelectQuery; newIndex: number }`
44
+ Parses a tokenized SQL query from the given index and returns the AST node and the new index. Used internally for advanced parsing scenarios.
40
45
 
41
- ## ⚠️ Important Notes
46
+ **Notes:**
47
+ - Only PostgreSQL syntax is supported at this time.
48
+ - Only SELECT and VALUES queries are supported (INSERT/UPDATE/DELETE are not yet implemented).
49
+ - All SQL comments are removed during parsing.
42
50
 
43
- **Under development with the following limitations:**
51
+ This class is designed to handle all practical SQL parsing needs for SELECT/VALUES queries in PostgreSQL, including:
52
+ - CTEs (WITH), including recursive and materialized options
53
+ - UNION, INTERSECT, EXCEPT, and subqueries
54
+ - Window functions and analytic clauses
55
+ - Complex expressions, functions, and operators
56
+ - Robust error handling with detailed messages
57
+ - Accurate tokenization, including comments and special literals
44
58
 
45
- - **PostgreSQL Only**: Only PostgreSQL syntax is currently supported
46
- - **Comments Stripped**: SQL comments are removed during parsing
47
- - **SELECT Queries Only**: Currently only handles SELECT queries (no INSERT/UPDATE/DELETE)
48
- - **One-line Formatting**: Currently only supports single-line (compact) output formatting
49
- - **Beta Status**: API may change without notice until v1.0 release
59
+ ---
50
60
 
51
- ## Transformer Utilities
61
+ ## Core SQL Query Classes
52
62
 
53
- rawsql-ts includes powerful transformer utilities to analyze and transform SQL ASTs. These utilities let you format, analyze, and extract information from SQL queries in a super flexible way!
63
+ The following classes are the primary building blocks for representing and manipulating SQL queries in rawsql-ts:
54
64
 
55
- ### Formatter
65
+ ### SimpleSelectQuery
56
66
 
57
- The Formatter transforms SQL ASTs into clean, standardized SQL text output. It handles all SQL components, ensuring proper escaping and consistent formatting regardless of the complexity of your queries.
67
+ 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.
58
68
 
59
- ```typescript
60
- import { SelectQueryParser } from './parsers/SelectQueryParser';
61
- import { Formatter } from './transformers/Formatter';
62
-
63
- // Example complex query with subquery and functions
64
- const sql = `
65
- SELECT
66
- p.product_id
67
- , p.name
68
- , SUM(o.quantity) AS total_ordered
69
- , CASE
70
- WHEN SUM(o.quantity) > 1000 THEN 'High Demand'
71
- WHEN SUM(o.quantity) > 500 THEN 'Medium Demand'
72
- ELSE 'Low Demand'
73
- END AS demand_category
74
- FROM
75
- products AS p
76
- JOIN order_items AS o ON p.product_id = o.product_id
77
- WHERE
78
- p.category IN (
79
- SELECT
80
- category
81
- FROM
82
- featured_categories
83
- WHERE
84
- active = TRUE
85
- )
86
- GROUP BY
87
- p.product_id
88
- , p.name
89
- HAVING
90
- SUM(o.quantity) > 100
91
- ORDER BY
92
- total_ordered DESC`;
93
-
94
- // Parse the query into an AST
95
- const query = SelectQueryParser.parseFromText(sql);
69
+ **Key methods:**
70
+ - `toUnion`, `toUnionAll`, `toIntersect`, `toExcept`, etc.
71
+ - Combine this query with another using UNION, INTERSECT, EXCEPT, etc., returning a BinarySelectQuery.
72
+ - `appendWhere`, `appendWhereRaw`
73
+ - Add a new condition to the WHERE clause (as AST or raw SQL string).
74
+ - `appendHaving`, `appendHavingRaw`
75
+ - Add a new condition to the HAVING clause.
76
+ - `innerJoin`, `leftJoin`, `rightJoin`, `innerJoinRaw`, etc.
77
+ - Add JOIN clauses to the query, either as AST or from raw SQL.
78
+ - `toSource`
79
+ - Wrap this query as a subquery (for use in FROM/JOIN, etc.).
80
+ - `appendWith`, `appendWithRaw`
81
+ - Add CTEs (WITH clause) to the query.
96
82
 
97
- // Format the AST back to SQL
98
- const formatter = new Formatter();
99
- const formattedSql = formatter.visit(query);
83
+ ### BinarySelectQuery
100
84
 
101
- console.log(formattedSql);
102
- // Outputs clean, consistently formatted SQL with proper identifiers
103
- ```
85
+ 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.
104
86
 
105
- ### SelectValueCollector
87
+ **Key methods:**
88
+ - `union`, `unionAll`, `intersect`, `intersectAll`, `except`, `exceptAll`
89
+ - Chain additional queries to the current binary query.
90
+ - `appendSelectQuery`
91
+ - Add a new query with a custom operator.
92
+ - `toSource`
93
+ - Wrap this binary query as a subquery (for use in FROM/JOIN, etc.).
94
+ - `unionRaw`, `intersectRaw`, etc.
95
+ - Add a new query by parsing a raw SQL string and combining it.
106
96
 
107
- The SelectValueCollector extracts all column items from a SELECT clause, including their aliases and expressions. It provides access to both column names and their corresponding value expressions, making it perfect for analyzing the output structure of SQL queries. For information on wildcard resolution (like `*` or `table.*`), see the Wildcard Resolution section below.
97
+ ### ValuesQuery
108
98
 
109
- ```typescript
110
- import { SelectQueryParser } from './parsers/SelectQueryParser';
111
- import { SelectValueCollector } from './transformers/SelectValueCollector';
112
- import { Formatter } from './transformers/Formatter';
113
-
114
- // Example query with column references and expressions
115
- const sql = `
116
- SELECT
117
- id
118
- , name
119
- , price * quantity AS total
120
- , (
121
- SELECT
122
- COUNT(*)
123
- FROM
124
- orders AS o
125
- WHERE
126
- o.customer_id = c.id
127
- ) AS order_count
128
- FROM
129
- customers AS c
130
- WHERE
131
- status = 'active'`;
99
+ Represents a SQL `VALUES` clause, such as `VALUES (1, 'a'), (2, 'b')`, which is used for inline data tables.
132
100
 
133
- const query = SelectQueryParser.parseFromText(sql);
101
+ **Key methods:**
102
+ - (Primarily the constructor and tuple access)
103
+ - This class can be used as a subquery source or wrapped with QueryNormalizer to convert it into a standard SELECT query.
134
104
 
135
- // Collect all select values
136
- const collector = new SelectValueCollector();
137
- const items = collector.collect(query);
105
+ ---
138
106
 
139
- // Format expressions for display
140
- const formatter = new Formatter();
107
+ 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.
141
108
 
142
- // Output column names
143
- console.log(items.map(item => item.name));
144
- // ["id", "name", "total", "order_count"]
109
+ ---
145
110
 
146
- // Output column expressions to show full value components
147
- console.log(items.map(item => formatter.visit(item.value)));
148
- // ["id", "name", "price * quantity", "(SELECT COUNT(*) FROM orders AS o WHERE o.customer_id = c.id)"]
149
- ```
111
+ ## 🛠️ Transformer Features (AST Transformers)
150
112
 
151
- ### SelectableColumnCollector
113
+ 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.
152
114
 
153
- The SelectableColumnCollector identifies all column references throughout a query that could potentially be included in a SELECT clause. It scans the entire query structure and extracts columns with their full context, making it ideal for query builders. For information on wildcard resolution (like `*` or `table.*`), see the Wildcard Resolution section below.
115
+ ### Main Transformers
154
116
 
155
- ```typescript
156
- import { SelectQueryParser } from './parsers/SelectQueryParser';
157
- import { SelectableColumnCollector } from './transformers/SelectableColumnCollector';
158
- import { Formatter } from './transformers/Formatter';
159
-
160
- // Example query
161
- const sql = `
162
- SELECT
163
- u.id
164
- , u.name
165
- FROM
166
- users AS u
167
- JOIN profiles AS p ON u.id = p.user_id
168
- WHERE
169
- u.active = TRUE
170
- AND p.verified = TRUE`;
117
+ - **Formatter**
118
+ Converts SQL ASTs into standardized SQL text, handling identifier escaping and formatting for all SQL components.
119
+ **Note:** Output formatting is currently limited to single-line (compact) style.
171
120
 
172
- const query = SelectQueryParser.parseFromText(sql);
173
- const collector = new SelectableColumnCollector();
121
+ - **SelectValueCollector**
122
+ Extracts all columns, including aliases and expressions, from SELECT clauses. Supports wildcard expansion (e.g., `*`, `table.*`) when table structure information is provided.
174
123
 
175
- // Collect all column references from data sources
176
- collector.visit(query);
177
- const columns = collector.collect(query);
124
+ - **SelectableColumnCollector**
125
+ Collects all column references in a query that can be included in a SELECT clause. Gathers all columns available from root FROM/JOIN sources.
178
126
 
179
- // Format column references for display
180
- const formatter = new Formatter();
181
- const columnNames = columns.map(item => item.name);
182
- console.log(columnNames);
183
- // ["id", "name", "active", "user_id", "verified"]
184
-
185
- // Get the original column expressions with their full context
186
- const expressions = columns.map(item => formatter.visit(item.value));
187
- console.log(expressions);
188
- // ["u.id", "u.name", "u.active", "p.user_id", "p.verified"]
189
- ```
127
+ - **CTECollector**
128
+ Collects all Common Table Expressions (CTEs) from WITH clauses, subqueries, and UNION queries. Supports both nested and recursive CTEs.
190
129
 
191
- ### Wildcard Resolution
130
+ - **UpstreamSelectQueryFinder**
131
+ Identifies upstream SELECT queries that provide specific columns by traversing CTEs, subqueries, and UNION branches.
192
132
 
193
- The wildcard resolution feature enhances both collectors by supporting the expansion of wildcard expressions (`*` and `table.*`). Since SQL AST analysis alone cannot determine actual column names from wildcards, this feature allows you to provide table structure information through a custom resolver.
133
+ - **CTENormalizer**
134
+ 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.
194
135
 
195
- To use this feature, simply provide a `TableColumnResolver` function when creating a collector. This resolver maps table names to their column definitions, allowing the collectors to fully expand wildcard expressions into individual columns with proper context.
136
+ - **QueryNormalizer**
137
+ 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.
196
138
 
197
- ```typescript
198
- import { SelectQueryParser } from './parsers/SelectQueryParser';
199
- import { SelectValueCollector } from './transformers/SelectValueCollector';
200
- import { Formatter } from './transformers/Formatter';
201
-
202
- // Define a function to resolve column names from table names
203
- const tableColumnResolver = (tableName: string): string[] => {
204
- // In real applications, this would fetch from database metadata or schema information
205
- const tableColumns: Record<string, string[]> = {
206
- 'users': ['id', 'name', 'email', 'created_at'],
207
- 'posts': ['id', 'title', 'content', 'user_id', 'created_at'],
208
- 'comments': ['id', 'post_id', 'user_id', 'content', 'created_at']
209
- };
210
-
211
- return tableColumns[tableName] || [];
212
- };
213
-
214
- // Query containing wildcards
215
- const sql = `
216
- SELECT
217
- u.*
218
- , p.title
219
- , p.content
220
- FROM
221
- users AS u
222
- JOIN posts AS p ON u.id = p.user_id
223
- WHERE
224
- u.created_at > '2023-01-01'`;
139
+ ---
225
140
 
226
- const query = SelectQueryParser.parseFromText(sql);
141
+ ### Example Usage
227
142
 
228
- // Pass the TableColumnResolver to resolve wildcards
229
- const collector = new SelectValueCollector(tableColumnResolver);
230
- const items = collector.collect(query);
143
+ ```typescript
144
+ import { SelectQueryParser } from 'rawsql-ts';
145
+ import { SelectableColumnCollector } from 'rawsql-ts/transformers/SelectableColumnCollector';
231
146
 
232
- // Display results
233
- const formatter = new Formatter();
234
- console.log(items.map(item => item.name));
235
- // ["id", "name", "email", "created_at", "title", "content"]
147
+ const sql = `SELECT u.id, u.name FROM users u JOIN posts p ON u.id = p.user_id`;
148
+ const query = SelectQueryParser.parseFromText(sql);
236
149
 
237
- // Show full reference expressions for each column
238
- console.log(items.map(item => formatter.visit(item.value)));
239
- // ["u.id", "u.name", "u.email", "u.created_at", "p.title", "p.content"]
150
+ const collector = new SelectableColumnCollector();
151
+ const columns = collector.collect(query);
152
+ console.log(columns.map(col => col.name)); // ["id", "name", "user_id", ...]
240
153
  ```
241
154
 
242
- This capability allows you to parse queries containing wildcards and understand exactly which columns are being referenced. It also supports expansion of wildcards from multiple tables and subqueries.
155
+ ---
156
+
157
+ By utilizing these transformer utilities, you can perform advanced SQL analysis and manipulation with reliability and consistency.
243
158
 
244
159
  ## Benchmarks
245
160
 
246
- This project includes benchmarking functionality.
247
- To run benchmarks:
161
+ This project includes benchmarking functionality. To run benchmarks, execute:
248
162
 
249
163
  ```bash
250
164
  npm run benchmark
@@ -252,12 +166,12 @@ npm run benchmark
252
166
 
253
167
  ## Benchmark Details
254
168
 
255
- This benchmark evaluates the SQL parsing and formatting performance of `rawsql-ts` against popular libraries: `sql-formatter` and `node-sql-parser`. We test queries of varying complexity:
169
+ 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:
256
170
 
257
171
  - **Tokens20**: Simple `SELECT` query with a basic `WHERE` condition (~20 tokens)
258
172
  - **Tokens70**: Medium complexity query with `JOIN`s and multiple conditions (~70 tokens)
259
173
  - **Tokens140**: Complex query with `CTE`s and aggregations (~140 tokens)
260
- - **Tokens230**: Very complex query with multiple `CTE`s, subqueries, and window functions (~230 tokens)
174
+ - **Tokens230**: Highly complex query with multiple `CTE`s, subqueries, and window functions (~230 tokens)
261
175
 
262
176
  ## Benchmark Environment
263
177
 
@@ -300,9 +214,39 @@ Node.js v22.14.0
300
214
 
301
215
  ## Performance Summary
302
216
 
303
- - `rawsql-ts` **consistently outperforms** both `node-sql-parser` and `sql-formatter` in all tested cases.
304
- - **4x faster** than `node-sql-parser`.
305
- - **9-10x faster** than `sql-formatter`.
306
- - Maintains **full SQL parsing capabilities** while significantly improving performance.
217
+ - `rawsql-ts` consistently outperforms both `node-sql-parser` and `sql-formatter` in all tested scenarios.
218
+ - Approximately 4x faster than `node-sql-parser`.
219
+ - Approximately 9-10x faster than `sql-formatter`.
220
+ - Maintains comprehensive SQL parsing capabilities while delivering significant performance improvements.
221
+
222
+ > **Note:** These benchmarks are based on a specific hardware and software environment. Actual performance may vary depending on system configuration and workload.
223
+
224
+ ## Practical Example: Table Join
225
+
226
+ Let's see how easy it is to join two tables using rawsql-ts! You don't need to know the internal structure of SelectQuery or worry about alias names—just specify the join key(s) and the library will generate the ON clause for you automatically.
227
+
228
+ ```typescript
229
+ import { SelectQueryParser } from 'rawsql-ts';
230
+ import { Formatter } from 'rawsql-ts';
231
+
232
+ // Parse two separate queries
233
+ const userQuery = SelectQueryParser.parseFromText('SELECT user_id, user_name FROM users');
234
+ const postQuery = SelectQueryParser.parseFromText('SELECT post_id, user_id, title FROM posts');
235
+
236
+ // Join the two queries using innerJoin
237
+ // Just pass the join key(s) as an array, and the ON clause will be generated automatically!
238
+ const joinedQuery = userQuery.innerJoin(postQuery, ['user_id']);
239
+
240
+ // Format the joined query back to SQL
241
+ const formatter = new Formatter();
242
+ const sql = formatter.visit(joinedQuery);
243
+ console.log(sql);
244
+ // Output:
245
+ // 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"
246
+ ```
307
247
 
308
- > ⚠️ **Note:** These benchmarks are based on a specific hardware and software environment. Actual performance may vary depending on system configuration and workload.
248
+ **Point:**
249
+ - You do not need to understand the internal code of SelectQuery to perform joins.
250
+ - You only need to specify the join key(s) (e.g., `['user_id']`). The ON clause is generated for you.
251
+ - Alias names and subquery details are handled automatically by the library, so you never have to worry about them!
252
+ - This makes it super easy to join queries, even if you don't know the full structure of the SQL or the internal AST.
package/dist/index.js ADDED
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ // Entry point for rawsql-ts package
18
+ __exportStar(require("./parsers/SelectQueryParser"), exports);
19
+ __exportStar(require("./models/BinarySelectQuery"), exports);
20
+ __exportStar(require("./models/SelectQuery"), exports);
21
+ __exportStar(require("./models/ValueComponent"), exports);
22
+ __exportStar(require("./models/ValuesQuery"), exports);
23
+ __exportStar(require("./transformers/CTECollector"), exports);
24
+ __exportStar(require("./transformers/CTENormalizer"), exports);
25
+ __exportStar(require("./transformers/Formatter"), exports);
26
+ __exportStar(require("./transformers/QueryNormalizer"), exports);
27
+ __exportStar(require("./transformers/SelectValueCollector"), exports);
28
+ __exportStar(require("./transformers/SelectableColumnCollector"), exports);
29
+ __exportStar(require("./transformers/TableSourceCollector"), exports);
30
+ __exportStar(require("./transformers/UpstreamSelectQueryFinder"), exports);
31
+ // Add more exports here if you want to expose additional public API
32
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,oCAAoC;AACpC,8DAA4C;AAE5C,6DAA2C;AAC3C,uDAAqC;AACrC,0DAAwC;AACxC,uDAAqC;AAErC,8DAA4C;AAC5C,+DAA6C;AAC7C,2DAAyC;AACzC,iEAA+C;AAC/C,sEAAoD;AACpD,2EAAyD;AACzD,sEAAoD;AACpD,2EAAyD;AACzD,oEAAoE"}
@@ -0,0 +1,140 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BinarySelectQuery = void 0;
4
+ const Clause_1 = require("./Clause");
5
+ const SqlComponent_1 = require("./SqlComponent");
6
+ const ValueComponent_1 = require("./ValueComponent");
7
+ const CTENormalizer_1 = require("../transformers/CTENormalizer");
8
+ const SelectQueryParser_1 = require("../parsers/SelectQueryParser");
9
+ /**
10
+ * Represents a binary SELECT query (e.g., UNION, INTERSECT, EXCEPT).
11
+ */
12
+ class BinarySelectQuery extends SqlComponent_1.SqlComponent {
13
+ constructor(left, operator, right) {
14
+ super();
15
+ this.left = left;
16
+ this.operator = new ValueComponent_1.RawString(operator);
17
+ this.right = right;
18
+ }
19
+ /**
20
+ * Appends another query to this binary query using UNION as the operator.
21
+ * This creates a new BinarySelectQuery where the left side is this binary query
22
+ * and the right side is the provided query.
23
+ *
24
+ * @param query The query to append with UNION
25
+ * @returns A new BinarySelectQuery representing "(this) UNION query"
26
+ */
27
+ union(query) {
28
+ return this.appendSelectQuery('union', query);
29
+ }
30
+ /**
31
+ * Appends another query to this binary query using UNION ALL as the operator.
32
+ * This creates a new BinarySelectQuery where the left side is this binary query
33
+ * and the right side is the provided query.
34
+ *
35
+ * @param query The query to append with UNION ALL
36
+ * @returns A new BinarySelectQuery representing "(this) UNION ALL query"
37
+ */
38
+ unionAll(query) {
39
+ return this.appendSelectQuery('union all', query);
40
+ }
41
+ /**
42
+ * Appends another query to this binary query using INTERSECT as the operator.
43
+ * This creates a new BinarySelectQuery where the left side is this binary query
44
+ * and the right side is the provided query.
45
+ *
46
+ * @param query The query to append with INTERSECT
47
+ * @returns A new BinarySelectQuery representing "(this) INTERSECT query"
48
+ */
49
+ intersect(query) {
50
+ return this.appendSelectQuery('intersect', query);
51
+ }
52
+ /**
53
+ * Appends another query to this binary query using INTERSECT ALL as the operator.
54
+ * This creates a new BinarySelectQuery where the left side is this binary query
55
+ * and the right side is the provided query.
56
+ *
57
+ * @param query The query to append with INTERSECT ALL
58
+ * @returns A new BinarySelectQuery representing "(this) INTERSECT ALL query"
59
+ */
60
+ intersectAll(query) {
61
+ return this.appendSelectQuery('intersect all', query);
62
+ }
63
+ /**
64
+ * Appends another query to this binary query using EXCEPT as the operator.
65
+ * This creates a new BinarySelectQuery where the left side is this binary query
66
+ * and the right side is the provided query.
67
+ *
68
+ * @param query The query to append with EXCEPT
69
+ * @returns A new BinarySelectQuery representing "(this) EXCEPT query"
70
+ */
71
+ except(query) {
72
+ return this.appendSelectQuery('except', query);
73
+ }
74
+ /**
75
+ * Appends another query to this binary query using EXCEPT ALL as the operator.
76
+ * This creates a new BinarySelectQuery where the left side is this binary query
77
+ * and the right side is the provided query.
78
+ *
79
+ * @param query The query to append with EXCEPT ALL
80
+ * @returns A new BinarySelectQuery representing "(this) EXCEPT ALL query"
81
+ */
82
+ exceptAll(query) {
83
+ return this.appendSelectQuery('except all', query);
84
+ }
85
+ /**
86
+ * Appends another query to this binary query using the specified operator.
87
+ * This creates a new BinarySelectQuery where the left side is this binary query
88
+ * and the right side is the provided query.
89
+ *
90
+ * @param operator SQL operator to use (e.g. 'union', 'union all', 'intersect', 'except')
91
+ * @param query The query to append with the specified operator
92
+ * @returns A new BinarySelectQuery representing "(this) [operator] query"
93
+ */
94
+ appendSelectQuery(operator, query) {
95
+ this.left = new BinarySelectQuery(this.left, this.operator.value, this.right);
96
+ this.operator = new ValueComponent_1.RawString(operator);
97
+ this.right = query;
98
+ const normalizer = new CTENormalizer_1.CTENormalizer();
99
+ normalizer.normalize(this);
100
+ return this;
101
+ }
102
+ /**
103
+ * Appends another query to this binary query using UNION as the operator, accepting a raw SQL string.
104
+ * This method parses the SQL string and appends the resulting query using UNION.
105
+ * @param sql The SQL string to parse and union
106
+ * @returns A new BinarySelectQuery representing "(this) UNION (parsed query)"
107
+ */
108
+ unionRaw(sql) {
109
+ const parsedQuery = SelectQueryParser_1.SelectQueryParser.parseFromText(sql);
110
+ return this.union(parsedQuery);
111
+ }
112
+ unionAllRaw(sql) {
113
+ const parsedQuery = SelectQueryParser_1.SelectQueryParser.parseFromText(sql);
114
+ return this.unionAll(parsedQuery);
115
+ }
116
+ intersectRaw(sql) {
117
+ const parsedQuery = SelectQueryParser_1.SelectQueryParser.parseFromText(sql);
118
+ return this.intersect(parsedQuery);
119
+ }
120
+ intersectAllRaw(sql) {
121
+ const parsedQuery = SelectQueryParser_1.SelectQueryParser.parseFromText(sql);
122
+ return this.intersectAll(parsedQuery);
123
+ }
124
+ exceptRaw(sql) {
125
+ const parsedQuery = SelectQueryParser_1.SelectQueryParser.parseFromText(sql);
126
+ return this.except(parsedQuery);
127
+ }
128
+ exceptAllRaw(sql) {
129
+ const parsedQuery = SelectQueryParser_1.SelectQueryParser.parseFromText(sql);
130
+ return this.exceptAll(parsedQuery);
131
+ }
132
+ // Returns a SourceExpression wrapping this query as a subquery source.
133
+ // Optionally takes an alias name (default: "subq")
134
+ toSource(alias = "subq") {
135
+ return new Clause_1.SourceExpression(new Clause_1.SubQuerySource(this), new Clause_1.SourceAliasExpression(alias, null));
136
+ }
137
+ }
138
+ exports.BinarySelectQuery = BinarySelectQuery;
139
+ BinarySelectQuery.kind = Symbol("BinarySelectQuery");
140
+ //# sourceMappingURL=BinarySelectQuery.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"BinarySelectQuery.js","sourceRoot":"","sources":["../../src/models/BinarySelectQuery.ts"],"names":[],"mappings":"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"}