jsql-neo 6.5.29 → 6.5.30

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 CHANGED
@@ -3,7 +3,7 @@
3
3
  > **One engine to rule them all** — a Rust-powered embedded database that speaks your language:
4
4
  > MySQL, PostgreSQL, MongoDB, Redis, SQL, TypeScript, **and the browser**.
5
5
 
6
- > **v6.5.29** — stable release · [github.com/vexify-org/JSQL-neo](https://github.com/vexify-org/JSQL-neo) · [jsql.vexify.top](https://jsql.vexify.top)
6
+ > **v6.5.30** — stable release · [github.com/vexify-org/JSQL-neo](https://github.com/vexify-org/JSQL-neo) · [jsql.vexify.top](https://jsql.vexify.top)
7
7
 
8
8
  ![Engines](https://img.shields.io/badge/engines-Native%20%7C%20WASM%20%7C%20Pure%20JS-7ee787)
9
9
  ![MySQL](https://img.shields.io/badge/protocol-MySQL%20compatible-1f6feb)
package/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /**
2
- * JSQL-NEO v6.5.29 — Rust-Powered Embedded Database (WASM + HTTP)
2
+ * JSQL-NEO v6.5.30 — Rust-Powered Embedded Database (WASM + HTTP)
3
3
  *
4
4
  * @example
5
5
  * const jsql = require('jsql-neo');
package/lib/sql.js CHANGED
@@ -534,6 +534,24 @@ class Parser {
534
534
  parseStatement() {
535
535
  const t = this.peek();
536
536
  if (t.type === 'eof') return null;
537
+ // 括号化的查询表达式(#60):(SELECT ...) UNION (SELECT ...)。
538
+ // 解析完括号内的 SELECT 后,括号外仍可能有集合运算符/ORDER BY/LIMIT。
539
+ if (t.type === 'op' && t.value === '(' && this.isKeyword('SELECT', 1)) {
540
+ this.next();
541
+ const stmt = this.parseSelect();
542
+ this.expect('op', ')');
543
+ // 括号内 SELECT 自身的 ORDER BY/LIMIT/OFFSET 属于该操作数,
544
+ // 不能作为整个组合查询的尾部子句(否则会作用到 union 后的结果)。
545
+ if (this.isKeyword('UNION') || this.isKeyword('INTERSECT') || this.isKeyword('EXCEPT')) {
546
+ stmt._operandTail = { orderBy: stmt.orderBy, limit: stmt.limit, offset: stmt.offset };
547
+ stmt.orderBy = null;
548
+ stmt.limit = null;
549
+ stmt.offset = 0;
550
+ }
551
+ this.parseSetOps(stmt);
552
+ this.optionalTailSemicolon();
553
+ return stmt;
554
+ }
537
555
  if (t.type !== 'keyword') {
538
556
  // 少数语句起始词刻意不进关键字表(它们同时是常见标识符),按上下文识别
539
557
  const w = String(t.value).toUpperCase();
@@ -1564,53 +1582,70 @@ class Parser {
1564
1582
  }
1565
1583
  let having = null;
1566
1584
  if (this.isKeyword('HAVING')) { this.next(); having = this.parseBooleanExpr(); }
1567
- let orderBy = null;
1568
- if (this.isKeyword('ORDER')) {
1569
- this.expectKeyword('ORDER'); this.expectKeyword('BY');
1570
- orderBy = [];
1571
- while (true) {
1572
- // 支持表达式排序:ORDER BY a + b DESC、ORDER BY UPPER(name)
1573
- // 纯列名仍走 parseColumnRef,保持既有字符串形态不破坏下游
1574
- let col;
1575
- const save = this.pos;
1576
- // ORDER BY 项的合法结束符;不是这些就说明是表达式(如 a+b、UPPER(name))
1577
- const ORDER_END = ['ASC', 'DESC', 'LIMIT', 'OFFSET', 'FETCH', 'UNION', 'INTERSECT',
1578
- 'EXCEPT', 'RETURNING', 'FOR', 'WITH'];
1579
- const endsClause = (tk) => {
1580
- if (tk.type === 'eof') return true;
1581
- if (tk.type === 'op' && (tk.value === ',' || tk.value === ';' || tk.value === ')')) return true;
1582
- return ORDER_END.some(w => this.isWord(w));
1583
- };
1584
- try {
1585
- col = this.parseColumnRef();
1586
- if (!endsClause(this.peek())) {
1587
- this.pos = save;
1588
- col = this.parseScalar();
1589
- }
1590
- } catch (e) {
1585
+ const orderBy = this.parseOrderByClause();
1586
+ const lim = this.parseLimitOffset();
1587
+ const limit = lim.limit;
1588
+ const offset = lim.offset;
1589
+ const node = { type: 'select', columns, aggregate, distinct, from, where, groupBy, having, orderBy, limit, offset, union: null, intersect: null, except: null, rollup };
1590
+ this.parseSetOps(node);
1591
+ this.optionalTailSemicolon();
1592
+ this.depth--;
1593
+
1594
+ return node;
1595
+ }
1596
+
1597
+ /** ORDER BY 子句(可选)。返回 null 或 [{column, dir}]。 */
1598
+ parseOrderByClause() {
1599
+ if (!this.isKeyword('ORDER')) return null;
1600
+ this.expectKeyword('ORDER'); this.expectKeyword('BY');
1601
+ const orderBy = [];
1602
+ while (true) {
1603
+ // 支持表达式排序:ORDER BY a + b DESC、ORDER BY UPPER(name)
1604
+ // 纯列名仍走 parseColumnRef,保持既有字符串形态不破坏下游
1605
+ let col;
1606
+ const save = this.pos;
1607
+ // ORDER BY 项的合法结束符;不是这些就说明是表达式(如 a+b、UPPER(name))
1608
+ const ORDER_END = ['ASC', 'DESC', 'LIMIT', 'OFFSET', 'FETCH', 'UNION', 'INTERSECT',
1609
+ 'EXCEPT', 'RETURNING', 'FOR', 'WITH'];
1610
+ const endsClause = (tk) => {
1611
+ if (tk.type === 'eof') return true;
1612
+ if (tk.type === 'op' && (tk.value === ',' || tk.value === ';' || tk.value === ')')) return true;
1613
+ return ORDER_END.some(w => this.isWord(w));
1614
+ };
1615
+ try {
1616
+ col = this.parseColumnRef();
1617
+ if (!endsClause(this.peek())) {
1591
1618
  this.pos = save;
1592
- // ORDER BY 可以引用输出列别名,而别名可能与聚合函数同名:
1593
- // SELECT AVG(v) AS avg FROM t GROUP BY g ORDER BY avg DESC
1594
- // 这种 token 已被词法器标成 keyword(AVG/SUM/COUNT/MAX/MIN 都在关键字表里),
1595
- // parseColumnRef 只认 ident 会失败,parseScalar 又会把它当函数调用 → "got 'AVG'"。
1596
- // 此处把裸 keyword 当作列名,与 SQLite / MySQL 的行为一致。
1597
- const tk = this.peek();
1598
- if (tk.type === 'keyword' && !this.isWord('ASC') && !this.isWord('DESC')
1599
- && !ORDER_END.some(w => this.isWord(w))) {
1600
- this.next();
1601
- col = tk.value;
1602
- } else {
1603
- col = this.parseScalar();
1604
- }
1619
+ col = this.parseScalar();
1620
+ }
1621
+ } catch (e) {
1622
+ this.pos = save;
1623
+ // ORDER BY 可以引用输出列别名,而别名可能与聚合函数同名:
1624
+ // SELECT AVG(v) AS avg FROM t GROUP BY g ORDER BY avg DESC
1625
+ // 这种 token 已被词法器标成 keyword(AVG/SUM/COUNT/MAX/MIN 都在关键字表里),
1626
+ // parseColumnRef 只认 ident 会失败,parseScalar 又会把它当函数调用 → "got 'AVG'"。
1627
+ // 此处把裸 keyword 当作列名,与 SQLite / MySQL 的行为一致。
1628
+ const tk = this.peek();
1629
+ if (tk.type === 'keyword' && !this.isWord('ASC') && !this.isWord('DESC')
1630
+ && !ORDER_END.some(w => this.isWord(w))) {
1631
+ this.next();
1632
+ col = tk.value;
1633
+ } else {
1634
+ col = this.parseScalar();
1605
1635
  }
1606
- let dir = 'asc';
1607
- if (this.isKeyword('ASC')) { this.next(); }
1608
- else if (this.isKeyword('DESC')) { this.next(); dir = 'desc'; }
1609
- orderBy.push({ column: col, dir });
1610
- if (this.peek().type === 'op' && this.peek().value === ',') { this.next(); continue; }
1611
- break;
1612
1636
  }
1637
+ let dir = 'asc';
1638
+ if (this.isKeyword('ASC')) { this.next(); }
1639
+ else if (this.isKeyword('DESC')) { this.next(); dir = 'desc'; }
1640
+ orderBy.push({ column: col, dir });
1641
+ if (this.peek().type === 'op' && this.peek().value === ',') { this.next(); continue; }
1642
+ break;
1613
1643
  }
1644
+ return orderBy;
1645
+ }
1646
+
1647
+ /** LIMIT / OFFSET 子句(MySQL 语法,含 LIMIT a,b 与独立 OFFSET)。 */
1648
+ parseLimitOffset() {
1614
1649
  let limit = null, offset = 0;
1615
1650
  if (this.isKeyword('LIMIT')) {
1616
1651
  this.next();
@@ -1628,38 +1663,67 @@ class Parser {
1628
1663
  if (typeof offset === 'number' && offset < 0) throw new Error(`OFFSET must be a non-negative integer, got ${offset}`);
1629
1664
  limit = Number.MAX_SAFE_INTEGER;
1630
1665
  }
1631
- // 集合运算:UNION [ALL|DISTINCT] / INTERSECT [ALL|DISTINCT] / EXCEPT [ALL|DISTINCT]
1666
+ return { limit, offset };
1667
+ }
1668
+
1669
+ /**
1670
+ * 集合运算后缀(#60):UNION [ALL|DISTINCT] / INTERSECT / EXCEPT。
1671
+ * 右侧操作数允许括号化的 SELECT((SELECT ...))。
1672
+ * 标准 SQL 里组合查询尾部的 ORDER BY / LIMIT / OFFSET 作用于整个结果,
1673
+ * 语法上它们先被解析进最右侧 SELECT(#57),这里逐层提升到外层,
1674
+ * 否则执行层在 concat 后直接返回,尾部子句被静默丢弃。
1675
+ *
1676
+ * 单独成方法,供普通 SELECT 与括号化根节点 (SELECT ...) UNION ... 共用。
1677
+ */
1678
+ parseSetOps(statement) {
1632
1679
  let union = null, intersect = null, except = null;
1680
+ let rhsParenthesized = false;
1681
+ const rhsOperand = () => {
1682
+ const operand = this.parseSetOperand();
1683
+ rhsParenthesized = operand.parenthesized;
1684
+ return operand.select;
1685
+ };
1633
1686
  if (this.isKeyword('UNION')) {
1634
1687
  this.next();
1635
1688
  const all = this.isKeyword('ALL');
1636
1689
  if (all) this.next();
1637
1690
  else if (this.isKeyword('DISTINCT')) this.next();
1638
- union = { all, select: this.parseSelect() };
1691
+ union = { all, select: rhsOperand() };
1639
1692
  } else if (this.isKeyword('INTERSECT')) {
1640
1693
  this.next();
1641
1694
  const all = this.isKeyword('ALL');
1642
1695
  if (all) this.next();
1643
1696
  else if (this.isKeyword('DISTINCT')) this.next();
1644
- intersect = { all, select: this.parseSelect() };
1697
+ intersect = { all, select: rhsOperand() };
1645
1698
  } else if (this.isKeyword('EXCEPT')) {
1646
1699
  this.next();
1647
1700
  const all = this.isKeyword('ALL');
1648
1701
  if (all) this.next();
1649
1702
  else if (this.isKeyword('DISTINCT')) this.next();
1650
- except = { all, select: this.parseSelect() };
1651
- }
1652
- // 标准 SQL:组合查询尾部的 ORDER BY / LIMIT / OFFSET 作用于整个结果。
1653
- // 语法上它们会被解析进最右侧的 SELECT(#57),这里逐层提升到外层,
1654
- // 否则执行层在 concat 后直接返回,尾部子句被静默丢弃。
1655
- {
1703
+ except = { all, select: rhsOperand() };
1704
+ }
1705
+ statement.union = union;
1706
+ statement.intersect = intersect;
1707
+ statement.except = except;
1708
+ // 括号化操作数(#60)内部的 ORDER BY/LIMIT/OFFSET 属于它自己,不能提升;
1709
+ // 普通无括号的最右侧 SELECT 上挂的尾部子句才属于整个组合查询(#57)。
1710
+ if (!rhsParenthesized) {
1656
1711
  const rhsSel = (union || intersect || except || {}).select;
1657
1712
  if (rhsSel) {
1658
- if (rhsSel.orderBy) { orderBy = rhsSel.orderBy; rhsSel.orderBy = null; }
1659
- if (rhsSel.limit !== null && rhsSel.limit !== undefined) { limit = rhsSel.limit; rhsSel.limit = null; }
1660
- if (rhsSel.offset) { offset = rhsSel.offset; rhsSel.offset = 0; }
1713
+ if (rhsSel.orderBy) { statement.orderBy = rhsSel.orderBy; rhsSel.orderBy = null; }
1714
+ if (rhsSel.limit !== null && rhsSel.limit !== undefined) { statement.limit = rhsSel.limit; rhsSel.limit = null; }
1715
+ if (rhsSel.offset) { statement.offset = rhsSel.offset; rhsSel.offset = 0; }
1661
1716
  }
1662
1717
  }
1718
+ // 括号化操作数会把尾部 ORDER BY/LIMIT/OFFSET 留在括号外(#60):
1719
+ // 此时它们属于整个组合查询,在此直接解析。普通路径下这些子句已被
1720
+ // 最右侧 SELECT 消费并提升,不会重复解析。
1721
+ if (this.isKeyword('ORDER')) statement.orderBy = this.parseOrderByClause();
1722
+ if (this.isKeyword('LIMIT') || this.isKeyword('OFFSET')) {
1723
+ const tail = this.parseLimitOffset();
1724
+ statement.limit = tail.limit;
1725
+ statement.offset = tail.offset;
1726
+ }
1663
1727
  // FETCH FIRST n ROWS ONLY(SQL 标准的 LIMIT 写法)
1664
1728
  if (this.isWord('FETCH')) {
1665
1729
  this.next();
@@ -1669,14 +1733,26 @@ class Parser {
1669
1733
  let n = nTok.type === 'number' ? nTok.value : Number(nTok.value);
1670
1734
  if (typeof n === 'bigint') n = Number(n <= BigInt(Number.MAX_SAFE_INTEGER) ? n : BigInt(Number.MAX_SAFE_INTEGER));
1671
1735
  if (!Number.isFinite(n)) throw new Error(`Expected row count after FETCH FIRST, got '${nTok.value}'`);
1672
- limit = n;
1736
+ statement.limit = n;
1673
1737
  if (this.isWord('ROW') || this.isWord('ROWS')) this.next();
1674
1738
  if (this.isWord('ONLY')) this.next();
1675
1739
  }
1676
- this.optionalTailSemicolon();
1677
- this.depth--;
1740
+ return statement;
1741
+ }
1678
1742
 
1679
- return { type: 'select', columns, aggregate, distinct, from, where, groupBy, having, orderBy, limit, offset, union, intersect, except, rollup };
1743
+ /** 集合运算的操作数:普通 SELECT,或括号化的 SELECT(#60)。 */
1744
+ parseSetOperand() {
1745
+ if (this.peek().type === 'op' && this.peek().value === '(' && this.isKeyword('SELECT', 1)) {
1746
+ this.next();
1747
+ const inner = this.parseSelect();
1748
+ this.expect('op', ')');
1749
+ // A UNION (B) UNION C —— 括号后若仍有集合运算符,继续挂到该操作数上。
1750
+ if (this.isKeyword('UNION') || this.isKeyword('INTERSECT') || this.isKeyword('EXCEPT')) {
1751
+ this.parseSetOps(inner);
1752
+ }
1753
+ return { select: inner, parenthesized: true };
1754
+ }
1755
+ return { select: this.parseSelect(), parenthesized: false };
1680
1756
  }
1681
1757
 
1682
1758
  parseFrom() {
@@ -4702,6 +4778,22 @@ function dedupeRows(rows) {
4702
4778
  });
4703
4779
  }
4704
4780
 
4781
+ /**
4782
+ * 集合运算两侧列数必须一致(#60)。
4783
+ * MySQL/PostgreSQL 遇到列数不同的组合查询会直接报错;此前 jsql-neo 静默按左分支
4784
+ * 截断,第二个 SELECT 多出的列被丢弃 → 静默数据丢失。
4785
+ */
4786
+ function assertSetOpColumnCount(left, right) {
4787
+ const lc = (left && left.columns ? left.columns.length : 0);
4788
+ const rc = (right && right.columns ? right.columns.length : 0);
4789
+ if (lc !== rc) {
4790
+ const err = new Error('The used SELECT statements have a different number of columns');
4791
+ err.code = 'ER_WRONG_NUMBER_OF_COLUMNS_IN_SELECT';
4792
+ err.errno = 1222;
4793
+ throw err;
4794
+ }
4795
+ }
4796
+
4705
4797
  // 比较前把布尔归一化为 1/0,但只在“另一侧是数值/布尔”时做,
4706
4798
  // 避免把 true 与字符串 'true' 的比较悄悄改成 1 与 'true' 的比较。
4707
4799
  function normalizeCmpValue(a, other) {
@@ -7487,8 +7579,11 @@ class SQLExecutor {
7487
7579
 
7488
7580
  // UNION 处理
7489
7581
  if (statement.union) {
7490
- const left = await this.executeSelect({ ...statement, ...setOpBase, union: null });
7582
+ const leftStatement = { ...statement, ...setOpBase, union: null };
7583
+ if (statement._operandTail) Object.assign(leftStatement, statement._operandTail);
7584
+ const left = await this.executeSelect(leftStatement);
7491
7585
  const right = await this.executeSelect(statement.union.select);
7586
+ assertSetOpColumnCount(left, right);
7492
7587
  let rows = left.rows.concat(right.rows);
7493
7588
  if (!statement.union.all) rows = dedupeRows(rows);
7494
7589
  rows = this._finalizeSetOp(rows, left.columns, statement);
@@ -7497,8 +7592,11 @@ class SQLExecutor {
7497
7592
 
7498
7593
  // INTERSECT:保留同时出现在右侧结果中的行(默认 DISTINCT,与 UNION 的处理保持一致)
7499
7594
  if (statement.intersect) {
7500
- const left = await this.executeSelect({ ...statement, ...setOpBase, intersect: null, except: null });
7595
+ const leftStatement = { ...statement, ...setOpBase, intersect: null, except: null };
7596
+ if (statement._operandTail) Object.assign(leftStatement, statement._operandTail);
7597
+ const left = await this.executeSelect(leftStatement);
7501
7598
  const right = await this.executeSelect(statement.intersect.select);
7599
+ assertSetOpColumnCount(left, right);
7502
7600
  const seen = new Set(right.rows.map(r => JSON.stringify(r)));
7503
7601
  let out = left.rows.filter(r => seen.has(JSON.stringify(r)));
7504
7602
  if (!statement.intersect.all) out = dedupeRows(out);
@@ -7508,8 +7606,11 @@ class SQLExecutor {
7508
7606
 
7509
7607
  // EXCEPT:去掉出现在右侧结果中的行(默认 DISTINCT)
7510
7608
  if (statement.except) {
7511
- const left = await this.executeSelect({ ...statement, ...setOpBase, intersect: null, except: null });
7609
+ const leftStatement = { ...statement, ...setOpBase, intersect: null, except: null };
7610
+ if (statement._operandTail) Object.assign(leftStatement, statement._operandTail);
7611
+ const left = await this.executeSelect(leftStatement);
7512
7612
  const right = await this.executeSelect(statement.except.select);
7613
+ assertSetOpColumnCount(left, right);
7513
7614
  const seen = new Set(right.rows.map(r => JSON.stringify(r)));
7514
7615
  let out = left.rows.filter(r => !seen.has(JSON.stringify(r)));
7515
7616
  if (!statement.except.all) out = dedupeRows(out);
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jsql-neo",
3
- "version": "6.5.29",
3
+ "version": "6.5.30",
4
4
  "description": "JSQL-NEO — Rust-powered embedded database with WASM, REST API, B-Tree indexes, WAL, crash recovery",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -50,12 +50,12 @@
50
50
  "scripts": {
51
51
  "postinstall": "node postinstall.js",
52
52
  "build:native": "node scripts/build-native.js",
53
- "test": "node test/smoke.js && node test/result-raw.test.js && node test/native-install.test.js && node test/native-cli.test.js && node test/finalize-bigint.test.js && node test/native.test.js && node test/json-functions.test.js && node test/sql-parser.test.js && node test/readme-audit.test.js && node test/query-opt.test.js && node test/wal-recovery.test.js && node test/mysql-compat-hijack.test.js && node test/dml-constraints.test.js && node test/star-nag.test.js && node test/plugin-xql.test.js && node test/bug-regression-2.test.js && node test/issue-34-35-36.test.js && node test/issue-g4.test.js && node test/issue-38.test.js && node test/issue-39.test.js && node test/issue-40.test.js && node test/issue-44.test.js && node test/issue-45.test.js && node test/issue-46.test.js && node test/issue-47.test.js && node test/issue-48.test.js && node test/issue-50.test.js && node test/issue-51.test.js && node test/issue-52.test.js && node test/issue-53.test.js && node test/issue-54.test.js && node test/issue-55.test.js && node test/issue-56.test.js && node test/issue-57.test.js && node test/issue-58.test.js",
53
+ "test": "node test/smoke.js && node test/result-raw.test.js && node test/native-install.test.js && node test/native-cli.test.js && node test/finalize-bigint.test.js && node test/native.test.js && node test/json-functions.test.js && node test/sql-parser.test.js && node test/readme-audit.test.js && node test/query-opt.test.js && node test/wal-recovery.test.js && node test/mysql-compat-hijack.test.js && node test/dml-constraints.test.js && node test/star-nag.test.js && node test/plugin-xql.test.js && node test/bug-regression-2.test.js && node test/issue-34-35-36.test.js && node test/issue-g4.test.js && node test/issue-38.test.js && node test/issue-39.test.js && node test/issue-40.test.js && node test/issue-44.test.js && node test/issue-45.test.js && node test/issue-46.test.js && node test/issue-47.test.js && node test/issue-48.test.js && node test/issue-50.test.js && node test/issue-51.test.js && node test/issue-52.test.js && node test/issue-53.test.js && node test/issue-54.test.js && node test/issue-55.test.js && node test/issue-56.test.js && node test/issue-57.test.js && node test/issue-58.test.js && node test/issue-59.test.js && node test/issue-60.test.js",
54
54
  "test:parser": "node test/sql-parser.test.js",
55
55
  "test:btree": "node test/btree.test.js",
56
56
  "test:native": "node test/native.test.js",
57
57
  "test:regress": "node test/regress-5.1.0.js",
58
- "test:all": "node test/smoke.js && node test/result-raw.test.js && node test/native-install.test.js && node test/native-cli.test.js && node test/finalize-bigint.test.js && node test/sql-parser.test.js && node test/readme-audit.test.js && node test/native.test.js && node test/native-multi-instance.test.js && node test/wal-recovery.test.js && node test/mysql-compat-hijack.test.js && node test/wasm.test.js && node test/coverage.js && node test/regress-5.1.0.js && node test/join.test.js && node test/btree.test.js && node test/dml-constraints.test.js && node test/savepoint.test.js && node test/alter-table.test.js && node test/null-semantics.test.js && node test/identifier-keywords.test.js && node test/issue-34-35-36.test.js && node test/sql-semantics.test.js && node test/extra-coverage.test.js && node test/filter-compile.test.js && node test/recursive-cte.test.js && node test/index-ddl.test.js && node test/mongo-group.test.js && node test/multiprotocol.test.js && node test/issue-g4.test.js && node test/issue-38.test.js && node test/issue-39.test.js && node test/issue-40.test.js && node test/issue-44.test.js && node test/issue-45.test.js && node test/issue-46.test.js && node test/issue-47.test.js && node test/issue-48.test.js && node test/issue-50.test.js && node test/issue-51.test.js && node test/issue-52.test.js && node test/issue-53.test.js && node test/issue-54.test.js && node test/issue-55.test.js && node test/issue-56.test.js && node test/issue-57.test.js && node test/issue-58.test.js",
58
+ "test:all": "node test/smoke.js && node test/result-raw.test.js && node test/native-install.test.js && node test/native-cli.test.js && node test/finalize-bigint.test.js && node test/sql-parser.test.js && node test/readme-audit.test.js && node test/native.test.js && node test/native-multi-instance.test.js && node test/wal-recovery.test.js && node test/mysql-compat-hijack.test.js && node test/wasm.test.js && node test/coverage.js && node test/regress-5.1.0.js && node test/join.test.js && node test/btree.test.js && node test/dml-constraints.test.js && node test/savepoint.test.js && node test/alter-table.test.js && node test/null-semantics.test.js && node test/identifier-keywords.test.js && node test/issue-34-35-36.test.js && node test/sql-semantics.test.js && node test/extra-coverage.test.js && node test/filter-compile.test.js && node test/recursive-cte.test.js && node test/index-ddl.test.js && node test/mongo-group.test.js && node test/multiprotocol.test.js && node test/issue-g4.test.js && node test/issue-38.test.js && node test/issue-39.test.js && node test/issue-40.test.js && node test/issue-44.test.js && node test/issue-45.test.js && node test/issue-46.test.js && node test/issue-47.test.js && node test/issue-48.test.js && node test/issue-50.test.js && node test/issue-51.test.js && node test/issue-52.test.js && node test/issue-53.test.js && node test/issue-54.test.js && node test/issue-55.test.js && node test/issue-56.test.js && node test/issue-57.test.js && node test/issue-58.test.js && node test/issue-59.test.js && node test/issue-60.test.js",
59
59
  "test:coverage": "c8 --all --include='lib/**' --exclude='lib/web_ui.js' --reporter=text --reporter=lcov node test/coverage.js",
60
60
  "test:orms": "node examples/orms/run-all.js",
61
61
  "test:dml": "node test/dml-constraints.test.js",
@@ -75,6 +75,35 @@ function localStr(utcMs) {
75
75
  validateDateType('2024-01-15T10:30:00Z', 'datetime', 'dt'));
76
76
  ok('#50 validateDateType 非法日期仍报错',
77
77
  (() => { try { validateDateType('2024-13-45T10:30:00Z', 'datetime', 'dt'); return false; } catch (e) { return true; } })());
78
+
79
+ // 8) 更多等价写法:小写 z、紧凑偏移 ±HHMM、非整点时区、跨年、带空格的偏移
80
+ const isoCases = [
81
+ '2024-01-15T10:30:00z',
82
+ '2024-01-15T10:30:00-0500',
83
+ '2024-01-15T10:30:00+0530',
84
+ '2024-12-31T23:59:59Z',
85
+ '2024-02-29T00:00:00+09:00',
86
+ '2024-01-15 10:30:00+08:00',
87
+ ];
88
+ await q('CREATE TABLE tz (id INTEGER PRIMARY KEY, dt DATETIME)');
89
+ for (let i = 0; i < isoCases.length; i++) {
90
+ const s = isoCases[i];
91
+ await q(`INSERT INTO tz (id, dt) VALUES (${i + 1}, '${s}')`);
92
+ const got = (await q(`SELECT dt FROM tz WHERE id = ${i + 1}`)).rows[0][0];
93
+ const want = localStr(Date.parse(s));
94
+ ok(`#50 等价写法 ${s} 换算正确`, got === want, { got, want });
95
+ }
96
+
97
+ // 9) DATETIME 存储的是「瞬间」:跨日/跨年的 UTC 换算会改变日期部分
98
+ await q('CREATE TABLE crossyear (id INTEGER PRIMARY KEY, dt DATETIME)');
99
+ await q("INSERT INTO crossyear VALUES (1, '2024-01-01T00:30:00+09:00')");
100
+ r = await q('SELECT dt FROM crossyear WHERE id = 1');
101
+ // UTC+8 下应为 2023-12-31 23:30:00(跨回上一年)
102
+ const expectCross = localStr(Date.parse('2024-01-01T00:30:00+09:00'));
103
+ ok('#50 时区换算可跨年', r.rows[0][0] === expectCross, { got: r.rows[0][0], want: expectCross });
104
+ if (new Date().getTimezoneOffset() === -480) {
105
+ ok('#50 UTC+8 跨年具体值', r.rows[0][0] === '2023-12-31 23:30:00', r.rows[0][0]);
106
+ }
78
107
  })();
79
108
 
80
109
  setTimeout(() => {
@@ -51,6 +51,26 @@ const norm = (rows) => rows.map(r => r.map(v => (typeof v === 'bigint' ? Number(
51
51
  r = await q('SELECT a, b, COUNT(*) FROM g GROUP BY a, b');
52
52
  ok('#51 多列分组未被破坏', r.rows.length === 4, r.rows);
53
53
 
54
+ // 表达式与裸列混排(#51 扩展):每一项都能独立走表达式解析
55
+ r = await q('SELECT a%2 AS p, b, COUNT(*) FROM g GROUP BY a%2, b');
56
+ ok('#51 表达式 + 裸列混合分组', JSON.stringify(norm(r.rows)) === JSON.stringify([[0, 20, 1], [0, 40, 1], [1, 10, 1], [1, 30, 1]]), r.rows);
57
+
58
+ r = await q('SELECT b, a%2 AS p FROM g GROUP BY b, a%2');
59
+ ok('#51 裸列 + 表达式混合分组(表达式在后)', JSON.stringify(norm(r.rows)) === JSON.stringify([[10, 1], [20, 0], [30, 1], [40, 0]]), r.rows);
60
+
61
+ r = await q('SELECT a*2+1 AS x, COUNT(*) FROM g GROUP BY a*2+1');
62
+ ok('#51 多运算符表达式分组', JSON.stringify(norm(r.rows)) === JSON.stringify([[3, 1], [5, 1], [7, 1], [9, 1]]), r.rows);
63
+
64
+ // 字符串函数分组:结果真正的分组边界
65
+ await q('CREATE TABLE s (id INTEGER PRIMARY KEY, name TEXT)');
66
+ await q("INSERT INTO s VALUES (1,'ab'),(2,'cd'),(3,'ab'),(4,'ef'),(5,'cd')");
67
+ r = await q('SELECT LOWER(name) AS n, COUNT(*) FROM s GROUP BY LOWER(name)');
68
+ ok('#51 字符串函数表达式分组', JSON.stringify(norm(r.rows)) === JSON.stringify([['ab', 2], ['cd', 2], ['ef', 1]]), r.rows);
69
+
70
+ // ORDER BY 表达式仍然可用(回归保护)
71
+ r = await q('SELECT a FROM g ORDER BY a%2');
72
+ ok('#51 ORDER BY 表达式未被破坏', r.rows.length === 4, r.rows);
73
+
54
74
  console.log(`\n${fail === 0 ? 'ALL PASS' : 'FAILURES'} — ${pass} passed, ${fail} failed\n`);
55
75
  if (fail > 0) process.exit(1);
56
76
  })();
@@ -57,6 +57,28 @@ const j = (rows) => JSON.stringify(rows, (k, v) => (typeof v === 'bigint' ? v.to
57
57
  r = await q('SELECT CASE WHEN COUNT(*)>2 THEN 1 ELSE 0 END AS f FROM t HAVING CASE WHEN COUNT(*)>2 THEN 1 ELSE 0 END = 1');
58
58
  ok('#52 HAVING 里的 CASE 聚合', j(r.rows) === j([[1]]), r.rows);
59
59
 
60
+ // 聚合藏在函数参数里再进 CASE(#52 扩展)
61
+ r = await q('SELECT CASE WHEN ROUND(AVG(v),0) > 2 THEN COUNT(*) ELSE 0 END FROM t');
62
+ ok('#52 函数包裹的聚合在 CASE 条件里', j(r.rows) === j([[4]]), r.rows);
63
+
64
+ r = await q('SELECT CASE WHEN SUM(v)/COUNT(*) > 2 THEN 1 ELSE 0 END FROM t');
65
+ ok('#52 CASE 条件里的聚合算术表达式', j(r.rows) === j([[1]]), r.rows);
66
+
67
+ r = await q('SELECT CASE WHEN SUM(v)/COUNT(*) > 3 THEN 1 ELSE 0 END FROM t');
68
+ ok('#52 聚合算术表达式条件为假', j(r.rows) === j([[0]]), r.rows);
69
+
70
+ // GROUP BY 路径:表达式 + SUM 条件
71
+ r = await q('SELECT g, CASE WHEN SUM(v)>3 THEN 1 ELSE 0 END AS f FROM t GROUP BY g ORDER BY g');
72
+ ok('#52 分组路径 CASE+SUM(排序后)', j(r.rows) === j([['a', 0], ['b', 1]]), r.rows);
73
+
74
+ // 多个分支同时含聚合
75
+ r = await q('SELECT CASE WHEN COUNT(*)>10 THEN COUNT(*) WHEN COUNT(*)>=4 THEN MIN(v) ELSE 0 END FROM t');
76
+ ok('#52 多分支聚合 CASE', j(r.rows) === j([[1]]), r.rows);
77
+
78
+ // 不含聚合的 CASE 仍然逐行求值,不能被误当成聚合列
79
+ r = await q('SELECT CASE WHEN v>2 THEN 1 ELSE 0 END AS f FROM t ORDER BY id');
80
+ ok('#52 无聚合 CASE 仍逐行', j(r.rows) === j([[0], [0], [1], [1]]), r.rows);
81
+
60
82
  console.log(`\n${fail === 0 ? 'ALL PASS' : 'FAILURES'} — ${pass} passed, ${fail} failed\n`);
61
83
  if (fail > 0) process.exit(1);
62
84
  })();
@@ -41,6 +41,39 @@ try {
41
41
  const left = big.search(5);
42
42
  ok('#53 批量删除后剩余行完整', JSON.stringify(left) === JSON.stringify(Array.from({ length: 20 }, (_, i) => i + 20)), left);
43
43
  ok('#53 批量删除后 size 按 distinct keys 计(1)', big.size === 1, big.size);
44
+
45
+ // 多叶子场景:40 个 key × 5 行,order=4 会分裂成多层;逐行删除全部应都返回 true
46
+ const multi = new BTree(4);
47
+ for (let k = 0; k < 40; k++) for (let r = 0; r < 5; r++) multi.insert(k, k * 100 + r);
48
+ ok('#53 多叶子:size 按 distinct keys 计', multi.size === 40, multi.size);
49
+ let allTrue = true;
50
+ let rowsRemoved = 0;
51
+ for (let k = 0; k < 40; k++) {
52
+ for (let r = 0; r < 5; r++) {
53
+ if (multi.remove(k, k * 100 + r) !== true) allTrue = false;
54
+ rowsRemoved++;
55
+ const expect = 4 - r; // 删到第 r 行后剩下的行数
56
+ const leftN = multi.search(k).length;
57
+ if (leftN !== expect) { allTrue = false; console.log('[FAIL] 残留行数不对', k, r, leftN, expect); }
58
+ }
59
+ }
60
+ ok('#53 多叶子:删除全部行都返回 true 且无残留', allTrue && rowsRemoved === 200, { allTrue, rowsRemoved });
61
+ ok('#53 多叶子:全部删完后 size 归零', multi.size === 0, multi.size);
62
+
63
+ // 删除不存在的行不能改动已有行;重复删除同一行第二次应返回 false
64
+ const noop = new BTree(4);
65
+ noop.insert(3, 30);
66
+ noop.insert(3, 31);
67
+ ok('#53 删不存在的 rowIndex 返回 false 且 no-op', noop.remove(3, 99) === false && noop.size === 1);
68
+ ok('#53 同一行重复删除第二次返回 false', noop.remove(3, 30) === true && noop.remove(3, 30) === false);
69
+ ok('#53 重复删除后剩余行不变', JSON.stringify(noop.search(3)) === JSON.stringify([31]), noop.search(3));
70
+
71
+ // unique 索引:重复插入被忽略,删除仍应返回 true
72
+ const uni = new BTree(64, true);
73
+ uni.insert(5, 1);
74
+ uni.insert(5, 2); // unique:忽略
75
+ ok('#53 unique:重复插入不增加 size', uni.size === 1, uni.size);
76
+ ok('#53 unique:删除返回 true 且 size 归零', uni.remove(5, 1) === true && uni.size === 0, uni.size);
44
77
  } catch (e) {
45
78
  fail++;
46
79
  console.log('[FAIL] unexpected exception:', (e && e.stack) || e);
@@ -0,0 +1,76 @@
1
+ /*
2
+ * Issue #59 — WHERE 子句拒绝裸布尔谓词(#21/#42 回归)。
3
+ *
4
+ * WHERE 1 / WHERE col / WHERE name 这类没有显式比较运算符的谓词,
5
+ * 在布尔上下文里应按 MySQL 三值逻辑的 truthiness 求值:
6
+ * 数字/布尔 → <> 0;字符串 → 非空;NULL → 未知(不匹配)。
7
+ */
8
+ const path = require('path');
9
+ const ROOT = path.join(__dirname, '..');
10
+ const { Database } = require(path.join(ROOT, 'index.js'));
11
+ const { executeSQL } = require(path.join(ROOT, 'lib', 'sql.js'));
12
+
13
+ let pass = 0, fail = 0;
14
+ function ok(name, cond, extra) {
15
+ if (cond) { pass++; console.log('[OK] ', name); }
16
+ else { fail++; console.log('[FAIL]', name, extra !== undefined ? '-> ' + JSON.stringify(extra).slice(0, 400) : ''); }
17
+ }
18
+ const j = (rows) => JSON.stringify(rows);
19
+
20
+ (async () => {
21
+ const db = new Database(':memory:');
22
+ const q = (sql) => executeSQL(db, sql, { safety: false });
23
+
24
+ await q('CREATE TABLE t (id INT PRIMARY KEY, active INT, name TEXT, flag INT)');
25
+ await q("INSERT INTO t VALUES (1, 1, 'a', 1), (2, 0, NULL, 0), (3, 5, 'c', 1)");
26
+
27
+ // 裸列谓词
28
+ let r = await q('SELECT id FROM t WHERE active');
29
+ ok('#59 WHERE active(整数列 truthiness)=> [1,3]', j(r.rows) === j([[1], [3]]), r.rows);
30
+
31
+ r = await q('SELECT id FROM t WHERE name');
32
+ ok('#59 WHERE name(非空字符串)=> [1,3]', j(r.rows) === j([[1], [3]]), r.rows);
33
+
34
+ r = await q('SELECT id FROM t WHERE flag');
35
+ ok('#59 WHERE flag => [1,3]', j(r.rows) === j([[1], [3]]), r.rows);
36
+
37
+ // 裸常量谓词
38
+ r = await q('SELECT id FROM t WHERE 1');
39
+ ok('#59 WHERE 1 恒真 => 全部行', j(r.rows) === j([[1], [2], [3]]), r.rows);
40
+
41
+ r = await q('SELECT id FROM t WHERE 0');
42
+ ok('#59 WHERE 0 恒假 => 空', j(r.rows) === j([]), r.rows);
43
+
44
+ r = await q("SELECT id FROM t WHERE ''");
45
+ ok('#59 WHERE 空字符串 => 空', j(r.rows) === j([]), r.rows);
46
+
47
+ // 与 ORDER BY / 逻辑运算组合
48
+ r = await q('SELECT id FROM t WHERE active ORDER BY id DESC');
49
+ ok('#59 WHERE 裸谓词 + ORDER BY', j(r.rows) === j([[3], [1]]), r.rows);
50
+
51
+ r = await q('SELECT id FROM t WHERE 1 AND id > 1 ORDER BY id');
52
+ ok('#59 WHERE 裸谓词 AND 比较式', j(r.rows) === j([[2], [3]]), r.rows);
53
+
54
+ r = await q('SELECT id FROM t WHERE 0 OR active');
55
+ ok('#59 WHERE 裸谓词 OR 裸谓词', j(r.rows) === j([[1], [3]]), r.rows);
56
+
57
+ // HAVING 裸谓词
58
+ r = await q('SELECT active, COUNT(*) AS c FROM t GROUP BY active HAVING flag ORDER BY active');
59
+ ok('#59 HAVING 裸谓词', j(r.rows) === j([[1, 1], [5, 1]]), r.rows);
60
+
61
+ // 不破坏 #21/#42 既有比较语义
62
+ r = await q('SELECT id FROM t WHERE active = 1');
63
+ ok('#59 普通比较未回归(= 1)', j(r.rows) === j([[1]]), r.rows);
64
+
65
+ r = await q('SELECT id FROM t WHERE active IN (0, 5) ORDER BY id');
66
+ ok('#59 普通比较未回归(IN)', j(r.rows) === j([[2], [3]]), r.rows);
67
+
68
+ r = await q('SELECT id FROM t WHERE name IS NULL');
69
+ ok('#59 IS NULL 未回归', j(r.rows) === j([[2]]), r.rows);
70
+
71
+ r = await q('SELECT id FROM t WHERE active BETWEEN 1 AND 3');
72
+ ok('#59 BETWEEN 未回归', j(r.rows) === j([[1]]), r.rows);
73
+
74
+ console.log(`\n${fail === 0 ? 'ALL PASS' : 'FAILURES'} — ${pass} passed, ${fail} failed\n`);
75
+ if (fail > 0) process.exit(1);
76
+ })();
@@ -0,0 +1,92 @@
1
+ /*
2
+ * Issue #60 — 集合运算不校验等列数(静默截断)+ 括号化集合查询被拒。
3
+ *
4
+ * 1) UNION / INTERSECT / EXCEPT 两侧列数不一致时必须显式报错(MySQL 1222),
5
+ * 而不是按左分支静默丢弃右侧多出的列。
6
+ * 2) 集合运算的操作数允许括号化:(SELECT ...) UNION (SELECT ...)。
7
+ * 括号内自带的 ORDER BY / LIMIT / OFFSET 只作用于该操作数;
8
+ * 括号外的尾部子句作用于整个组合查询。
9
+ */
10
+ const path = require('path');
11
+ const ROOT = path.join(__dirname, '..');
12
+ const { Database } = require(path.join(ROOT, 'index.js'));
13
+ const { executeSQL } = require(path.join(ROOT, 'lib', 'sql.js'));
14
+
15
+ let pass = 0, fail = 0;
16
+ function ok(name, cond, extra) {
17
+ if (cond) { pass++; console.log('[OK] ', name); }
18
+ else { fail++; console.log('[FAIL]', name, extra !== undefined ? '-> ' + JSON.stringify(extra).slice(0, 400) : ''); }
19
+ }
20
+ const j = (rows) => JSON.stringify(rows);
21
+
22
+ async function expectErr(q, sql, needle) {
23
+ try {
24
+ await q(sql);
25
+ return { ok: false, reason: 'no error thrown' };
26
+ } catch (e) {
27
+ const msg = String(e && e.message);
28
+ return { ok: msg.includes(needle), reason: msg, code: e && e.code };
29
+ }
30
+ }
31
+
32
+ (async () => {
33
+ const db = new Database(':memory:');
34
+ const q = (sql) => executeSQL(db, sql, { safety: false });
35
+
36
+ await q('CREATE TABLE t (id INT PRIMARY KEY, v INT, s TEXT)');
37
+ await q("INSERT INTO t VALUES (1, 10, 'a'), (2, 20, 'b')");
38
+
39
+ // ---- 列数校验 ----
40
+ const wsp = 'The used SELECT statements have a different number of columns';
41
+ let e = await expectErr(q, 'SELECT id FROM t UNION SELECT id, s FROM t', wsp);
42
+ ok('#60 UNION 列数不同报错', e.ok, e.reason);
43
+
44
+ e = await expectErr(q, 'SELECT id FROM t UNION ALL SELECT id, s FROM t', wsp);
45
+ ok('#60 UNION ALL 列数不同报错', e.ok, e.reason);
46
+
47
+ e = await expectErr(q, 'SELECT id FROM t INTERSECT SELECT id, s FROM t', wsp);
48
+ ok('#60 INTERSECT 列数不同报错', e.ok, e.reason);
49
+
50
+ e = await expectErr(q, 'SELECT id FROM t EXCEPT SELECT id, s FROM t', wsp);
51
+ ok('#60 EXCEPT 列数不同报错', e.ok, e.reason);
52
+
53
+ let r = await q('SELECT id FROM t UNION SELECT v FROM t');
54
+ ok('#60 等列数 UNION 正常', j(r.rows) === j([[1], [2], [10], [20]]), r.rows);
55
+
56
+ r = await q('SELECT id FROM t UNION SELECT id FROM t');
57
+ ok('#60 同列数单列 UNION 正常', j(r.rows) === j([[1], [2]]), r.rows);
58
+
59
+ // ---- 括号化操作数 ----
60
+ r = await q('(SELECT id FROM t) UNION (SELECT id FROM t)');
61
+ ok('#60 (SELECT) UNION (SELECT) 可解析', j(r.rows) === j([[1], [2]]), r.rows);
62
+
63
+ r = await q('(SELECT id FROM t WHERE id = 1) UNION ALL (SELECT id FROM t WHERE id = 2)');
64
+ ok('#60 括号化 UNION ALL', j(r.rows) === j([[1], [2]]), r.rows);
65
+
66
+ r = await q('SELECT id FROM t UNION (SELECT id FROM t)');
67
+ ok('#60 右侧括号化操作数', j(r.rows) === j([[1], [2]]), r.rows);
68
+
69
+ r = await q('(SELECT id FROM t) INTERSECT (SELECT id FROM t WHERE id = 2)');
70
+ ok('#60 括号化 INTERSECT', j(r.rows) === j([[2]]), r.rows);
71
+
72
+ r = await q('(SELECT id FROM t) EXCEPT (SELECT id FROM t WHERE id = 1)');
73
+ ok('#60 括号化 EXCEPT', j(r.rows) === j([[2]]), r.rows);
74
+
75
+ // 括号外尾部 ORDER BY / LIMIT 作用于整体
76
+ r = await q('(SELECT id FROM t) UNION (SELECT id FROM t) ORDER BY id DESC LIMIT 1');
77
+ ok('#60 括号化组合 + 尾部 ORDER BY/LIMIT', j(r.rows) === j([[2]]), r.rows);
78
+
79
+ // 括号内 ORDER BY / LIMIT 只作用于该操作数
80
+ r = await q('(SELECT id FROM t ORDER BY id DESC LIMIT 1) UNION (SELECT id FROM t ORDER BY id ASC LIMIT 1)');
81
+ ok('#60 括号内 LIMIT 局部生效', j(r.rows) === j([[2], [1]]), r.rows);
82
+
83
+ r = await q('(SELECT id FROM t ORDER BY id DESC LIMIT 1) UNION ALL (SELECT id FROM t LIMIT 1)');
84
+ ok('#60 UNION ALL 保留括号内局部 LIMIT', j(r.rows) === j([[2], [1]]), r.rows);
85
+
86
+ // 括号化操作数也参与列数校验
87
+ e = await expectErr(q, '(SELECT id FROM t) UNION (SELECT id, s FROM t)', wsp);
88
+ ok('#60 括号化操作数列数不同报错', e.ok, e.reason);
89
+
90
+ console.log(`\n${fail === 0 ? 'ALL PASS' : 'FAILURES'} — ${pass} passed, ${fail} failed\n`);
91
+ if (fail > 0) process.exit(1);
92
+ })();
@@ -1,8 +0,0 @@
1
- {
2
- "tag": "v6.5.29",
3
- "sha256": "e39295e4bd4f2b705a8b686454c5f2bbe519261bc839810185775146acbadcde",
4
- "packageVersion": "6.5.29",
5
- "installedAt": "2026-11-09T04:35:25.960Z",
6
- "platform": "linux",
7
- "arch": "x64"
8
- }