sqlparser-rs 0.60.1 → 0.60.3-rc1

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 (48) hide show
  1. package/README.md +14 -21
  2. package/dist/index.cjs +441 -0
  3. package/dist/index.d.cts +1375 -0
  4. package/dist/index.d.mts +1375 -0
  5. package/dist/index.mjs +421 -0
  6. package/package.json +10 -10
  7. package/wasm/README.md +14 -21
  8. package/wasm/package.json +1 -1
  9. package/wasm/sqlparser_rs_wasm_bg.wasm +0 -0
  10. package/wasm/sqlparser_rs_wasm_web.js +628 -0
  11. package/wasm/sqlparser_rs_wasm_web_bg.wasm +0 -0
  12. package/dist/cjs/dialects.js +0 -202
  13. package/dist/cjs/dialects.js.map +0 -1
  14. package/dist/cjs/index.js +0 -82
  15. package/dist/cjs/index.js.map +0 -1
  16. package/dist/cjs/parser.js +0 -264
  17. package/dist/cjs/parser.js.map +0 -1
  18. package/dist/cjs/types/ast.js +0 -10
  19. package/dist/cjs/types/ast.js.map +0 -1
  20. package/dist/cjs/types/errors.js +0 -49
  21. package/dist/cjs/types/errors.js.map +0 -1
  22. package/dist/cjs/types/index.js +0 -19
  23. package/dist/cjs/types/index.js.map +0 -1
  24. package/dist/esm/dialects.js +0 -184
  25. package/dist/esm/dialects.js.map +0 -1
  26. package/dist/esm/index.js +0 -47
  27. package/dist/esm/index.js.map +0 -1
  28. package/dist/esm/package.json +0 -1
  29. package/dist/esm/parser.js +0 -226
  30. package/dist/esm/parser.js.map +0 -1
  31. package/dist/esm/types/ast.js +0 -9
  32. package/dist/esm/types/ast.js.map +0 -1
  33. package/dist/esm/types/errors.js +0 -44
  34. package/dist/esm/types/errors.js.map +0 -1
  35. package/dist/esm/types/index.js +0 -3
  36. package/dist/esm/types/index.js.map +0 -1
  37. package/dist/types/dialects.d.ts +0 -118
  38. package/dist/types/dialects.d.ts.map +0 -1
  39. package/dist/types/index.d.ts +0 -46
  40. package/dist/types/index.d.ts.map +0 -1
  41. package/dist/types/parser.d.ts +0 -130
  42. package/dist/types/parser.d.ts.map +0 -1
  43. package/dist/types/types/ast.d.ts +0 -1097
  44. package/dist/types/types/ast.d.ts.map +0 -1
  45. package/dist/types/types/errors.d.ts +0 -25
  46. package/dist/types/types/errors.d.ts.map +0 -1
  47. package/dist/types/types/index.d.ts +0 -3
  48. package/dist/types/types/index.d.ts.map +0 -1
package/README.md CHANGED
@@ -14,7 +14,6 @@ A SQL parser for JavaScript and TypeScript, powered by [datafusion-sqlparser-rs]
14
14
  - Parse SQL into a detailed Abstract Syntax Tree (AST)
15
15
  - Support for 13+ SQL dialects (PostgreSQL, MySQL, SQLite, BigQuery, etc.)
16
16
  - Full TypeScript type definitions
17
- - Works in Node.js and browsers
18
17
  - Fast and accurate parsing using the battle-tested Rust implementation
19
18
  - Zero native dependencies
20
19
 
@@ -30,22 +29,22 @@ npm install sqlparser-rs
30
29
  import { Parser, GenericDialect, PostgreSqlDialect } from 'sqlparser-rs';
31
30
 
32
31
  // Simple parsing
33
- const statements = await Parser.parse('SELECT * FROM users', new GenericDialect());
32
+ const statements = Parser.parse('SELECT * FROM users', new GenericDialect());
34
33
  console.log(statements);
35
34
 
36
35
  // With specific dialect
37
- const pgStatements = await Parser.parse(
36
+ const pgStatements = Parser.parse(
38
37
  'SELECT * FROM users WHERE id = $1',
39
38
  new PostgreSqlDialect()
40
39
  );
41
40
 
42
41
  // Format SQL
43
- const formatted = await Parser.format('select * from users', new GenericDialect());
42
+ const formatted = Parser.format('select * from users', new GenericDialect());
44
43
  console.log(formatted); // "SELECT * FROM users"
45
44
 
46
45
  // Validate SQL
47
46
  try {
48
- await Parser.validate('SELEC * FROM users', new GenericDialect());
47
+ Parser.validate('SELEC * FROM users', new GenericDialect());
49
48
  } catch (error) {
50
49
  console.log('Invalid SQL:', error.message);
51
50
  }
@@ -61,22 +60,22 @@ The main class for parsing SQL.
61
60
 
62
61
  ```typescript
63
62
  // Parse SQL into statements
64
- const statements = await Parser.parse(sql: string, dialect: Dialect): Promise<Statement[]>;
63
+ const statements = Parser.parse(sql: string, dialect: Dialect): Statement[];
65
64
 
66
65
  // Parse and return JSON string
67
- const json = await Parser.parseToJson(sql: string, dialect: Dialect): Promise<string>;
66
+ const json = Parser.parseToJson(sql: string, dialect: Dialect): string;
68
67
 
69
68
  // Parse and return formatted SQL string
70
- const formatted = await Parser.parseToString(sql: string, dialect: Dialect): Promise<string>;
69
+ const formatted = Parser.parseToString(sql: string, dialect: Dialect): string;
71
70
 
72
71
  // Format SQL (round-trip through parser)
73
- const formatted = await Parser.format(sql: string, dialect: Dialect): Promise<string>;
72
+ const formatted = Parser.format(sql: string, dialect: Dialect): string;
74
73
 
75
74
  // Validate SQL syntax
76
- const isValid = await Parser.validate(sql: string, dialect: Dialect): Promise<boolean>;
75
+ const isValid = Parser.validate(sql: string, dialect: Dialect): boolean;
77
76
 
78
77
  // Get list of supported dialects
79
- const dialects = await Parser.getSupportedDialects(): Promise<string[]>;
78
+ const dialects = Parser.getSupportedDialects(): string[];
80
79
  ```
81
80
 
82
81
  #### Instance Methods (Builder Pattern)
@@ -90,13 +89,7 @@ const parser = new Parser(new PostgreSqlDialect())
90
89
  trailingCommas: true
91
90
  });
92
91
 
93
- // Parse asynchronously
94
- const statements = await parser.parseAsync('SELECT * FROM users');
95
-
96
- // Parse synchronously (requires initWasm() first)
97
- import { initWasm } from 'sqlparser-rs';
98
- await initWasm();
99
- const statements = parser.parseSync('SELECT * FROM users');
92
+ const statements = parser.parse('SELECT * FROM users');
100
93
  ```
101
94
 
102
95
  ### Dialects
@@ -131,7 +124,7 @@ const dialect = dialectFromString('postgresql'); // Returns PostgreSqlDialect in
131
124
  import { Parser, GenericDialect, ParserError } from 'sqlparser-rs';
132
125
 
133
126
  try {
134
- await Parser.parse('SELEC * FROM users', new GenericDialect());
127
+ Parser.parse('SELEC * FROM users', new GenericDialect());
135
128
  } catch (error) {
136
129
  if (error instanceof ParserError) {
137
130
  console.log('Parse error:', error.message);
@@ -149,7 +142,7 @@ Full TypeScript types are provided for the AST:
149
142
  ```typescript
150
143
  import type { Statement, Query, Expr, Ident, ObjectName } from 'sqlparser-rs';
151
144
 
152
- const statements: Statement[] = await Parser.parse('SELECT 1', new GenericDialect());
145
+ const statements: Statement[] = Parser.parse('SELECT 1', new GenericDialect());
153
146
 
154
147
  // Statement is a discriminated union type
155
148
  for (const stmt of statements) {
@@ -198,7 +191,7 @@ npm test
198
191
  This package follows the upstream [sqlparser-rs](https://github.com/apache/datafusion-sqlparser-rs) version:
199
192
 
200
193
  - **Major.Minor** matches the upstream Rust crate version
201
- - **Patch** is for TypeScript binding fixes (e.g., `0.60.1` = upstream `0.60.0` + binding fix)
194
+ - **Patch** is for TypeScript binding fixes (e.g., `0.60.3-rc1` = upstream `0.60.0` + binding fix)
202
195
 
203
196
  | This package | sqlparser-rs |
204
197
  |--------------|--------------|
package/dist/index.cjs ADDED
@@ -0,0 +1,441 @@
1
+
2
+ //#region src/dialects.ts
3
+ /**
4
+ * Generic SQL dialect - accepts most SQL syntax
5
+ */
6
+ var GenericDialect = class {
7
+ constructor() {
8
+ this.name = "generic";
9
+ }
10
+ };
11
+ /**
12
+ * ANSI SQL standard dialect
13
+ */
14
+ var AnsiDialect = class {
15
+ constructor() {
16
+ this.name = "ansi";
17
+ }
18
+ };
19
+ /**
20
+ * MySQL dialect
21
+ */
22
+ var MySqlDialect = class {
23
+ constructor() {
24
+ this.name = "mysql";
25
+ }
26
+ };
27
+ /**
28
+ * PostgreSQL dialect
29
+ */
30
+ var PostgreSqlDialect = class {
31
+ constructor() {
32
+ this.name = "postgresql";
33
+ }
34
+ };
35
+ /**
36
+ * SQLite dialect
37
+ */
38
+ var SQLiteDialect = class {
39
+ constructor() {
40
+ this.name = "sqlite";
41
+ }
42
+ };
43
+ /**
44
+ * Snowflake dialect
45
+ */
46
+ var SnowflakeDialect = class {
47
+ constructor() {
48
+ this.name = "snowflake";
49
+ }
50
+ };
51
+ /**
52
+ * Amazon Redshift dialect
53
+ */
54
+ var RedshiftDialect = class {
55
+ constructor() {
56
+ this.name = "redshift";
57
+ }
58
+ };
59
+ /**
60
+ * Microsoft SQL Server dialect
61
+ */
62
+ var MsSqlDialect = class {
63
+ constructor() {
64
+ this.name = "mssql";
65
+ }
66
+ };
67
+ /**
68
+ * ClickHouse dialect
69
+ */
70
+ var ClickHouseDialect = class {
71
+ constructor() {
72
+ this.name = "clickhouse";
73
+ }
74
+ };
75
+ /**
76
+ * Google BigQuery dialect
77
+ */
78
+ var BigQueryDialect = class {
79
+ constructor() {
80
+ this.name = "bigquery";
81
+ }
82
+ };
83
+ /**
84
+ * DuckDB dialect
85
+ */
86
+ var DuckDbDialect = class {
87
+ constructor() {
88
+ this.name = "duckdb";
89
+ }
90
+ };
91
+ /**
92
+ * Databricks dialect
93
+ */
94
+ var DatabricksDialect = class {
95
+ constructor() {
96
+ this.name = "databricks";
97
+ }
98
+ };
99
+ /**
100
+ * Apache Hive dialect
101
+ */
102
+ var HiveDialect = class {
103
+ constructor() {
104
+ this.name = "hive";
105
+ }
106
+ };
107
+ /**
108
+ * Oracle dialect
109
+ */
110
+ var OracleDialect = class {
111
+ constructor() {
112
+ this.name = "oracle";
113
+ }
114
+ };
115
+ /**
116
+ * All supported dialect names
117
+ */
118
+ const SUPPORTED_DIALECTS = [
119
+ "generic",
120
+ "ansi",
121
+ "mysql",
122
+ "postgresql",
123
+ "sqlite",
124
+ "snowflake",
125
+ "redshift",
126
+ "mssql",
127
+ "clickhouse",
128
+ "bigquery",
129
+ "duckdb",
130
+ "databricks",
131
+ "hive",
132
+ "oracle"
133
+ ];
134
+ /**
135
+ * Map of dialect names to dialect classes
136
+ */
137
+ const DIALECT_MAP = {
138
+ generic: GenericDialect,
139
+ ansi: AnsiDialect,
140
+ mysql: MySqlDialect,
141
+ postgresql: PostgreSqlDialect,
142
+ sqlite: SQLiteDialect,
143
+ snowflake: SnowflakeDialect,
144
+ redshift: RedshiftDialect,
145
+ mssql: MsSqlDialect,
146
+ clickhouse: ClickHouseDialect,
147
+ bigquery: BigQueryDialect,
148
+ duckdb: DuckDbDialect,
149
+ databricks: DatabricksDialect,
150
+ hive: HiveDialect,
151
+ oracle: OracleDialect
152
+ };
153
+ /**
154
+ * Create a dialect instance from a string name
155
+ *
156
+ * @param name - The name of the dialect (case-insensitive)
157
+ * @returns A dialect instance, or undefined if the dialect is not recognized
158
+ *
159
+ * @example
160
+ * ```typescript
161
+ * const dialect = dialectFromString('postgresql');
162
+ * if (dialect) {
163
+ * const ast = Parser.parse('SELECT 1', dialect);
164
+ * }
165
+ * ```
166
+ */
167
+ function dialectFromString(name) {
168
+ const normalized = name.toLowerCase();
169
+ const DialectClass = DIALECT_MAP[{
170
+ postgres: "postgresql",
171
+ pg: "postgresql",
172
+ sqlserver: "mssql",
173
+ duck: "duckdb"
174
+ }[normalized] ?? normalized];
175
+ return DialectClass ? new DialectClass() : void 0;
176
+ }
177
+
178
+ //#endregion
179
+ //#region src/types/errors.ts
180
+ /**
181
+ * Error thrown when SQL parsing fails
182
+ */
183
+ var ParserError = class ParserError extends Error {
184
+ constructor(message, location) {
185
+ super(message);
186
+ this.name = "ParserError";
187
+ this.location = location;
188
+ if (Error.captureStackTrace) Error.captureStackTrace(this, ParserError);
189
+ }
190
+ /**
191
+ * Create a ParserError from a WASM error object
192
+ */
193
+ static fromWasmError(error) {
194
+ if (typeof error === "string") return new ParserError(error);
195
+ if (error && typeof error === "object") {
196
+ const err = error;
197
+ return new ParserError(typeof err.message === "string" ? err.message : String(error), typeof err.line === "number" && typeof err.column === "number" ? {
198
+ line: err.line,
199
+ column: err.column
200
+ } : void 0);
201
+ }
202
+ return new ParserError(String(error));
203
+ }
204
+ };
205
+ /**
206
+ * Error thrown when WASM module fails to initialize
207
+ */
208
+ var WasmInitError = class WasmInitError extends Error {
209
+ constructor(message) {
210
+ super(message);
211
+ this.name = "WasmInitError";
212
+ if (Error.captureStackTrace) Error.captureStackTrace(this, WasmInitError);
213
+ }
214
+ };
215
+
216
+ //#endregion
217
+ //#region src/parser.ts
218
+ let wasmModule = null;
219
+ const isBrowser = typeof window !== "undefined" || typeof self !== "undefined";
220
+ function getWasmModule() {
221
+ if (wasmModule) return wasmModule;
222
+ if (isBrowser) throw new WasmInitError("WASM module not initialized. Call initWasm() first in browser environments.");
223
+ try {
224
+ const nodeRequire = (0, eval)("require");
225
+ const nodeModule = nodeRequire("node:module");
226
+ const nodeUrl = nodeRequire("node:url");
227
+ const nodePath = nodeRequire("node:path");
228
+ const currentDir = nodePath.dirname(nodeUrl.fileURLToPath(require("url").pathToFileURL(__filename).href));
229
+ const wasmPath = nodePath.join(currentDir, "../wasm/sqlparser_rs_wasm.js");
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
+ }
235
+ }
236
+ /**
237
+ * Initialize the WASM module for browser environments.
238
+ * This must be called before using the Parser in browsers.
239
+ *
240
+ * @example
241
+ * ```typescript
242
+ * import { initWasm, Parser, GenericDialect } from 'sqlparser-rs';
243
+ *
244
+ * // In browser, initialize first
245
+ * await initWasm();
246
+ *
247
+ * // Then use the parser
248
+ * const ast = Parser.parse('SELECT 1', new GenericDialect());
249
+ * ```
250
+ */
251
+ async function initWasm() {
252
+ if (wasmModule) return;
253
+ if (!isBrowser) {
254
+ getWasmModule();
255
+ return;
256
+ }
257
+ try {
258
+ const wasmJsUrl = new URL("../wasm/sqlparser_rs_wasm_web.js", require("url").pathToFileURL(__filename).href);
259
+ const wasmBinaryUrl = new URL("../wasm/sqlparser_rs_wasm_web_bg.wasm", require("url").pathToFileURL(__filename).href);
260
+ const wasm = await import(
261
+ /* @vite-ignore */
262
+ wasmJsUrl.href
263
+ );
264
+ if (typeof wasm.default === "function") await wasm.default(wasmBinaryUrl);
265
+ wasmModule = wasm;
266
+ } catch (error) {
267
+ throw new WasmInitError(`Failed to load WASM module in browser: ${error instanceof Error ? error.message : String(error)}`);
268
+ }
269
+ }
270
+ /**
271
+ * SQL Parser
272
+ *
273
+ * Parses SQL statements into an Abstract Syntax Tree (AST).
274
+ *
275
+ * @example
276
+ * ```typescript
277
+ * import { Parser, PostgreSqlDialect } from 'sqlparser-rs';
278
+ *
279
+ * // Simple parsing
280
+ * const statements = Parser.parse('SELECT * FROM users', new PostgreSqlDialect());
281
+ *
282
+ * // With builder pattern
283
+ * const parser = new Parser(new PostgreSqlDialect())
284
+ * .withRecursionLimit(50)
285
+ * .withOptions({ trailingCommas: true });
286
+ *
287
+ * const ast = parser.parse('SELECT * FROM users');
288
+ * ```
289
+ */
290
+ var Parser = class Parser {
291
+ /**
292
+ * Create a new parser instance
293
+ *
294
+ * @param dialect - The SQL dialect to use (defaults to GenericDialect)
295
+ */
296
+ constructor(dialect = new GenericDialect()) {
297
+ this.dialect = dialect;
298
+ this.options = {};
299
+ }
300
+ /**
301
+ * Set the recursion limit for parsing nested expressions
302
+ *
303
+ * @param limit - Maximum recursion depth
304
+ * @returns This parser instance for chaining
305
+ */
306
+ withRecursionLimit(limit) {
307
+ this.options.recursionLimit = limit;
308
+ return this;
309
+ }
310
+ /**
311
+ * Set parser options
312
+ *
313
+ * @param options - Parser options
314
+ * @returns This parser instance for chaining
315
+ */
316
+ withOptions(options) {
317
+ this.options = {
318
+ ...this.options,
319
+ ...options
320
+ };
321
+ return this;
322
+ }
323
+ /**
324
+ * Parse SQL statements
325
+ *
326
+ * @param sql - SQL string to parse
327
+ * @returns Array of parsed statements
328
+ */
329
+ parse(sql) {
330
+ const wasm = getWasmModule();
331
+ try {
332
+ if (Object.keys(this.options).length > 0) return wasm.parse_sql_with_options(this.dialect.name, sql, this.options);
333
+ else return wasm.parse_sql(this.dialect.name, sql);
334
+ } catch (error) {
335
+ throw ParserError.fromWasmError(error);
336
+ }
337
+ }
338
+ /**
339
+ * Parse SQL statements
340
+ *
341
+ * @param sql - SQL string to parse
342
+ * @param dialect - SQL dialect to use
343
+ * @returns Array of parsed statements
344
+ *
345
+ * @example
346
+ * ```typescript
347
+ * const statements = Parser.parse('SELECT 1', new GenericDialect());
348
+ * ```
349
+ */
350
+ static parse(sql, dialect = new GenericDialect()) {
351
+ return new Parser(dialect).parse(sql);
352
+ }
353
+ /**
354
+ * Parse SQL and return the AST as a JSON string
355
+ *
356
+ * @param sql - SQL string to parse
357
+ * @param dialect - SQL dialect to use
358
+ * @returns JSON string representation of the AST
359
+ */
360
+ static parseToJson(sql, dialect = new GenericDialect()) {
361
+ const wasm = getWasmModule();
362
+ try {
363
+ return wasm.parse_sql_to_json_string(dialect.name, sql);
364
+ } catch (error) {
365
+ throw ParserError.fromWasmError(error);
366
+ }
367
+ }
368
+ /**
369
+ * Parse SQL and return a formatted string representation
370
+ *
371
+ * @param sql - SQL string to parse
372
+ * @param dialect - SQL dialect to use
373
+ * @returns String representation of the parsed SQL
374
+ */
375
+ static parseToString(sql, dialect = new GenericDialect()) {
376
+ const wasm = getWasmModule();
377
+ try {
378
+ return wasm.parse_sql_to_string(dialect.name, sql);
379
+ } catch (error) {
380
+ throw ParserError.fromWasmError(error);
381
+ }
382
+ }
383
+ /**
384
+ * Format SQL by parsing and regenerating it (round-trip)
385
+ *
386
+ * @param sql - SQL string to format
387
+ * @param dialect - SQL dialect to use
388
+ * @returns Formatted SQL string
389
+ */
390
+ static format(sql, dialect = new GenericDialect()) {
391
+ const wasm = getWasmModule();
392
+ try {
393
+ return wasm.format_sql(dialect.name, sql);
394
+ } catch (error) {
395
+ throw ParserError.fromWasmError(error);
396
+ }
397
+ }
398
+ /**
399
+ * Validate SQL syntax without returning the full AST
400
+ *
401
+ * @param sql - SQL string to validate
402
+ * @param dialect - SQL dialect to use
403
+ * @returns true if valid, throws ParserError if invalid
404
+ */
405
+ static validate(sql, dialect = new GenericDialect()) {
406
+ const wasm = getWasmModule();
407
+ try {
408
+ return wasm.validate_sql(dialect.name, sql);
409
+ } catch (error) {
410
+ throw ParserError.fromWasmError(error);
411
+ }
412
+ }
413
+ /**
414
+ * Get the list of supported dialect names
415
+ */
416
+ static getSupportedDialects() {
417
+ return getWasmModule().get_supported_dialects();
418
+ }
419
+ };
420
+
421
+ //#endregion
422
+ exports.AnsiDialect = AnsiDialect;
423
+ exports.BigQueryDialect = BigQueryDialect;
424
+ exports.ClickHouseDialect = ClickHouseDialect;
425
+ exports.DatabricksDialect = DatabricksDialect;
426
+ exports.DuckDbDialect = DuckDbDialect;
427
+ exports.GenericDialect = GenericDialect;
428
+ exports.HiveDialect = HiveDialect;
429
+ exports.MsSqlDialect = MsSqlDialect;
430
+ exports.MySqlDialect = MySqlDialect;
431
+ exports.OracleDialect = OracleDialect;
432
+ exports.Parser = Parser;
433
+ exports.ParserError = ParserError;
434
+ exports.PostgreSqlDialect = PostgreSqlDialect;
435
+ exports.RedshiftDialect = RedshiftDialect;
436
+ exports.SQLiteDialect = SQLiteDialect;
437
+ exports.SUPPORTED_DIALECTS = SUPPORTED_DIALECTS;
438
+ exports.SnowflakeDialect = SnowflakeDialect;
439
+ exports.WasmInitError = WasmInitError;
440
+ exports.dialectFromString = dialectFromString;
441
+ exports.initWasm = initWasm;