@pineliner/odb-client 1.0.2 → 1.0.4

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 custom parser that handles BEGIN...END blocks
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":"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;AAoFD;;GAEG;AACH,wBAAgB,kBAAkB,CAAC,UAAU,EAAE,MAAM,GAAG,MAAM,EAAE,CAG/D"}
package/dist/index.cjs CHANGED
@@ -1443,10 +1443,10 @@ const external_libsql_client_namespaceObject = require("@libsql/client");
1443
1443
  }
1444
1444
  /**
1445
1445
  * Parse SQL file into statements, separating PRAGMA from regular SQL
1446
- * Handles comments, quotes, and semicolons properly
1446
+ * Uses custom parser that handles BEGIN...END blocks
1447
1447
  */ function parseSQL(sqlContent, options = {}) {
1448
1448
  const separatePragma = options.separatePragma ?? true;
1449
- // Split by semicolons (node-sql-parser doesn't handle multiple statements well)
1449
+ // Split SQL statements manually
1450
1450
  const statements = splitStatements(sqlContent);
1451
1451
  if (!separatePragma) return {
1452
1452
  pragmaStatements: [],
@@ -1466,15 +1466,27 @@ const external_libsql_client_namespaceObject = require("@libsql/client");
1466
1466
  }
1467
1467
  /**
1468
1468
  * Split SQL content into individual statements
1469
- * Handles comments, quotes, and semicolons properly
1469
+ * Handles comments, quotes, semicolons, and BEGIN...END blocks properly
1470
1470
  */ function splitStatements(sqlContent) {
1471
1471
  const statements = [];
1472
1472
  let currentStatement = '';
1473
1473
  let inQuote = false;
1474
1474
  let quoteChar = '';
1475
+ let beginEndDepth = 0;
1475
1476
  const lines = sqlContent.split('\n');
1476
1477
  for (const line of lines){
1477
1478
  let processedLine = '';
1479
+ const lineWithoutComments = line.replace(/--.*$/, '');
1480
+ // Track BEGIN...END depth for this line BEFORE processing characters
1481
+ if (!inQuote && lineWithoutComments.trim()) {
1482
+ const beginMatches = lineWithoutComments.match(/\bBEGIN\b/gi);
1483
+ const endMatches = lineWithoutComments.match(/\bEND\b/gi);
1484
+ if (beginMatches) beginEndDepth += beginMatches.length;
1485
+ if (endMatches) {
1486
+ beginEndDepth -= endMatches.length;
1487
+ if (beginEndDepth < 0) beginEndDepth = 0;
1488
+ }
1489
+ }
1478
1490
  for(let i = 0; i < line.length; i++){
1479
1491
  const char = line[i];
1480
1492
  const prevChar = i > 0 ? line[i - 1] : '';
@@ -1494,11 +1506,11 @@ const external_libsql_client_namespaceObject = require("@libsql/client");
1494
1506
  }
1495
1507
  }
1496
1508
  processedLine += char;
1497
- // Split on semicolon when not in quotes
1498
- if (';' === char && !inQuote) {
1509
+ // Split on semicolon when not in quotes AND not inside BEGIN...END
1510
+ if (';' === char && !inQuote && 0 === beginEndDepth) {
1499
1511
  currentStatement += processedLine;
1500
1512
  const stmt = currentStatement.trim();
1501
- if (stmt) statements.push(stmt);
1513
+ if (stmt && !stmt.startsWith('--')) statements.push(stmt);
1502
1514
  currentStatement = '';
1503
1515
  processedLine = '';
1504
1516
  }
@@ -1507,7 +1519,7 @@ const external_libsql_client_namespaceObject = require("@libsql/client");
1507
1519
  }
1508
1520
  // Handle remaining statement
1509
1521
  const finalStmt = currentStatement.trim();
1510
- if (finalStmt) statements.push(finalStmt);
1522
+ if (finalStmt && !finalStmt.startsWith('--')) statements.push(finalStmt);
1511
1523
  return statements;
1512
1524
  }
1513
1525
  /**
package/dist/index.js CHANGED
@@ -1368,10 +1368,10 @@ ODBLiteClient.join;
1368
1368
  }
1369
1369
  /**
1370
1370
  * Parse SQL file into statements, separating PRAGMA from regular SQL
1371
- * Handles comments, quotes, and semicolons properly
1371
+ * Uses custom parser that handles BEGIN...END blocks
1372
1372
  */ function parseSQL(sqlContent, options = {}) {
1373
1373
  const separatePragma = options.separatePragma ?? true;
1374
- // Split by semicolons (node-sql-parser doesn't handle multiple statements well)
1374
+ // Split SQL statements manually
1375
1375
  const statements = splitStatements(sqlContent);
1376
1376
  if (!separatePragma) return {
1377
1377
  pragmaStatements: [],
@@ -1391,15 +1391,27 @@ ODBLiteClient.join;
1391
1391
  }
1392
1392
  /**
1393
1393
  * Split SQL content into individual statements
1394
- * Handles comments, quotes, and semicolons properly
1394
+ * Handles comments, quotes, semicolons, and BEGIN...END blocks properly
1395
1395
  */ function splitStatements(sqlContent) {
1396
1396
  const statements = [];
1397
1397
  let currentStatement = '';
1398
1398
  let inQuote = false;
1399
1399
  let quoteChar = '';
1400
+ let beginEndDepth = 0;
1400
1401
  const lines = sqlContent.split('\n');
1401
1402
  for (const line of lines){
1402
1403
  let processedLine = '';
1404
+ const lineWithoutComments = line.replace(/--.*$/, '');
1405
+ // Track BEGIN...END depth for this line BEFORE processing characters
1406
+ if (!inQuote && lineWithoutComments.trim()) {
1407
+ const beginMatches = lineWithoutComments.match(/\bBEGIN\b/gi);
1408
+ const endMatches = lineWithoutComments.match(/\bEND\b/gi);
1409
+ if (beginMatches) beginEndDepth += beginMatches.length;
1410
+ if (endMatches) {
1411
+ beginEndDepth -= endMatches.length;
1412
+ if (beginEndDepth < 0) beginEndDepth = 0;
1413
+ }
1414
+ }
1403
1415
  for(let i = 0; i < line.length; i++){
1404
1416
  const char = line[i];
1405
1417
  const prevChar = i > 0 ? line[i - 1] : '';
@@ -1419,11 +1431,11 @@ ODBLiteClient.join;
1419
1431
  }
1420
1432
  }
1421
1433
  processedLine += char;
1422
- // Split on semicolon when not in quotes
1423
- if (';' === char && !inQuote) {
1434
+ // Split on semicolon when not in quotes AND not inside BEGIN...END
1435
+ if (';' === char && !inQuote && 0 === beginEndDepth) {
1424
1436
  currentStatement += processedLine;
1425
1437
  const stmt = currentStatement.trim();
1426
- if (stmt) statements.push(stmt);
1438
+ if (stmt && !stmt.startsWith('--')) statements.push(stmt);
1427
1439
  currentStatement = '';
1428
1440
  processedLine = '';
1429
1441
  }
@@ -1432,7 +1444,7 @@ ODBLiteClient.join;
1432
1444
  }
1433
1445
  // Handle remaining statement
1434
1446
  const finalStmt = currentStatement.trim();
1435
- if (finalStmt) statements.push(finalStmt);
1447
+ if (finalStmt && !finalStmt.startsWith('--')) statements.push(finalStmt);
1436
1448
  return statements;
1437
1449
  }
1438
1450
  /**
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.4",
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",
@@ -9,12 +9,12 @@ export interface SQLParserOptions {
9
9
 
10
10
  /**
11
11
  * Parse SQL file into statements, separating PRAGMA from regular SQL
12
- * Handles comments, quotes, and semicolons properly
12
+ * Uses custom parser that handles BEGIN...END blocks
13
13
  */
14
14
  export function parseSQL(sqlContent: string, options: SQLParserOptions = {}): ParsedStatements {
15
15
  const separatePragma = options.separatePragma ?? true
16
16
 
17
- // Split by semicolons (node-sql-parser doesn't handle multiple statements well)
17
+ // Split SQL statements manually
18
18
  const statements = splitStatements(sqlContent)
19
19
 
20
20
  if (!separatePragma) {
@@ -41,18 +41,36 @@ export function parseSQL(sqlContent: string, options: SQLParserOptions = {}): Pa
41
41
 
42
42
  /**
43
43
  * Split SQL content into individual statements
44
- * Handles comments, quotes, and semicolons properly
44
+ * Handles comments, quotes, semicolons, and BEGIN...END blocks properly
45
45
  */
46
46
  function splitStatements(sqlContent: string): string[] {
47
47
  const statements: string[] = []
48
48
  let currentStatement = ''
49
49
  let inQuote = false
50
50
  let quoteChar = ''
51
+ let beginEndDepth = 0
51
52
 
52
53
  const lines = sqlContent.split('\n')
53
54
 
54
55
  for (const line of lines) {
55
56
  let processedLine = ''
57
+ const lineWithoutComments = line.replace(/--.*$/, '')
58
+
59
+ // Track BEGIN...END depth for this line BEFORE processing characters
60
+ if (!inQuote && lineWithoutComments.trim()) {
61
+ const beginMatches = lineWithoutComments.match(/\bBEGIN\b/gi)
62
+ const endMatches = lineWithoutComments.match(/\bEND\b/gi)
63
+
64
+ if (beginMatches) {
65
+ beginEndDepth += beginMatches.length
66
+ }
67
+ if (endMatches) {
68
+ beginEndDepth -= endMatches.length
69
+ if (beginEndDepth < 0) {
70
+ beginEndDepth = 0
71
+ }
72
+ }
73
+ }
56
74
 
57
75
  for (let i = 0; i < line.length; i++) {
58
76
  const char = line[i]
@@ -77,11 +95,11 @@ function splitStatements(sqlContent: string): string[] {
77
95
 
78
96
  processedLine += char
79
97
 
80
- // Split on semicolon when not in quotes
81
- if (char === ';' && !inQuote) {
98
+ // Split on semicolon when not in quotes AND not inside BEGIN...END
99
+ if (char === ';' && !inQuote && beginEndDepth === 0) {
82
100
  currentStatement += processedLine
83
101
  const stmt = currentStatement.trim()
84
- if (stmt) {
102
+ if (stmt && !stmt.startsWith('--')) {
85
103
  statements.push(stmt)
86
104
  }
87
105
  currentStatement = ''
@@ -96,7 +114,7 @@ function splitStatements(sqlContent: string): string[] {
96
114
 
97
115
  // Handle remaining statement
98
116
  const finalStmt = currentStatement.trim()
99
- if (finalStmt) {
117
+ if (finalStmt && !finalStmt.startsWith('--')) {
100
118
  statements.push(finalStmt)
101
119
  }
102
120