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