jsql-neo 6.5.23 → 6.5.25
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/LICENSE +1 -1
- package/README.md +4 -3
- package/bin/jsql +120 -1
- package/index.js +1 -1
- package/lib/native_client.js +91 -34
- package/lib/uninstaller.js +216 -0
- package/lib/updater.js +365 -0
- package/lib/wasm_client.js +88 -30
- package/native/jsql-neo-native.node +0 -0
- package/nativesrc/jsql-neo-core/src/engine/memory.rs +3 -3
- package/nativesrc/jsql-neo-core/src/engine/table.rs +263 -42
- package/nativesrc/jsql-neo-core/src/types.rs +2 -0
- package/package.json +1 -1
- package/postinstall.js +179 -44
- package/test/cli-maintenance.test.js +112 -0
- package/test/issue-38.test.js +56 -0
- package/wasm/browser.d.ts +34 -22
- package/wasm/browser.mjs +101 -29
- package/wasm/jsql_neo_wasm_bg.wasm +0 -0
package/lib/updater.js
ADDED
|
@@ -0,0 +1,365 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
/**
|
|
3
|
+
* jsql update —— 检查并安装 jsql-neo 的新版本。
|
|
4
|
+
*
|
|
5
|
+
* 设计取舍:
|
|
6
|
+
* - 版本来源优先 npm registry(https://registry.npmjs.org/jsql-neo/latest),
|
|
7
|
+
* 失败时退回 `npm view jsql-neo version`;两者都失败就优雅报错,不 crash。
|
|
8
|
+
* - 只有「全局安装」才自动执行 `npm install -g jsql-neo@latest`;
|
|
9
|
+
* 本地依赖安装绝不自动改用户的 package.json,只打印清晰指令。
|
|
10
|
+
* - `--check` 只看不装;`--dry-run` 打印将要执行的命令;`--force` 即使已是最新也重装。
|
|
11
|
+
*
|
|
12
|
+
* 不引入任何第三方依赖(与项目其它部分保持一致)。
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
const path = require('path');
|
|
16
|
+
const https = require('https');
|
|
17
|
+
const { execFileSync, spawnSync } = require('child_process');
|
|
18
|
+
|
|
19
|
+
const PKG_NAME = 'jsql-neo';
|
|
20
|
+
const REGISTRY_LATEST = `https://registry.npmjs.org/${PKG_NAME}/latest`;
|
|
21
|
+
const FETCH_TIMEOUT_MS = 8000;
|
|
22
|
+
const MAX_BODY_BYTES = 512 * 1024;
|
|
23
|
+
|
|
24
|
+
function packageDir() {
|
|
25
|
+
return path.resolve(__dirname, '..');
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function currentVersion() {
|
|
29
|
+
return require('../package.json').version;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// ─── 版本比较 ────────────────────────────────────────────────
|
|
33
|
+
|
|
34
|
+
/** 解析 semver(宽松):v1.2.3 / 1.2 / 1.2.3-beta.1 → { nums, pre } */
|
|
35
|
+
function parseVersion(v) {
|
|
36
|
+
let s = String(v == null ? '' : v).trim();
|
|
37
|
+
if (s[0] === 'v' || s[0] === 'V') s = s.slice(1);
|
|
38
|
+
const plus = s.indexOf('+');
|
|
39
|
+
if (plus !== -1) s = s.slice(0, plus);
|
|
40
|
+
const dash = s.indexOf('-');
|
|
41
|
+
const core = dash === -1 ? s : s.slice(0, dash);
|
|
42
|
+
const pre = dash === -1 ? '' : s.slice(dash + 1);
|
|
43
|
+
const nums = core.split('.').map((n) => {
|
|
44
|
+
const x = parseInt(n, 10);
|
|
45
|
+
return Number.isFinite(x) ? x : 0;
|
|
46
|
+
});
|
|
47
|
+
while (nums.length < 3) nums.push(0);
|
|
48
|
+
return { nums, pre };
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** semver-ish 比较:a < b → -1,相等 → 0,a > b → 1(prerelease 视为更低) */
|
|
52
|
+
function compareVersions(a, b) {
|
|
53
|
+
const A = parseVersion(a);
|
|
54
|
+
const B = parseVersion(b);
|
|
55
|
+
for (let i = 0; i < 3; i++) {
|
|
56
|
+
if (A.nums[i] !== B.nums[i]) return A.nums[i] > B.nums[i] ? 1 : -1;
|
|
57
|
+
}
|
|
58
|
+
if (A.pre === B.pre) return 0;
|
|
59
|
+
if (!A.pre) return 1;
|
|
60
|
+
if (!B.pre) return -1;
|
|
61
|
+
return A.pre > B.pre ? 1 : -1;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function isNewer(latest, current) {
|
|
65
|
+
return compareVersions(latest, current) > 0;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// ─── 网络 ───────────────────────────────────────────────────
|
|
69
|
+
|
|
70
|
+
function httpGetText(url, redirects = 0) {
|
|
71
|
+
return new Promise((resolve, reject) => {
|
|
72
|
+
if (redirects > 5) {
|
|
73
|
+
reject(new Error(`too many redirects for ${url}`));
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
let req;
|
|
77
|
+
try {
|
|
78
|
+
req = https.get(url, {
|
|
79
|
+
headers: {
|
|
80
|
+
'User-Agent': `jsql-neo/${currentVersion()} (+https://github.com/vexify-org/JSQL-neo)`,
|
|
81
|
+
Accept: 'application/vnd.npm.install-v1+json, application/json;q=0.9, */*;q=0.1',
|
|
82
|
+
},
|
|
83
|
+
}, (res) => {
|
|
84
|
+
if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) {
|
|
85
|
+
res.resume();
|
|
86
|
+
const next = new URL(res.headers.location, url).toString();
|
|
87
|
+
if (!/^https?:/i.test(next)) {
|
|
88
|
+
reject(new Error(`invalid redirect target for ${url}`));
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
httpGetText(next, redirects + 1).then(resolve, reject);
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
if (res.statusCode !== 200) {
|
|
95
|
+
res.resume();
|
|
96
|
+
reject(new Error(`HTTP ${res.statusCode} for ${url}`));
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
const chunks = [];
|
|
100
|
+
let received = 0;
|
|
101
|
+
res.on('data', (chunk) => {
|
|
102
|
+
received += chunk.length;
|
|
103
|
+
if (received > MAX_BODY_BYTES) {
|
|
104
|
+
res.destroy();
|
|
105
|
+
reject(new Error(`response too large for ${url}`));
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
chunks.push(chunk);
|
|
109
|
+
});
|
|
110
|
+
res.on('end', () => resolve(Buffer.concat(chunks).toString('utf8')));
|
|
111
|
+
res.on('error', reject);
|
|
112
|
+
});
|
|
113
|
+
} catch (e) {
|
|
114
|
+
reject(e);
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
req.on('error', reject);
|
|
118
|
+
req.setTimeout(FETCH_TIMEOUT_MS, () => {
|
|
119
|
+
try { req.destroy(); } catch (_) { /* ignore */ }
|
|
120
|
+
reject(new Error(`request timed out after ${FETCH_TIMEOUT_MS}ms: ${url}`));
|
|
121
|
+
});
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* 查询 npm 上的最新版本号。
|
|
127
|
+
* @param {{ fetchImpl?: Function, npmView?: boolean }} [opts] 测试可注入
|
|
128
|
+
* @returns {Promise<{version: string, source: string}>}
|
|
129
|
+
*/
|
|
130
|
+
async function fetchLatestVersion(opts = {}) {
|
|
131
|
+
const fetchImpl = opts.fetchImpl || httpGetText;
|
|
132
|
+
const errors = [];
|
|
133
|
+
|
|
134
|
+
// 1) registry HTTP(快,且不需要 npm 进程)
|
|
135
|
+
try {
|
|
136
|
+
const body = await fetchImpl(REGISTRY_LATEST);
|
|
137
|
+
const data = JSON.parse(body);
|
|
138
|
+
if (data && typeof data.version === 'string' && data.version.trim()) {
|
|
139
|
+
return { version: data.version.trim(), source: 'registry' };
|
|
140
|
+
}
|
|
141
|
+
throw new Error('registry response has no version field');
|
|
142
|
+
} catch (e) {
|
|
143
|
+
errors.push(`registry: ${e.message}`);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// 2) npm CLI 兜底(有些环境走私有 registry 配置,curl 直连反而不通)
|
|
147
|
+
if (opts.npmView !== false) {
|
|
148
|
+
try {
|
|
149
|
+
const out = execFileSync(npmCommand(), ['view', PKG_NAME, 'version'], {
|
|
150
|
+
encoding: 'utf8',
|
|
151
|
+
timeout: 15000,
|
|
152
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
153
|
+
}).trim();
|
|
154
|
+
// `npm view` 在旧版本里可能输出多行/带引号
|
|
155
|
+
const m = out.match(/\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?/);
|
|
156
|
+
if (m) return { version: m[0], source: 'npm-view' };
|
|
157
|
+
throw new Error(`unexpected npm view output: ${out.slice(0, 120)}`);
|
|
158
|
+
} catch (e) {
|
|
159
|
+
errors.push(`npm view: ${e.message}`);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
const err = new Error(`could not determine the latest version (${errors.join('; ')})`);
|
|
164
|
+
err.causes = errors;
|
|
165
|
+
throw err;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// ─── 安装形态判断 ───────────────────────────────────────────
|
|
169
|
+
|
|
170
|
+
function npmCommand() {
|
|
171
|
+
return process.platform === 'win32' ? 'npm.cmd' : 'npm';
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
function isInside(child, parent) {
|
|
175
|
+
const rel = path.relative(path.resolve(parent), path.resolve(child));
|
|
176
|
+
return rel === '' || (!!rel && !rel.startsWith('..') && !path.isAbsolute(rel));
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/** 收集可能的全局 node_modules 根目录 */
|
|
180
|
+
function globalRoots() {
|
|
181
|
+
const roots = [];
|
|
182
|
+
try {
|
|
183
|
+
const out = execFileSync(npmCommand(), ['root', '-g'], {
|
|
184
|
+
encoding: 'utf8',
|
|
185
|
+
timeout: 15000,
|
|
186
|
+
stdio: ['ignore', 'pipe', 'ignore'],
|
|
187
|
+
}).trim();
|
|
188
|
+
if (out) roots.push(out);
|
|
189
|
+
} catch (_) { /* npm 不可用就靠下面的启发式 */ }
|
|
190
|
+
|
|
191
|
+
if (process.env.npm_config_prefix) {
|
|
192
|
+
roots.push(path.join(process.env.npm_config_prefix, 'lib', 'node_modules'));
|
|
193
|
+
if (process.platform === 'win32') {
|
|
194
|
+
roots.push(path.join(process.env.npm_config_prefix, 'node_modules'));
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
if (process.platform === 'win32' && process.env.APPDATA) {
|
|
198
|
+
roots.push(path.join(process.env.APPDATA, 'npm', 'node_modules'));
|
|
199
|
+
}
|
|
200
|
+
return Array.from(new Set(roots.filter(Boolean)));
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* 判断当前这份 jsql-neo 是否来自全局安装。
|
|
205
|
+
* @param {string} [dir] 包目录,默认是 lib/..
|
|
206
|
+
*/
|
|
207
|
+
function isGlobalInstall(dir) {
|
|
208
|
+
const pkgDir = path.resolve(dir || packageDir());
|
|
209
|
+
return globalRoots().some((root) => isInside(pkgDir, root));
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/** 当前包是否位于某个项目的 node_modules 下(本地依赖安装) */
|
|
213
|
+
function isLocalDependency(dir) {
|
|
214
|
+
const pkgDir = path.resolve(dir || packageDir());
|
|
215
|
+
return path.basename(path.dirname(pkgDir)) === 'node_modules';
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
// ─── 更新流程 ───────────────────────────────────────────────
|
|
219
|
+
|
|
220
|
+
function installCommand(version) {
|
|
221
|
+
return `${npmCommand()} install -g ${PKG_NAME}@${version}`;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* 执行更新检查 / 安装。
|
|
226
|
+
* @param {{checkOnly?: boolean, force?: boolean, dryRun?: boolean}} [opts]
|
|
227
|
+
*/
|
|
228
|
+
async function runUpdate(opts = {}) {
|
|
229
|
+
const current = currentVersion();
|
|
230
|
+
let latestInfo;
|
|
231
|
+
try {
|
|
232
|
+
latestInfo = await fetchLatestVersion({ fetchImpl: opts.fetchImpl, npmView: opts.npmView });
|
|
233
|
+
} catch (e) {
|
|
234
|
+
return {
|
|
235
|
+
ok: false,
|
|
236
|
+
current,
|
|
237
|
+
latest: null,
|
|
238
|
+
error: e.message,
|
|
239
|
+
hints: [
|
|
240
|
+
'检查网络/registry 配置,或手动执行: ' + installCommand('latest'),
|
|
241
|
+
],
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
const latest = latestInfo.version;
|
|
246
|
+
const updateAvailable = isNewer(latest, current);
|
|
247
|
+
const global = typeof opts.global === 'boolean' ? opts.global : isGlobalInstall();
|
|
248
|
+
const localDep = typeof opts.localDependency === 'boolean' ? opts.localDependency : isLocalDependency();
|
|
249
|
+
const result = {
|
|
250
|
+
ok: true,
|
|
251
|
+
current,
|
|
252
|
+
latest,
|
|
253
|
+
latestSource: latestInfo.source,
|
|
254
|
+
updateAvailable,
|
|
255
|
+
installType: global ? 'global' : (localDep ? 'local-dependency' : 'unknown'),
|
|
256
|
+
action: 'none',
|
|
257
|
+
command: null,
|
|
258
|
+
installed: false,
|
|
259
|
+
warnings: [],
|
|
260
|
+
};
|
|
261
|
+
|
|
262
|
+
if (!updateAvailable && !opts.force) {
|
|
263
|
+
result.action = opts.checkOnly ? 'checked' : 'up-to-date';
|
|
264
|
+
return result;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
if (opts.checkOnly) {
|
|
268
|
+
result.action = 'checked';
|
|
269
|
+
result.command = global ? installCommand(latest) : installCommand(latest);
|
|
270
|
+
return result;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
if (opts.dryRun) {
|
|
274
|
+
result.action = 'would-install';
|
|
275
|
+
result.command = installCommand(latest);
|
|
276
|
+
return result;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
// 非全局安装:不自动改动用户项目依赖,只给清晰指令
|
|
280
|
+
if (!global) {
|
|
281
|
+
result.action = 'instructions';
|
|
282
|
+
result.command = installCommand(latest);
|
|
283
|
+
result.localHint = `npm install ${PKG_NAME}@${latest}`;
|
|
284
|
+
result.warnings.push(
|
|
285
|
+
'jsql-neo 不是全局安装(当前位于项目 node_modules 或开发目录),未自动安装。'
|
|
286
|
+
);
|
|
287
|
+
return result;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
const args = ['install', '-g', `${PKG_NAME}@${latest}`];
|
|
291
|
+
const r = spawnSync(npmCommand(), args, {
|
|
292
|
+
stdio: 'inherit',
|
|
293
|
+
timeout: 10 * 60 * 1000,
|
|
294
|
+
});
|
|
295
|
+
result.command = `${npmCommand()} ${args.join(' ')}`;
|
|
296
|
+
if (r.error) {
|
|
297
|
+
result.ok = false;
|
|
298
|
+
result.error = r.error.message;
|
|
299
|
+
result.action = 'install-failed';
|
|
300
|
+
result.warnings.push('可尝试手动执行并检查权限: ' + result.command);
|
|
301
|
+
return result;
|
|
302
|
+
}
|
|
303
|
+
if (r.status !== 0) {
|
|
304
|
+
result.ok = false;
|
|
305
|
+
result.error = `npm install exited with code ${r.status}`;
|
|
306
|
+
result.action = 'install-failed';
|
|
307
|
+
result.warnings.push(
|
|
308
|
+
'如果是权限问题,尝试: sudo ' + result.command,
|
|
309
|
+
);
|
|
310
|
+
return result;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
result.installed = true;
|
|
314
|
+
result.action = 'installed';
|
|
315
|
+
result.after = latest;
|
|
316
|
+
return result;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
/** 把 runUpdate 的结果渲染成人读文本 */
|
|
320
|
+
function formatResult(r) {
|
|
321
|
+
const lines = [];
|
|
322
|
+
if (!r.ok) {
|
|
323
|
+
lines.push(`更新失败: ${r.error}`);
|
|
324
|
+
for (const h of r.hints || []) lines.push(` ${h}`);
|
|
325
|
+
return lines.join('\n');
|
|
326
|
+
}
|
|
327
|
+
lines.push(`当前版本: ${r.current}`);
|
|
328
|
+
lines.push(`最新版本: ${r.latest}${r.latestSource === 'npm-view' ? ' (via npm view)' : ''}`);
|
|
329
|
+
|
|
330
|
+
if (r.action === 'up-to-date') {
|
|
331
|
+
lines.push('已是最新版本。');
|
|
332
|
+
} else if (r.action === 'checked') {
|
|
333
|
+
lines.push(r.updateAvailable ? '有新版本可用。' : '已是最新版本。');
|
|
334
|
+
if (r.updateAvailable) lines.push(`运行 \`jsql update\` 安装,或手动执行: ${r.command}`);
|
|
335
|
+
} else if (r.action === 'would-install') {
|
|
336
|
+
lines.push(`将执行: ${r.command}`);
|
|
337
|
+
lines.push('(--dry-run,未做任何修改)');
|
|
338
|
+
} else if (r.action === 'installed') {
|
|
339
|
+
lines.push(`已更新到 ${r.latest}。重新打开终端后 \`jsql version\` 生效。`);
|
|
340
|
+
} else if (r.action === 'instructions') {
|
|
341
|
+
lines.push('检测到非全局安装,未自动更新(避免改动你的项目依赖)。');
|
|
342
|
+
lines.push(`全局安装: ${r.command}`);
|
|
343
|
+
if (r.localHint) lines.push(`项目内升级: ${r.localHint}`);
|
|
344
|
+
} else if (r.action === 'install-failed') {
|
|
345
|
+
lines.push(`安装失败: ${r.error}`);
|
|
346
|
+
}
|
|
347
|
+
for (const w of r.warnings || []) lines.push(`注意: ${w}`);
|
|
348
|
+
return lines.join('\n');
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
module.exports = {
|
|
352
|
+
PKG_NAME,
|
|
353
|
+
REGISTRY_LATEST,
|
|
354
|
+
parseVersion,
|
|
355
|
+
compareVersions,
|
|
356
|
+
isNewer,
|
|
357
|
+
currentVersion,
|
|
358
|
+
fetchLatestVersion,
|
|
359
|
+
isGlobalInstall,
|
|
360
|
+
isLocalDependency,
|
|
361
|
+
globalRoots,
|
|
362
|
+
runUpdate,
|
|
363
|
+
formatResult,
|
|
364
|
+
installCommand,
|
|
365
|
+
};
|
package/lib/wasm_client.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
const path = require('path');
|
|
2
2
|
const { executeSQL } = require('./sql');
|
|
3
|
+
const { createError } = require('./errors');
|
|
4
|
+
const bigintJson = require('./bigint_json');
|
|
3
5
|
const {
|
|
4
6
|
createHookRegistry, applyPlugin, buildPluginContext, registerHook,
|
|
5
7
|
runHooks, getPluginApis
|
|
@@ -77,6 +79,73 @@ function assertRowObject(row, index, isBatch) {
|
|
|
77
79
|
throw new TypeError(`insert(): expected a row object${where}, got ${got}`);
|
|
78
80
|
}
|
|
79
81
|
|
|
82
|
+
const INT64_MIN = -9223372036854775808n;
|
|
83
|
+
const INT64_MAX = 9223372036854775807n;
|
|
84
|
+
const SAFE_INT_MIN = BigInt(Number.MIN_SAFE_INTEGER);
|
|
85
|
+
const SAFE_INT_MAX = BigInt(Number.MAX_SAFE_INTEGER);
|
|
86
|
+
const DECIMAL_INT_RE = /^[+-]?\d+$/;
|
|
87
|
+
|
|
88
|
+
function isIntegerType(t) {
|
|
89
|
+
return t === 'integer' || t === 'int' || t === 'bigint';
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** 与 lib/table.js 的 normalizeInteger 一致(Issue #38)。 */
|
|
93
|
+
function normalizeInteger(v, type) {
|
|
94
|
+
let b;
|
|
95
|
+
if (typeof v === 'bigint') b = v;
|
|
96
|
+
else {
|
|
97
|
+
try { b = BigInt(String(v).trim()); } catch (_) { throw createError('ER_TRUNCATED_WRONG_VALUE', type, v); }
|
|
98
|
+
}
|
|
99
|
+
if (b < INT64_MIN || b > INT64_MAX) throw createError('ER_TRUNCATED_WRONG_VALUE', type, b.toString());
|
|
100
|
+
return (b >= SAFE_INT_MIN && b <= SAFE_INT_MAX) ? Number(b) : b;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** Rust 绑定层把 >2^53 的整数以十进制字符串返回,这里还原为 BigInt。 */
|
|
104
|
+
function restoreInteger(v) {
|
|
105
|
+
if (typeof v === 'bigint') return v;
|
|
106
|
+
if (typeof v === 'string' && DECIMAL_INT_RE.test(v)) {
|
|
107
|
+
const b = BigInt(v);
|
|
108
|
+
return (b >= SAFE_INT_MIN && b <= SAFE_INT_MAX) ? Number(b) : b;
|
|
109
|
+
}
|
|
110
|
+
if (typeof v === 'number' && Number.isInteger(v) && !Number.isSafeInteger(v)) return BigInt(v);
|
|
111
|
+
return v;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* 传给 Rust 的整数列值:BigInt / 十进制字符串 / 安全 number 转成精确值;
|
|
116
|
+
* 越界或小数/布尔直接抛 1292(与 JS 引擎一致)。
|
|
117
|
+
*/
|
|
118
|
+
function normalizeIntegerInput(v, type) {
|
|
119
|
+
if (typeof v === 'bigint') return normalizeInteger(v, type);
|
|
120
|
+
if (typeof v === 'number') {
|
|
121
|
+
if (!Number.isInteger(v) || !Number.isSafeInteger(v)) throw createError('ER_TRUNCATED_WRONG_VALUE', type, v);
|
|
122
|
+
return v;
|
|
123
|
+
}
|
|
124
|
+
if (typeof v === 'string' && DECIMAL_INT_RE.test(v.trim())) return normalizeInteger(v.trim(), type);
|
|
125
|
+
throw createError('ER_TRUNCATED_WRONG_VALUE', type, v);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** 按 schema 做插入/更新前的值归一(JSON 列序列化、整数列精确化)。 */
|
|
129
|
+
function normalizeRowForRust(table, schema, row) {
|
|
130
|
+
const out = {};
|
|
131
|
+
for (const [k, v] of Object.entries(row)) {
|
|
132
|
+
const def = schema[k];
|
|
133
|
+
let t = null;
|
|
134
|
+
if (typeof def === 'string') t = def.toLowerCase();
|
|
135
|
+
else if (def && typeof def === 'object' && def.type) t = String(def.type).toLowerCase();
|
|
136
|
+
if (v !== null && v !== undefined && ['json', 'array', 'object'].includes(t) && typeof v !== 'string') {
|
|
137
|
+
out[k] = safeStringify(v, table, k);
|
|
138
|
+
} else if (v !== null && v !== undefined && isIntegerType(t)) {
|
|
139
|
+
out[k] = normalizeIntegerInput(v, t);
|
|
140
|
+
} else if (v instanceof Date) {
|
|
141
|
+
out[k] = v.toISOString();
|
|
142
|
+
} else {
|
|
143
|
+
out[k] = v;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
return out;
|
|
147
|
+
}
|
|
148
|
+
|
|
80
149
|
const NATIVE_TYPE_MAP = {
|
|
81
150
|
text: 'string',
|
|
82
151
|
varchar: 'string',
|
|
@@ -91,7 +160,7 @@ const NATIVE_TYPE_MAP = {
|
|
|
91
160
|
array: 'string',
|
|
92
161
|
object: 'string',
|
|
93
162
|
bool: 'boolean',
|
|
94
|
-
bigint: '
|
|
163
|
+
bigint: 'bigint',
|
|
95
164
|
int: 'integer',
|
|
96
165
|
};
|
|
97
166
|
|
|
@@ -182,9 +251,9 @@ function restoreRow(row, schema) {
|
|
|
182
251
|
if (typeof colDef === 'string') t = colDef.toLowerCase();
|
|
183
252
|
else if (colDef && typeof colDef === 'object' && colDef.type) t = String(colDef.type).toLowerCase();
|
|
184
253
|
if (['json', 'array', 'object'].includes(t) && typeof value === 'string') {
|
|
185
|
-
try { out[field] =
|
|
186
|
-
} else if ((t
|
|
187
|
-
out[field] =
|
|
254
|
+
try { out[field] = bigintJson.parse(value); } catch (e) { out[field] = value; }
|
|
255
|
+
} else if (isIntegerType(t)) {
|
|
256
|
+
out[field] = restoreInteger(value);
|
|
188
257
|
} else if ((t === 'float' || t === 'double' || t === 'number') && typeof value === 'string' && !Number.isNaN(Number(value))) {
|
|
189
258
|
out[field] = Number(value);
|
|
190
259
|
} else {
|
|
@@ -450,7 +519,9 @@ class JSQL {
|
|
|
450
519
|
let t = null;
|
|
451
520
|
if (typeof def === 'string') t = def.toLowerCase();
|
|
452
521
|
else if (def && typeof def === 'object' && def.type) t = String(def.type).toLowerCase();
|
|
453
|
-
if (
|
|
522
|
+
if (isIntegerType(t)) {
|
|
523
|
+
try { return normalizeIntegerInput(id, t); } catch (_) { return id; }
|
|
524
|
+
}
|
|
454
525
|
return id;
|
|
455
526
|
}
|
|
456
527
|
|
|
@@ -462,7 +533,7 @@ class JSQL {
|
|
|
462
533
|
if (Number.isFinite(nid) && nid < 0) throw new Error('id must be a non-negative number');
|
|
463
534
|
const filter = {};
|
|
464
535
|
filter[pk] = this._coercePkId(table, id);
|
|
465
|
-
const r = safeJsonParse(wasmBindings.jsql_find(table,
|
|
536
|
+
const r = safeJsonParse(wasmBindings.jsql_find(table, bigintJson.stringify(filter), 1, 0));
|
|
466
537
|
if (r && r.error) return r;
|
|
467
538
|
return Array.isArray(r) && r.length > 0 ? r[0] : null;
|
|
468
539
|
}
|
|
@@ -479,7 +550,7 @@ class JSQL {
|
|
|
479
550
|
if (pk) {
|
|
480
551
|
const filter = {};
|
|
481
552
|
filter[pk] = this._coercePkId(table, id);
|
|
482
|
-
const r = safeJsonParse(wasmBindings.jsql_find(table,
|
|
553
|
+
const r = safeJsonParse(wasmBindings.jsql_find(table, bigintJson.stringify(filter), 1, 0));
|
|
483
554
|
if (Array.isArray(r) && r.length > 0) return r[0].id;
|
|
484
555
|
return null;
|
|
485
556
|
}
|
|
@@ -504,25 +575,9 @@ class JSQL {
|
|
|
504
575
|
// schema-aware 转换:JSON 列用 safeStringify 给出可读错误(环形对象等)
|
|
505
576
|
const schema = this._schemas[table];
|
|
506
577
|
if (schema) {
|
|
507
|
-
rows = rows.map(row =>
|
|
508
|
-
const out = {};
|
|
509
|
-
for (const [k, v] of Object.entries(row)) {
|
|
510
|
-
const def = schema[k];
|
|
511
|
-
let t = null;
|
|
512
|
-
if (typeof def === 'string') t = def.toLowerCase();
|
|
513
|
-
else if (def && typeof def === 'object' && def.type) t = String(def.type).toLowerCase();
|
|
514
|
-
if (v !== null && v !== undefined && ['json', 'array', 'object'].includes(t) && typeof v !== 'string') {
|
|
515
|
-
out[k] = safeStringify(v, table, k);
|
|
516
|
-
} else if (v instanceof Date) {
|
|
517
|
-
out[k] = v.toISOString();
|
|
518
|
-
} else {
|
|
519
|
-
out[k] = v;
|
|
520
|
-
}
|
|
521
|
-
}
|
|
522
|
-
return out;
|
|
523
|
-
});
|
|
578
|
+
rows = rows.map(row => normalizeRowForRust(table, schema, row));
|
|
524
579
|
}
|
|
525
|
-
const rawJson =
|
|
580
|
+
const rawJson = bigintJson.stringify(rows);
|
|
526
581
|
const pk = this._pkOf(table);
|
|
527
582
|
const isAutoPk = this._pkAutoIncrement(table);
|
|
528
583
|
// #36:旧 WASM 二进制对自增主键表按「保留区间 [next_id, next_id+n)」批量分配 id,
|
|
@@ -547,7 +602,7 @@ class JSQL {
|
|
|
547
602
|
throw new Error(`ER_DUP_ENTRY: Duplicate entry '${explicitPk}' for key '${pk}'`);
|
|
548
603
|
}
|
|
549
604
|
}
|
|
550
|
-
const r = safeJsonParse(wasmBindings.jsql_insert_json(table,
|
|
605
|
+
const r = safeJsonParse(wasmBindings.jsql_insert_json(table, bigintJson.stringify([row])));
|
|
551
606
|
if (r && r.error) throw new Error(r.error);
|
|
552
607
|
ids.push(explicitPk !== null ? explicitPk : (Array.isArray(r) ? r[0] : undefined));
|
|
553
608
|
}
|
|
@@ -699,7 +754,7 @@ class JSQL {
|
|
|
699
754
|
async find(table, filter, opts = {}) {
|
|
700
755
|
await this._flush();
|
|
701
756
|
if (!(await this._runHooks('beforeFind', [table, { filter, opts }]))) return [];
|
|
702
|
-
const filterStr = filter ?
|
|
757
|
+
const filterStr = filter ? bigintJson.stringify(filter) : '';
|
|
703
758
|
const { limit = 100, offset = 0 } = opts;
|
|
704
759
|
// WASM 端 jsql_find 的 limit/offset 是 usize(无符号),负数会经 wasm-bindgen
|
|
705
760
|
// 静默回绕成巨大值,导致空结果或溢出。#35:绑定层先校验(同 native_client.js)。
|
|
@@ -723,14 +778,15 @@ class JSQL {
|
|
|
723
778
|
}
|
|
724
779
|
|
|
725
780
|
async updateByIds(table, entries) {
|
|
781
|
+
const schema = this._schemas[table];
|
|
726
782
|
const pairs = [];
|
|
727
783
|
for (const [id, data] of entries) {
|
|
728
784
|
const seq = this._seqIdByPk(table, id);
|
|
729
|
-
if (seq !== null) pairs.push([seq, data]);
|
|
785
|
+
if (seq !== null) pairs.push([seq, schema ? normalizeRowForRust(table, schema, data) : data]);
|
|
730
786
|
}
|
|
731
787
|
if (!(await this._runHooks('beforeUpdate', [table, pairs]))) return;
|
|
732
788
|
await this._flush();
|
|
733
|
-
const r = safeJsonParse(wasmBindings.jsql_update_by_ids(table,
|
|
789
|
+
const r = safeJsonParse(wasmBindings.jsql_update_by_ids(table, bigintJson.stringify(pairs)));
|
|
734
790
|
if (r && r.error) throw new Error(r.error);
|
|
735
791
|
this._emit('update', { table, entries: pairs, result: r });
|
|
736
792
|
await this._runHooks('afterUpdate', [table, pairs, r]);
|
|
@@ -779,7 +835,9 @@ class JSQL {
|
|
|
779
835
|
// 静默产生两行同主键 —— 主键索引只留一个,索引与实际行数组不一致。
|
|
780
836
|
// MySQL / SQLite 都会报 UNIQUE constraint failed。
|
|
781
837
|
await this._assertNoKeyConflict(table, id, data, seq);
|
|
782
|
-
const
|
|
838
|
+
const schema = this._schemas[table];
|
|
839
|
+
const payload = schema ? normalizeRowForRust(table, schema, data) : data;
|
|
840
|
+
const r = safeJsonParse(wasmBindings.jsql_update_by_id(table, BigInt(seq), bigintJson.stringify(payload)));
|
|
783
841
|
if (r && r.ok === false) throw new Error(r.error || 'update failed');
|
|
784
842
|
this._emit('update', { table, id, data });
|
|
785
843
|
await this._runHooks('afterUpdate', [table, id, data, r]);
|
|
Binary file
|
|
@@ -109,7 +109,7 @@ impl Engine for MemoryEngine {
|
|
|
109
109
|
|
|
110
110
|
fn update_by_id(&mut self, table: &str, id: u64, data: HashMap<String, serde_json::Value>) -> Result<bool, String> {
|
|
111
111
|
let t = self.tables.get_mut(table).ok_or_else(|| format!("table '{}' not found", table))?;
|
|
112
|
-
|
|
112
|
+
t.update_row(id, data)
|
|
113
113
|
}
|
|
114
114
|
|
|
115
115
|
fn remove_by_id(&mut self, table: &str, id: u64) -> Result<bool, String> {
|
|
@@ -124,7 +124,7 @@ impl Engine for MemoryEngine {
|
|
|
124
124
|
|
|
125
125
|
fn update_by_ids(&mut self, table: &str, batch: Vec<(u64, HashMap<String, serde_json::Value>)>) -> Result<usize, String> {
|
|
126
126
|
let t = self.tables.get_mut(table).ok_or_else(|| format!("table '{}' not found", table))?;
|
|
127
|
-
|
|
127
|
+
t.update_rows(batch)
|
|
128
128
|
}
|
|
129
129
|
|
|
130
130
|
fn update_by_filter(&mut self, table: &str, filter: &Option<HashMap<String, serde_json::Value>>,
|
|
@@ -147,7 +147,7 @@ impl Engine for MemoryEngine {
|
|
|
147
147
|
};
|
|
148
148
|
let count = ids.len();
|
|
149
149
|
for id in &ids {
|
|
150
|
-
t.update_row_ref(*id, &data)
|
|
150
|
+
t.update_row_ref(*id, &data)?;
|
|
151
151
|
}
|
|
152
152
|
Ok(count)
|
|
153
153
|
}
|