jsql-neo 6.8.25 → 6.8.26

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.
@@ -39,7 +39,8 @@ function ok(name, cond, extra) {
39
39
  const { createMysqlServer } = require(path.join(ROOT, 'lib', 'mysql_server.js'));
40
40
  const srv = createMysqlServer({ port: 0, host: '127.0.0.1', dataDir: ':memory:', noAuth: true });
41
41
  await new Promise((r) => srv.listen(r));
42
- const conn = await mysql.createConnection({ host: '127.0.0.1', port: srv.address.port, user: 'root', database: 'app' });
42
+ const conn = await mysql.createConnection({ host: '127.0.0.1', port: srv.address.port, user: 'root' });
43
+ await conn.query('CREATE DATABASE IF NOT EXISTS app'); await conn.query('USE app');
43
44
  await conn.query('CREATE TABLE t (id INT)');
44
45
  const [, fields] = await conn.query('SELECT id FROM t');
45
46
  ok('#306 ColumnDefinition schema is database name', fields[0].schema === 'app' && fields[0].table === 't', { schema: fields[0].schema, table: fields[0].table });
@@ -0,0 +1,133 @@
1
+ /*
2
+ * Regression tests for #327 #357 #359 #418 #406.
3
+ *
4
+ * 口径:全部走公共 API(require('../index.js') 导出的 JSQL / Database),
5
+ * 不直接断言私有类(#247 假绿前车之鉴)。
6
+ */
7
+ const fs = require('fs');
8
+ const os = require('os');
9
+ const path = require('path');
10
+ const assert = require('assert');
11
+ const { JSQL, Database } = require('../index.js');
12
+ const { xql } = require('../lib/xql');
13
+
14
+ let passed = 0, failed = 0;
15
+ async function t(name, fn) {
16
+ try { await fn(); passed++; console.log(`ok - ${name}`); }
17
+ catch (e) { failed++; console.error(`not ok - ${name}\n ${e && e.message}`); }
18
+ }
19
+
20
+ (async () => {
21
+ // #327(P0):公共导出的 JSQL 在 disk 模式下必须真正落盘,重开能读回表与行。
22
+ // 此前 index.js 的 JSQL 挂的是纯内存 WASM 实现,path/mode 被静默丢弃。
23
+ await t('#327 公共 JSQL disk 模式落盘并重开读回', async () => {
24
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'jsql-327-'));
25
+ const db1 = new JSQL({ path: dir, mode: 'disk' });
26
+ await db1.start();
27
+ await db1.createTable('p', { name: { type: 'string' } });
28
+ await db1.insert('p', { name: 'x' });
29
+ await db1.stop();
30
+
31
+ const files = fs.readdirSync(dir);
32
+ assert.ok(files.length > 0, `落盘目录应为非空,实际 ${JSON.stringify(files)}`);
33
+
34
+ const db2 = new JSQL({ path: dir, mode: 'disk' });
35
+ await db2.start();
36
+ assert.deepStrictEqual(await db2.getTables(), ['p']);
37
+ const rows = await db2.find('p', {});
38
+ assert.strictEqual(rows.length, 1);
39
+ assert.strictEqual(rows[0].name, 'x');
40
+ await db2.stop();
41
+ fs.rmSync(dir, { recursive: true, force: true });
42
+ });
43
+
44
+ // #357:RENAME COLUMN 后列 DEFAULT 必须随列迁移,新行拿到默认值而非 NULL。
45
+ await t('#357 RENAME COLUMN 后 DEFAULT 随列迁移', async () => {
46
+ const db = new Database(':memory:');
47
+ await db.query("CREATE TABLE u(id INT PRIMARY KEY, name TEXT DEFAULT 'anon')", { safety: false });
48
+ await db.query("INSERT INTO u VALUES (1,'x')", { safety: false });
49
+ await db.query('ALTER TABLE u RENAME COLUMN name TO username', { safety: false });
50
+ await db.query('INSERT INTO u(id) VALUES (2)', { safety: false });
51
+ const rows = (await db.query('SELECT id,username FROM u ORDER BY id', { safety: false })).rows;
52
+ assert.deepStrictEqual(rows, [[1, 'x'], [2, 'anon']]);
53
+ assert.deepStrictEqual(db._tables.u._defaults, { username: 'anon' });
54
+ await db.close();
55
+ });
56
+
57
+ // #359:MODIFY COLUMN 去掉 CHECK 后旧约束登记必须清除,-5 可写入;
58
+ // 对照:仍带 CHECK 的表插入 -5 必须被拒。
59
+ await t('#359 MODIFY COLUMN 清除旧 CHECK 后 -5 可写入', async () => {
60
+ const db = new Database(':memory:');
61
+ await db.query('CREATE TABLE t(id INT PRIMARY KEY, age INT CHECK (age >= 0))', { safety: false });
62
+ await db.query('INSERT INTO t VALUES (1,10)', { safety: false });
63
+ await db.query('ALTER TABLE t MODIFY age TEXT', { safety: false });
64
+ assert.deepStrictEqual(db._tables.t._checkConstraints.map(c => c.field), []);
65
+ await db.query('INSERT INTO t(id,age) VALUES (2,-5)', { safety: false });
66
+ const rows = (await db.query('SELECT id,age FROM t ORDER BY id', { safety: false })).rows;
67
+ assert.strictEqual(rows.length, 2);
68
+ assert.strictEqual(Number(rows[1][1]), -5);
69
+ await db.close();
70
+
71
+ const db2 = new Database(':memory:');
72
+ await db2.query('CREATE TABLE g(id INT PRIMARY KEY, v INT CHECK (v >= 0))', { safety: false });
73
+ await assert.rejects(
74
+ () => db2.query('INSERT INTO g VALUES (2,-5)', { safety: false }),
75
+ /Check constraint/);
76
+ await db2.close();
77
+ });
78
+
79
+ // #418:updateByIds 必须同步主键/唯一索引,并拦截 NOT NULL 与唯一冲突。
80
+ await t('#418 updateByIds 同步索引并拦截 NULL/唯一冲突', async () => {
81
+ const db = new Database(':memory:');
82
+ await db.createTable('t', {
83
+ id: { type: 'integer', primaryKey: true },
84
+ name: { type: 'string', required: true },
85
+ });
86
+ await db.insert('t', { id: 1, name: 'a' });
87
+ await db.insert('t', { id: 2, name: 'b' });
88
+
89
+ // 批量改主键:旧键必须摘出、新键必须入索引
90
+ await db.updateByIds('t', [[1, { id: 10, name: 'a' }]]);
91
+ const tbl = db._tables.t;
92
+ assert.deepStrictEqual([...tbl._pkIndex], [[2, 1], [10, 0]]);
93
+ assert.strictEqual((await db.findById('t', 10)).name, 'a');
94
+
95
+ // required 列写 NULL 必须被拦截(NOT NULL 校验)
96
+ await assert.rejects(
97
+ () => db.updateByIds('t', [[2, { name: null }]]),
98
+ /cannot be null|NULL/i);
99
+
100
+ // 主键改成已存在的值必须被拦截(唯一冲突)
101
+ await assert.rejects(
102
+ () => db.updateByIds('t', [[10, { id: 2 }]]),
103
+ /Duplicate entry|DUP_ENTRY|UNIQUE/i);
104
+ await db.close();
105
+ });
106
+
107
+ // #406:同名集合重载并重建索引后,全局编译缓存不得复用旧索引闭包,范围查询不漏行。
108
+ await t('#406 XQL 同名集合重载后范围查询不漏行', async () => {
109
+ xql.clearCache();
110
+ const mk = (hiFirst) => {
111
+ const r = [];
112
+ for (let i = 0; i < 10; i++) r.push({ age: 5 });
113
+ if (hiFirst) r[0] = { age: 30 }; else r[9] = { age: 30 };
114
+ return r;
115
+ };
116
+ xql.load('users', mk(false));
117
+ xql.index('users', 'age', 'range');
118
+ const A = xql('FROM users WHERE age > 18');
119
+ assert.strictEqual(A.length, 1);
120
+ assert.strictEqual(A[0].age, 30);
121
+
122
+ // 同名重载为新集合(行序反转),重建索引;全局缓存必须按索引身份区分
123
+ xql.load('users', mk(true));
124
+ xql.index('users', 'age', 'range');
125
+ const B = xql('FROM users WHERE age > 18');
126
+ assert.strictEqual(B.length, 1);
127
+ assert.strictEqual(B[0].age, 30);
128
+ xql.clearCache();
129
+ });
130
+
131
+ console.log(`\n${passed} passed, ${failed} failed`);
132
+ process.exit(failed ? 1 : 0);
133
+ })().catch((e) => { console.error('FATAL', e); process.exit(1); });
@@ -0,0 +1,99 @@
1
+ /*
2
+ * Regression tests for #328 #329 #339 #343 #346.
3
+ */
4
+ const net = require('net');
5
+ const path = require('path');
6
+ const ROOT = path.join(__dirname, '..');
7
+ const { Database, createMongoServer } = require(path.join(ROOT, 'index.js'));
8
+ const { bsonDocument, bsonReadDocument } = require(path.join(ROOT, 'lib', 'mongo_server.js'));
9
+ const SqliteCompat = require(path.join(ROOT, 'lib', 'sqlite_compat.js'));
10
+
11
+ let passed = 0, failed = 0;
12
+ function ok(name, cond, extra) {
13
+ if (cond) { passed++; console.log('[OK]', name); }
14
+ else { failed++; console.log('[FAIL]', name, extra !== undefined ? '→ ' + JSON.stringify(extra) : ''); }
15
+ }
16
+
17
+ function opMsg(doc) {
18
+ const p = bsonDocument(doc), h = Buffer.alloc(16);
19
+ h.writeInt32LE(21 + p.length, 0); h.writeInt32LE(1, 4); h.writeInt32LE(0, 8); h.writeInt32LE(2013, 12);
20
+ return Buffer.concat([h, Buffer.alloc(4), Buffer.from([0]), p]);
21
+ }
22
+
23
+ (async () => {
24
+ // #343 NTILE:前 len % n 桶各多一行,n > len 时按 1..len 顺序派桶
25
+ {
26
+ const db = new Database(':memory:');
27
+ const rows = async (sql) => (await db.query(sql, { safety: false })).rows;
28
+ await rows('CREATE TABLE nt (id INT PRIMARY KEY)');
29
+ for (let i = 1; i <= 5; i++) await db.insert('nt', { id: i });
30
+ ok('#343 NTILE(10) 5 行不跳号',
31
+ JSON.stringify(await rows('SELECT NTILE(10) OVER (ORDER BY id) b FROM nt ORDER BY id')) === '[[1],[2],[3],[4],[5]]',
32
+ await rows('SELECT NTILE(10) OVER (ORDER BY id) b FROM nt ORDER BY id'));
33
+ ok('#343 NTILE(2) 5 行 = 1,1,1,2,2',
34
+ JSON.stringify(await rows('SELECT NTILE(2) OVER (ORDER BY id) b FROM nt ORDER BY id')) === '[[1],[1],[1],[2],[2]]');
35
+ await db.insert('nt', { id: 6 }); await db.insert('nt', { id: 7 });
36
+ ok('#343 NTILE(3) 7 行 = 1,1,1,2,2,3,3',
37
+ JSON.stringify(await rows('SELECT NTILE(3) OVER (ORDER BY id) b FROM nt ORDER BY id')) === '[[1],[1],[1],[2],[2],[3],[3]]');
38
+ await db.close();
39
+ }
40
+
41
+ // #346 反引号标识符内的 `` 是转义单反引号
42
+ {
43
+ const db = new Database(':memory:');
44
+ const q = async (sql) => { try { return (await db.query(sql, { safety: false })); } catch (e) { return { err: e.message }; } };
45
+ const r1 = await q('SELECT 1 AS `a``b`');
46
+ ok('#346 列别名含转义反引号', !r1.err && r1.columns[0] === 'a`b', r1.err || r1.columns);
47
+ const r2 = await q('CREATE TABLE `t``x` (id INT)');
48
+ ok('#346 建表名含转义反引号', !r2.err, r2.err);
49
+ await db.close();
50
+ }
51
+
52
+ // #329 sqlite 兼容层:空串 / 纯空白 / 只有分号 一律 no-op
53
+ {
54
+ const db = new SqliteCompat(':memory:');
55
+ for (const sql of ['', ' ', '\n\t ', ';']) {
56
+ let got, err;
57
+ try { got = db.prepare(sql).all(); } catch (e) { err = e.message; }
58
+ ok(`#329 prepare(${JSON.stringify(sql)}) => []`, !err && Array.isArray(got) && got.length === 0, err || got);
59
+ }
60
+ db.close();
61
+ }
62
+
63
+ // #328 聚合管道 $addFields / $set
64
+ {
65
+ const srv = createMongoServer({ dataDir: ':memory:' });
66
+ await new Promise((r) => srv.listen(r));
67
+ const call = (doc) => new Promise((res) => {
68
+ const s = net.connect(srv._server.address().port, '127.0.0.1', () => s.write(opMsg(doc)));
69
+ const c = [];
70
+ s.on('data', (d) => {
71
+ c.push(d);
72
+ const b = Buffer.concat(c);
73
+ if (b.length >= 16 && b.length >= b.readInt32LE(0)) { res(bsonReadDocument(b.slice(21), 0).value); s.end(); }
74
+ });
75
+ });
76
+ await call({ insert: 'af', documents: [{ n: 1 }, { n: 2 }], $db: 'test' });
77
+ const add = await call({ aggregate: 'af', pipeline: [{ $addFields: { z: 5 } }], cursor: {}, $db: 'test' });
78
+ ok('#328 $addFields 保留原字段并新增字段',
79
+ add.ok === 1 && JSON.stringify(add.cursor.firstBatch) === '[{"n":1,"z":5},{"n":2,"z":5}]', add);
80
+ const set = await call({ aggregate: 'af', pipeline: [{ $set: { y: { $multiply: ['$n', 10] } } }], cursor: {}, $db: 'test' });
81
+ ok('#328 $set 作为别名求值表达式',
82
+ set.ok === 1 && JSON.stringify(set.cursor.firstBatch) === '[{"n":1,"y":10},{"n":2,"y":20}]', set);
83
+ const bogus = await call({ aggregate: 'af', pipeline: [{ $bogus: {} }], cursor: {}, $db: 'test' });
84
+ ok('#328 未知 stage 仍报 40324', bogus.ok === 0 && bogus.code === 40324, bogus);
85
+
86
+ // #339 getMore 支持扩展 JSON 的 $numberLong 游标 id
87
+ await call({ insert: 'cur', documents: [{ n: 1 }, { n: 2 }, { n: 3 }], $db: 'test' });
88
+ const find = await call({ find: 'cur', batchSize: 2, $db: 'test' });
89
+ const rawId = find.cursor.id;
90
+ const id = rawId && typeof rawId === 'object' ? (rawId.$long ?? rawId.$numberLong) : rawId;
91
+ const more = await call({ getMore: { $numberLong: String(id) }, collection: 'cur', batchSize: 2, $db: 'test' });
92
+ ok('#339 getMore 接受 $numberLong 形态游标 id',
93
+ more.ok === 1 && more.cursor && more.cursor.nextBatch.length === 1, more);
94
+ srv.close();
95
+ }
96
+
97
+ console.log(`\n${passed} passed, ${failed} failed`);
98
+ process.exit(failed === 0 ? 0 : 1);
99
+ })().catch((e) => { console.error(e); process.exit(1); });
@@ -0,0 +1,49 @@
1
+ /*
2
+ * Regression tests for #334 #340 #345.
3
+ */
4
+ const path = require('path');
5
+ const ROOT = path.join(__dirname, '..');
6
+ const { Database } = require(path.join(ROOT, 'index.js'));
7
+
8
+ let passed = 0, failed = 0;
9
+ function ok(name, cond, extra) {
10
+ if (cond) { passed++; console.log('[OK]', name); }
11
+ else { failed++; console.log('[FAIL]', name, extra !== undefined ? '→ ' + JSON.stringify(extra) : ''); }
12
+ }
13
+
14
+ (async () => {
15
+ const db = new Database(':memory:');
16
+ const q = async (sql) => { try { return await db.query(sql, { safety: false }); } catch (e) { return { err: e.message }; } };
17
+
18
+ // #340 未闭合块注释必须显式报错
19
+ {
20
+ const r = await q('SELECT 1 /* 没有结束');
21
+ ok('#340 未闭合块注释报错', !!r.err && /Unterminated block comment/.test(r.err), r.err);
22
+ const ok2 = await q('SELECT 1 /* 正常 */');
23
+ ok('#340 正常块注释不受影响', !ok2.err && JSON.stringify(ok2.rows) === '[[1]]', ok2.err || ok2.rows);
24
+ }
25
+
26
+ // #345 纯注释语句与纯空白一致:空结果、不抛错
27
+ {
28
+ for (const sql of ['/* x */', '-- y', '# z', ' ']) {
29
+ const r = await q(sql);
30
+ const rows = Array.isArray(r) ? [] : r.rows;
31
+ ok(`#345 ${JSON.stringify(sql)} 返回空结果且不抛错`,
32
+ !r.err && (rows === undefined || (Array.isArray(rows) && rows.length === 0)), r.err || r);
33
+ }
34
+ }
35
+
36
+ // #334 count(table, filter)
37
+ {
38
+ await q('CREATE TABLE c (id INT PRIMARY KEY, v INT)');
39
+ for (const row of [{ id: 1, v: 1 }, { id: 2, v: 1 }, { id: 3, v: 2 }]) await db.insert('c', row);
40
+ ok('#334 count 全表', (await db.count('c')) === 3, await db.count('c'));
41
+ ok('#334 count 带过滤条件', (await db.count('c', { v: 1 })) === 2, await db.count('c', { v: 1 }));
42
+ ok('#334 count 空过滤等价全表', (await db.count('c', {})) === 3, await db.count('c', {}));
43
+ ok('#334 count 无匹配为 0', (await db.count('c', { v: 99 })) === 0, await db.count('c', { v: 99 }));
44
+ }
45
+
46
+ console.log(`\n${passed} passed, ${failed} failed`);
47
+ await db.close();
48
+ process.exit(failed === 0 ? 0 : 1);
49
+ })().catch((e) => { console.error(e); process.exit(1); });
@@ -0,0 +1,134 @@
1
+ /*
2
+ * #341 #344 #390 #419 回归测试(SQL 解析 / 语义)。
3
+ *
4
+ * 全部走公共 API:Database.query / executeSQL(与既有 issue-*.test.js 同一入口)。
5
+ *
6
+ * #341 `--` 注释 opener 按 MySQL 口径要求后跟空白/控制字符/行尾;
7
+ * `1--5` 是 1 - (-5) = 6,不是注释。
8
+ * #344 窗口帧简写(只给起始 bound)缺省结束 bound 为 CURRENT ROW,
9
+ * 与 ROWS BETWEEN <start> AND CURRENT ROW 等价。
10
+ * #390 INSERT ... SELECT 无目标列清单时按位置映射到目标表列;
11
+ * 列数不符按 MySQL 报 1136。
12
+ * #419 限定星号 `o.*` 只展开该表(或其别名)的列,不再把右表列一并投影。
13
+ *
14
+ * 注:#342 经复核,投影后的裸标识符是 MySQL 合法别名(AS 可省略),
15
+ * 与残留 token 在词法上无法区分,故不收紧;合法别名写法继续保留。
16
+ */
17
+ const assert = require('assert');
18
+ const { Database } = require('../index.js');
19
+
20
+ let passed = 0, failed = 0;
21
+ async function t(name, fn) {
22
+ try { await fn(); passed++; console.log(`ok - ${name}`); }
23
+ catch (e) { failed++; console.error(`not ok - ${name}\n ${e && e.message}`); }
24
+ }
25
+
26
+ (async () => {
27
+ // #341:`--` 后必须是空白/控制字符/行尾才构成注释
28
+ await t('#341 `1--5` 按减号解析为 6(不是注释)', async () => {
29
+ const db = new Database(':memory:');
30
+ const r = await db.query('SELECT 1--5 AS x', { safety: false });
31
+ assert.deepStrictEqual(r.rows, [[6]]);
32
+ assert.strictEqual(r.columns[0], 'x');
33
+ await db.close();
34
+ });
35
+
36
+ await t('#341 `-- ` 带空白仍是行注释', async () => {
37
+ const db = new Database(':memory:');
38
+ const r = await db.query('SELECT 1 -- 5', { safety: false });
39
+ assert.deepStrictEqual(r.rows, [[1]]);
40
+ await db.close();
41
+ });
42
+
43
+ // 合法别名写法必须继续工作(#342 复核:裸 ident 是 MySQL 合法别名)
44
+ await t('合法别名:AS 显式别名 / 裸 ident 别名', async () => {
45
+ const db = new Database(':memory:');
46
+ const r1 = await db.query('SELECT 1 AS x', { safety: false });
47
+ assert.deepStrictEqual(r1.rows, [[1]]);
48
+ assert.strictEqual(r1.columns[0], 'x');
49
+ const r2 = await db.query('SELECT 1 x', { safety: false });
50
+ assert.deepStrictEqual(r2.rows, [[1]]);
51
+ assert.strictEqual(r2.columns[0], 'x');
52
+ await db.close();
53
+ });
54
+
55
+ await t('残留非别名 token 仍报语法错误', async () => {
56
+ const db = new Database(':memory:');
57
+ await assert.rejects(() => db.query('SELECT 1 2', { safety: false }), /Unexpected token|syntax/i);
58
+ await assert.rejects(() => db.query('SELECT 1 WHERE 1 = 1 junk', { safety: false }), /Unexpected token|syntax/i);
59
+ await db.close();
60
+ });
61
+
62
+ // #344:简写帧 ROWS UNBOUNDED PRECEDING 缺省 AND CURRENT ROW(累计求和)
63
+ await t('#344 简写窗口帧累计求和 10/50/70/120/150', async () => {
64
+ const db = new Database(':memory:');
65
+ await db.query('CREATE TABLE w (a INT, v INT)', { safety: false });
66
+ await db.query('INSERT INTO w VALUES (1,10),(2,40),(3,20),(4,50),(5,30)', { safety: false });
67
+ const r = await db.query(
68
+ 'SELECT a, v, SUM(v) OVER (ORDER BY a ROWS UNBOUNDED PRECEDING) s FROM w ORDER BY a',
69
+ { safety: false });
70
+ assert.deepStrictEqual(r.rows, [[1, 10, 10], [2, 40, 50], [3, 20, 70], [4, 50, 120], [5, 30, 150]]);
71
+ await db.close();
72
+ });
73
+
74
+ await t('#344 显式 BETWEEN ... AND CURRENT ROW 与简写帧结果一致', async () => {
75
+ const db = new Database(':memory:');
76
+ await db.query('CREATE TABLE w2 (a INT, v INT)', { safety: false });
77
+ await db.query('INSERT INTO w2 VALUES (1,10),(2,40),(3,20),(4,50),(5,30)', { safety: false });
78
+ const r = await db.query(
79
+ 'SELECT a, v, SUM(v) OVER (ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) s FROM w2 ORDER BY a',
80
+ { safety: false });
81
+ assert.deepStrictEqual(r.rows, [[1, 10, 10], [2, 40, 50], [3, 20, 70], [4, 50, 120], [5, 30, 150]]);
82
+ await db.close();
83
+ });
84
+
85
+ // #390:INSERT ... SELECT 按位置映射,表达式列正确落库
86
+ await t('#390 INSERT...SELECT 表达式列按位置写入', async () => {
87
+ const db = new Database(':memory:');
88
+ await db.query('CREATE TABLE t (a INT, b INT)', { safety: false });
89
+ await db.query('INSERT INTO t VALUES (1,10),(2,20)', { safety: false });
90
+ await db.query('INSERT INTO t SELECT a+100, b FROM t', { safety: false });
91
+ const r = await db.query('SELECT a, b FROM t ORDER BY a, b', { safety: false });
92
+ assert.deepStrictEqual(r.rows, [[1, 10], [2, 20], [101, 10], [102, 20]]);
93
+ await db.close();
94
+ });
95
+
96
+ await t('#390 SELECT 列数多于目标表列报 1136', async () => {
97
+ const db = new Database(':memory:');
98
+ await db.query('CREATE TABLE t2 (a INT, b INT)', { safety: false });
99
+ await db.query('INSERT INTO t2 VALUES (1,10)', { safety: false });
100
+ await assert.rejects(
101
+ () => db.query('INSERT INTO t2 SELECT a, b, a FROM t2', { safety: false }),
102
+ /1136|Column count|value count/i);
103
+ await db.close();
104
+ });
105
+
106
+ // #419:限定星号只展开本表列
107
+ await t('#419 SELECT o.* 只投影 o 表列', async () => {
108
+ const db = new Database(':memory:');
109
+ await db.query('CREATE TABLE o (id INT, cid INT, v INT)', { safety: false });
110
+ await db.query('CREATE TABLE c (cid INT, name VARCHAR(10))', { safety: false });
111
+ await db.query('INSERT INTO o VALUES (1,1,100),(2,2,200)', { safety: false });
112
+ await db.query("INSERT INTO c VALUES (1,'a')", { safety: false });
113
+ const r = await db.query('SELECT o.* FROM o JOIN c ON o.cid = c.cid', { safety: false });
114
+ assert.deepStrictEqual(r.columns, ['o.id', 'o.cid', 'o.v']);
115
+ assert.ok(!r.columns.some((k) => k.startsWith('c.')), '不应投影右表 c 的列');
116
+ assert.deepStrictEqual(r.rows, [[1, 1, 100]]);
117
+ await db.close();
118
+ });
119
+
120
+ await t('#419 裸 SELECT * 仍展开全部连接表列(回归保护)', async () => {
121
+ const db = new Database(':memory:');
122
+ await db.query('CREATE TABLE o2 (id INT, cid INT, v INT)', { safety: false });
123
+ await db.query('CREATE TABLE c2 (cid INT, name VARCHAR(10))', { safety: false });
124
+ await db.query('INSERT INTO o2 VALUES (1,1,100)', { safety: false });
125
+ await db.query("INSERT INTO c2 VALUES (1,'a')", { safety: false });
126
+ const r = await db.query('SELECT * FROM o2 JOIN c2 ON o2.cid = c2.cid', { safety: false });
127
+ assert.ok(r.columns.some((k) => k.endsWith('.v')), '裸 * 仍应含左表 v 列');
128
+ assert.ok(r.columns.some((k) => k.endsWith('.name')), '裸 * 仍应含右表 name 列');
129
+ await db.close();
130
+ });
131
+
132
+ console.log(`\n${passed} passed, ${failed} failed`);
133
+ process.exit(failed ? 1 : 0);
134
+ })();