jsql-neo 6.8.21 → 6.8.22

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.8.21** — stable release · [github.com/vexify-org/JSQL-neo](https://github.com/vexify-org/JSQL-neo) · [jsql.vexify.top](https://jsql.vexify.top)
6
+ > **v6.8.22** — 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.8.21 — Rust-Powered Embedded Database (WASM + HTTP)
2
+ * JSQL-NEO v6.8.22 — Rust-Powered Embedded Database (WASM + HTTP)
3
3
  *
4
4
  * @example
5
5
  * const jsql = require('jsql-neo');
@@ -634,6 +634,23 @@ class MysqlConnection {
634
634
  break;
635
635
  }
636
636
  case 0x09: this._send(okPacket(0, 0, SERVER_STATUS_AUTOCOMMIT)); break; // COM_STATISTICS
637
+ case 0x04: { // #307 COM_FIELD_LIST
638
+ (async () => {
639
+ try {
640
+ const table = body.toString('utf8').split('\0')[0];
641
+ const eng = await this.server._getDatabase(this.currentDb);
642
+ const schema = await eng.getTableSchema(table);
643
+ if (!schema) { this._send(errPacket(1146, `Table '${this.currentDb}.${table}' doesn't exist`)); return; }
644
+ for (const [n, d] of Object.entries(schema)) {
645
+ this._send(columnDefinition({ name: n, table, database: this.currentDb, type: columnTypeFromSchema(d), flags: columnFlags(d) }));
646
+ }
647
+ this._send(eofPacket());
648
+ } catch (e) {
649
+ this._send(errPacket(toMysqlErrno(e), e.message));
650
+ }
651
+ })();
652
+ break;
653
+ }
637
654
  default:
638
655
  this._send(errPacket(1105, `Unsupported command: ${cmd}`));
639
656
  }
@@ -795,42 +812,61 @@ class MysqlConnection {
795
812
  }
796
813
 
797
814
  _handleStmtPrepare(body) {
798
- try {
799
- const sql = body.toString('utf8');
800
- const numParams = this.countPlaceholders(sql);
801
- let numColumns = 0;
815
+ (async () => {
802
816
  try {
803
- const stmt = parseSQL(sql);
804
- if (stmt && stmt.type === 'select' && stmt.columns) numColumns = stmt.columns.length;
805
- } catch (e) { /* 无法解析的 SQL 仍可 prepare */ }
806
- const stmtId = ++this._stmtSeq;
807
- this._stmts.set(stmtId, { sql, numParams, numColumns });
817
+ const sql = body.toString('utf8');
818
+ const numParams = this.countPlaceholders(sql);
819
+ // #273:列元数据来自真实 SQL/schema,不能再用 col1/col2 占位
820
+ let colDefs = [];
821
+ try {
822
+ const stmt = parseSQL(sql);
823
+ if (stmt && stmt.type === 'select') {
824
+ let schema = null;
825
+ try {
826
+ const eng = await this.server._getDatabase(this.currentDb);
827
+ const from = stmt.from && stmt.from.tables && stmt.from.tables[0];
828
+ if (from && from.table && eng.getTableSchema) schema = await eng.getTableSchema(from.table);
829
+ } catch (e) { schema = null; }
830
+ const cols = stmt.columns || [];
831
+ for (let i = 0; i < cols.length; i++) {
832
+ const c = cols[i];
833
+ if (c.expr === '*' && schema) {
834
+ for (const [n, d] of Object.entries(schema)) {
835
+ colDefs.push({ name: n, table: null, database: this.currentDb, type: columnTypeFromSchema(d), flags: columnFlags(d) });
836
+ }
837
+ continue;
838
+ }
839
+ const name = c.alias || (typeof c.expr === 'string' ? c.expr : 'col' + (i + 1));
840
+ const def = schema && schema[name];
841
+ const type = c.aggregate ? MYSQL_TYPE_DOUBLE : columnTypeFromSchema(def || {});
842
+ colDefs.push({ name, table: null, database: this.currentDb, type, flags: columnFlags(def) });
843
+ }
844
+ }
845
+ } catch (e) { /* 无法解析的 SQL 仍可 prepare */ }
846
+ const numColumns = colDefs.length;
847
+ const stmtId = ++this._stmtSeq;
848
+ this._stmts.set(stmtId, { sql, numParams, numColumns, columnDefs: colDefs });
849
+
850
+ const pkt = Buffer.alloc(12);
851
+ let off = 0;
852
+ pkt[off++] = 0x00;
853
+ pkt.writeUInt32LE(stmtId, off); off += 4;
854
+ pkt.writeUInt16LE(numColumns, off); off += 2;
855
+ pkt.writeUInt16LE(numParams, off); off += 2;
856
+ pkt[off++] = 0x00;
857
+ pkt.writeUInt16LE(0, off);
858
+ this._send(pkt);
808
859
 
809
- const pkt = Buffer.alloc(12);
810
- let off = 0;
811
- pkt[off++] = 0x00;
812
- pkt.writeUInt32LE(stmtId, off); off += 4;
813
- pkt.writeUInt16LE(numColumns, off); off += 2;
814
- pkt.writeUInt16LE(numParams, off); off += 2;
815
- pkt[off++] = 0x00;
816
- pkt.writeUInt16LE(0, off);
817
- this._send(pkt);
818
-
819
- if (numParams > 0) {
820
860
  for (let i = 0; i < numParams; i++) {
821
861
  this._send(columnDefinition({ name: '?', type: MYSQL_TYPE_VAR_STRING }));
822
862
  }
823
- this._send(eofPacket());
824
- }
825
- if (numColumns > 0) {
826
- for (let i = 0; i < numColumns; i++) {
827
- this._send(columnDefinition({ name: 'col' + (i + 1), type: MYSQL_TYPE_VAR_STRING }));
828
- }
829
- this._send(eofPacket());
863
+ if (numParams > 0) this._send(eofPacket());
864
+ for (const cd of colDefs) this._send(columnDefinition(cd));
865
+ if (numColumns > 0) this._send(eofPacket());
866
+ } catch (e) {
867
+ this._send(errPacket(toMysqlErrno(e), e.message));
830
868
  }
831
- } catch (e) {
832
- this._send(errPacket(toMysqlErrno(e), e.message));
833
- }
869
+ })();
834
870
  }
835
871
 
836
872
  _handleStmtClose(body) {
@@ -137,19 +137,33 @@ class Database {
137
137
 
138
138
  transaction(fn) {
139
139
  if (typeof fn !== 'function') throw new TypeError('Expected a function');
140
+ if (this._txDepth === undefined) this._txDepth = 0;
140
141
  const wrapped = (...params) => {
141
142
  if (!this.open) throw new SqliteError('The database connection is not open', 'SQLITE_MISUSE');
142
- this.exec('BEGIN');
143
+ // #265:嵌套事务用 SAVEPOINT,内层提交不能提前提交外层
144
+ const depth = this._txDepth;
145
+ const sp = 'sqlite_tx_' + depth;
146
+ if (depth === 0) this.exec('BEGIN');
147
+ else this.exec('SAVEPOINT ' + sp);
148
+ this._txDepth = depth + 1;
143
149
  try {
144
150
  const result = fn.apply(this, params);
145
151
  if (result && typeof result.then === 'function') {
146
- this.exec('ROLLBACK');
152
+ if (depth === 0) this.exec('ROLLBACK');
153
+ else { this.exec('ROLLBACK TO ' + sp); this.exec('RELEASE ' + sp); }
154
+ this._txDepth = depth;
147
155
  throw new SqliteError('Transaction functions cannot be asynchronous', 'SQLITE_MISUSE');
148
156
  }
149
- this.exec('COMMIT');
157
+ if (depth === 0) this.exec('COMMIT');
158
+ else this.exec('RELEASE ' + sp);
159
+ this._txDepth = depth;
150
160
  return result;
151
161
  } catch (err) {
152
- try { this.exec('ROLLBACK'); } catch (e) {}
162
+ try {
163
+ if (depth === 0) this.exec('ROLLBACK');
164
+ else { this.exec('ROLLBACK TO ' + sp); this.exec('RELEASE ' + sp); }
165
+ } catch (e) {}
166
+ this._txDepth = depth;
153
167
  throw err;
154
168
  }
155
169
  };
package/lib/table.js CHANGED
@@ -693,7 +693,8 @@ class Table {
693
693
  if (uniqueTracker[field].has(ukey)) throw createError('ER_DUP_ENTRY', String(data[field]), field);
694
694
  uniqueTracker[field].add(ukey);
695
695
  }
696
- if (def.length && typeof data[field] === 'string' && data[field].length > def.length) throw createError('ER_DATA_TOO_LONG', field);
696
+ // #267:按 Unicode code point 计长度,emoji 算 1 个字符
697
+ if (def.length && typeof data[field] === 'string' && [...data[field]].length > def.length) throw createError('ER_DATA_TOO_LONG', field);
697
698
  if (def.type === 'integer' || def.type === 'int' || def.type === 'bigint' || def.type === 'number') {
698
699
  const raw = data[field];
699
700
  // BigInt 与 number 的 </> 比较是精确的,不要先 Number() 丢精度
@@ -2117,7 +2118,7 @@ class Table {
2117
2118
  }
2118
2119
 
2119
2120
  // 长度限制
2120
- if (def.length && typeof data[field] === 'string' && data[field].length > def.length) {
2121
+ if (def.length && typeof data[field] === 'string' && [...data[field]].length > def.length) {
2121
2122
  throw createError('ER_DATA_TOO_LONG', field);
2122
2123
  }
2123
2124
 
@@ -487,6 +487,12 @@ class JSQL {
487
487
 
488
488
  async start() {
489
489
  wasmBindings.jsql_reset();
490
+ // #294:stop→start 后底层表已清空,JS 侧元数据必须同步清掉
491
+ this._tableNames.clear();
492
+ this._schemas = {};
493
+ this._buffer = {};
494
+ this._bufferSize = 0;
495
+ this._txId = undefined;
490
496
  await this._runHooks('onStart', []);
491
497
  this._emit('start', {});
492
498
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jsql-neo",
3
- "version": "6.8.21",
3
+ "version": "6.8.22",
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",
@@ -52,12 +52,12 @@
52
52
  "build:native": "node scripts/build-native.js",
53
53
  "check:release": "node scripts/check-release-metadata.js",
54
54
  "release:prep": "node scripts/release.js",
55
- "test": "npm run check:release && 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 && node test/issue-63-68.test.js && node test/network-policy.test.js && node test/issue-69-70.test.js && node test/issue-71-72.test.js && node test/issue-73-76.test.js && node test/issue-77-84.test.js && node test/issue-86-99.test.js && node test/issue-101-103.test.js && node test/issue-105.test.js && node test/issue-118-136.test.js && node test/issue-127.test.js && node test/issue-138-156.test.js && node test/issue-157-161.test.js && node test/issue-162-179.test.js && node test/issue-181-186.test.js && node test/issue-187.test.js && node test/issue-188-237.test.js && node test/issue-238-241.test.js && node test/bigint-math.test.js && node test/release-metadata.test.js && node test/date-conversions.test.js && node test/mysql-utility-functions.test.js && node test/limit-pushdown.test.js && node test/columnar.test.js && node test/columnar-vector.test.js && node test/cast-rounding.test.js && node test/convert-subquery.test.js && node test/mongo-persistence.test.js && node test/mongo-objectid.test.js && node test/mongo-arrays.test.js && node test/issue-117.test.js && node test/issue-314-321.test.js && node test/issue-312-320.test.js && node test/issue-243-289.test.js && node test/issue-284-292.test.js && node test/issue-301-305.test.js && node test/issue-248-252.test.js && node test/issue-259-264.test.js && node test/issue-255-270.test.js && node test/issue-253-269.test.js && node test/issue-280-310.test.js && node test/issue-272-311.test.js && node test/issue-279-306.test.js && node test/issue-271-283.test.js",
55
+ "test": "npm run check:release && 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 && node test/issue-63-68.test.js && node test/network-policy.test.js && node test/issue-69-70.test.js && node test/issue-71-72.test.js && node test/issue-73-76.test.js && node test/issue-77-84.test.js && node test/issue-86-99.test.js && node test/issue-101-103.test.js && node test/issue-105.test.js && node test/issue-118-136.test.js && node test/issue-127.test.js && node test/issue-138-156.test.js && node test/issue-157-161.test.js && node test/issue-162-179.test.js && node test/issue-181-186.test.js && node test/issue-187.test.js && node test/issue-188-237.test.js && node test/issue-238-241.test.js && node test/bigint-math.test.js && node test/release-metadata.test.js && node test/date-conversions.test.js && node test/mysql-utility-functions.test.js && node test/limit-pushdown.test.js && node test/columnar.test.js && node test/columnar-vector.test.js && node test/cast-rounding.test.js && node test/convert-subquery.test.js && node test/mongo-persistence.test.js && node test/mongo-objectid.test.js && node test/mongo-arrays.test.js && node test/issue-117.test.js && node test/issue-314-321.test.js && node test/issue-312-320.test.js && node test/issue-243-289.test.js && node test/issue-284-292.test.js && node test/issue-301-305.test.js && node test/issue-248-252.test.js && node test/issue-259-264.test.js && node test/issue-255-270.test.js && node test/issue-253-269.test.js && node test/issue-280-310.test.js && node test/issue-272-311.test.js && node test/issue-279-306.test.js && node test/issue-271-283.test.js && node test/issue-265-307.test.js",
56
56
  "test:parser": "node test/sql-parser.test.js",
57
57
  "test:btree": "node test/btree.test.js",
58
58
  "test:native": "node test/native.test.js",
59
59
  "test:regress": "node test/regress-5.1.0.js",
60
- "test:all": "npm run check:release && 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 && node test/issue-63-68.test.js && node test/network-policy.test.js && node test/issue-69-70.test.js && node test/issue-71-72.test.js && node test/issue-77-84.test.js && node test/issue-86-99.test.js && node test/issue-101-103.test.js && node test/issue-105.test.js && node test/issue-118-136.test.js && node test/issue-127.test.js && node test/issue-138-156.test.js && node test/issue-157-161.test.js && node test/issue-162-179.test.js && node test/issue-181-186.test.js && node test/issue-187.test.js && node test/issue-188-237.test.js && node test/issue-238-241.test.js && node test/bigint-math.test.js && node test/release-metadata.test.js && node test/date-conversions.test.js && node test/mysql-utility-functions.test.js && node test/limit-pushdown.test.js && node test/columnar.test.js && node test/columnar-vector.test.js && node test/cast-rounding.test.js && node test/convert-subquery.test.js && node test/mongo-persistence.test.js && node test/mongo-objectid.test.js && node test/mongo-arrays.test.js && node test/issue-117.test.js && node test/issue-314-321.test.js && node test/issue-312-320.test.js && node test/issue-243-289.test.js && node test/issue-284-292.test.js && node test/issue-301-305.test.js && node test/issue-248-252.test.js && node test/issue-259-264.test.js && node test/issue-255-270.test.js && node test/issue-253-269.test.js && node test/issue-280-310.test.js && node test/issue-272-311.test.js && node test/issue-279-306.test.js && node test/issue-271-283.test.js",
60
+ "test:all": "npm run check:release && 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 && node test/issue-63-68.test.js && node test/network-policy.test.js && node test/issue-69-70.test.js && node test/issue-71-72.test.js && node test/issue-77-84.test.js && node test/issue-86-99.test.js && node test/issue-101-103.test.js && node test/issue-105.test.js && node test/issue-118-136.test.js && node test/issue-127.test.js && node test/issue-138-156.test.js && node test/issue-157-161.test.js && node test/issue-162-179.test.js && node test/issue-181-186.test.js && node test/issue-187.test.js && node test/issue-188-237.test.js && node test/issue-238-241.test.js && node test/bigint-math.test.js && node test/release-metadata.test.js && node test/date-conversions.test.js && node test/mysql-utility-functions.test.js && node test/limit-pushdown.test.js && node test/columnar.test.js && node test/columnar-vector.test.js && node test/cast-rounding.test.js && node test/convert-subquery.test.js && node test/mongo-persistence.test.js && node test/mongo-objectid.test.js && node test/mongo-arrays.test.js && node test/issue-117.test.js && node test/issue-314-321.test.js && node test/issue-312-320.test.js && node test/issue-243-289.test.js && node test/issue-284-292.test.js && node test/issue-301-305.test.js && node test/issue-248-252.test.js && node test/issue-259-264.test.js && node test/issue-255-270.test.js && node test/issue-253-269.test.js && node test/issue-280-310.test.js && node test/issue-272-311.test.js && node test/issue-279-306.test.js && node test/issue-271-283.test.js && node test/issue-265-307.test.js",
61
61
  "test:coverage": "c8 --all --include='lib/**' --exclude='lib/web_ui.js' --reporter=text --reporter=lcov node test/coverage.js",
62
62
  "test:orms": "node examples/orms/run-all.js",
63
63
  "test:dml": "node test/dml-constraints.test.js",
@@ -0,0 +1,88 @@
1
+ /*
2
+ * Regression tests for #265 #267 #273 #294 #307.
3
+ * node test/issue-265-307.test.js
4
+ */
5
+ const path = require('path');
6
+ const ROOT = path.join(__dirname, '..');
7
+ const { Database, executeSQL, JSQL } = require(path.join(ROOT, 'index.js'));
8
+ const SqliteDatabase = require(path.join(ROOT, 'lib', 'sqlite_compat.js'));
9
+
10
+ let passed = 0, failed = 0;
11
+ function ok(name, cond, extra) {
12
+ if (cond) { passed++; console.log('[OK]', name); }
13
+ else { failed++; console.log('[FAIL]', name, extra !== undefined ? '→ ' + JSON.stringify(extra) : ''); }
14
+ }
15
+ const sleep = (ms) => new Promise(r => setTimeout(r, ms));
16
+
17
+ (async () => {
18
+ // #267
19
+ {
20
+ const db = new Database(':memory:');
21
+ await executeSQL(db, 'CREATE TABLE t (v VARCHAR(3))', { safety: false });
22
+ let e3 = null;
23
+ try { await executeSQL(db, "INSERT INTO t VALUES ('😀😀😀')", { safety: false }); } catch (e) { e3 = e.code; }
24
+ let e4 = null;
25
+ try { await executeSQL(db, "INSERT INTO t VALUES ('😀😀😀😀')", { safety: false }); } catch (e) { e4 = e.code; }
26
+ ok('#267 VARCHAR length counts code points', e3 === null && e4 === 1406, { e3, e4 });
27
+ db.stop();
28
+ }
29
+ // #265
30
+ {
31
+ const db = new SqliteDatabase(':memory:');
32
+ db.exec('CREATE TABLE t (id INTEGER PRIMARY KEY)');
33
+ let caught = null;
34
+ try {
35
+ db.transaction(() => {
36
+ db.prepare('INSERT INTO t VALUES (1)').run();
37
+ db.transaction(() => { db.prepare('INSERT INTO t VALUES (2)').run(); })();
38
+ throw new Error('outer rollback');
39
+ })();
40
+ } catch (e) { caught = e.message; }
41
+ const n = db.prepare('SELECT COUNT(*) c FROM t').get().c;
42
+ ok('#265 nested sqlite transaction rolls back inner writes', caught === 'outer rollback' && n === 0, { caught, n });
43
+ db.close();
44
+ }
45
+ // #294
46
+ {
47
+ const db = new JSQL();
48
+ await db.start();
49
+ await db.createTable('x', { id: { type: 'integer', primaryKey: true } });
50
+ await db.stop();
51
+ await db.start();
52
+ const tables = await db.getTables();
53
+ ok('#294 wasm stop/start clears JS table metadata', Array.isArray(tables) && tables.length === 0, tables);
54
+ await db.stop();
55
+ }
56
+ // #273 / #307 via protocol-level unit invocation
57
+ {
58
+ const { MysqlConnection } = require(path.join(ROOT, 'lib', 'mysql_server.js'));
59
+ const engine = new Database(':memory:');
60
+ await engine.createTable('t', { id: { type: 'integer', primaryKey: true }, name: { type: 'string' } });
61
+ const makeConn = () => {
62
+ const c = Object.create(MysqlConnection.prototype);
63
+ c.sequence = 0;
64
+ c.authenticated = true;
65
+ c._stmtSeq = 0;
66
+ c._stmts = new Map();
67
+ c.currentDb = 'app';
68
+ c.server = { _getDatabase: async () => engine };
69
+ c.packets = [];
70
+ c.socket = { writable: true, write: (b) => c.packets.push(b.slice(4)), end() {} };
71
+ return c;
72
+ };
73
+ const conn = makeConn();
74
+ conn._handleStmtPrepare(Buffer.from('SELECT id, name FROM t'));
75
+ await sleep(50);
76
+ const joined = Buffer.concat(conn.packets).toString('latin1');
77
+ ok('#273 prepare column metadata uses real names', joined.includes('id') && joined.includes('name') && !joined.includes('col1'), conn.packets.length);
78
+
79
+ const conn2 = makeConn();
80
+ conn2._handlePacket(Buffer.concat([Buffer.from([0x04]), Buffer.from('t\0\0')]), 0);
81
+ await sleep(50);
82
+ const fieldList = Buffer.concat(conn2.packets).toString('latin1');
83
+ ok('#307 COM_FIELD_LIST returns column definitions', fieldList.includes('id') && fieldList.includes('name'), conn2.packets.length);
84
+ engine.stop();
85
+ }
86
+ console.log(`\n${passed} passed, ${failed} failed`);
87
+ process.exit(failed === 0 ? 0 : 1);
88
+ })().catch((e) => { console.error('FATAL', e); process.exit(1); });