@pineliner/odb-client 1.0.2 → 1.0.3

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.
@@ -7,7 +7,7 @@ export interface SQLParserOptions {
7
7
  }
8
8
  /**
9
9
  * Parse SQL file into statements, separating PRAGMA from regular SQL
10
- * Handles comments, quotes, and semicolons properly
10
+ * Uses node-sql-parser for robust SQL parsing
11
11
  */
12
12
  export declare function parseSQL(sqlContent: string, options?: SQLParserOptions): ParsedStatements;
13
13
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"sql-parser.d.ts","sourceRoot":"","sources":["../../src/database/sql-parser.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,gBAAgB;IAC/B,gBAAgB,EAAE,MAAM,EAAE,CAAA;IAC1B,iBAAiB,EAAE,MAAM,EAAE,CAAA;CAC5B;AAED,MAAM,WAAW,gBAAgB;IAC/B,cAAc,CAAC,EAAE,OAAO,CAAA;CACzB;AAED;;;GAGG;AACH,wBAAgB,QAAQ,CAAC,UAAU,EAAE,MAAM,EAAE,OAAO,GAAE,gBAAqB,GAAG,gBAAgB,CA0B7F;AAkED;;GAEG;AACH,wBAAgB,kBAAkB,CAAC,UAAU,EAAE,MAAM,GAAG,MAAM,EAAE,CAG/D"}
1
+ {"version":3,"file":"sql-parser.d.ts","sourceRoot":"","sources":["../../src/database/sql-parser.ts"],"names":[],"mappings":"AAEA,MAAM,WAAW,gBAAgB;IAC/B,gBAAgB,EAAE,MAAM,EAAE,CAAA;IAC1B,iBAAiB,EAAE,MAAM,EAAE,CAAA;CAC5B;AAED,MAAM,WAAW,gBAAgB;IAC/B,cAAc,CAAC,EAAE,OAAO,CAAA;CACzB;AAED;;;GAGG;AACH,wBAAgB,QAAQ,CAAC,UAAU,EAAE,MAAM,EAAE,OAAO,GAAE,gBAAqB,GAAG,gBAAgB,CA0B7F;AAwHD;;GAEG;AACH,wBAAgB,kBAAkB,CAAC,UAAU,EAAE,MAAM,GAAG,MAAM,EAAE,CAG/D"}
package/dist/index.cjs CHANGED
@@ -1441,13 +1441,14 @@ const external_libsql_client_namespaceObject = require("@libsql/client");
1441
1441
  // No explicit close needed
1442
1442
  }
1443
1443
  }
1444
+ const external_node_sql_parser_namespaceObject = require("node-sql-parser");
1444
1445
  /**
1445
1446
  * Parse SQL file into statements, separating PRAGMA from regular SQL
1446
- * Handles comments, quotes, and semicolons properly
1447
+ * Uses node-sql-parser for robust SQL parsing
1447
1448
  */ function parseSQL(sqlContent, options = {}) {
1448
1449
  const separatePragma = options.separatePragma ?? true;
1449
- // Split by semicolons (node-sql-parser doesn't handle multiple statements well)
1450
- const statements = splitStatements(sqlContent);
1450
+ // Use node-sql-parser to properly split SQL statements
1451
+ const statements = splitStatementsWithParser(sqlContent);
1451
1452
  if (!separatePragma) return {
1452
1453
  pragmaStatements: [],
1453
1454
  regularStatements: statements
@@ -1464,6 +1465,43 @@ const external_libsql_client_namespaceObject = require("@libsql/client");
1464
1465
  regularStatements
1465
1466
  };
1466
1467
  }
1468
+ /**
1469
+ * Split SQL content using node-sql-parser (more robust than custom parser)
1470
+ * Falls back to simple split if parser fails
1471
+ */ function splitStatementsWithParser(sqlContent) {
1472
+ try {
1473
+ const parser = new external_node_sql_parser_namespaceObject.Parser();
1474
+ // node-sql-parser supports splitting multiple statements
1475
+ // Try to use it, but have fallback for edge cases
1476
+ const opt = {
1477
+ database: 'sqlite',
1478
+ type: 'table'
1479
+ };
1480
+ // Split by semicolons first to handle parser limitations with very large files
1481
+ const roughStatements = sqlContent.split(/;[\s\n]*/).map((s)=>s.trim()).filter((s)=>s.length > 0);
1482
+ const validStatements = [];
1483
+ for (const stmt of roughStatements){
1484
+ // Add semicolon back
1485
+ const fullStmt = stmt + ';';
1486
+ // Skip comments-only statements
1487
+ if (!stmt.trim().startsWith('--') && '' !== stmt.trim()) // Validate with parser (but don't fail if it can't parse complex statements)
1488
+ try {
1489
+ parser.astify(fullStmt, opt);
1490
+ validStatements.push(fullStmt);
1491
+ } catch (parseError) {
1492
+ // If parser can't handle it, include it anyway if it looks like valid SQL
1493
+ // This handles complex statements like CREATE TRIGGER, CREATE VIEW with subqueries
1494
+ if (stmt.match(/^(CREATE|ALTER|DROP|INSERT|UPDATE|DELETE|SELECT|PRAGMA)/i)) validStatements.push(fullStmt);
1495
+ else console.warn(`Skipping potentially invalid statement: ${stmt.substring(0, 50)}...`);
1496
+ }
1497
+ }
1498
+ return validStatements;
1499
+ } catch (error) {
1500
+ console.warn('node-sql-parser failed, using fallback parser:', error);
1501
+ // Fallback to original simple parser
1502
+ return splitStatements(sqlContent);
1503
+ }
1504
+ }
1467
1505
  /**
1468
1506
  * Split SQL content into individual statements
1469
1507
  * Handles comments, quotes, and semicolons properly
package/dist/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import * as __WEBPACK_EXTERNAL_MODULE_bun_sqlite__ from "bun:sqlite";
2
2
  import * as __WEBPACK_EXTERNAL_MODULE__libsql_client__ from "@libsql/client";
3
+ import * as __WEBPACK_EXTERNAL_MODULE_node_sql_parser__ from "node-sql-parser";
3
4
  import * as __WEBPACK_EXTERNAL_MODULE_node_fs__ from "node:fs";
4
5
  // Core types for the ODBLite client
5
6
  // Error types
@@ -1368,11 +1369,11 @@ ODBLiteClient.join;
1368
1369
  }
1369
1370
  /**
1370
1371
  * Parse SQL file into statements, separating PRAGMA from regular SQL
1371
- * Handles comments, quotes, and semicolons properly
1372
+ * Uses node-sql-parser for robust SQL parsing
1372
1373
  */ function parseSQL(sqlContent, options = {}) {
1373
1374
  const separatePragma = options.separatePragma ?? true;
1374
- // Split by semicolons (node-sql-parser doesn't handle multiple statements well)
1375
- const statements = splitStatements(sqlContent);
1375
+ // Use node-sql-parser to properly split SQL statements
1376
+ const statements = splitStatementsWithParser(sqlContent);
1376
1377
  if (!separatePragma) return {
1377
1378
  pragmaStatements: [],
1378
1379
  regularStatements: statements
@@ -1389,6 +1390,43 @@ ODBLiteClient.join;
1389
1390
  regularStatements
1390
1391
  };
1391
1392
  }
1393
+ /**
1394
+ * Split SQL content using node-sql-parser (more robust than custom parser)
1395
+ * Falls back to simple split if parser fails
1396
+ */ function splitStatementsWithParser(sqlContent) {
1397
+ try {
1398
+ const parser = new __WEBPACK_EXTERNAL_MODULE_node_sql_parser__.Parser();
1399
+ // node-sql-parser supports splitting multiple statements
1400
+ // Try to use it, but have fallback for edge cases
1401
+ const opt = {
1402
+ database: 'sqlite',
1403
+ type: 'table'
1404
+ };
1405
+ // Split by semicolons first to handle parser limitations with very large files
1406
+ const roughStatements = sqlContent.split(/;[\s\n]*/).map((s)=>s.trim()).filter((s)=>s.length > 0);
1407
+ const validStatements = [];
1408
+ for (const stmt of roughStatements){
1409
+ // Add semicolon back
1410
+ const fullStmt = stmt + ';';
1411
+ // Skip comments-only statements
1412
+ if (!stmt.trim().startsWith('--') && '' !== stmt.trim()) // Validate with parser (but don't fail if it can't parse complex statements)
1413
+ try {
1414
+ parser.astify(fullStmt, opt);
1415
+ validStatements.push(fullStmt);
1416
+ } catch (parseError) {
1417
+ // If parser can't handle it, include it anyway if it looks like valid SQL
1418
+ // This handles complex statements like CREATE TRIGGER, CREATE VIEW with subqueries
1419
+ if (stmt.match(/^(CREATE|ALTER|DROP|INSERT|UPDATE|DELETE|SELECT|PRAGMA)/i)) validStatements.push(fullStmt);
1420
+ else console.warn(`Skipping potentially invalid statement: ${stmt.substring(0, 50)}...`);
1421
+ }
1422
+ }
1423
+ return validStatements;
1424
+ } catch (error) {
1425
+ console.warn('node-sql-parser failed, using fallback parser:', error);
1426
+ // Fallback to original simple parser
1427
+ return splitStatements(sqlContent);
1428
+ }
1429
+ }
1392
1430
  /**
1393
1431
  * Split SQL content into individual statements
1394
1432
  * Handles comments, quotes, and semicolons properly
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pineliner/odb-client",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "description": "Isomorphic client for ODB-Lite with postgres.js-like template string SQL support",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.js",
@@ -1,3 +1,5 @@
1
+ import { Parser } from 'node-sql-parser'
2
+
1
3
  export interface ParsedStatements {
2
4
  pragmaStatements: string[]
3
5
  regularStatements: string[]
@@ -9,13 +11,13 @@ export interface SQLParserOptions {
9
11
 
10
12
  /**
11
13
  * Parse SQL file into statements, separating PRAGMA from regular SQL
12
- * Handles comments, quotes, and semicolons properly
14
+ * Uses node-sql-parser for robust SQL parsing
13
15
  */
14
16
  export function parseSQL(sqlContent: string, options: SQLParserOptions = {}): ParsedStatements {
15
17
  const separatePragma = options.separatePragma ?? true
16
18
 
17
- // Split by semicolons (node-sql-parser doesn't handle multiple statements well)
18
- const statements = splitStatements(sqlContent)
19
+ // Use node-sql-parser to properly split SQL statements
20
+ const statements = splitStatementsWithParser(sqlContent)
19
21
 
20
22
  if (!separatePragma) {
21
23
  return {
@@ -39,6 +41,60 @@ export function parseSQL(sqlContent: string, options: SQLParserOptions = {}): Pa
39
41
  return { pragmaStatements, regularStatements }
40
42
  }
41
43
 
44
+ /**
45
+ * Split SQL content using node-sql-parser (more robust than custom parser)
46
+ * Falls back to simple split if parser fails
47
+ */
48
+ function splitStatementsWithParser(sqlContent: string): string[] {
49
+ try {
50
+ const parser = new Parser()
51
+
52
+ // node-sql-parser supports splitting multiple statements
53
+ // Try to use it, but have fallback for edge cases
54
+ const opt = {
55
+ database: 'sqlite',
56
+ type: 'table' as any
57
+ }
58
+
59
+ // Split by semicolons first to handle parser limitations with very large files
60
+ const roughStatements = sqlContent.split(/;[\s\n]*/)
61
+ .map(s => s.trim())
62
+ .filter(s => s.length > 0)
63
+
64
+ const validStatements: string[] = []
65
+
66
+ for (const stmt of roughStatements) {
67
+ // Add semicolon back
68
+ const fullStmt = stmt + ';'
69
+
70
+ // Skip comments-only statements
71
+ if (stmt.trim().startsWith('--') || stmt.trim() === '') {
72
+ continue
73
+ }
74
+
75
+ // Validate with parser (but don't fail if it can't parse complex statements)
76
+ try {
77
+ parser.astify(fullStmt, opt)
78
+ validStatements.push(fullStmt)
79
+ } catch (parseError) {
80
+ // If parser can't handle it, include it anyway if it looks like valid SQL
81
+ // This handles complex statements like CREATE TRIGGER, CREATE VIEW with subqueries
82
+ if (stmt.match(/^(CREATE|ALTER|DROP|INSERT|UPDATE|DELETE|SELECT|PRAGMA)/i)) {
83
+ validStatements.push(fullStmt)
84
+ } else {
85
+ console.warn(`Skipping potentially invalid statement: ${stmt.substring(0, 50)}...`)
86
+ }
87
+ }
88
+ }
89
+
90
+ return validStatements
91
+ } catch (error) {
92
+ console.warn('node-sql-parser failed, using fallback parser:', error)
93
+ // Fallback to original simple parser
94
+ return splitStatements(sqlContent)
95
+ }
96
+ }
97
+
42
98
  /**
43
99
  * Split SQL content into individual statements
44
100
  * Handles comments, quotes, and semicolons properly