sqlparser-rs 0.60.3-rc1 → 0.60.3-rc2
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 +65 -32
- package/dist/index.cjs +128 -43
- package/dist/index.d.cts +78 -15
- package/dist/index.d.mts +78 -15
- package/dist/index.mjs +124 -43
- package/package.json +2 -2
- package/wasm/README.md +65 -32
- package/wasm/package.json +1 -1
- package/wasm/sqlparser_rs_wasm_bg.wasm +0 -0
- package/wasm/sqlparser_rs_wasm_web_bg.wasm +0 -0
package/README.md
CHANGED
|
@@ -26,25 +26,22 @@ npm install sqlparser-rs
|
|
|
26
26
|
## Quick Start
|
|
27
27
|
|
|
28
28
|
```typescript
|
|
29
|
-
import {
|
|
29
|
+
import { parse, format, validate } from 'sqlparser-rs';
|
|
30
30
|
|
|
31
|
-
// Simple parsing
|
|
32
|
-
const statements =
|
|
31
|
+
// Simple parsing (uses generic dialect by default)
|
|
32
|
+
const statements = parse('SELECT * FROM users');
|
|
33
33
|
console.log(statements);
|
|
34
34
|
|
|
35
|
-
// With specific dialect
|
|
36
|
-
const pgStatements =
|
|
37
|
-
'SELECT * FROM users WHERE id = $1',
|
|
38
|
-
new PostgreSqlDialect()
|
|
39
|
-
);
|
|
35
|
+
// With specific dialect (string name)
|
|
36
|
+
const pgStatements = parse('SELECT * FROM users WHERE id = $1', 'postgresql');
|
|
40
37
|
|
|
41
38
|
// Format SQL
|
|
42
|
-
const formatted =
|
|
39
|
+
const formatted = format('select * from users');
|
|
43
40
|
console.log(formatted); // "SELECT * FROM users"
|
|
44
41
|
|
|
45
42
|
// Validate SQL
|
|
46
43
|
try {
|
|
47
|
-
|
|
44
|
+
validate('SELEC * FROM users');
|
|
48
45
|
} catch (error) {
|
|
49
46
|
console.log('Invalid SQL:', error.message);
|
|
50
47
|
}
|
|
@@ -52,46 +49,82 @@ try {
|
|
|
52
49
|
|
|
53
50
|
## API
|
|
54
51
|
|
|
55
|
-
###
|
|
52
|
+
### Top-Level Functions
|
|
56
53
|
|
|
57
|
-
The
|
|
58
|
-
|
|
59
|
-
#### Static Methods
|
|
54
|
+
The simplest way to use the parser:
|
|
60
55
|
|
|
61
56
|
```typescript
|
|
62
|
-
|
|
63
|
-
const statements = Parser.parse(sql: string, dialect: Dialect): Statement[];
|
|
64
|
-
|
|
65
|
-
// Parse and return JSON string
|
|
66
|
-
const json = Parser.parseToJson(sql: string, dialect: Dialect): string;
|
|
57
|
+
import { parse, format, validate } from 'sqlparser-rs';
|
|
67
58
|
|
|
68
|
-
// Parse
|
|
69
|
-
|
|
59
|
+
// Parse SQL - dialect defaults to 'generic'
|
|
60
|
+
parse('SELECT * FROM users');
|
|
61
|
+
parse('SELECT * FROM users', 'postgresql');
|
|
62
|
+
parse('SELECT * FROM users', 'mysql');
|
|
70
63
|
|
|
71
64
|
// Format SQL (round-trip through parser)
|
|
72
|
-
|
|
65
|
+
format('select * from users'); // "SELECT * FROM users"
|
|
66
|
+
|
|
67
|
+
// Validate SQL syntax (throws ParserError if invalid)
|
|
68
|
+
validate('SELECT * FROM users'); // true
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
### Dialect Options
|
|
73
72
|
|
|
74
|
-
|
|
75
|
-
const isValid = Parser.validate(sql: string, dialect: Dialect): boolean;
|
|
73
|
+
Dialects can be specified as strings or class instances:
|
|
76
74
|
|
|
77
|
-
|
|
78
|
-
|
|
75
|
+
```typescript
|
|
76
|
+
import { parse, PostgreSqlDialect } from 'sqlparser-rs';
|
|
77
|
+
|
|
78
|
+
// String dialect names (recommended)
|
|
79
|
+
parse('SELECT $1', 'postgresql');
|
|
80
|
+
parse('SELECT `col` FROM t', 'mysql');
|
|
81
|
+
|
|
82
|
+
// Or dialect instances
|
|
83
|
+
parse('SELECT $1', new PostgreSqlDialect());
|
|
79
84
|
```
|
|
80
85
|
|
|
81
|
-
|
|
86
|
+
Supported dialect strings: `'generic'`, `'ansi'`, `'mysql'`, `'postgresql'`, `'sqlite'`, `'snowflake'`, `'redshift'`, `'mssql'`, `'clickhouse'`, `'bigquery'`, `'duckdb'`, `'databricks'`, `'hive'`, `'oracle'`
|
|
87
|
+
|
|
88
|
+
### Parser Class
|
|
89
|
+
|
|
90
|
+
For advanced use cases with options:
|
|
82
91
|
|
|
83
92
|
```typescript
|
|
84
93
|
import { Parser, PostgreSqlDialect } from 'sqlparser-rs';
|
|
85
94
|
|
|
95
|
+
// Static methods (same as top-level functions)
|
|
96
|
+
Parser.parse('SELECT 1');
|
|
97
|
+
Parser.parse('SELECT 1', 'postgresql');
|
|
98
|
+
Parser.format('select 1');
|
|
99
|
+
Parser.validate('SELECT 1');
|
|
100
|
+
Parser.parseToJson('SELECT 1'); // Returns JSON string
|
|
101
|
+
Parser.parseToString('SELECT 1'); // Returns formatted string
|
|
102
|
+
Parser.getSupportedDialects(); // Returns dialect names
|
|
103
|
+
|
|
104
|
+
// Builder pattern for custom options
|
|
86
105
|
const parser = new Parser(new PostgreSqlDialect())
|
|
87
|
-
.withRecursionLimit(50)
|
|
88
|
-
.withOptions({
|
|
89
|
-
trailingCommas: true
|
|
90
|
-
});
|
|
106
|
+
.withRecursionLimit(50)
|
|
107
|
+
.withOptions({ trailingCommas: true });
|
|
91
108
|
|
|
92
109
|
const statements = parser.parse('SELECT * FROM users');
|
|
93
110
|
```
|
|
94
111
|
|
|
112
|
+
### WASM Initialization
|
|
113
|
+
|
|
114
|
+
The WASM module auto-initializes on import. For most use cases, no action is needed.
|
|
115
|
+
|
|
116
|
+
If you need to ensure WASM is ready before calling sync APIs (e.g., in tests):
|
|
117
|
+
|
|
118
|
+
```typescript
|
|
119
|
+
import { ready, parse } from 'sqlparser-rs';
|
|
120
|
+
|
|
121
|
+
// Wait for WASM to be ready
|
|
122
|
+
await ready();
|
|
123
|
+
|
|
124
|
+
// Now sync APIs are guaranteed to work
|
|
125
|
+
const ast = parse('SELECT 1');
|
|
126
|
+
```
|
|
127
|
+
|
|
95
128
|
### Dialects
|
|
96
129
|
|
|
97
130
|
All dialects from the upstream Rust crate are supported:
|
|
@@ -191,7 +224,7 @@ npm test
|
|
|
191
224
|
This package follows the upstream [sqlparser-rs](https://github.com/apache/datafusion-sqlparser-rs) version:
|
|
192
225
|
|
|
193
226
|
- **Major.Minor** matches the upstream Rust crate version
|
|
194
|
-
- **Patch** is for TypeScript binding fixes (e.g., `0.60.3-
|
|
227
|
+
- **Patch** is for TypeScript binding fixes (e.g., `0.60.3-rc2` = upstream `0.60.0` + binding fix)
|
|
195
228
|
|
|
196
229
|
| This package | sqlparser-rs |
|
|
197
230
|
|--------------|--------------|
|
package/dist/index.cjs
CHANGED
|
@@ -215,33 +215,45 @@ var WasmInitError = class WasmInitError extends Error {
|
|
|
215
215
|
|
|
216
216
|
//#endregion
|
|
217
217
|
//#region src/parser.ts
|
|
218
|
+
function resolveDialect(dialect = "generic") {
|
|
219
|
+
if (typeof dialect === "string") {
|
|
220
|
+
const resolved = dialectFromString(dialect);
|
|
221
|
+
if (!resolved) throw new Error(`Unknown dialect: ${dialect}`);
|
|
222
|
+
return resolved;
|
|
223
|
+
}
|
|
224
|
+
return dialect;
|
|
225
|
+
}
|
|
218
226
|
let wasmModule = null;
|
|
219
|
-
|
|
227
|
+
let initPromise = null;
|
|
228
|
+
let initStarted = false;
|
|
229
|
+
const isBrowser = typeof window !== "undefined" && typeof process === "undefined";
|
|
230
|
+
function startInit() {
|
|
231
|
+
if (initStarted) return;
|
|
232
|
+
initStarted = true;
|
|
233
|
+
initPromise = initWasm().catch(() => {});
|
|
234
|
+
}
|
|
235
|
+
startInit();
|
|
220
236
|
function getWasmModule() {
|
|
221
237
|
if (wasmModule) return wasmModule;
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
wasmModule = nodeModule.createRequire(require("url").pathToFileURL(__filename).href)(wasmPath);
|
|
231
|
-
return wasmModule;
|
|
232
|
-
} catch (error) {
|
|
233
|
-
throw new WasmInitError(`Failed to load WASM module: ${error instanceof Error ? error.message : String(error)}`);
|
|
234
|
-
}
|
|
238
|
+
throw new WasmInitError("WASM module not yet initialized. Use the async import or wait for module to load: import(\"sqlparser-rs\").then(({ parse }) => parse(sql))");
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* Wait for WASM to be ready (useful for ensuring sync APIs work)
|
|
242
|
+
*/
|
|
243
|
+
async function ready() {
|
|
244
|
+
startInit();
|
|
245
|
+
await initPromise;
|
|
235
246
|
}
|
|
236
247
|
/**
|
|
237
|
-
* Initialize the WASM module
|
|
238
|
-
* This must be called before using the Parser in browsers.
|
|
248
|
+
* Initialize the WASM module.
|
|
249
|
+
* This must be called before using the Parser in browsers and Node.js ESM.
|
|
250
|
+
* In Node.js CommonJS, the module is loaded automatically.
|
|
239
251
|
*
|
|
240
252
|
* @example
|
|
241
253
|
* ```typescript
|
|
242
254
|
* import { initWasm, Parser, GenericDialect } from 'sqlparser-rs';
|
|
243
255
|
*
|
|
244
|
-
* //
|
|
256
|
+
* // Initialize first
|
|
245
257
|
* await initWasm();
|
|
246
258
|
*
|
|
247
259
|
* // Then use the parser
|
|
@@ -250,21 +262,28 @@ function getWasmModule() {
|
|
|
250
262
|
*/
|
|
251
263
|
async function initWasm() {
|
|
252
264
|
if (wasmModule) return;
|
|
253
|
-
if (
|
|
254
|
-
|
|
265
|
+
if (isBrowser) {
|
|
266
|
+
try {
|
|
267
|
+
const wasmJsUrl = new URL("../wasm/sqlparser_rs_wasm_web.js", require("url").pathToFileURL(__filename).href);
|
|
268
|
+
const wasmBinaryUrl = new URL("../wasm/sqlparser_rs_wasm_web_bg.wasm", require("url").pathToFileURL(__filename).href);
|
|
269
|
+
const wasm = await import(
|
|
270
|
+
/* @vite-ignore */
|
|
271
|
+
wasmJsUrl.href
|
|
272
|
+
);
|
|
273
|
+
if (typeof wasm.default === "function") await wasm.default(wasmBinaryUrl);
|
|
274
|
+
wasmModule = wasm;
|
|
275
|
+
} catch (error) {
|
|
276
|
+
throw new WasmInitError(`Failed to load WASM module in browser: ${error instanceof Error ? error.message : String(error)}`);
|
|
277
|
+
}
|
|
255
278
|
return;
|
|
256
279
|
}
|
|
257
280
|
try {
|
|
258
|
-
|
|
259
|
-
const wasmBinaryUrl = new URL("../wasm/sqlparser_rs_wasm_web_bg.wasm", require("url").pathToFileURL(__filename).href);
|
|
260
|
-
const wasm = await import(
|
|
281
|
+
wasmModule = await import(
|
|
261
282
|
/* @vite-ignore */
|
|
262
|
-
|
|
283
|
+
new URL("../wasm/sqlparser_rs_wasm.js", require("url").pathToFileURL(__filename).href).href
|
|
263
284
|
);
|
|
264
|
-
if (typeof wasm.default === "function") await wasm.default(wasmBinaryUrl);
|
|
265
|
-
wasmModule = wasm;
|
|
266
285
|
} catch (error) {
|
|
267
|
-
throw new WasmInitError(`Failed to load WASM module
|
|
286
|
+
throw new WasmInitError(`Failed to load WASM module: ${error instanceof Error ? error.message : String(error)}`);
|
|
268
287
|
}
|
|
269
288
|
}
|
|
270
289
|
/**
|
|
@@ -339,28 +358,35 @@ var Parser = class Parser {
|
|
|
339
358
|
* Parse SQL statements
|
|
340
359
|
*
|
|
341
360
|
* @param sql - SQL string to parse
|
|
342
|
-
* @param dialect - SQL dialect to use
|
|
361
|
+
* @param dialect - SQL dialect to use (string name or Dialect instance, defaults to 'generic')
|
|
343
362
|
* @returns Array of parsed statements
|
|
344
363
|
*
|
|
345
364
|
* @example
|
|
346
365
|
* ```typescript
|
|
347
|
-
*
|
|
366
|
+
* // Using default dialect
|
|
367
|
+
* const statements = Parser.parse('SELECT 1');
|
|
368
|
+
*
|
|
369
|
+
* // Using string dialect name
|
|
370
|
+
* const pgStatements = Parser.parse('SELECT $1', 'postgresql');
|
|
371
|
+
*
|
|
372
|
+
* // Using dialect instance
|
|
373
|
+
* const mysqlStatements = Parser.parse('SELECT 1', new MySqlDialect());
|
|
348
374
|
* ```
|
|
349
375
|
*/
|
|
350
|
-
static parse(sql, dialect =
|
|
351
|
-
return new Parser(dialect).parse(sql);
|
|
376
|
+
static parse(sql, dialect = "generic") {
|
|
377
|
+
return new Parser(resolveDialect(dialect)).parse(sql);
|
|
352
378
|
}
|
|
353
379
|
/**
|
|
354
380
|
* Parse SQL and return the AST as a JSON string
|
|
355
381
|
*
|
|
356
382
|
* @param sql - SQL string to parse
|
|
357
|
-
* @param dialect - SQL dialect to use
|
|
383
|
+
* @param dialect - SQL dialect to use (string name or Dialect instance, defaults to 'generic')
|
|
358
384
|
* @returns JSON string representation of the AST
|
|
359
385
|
*/
|
|
360
|
-
static parseToJson(sql, dialect =
|
|
386
|
+
static parseToJson(sql, dialect = "generic") {
|
|
361
387
|
const wasm = getWasmModule();
|
|
362
388
|
try {
|
|
363
|
-
return wasm.parse_sql_to_json_string(dialect.name, sql);
|
|
389
|
+
return wasm.parse_sql_to_json_string(resolveDialect(dialect).name, sql);
|
|
364
390
|
} catch (error) {
|
|
365
391
|
throw ParserError.fromWasmError(error);
|
|
366
392
|
}
|
|
@@ -369,13 +395,13 @@ var Parser = class Parser {
|
|
|
369
395
|
* Parse SQL and return a formatted string representation
|
|
370
396
|
*
|
|
371
397
|
* @param sql - SQL string to parse
|
|
372
|
-
* @param dialect - SQL dialect to use
|
|
398
|
+
* @param dialect - SQL dialect to use (string name or Dialect instance, defaults to 'generic')
|
|
373
399
|
* @returns String representation of the parsed SQL
|
|
374
400
|
*/
|
|
375
|
-
static parseToString(sql, dialect =
|
|
401
|
+
static parseToString(sql, dialect = "generic") {
|
|
376
402
|
const wasm = getWasmModule();
|
|
377
403
|
try {
|
|
378
|
-
return wasm.parse_sql_to_string(dialect.name, sql);
|
|
404
|
+
return wasm.parse_sql_to_string(resolveDialect(dialect).name, sql);
|
|
379
405
|
} catch (error) {
|
|
380
406
|
throw ParserError.fromWasmError(error);
|
|
381
407
|
}
|
|
@@ -384,13 +410,13 @@ var Parser = class Parser {
|
|
|
384
410
|
* Format SQL by parsing and regenerating it (round-trip)
|
|
385
411
|
*
|
|
386
412
|
* @param sql - SQL string to format
|
|
387
|
-
* @param dialect - SQL dialect to use
|
|
413
|
+
* @param dialect - SQL dialect to use (string name or Dialect instance, defaults to 'generic')
|
|
388
414
|
* @returns Formatted SQL string
|
|
389
415
|
*/
|
|
390
|
-
static format(sql, dialect =
|
|
416
|
+
static format(sql, dialect = "generic") {
|
|
391
417
|
const wasm = getWasmModule();
|
|
392
418
|
try {
|
|
393
|
-
return wasm.format_sql(dialect.name, sql);
|
|
419
|
+
return wasm.format_sql(resolveDialect(dialect).name, sql);
|
|
394
420
|
} catch (error) {
|
|
395
421
|
throw ParserError.fromWasmError(error);
|
|
396
422
|
}
|
|
@@ -399,13 +425,13 @@ var Parser = class Parser {
|
|
|
399
425
|
* Validate SQL syntax without returning the full AST
|
|
400
426
|
*
|
|
401
427
|
* @param sql - SQL string to validate
|
|
402
|
-
* @param dialect - SQL dialect to use
|
|
428
|
+
* @param dialect - SQL dialect to use (string name or Dialect instance, defaults to 'generic')
|
|
403
429
|
* @returns true if valid, throws ParserError if invalid
|
|
404
430
|
*/
|
|
405
|
-
static validate(sql, dialect =
|
|
431
|
+
static validate(sql, dialect = "generic") {
|
|
406
432
|
const wasm = getWasmModule();
|
|
407
433
|
try {
|
|
408
|
-
return wasm.validate_sql(dialect.name, sql);
|
|
434
|
+
return wasm.validate_sql(resolveDialect(dialect).name, sql);
|
|
409
435
|
} catch (error) {
|
|
410
436
|
throw ParserError.fromWasmError(error);
|
|
411
437
|
}
|
|
@@ -417,6 +443,61 @@ var Parser = class Parser {
|
|
|
417
443
|
return getWasmModule().get_supported_dialects();
|
|
418
444
|
}
|
|
419
445
|
};
|
|
446
|
+
/**
|
|
447
|
+
* Parse SQL statements (convenience function)
|
|
448
|
+
*
|
|
449
|
+
* @param sql - SQL string to parse
|
|
450
|
+
* @param dialect - SQL dialect to use (string name or Dialect instance, defaults to 'generic')
|
|
451
|
+
* @returns Array of parsed statements
|
|
452
|
+
*
|
|
453
|
+
* @example
|
|
454
|
+
* ```typescript
|
|
455
|
+
* import { parse } from 'sqlparser-rs';
|
|
456
|
+
*
|
|
457
|
+
* const statements = parse('SELECT * FROM users');
|
|
458
|
+
* const pgStatements = parse('SELECT $1', 'postgresql');
|
|
459
|
+
* ```
|
|
460
|
+
*/
|
|
461
|
+
function parse(sql, dialect = "generic") {
|
|
462
|
+
return Parser.parse(sql, dialect);
|
|
463
|
+
}
|
|
464
|
+
/**
|
|
465
|
+
* Validate SQL syntax (convenience function)
|
|
466
|
+
*
|
|
467
|
+
* @param sql - SQL string to validate
|
|
468
|
+
* @param dialect - SQL dialect to use (string name or Dialect instance, defaults to 'generic')
|
|
469
|
+
* @returns true if valid, throws ParserError if invalid
|
|
470
|
+
*
|
|
471
|
+
* @example
|
|
472
|
+
* ```typescript
|
|
473
|
+
* import { validate } from 'sqlparser-rs';
|
|
474
|
+
*
|
|
475
|
+
* if (validate('SELECT * FROM users')) {
|
|
476
|
+
* console.log('Valid SQL!');
|
|
477
|
+
* }
|
|
478
|
+
* ```
|
|
479
|
+
*/
|
|
480
|
+
function validate(sql, dialect = "generic") {
|
|
481
|
+
return Parser.validate(sql, dialect);
|
|
482
|
+
}
|
|
483
|
+
/**
|
|
484
|
+
* Format SQL by parsing and regenerating it (convenience function)
|
|
485
|
+
*
|
|
486
|
+
* @param sql - SQL string to format
|
|
487
|
+
* @param dialect - SQL dialect to use (string name or Dialect instance, defaults to 'generic')
|
|
488
|
+
* @returns Formatted SQL string
|
|
489
|
+
*
|
|
490
|
+
* @example
|
|
491
|
+
* ```typescript
|
|
492
|
+
* import { format } from 'sqlparser-rs';
|
|
493
|
+
*
|
|
494
|
+
* const formatted = format('select * from users');
|
|
495
|
+
* // Returns: "SELECT * FROM users"
|
|
496
|
+
* ```
|
|
497
|
+
*/
|
|
498
|
+
function format(sql, dialect = "generic") {
|
|
499
|
+
return Parser.format(sql, dialect);
|
|
500
|
+
}
|
|
420
501
|
|
|
421
502
|
//#endregion
|
|
422
503
|
exports.AnsiDialect = AnsiDialect;
|
|
@@ -438,4 +519,8 @@ exports.SUPPORTED_DIALECTS = SUPPORTED_DIALECTS;
|
|
|
438
519
|
exports.SnowflakeDialect = SnowflakeDialect;
|
|
439
520
|
exports.WasmInitError = WasmInitError;
|
|
440
521
|
exports.dialectFromString = dialectFromString;
|
|
441
|
-
exports.
|
|
522
|
+
exports.format = format;
|
|
523
|
+
exports.initWasm = initWasm;
|
|
524
|
+
exports.parse = parse;
|
|
525
|
+
exports.ready = ready;
|
|
526
|
+
exports.validate = validate;
|
package/dist/index.d.cts
CHANGED
|
@@ -1216,6 +1216,8 @@ type MergeClause = {
|
|
|
1216
1216
|
type CommentObject = 'Column' | 'Table';
|
|
1217
1217
|
//#endregion
|
|
1218
1218
|
//#region src/parser.d.ts
|
|
1219
|
+
/** Dialect can be specified as a Dialect instance or a string name */
|
|
1220
|
+
type DialectInput = Dialect | DialectName;
|
|
1219
1221
|
/**
|
|
1220
1222
|
* Parser options
|
|
1221
1223
|
*/
|
|
@@ -1230,14 +1232,19 @@ interface ParserOptions {
|
|
|
1230
1232
|
recursionLimit?: number;
|
|
1231
1233
|
}
|
|
1232
1234
|
/**
|
|
1233
|
-
*
|
|
1234
|
-
|
|
1235
|
+
* Wait for WASM to be ready (useful for ensuring sync APIs work)
|
|
1236
|
+
*/
|
|
1237
|
+
declare function ready(): Promise<void>;
|
|
1238
|
+
/**
|
|
1239
|
+
* Initialize the WASM module.
|
|
1240
|
+
* This must be called before using the Parser in browsers and Node.js ESM.
|
|
1241
|
+
* In Node.js CommonJS, the module is loaded automatically.
|
|
1235
1242
|
*
|
|
1236
1243
|
* @example
|
|
1237
1244
|
* ```typescript
|
|
1238
1245
|
* import { initWasm, Parser, GenericDialect } from 'sqlparser-rs';
|
|
1239
1246
|
*
|
|
1240
|
-
* //
|
|
1247
|
+
* // Initialize first
|
|
1241
1248
|
* await initWasm();
|
|
1242
1249
|
*
|
|
1243
1250
|
* // Then use the parser
|
|
@@ -1299,52 +1306,108 @@ declare class Parser {
|
|
|
1299
1306
|
* Parse SQL statements
|
|
1300
1307
|
*
|
|
1301
1308
|
* @param sql - SQL string to parse
|
|
1302
|
-
* @param dialect - SQL dialect to use
|
|
1309
|
+
* @param dialect - SQL dialect to use (string name or Dialect instance, defaults to 'generic')
|
|
1303
1310
|
* @returns Array of parsed statements
|
|
1304
1311
|
*
|
|
1305
1312
|
* @example
|
|
1306
1313
|
* ```typescript
|
|
1307
|
-
*
|
|
1314
|
+
* // Using default dialect
|
|
1315
|
+
* const statements = Parser.parse('SELECT 1');
|
|
1316
|
+
*
|
|
1317
|
+
* // Using string dialect name
|
|
1318
|
+
* const pgStatements = Parser.parse('SELECT $1', 'postgresql');
|
|
1319
|
+
*
|
|
1320
|
+
* // Using dialect instance
|
|
1321
|
+
* const mysqlStatements = Parser.parse('SELECT 1', new MySqlDialect());
|
|
1308
1322
|
* ```
|
|
1309
1323
|
*/
|
|
1310
|
-
static parse(sql: string, dialect?:
|
|
1324
|
+
static parse(sql: string, dialect?: DialectInput): Statement[];
|
|
1311
1325
|
/**
|
|
1312
1326
|
* Parse SQL and return the AST as a JSON string
|
|
1313
1327
|
*
|
|
1314
1328
|
* @param sql - SQL string to parse
|
|
1315
|
-
* @param dialect - SQL dialect to use
|
|
1329
|
+
* @param dialect - SQL dialect to use (string name or Dialect instance, defaults to 'generic')
|
|
1316
1330
|
* @returns JSON string representation of the AST
|
|
1317
1331
|
*/
|
|
1318
|
-
static parseToJson(sql: string, dialect?:
|
|
1332
|
+
static parseToJson(sql: string, dialect?: DialectInput): string;
|
|
1319
1333
|
/**
|
|
1320
1334
|
* Parse SQL and return a formatted string representation
|
|
1321
1335
|
*
|
|
1322
1336
|
* @param sql - SQL string to parse
|
|
1323
|
-
* @param dialect - SQL dialect to use
|
|
1337
|
+
* @param dialect - SQL dialect to use (string name or Dialect instance, defaults to 'generic')
|
|
1324
1338
|
* @returns String representation of the parsed SQL
|
|
1325
1339
|
*/
|
|
1326
|
-
static parseToString(sql: string, dialect?:
|
|
1340
|
+
static parseToString(sql: string, dialect?: DialectInput): string;
|
|
1327
1341
|
/**
|
|
1328
1342
|
* Format SQL by parsing and regenerating it (round-trip)
|
|
1329
1343
|
*
|
|
1330
1344
|
* @param sql - SQL string to format
|
|
1331
|
-
* @param dialect - SQL dialect to use
|
|
1345
|
+
* @param dialect - SQL dialect to use (string name or Dialect instance, defaults to 'generic')
|
|
1332
1346
|
* @returns Formatted SQL string
|
|
1333
1347
|
*/
|
|
1334
|
-
static format(sql: string, dialect?:
|
|
1348
|
+
static format(sql: string, dialect?: DialectInput): string;
|
|
1335
1349
|
/**
|
|
1336
1350
|
* Validate SQL syntax without returning the full AST
|
|
1337
1351
|
*
|
|
1338
1352
|
* @param sql - SQL string to validate
|
|
1339
|
-
* @param dialect - SQL dialect to use
|
|
1353
|
+
* @param dialect - SQL dialect to use (string name or Dialect instance, defaults to 'generic')
|
|
1340
1354
|
* @returns true if valid, throws ParserError if invalid
|
|
1341
1355
|
*/
|
|
1342
|
-
static validate(sql: string, dialect?:
|
|
1356
|
+
static validate(sql: string, dialect?: DialectInput): boolean;
|
|
1343
1357
|
/**
|
|
1344
1358
|
* Get the list of supported dialect names
|
|
1345
1359
|
*/
|
|
1346
1360
|
static getSupportedDialects(): string[];
|
|
1347
1361
|
}
|
|
1362
|
+
/**
|
|
1363
|
+
* Parse SQL statements (convenience function)
|
|
1364
|
+
*
|
|
1365
|
+
* @param sql - SQL string to parse
|
|
1366
|
+
* @param dialect - SQL dialect to use (string name or Dialect instance, defaults to 'generic')
|
|
1367
|
+
* @returns Array of parsed statements
|
|
1368
|
+
*
|
|
1369
|
+
* @example
|
|
1370
|
+
* ```typescript
|
|
1371
|
+
* import { parse } from 'sqlparser-rs';
|
|
1372
|
+
*
|
|
1373
|
+
* const statements = parse('SELECT * FROM users');
|
|
1374
|
+
* const pgStatements = parse('SELECT $1', 'postgresql');
|
|
1375
|
+
* ```
|
|
1376
|
+
*/
|
|
1377
|
+
declare function parse(sql: string, dialect?: DialectInput): Statement[];
|
|
1378
|
+
/**
|
|
1379
|
+
* Validate SQL syntax (convenience function)
|
|
1380
|
+
*
|
|
1381
|
+
* @param sql - SQL string to validate
|
|
1382
|
+
* @param dialect - SQL dialect to use (string name or Dialect instance, defaults to 'generic')
|
|
1383
|
+
* @returns true if valid, throws ParserError if invalid
|
|
1384
|
+
*
|
|
1385
|
+
* @example
|
|
1386
|
+
* ```typescript
|
|
1387
|
+
* import { validate } from 'sqlparser-rs';
|
|
1388
|
+
*
|
|
1389
|
+
* if (validate('SELECT * FROM users')) {
|
|
1390
|
+
* console.log('Valid SQL!');
|
|
1391
|
+
* }
|
|
1392
|
+
* ```
|
|
1393
|
+
*/
|
|
1394
|
+
declare function validate(sql: string, dialect?: DialectInput): boolean;
|
|
1395
|
+
/**
|
|
1396
|
+
* Format SQL by parsing and regenerating it (convenience function)
|
|
1397
|
+
*
|
|
1398
|
+
* @param sql - SQL string to format
|
|
1399
|
+
* @param dialect - SQL dialect to use (string name or Dialect instance, defaults to 'generic')
|
|
1400
|
+
* @returns Formatted SQL string
|
|
1401
|
+
*
|
|
1402
|
+
* @example
|
|
1403
|
+
* ```typescript
|
|
1404
|
+
* import { format } from 'sqlparser-rs';
|
|
1405
|
+
*
|
|
1406
|
+
* const formatted = format('select * from users');
|
|
1407
|
+
* // Returns: "SELECT * FROM users"
|
|
1408
|
+
* ```
|
|
1409
|
+
*/
|
|
1410
|
+
declare function format(sql: string, dialect?: DialectInput): string;
|
|
1348
1411
|
//#endregion
|
|
1349
1412
|
//#region src/types/errors.d.ts
|
|
1350
1413
|
/**
|
|
@@ -1372,4 +1435,4 @@ declare class WasmInitError extends Error {
|
|
|
1372
1435
|
constructor(message: string);
|
|
1373
1436
|
}
|
|
1374
1437
|
//#endregion
|
|
1375
|
-
export { Action, AlterColumnOperation, AlterTableOperation, AnsiDialect, Assignment, BigQueryDialect, BinaryOperator, ClickHouseDialect, CloseCursor, ColumnDef, ColumnOption, ColumnOptionDef, CommentObject, CopyLegacyOption, CopyOption, CopySource, CopyTarget, Cte, DataType, DatabricksDialect, DateTimeField, DescribeAlias, type Dialect, type DialectName, DiscardObject, Distinct, DoUpdate, DropStatement, DuckDbDialect, ErrorLocation, ExceptSelectItem, ExcludeSelectItem, Expr, Fetch, FetchDirection, FileFormat, ForeignKeyOption, FunctionArg, FunctionArgExpr, FunctionExpr, GeneratedAs, GeneratedExpressionMode, GenericDialect, GrantObjects, GroupByExpr, HiveDialect, HiveDistributionStyle, HiveFormat, HiveIOFormat, HiveRowFormat, Ident, IdentWithAlias, IndexType, IntervalExpr, Join, JoinConstraint, JoinOperator, LateralView, LockClause, LockType, MergeClause, MsSqlDialect, MySqlDialect, NamedWindowDefinition, NonBlock, ObjectName, ObjectType, Offset, OffsetRows, OnCommit, OnInsert, OracleDialect, OrderByExpr, Parser, ParserError, type ParserOptions, PostgreSqlDialect, Privileges, Query, RedshiftDialect, ReferentialAction, RenameSelectItem, ReplaceSelectElement, ReplaceSelectItem, SQLiteDialect, SUPPORTED_DIALECTS, SchemaName, Select, SelectInto, SelectItem, SetExpr, SetOperator, SetQuantifier, ShowCreateObject, ShowStatementFilter, SnowflakeDialect, SqlOption, SqliteOnConflict, Statement, TableAlias, TableConstraint, TableFactor, TableWithJoins, Top, TransactionAccessMode, TransactionIsolationLevel, TransactionMode, TrimWhereField, UnaryOperator, Value, Values, WasmInitError, WildcardAdditionalOptions, WindowFrame, WindowFrameBound, WindowFrameUnits, WindowSpec, With, dialectFromString, initWasm };
|
|
1438
|
+
export { Action, AlterColumnOperation, AlterTableOperation, AnsiDialect, Assignment, BigQueryDialect, BinaryOperator, ClickHouseDialect, CloseCursor, ColumnDef, ColumnOption, ColumnOptionDef, CommentObject, CopyLegacyOption, CopyOption, CopySource, CopyTarget, Cte, DataType, DatabricksDialect, DateTimeField, DescribeAlias, type Dialect, type DialectInput, type DialectName, DiscardObject, Distinct, DoUpdate, DropStatement, DuckDbDialect, ErrorLocation, ExceptSelectItem, ExcludeSelectItem, Expr, Fetch, FetchDirection, FileFormat, ForeignKeyOption, FunctionArg, FunctionArgExpr, FunctionExpr, GeneratedAs, GeneratedExpressionMode, GenericDialect, GrantObjects, GroupByExpr, HiveDialect, HiveDistributionStyle, HiveFormat, HiveIOFormat, HiveRowFormat, Ident, IdentWithAlias, IndexType, IntervalExpr, Join, JoinConstraint, JoinOperator, LateralView, LockClause, LockType, MergeClause, MsSqlDialect, MySqlDialect, NamedWindowDefinition, NonBlock, ObjectName, ObjectType, Offset, OffsetRows, OnCommit, OnInsert, OracleDialect, OrderByExpr, Parser, ParserError, type ParserOptions, PostgreSqlDialect, Privileges, Query, RedshiftDialect, ReferentialAction, RenameSelectItem, ReplaceSelectElement, ReplaceSelectItem, SQLiteDialect, SUPPORTED_DIALECTS, SchemaName, Select, SelectInto, SelectItem, SetExpr, SetOperator, SetQuantifier, ShowCreateObject, ShowStatementFilter, SnowflakeDialect, SqlOption, SqliteOnConflict, Statement, TableAlias, TableConstraint, TableFactor, TableWithJoins, Top, TransactionAccessMode, TransactionIsolationLevel, TransactionMode, TrimWhereField, UnaryOperator, Value, Values, WasmInitError, WildcardAdditionalOptions, WindowFrame, WindowFrameBound, WindowFrameUnits, WindowSpec, With, dialectFromString, format, initWasm, parse, ready, validate };
|
package/dist/index.d.mts
CHANGED
|
@@ -1216,6 +1216,8 @@ type MergeClause = {
|
|
|
1216
1216
|
type CommentObject = 'Column' | 'Table';
|
|
1217
1217
|
//#endregion
|
|
1218
1218
|
//#region src/parser.d.ts
|
|
1219
|
+
/** Dialect can be specified as a Dialect instance or a string name */
|
|
1220
|
+
type DialectInput = Dialect | DialectName;
|
|
1219
1221
|
/**
|
|
1220
1222
|
* Parser options
|
|
1221
1223
|
*/
|
|
@@ -1230,14 +1232,19 @@ interface ParserOptions {
|
|
|
1230
1232
|
recursionLimit?: number;
|
|
1231
1233
|
}
|
|
1232
1234
|
/**
|
|
1233
|
-
*
|
|
1234
|
-
|
|
1235
|
+
* Wait for WASM to be ready (useful for ensuring sync APIs work)
|
|
1236
|
+
*/
|
|
1237
|
+
declare function ready(): Promise<void>;
|
|
1238
|
+
/**
|
|
1239
|
+
* Initialize the WASM module.
|
|
1240
|
+
* This must be called before using the Parser in browsers and Node.js ESM.
|
|
1241
|
+
* In Node.js CommonJS, the module is loaded automatically.
|
|
1235
1242
|
*
|
|
1236
1243
|
* @example
|
|
1237
1244
|
* ```typescript
|
|
1238
1245
|
* import { initWasm, Parser, GenericDialect } from 'sqlparser-rs';
|
|
1239
1246
|
*
|
|
1240
|
-
* //
|
|
1247
|
+
* // Initialize first
|
|
1241
1248
|
* await initWasm();
|
|
1242
1249
|
*
|
|
1243
1250
|
* // Then use the parser
|
|
@@ -1299,52 +1306,108 @@ declare class Parser {
|
|
|
1299
1306
|
* Parse SQL statements
|
|
1300
1307
|
*
|
|
1301
1308
|
* @param sql - SQL string to parse
|
|
1302
|
-
* @param dialect - SQL dialect to use
|
|
1309
|
+
* @param dialect - SQL dialect to use (string name or Dialect instance, defaults to 'generic')
|
|
1303
1310
|
* @returns Array of parsed statements
|
|
1304
1311
|
*
|
|
1305
1312
|
* @example
|
|
1306
1313
|
* ```typescript
|
|
1307
|
-
*
|
|
1314
|
+
* // Using default dialect
|
|
1315
|
+
* const statements = Parser.parse('SELECT 1');
|
|
1316
|
+
*
|
|
1317
|
+
* // Using string dialect name
|
|
1318
|
+
* const pgStatements = Parser.parse('SELECT $1', 'postgresql');
|
|
1319
|
+
*
|
|
1320
|
+
* // Using dialect instance
|
|
1321
|
+
* const mysqlStatements = Parser.parse('SELECT 1', new MySqlDialect());
|
|
1308
1322
|
* ```
|
|
1309
1323
|
*/
|
|
1310
|
-
static parse(sql: string, dialect?:
|
|
1324
|
+
static parse(sql: string, dialect?: DialectInput): Statement[];
|
|
1311
1325
|
/**
|
|
1312
1326
|
* Parse SQL and return the AST as a JSON string
|
|
1313
1327
|
*
|
|
1314
1328
|
* @param sql - SQL string to parse
|
|
1315
|
-
* @param dialect - SQL dialect to use
|
|
1329
|
+
* @param dialect - SQL dialect to use (string name or Dialect instance, defaults to 'generic')
|
|
1316
1330
|
* @returns JSON string representation of the AST
|
|
1317
1331
|
*/
|
|
1318
|
-
static parseToJson(sql: string, dialect?:
|
|
1332
|
+
static parseToJson(sql: string, dialect?: DialectInput): string;
|
|
1319
1333
|
/**
|
|
1320
1334
|
* Parse SQL and return a formatted string representation
|
|
1321
1335
|
*
|
|
1322
1336
|
* @param sql - SQL string to parse
|
|
1323
|
-
* @param dialect - SQL dialect to use
|
|
1337
|
+
* @param dialect - SQL dialect to use (string name or Dialect instance, defaults to 'generic')
|
|
1324
1338
|
* @returns String representation of the parsed SQL
|
|
1325
1339
|
*/
|
|
1326
|
-
static parseToString(sql: string, dialect?:
|
|
1340
|
+
static parseToString(sql: string, dialect?: DialectInput): string;
|
|
1327
1341
|
/**
|
|
1328
1342
|
* Format SQL by parsing and regenerating it (round-trip)
|
|
1329
1343
|
*
|
|
1330
1344
|
* @param sql - SQL string to format
|
|
1331
|
-
* @param dialect - SQL dialect to use
|
|
1345
|
+
* @param dialect - SQL dialect to use (string name or Dialect instance, defaults to 'generic')
|
|
1332
1346
|
* @returns Formatted SQL string
|
|
1333
1347
|
*/
|
|
1334
|
-
static format(sql: string, dialect?:
|
|
1348
|
+
static format(sql: string, dialect?: DialectInput): string;
|
|
1335
1349
|
/**
|
|
1336
1350
|
* Validate SQL syntax without returning the full AST
|
|
1337
1351
|
*
|
|
1338
1352
|
* @param sql - SQL string to validate
|
|
1339
|
-
* @param dialect - SQL dialect to use
|
|
1353
|
+
* @param dialect - SQL dialect to use (string name or Dialect instance, defaults to 'generic')
|
|
1340
1354
|
* @returns true if valid, throws ParserError if invalid
|
|
1341
1355
|
*/
|
|
1342
|
-
static validate(sql: string, dialect?:
|
|
1356
|
+
static validate(sql: string, dialect?: DialectInput): boolean;
|
|
1343
1357
|
/**
|
|
1344
1358
|
* Get the list of supported dialect names
|
|
1345
1359
|
*/
|
|
1346
1360
|
static getSupportedDialects(): string[];
|
|
1347
1361
|
}
|
|
1362
|
+
/**
|
|
1363
|
+
* Parse SQL statements (convenience function)
|
|
1364
|
+
*
|
|
1365
|
+
* @param sql - SQL string to parse
|
|
1366
|
+
* @param dialect - SQL dialect to use (string name or Dialect instance, defaults to 'generic')
|
|
1367
|
+
* @returns Array of parsed statements
|
|
1368
|
+
*
|
|
1369
|
+
* @example
|
|
1370
|
+
* ```typescript
|
|
1371
|
+
* import { parse } from 'sqlparser-rs';
|
|
1372
|
+
*
|
|
1373
|
+
* const statements = parse('SELECT * FROM users');
|
|
1374
|
+
* const pgStatements = parse('SELECT $1', 'postgresql');
|
|
1375
|
+
* ```
|
|
1376
|
+
*/
|
|
1377
|
+
declare function parse(sql: string, dialect?: DialectInput): Statement[];
|
|
1378
|
+
/**
|
|
1379
|
+
* Validate SQL syntax (convenience function)
|
|
1380
|
+
*
|
|
1381
|
+
* @param sql - SQL string to validate
|
|
1382
|
+
* @param dialect - SQL dialect to use (string name or Dialect instance, defaults to 'generic')
|
|
1383
|
+
* @returns true if valid, throws ParserError if invalid
|
|
1384
|
+
*
|
|
1385
|
+
* @example
|
|
1386
|
+
* ```typescript
|
|
1387
|
+
* import { validate } from 'sqlparser-rs';
|
|
1388
|
+
*
|
|
1389
|
+
* if (validate('SELECT * FROM users')) {
|
|
1390
|
+
* console.log('Valid SQL!');
|
|
1391
|
+
* }
|
|
1392
|
+
* ```
|
|
1393
|
+
*/
|
|
1394
|
+
declare function validate(sql: string, dialect?: DialectInput): boolean;
|
|
1395
|
+
/**
|
|
1396
|
+
* Format SQL by parsing and regenerating it (convenience function)
|
|
1397
|
+
*
|
|
1398
|
+
* @param sql - SQL string to format
|
|
1399
|
+
* @param dialect - SQL dialect to use (string name or Dialect instance, defaults to 'generic')
|
|
1400
|
+
* @returns Formatted SQL string
|
|
1401
|
+
*
|
|
1402
|
+
* @example
|
|
1403
|
+
* ```typescript
|
|
1404
|
+
* import { format } from 'sqlparser-rs';
|
|
1405
|
+
*
|
|
1406
|
+
* const formatted = format('select * from users');
|
|
1407
|
+
* // Returns: "SELECT * FROM users"
|
|
1408
|
+
* ```
|
|
1409
|
+
*/
|
|
1410
|
+
declare function format(sql: string, dialect?: DialectInput): string;
|
|
1348
1411
|
//#endregion
|
|
1349
1412
|
//#region src/types/errors.d.ts
|
|
1350
1413
|
/**
|
|
@@ -1372,4 +1435,4 @@ declare class WasmInitError extends Error {
|
|
|
1372
1435
|
constructor(message: string);
|
|
1373
1436
|
}
|
|
1374
1437
|
//#endregion
|
|
1375
|
-
export { Action, AlterColumnOperation, AlterTableOperation, AnsiDialect, Assignment, BigQueryDialect, BinaryOperator, ClickHouseDialect, CloseCursor, ColumnDef, ColumnOption, ColumnOptionDef, CommentObject, CopyLegacyOption, CopyOption, CopySource, CopyTarget, Cte, DataType, DatabricksDialect, DateTimeField, DescribeAlias, type Dialect, type DialectName, DiscardObject, Distinct, DoUpdate, DropStatement, DuckDbDialect, ErrorLocation, ExceptSelectItem, ExcludeSelectItem, Expr, Fetch, FetchDirection, FileFormat, ForeignKeyOption, FunctionArg, FunctionArgExpr, FunctionExpr, GeneratedAs, GeneratedExpressionMode, GenericDialect, GrantObjects, GroupByExpr, HiveDialect, HiveDistributionStyle, HiveFormat, HiveIOFormat, HiveRowFormat, Ident, IdentWithAlias, IndexType, IntervalExpr, Join, JoinConstraint, JoinOperator, LateralView, LockClause, LockType, MergeClause, MsSqlDialect, MySqlDialect, NamedWindowDefinition, NonBlock, ObjectName, ObjectType, Offset, OffsetRows, OnCommit, OnInsert, OracleDialect, OrderByExpr, Parser, ParserError, type ParserOptions, PostgreSqlDialect, Privileges, Query, RedshiftDialect, ReferentialAction, RenameSelectItem, ReplaceSelectElement, ReplaceSelectItem, SQLiteDialect, SUPPORTED_DIALECTS, SchemaName, Select, SelectInto, SelectItem, SetExpr, SetOperator, SetQuantifier, ShowCreateObject, ShowStatementFilter, SnowflakeDialect, SqlOption, SqliteOnConflict, Statement, TableAlias, TableConstraint, TableFactor, TableWithJoins, Top, TransactionAccessMode, TransactionIsolationLevel, TransactionMode, TrimWhereField, UnaryOperator, Value, Values, WasmInitError, WildcardAdditionalOptions, WindowFrame, WindowFrameBound, WindowFrameUnits, WindowSpec, With, dialectFromString, initWasm };
|
|
1438
|
+
export { Action, AlterColumnOperation, AlterTableOperation, AnsiDialect, Assignment, BigQueryDialect, BinaryOperator, ClickHouseDialect, CloseCursor, ColumnDef, ColumnOption, ColumnOptionDef, CommentObject, CopyLegacyOption, CopyOption, CopySource, CopyTarget, Cte, DataType, DatabricksDialect, DateTimeField, DescribeAlias, type Dialect, type DialectInput, type DialectName, DiscardObject, Distinct, DoUpdate, DropStatement, DuckDbDialect, ErrorLocation, ExceptSelectItem, ExcludeSelectItem, Expr, Fetch, FetchDirection, FileFormat, ForeignKeyOption, FunctionArg, FunctionArgExpr, FunctionExpr, GeneratedAs, GeneratedExpressionMode, GenericDialect, GrantObjects, GroupByExpr, HiveDialect, HiveDistributionStyle, HiveFormat, HiveIOFormat, HiveRowFormat, Ident, IdentWithAlias, IndexType, IntervalExpr, Join, JoinConstraint, JoinOperator, LateralView, LockClause, LockType, MergeClause, MsSqlDialect, MySqlDialect, NamedWindowDefinition, NonBlock, ObjectName, ObjectType, Offset, OffsetRows, OnCommit, OnInsert, OracleDialect, OrderByExpr, Parser, ParserError, type ParserOptions, PostgreSqlDialect, Privileges, Query, RedshiftDialect, ReferentialAction, RenameSelectItem, ReplaceSelectElement, ReplaceSelectItem, SQLiteDialect, SUPPORTED_DIALECTS, SchemaName, Select, SelectInto, SelectItem, SetExpr, SetOperator, SetQuantifier, ShowCreateObject, ShowStatementFilter, SnowflakeDialect, SqlOption, SqliteOnConflict, Statement, TableAlias, TableConstraint, TableFactor, TableWithJoins, Top, TransactionAccessMode, TransactionIsolationLevel, TransactionMode, TrimWhereField, UnaryOperator, Value, Values, WasmInitError, WildcardAdditionalOptions, WindowFrame, WindowFrameBound, WindowFrameUnits, WindowSpec, With, dialectFromString, format, initWasm, parse, ready, validate };
|
package/dist/index.mjs
CHANGED
|
@@ -214,33 +214,45 @@ var WasmInitError = class WasmInitError extends Error {
|
|
|
214
214
|
|
|
215
215
|
//#endregion
|
|
216
216
|
//#region src/parser.ts
|
|
217
|
+
function resolveDialect(dialect = "generic") {
|
|
218
|
+
if (typeof dialect === "string") {
|
|
219
|
+
const resolved = dialectFromString(dialect);
|
|
220
|
+
if (!resolved) throw new Error(`Unknown dialect: ${dialect}`);
|
|
221
|
+
return resolved;
|
|
222
|
+
}
|
|
223
|
+
return dialect;
|
|
224
|
+
}
|
|
217
225
|
let wasmModule = null;
|
|
218
|
-
|
|
226
|
+
let initPromise = null;
|
|
227
|
+
let initStarted = false;
|
|
228
|
+
const isBrowser = typeof window !== "undefined" && typeof process === "undefined";
|
|
229
|
+
function startInit() {
|
|
230
|
+
if (initStarted) return;
|
|
231
|
+
initStarted = true;
|
|
232
|
+
initPromise = initWasm().catch(() => {});
|
|
233
|
+
}
|
|
234
|
+
startInit();
|
|
219
235
|
function getWasmModule() {
|
|
220
236
|
if (wasmModule) return wasmModule;
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
wasmModule = nodeModule.createRequire(import.meta.url)(wasmPath);
|
|
230
|
-
return wasmModule;
|
|
231
|
-
} catch (error) {
|
|
232
|
-
throw new WasmInitError(`Failed to load WASM module: ${error instanceof Error ? error.message : String(error)}`);
|
|
233
|
-
}
|
|
237
|
+
throw new WasmInitError("WASM module not yet initialized. Use the async import or wait for module to load: import(\"sqlparser-rs\").then(({ parse }) => parse(sql))");
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* Wait for WASM to be ready (useful for ensuring sync APIs work)
|
|
241
|
+
*/
|
|
242
|
+
async function ready() {
|
|
243
|
+
startInit();
|
|
244
|
+
await initPromise;
|
|
234
245
|
}
|
|
235
246
|
/**
|
|
236
|
-
* Initialize the WASM module
|
|
237
|
-
* This must be called before using the Parser in browsers.
|
|
247
|
+
* Initialize the WASM module.
|
|
248
|
+
* This must be called before using the Parser in browsers and Node.js ESM.
|
|
249
|
+
* In Node.js CommonJS, the module is loaded automatically.
|
|
238
250
|
*
|
|
239
251
|
* @example
|
|
240
252
|
* ```typescript
|
|
241
253
|
* import { initWasm, Parser, GenericDialect } from 'sqlparser-rs';
|
|
242
254
|
*
|
|
243
|
-
* //
|
|
255
|
+
* // Initialize first
|
|
244
256
|
* await initWasm();
|
|
245
257
|
*
|
|
246
258
|
* // Then use the parser
|
|
@@ -249,21 +261,28 @@ function getWasmModule() {
|
|
|
249
261
|
*/
|
|
250
262
|
async function initWasm() {
|
|
251
263
|
if (wasmModule) return;
|
|
252
|
-
if (
|
|
253
|
-
|
|
264
|
+
if (isBrowser) {
|
|
265
|
+
try {
|
|
266
|
+
const wasmJsUrl = new URL("../wasm/sqlparser_rs_wasm_web.js", import.meta.url);
|
|
267
|
+
const wasmBinaryUrl = new URL("../wasm/sqlparser_rs_wasm_web_bg.wasm", import.meta.url);
|
|
268
|
+
const wasm = await import(
|
|
269
|
+
/* @vite-ignore */
|
|
270
|
+
wasmJsUrl.href
|
|
271
|
+
);
|
|
272
|
+
if (typeof wasm.default === "function") await wasm.default(wasmBinaryUrl);
|
|
273
|
+
wasmModule = wasm;
|
|
274
|
+
} catch (error) {
|
|
275
|
+
throw new WasmInitError(`Failed to load WASM module in browser: ${error instanceof Error ? error.message : String(error)}`);
|
|
276
|
+
}
|
|
254
277
|
return;
|
|
255
278
|
}
|
|
256
279
|
try {
|
|
257
|
-
|
|
258
|
-
const wasmBinaryUrl = new URL("../wasm/sqlparser_rs_wasm_web_bg.wasm", import.meta.url);
|
|
259
|
-
const wasm = await import(
|
|
280
|
+
wasmModule = await import(
|
|
260
281
|
/* @vite-ignore */
|
|
261
|
-
|
|
282
|
+
new URL("../wasm/sqlparser_rs_wasm.js", import.meta.url).href
|
|
262
283
|
);
|
|
263
|
-
if (typeof wasm.default === "function") await wasm.default(wasmBinaryUrl);
|
|
264
|
-
wasmModule = wasm;
|
|
265
284
|
} catch (error) {
|
|
266
|
-
throw new WasmInitError(`Failed to load WASM module
|
|
285
|
+
throw new WasmInitError(`Failed to load WASM module: ${error instanceof Error ? error.message : String(error)}`);
|
|
267
286
|
}
|
|
268
287
|
}
|
|
269
288
|
/**
|
|
@@ -338,28 +357,35 @@ var Parser = class Parser {
|
|
|
338
357
|
* Parse SQL statements
|
|
339
358
|
*
|
|
340
359
|
* @param sql - SQL string to parse
|
|
341
|
-
* @param dialect - SQL dialect to use
|
|
360
|
+
* @param dialect - SQL dialect to use (string name or Dialect instance, defaults to 'generic')
|
|
342
361
|
* @returns Array of parsed statements
|
|
343
362
|
*
|
|
344
363
|
* @example
|
|
345
364
|
* ```typescript
|
|
346
|
-
*
|
|
365
|
+
* // Using default dialect
|
|
366
|
+
* const statements = Parser.parse('SELECT 1');
|
|
367
|
+
*
|
|
368
|
+
* // Using string dialect name
|
|
369
|
+
* const pgStatements = Parser.parse('SELECT $1', 'postgresql');
|
|
370
|
+
*
|
|
371
|
+
* // Using dialect instance
|
|
372
|
+
* const mysqlStatements = Parser.parse('SELECT 1', new MySqlDialect());
|
|
347
373
|
* ```
|
|
348
374
|
*/
|
|
349
|
-
static parse(sql, dialect =
|
|
350
|
-
return new Parser(dialect).parse(sql);
|
|
375
|
+
static parse(sql, dialect = "generic") {
|
|
376
|
+
return new Parser(resolveDialect(dialect)).parse(sql);
|
|
351
377
|
}
|
|
352
378
|
/**
|
|
353
379
|
* Parse SQL and return the AST as a JSON string
|
|
354
380
|
*
|
|
355
381
|
* @param sql - SQL string to parse
|
|
356
|
-
* @param dialect - SQL dialect to use
|
|
382
|
+
* @param dialect - SQL dialect to use (string name or Dialect instance, defaults to 'generic')
|
|
357
383
|
* @returns JSON string representation of the AST
|
|
358
384
|
*/
|
|
359
|
-
static parseToJson(sql, dialect =
|
|
385
|
+
static parseToJson(sql, dialect = "generic") {
|
|
360
386
|
const wasm = getWasmModule();
|
|
361
387
|
try {
|
|
362
|
-
return wasm.parse_sql_to_json_string(dialect.name, sql);
|
|
388
|
+
return wasm.parse_sql_to_json_string(resolveDialect(dialect).name, sql);
|
|
363
389
|
} catch (error) {
|
|
364
390
|
throw ParserError.fromWasmError(error);
|
|
365
391
|
}
|
|
@@ -368,13 +394,13 @@ var Parser = class Parser {
|
|
|
368
394
|
* Parse SQL and return a formatted string representation
|
|
369
395
|
*
|
|
370
396
|
* @param sql - SQL string to parse
|
|
371
|
-
* @param dialect - SQL dialect to use
|
|
397
|
+
* @param dialect - SQL dialect to use (string name or Dialect instance, defaults to 'generic')
|
|
372
398
|
* @returns String representation of the parsed SQL
|
|
373
399
|
*/
|
|
374
|
-
static parseToString(sql, dialect =
|
|
400
|
+
static parseToString(sql, dialect = "generic") {
|
|
375
401
|
const wasm = getWasmModule();
|
|
376
402
|
try {
|
|
377
|
-
return wasm.parse_sql_to_string(dialect.name, sql);
|
|
403
|
+
return wasm.parse_sql_to_string(resolveDialect(dialect).name, sql);
|
|
378
404
|
} catch (error) {
|
|
379
405
|
throw ParserError.fromWasmError(error);
|
|
380
406
|
}
|
|
@@ -383,13 +409,13 @@ var Parser = class Parser {
|
|
|
383
409
|
* Format SQL by parsing and regenerating it (round-trip)
|
|
384
410
|
*
|
|
385
411
|
* @param sql - SQL string to format
|
|
386
|
-
* @param dialect - SQL dialect to use
|
|
412
|
+
* @param dialect - SQL dialect to use (string name or Dialect instance, defaults to 'generic')
|
|
387
413
|
* @returns Formatted SQL string
|
|
388
414
|
*/
|
|
389
|
-
static format(sql, dialect =
|
|
415
|
+
static format(sql, dialect = "generic") {
|
|
390
416
|
const wasm = getWasmModule();
|
|
391
417
|
try {
|
|
392
|
-
return wasm.format_sql(dialect.name, sql);
|
|
418
|
+
return wasm.format_sql(resolveDialect(dialect).name, sql);
|
|
393
419
|
} catch (error) {
|
|
394
420
|
throw ParserError.fromWasmError(error);
|
|
395
421
|
}
|
|
@@ -398,13 +424,13 @@ var Parser = class Parser {
|
|
|
398
424
|
* Validate SQL syntax without returning the full AST
|
|
399
425
|
*
|
|
400
426
|
* @param sql - SQL string to validate
|
|
401
|
-
* @param dialect - SQL dialect to use
|
|
427
|
+
* @param dialect - SQL dialect to use (string name or Dialect instance, defaults to 'generic')
|
|
402
428
|
* @returns true if valid, throws ParserError if invalid
|
|
403
429
|
*/
|
|
404
|
-
static validate(sql, dialect =
|
|
430
|
+
static validate(sql, dialect = "generic") {
|
|
405
431
|
const wasm = getWasmModule();
|
|
406
432
|
try {
|
|
407
|
-
return wasm.validate_sql(dialect.name, sql);
|
|
433
|
+
return wasm.validate_sql(resolveDialect(dialect).name, sql);
|
|
408
434
|
} catch (error) {
|
|
409
435
|
throw ParserError.fromWasmError(error);
|
|
410
436
|
}
|
|
@@ -416,6 +442,61 @@ var Parser = class Parser {
|
|
|
416
442
|
return getWasmModule().get_supported_dialects();
|
|
417
443
|
}
|
|
418
444
|
};
|
|
445
|
+
/**
|
|
446
|
+
* Parse SQL statements (convenience function)
|
|
447
|
+
*
|
|
448
|
+
* @param sql - SQL string to parse
|
|
449
|
+
* @param dialect - SQL dialect to use (string name or Dialect instance, defaults to 'generic')
|
|
450
|
+
* @returns Array of parsed statements
|
|
451
|
+
*
|
|
452
|
+
* @example
|
|
453
|
+
* ```typescript
|
|
454
|
+
* import { parse } from 'sqlparser-rs';
|
|
455
|
+
*
|
|
456
|
+
* const statements = parse('SELECT * FROM users');
|
|
457
|
+
* const pgStatements = parse('SELECT $1', 'postgresql');
|
|
458
|
+
* ```
|
|
459
|
+
*/
|
|
460
|
+
function parse(sql, dialect = "generic") {
|
|
461
|
+
return Parser.parse(sql, dialect);
|
|
462
|
+
}
|
|
463
|
+
/**
|
|
464
|
+
* Validate SQL syntax (convenience function)
|
|
465
|
+
*
|
|
466
|
+
* @param sql - SQL string to validate
|
|
467
|
+
* @param dialect - SQL dialect to use (string name or Dialect instance, defaults to 'generic')
|
|
468
|
+
* @returns true if valid, throws ParserError if invalid
|
|
469
|
+
*
|
|
470
|
+
* @example
|
|
471
|
+
* ```typescript
|
|
472
|
+
* import { validate } from 'sqlparser-rs';
|
|
473
|
+
*
|
|
474
|
+
* if (validate('SELECT * FROM users')) {
|
|
475
|
+
* console.log('Valid SQL!');
|
|
476
|
+
* }
|
|
477
|
+
* ```
|
|
478
|
+
*/
|
|
479
|
+
function validate(sql, dialect = "generic") {
|
|
480
|
+
return Parser.validate(sql, dialect);
|
|
481
|
+
}
|
|
482
|
+
/**
|
|
483
|
+
* Format SQL by parsing and regenerating it (convenience function)
|
|
484
|
+
*
|
|
485
|
+
* @param sql - SQL string to format
|
|
486
|
+
* @param dialect - SQL dialect to use (string name or Dialect instance, defaults to 'generic')
|
|
487
|
+
* @returns Formatted SQL string
|
|
488
|
+
*
|
|
489
|
+
* @example
|
|
490
|
+
* ```typescript
|
|
491
|
+
* import { format } from 'sqlparser-rs';
|
|
492
|
+
*
|
|
493
|
+
* const formatted = format('select * from users');
|
|
494
|
+
* // Returns: "SELECT * FROM users"
|
|
495
|
+
* ```
|
|
496
|
+
*/
|
|
497
|
+
function format(sql, dialect = "generic") {
|
|
498
|
+
return Parser.format(sql, dialect);
|
|
499
|
+
}
|
|
419
500
|
|
|
420
501
|
//#endregion
|
|
421
|
-
export { AnsiDialect, BigQueryDialect, ClickHouseDialect, DatabricksDialect, DuckDbDialect, GenericDialect, HiveDialect, MsSqlDialect, MySqlDialect, OracleDialect, Parser, ParserError, PostgreSqlDialect, RedshiftDialect, SQLiteDialect, SUPPORTED_DIALECTS, SnowflakeDialect, WasmInitError, dialectFromString, initWasm };
|
|
502
|
+
export { AnsiDialect, BigQueryDialect, ClickHouseDialect, DatabricksDialect, DuckDbDialect, GenericDialect, HiveDialect, MsSqlDialect, MySqlDialect, OracleDialect, Parser, ParserError, PostgreSqlDialect, RedshiftDialect, SQLiteDialect, SUPPORTED_DIALECTS, SnowflakeDialect, WasmInitError, dialectFromString, format, initWasm, parse, ready, validate };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "sqlparser-rs",
|
|
3
|
-
"version": "0.60.3-
|
|
3
|
+
"version": "0.60.3-rc2",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "A SQL parser for JavaScript and TypeScript, powered by datafusion-sqlparser-rs via WASM",
|
|
6
6
|
"main": "dist/index.cjs",
|
|
@@ -44,7 +44,7 @@
|
|
|
44
44
|
"license": "Apache-2.0",
|
|
45
45
|
"repository": {
|
|
46
46
|
"type": "git",
|
|
47
|
-
"url": "https://github.com/guan404ming/sqlparser-rs"
|
|
47
|
+
"url": "git+https://github.com/guan404ming/sqlparser-rs.git"
|
|
48
48
|
},
|
|
49
49
|
"devDependencies": {
|
|
50
50
|
"@types/node": "^25.0.10",
|
package/wasm/README.md
CHANGED
|
@@ -26,25 +26,22 @@ npm install sqlparser-rs
|
|
|
26
26
|
## Quick Start
|
|
27
27
|
|
|
28
28
|
```typescript
|
|
29
|
-
import {
|
|
29
|
+
import { parse, format, validate } from 'sqlparser-rs';
|
|
30
30
|
|
|
31
|
-
// Simple parsing
|
|
32
|
-
const statements =
|
|
31
|
+
// Simple parsing (uses generic dialect by default)
|
|
32
|
+
const statements = parse('SELECT * FROM users');
|
|
33
33
|
console.log(statements);
|
|
34
34
|
|
|
35
|
-
// With specific dialect
|
|
36
|
-
const pgStatements =
|
|
37
|
-
'SELECT * FROM users WHERE id = $1',
|
|
38
|
-
new PostgreSqlDialect()
|
|
39
|
-
);
|
|
35
|
+
// With specific dialect (string name)
|
|
36
|
+
const pgStatements = parse('SELECT * FROM users WHERE id = $1', 'postgresql');
|
|
40
37
|
|
|
41
38
|
// Format SQL
|
|
42
|
-
const formatted =
|
|
39
|
+
const formatted = format('select * from users');
|
|
43
40
|
console.log(formatted); // "SELECT * FROM users"
|
|
44
41
|
|
|
45
42
|
// Validate SQL
|
|
46
43
|
try {
|
|
47
|
-
|
|
44
|
+
validate('SELEC * FROM users');
|
|
48
45
|
} catch (error) {
|
|
49
46
|
console.log('Invalid SQL:', error.message);
|
|
50
47
|
}
|
|
@@ -52,46 +49,82 @@ try {
|
|
|
52
49
|
|
|
53
50
|
## API
|
|
54
51
|
|
|
55
|
-
###
|
|
52
|
+
### Top-Level Functions
|
|
56
53
|
|
|
57
|
-
The
|
|
58
|
-
|
|
59
|
-
#### Static Methods
|
|
54
|
+
The simplest way to use the parser:
|
|
60
55
|
|
|
61
56
|
```typescript
|
|
62
|
-
|
|
63
|
-
const statements = Parser.parse(sql: string, dialect: Dialect): Statement[];
|
|
64
|
-
|
|
65
|
-
// Parse and return JSON string
|
|
66
|
-
const json = Parser.parseToJson(sql: string, dialect: Dialect): string;
|
|
57
|
+
import { parse, format, validate } from 'sqlparser-rs';
|
|
67
58
|
|
|
68
|
-
// Parse
|
|
69
|
-
|
|
59
|
+
// Parse SQL - dialect defaults to 'generic'
|
|
60
|
+
parse('SELECT * FROM users');
|
|
61
|
+
parse('SELECT * FROM users', 'postgresql');
|
|
62
|
+
parse('SELECT * FROM users', 'mysql');
|
|
70
63
|
|
|
71
64
|
// Format SQL (round-trip through parser)
|
|
72
|
-
|
|
65
|
+
format('select * from users'); // "SELECT * FROM users"
|
|
66
|
+
|
|
67
|
+
// Validate SQL syntax (throws ParserError if invalid)
|
|
68
|
+
validate('SELECT * FROM users'); // true
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
### Dialect Options
|
|
73
72
|
|
|
74
|
-
|
|
75
|
-
const isValid = Parser.validate(sql: string, dialect: Dialect): boolean;
|
|
73
|
+
Dialects can be specified as strings or class instances:
|
|
76
74
|
|
|
77
|
-
|
|
78
|
-
|
|
75
|
+
```typescript
|
|
76
|
+
import { parse, PostgreSqlDialect } from 'sqlparser-rs';
|
|
77
|
+
|
|
78
|
+
// String dialect names (recommended)
|
|
79
|
+
parse('SELECT $1', 'postgresql');
|
|
80
|
+
parse('SELECT `col` FROM t', 'mysql');
|
|
81
|
+
|
|
82
|
+
// Or dialect instances
|
|
83
|
+
parse('SELECT $1', new PostgreSqlDialect());
|
|
79
84
|
```
|
|
80
85
|
|
|
81
|
-
|
|
86
|
+
Supported dialect strings: `'generic'`, `'ansi'`, `'mysql'`, `'postgresql'`, `'sqlite'`, `'snowflake'`, `'redshift'`, `'mssql'`, `'clickhouse'`, `'bigquery'`, `'duckdb'`, `'databricks'`, `'hive'`, `'oracle'`
|
|
87
|
+
|
|
88
|
+
### Parser Class
|
|
89
|
+
|
|
90
|
+
For advanced use cases with options:
|
|
82
91
|
|
|
83
92
|
```typescript
|
|
84
93
|
import { Parser, PostgreSqlDialect } from 'sqlparser-rs';
|
|
85
94
|
|
|
95
|
+
// Static methods (same as top-level functions)
|
|
96
|
+
Parser.parse('SELECT 1');
|
|
97
|
+
Parser.parse('SELECT 1', 'postgresql');
|
|
98
|
+
Parser.format('select 1');
|
|
99
|
+
Parser.validate('SELECT 1');
|
|
100
|
+
Parser.parseToJson('SELECT 1'); // Returns JSON string
|
|
101
|
+
Parser.parseToString('SELECT 1'); // Returns formatted string
|
|
102
|
+
Parser.getSupportedDialects(); // Returns dialect names
|
|
103
|
+
|
|
104
|
+
// Builder pattern for custom options
|
|
86
105
|
const parser = new Parser(new PostgreSqlDialect())
|
|
87
|
-
.withRecursionLimit(50)
|
|
88
|
-
.withOptions({
|
|
89
|
-
trailingCommas: true
|
|
90
|
-
});
|
|
106
|
+
.withRecursionLimit(50)
|
|
107
|
+
.withOptions({ trailingCommas: true });
|
|
91
108
|
|
|
92
109
|
const statements = parser.parse('SELECT * FROM users');
|
|
93
110
|
```
|
|
94
111
|
|
|
112
|
+
### WASM Initialization
|
|
113
|
+
|
|
114
|
+
The WASM module auto-initializes on import. For most use cases, no action is needed.
|
|
115
|
+
|
|
116
|
+
If you need to ensure WASM is ready before calling sync APIs (e.g., in tests):
|
|
117
|
+
|
|
118
|
+
```typescript
|
|
119
|
+
import { ready, parse } from 'sqlparser-rs';
|
|
120
|
+
|
|
121
|
+
// Wait for WASM to be ready
|
|
122
|
+
await ready();
|
|
123
|
+
|
|
124
|
+
// Now sync APIs are guaranteed to work
|
|
125
|
+
const ast = parse('SELECT 1');
|
|
126
|
+
```
|
|
127
|
+
|
|
95
128
|
### Dialects
|
|
96
129
|
|
|
97
130
|
All dialects from the upstream Rust crate are supported:
|
|
@@ -191,7 +224,7 @@ npm test
|
|
|
191
224
|
This package follows the upstream [sqlparser-rs](https://github.com/apache/datafusion-sqlparser-rs) version:
|
|
192
225
|
|
|
193
226
|
- **Major.Minor** matches the upstream Rust crate version
|
|
194
|
-
- **Patch** is for TypeScript binding fixes (e.g., `0.60.3-
|
|
227
|
+
- **Patch** is for TypeScript binding fixes (e.g., `0.60.3-rc2` = upstream `0.60.0` + binding fix)
|
|
195
228
|
|
|
196
229
|
| This package | sqlparser-rs |
|
|
197
230
|
|--------------|--------------|
|
package/wasm/package.json
CHANGED
|
Binary file
|
|
Binary file
|