jsql-neo 6.8.22 → 6.8.24
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 +1 -1
- package/index.js +1 -1
- package/lib/database.js +11 -9
- package/lib/errors.js +1 -0
- package/lib/native_client.js +103 -6
- package/lib/pg_server.js +55 -19
- package/lib/sql.js +31 -2
- package/lib/tui.js +8 -1
- package/package.json +3 -3
- package/test/issue-188-237.test.js +1 -1
- package/test/issue-247-295-277.test.js +66 -0
- package/test/issue-258-300.test.js +64 -0
- package/test/issue-38.test.js +3 -0
- package/test/wal-recovery.test.js +9 -9
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.
|
|
6
|
+
> **v6.8.24** — 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
|

|
|
9
9
|

|
package/index.js
CHANGED
package/lib/database.js
CHANGED
|
@@ -213,7 +213,8 @@ class Database {
|
|
|
213
213
|
this._jsqlFormat = this._jsqlMode ? new JSQLFormat(this._filePath, this._encryptKey) : null;
|
|
214
214
|
|
|
215
215
|
// 文件锁
|
|
216
|
-
|
|
216
|
+
// #295:默认开启文件锁,避免两个实例静默互相覆盖
|
|
217
|
+
this._fileLockEnabled = options.fileLock !== false && !this._memoryMode;
|
|
217
218
|
this._lockFd = null;
|
|
218
219
|
this._lockPath = this._filePath ? this._filePath + '.lock' : null;
|
|
219
220
|
|
|
@@ -228,14 +229,13 @@ class Database {
|
|
|
228
229
|
if (!this._memoryMode && !this._dirMode) {
|
|
229
230
|
const hasDataFile = this._filePath ? fs.existsSync(this._filePath) : false;
|
|
230
231
|
const hasWal = this._walEnabled && this._walPath && fs.existsSync(this._walPath);
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
}
|
|
232
|
+
// #295:即使数据文件尚不存在也要先拿锁,否则两个新实例会同时写同一路径
|
|
233
|
+
this._loading = true;
|
|
234
|
+
this._acquireLock();
|
|
235
|
+
try {
|
|
236
|
+
if (hasDataFile || hasWal) this._load();
|
|
237
|
+
} finally {
|
|
238
|
+
this._loading = false;
|
|
239
239
|
}
|
|
240
240
|
}
|
|
241
241
|
|
|
@@ -2776,6 +2776,8 @@ class Database {
|
|
|
2776
2776
|
this._sigInstalled = false;
|
|
2777
2777
|
Database._uninstallSignalHandler(this);
|
|
2778
2778
|
}
|
|
2779
|
+
// #295:stop() 表示本实例停止写入,必须释放文件锁,否则同路径再打开会误报占用
|
|
2780
|
+
this._releaseLock();
|
|
2779
2781
|
return this;
|
|
2780
2782
|
}
|
|
2781
2783
|
|
package/lib/errors.js
CHANGED
|
@@ -57,6 +57,7 @@ const ErrorCodes = {
|
|
|
57
57
|
// 保存点不存在(MySQL 用这个码:SAVEPOINT xxx does not exist)
|
|
58
58
|
ER_SP_DOES_NOT_EXIST: { code: 1305, msg: "SAVEPOINT %s does not exist" },
|
|
59
59
|
ER_FUNCTION_NOT_EXIST: { code: 1305, msg: "FUNCTION %s does not exist" },
|
|
60
|
+
ER_SUBSELECT_NO_1_ROW: { code: 1242, msg: "Subquery returns more than 1 row" },
|
|
60
61
|
ER_WRONG_VALUE_COUNT_ON_ROW: { code: 1136, msg: "Column count doesn't match value count at row %s" },
|
|
61
62
|
ER_TRUNCATE_ILLEGAL_FK: { code: 1701, msg: "Cannot truncate a table referenced in a foreign key constraint (%s)" },
|
|
62
63
|
ER_ROW_IS_REFERENCED_2: { code: 1451, msg: "Cannot delete or update a parent row: a foreign key constraint fails (%s)" },
|
package/lib/native_client.js
CHANGED
|
@@ -1156,13 +1156,110 @@ class JSQL {
|
|
|
1156
1156
|
// 没有原生模块时不要构造一个「所有属性都是函数」的假 native:
|
|
1157
1157
|
// 直接回退到 WASM/JS 引擎,保持 API 可用(性能较低但功能完整)。
|
|
1158
1158
|
let ExportedJSQL = JSQL;
|
|
1159
|
+
let DiskFallbackJSQL = null;
|
|
1160
|
+
let WasmJSQL = null;
|
|
1161
|
+
try {
|
|
1162
|
+
WasmJSQL = require('./wasm_client').JSQL;
|
|
1163
|
+
} catch (e) {
|
|
1164
|
+
console.warn('[jsql-neo] WASM fallback unavailable:', e.message);
|
|
1165
|
+
}
|
|
1166
|
+
if (WasmJSQL) {
|
|
1167
|
+
// #247:WASM/JS 引擎本身是纯内存实现。带 path 且非 memory 模式时,
|
|
1168
|
+
// 改由纯 JS 的 Database 承载,保证 stop()/重启后数据真正落盘。
|
|
1169
|
+
DiskFallbackJSQL = class DiskFallbackJSQL extends WasmJSQL {
|
|
1170
|
+
constructor(opts = {}) {
|
|
1171
|
+
const wantDisk = !!opts.path && opts.path !== ':memory:'
|
|
1172
|
+
&& (opts.mode || 'disk') !== 'memory';
|
|
1173
|
+
const superOpts = wantDisk ? Object.assign({}, opts, { modules: false }) : opts;
|
|
1174
|
+
super(superOpts);
|
|
1175
|
+
if (wantDisk) {
|
|
1176
|
+
const Database = require('./database');
|
|
1177
|
+
this._disk = new Database(opts.path, opts);
|
|
1178
|
+
}
|
|
1179
|
+
}
|
|
1180
|
+
async start() {
|
|
1181
|
+
if (!this._disk) return super.start();
|
|
1182
|
+
await this._disk.start();
|
|
1183
|
+
this._runHooks('onStart', []);
|
|
1184
|
+
this._emit('start', {});
|
|
1185
|
+
}
|
|
1186
|
+
async stop() {
|
|
1187
|
+
if (!this._disk) return super.stop();
|
|
1188
|
+
await this._disk.stop();
|
|
1189
|
+
this._runHooks('onStop', []);
|
|
1190
|
+
this._emit('stop', {});
|
|
1191
|
+
}
|
|
1192
|
+
async createTable(name, schema) {
|
|
1193
|
+
return this._disk ? this._disk.createTable(name, schema) : super.createTable(name, schema);
|
|
1194
|
+
}
|
|
1195
|
+
async addColumn(table, name, def) {
|
|
1196
|
+
return this._disk ? this._disk.addColumn(table, name, def) : super.addColumn(table, name, def);
|
|
1197
|
+
}
|
|
1198
|
+
async dropTable(name) {
|
|
1199
|
+
return this._disk ? this._disk.dropTable(name) : super.dropTable(name);
|
|
1200
|
+
}
|
|
1201
|
+
async insert(table, data) {
|
|
1202
|
+
return this._disk ? this._disk.insert(table, data) : super.insert(table, data);
|
|
1203
|
+
}
|
|
1204
|
+
async insertMany(table, data) {
|
|
1205
|
+
return this._disk ? this._disk.insertMany(table, data) : super.insertMany(table, data);
|
|
1206
|
+
}
|
|
1207
|
+
async find(table, filter, opts) {
|
|
1208
|
+
return this._disk ? this._disk.find(table, filter, opts) : super.find(table, filter, opts);
|
|
1209
|
+
}
|
|
1210
|
+
async findById(table, id) {
|
|
1211
|
+
return this._disk ? this._disk.findById(table, id) : super.findById(table, id);
|
|
1212
|
+
}
|
|
1213
|
+
async findByIds(table, ids) {
|
|
1214
|
+
return this._disk ? this._disk.findByIds(table, ids) : super.findByIds(table, ids);
|
|
1215
|
+
}
|
|
1216
|
+
async updateById(table, id, data) {
|
|
1217
|
+
return this._disk ? this._disk.updateById(table, id, data) : super.updateById(table, id, data);
|
|
1218
|
+
}
|
|
1219
|
+
async updateByIds(table, entries) {
|
|
1220
|
+
return this._disk ? this._disk.updateByIds(table, entries) : super.updateByIds(table, entries);
|
|
1221
|
+
}
|
|
1222
|
+
async removeById(table, id) {
|
|
1223
|
+
return this._disk ? this._disk.removeById(table, id) : super.removeById(table, id);
|
|
1224
|
+
}
|
|
1225
|
+
async removeByIds(table, ids) {
|
|
1226
|
+
return this._disk ? this._disk.removeByIds(table, ids) : super.removeByIds(table, ids);
|
|
1227
|
+
}
|
|
1228
|
+
async count(table) {
|
|
1229
|
+
return this._disk ? this._disk.count(table) : super.count(table);
|
|
1230
|
+
}
|
|
1231
|
+
getTables() {
|
|
1232
|
+
return this._disk ? this._disk.getTables() : super.getTables();
|
|
1233
|
+
}
|
|
1234
|
+
hasTable(name) {
|
|
1235
|
+
return this._disk ? this._disk.hasTable(name) : super.hasTable(name);
|
|
1236
|
+
}
|
|
1237
|
+
async getTableSchema(name) {
|
|
1238
|
+
return this._disk ? this._disk.getTableSchema(name) : super.getTableSchema(name);
|
|
1239
|
+
}
|
|
1240
|
+
async query(sql, paramsOrOpts, opts) {
|
|
1241
|
+
return this._disk ? this._disk.query(sql, paramsOrOpts, opts) : super.query(sql, paramsOrOpts, opts);
|
|
1242
|
+
}
|
|
1243
|
+
async exists(table, filter) {
|
|
1244
|
+
return this._disk ? this._disk.exists(table, filter) : super.exists(table, filter);
|
|
1245
|
+
}
|
|
1246
|
+
async flush() {
|
|
1247
|
+
return this._disk ? this._disk.flush() : super.flush();
|
|
1248
|
+
}
|
|
1249
|
+
async beginTx() {
|
|
1250
|
+
return this._disk ? this._disk.beginTx() : super.beginTx();
|
|
1251
|
+
}
|
|
1252
|
+
async commitTx(id) {
|
|
1253
|
+
return this._disk ? this._disk.commitTx(id) : super.commitTx(id);
|
|
1254
|
+
}
|
|
1255
|
+
async rollbackTx(id) {
|
|
1256
|
+
return this._disk ? this._disk.rollbackTx(id) : super.rollbackTx(id);
|
|
1257
|
+
}
|
|
1258
|
+
}
|
|
1259
|
+
}
|
|
1159
1260
|
if (!NATIVE_AVAILABLE) {
|
|
1160
1261
|
console.warn('[jsql-neo] NativeJSQL unavailable; falling back to the WASM/JS engine.');
|
|
1161
|
-
|
|
1162
|
-
ExportedJSQL = require('./wasm_client').JSQL;
|
|
1163
|
-
} catch (e) {
|
|
1164
|
-
console.warn('[jsql-neo] WASM fallback also unavailable:', e.message);
|
|
1165
|
-
}
|
|
1262
|
+
ExportedJSQL = DiskFallbackJSQL || WasmJSQL || JSQL;
|
|
1166
1263
|
}
|
|
1167
1264
|
|
|
1168
|
-
module.exports = { JSQL: ExportedJSQL, _nativeAvailable: NATIVE_AVAILABLE };
|
|
1265
|
+
module.exports = { JSQL: ExportedJSQL, _nativeAvailable: NATIVE_AVAILABLE, _DiskFallbackJSQL: DiskFallbackJSQL };
|
package/lib/pg_server.js
CHANGED
|
@@ -134,27 +134,46 @@ function frame(type, payload) {
|
|
|
134
134
|
return Buffer.concat([head, payload]);
|
|
135
135
|
}
|
|
136
136
|
|
|
137
|
-
|
|
137
|
+
// #277:按列的结果格式码编码二进制 DataRow
|
|
138
|
+
function pgEncodeBinary(value, type) {
|
|
139
|
+
const t = String(type || '').toLowerCase();
|
|
140
|
+
if (t === 'boolean' || t === 'bool') return Buffer.from([value ? 1 : 0]);
|
|
141
|
+
if (t === 'integer' || t === 'int' || t === 'int4' || t === 'serial') {
|
|
142
|
+
const b = Buffer.alloc(4); b.writeInt32BE(Number(value) | 0, 0); return b;
|
|
143
|
+
}
|
|
144
|
+
if (t === 'bigint' || t === 'int8' || t === 'bigserial') {
|
|
145
|
+
const b = Buffer.alloc(8); b.writeBigInt64BE(BigInt(value), 0); return b;
|
|
146
|
+
}
|
|
147
|
+
if (t === 'numeric' || t === 'decimal' || t === 'number' || t === 'float' || t === 'float8') {
|
|
148
|
+
const b = Buffer.alloc(8); b.writeDoubleBE(Number(value), 0); return b;
|
|
149
|
+
}
|
|
150
|
+
return Buffer.from(pgFormatValue(value, type) === null ? '' : String(pgFormatValue(value, type)), 'utf8');
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function buildDataRow(values, types, formats) {
|
|
138
154
|
const p = new PgBuffer();
|
|
139
155
|
p.i16(values.length);
|
|
140
156
|
for (let i = 0; i < values.length; i++) {
|
|
141
|
-
const
|
|
142
|
-
if (
|
|
157
|
+
const fmt = formats && formats.length ? (formats.length === 1 ? formats[0] : formats[i]) : 0;
|
|
158
|
+
if (values[i] === null || values[i] === undefined) {
|
|
143
159
|
p.i32(-1);
|
|
144
|
-
|
|
145
|
-
const b = Buffer.from(s, 'utf8');
|
|
146
|
-
p.i32(b.length);
|
|
147
|
-
p.raw(b);
|
|
160
|
+
continue;
|
|
148
161
|
}
|
|
162
|
+
const b = fmt === 1
|
|
163
|
+
? pgEncodeBinary(values[i], types[i])
|
|
164
|
+
: Buffer.from(pgFormatValue(values[i], types[i]) === null ? '' : String(pgFormatValue(values[i], types[i])), 'utf8');
|
|
165
|
+
p.i32(b.length);
|
|
166
|
+
p.raw(b);
|
|
149
167
|
}
|
|
150
168
|
return frame(0x44, p.build()); // 'D'
|
|
151
169
|
}
|
|
152
170
|
|
|
153
|
-
function buildRowDescription(columns, types) {
|
|
171
|
+
function buildRowDescription(columns, types, formats) {
|
|
154
172
|
const p = new PgBuffer();
|
|
155
173
|
p.i16(columns.length);
|
|
156
174
|
for (let i = 0; i < columns.length; i++) {
|
|
157
175
|
const c = columns[i];
|
|
176
|
+
const fmt = formats && formats.length ? (formats.length === 1 ? formats[0] : formats[i]) : 0;
|
|
158
177
|
p.str(c);
|
|
159
178
|
p.nul();
|
|
160
179
|
p.i32(0); // table oid
|
|
@@ -162,7 +181,7 @@ function buildRowDescription(columns, types) {
|
|
|
162
181
|
p.i32(pgTypeOid(types[i]));
|
|
163
182
|
p.i16(types[i] === 'integer' ? 4 : (types[i] === 'bigint' ? 8 : (types[i] === 'boolean' ? 1 : -1))); // typlen
|
|
164
183
|
p.i32(-1); // typmod
|
|
165
|
-
p.i16(0);
|
|
184
|
+
p.i16(fmt === 1 ? 1 : 0); // format code
|
|
166
185
|
}
|
|
167
186
|
return frame(0x54, p.build()); // 'T'
|
|
168
187
|
}
|
|
@@ -568,7 +587,15 @@ class PgConnection {
|
|
|
568
587
|
err.sqlState = '08P01';
|
|
569
588
|
throw err;
|
|
570
589
|
}
|
|
571
|
-
|
|
590
|
+
// #277:Bind 末尾还有结果格式码列表(0=text,1=binary)
|
|
591
|
+
const resultFormats = [];
|
|
592
|
+
if (off + 2 <= body.length) {
|
|
593
|
+
const nResultFormats = body.readInt16BE(off); off += 2;
|
|
594
|
+
for (let i = 0; i < nResultFormats && off + 2 <= body.length; i++) {
|
|
595
|
+
resultFormats.push(body.readInt16BE(off)); off += 2;
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
this._portal.set(portal, { values, stmt, resultFormats });
|
|
572
599
|
this._send(buildBindComplete()); break;
|
|
573
600
|
}
|
|
574
601
|
case 0x44: // 'D' describe
|
|
@@ -638,8 +665,9 @@ class PgConnection {
|
|
|
638
665
|
const portal = this._portal.get(name);
|
|
639
666
|
if (!portal) throw new Error(`portal "${name}" does not exist`);
|
|
640
667
|
const cols = this._describeCols(portal.stmt.sql);
|
|
641
|
-
if (cols && cols.length > 0) {
|
|
642
|
-
this.
|
|
668
|
+
if (cols && cols.columns && cols.columns.length > 0) {
|
|
669
|
+
const fmts = this._columnFormats(cols.columns.length, portal.resultFormats);
|
|
670
|
+
this._send(buildRowDescription(cols.columns, cols.types, fmts));
|
|
643
671
|
} else {
|
|
644
672
|
this._send(buildNoData());
|
|
645
673
|
}
|
|
@@ -719,7 +747,7 @@ class PgConnection {
|
|
|
719
747
|
return;
|
|
720
748
|
}
|
|
721
749
|
try {
|
|
722
|
-
await this._executeSql(sql, null);
|
|
750
|
+
await this._executeSql(sql, null, null);
|
|
723
751
|
} catch (e) {
|
|
724
752
|
if (this._inTransaction) this._txAborted = true;
|
|
725
753
|
this._send(buildErrorResponse(this._pgErrorCode(e), e && e.message ? e.message : String(e)));
|
|
@@ -736,14 +764,14 @@ class PgConnection {
|
|
|
736
764
|
}
|
|
737
765
|
try {
|
|
738
766
|
const { sql } = portal.stmt;
|
|
739
|
-
await this._executeSql(sql, portal.values || []);
|
|
767
|
+
await this._executeSql(sql, portal.values || [], portal.resultFormats);
|
|
740
768
|
} catch (e) {
|
|
741
769
|
if (this._inTransaction) this._txAborted = true;
|
|
742
770
|
this._send(buildErrorResponse(this._pgErrorCode(e), e && e.message ? e.message : String(e)));
|
|
743
771
|
}
|
|
744
772
|
}
|
|
745
773
|
|
|
746
|
-
async _executeSql(sql, values) {
|
|
774
|
+
async _executeSql(sql, values, resultFormats) {
|
|
747
775
|
const engine = await this._getEngine();
|
|
748
776
|
const ctx = {
|
|
749
777
|
session: { currentDb: this.database, connectionId: this._pid },
|
|
@@ -759,19 +787,27 @@ class PgConnection {
|
|
|
759
787
|
}
|
|
760
788
|
const list = Array.isArray(results) ? results : [results];
|
|
761
789
|
for (const res of list) {
|
|
762
|
-
this._emitResult(res);
|
|
790
|
+
this._emitResult(res, resultFormats);
|
|
763
791
|
}
|
|
764
792
|
}
|
|
765
793
|
|
|
766
|
-
|
|
794
|
+
_columnFormats(count, resultFormats) {
|
|
795
|
+
if (!resultFormats || resultFormats.length === 0) return null;
|
|
796
|
+
const out = new Array(count).fill(0);
|
|
797
|
+
for (let i = 0; i < count; i++) out[i] = resultFormats.length === 1 ? resultFormats[0] : (resultFormats[i] || 0);
|
|
798
|
+
return out;
|
|
799
|
+
}
|
|
800
|
+
|
|
801
|
+
_emitResult(res, resultFormats) {
|
|
767
802
|
if (!res) return;
|
|
768
803
|
if (res.type === 'select') {
|
|
769
804
|
const types = this._resultTypes(res);
|
|
805
|
+
const fmts = this._columnFormats((res.columns || []).length, resultFormats);
|
|
770
806
|
if (res.columns && res.columns.length > 0) {
|
|
771
|
-
this._send(buildRowDescription(res.columns, types));
|
|
807
|
+
this._send(buildRowDescription(res.columns, types, fmts));
|
|
772
808
|
}
|
|
773
809
|
for (const row of res.rows || []) {
|
|
774
|
-
this._send(buildDataRow(row, types));
|
|
810
|
+
this._send(buildDataRow(row, types, fmts));
|
|
775
811
|
}
|
|
776
812
|
const tag = `SELECT ${(res.rows || []).length}`;
|
|
777
813
|
this._send(buildCommandComplete(tag));
|
package/lib/sql.js
CHANGED
|
@@ -529,7 +529,7 @@ function tokenize(sql) {
|
|
|
529
529
|
const AGG_FUNCS = new Set(['SUM', 'AVG', 'MIN', 'MAX', 'COUNT',
|
|
530
530
|
'GROUP_CONCAT', 'STDDEV', 'STD', 'STDDEV_POP', 'STDDEV_SAMP',
|
|
531
531
|
'VARIANCE', 'VAR_POP', 'VAR_SAMP', 'FIRST', 'LAST',
|
|
532
|
-
'BIT_AND', 'BIT_OR', 'BIT_XOR']);
|
|
532
|
+
'BIT_AND', 'BIT_OR', 'BIT_XOR', 'JSON_ARRAYAGG', 'JSON_OBJECTAGG']);
|
|
533
533
|
|
|
534
534
|
// 方差/标准差族(Issue #114):MySQL 里无后缀的 VARIANCE / STDDEV / STD
|
|
535
535
|
// 都是**总体**口径(= VAR_POP / STDDEV_POP),不是样本。
|
|
@@ -2660,7 +2660,15 @@ class Parser {
|
|
|
2660
2660
|
}
|
|
2661
2661
|
column = { type: 'tuple', items: cols };
|
|
2662
2662
|
} else if (this.peek().type === 'op' && this.peek().value === ',') {
|
|
2663
|
-
if (
|
|
2663
|
+
if (fn === 'JSON_OBJECTAGG') {
|
|
2664
|
+
// JSON_OBJECTAGG(key, value):收集 [key, value] 两个表达式
|
|
2665
|
+
const items = [column];
|
|
2666
|
+
while (this.peek().type === 'op' && this.peek().value === ',') {
|
|
2667
|
+
this.next();
|
|
2668
|
+
items.push(this.parseScalar());
|
|
2669
|
+
}
|
|
2670
|
+
column = { type: 'arglist', items };
|
|
2671
|
+
} else if (VARIANCE_FUNCS.has(fn)) {
|
|
2664
2672
|
// Issue #114:MySQL 的 VARIANCE/STDDEV/STD 只接受单个表达式,
|
|
2665
2673
|
// 但本项目历史上接受 VARIANCE(1,2,3) 这种多参数写法,统一收集成 arglist,
|
|
2666
2674
|
// 让每个参数值都参与统计(多参数时不再误当成 GROUP_CONCAT 的分隔符)。
|
|
@@ -8200,6 +8208,8 @@ class SQLExecutor {
|
|
|
8200
8208
|
}
|
|
8201
8209
|
if (expr.type === 'subquery') {
|
|
8202
8210
|
const res = await this.executeSelect(expr.select);
|
|
8211
|
+
// #258:标量子查询返回多行必须报 1242,不能静默取首行
|
|
8212
|
+
if (res.rows.length > 1) throw createError('ER_SUBSELECT_NO_1_ROW');
|
|
8203
8213
|
expr._value = res.rows.length > 0 ? res.rows[0][0] : null;
|
|
8204
8214
|
// 不改写类型、不 delete select:保留节点以便相关场景逐行重算
|
|
8205
8215
|
return;
|
|
@@ -8305,6 +8315,24 @@ class SQLExecutor {
|
|
|
8305
8315
|
const op = typeof column === 'string' ? { type: 'column', name: column } : column;
|
|
8306
8316
|
// COUNT(*) / COUNT():参数为 star 或缺失,直接按行数算
|
|
8307
8317
|
const noColumn = !op || op.type === 'star';
|
|
8318
|
+
// #293:JSON 聚合按整组求值,不能退化成每组取代表行
|
|
8319
|
+
if (fn === 'JSON_ARRAYAGG') {
|
|
8320
|
+
const out = rows.map(r => resolveOperand(op, r, this.ctx));
|
|
8321
|
+
return out;
|
|
8322
|
+
}
|
|
8323
|
+
if (fn === 'JSON_OBJECTAGG') {
|
|
8324
|
+
const obj = {};
|
|
8325
|
+
const items = op && op.type === 'arglist' ? op.items : [];
|
|
8326
|
+
if (items.length >= 2) {
|
|
8327
|
+
for (const r of rows) {
|
|
8328
|
+
const k = resolveOperand(items[0], r, this.ctx);
|
|
8329
|
+
const v = resolveOperand(items[1], r, this.ctx);
|
|
8330
|
+
if (k === null || k === undefined) continue;
|
|
8331
|
+
obj[String(k)] = v === undefined ? null : v;
|
|
8332
|
+
}
|
|
8333
|
+
}
|
|
8334
|
+
return obj;
|
|
8335
|
+
}
|
|
8308
8336
|
let values = noColumn
|
|
8309
8337
|
? rows.map(r => 1)
|
|
8310
8338
|
// Issue #114:VARIANCE(1,2,3) 这类多参数写法,所有参数值都纳入统计
|
|
@@ -8955,6 +8983,7 @@ class SQLExecutor {
|
|
|
8955
8983
|
node._rowCount = res.rows.length;
|
|
8956
8984
|
} else if (node.type === 'subquery') {
|
|
8957
8985
|
const res = await this.executeSelect(node.select);
|
|
8986
|
+
if (res.rows.length > 1) throw createError('ER_SUBSELECT_NO_1_ROW');
|
|
8958
8987
|
node._value = res.rows.length > 0 ? res.rows[0][0] : null;
|
|
8959
8988
|
} else if (node.type === 'in') {
|
|
8960
8989
|
const res = await this.executeSelect(node.subquery);
|
package/lib/tui.js
CHANGED
|
@@ -13,7 +13,8 @@ const os = require('os');
|
|
|
13
13
|
const Database = require('./database');
|
|
14
14
|
const { executeSQL } = require('./sql');
|
|
15
15
|
|
|
16
|
-
|
|
16
|
+
// #300:历史文件不再内嵌 pid,跨会话可读
|
|
17
|
+
const HISTORY_FILE = path.join(os.homedir(), '.jsql-history');
|
|
17
18
|
const MAX_HISTORY = 500;
|
|
18
19
|
|
|
19
20
|
/* ---------- ANSI helpers ---------- */
|
|
@@ -39,6 +40,10 @@ function wswidth(str) {
|
|
|
39
40
|
let w = 0;
|
|
40
41
|
for (const ch of String(str)) {
|
|
41
42
|
const cp = ch.codePointAt(0);
|
|
43
|
+
// #298:emoji / 宽符号按 2 列,组合符/变体选择符按 0 列
|
|
44
|
+
if (cp >= 0x1f000 && cp <= 0x1faff) { w += 2; continue; }
|
|
45
|
+
if (cp >= 0x2600 && cp <= 0x27bf) { w += 2; continue; }
|
|
46
|
+
if ((cp >= 0x0300 && cp <= 0x036f) || (cp >= 0xfe00 && cp <= 0xfe0f) || cp === 0x200d) { continue; }
|
|
42
47
|
if (cp >= 0x1100 && (cp <= 0x115f || cp === 0x2329 || cp === 0x232a ||
|
|
43
48
|
(cp >= 0x2e80 && cp <= 0xa4cf && cp !== 0x303f) ||
|
|
44
49
|
(cp >= 0xac00 && cp <= 0xd7a3) || (cp >= 0xf900 && cp <= 0xfaff) ||
|
|
@@ -297,6 +302,8 @@ class TUIShell {
|
|
|
297
302
|
let inSQ = false, inDQ = false, parens = 0;
|
|
298
303
|
for (let i = 0; i < sql.length; i++) {
|
|
299
304
|
const c = sql[i];
|
|
305
|
+
// #299:反斜杠转义下一个字符(\' / \"),不能当成引号翻转
|
|
306
|
+
if (c === '\\' && (inSQ || inDQ)) { i++; continue; }
|
|
300
307
|
if (c === "'" && !inDQ) inSQ = !inSQ;
|
|
301
308
|
else if (c === '"' && !inSQ) inDQ = !inDQ;
|
|
302
309
|
else if (!inSQ && !inDQ) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "jsql-neo",
|
|
3
|
-
"version": "6.8.
|
|
3
|
+
"version": "6.8.24",
|
|
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 && node test/issue-265-307.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 && node test/issue-258-300.test.js && node test/issue-247-295-277.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 && node test/issue-265-307.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 && node test/issue-258-300.test.js && node test/issue-247-295-277.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",
|
|
@@ -235,7 +235,7 @@ async function sqlCases() {
|
|
|
235
235
|
fs.writeFileSync(file, snap.slice(0, Math.floor(snap.length / 2)));
|
|
236
236
|
const orig = console.error; console.error = () => {};
|
|
237
237
|
let err = null, r = null;
|
|
238
|
-
try { const db2 = new Database(file); r = await rows(db2, 'SELECT id,v FROM t ORDER BY id'); db2.stop(); } catch (e) { err = e.message; }
|
|
238
|
+
try { const db2 = new Database(file, { fileLock: false }); r = await rows(db2, 'SELECT id,v FROM t ORDER BY id'); db2.stop(); } catch (e) { err = e.message; }
|
|
239
239
|
console.error = orig;
|
|
240
240
|
ok('#237 torn snapshot falls back to WAL', err === null && r.some(x => x[0] === 3), { err, r });
|
|
241
241
|
db.stop();
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Regression tests for #247 #295 #277.
|
|
3
|
+
*/
|
|
4
|
+
const path = require('path');
|
|
5
|
+
const os = require('os');
|
|
6
|
+
const fs = require('fs');
|
|
7
|
+
const ROOT = path.join(__dirname, '..');
|
|
8
|
+
const { Database } = require(path.join(ROOT, 'index.js'));
|
|
9
|
+
const pg = require(path.join(ROOT, 'lib', 'pg_server.js'));
|
|
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
|
+
|
|
16
|
+
(async () => {
|
|
17
|
+
// #247 WASM/JS 回退在 disk 模式下必须真正落盘
|
|
18
|
+
{
|
|
19
|
+
const native = require(path.join(ROOT, 'lib', 'native_client.js'));
|
|
20
|
+
if (native._DiskFallbackJSQL) {
|
|
21
|
+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'jsql-247-'));
|
|
22
|
+
const file = path.join(dir, 'db.jsql');
|
|
23
|
+
const db1 = new native._DiskFallbackJSQL({ path: file, mode: 'disk' });
|
|
24
|
+
await db1.start();
|
|
25
|
+
await db1.createTable('users', { id: 'INTEGER PRIMARY KEY', name: 'TEXT' });
|
|
26
|
+
await db1.insert('users', { id: 1, name: 'alice' });
|
|
27
|
+
await db1.stop();
|
|
28
|
+
const db2 = new native._DiskFallbackJSQL({ path: file, mode: 'disk' });
|
|
29
|
+
await db2.start();
|
|
30
|
+
const rows = await db2.find('users', {});
|
|
31
|
+
ok('#247 disk fallback persists across restart', rows.length === 1 && rows[0].name === 'alice', rows);
|
|
32
|
+
await db2.stop();
|
|
33
|
+
} else {
|
|
34
|
+
console.log('[SKIP] #247 native module present');
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
// #295 默认文件锁阻止同路径双实例
|
|
38
|
+
{
|
|
39
|
+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'jsql-295-'));
|
|
40
|
+
const file = path.join(dir, 'lock.jsql');
|
|
41
|
+
const db1 = new Database(file);
|
|
42
|
+
let code = null;
|
|
43
|
+
try { const db2 = new Database(file); db2.stop(); } catch (e) { code = e.code; }
|
|
44
|
+
ok('#295 second instance on same path is rejected', code === 1205 || code === 'ER_LOCK_WAIT_TIMEOUT', code);
|
|
45
|
+
await db1.stop();
|
|
46
|
+
const db3 = new Database(file);
|
|
47
|
+
ok('#295 lock released after stop', !!db3);
|
|
48
|
+
await db3.stop();
|
|
49
|
+
}
|
|
50
|
+
// #277 Bind 结果格式码 → RowDescription/DataRow 使用二进制
|
|
51
|
+
{
|
|
52
|
+
const desc = pg.buildRowDescription(['n'], ['integer'], [1]);
|
|
53
|
+
ok('#277 RowDescription format code = 1', desc.readInt16BE(desc.length - 2) === 1, desc.toString('hex'));
|
|
54
|
+
const row = pg.buildDataRow([5], ['integer'], [1]);
|
|
55
|
+
const payload = row.slice(5);
|
|
56
|
+
ok('#277 binary int4 DataRow', payload.readInt16BE(0) === 1 && payload.readInt32BE(2) === 4 && payload.readInt32BE(6) === 5, payload.toString('hex'));
|
|
57
|
+
const textRow = pg.buildDataRow([5], ['integer'], [0]);
|
|
58
|
+
ok('#277 text DataRow unchanged', textRow.slice(11).toString() === '5', textRow.toString('hex'));
|
|
59
|
+
const fmts = pg.PgConnection.prototype._columnFormats.call({}, 3, [1]);
|
|
60
|
+
ok('#277 single format applies to all columns', JSON.stringify(fmts) === '[1,1,1]', fmts);
|
|
61
|
+
const perCol = pg.PgConnection.prototype._columnFormats.call({}, 3, [0, 1]);
|
|
62
|
+
ok('#277 per-column format codes', JSON.stringify(perCol) === '[0,1,0]', perCol);
|
|
63
|
+
}
|
|
64
|
+
console.log(`\n${passed} passed, ${failed} failed`);
|
|
65
|
+
process.exit(failed === 0 ? 0 : 1);
|
|
66
|
+
})().catch((e) => { console.error('FATAL', e); process.exit(1); });
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Regression tests for #258 #293 #298 #299 #300.
|
|
3
|
+
* node test/issue-258-300.test.js
|
|
4
|
+
*/
|
|
5
|
+
const path = require('path');
|
|
6
|
+
const os = require('os');
|
|
7
|
+
const fs = require('fs');
|
|
8
|
+
const ROOT = path.join(__dirname, '..');
|
|
9
|
+
const { Database, executeSQL } = require(path.join(ROOT, 'index.js'));
|
|
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 q = (db, sql) => executeSQL(db, sql, { safety: false });
|
|
16
|
+
|
|
17
|
+
(async () => {
|
|
18
|
+
// #258
|
|
19
|
+
{
|
|
20
|
+
const db = new Database(':memory:');
|
|
21
|
+
await q(db, 'CREATE TABLE t (id INT)');
|
|
22
|
+
await q(db, 'INSERT INTO t VALUES (1),(2)');
|
|
23
|
+
let code = null;
|
|
24
|
+
try { await q(db, 'SELECT (SELECT id FROM t) AS x'); } catch (e) { code = e.code; }
|
|
25
|
+
ok('#258 scalar subquery with multiple rows -> 1242', code === 1242, code);
|
|
26
|
+
db.stop();
|
|
27
|
+
}
|
|
28
|
+
// #293
|
|
29
|
+
{
|
|
30
|
+
const db = new Database(':memory:');
|
|
31
|
+
await q(db, 'CREATE TABLE t (g TEXT, v INT)');
|
|
32
|
+
await q(db, "INSERT INTO t VALUES ('a',1),('a',2),('b',3)");
|
|
33
|
+
const arr = (await q(db, 'SELECT g, JSON_ARRAYAGG(v) AS j FROM t GROUP BY g ORDER BY g')).rows;
|
|
34
|
+
ok('#293 JSON_ARRAYAGG aggregates whole group', JSON.stringify(arr) === JSON.stringify([['a', [1, 2]], ['b', [3]]]), arr);
|
|
35
|
+
const obj = (await q(db, "SELECT JSON_OBJECTAGG(g, v) AS o FROM t")).rows;
|
|
36
|
+
ok('#293 JSON_OBJECTAGG builds object', obj.length === 1 && JSON.stringify(obj[0][0]) === JSON.stringify({ a: 2, b: 3 }), obj);
|
|
37
|
+
db.stop();
|
|
38
|
+
}
|
|
39
|
+
// #298 / #299
|
|
40
|
+
{
|
|
41
|
+
const tui = require(path.join(ROOT, 'lib', 'tui.js'));
|
|
42
|
+
ok('#298 emoji width counted as 2', tui.wswidth('😀') === 2 && tui.wswidth('abc😀') === 5, [tui.wswidth('😀'), tui.wswidth('abc😀')]);
|
|
43
|
+
const shell = Object.create(tui.TUIShell.prototype);
|
|
44
|
+
ok('#299 escaped quote keeps statement balanced', shell._statementComplete("SELECT 'a\\'b';") === true, shell._statementComplete("SELECT 'a\\'b';"));
|
|
45
|
+
}
|
|
46
|
+
// #300 history file has no pid
|
|
47
|
+
{
|
|
48
|
+
const home = fs.mkdtempSync(path.join(os.tmpdir(), 'jsql-home-'));
|
|
49
|
+
const oldHome = process.env.HOME;
|
|
50
|
+
process.env.HOME = home;
|
|
51
|
+
const tuiPath = path.join(ROOT, 'lib', 'tui.js');
|
|
52
|
+
delete require.cache[require.resolve(tuiPath)];
|
|
53
|
+
const tui = require(tuiPath);
|
|
54
|
+
const shell = Object.create(tui.TUIShell.prototype);
|
|
55
|
+
shell.history = ['SELECT 1;'];
|
|
56
|
+
shell._saveHistory();
|
|
57
|
+
const files = fs.readdirSync(home);
|
|
58
|
+
ok('#300 history file is session-independent', files.includes('.jsql-history'), files);
|
|
59
|
+
process.env.HOME = oldHome;
|
|
60
|
+
delete require.cache[require.resolve(tuiPath)];
|
|
61
|
+
}
|
|
62
|
+
console.log(`\n${passed} passed, ${failed} failed`);
|
|
63
|
+
process.exit(failed === 0 ? 0 : 1);
|
|
64
|
+
})().catch((e) => { console.error('FATAL', e); process.exit(1); });
|
package/test/issue-38.test.js
CHANGED
|
@@ -170,6 +170,7 @@ async function testPersistence() {
|
|
|
170
170
|
db.big.insert({ v: 9223372036854775807n });
|
|
171
171
|
db.big.insert({ v: -9223372036854775808n });
|
|
172
172
|
db.save();
|
|
173
|
+
await db.stop();
|
|
173
174
|
|
|
174
175
|
db = new Database(p);
|
|
175
176
|
const rows = db.big.find({}).sort((a, b) => (a.v < b.v ? -1 : 1));
|
|
@@ -187,6 +188,7 @@ async function testPersistence() {
|
|
|
187
188
|
});
|
|
188
189
|
db.big.insert({ v: '9007199254740993' });
|
|
189
190
|
db.save();
|
|
191
|
+
await db.stop();
|
|
190
192
|
|
|
191
193
|
db = new Database(p);
|
|
192
194
|
ok('#38 JSON persistence keeps >2^53 exactly', db.big.find({})[0].v === 9007199254740993n, db.big.find({}));
|
|
@@ -203,6 +205,7 @@ async function testPersistence() {
|
|
|
203
205
|
});
|
|
204
206
|
db.big.insert({ v: 9007199254740993n });
|
|
205
207
|
db.save();
|
|
208
|
+
await db.stop();
|
|
206
209
|
|
|
207
210
|
db = new Database(dir, { mode: 'disk' });
|
|
208
211
|
ok('#38 dir-mode persistence keeps >2^53 exactly', db.big.find({})[0].v === 9007199254740993n, db.big.find({}));
|
|
@@ -44,7 +44,7 @@ const SCHEMA = { id: { type: 'integer', primaryKey: true }, v: { type: 'string'
|
|
|
44
44
|
await db.insert('t', { id: 3, v: 'third' });
|
|
45
45
|
await db.removeById('t', 3);
|
|
46
46
|
// 模拟 kill -9:直接丢弃,不 save 不 stop
|
|
47
|
-
const db2 = new Database(file, { wal: true });
|
|
47
|
+
const db2 = new Database(file, { wal: true, fileLock: false });
|
|
48
48
|
const rows = await db2.find('t', {});
|
|
49
49
|
ok('崩溃后表仍存在', Object.keys(db2._tables).includes('t'));
|
|
50
50
|
ok('崩溃后行数正确 (2)', rows.length === 2, rows.length);
|
|
@@ -65,7 +65,7 @@ const SCHEMA = { id: { type: 'integer', primaryKey: true }, v: { type: 'string'
|
|
|
65
65
|
db.save(); // 快照:2 行,WAL 应清空
|
|
66
66
|
ok('save 后 WAL 被清空', !fs.existsSync(file + '.wal'));
|
|
67
67
|
await db.insert('t', { id: 3, v: 'c-after-save' }); // 只进 WAL
|
|
68
|
-
const db2 = new Database(file, { wal: true });
|
|
68
|
+
const db2 = new Database(file, { wal: true, fileLock: false });
|
|
69
69
|
const rows = await db2.find('t', {});
|
|
70
70
|
ok('快照 2 行 + WAL 1 行 = 3 行', rows.length === 3, rows.length);
|
|
71
71
|
ok('WAL 中的新行已恢复', rows.find(r => r.id === 3)?.v === 'c-after-save');
|
|
@@ -79,9 +79,9 @@ const SCHEMA = { id: { type: 'integer', primaryKey: true }, v: { type: 'string'
|
|
|
79
79
|
await db.createTable('t', SCHEMA);
|
|
80
80
|
await db.insert('t', { id: 1, v: 'x' });
|
|
81
81
|
await db.insert('t', { id: 2, v: 'y' });
|
|
82
|
-
const db2 = new Database(file, { wal: true });
|
|
82
|
+
const db2 = new Database(file, { wal: true, fileLock: false });
|
|
83
83
|
const r2 = await db2.find('t', {});
|
|
84
|
-
const db3 = new Database(file, { wal: true });
|
|
84
|
+
const db3 = new Database(file, { wal: true, fileLock: false });
|
|
85
85
|
const r3 = await db3.find('t', {});
|
|
86
86
|
ok('首次恢复 2 行', r2.length === 2, r2.length);
|
|
87
87
|
ok('二次重开仍 2 行(不重复回放)', r3.length === 2, r3.length);
|
|
@@ -96,7 +96,7 @@ const SCHEMA = { id: { type: 'integer', primaryKey: true }, v: { type: 'string'
|
|
|
96
96
|
await db.insert('t', { id: 1, v: 'ok' });
|
|
97
97
|
// 模拟崩溃时最后一行只写了一半
|
|
98
98
|
fs.appendFileSync(file + '.wal', '{"op":"insert","table":"t","ro');
|
|
99
|
-
const db2 = new Database(file, { wal: true });
|
|
99
|
+
const db2 = new Database(file, { wal: true, fileLock: false });
|
|
100
100
|
const rows = await db2.find('t', {});
|
|
101
101
|
ok('半截尾行被丢弃,完整记录仍恢复', rows.length === 1, rows.length);
|
|
102
102
|
ok('数据内容正确', rows[0] && rows[0].v === 'ok', rows);
|
|
@@ -112,7 +112,7 @@ const SCHEMA = { id: { type: 'integer', primaryKey: true }, v: { type: 'string'
|
|
|
112
112
|
await db.insert('t', { id: 1, v: 'a' });
|
|
113
113
|
await db.insert('u', { id: 1, v: 'b' });
|
|
114
114
|
await db.dropTable('u');
|
|
115
|
-
const db2 = new Database(file, { wal: true });
|
|
115
|
+
const db2 = new Database(file, { wal: true, fileLock: false });
|
|
116
116
|
ok('被 drop 的表未复活', !db2._tables.u, Object.keys(db2._tables));
|
|
117
117
|
ok('保留的表数据完整', (await db2.find('t', {})).length === 1);
|
|
118
118
|
fs.rmSync(dir, { recursive: true, force: true });
|
|
@@ -135,7 +135,7 @@ const SCHEMA = { id: { type: 'integer', primaryKey: true }, v: { type: 'string'
|
|
|
135
135
|
fs.writeFileSync(file + '.wal', JSON.stringify([
|
|
136
136
|
{ op: 'createTable', table: 'legacy', schema: SCHEMA },
|
|
137
137
|
]));
|
|
138
|
-
const db = new Database(file, { wal: true });
|
|
138
|
+
const db = new Database(file, { wal: true, fileLock: false });
|
|
139
139
|
ok('旧格式数组 WAL 能被回放', !!db._tables.legacy, Object.keys(db._tables));
|
|
140
140
|
fs.rmSync(dir, { recursive: true, force: true });
|
|
141
141
|
}
|
|
@@ -148,7 +148,7 @@ const SCHEMA = { id: { type: 'integer', primaryKey: true }, v: { type: 'string'
|
|
|
148
148
|
db.createTable && 0;
|
|
149
149
|
for (let i = 1; i <= 30; i++) await db.insert('t', { id: i, v: 'v' + i, n: i });
|
|
150
150
|
db._tables.t.createIndex('n');
|
|
151
|
-
const db2 = new Database(file, { wal: true });
|
|
151
|
+
const db2 = new Database(file, { wal: true, fileLock: false });
|
|
152
152
|
const found = await db2.find('t', { n: 17 });
|
|
153
153
|
ok('回放后按索引字段查询正确', found.length === 1 && found[0].id === 17, found);
|
|
154
154
|
const cnt = await db2.find('t', { n: { $gte: 25 } });
|
|
@@ -170,7 +170,7 @@ const SCHEMA = { id: { type: 'integer', primaryKey: true }, v: { type: 'string'
|
|
|
170
170
|
await db.delete('np', { x: 'n1' });
|
|
171
171
|
ok('写入后立即产生 .wal', fs.existsSync(file + '.wal'));
|
|
172
172
|
|
|
173
|
-
const db2 = new Database(file); // 不传 wal,模拟进程崩溃后重启
|
|
173
|
+
const db2 = new Database(file, { fileLock: false }); // 不传 wal,模拟进程崩溃后重启
|
|
174
174
|
const rows = await db2.find('t', {});
|
|
175
175
|
const npRows = await db2.find('np', {});
|
|
176
176
|
ok('默认 WAL 恢复主键表', rows.length === 1 && rows[0].v === 'a2', rows);
|