jsql-neo 6.5.32 → 6.6.1
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/date-types.js +127 -58
- package/lib/errors.js +2 -0
- package/lib/nedb_compat.js +14 -1
- package/lib/plugin.js +10 -6
- package/lib/sql.js +569 -123
- package/lib/table.js +37 -5
- package/package.json +3 -3
- package/postinstall.js +6 -4
- package/test/issue-50.test.js +37 -24
- package/test/issue-71-72.test.js +137 -0
- package/test/issue-73-76.test.js +254 -0
- package/test/issue-77-84.test.js +137 -0
- package/test/query-opt.test.js +10 -8
- package/test/readme-audit.test.js +8 -8
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
// 回归测试:Issue #77-#84
|
|
2
|
+
// #77 WHERE on BIGINT 用字典序字符串比较导致错误结果
|
|
3
|
+
// #78 UNION/INTERSECT/EXCEPT 遇 BIGINT 列 TypeError
|
|
4
|
+
// #79 BEFORE DELETE trigger 在 removeById() 上不触发
|
|
5
|
+
// #80 XOR 违反 SQL 三值逻辑(UNKNOWN 塌缩成 FALSE)
|
|
6
|
+
// #81 NeDB-compat $pull 是 no-op(硬编码键 "k")
|
|
7
|
+
// #82 DATETIME 静默丢弃亚秒精度
|
|
8
|
+
// #83 BOOLEAN 列与数字字符串字面量 '1' 比较返回空
|
|
9
|
+
// #84 插件 priority 被忽略(钩子按注册顺序执行)
|
|
10
|
+
const { executeSQL } = require('../lib/sql');
|
|
11
|
+
const Database = require('../lib/database');
|
|
12
|
+
const { Datastore } = require('../lib/nedb_compat');
|
|
13
|
+
const dateTypes = require('../lib/date-types');
|
|
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, 300) : ''); }
|
|
19
|
+
}
|
|
20
|
+
const engine = (schema, rows) => ({
|
|
21
|
+
hasTable: () => true,
|
|
22
|
+
find: async () => rows,
|
|
23
|
+
getTableSchema: async () => schema,
|
|
24
|
+
truncate: async () => {}, flush: async () => {},
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
(async () => {
|
|
28
|
+
// #77 BIGINT WHERE 比较
|
|
29
|
+
{
|
|
30
|
+
const r = await executeSQL(engine(
|
|
31
|
+
{ id: { type: 'integer', primaryKey: true }, big: { type: 'bigint' } },
|
|
32
|
+
[{ id: 1, big: 50n }, { id: 2, big: 2000n }]
|
|
33
|
+
), 'SELECT id FROM t WHERE big > 1000', { safety: false });
|
|
34
|
+
ok('#77 bigint > 1000 只返回 id=2', r.rows.map(x => x[0]).join(',') === '2', r.rows);
|
|
35
|
+
}
|
|
36
|
+
{
|
|
37
|
+
const r = await executeSQL(engine(
|
|
38
|
+
{ id: { type: 'integer', primaryKey: true }, big: { type: 'bigint' } },
|
|
39
|
+
[{ id: 1, big: 50n }, { id: 2, big: 2000n }]
|
|
40
|
+
), 'SELECT id FROM t WHERE big = 50', { safety: false });
|
|
41
|
+
ok('#77 bigint = 50 数值相等', r.rows.map(x => x[0]).join(',') === '1', r.rows);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// #78 UNION / INTERSECT / EXCEPT on bigint
|
|
45
|
+
{
|
|
46
|
+
const r = await executeSQL(engine(
|
|
47
|
+
{ big: { type: 'bigint' } },
|
|
48
|
+
[{ big: 50n }, { big: 2000n }]
|
|
49
|
+
), 'SELECT big FROM t UNION SELECT big FROM t', { safety: false });
|
|
50
|
+
ok('#78 UNION bigint 不抛错且去重', Array.isArray(r.rows) && r.rows.length === 2, r.rows);
|
|
51
|
+
}
|
|
52
|
+
{
|
|
53
|
+
const r = await executeSQL(engine(
|
|
54
|
+
{ big: { type: 'bigint' } },
|
|
55
|
+
[{ big: 50n }]
|
|
56
|
+
), 'SELECT big FROM t INTERSECT SELECT big FROM t', { safety: false });
|
|
57
|
+
ok('#78 INTERSECT bigint 不抛错', Array.isArray(r.rows) && r.rows.length === 1, r.rows);
|
|
58
|
+
}
|
|
59
|
+
{
|
|
60
|
+
const r = await executeSQL(engine(
|
|
61
|
+
{ big: { type: 'bigint' } },
|
|
62
|
+
[{ big: 50n }, { big: 2000n }]
|
|
63
|
+
), 'SELECT big FROM t EXCEPT SELECT big FROM t WHERE big = 50', { safety: false });
|
|
64
|
+
ok('#78 EXCEPT bigint 不抛错', Array.isArray(r.rows), r.rows);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// #79 BEFORE DELETE on removeById
|
|
68
|
+
{
|
|
69
|
+
const db = new Database(':memory:');
|
|
70
|
+
await db.createTable('t', { id: { type: 'integer', primaryKey: true } });
|
|
71
|
+
let fired = 0;
|
|
72
|
+
db.createTrigger('tr', { event: 'remove', table: 't', timing: 'before' }, () => { fired++; });
|
|
73
|
+
await db.insert('t', { id: 1 });
|
|
74
|
+
await db.removeById('t', 1);
|
|
75
|
+
ok('#79 removeById 触发 before remove', fired === 1, { fired });
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// #80 XOR 三值逻辑
|
|
79
|
+
{
|
|
80
|
+
const r = await executeSQL(engine(
|
|
81
|
+
{ id: { type: 'integer', primaryKey: true }, a: { type: 'integer' } },
|
|
82
|
+
[{ id: 1, a: null }, { id: 2, a: 1 }, { id: 3, a: 2 }]
|
|
83
|
+
), 'SELECT id FROM t WHERE TRUE XOR (a = 1)', { safety: false });
|
|
84
|
+
ok('#80 TRUE XOR (a=1) 只返回 id=3(UNKNOWN 不塌缩)', r.rows.map(x => x[0]).join(',') === '3', r.rows);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// #81 $pull
|
|
88
|
+
{
|
|
89
|
+
const ds = new Datastore({ inMemoryOnly: true });
|
|
90
|
+
await ds.loadDatabase();
|
|
91
|
+
const doc = await ds.insert({ tags: ['red', 'green', 'blue'] });
|
|
92
|
+
await ds.update({ _id: doc._id }, { $pull: { tags: 'red' } });
|
|
93
|
+
const after = await ds.findOne({ _id: doc._id });
|
|
94
|
+
ok('#81 $pull 移除匹配元素', after.tags.join(',') === 'green,blue', after.tags);
|
|
95
|
+
}
|
|
96
|
+
{
|
|
97
|
+
const ds = new Datastore({ inMemoryOnly: true });
|
|
98
|
+
await ds.loadDatabase();
|
|
99
|
+
const doc = await ds.insert({ items: [{ n: 1 }, { n: 2 }, { n: 3 }] });
|
|
100
|
+
await ds.update({ _id: doc._id }, { $pull: { items: { n: 2 } } });
|
|
101
|
+
const after = await ds.findOne({ _id: doc._id });
|
|
102
|
+
ok('#81 $pull 对象数组按字段匹配', after.items.length === 2 && after.items[1].n === 3, after.items);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// #82 DATETIME 亚秒
|
|
106
|
+
{
|
|
107
|
+
const out = dateTypes.validateDateType('2024-01-15T10:30:00.500Z', 'datetime', 'f');
|
|
108
|
+
ok('#82 保留 .500 亚秒', out === '2024-01-15 10:30:00.500', out);
|
|
109
|
+
}
|
|
110
|
+
{
|
|
111
|
+
const out = dateTypes.validateDateType('2024-01-15T10:30:00Z', 'datetime', 'f');
|
|
112
|
+
ok('#82 整秒不带 .000', out === '2024-01-15 10:30:00', out);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// #83 BOOLEAN vs '1'
|
|
116
|
+
{
|
|
117
|
+
const r = await executeSQL(engine(
|
|
118
|
+
{ id: { type: 'integer', primaryKey: true }, b: { type: 'boolean' } },
|
|
119
|
+
[{ id: 1, b: true }, { id: 2, b: false }]
|
|
120
|
+
), "SELECT id FROM t WHERE b = '1'", { safety: false });
|
|
121
|
+
ok('#83 b = \'1\' 返回 id=1', r.rows.map(x => x[0]).join(',') === '1', r.rows);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// #84 plugin priority
|
|
125
|
+
{
|
|
126
|
+
const db = new Database(':memory:');
|
|
127
|
+
await db.createTable('t', { id: { type: 'integer', primaryKey: true } });
|
|
128
|
+
const order = [];
|
|
129
|
+
await db.use({ name: 'low', priority: 1, hooks: { beforeInsert: () => order.push('low') } });
|
|
130
|
+
await db.use({ name: 'high', priority: 10, hooks: { beforeInsert: () => order.push('high') } });
|
|
131
|
+
await db.insert('t', { id: 1 });
|
|
132
|
+
ok('#84 高 priority 先执行', order.join(',') === 'high,low', order);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
console.log(`\n${pass} passed, ${fail} failed`);
|
|
136
|
+
process.exit(fail === 0 ? 0 : 1);
|
|
137
|
+
})().catch(e => { console.error('FATAL', e); process.exit(1); });
|
package/test/query-opt.test.js
CHANGED
|
@@ -108,17 +108,19 @@ function ok(name, cond, extra) {
|
|
|
108
108
|
|
|
109
109
|
/* ============ BUG 修复: now() 时区 / 监控定时器 / insertMany 归一 & 不污染 ============ */
|
|
110
110
|
{
|
|
111
|
-
// BUG-1
|
|
111
|
+
// BUG-1(6.6.0 语义修订):now() 与 validateDate/DateTime 必须以同一基准输出。
|
|
112
|
+
// 引擎已改为 **UTC 稳定基准**(字面量也是 UTC),因此 now() 返回 UTC 墙上时间;
|
|
113
|
+
// 旧断言要求「本地时区」是 6.5.x 及以前的语义,会把落库值锁死在部署主机时区上。
|
|
112
114
|
const savedTZ = process.env.TZ;
|
|
113
115
|
process.env.TZ = 'Asia/Shanghai';
|
|
114
116
|
try {
|
|
115
117
|
const d = new Date();
|
|
116
118
|
const p2 = n => String(n).padStart(2, '0');
|
|
117
|
-
const
|
|
118
|
-
const
|
|
119
|
-
ok('BUG-1 now("date")
|
|
120
|
-
ok('BUG-1 now("time")
|
|
121
|
-
ok('BUG-1 now()
|
|
119
|
+
const utcDate = `${d.getUTCFullYear()}-${p2(d.getUTCMonth() + 1)}-${p2(d.getUTCDate())}`;
|
|
120
|
+
const utcTime = `${p2(d.getUTCHours())}:${p2(d.getUTCMinutes())}:${p2(d.getUTCSeconds())}`;
|
|
121
|
+
ok('BUG-1 now("date") UTC 基准', now('date') === utcDate, { got: now('date'), want: utcDate });
|
|
122
|
+
ok('BUG-1 now("time") UTC 基准', now('time') === utcTime, { got: now('time'), want: utcTime });
|
|
123
|
+
ok('BUG-1 now() UTC 基准', now() === `${utcDate} ${utcTime}`, { got: now(), want: `${utcDate} ${utcTime}` });
|
|
122
124
|
} finally {
|
|
123
125
|
if (savedTZ === undefined) delete process.env.TZ; else process.env.TZ = savedTZ;
|
|
124
126
|
}
|
|
@@ -149,8 +151,8 @@ function ok(name, cond, extra) {
|
|
|
149
151
|
const stored = tdb.t.find({})[0];
|
|
150
152
|
ok('BUG-8 insertMany 归一年龄为 number', typeof stored.age === 'number' && stored.age === 25, stored.age);
|
|
151
153
|
const p2 = n => String(n).padStart(2, '0');
|
|
152
|
-
const wantDate = `${srcDate.
|
|
153
|
-
ok('BUG-8 insertMany
|
|
154
|
+
const wantDate = `${srcDate.getUTCFullYear()}-${p2(srcDate.getUTCMonth() + 1)}-${p2(srcDate.getUTCDate())}`;
|
|
155
|
+
ok('BUG-8 insertMany 归一日期为 UTC 字符串', stored.d === wantDate, { got: stored.d, want: wantDate });
|
|
154
156
|
|
|
155
157
|
// BUG-4: information_schema 过滤条件必须透传 ctx(@@会话变量)
|
|
156
158
|
const idb = new Database(':memory:', { autoSave: false });
|
|
@@ -441,7 +441,7 @@ async function main() {
|
|
|
441
441
|
}
|
|
442
442
|
}
|
|
443
443
|
|
|
444
|
-
console.log('\n--- 6.2.0 回归:JOIN 一对多 / NOW()
|
|
444
|
+
console.log('\n--- 6.2.0 回归:JOIN 一对多 / NOW() 用 UTC 基准(6.6.0 起)---');
|
|
445
445
|
{
|
|
446
446
|
let JSQL, db;
|
|
447
447
|
try {
|
|
@@ -461,10 +461,10 @@ async function main() {
|
|
|
461
461
|
ok('JOIN 一对多返回 3 行', j.rows.length === 3, j.rows);
|
|
462
462
|
|
|
463
463
|
const n = await run('SELECT NOW() AS d');
|
|
464
|
-
const
|
|
464
|
+
const nowUtc = new Date();
|
|
465
465
|
const p = (x) => String(x).padStart(2, '0');
|
|
466
|
-
const expectYmd =
|
|
467
|
-
ok('NOW()
|
|
466
|
+
const expectYmd = nowUtc.getUTCFullYear() + '-' + p(nowUtc.getUTCMonth() + 1) + '-' + p(nowUtc.getUTCDate());
|
|
467
|
+
ok('NOW() 返回 UTC 日期(与落库基准一致)', String(n.rows[0][0]).slice(0, 10) === expectYmd, n.rows);
|
|
468
468
|
await db.stop();
|
|
469
469
|
}
|
|
470
470
|
}
|
|
@@ -496,13 +496,13 @@ async function main() {
|
|
|
496
496
|
ok('NOT EXISTS 相关子查询',
|
|
497
497
|
JSON.stringify((await run('SELECT id FROM ta WHERE NOT EXISTS (SELECT 1 FROM tb WHERE tb.av = ta.v)')).rows) === '[[2]]');
|
|
498
498
|
|
|
499
|
-
// DATE
|
|
499
|
+
// DATE 序列化基准:6.6.0 起统一 UTC(无时区字面量即 UTC 墙上时间)
|
|
500
500
|
const { validateDateType } = require('../lib/date-types.js');
|
|
501
501
|
ok('DATE 字符串不回退一天', validateDateType('2023-01-01', 'date', 'f') === '2023-01-01');
|
|
502
502
|
ok('DATE 闰年边界(2024-02-29)', validateDateType('2024-02-29', 'date', 'f') === '2024-02-29');
|
|
503
|
-
ok('DATE
|
|
504
|
-
ok('DATETIME
|
|
505
|
-
validateDateType(new Date(2023, 0, 1, 8, 30), 'datetime', 'f') === '2023-01-01 08:30:00');
|
|
503
|
+
ok('DATE 对象按 UTC', validateDateType(new Date(Date.UTC(2023, 0, 1)), 'date', 'f') === '2023-01-01');
|
|
504
|
+
ok('DATETIME 对象按 UTC',
|
|
505
|
+
validateDateType(new Date(Date.UTC(2023, 0, 1, 8, 30)), 'datetime', 'f') === '2023-01-01 08:30:00');
|
|
506
506
|
await db.stop();
|
|
507
507
|
}
|
|
508
508
|
}
|