rawsql-ts 0.1.0-beta.12 → 0.1.0-beta.14
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.
- package/README.md +56 -18
- package/package.json +1 -9
package/README.md
CHANGED
@@ -1,6 +1,10 @@
|
|
1
1
|
# rawsql-ts
|
2
2
|
|
3
3
|

|
4
|
+

|
5
|
+

|
6
|
+

|
7
|
+

|
4
8
|
|
5
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
|
|
@@ -134,36 +138,70 @@ A suite of utilities for transforming and analyzing SQL ASTs.
|
|
134
138
|
## Usage Example
|
135
139
|
|
136
140
|
```typescript
|
137
|
-
import { TableColumnResolver } from
|
138
|
-
import { SelectQueryParser } from 'rawsql-ts';
|
139
|
-
import { SelectableColumnCollector } from 'rawsql-ts/transformers/SelectableColumnCollector';
|
140
|
-
import { SelectValueCollector } from 'rawsql-ts/transformers/SelectValueCollector';
|
141
|
-
import { TableSourceCollector } from 'rawsql-ts/transformers/TableSourceCollector';
|
141
|
+
import { TableColumnResolver, SelectQueryParser, SelectableColumnCollector, SelectValueCollector, TableSourceCollector, Formatter } from 'rawsql-ts';
|
142
142
|
|
143
143
|
// TableColumnResolver example
|
144
144
|
const resolver: TableColumnResolver = (tableName) => {
|
145
|
-
|
146
|
-
|
147
|
-
|
145
|
+
if (tableName === 'users') return ['user_id', 'user_name', 'email'];
|
146
|
+
if (tableName === 'posts') return ['post_id', 'user_id', 'title', 'content'];
|
147
|
+
return [];
|
148
148
|
};
|
149
149
|
|
150
|
-
const sql = `SELECT u
|
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
151
|
const query = SelectQueryParser.parse(sql);
|
152
|
+
const formatter = new Formatter();
|
152
153
|
|
153
|
-
//
|
154
|
+
// Collects information from the SELECT clause.
|
155
|
+
// To expand wildcards, you must specify a TableColumnResolver.
|
154
156
|
const selectValueCollector = new SelectValueCollector(resolver);
|
155
157
|
const selectValues = selectValueCollector.collect(query);
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
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);
|
160
175
|
const selectableColumns = selectableColumnCollector.collect(query);
|
161
|
-
|
162
|
-
|
163
|
-
|
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.
|
164
193
|
const tableSourceCollector = new TableSourceCollector();
|
165
194
|
const sources = tableSourceCollector.collect(query);
|
166
|
-
|
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
|
+
*/
|
167
205
|
```
|
168
206
|
|
169
207
|
---
|
package/package.json
CHANGED
@@ -1,18 +1,10 @@
|
|
1
1
|
{
|
2
2
|
"name": "rawsql-ts",
|
3
|
-
"version": "0.1.0-beta.
|
3
|
+
"version": "0.1.0-beta.14",
|
4
4
|
"description": "[beta]High-performance SQL parser and AST analyzer written in TypeScript. Provides fast parsing and advanced transformation capabilities.",
|
5
5
|
"main": "dist/index.js",
|
6
6
|
"module": "dist/esm/index.js",
|
7
7
|
"types": "dist/index.d.ts",
|
8
|
-
"exports": {
|
9
|
-
".": {
|
10
|
-
"require": "./dist/index.js",
|
11
|
-
"import": "./dist/esm/index.js",
|
12
|
-
"types": "./dist/index.d.ts",
|
13
|
-
"browser": "./dist/esm/index.js"
|
14
|
-
}
|
15
|
-
},
|
16
8
|
"browser": "dist/esm/index.js",
|
17
9
|
"scripts": {
|
18
10
|
"test": "vitest run",
|