jsql-neo 6.3.2 → 6.3.4

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/lib/sql.js CHANGED
@@ -1096,8 +1096,11 @@ class Parser {
1096
1096
  alias = this.parseOptionalAlias();
1097
1097
  columns.push({ expr: null, caseExpr, alias });
1098
1098
  } else {
1099
- // 列 / 常量 / 函数 / 算术表达式
1100
- const expr = this.parseScalar();
1099
+ // 列 / 常量 / 函数 / 算术表达式 / 括号分组 / 标量布尔(NOT、比较、LIKE…)。
1100
+ // 此前用 parseScalar(),导致 SELECT (1+2) / SELECT NOT 0 / SELECT 'a' LIKE 'b'
1101
+ // 这类合法标量在投影列表里解析报错。parseArgExpr() 允许裸操作数,
1102
+ // 又能在存在运算符时走完整表达式解析,列/常量/函数行为不变。
1103
+ const expr = this.parseArgExpr();
1101
1104
  let alias = null;
1102
1105
  alias = this.parseOptionalAlias();
1103
1106
  if (expr.type === 'star') {
@@ -1165,7 +1168,19 @@ class Parser {
1165
1168
  }
1166
1169
  } catch (e) {
1167
1170
  this.pos = save;
1168
- col = this.parseScalar();
1171
+ // ORDER BY 可以引用输出列别名,而别名可能与聚合函数同名:
1172
+ // SELECT AVG(v) AS avg FROM t GROUP BY g ORDER BY avg DESC
1173
+ // 这种 token 已被词法器标成 keyword(AVG/SUM/COUNT/MAX/MIN 都在关键字表里),
1174
+ // parseColumnRef 只认 ident 会失败,parseScalar 又会把它当函数调用 → "got 'AVG'"。
1175
+ // 此处把裸 keyword 当作列名,与 SQLite / MySQL 的行为一致。
1176
+ const tk = this.peek();
1177
+ if (tk.type === 'keyword' && !this.isWord('ASC') && !this.isWord('DESC')
1178
+ && !ORDER_END.some(w => this.isWord(w))) {
1179
+ this.next();
1180
+ col = tk.value;
1181
+ } else {
1182
+ col = this.parseScalar();
1183
+ }
1169
1184
  }
1170
1185
  let dir = 'asc';
1171
1186
  if (this.isKeyword('ASC')) { this.next(); }
@@ -2005,6 +2020,14 @@ class Parser {
2005
2020
  this.peek().type === 'op' && this.peek().value === '(') {
2006
2021
  return this.parseAggregateCall(String(t.value).toUpperCase());
2007
2022
  }
2023
+ // 裸关键字、且后面不跟 '(' → 当作列名/别名。
2024
+ // 典型场景:SELECT AVG(v) AS avg FROM t GROUP BY g ORDER BY avg DESC
2025
+ // 'avg' 已被词法器标成 keyword(AVG 在关键字表里),既进不了 parseColumnRef(只认 ident),
2026
+ // 也不满足上面的聚合调用分支(后面是 DESC 不是 '('),最终会抛 "got 'AVG'"。
2027
+ // ORDER BY 引用输出别名是标准 SQL 行为,SQLite / MySQL 都允许。
2028
+ if (t.type === 'keyword' && !(this.peek().type === 'op' && this.peek().value === '(')) {
2029
+ return { type: 'column', name: t.value };
2030
+ }
2008
2031
  // 一元按位取反 ~x
2009
2032
  if (t.type === 'op' && t.value === '~') {
2010
2033
  return { type: 'bitnot', expr: this.parseOperand() };
@@ -2014,6 +2037,13 @@ class Parser {
2014
2037
  this.expect('op', ')');
2015
2038
  return { type: 'subquery', select: sub };
2016
2039
  }
2040
+ // 括号分组:支持 (1+2)、2*(3+4) 等出现在算术/函数参数任意位置
2041
+ // 注意:parseOperand 开头已通过 const t = this.next() 消费了 '(', 此处不可再 next()
2042
+ if (t.type === 'op' && t.value === '(') {
2043
+ const inner = this.parseArgExpr(); // 容忍裸操作数,允许纯算术分组
2044
+ this.expect('op', ')');
2045
+ return inner;
2046
+ }
2017
2047
  if (t.type === 'op' && (t.value === '-' || t.value === '+')) {
2018
2048
  const num = this.next();
2019
2049
  if (num.type !== 'number') throw new Error('Expected number after sign');
@@ -2318,8 +2348,11 @@ function applyCast(value, dataType) {
2318
2348
  const t = String(dataType || '').toUpperCase().replace(/\(.*\)$/, '').trim();
2319
2349
  if (value === null || value === undefined) return null;
2320
2350
  if (t === 'INTEGER' || t === 'INT' || t === 'BIGINT' || t === 'SMALLINT' || t === 'TINYINT') {
2321
- const n = Number(value);
2322
- return Number.isFinite(n) ? Math.trunc(n) : null;
2351
+ // MySQL CAST(... AS SIGNED/INT):取数字前缀('12abc'→12),无法解析时返回 0(而非 null)。
2352
+ if (typeof value === 'boolean') return value ? 1 : 0;
2353
+ if (typeof value === 'number') return Number.isFinite(value) ? Math.trunc(value) : 0;
2354
+ const m = String(value).trim().match(/^[+-]?\d+(\.\d+)?/);
2355
+ return m ? Math.trunc(Number(m[0])) : 0;
2323
2356
  }
2324
2357
  if (t === 'FLOAT' || t === 'DOUBLE' || t === 'REAL' || t === 'NUMERIC' || t === 'DECIMAL') {
2325
2358
  const n = Number(value);
@@ -2659,6 +2692,23 @@ function pgDatePart(part, d) {
2659
2692
  }
2660
2693
  }
2661
2694
 
2695
+ /**
2696
+ * 按自然月推进并在月末做「钳制」,对齐 MySQL 的日期加减语义。
2697
+ * JS 的 setMonth()/setFullYear() 会溢出(2023-01-31 +1 月 → 2023-03-03),
2698
+ * 而 MySQL 会把超出目标月天数的日期夹到当月最后一天(→ 2023-02-28)。
2699
+ * 先 setDate(1) 再切月,避免切换过程中先溢出再被钳制。
2700
+ */
2701
+ function addMonthsClamped(d, n) {
2702
+ const day = d.getDate();
2703
+ const t = new Date(d.getTime());
2704
+ t.setDate(1);
2705
+ t.setMonth(t.getMonth() + n);
2706
+ const lastDay = new Date(t.getFullYear(), t.getMonth() + 1, 0).getDate();
2707
+ t.setDate(Math.min(day, lastDay));
2708
+ d.setTime(t.getTime());
2709
+ return d;
2710
+ }
2711
+
2662
2712
  /** PG AGE(from, to) 的间隔文本:'6 years 7 mons 11 days' */
2663
2713
  function pgAge(from, to) {
2664
2714
  let years = to.getFullYear() - from.getFullYear();
@@ -2760,9 +2810,9 @@ function applyScalarFunction(fnNode, row, ctx) {
2760
2810
  switch (String(iv.unit || '').toUpperCase()) {
2761
2811
  case 'DAY': case 'DAYS': d.setDate(d.getDate() + n); break;
2762
2812
  case 'WEEK': case 'WEEKS': d.setDate(d.getDate() + n * 7); break;
2763
- case 'MONTH': case 'MONTHS': d.setMonth(d.getMonth() + n); break;
2764
- case 'QUARTER': d.setMonth(d.getMonth() + n * 3); break;
2765
- case 'YEAR': case 'YEARS': d.setFullYear(d.getFullYear() + n); break;
2813
+ case 'MONTH': case 'MONTHS': addMonthsClamped(d, n); break;
2814
+ case 'QUARTER': addMonthsClamped(d, n * 3); break;
2815
+ case 'YEAR': case 'YEARS': addMonthsClamped(d, n * 12); break;
2766
2816
  case 'HOUR': case 'HOURS': d.setHours(d.getHours() + n); break;
2767
2817
  case 'MINUTE': case 'MINUTES': d.setMinutes(d.getMinutes() + n); break;
2768
2818
  case 'SECOND': case 'SECONDS': d.setSeconds(d.getSeconds() + n); break;
@@ -2956,7 +3006,14 @@ function applyScalarFunction(fnNode, row, ctx) {
2956
3006
  if (!Number.isFinite(factor)) return x; // 位数过大:值不变
2957
3007
  if (factor === 0) return 0; // 位数过小:舍入到 0
2958
3008
  const sign = x < 0 ? -1 : 1;
2959
- return sign * Math.round(Math.abs(x) * factor) / factor;
3009
+ const scaled = Math.abs(x) * factor;
3010
+ const floor = Math.floor(scaled);
3011
+ const frac = scaled - floor;
3012
+ // 二进制浮点误差会把恰好落在中点的值存成略小(1.005*100 = 100.49999999999999),
3013
+ // 在中点附近按相对误差判定为「半」,以「半远离零」进位,避免静默少 1。
3014
+ const eps = Math.abs(scaled) * Number.EPSILON * 4 + Number.EPSILON;
3015
+ const rounded = Math.abs(frac - 0.5) < eps ? floor + 1 : Math.round(scaled);
3016
+ return sign * rounded / factor;
2960
3017
  }
2961
3018
  case 'FLOOR': return args[0] == null ? null : Math.floor(args[0]);
2962
3019
  case 'CEIL': case 'CEILING': return args[0] == null ? null : Math.ceil(args[0]);
@@ -2968,6 +3025,13 @@ function applyScalarFunction(fnNode, row, ctx) {
2968
3025
  case 'NULLIF': return args[0] === args[1] ? null : args[0];
2969
3026
  case 'IF': return args[0] ? args[1] : args[2];
2970
3027
  case 'REPLACE': return args[0] == null ? null : String(args[0]).split(args[1]).join(args[2]);
3028
+ case 'REPEAT': {
3029
+ // 此前完全缺失,静默返回 null
3030
+ if (args[0] == null || args[1] == null) return null;
3031
+ const n = Math.trunc(Number(args[1]));
3032
+ if (!Number.isFinite(n) || n <= 0) return '';
3033
+ return String(args[0]).repeat(n);
3034
+ }
2971
3035
  case 'SUBSTRING': case 'SUBSTR': {
2972
3036
  if (args[0] == null) return null;
2973
3037
  const s = String(args[0]);
@@ -2985,11 +3049,18 @@ function applyScalarFunction(fnNode, row, ctx) {
2985
3049
  }
2986
3050
  case 'LEFT': return args[0] == null ? null : String(args[0]).slice(0, Number(args[1]));
2987
3051
  case 'RIGHT': return args[0] == null ? null : String(args[0]).slice(-Number(args[1]));
2988
- case 'LOCATE': case 'INSTR': {
3052
+ case 'LOCATE': {
2989
3053
  if (args[0] == null || args[1] == null) return null;
2990
3054
  const idx = String(args[1]).indexOf(String(args[0]));
2991
3055
  return idx + 1;
2992
3056
  }
3057
+ case 'INSTR': {
3058
+ // INSTR(str, substr):参数顺序与 LOCATE(substr, str) 相反。
3059
+ // 此前两者共用一段代码,导致 INSTR 静默返回 0。
3060
+ if (args[0] == null || args[1] == null) return null;
3061
+ const idx = String(args[0]).indexOf(String(args[1]));
3062
+ return idx + 1;
3063
+ }
2993
3064
  case 'REVERSE': return args[0] == null ? null : String(args[0]).split('').reverse().join('');
2994
3065
  case 'LPAD': {
2995
3066
  if (args[0] == null) return null;
@@ -3163,8 +3234,10 @@ function evaluateExpr(expr, row, ctx) {
3163
3234
  case 'xor': return Boolean(evaluateExpr(expr.left, row, ctx)) !== Boolean(evaluateExpr(expr.right, row, ctx));
3164
3235
  case 'not': {
3165
3236
  // 三值逻辑:NOT UNKNOWN = UNKNOWN(返回 null,在 WHERE/ON/HAVING 中视为不选中)
3166
- const v = evaluateExpr(expr.expr, row, ctx);
3167
- return v === null ? null : !v;
3237
+ // 必须用 resolveOperand 取值:evaluateExpr 只认布尔类节点,
3238
+ // 对 value/column/arith 会落到 default 返回 false,导致 NOT 恒为真。
3239
+ const v = resolveOperand(expr.expr, row, ctx);
3240
+ return v === null || v === undefined ? null : !v;
3168
3241
  }
3169
3242
  case 'compare': {
3170
3243
  const l = resolveOperand(expr.left, row, ctx);
@@ -3311,6 +3384,25 @@ function windowColumnValue(c, r) {
3311
3384
  return v === undefined ? null : v;
3312
3385
  }
3313
3386
 
3387
+ // 表达式树里是否含 aggregate 节点(用于识别 ROUND(AVG(x),1) 这类嵌套聚合)
3388
+ function containsAggregateNode(node) {
3389
+ if (!node || typeof node !== 'object') return false;
3390
+ if (Array.isArray(node)) return node.some(containsAggregateNode);
3391
+ if (node.type === 'aggregate') return true;
3392
+ for (const k of Object.keys(node)) {
3393
+ const v = node[k];
3394
+ if (v && typeof v === 'object' && containsAggregateNode(v)) return true;
3395
+ }
3396
+ return false;
3397
+ }
3398
+
3399
+ // 结果集中的 SQL 布尔值按 MySQL 语义呈现为 1/0(如 SELECT 'a' LIKE 'b' → 0)
3400
+ function projectScalar(v) {
3401
+ if (v === true) return 1;
3402
+ if (v === false) return 0;
3403
+ return v;
3404
+ }
3405
+
3314
3406
  function scalarColumnValue(c, r, ctx) {
3315
3407
  if (c.over || (c.scalar && c.scalar.over)) return windowColumnValue(c, r);
3316
3408
  // 聚合列优先:GROUP_CONCAT / STDDEV 等同时带 scalar 与 aggregate
@@ -3326,13 +3418,21 @@ function scalarColumnValue(c, r, ctx) {
3326
3418
  const col = s.args && s.args[0];
3327
3419
  return ctx._aggValue(ctx.group, fn, col, c.aggregate ? c.distinct : (c.scalar && c.scalar.distinct));
3328
3420
  }
3329
- return resolveOperand(c.scalar, r, ctx);
3421
+ // 嵌套聚合:ROUND(AVG(x),1)、UPPER(MIN(s)) 等 —— 顶层不是聚合函数,
3422
+ // 但参数里可能夹着 aggregate 节点。resolveOperand 不认识这种节点会返回 null,
3423
+ // 所以先把整棵 scalar 树里的聚合按当前分组求值替换成字面值(每行都要替换,故用深拷贝)。
3424
+ if (ctx && ctx._aggValue && s && containsAggregateNode(s)) {
3425
+ const copy = JSON.parse(JSON.stringify(s));
3426
+ ctx._replaceAggregates(copy, ctx.group || [r]);
3427
+ return projectScalar(resolveOperand(copy, r, ctx));
3428
+ }
3429
+ return projectScalar(resolveOperand(c.scalar, r, ctx));
3330
3430
  }
3331
3431
 
3332
3432
  if (c.literal !== undefined) return c.literal;
3333
- if (c.caseExpr) return evaluateCaseVal(c.caseExpr, r, ctx);
3433
+ if (c.caseExpr) return projectScalar(evaluateCaseVal(c.caseExpr, r, ctx));
3334
3434
  if (c.expr === '*') return r[Object.keys(r).find(k => !k.startsWith('_'))];
3335
- return resolveOperand({ type: 'column', name: c.expr }, r, ctx);
3435
+ return projectScalar(resolveOperand({ type: 'column', name: c.expr }, r, ctx));
3336
3436
  }
3337
3437
 
3338
3438
  function sqlTypeName(type) {
@@ -3387,21 +3487,21 @@ class SQLExecutor {
3387
3487
  async execute(statement) {
3388
3488
  switch (statement.type) {
3389
3489
  case 'createTable': {
3390
- if (statement.ifNotExists && this.engine.hasTable && this.engine.hasTable(statement.name)) {
3490
+ if (statement.ifNotExists && this.engine.hasTable && await this.engine.hasTable(statement.name)) {
3391
3491
  return { ok: true, type: 'createTable', table: statement.name, affectedRows: 0, skipped: true };
3392
3492
  }
3393
3493
  const r = await this.engine.createTable(statement.name, statement.schema);
3394
3494
  return { ok: true, type: 'createTable', table: statement.name, affectedRows: 0, result: r };
3395
3495
  }
3396
3496
  case 'dropTable': {
3397
- if (statement.ifExists && !this.engine.hasTable(statement.table)) {
3497
+ if (statement.ifExists && !(await this.engine.hasTable(statement.table))) {
3398
3498
  return { ok: true, type: 'dropTable', table: statement.table, affectedRows: 0 };
3399
3499
  }
3400
3500
  await this.engine.dropTable(statement.table);
3401
3501
  return { ok: true, type: 'dropTable', table: statement.table, affectedRows: 0 };
3402
3502
  }
3403
3503
  case 'truncate': {
3404
- if (this.engine.hasTable(statement.name)) await this.engine.truncate(statement.name);
3504
+ if (await this.engine.hasTable(statement.name)) await this.engine.truncate(statement.name);
3405
3505
  return { ok: true, type: 'truncate', table: statement.name, affectedRows: 0 };
3406
3506
  }
3407
3507
  case 'alterTable': {
@@ -3754,7 +3854,7 @@ class SQLExecutor {
3754
3854
  }
3755
3855
  case 'createIndex': {
3756
3856
  const tName = statement.table;
3757
- if (typeof this.engine.hasTable === 'function' && !this.engine.hasTable(tName)) {
3857
+ if (typeof this.engine.hasTable === 'function' && !(await this.engine.hasTable(tName))) {
3758
3858
  throw new Error(`Table '${tName}' does not exist`);
3759
3859
  }
3760
3860
  let handled = false;
@@ -4136,7 +4236,7 @@ class SQLExecutor {
4136
4236
  }
4137
4237
 
4138
4238
  async _getSchema(name) {
4139
- if (this.engine.hasTable && !this.engine.hasTable(name)) {
4239
+ if (this.engine.hasTable && !(await this.engine.hasTable(name))) {
4140
4240
  throw new Error(`Table '${name}' does not exist`);
4141
4241
  }
4142
4242
  const schema = this.engine.getTableSchema
@@ -4436,7 +4536,12 @@ class SQLExecutor {
4436
4536
  return typeof agg.result === 'function' ? agg.result(state) : state;
4437
4537
  }
4438
4538
  }
4439
- if (fn === 'SUM') return values.reduce((s, v) => s + (typeof v === 'number' ? v : Number(v) || 0), 0);
4539
+ if (fn === 'SUM') {
4540
+ // MySQL 语义:空集(或无任何非 NULL 值)的 SUM 为 NULL,而非 0。
4541
+ // AVG/MIN/MAX 已按此处理,此前只有 SUM 漏了。
4542
+ if (!values.length) return null;
4543
+ return values.reduce((s, v) => s + (typeof v === 'number' ? v : Number(v) || 0), 0);
4544
+ }
4440
4545
  if (fn === 'AVG') return values.length ? values.reduce((s, v) => s + (typeof v === 'number' ? v : Number(v) || 0), 0) / values.length : null;
4441
4546
  if (fn === 'MIN' || fn === 'MAX') {
4442
4547
  const vs = values.filter(v => v !== null && v !== undefined);
@@ -5165,7 +5270,11 @@ class SQLExecutor {
5165
5270
  // 聚合输出(无 GROUP BY 时)
5166
5271
  const hasCustomAgg = this.ctx && this.ctx.aggregates && statement.columns.some(c => c.scalar && c.scalar.type === 'func' && Object.prototype.hasOwnProperty.call(this.ctx.aggregates, String(c.scalar.name).toUpperCase()));
5167
5272
  // 注意:带 OVER 的聚合是窗口函数,不是“整表塌缩成一行”的聚合,必须排除
5168
- if (!statement.groupBy && (statement.columns.some(c => c.aggregate && !c.window) || hasCustomAgg)) {
5273
+ // 无 GROUP BY 的整表聚合:顶层是聚合(AVG(x))、自定义聚合(total(x)),
5274
+ // 以及嵌套聚合(ROUND(AVG(x),1) —— 聚合藏在 scalar 的参数里,顶层 c.aggregate 为空)
5275
+ const hasNestedAgg = !statement.groupBy &&
5276
+ statement.columns.some(c => c.scalar && containsAggregateNode(c.scalar));
5277
+ if (!statement.groupBy && (statement.columns.some(c => c.aggregate && !c.window) || hasCustomAgg || hasNestedAgg)) {
5169
5278
  const cols = statement.columns.map(c => scalarColumnName(c));
5170
5279
  const tableName2 = statement.from ? (statement.from.tables[0].table || null) : null;
5171
5280
  // HAVING 判定不通过时 rows 已为空,此时整表聚合也应为空,不能无条件补一行
@@ -5180,9 +5289,17 @@ class SQLExecutor {
5180
5289
  return this._aggValue(rows, fn, c.scalar.args && c.scalar.args[0]);
5181
5290
  }
5182
5291
  }
5183
- if (c.scalar) return resolveOperand(c.scalar, rows[0] || {}, this.ctx);
5184
- if (c.caseExpr) return resolveOperand(c.caseExpr, rows[0] || {}, this.ctx);
5185
- if (c.expr !== null && c.expr !== '*') return rows[0] ? resolveOperand({ type: 'column', name: c.expr }, rows[0]) : null;
5292
+ if (c.scalar) {
5293
+ // 先把参数里的聚合按整表求值替换掉,否则 ROUND(AVG(x),1) 会得到 null
5294
+ if (containsAggregateNode(c.scalar)) {
5295
+ const nested = JSON.parse(JSON.stringify(c.scalar));
5296
+ this._replaceAggregates(nested, rows);
5297
+ return projectScalar(resolveOperand(nested, rows[0] || {}, this.ctx));
5298
+ }
5299
+ return projectScalar(resolveOperand(c.scalar, rows[0] || {}, this.ctx));
5300
+ }
5301
+ if (c.caseExpr) return projectScalar(resolveOperand(c.caseExpr, rows[0] || {}, this.ctx));
5302
+ if (c.expr !== null && c.expr !== '*') return rows[0] ? projectScalar(resolveOperand({ type: 'column', name: c.expr }, rows[0])) : null;
5186
5303
  return null;
5187
5304
  };
5188
5305
  return { ok: true, type: 'select', table: tableName2, columns: cols, rows: [[...statement.columns.map(valueOf)]], aggregate: statement.aggregate };
@@ -5197,9 +5314,54 @@ class SQLExecutor {
5197
5314
  : rows;
5198
5315
  const mapped = await this._projectRows(statement, rowsForGroups, (r) => {
5199
5316
  const group = r._group || [r];
5200
- return statement.columns.map(c => scalarColumnValue(c, r, { _aggValue: this._aggValue.bind(this), group, ctxAggregates: this.ctx ? this.ctx.aggregates : null }));
5317
+ return statement.columns.map(c => scalarColumnValue(c, r, {
5318
+ _aggValue: this._aggValue.bind(this),
5319
+ _replaceAggregates: this._replaceAggregates.bind(this),
5320
+ group,
5321
+ ctxAggregates: this.ctx ? this.ctx.aggregates : null,
5322
+ }));
5201
5323
  });
5202
- return { ok: true, type: 'select', table: statement.from ? (statement.from.tables[0].table || null) : null, columns: cols, rows: mapped, raw: rowsForGroups };
5324
+ // GROUP BY 路径此前直接 return,ORDER BY 完全没有生效
5325
+ //(SELECT dept, SUM(sal) AS total FROM emp GROUP BY dept ORDER BY total DESC
5326
+ // 返回的仍是分组出现顺序)。这里补上排序,规则与非分组路径一致。
5327
+ let outRows = mapped;
5328
+ if (statement.orderBy && mapped.length > 1) {
5329
+ const ordVal = (row, o) => {
5330
+ // row 是投影后的 [v1, v2, ...],按列名下标取
5331
+ const name = typeof o.column === 'string' ? o.column
5332
+ : (o.column && o.column.type === 'column' ? o.column.name : null);
5333
+ const pos = (o.column && typeof o.column === 'object' && o.column.type === 'value'
5334
+ && typeof o.column.value === 'number') ? o.column.value
5335
+ : (typeof o.column === 'number' ? o.column : null);
5336
+ if (name === null && pos !== null && pos >= 1 && pos <= cols.length) {
5337
+ return row[pos - 1];
5338
+ }
5339
+ if (name === null) return null;
5340
+ const idx = cols.indexOf(name);
5341
+ return idx === -1 ? null : row[idx];
5342
+ };
5343
+ const cmp = (a, b) => {
5344
+ for (const o of statement.orderBy) {
5345
+ const av = ordVal(a.row, o);
5346
+ const bv = ordVal(b.row, o);
5347
+ if (av === bv || (av === undefined && bv === undefined)) continue;
5348
+ if (av === undefined || av === null) return o.dir === 'asc' ? -1 : 1;
5349
+ if (bv === undefined || bv === null) return o.dir === 'asc' ? 1 : -1;
5350
+ const r = typeof av === 'number' && typeof bv === 'number' ? av - bv : String(av).localeCompare(String(bv));
5351
+ if (r !== 0) return o.dir === 'asc' ? r : -r;
5352
+ }
5353
+ return 0;
5354
+ };
5355
+ outRows = mapped
5356
+ .map((row, i) => ({ row, src: rowsForGroups[i] }))
5357
+ .sort(cmp)
5358
+ .map(x => x.row);
5359
+ }
5360
+ if (statement.limit !== null) {
5361
+ const start = statement.offset || 0;
5362
+ outRows = outRows.slice(start, start + statement.limit);
5363
+ }
5364
+ return { ok: true, type: 'select', table: statement.from ? (statement.from.tables[0].table || null) : null, columns: cols, rows: outRows, raw: rowsForGroups };
5203
5365
  }
5204
5366
 
5205
5367
  if (statement.orderBy) {
@@ -5257,11 +5419,11 @@ class SQLExecutor {
5257
5419
  const cols = statement.columns.map(c => scalarColumnName(c));
5258
5420
  const mapped = await this._projectRows(statement, rows, (r) => statement.columns.map(c => {
5259
5421
  if (c.over || (c.scalar && c.scalar.over)) return windowColumnValue(c, r);
5260
- if (c.scalar) return resolveOperand(c.scalar, r, this.ctx);
5422
+ if (c.scalar) return projectScalar(resolveOperand(c.scalar, r, this.ctx));
5261
5423
  if (c.expr === '*') return null;
5262
5424
  if (c.literal !== undefined) return c.literal;
5263
- if (c.caseExpr) return evaluateCaseVal(c.caseExpr, r);
5264
- return resolveOperand({ type: 'column', name: c.expr }, r, this.ctx);
5425
+ if (c.caseExpr) return projectScalar(evaluateCaseVal(c.caseExpr, r));
5426
+ return projectScalar(resolveOperand({ type: 'column', name: c.expr }, r, this.ctx));
5265
5427
  }));
5266
5428
  return { ok: true, type: 'select', table: tableName, columns: cols, rows: mapped, raw: rows };
5267
5429
  }
Binary file
@@ -60,7 +60,14 @@ impl HybridEngine {
60
60
  }
61
61
 
62
62
  /// 打开磁盘存储目录(mode: 'hybrid' | 'disk' | 'memory';memory 或空目录忽略)
63
- /// 每次 open 重置引擎状态,避免多个库目录/实例间残留表数据
63
+ ///
64
+ /// 注意(6.3.4):这里仍然会重置引擎状态,但语义已改变 ——
65
+ /// 以前全进程只有一个引擎,`open(dirB)` 会把 `open(dirA)` 的表全部清空,
66
+ /// 导致同进程无法持有两个独立数据库。现在每个 JS `Database` 实例有独立的
67
+ /// 引擎句柄,`open()` 只重置自己那一份。
68
+ ///
69
+ /// 但对**同一个句柄**重复 open 不同目录仍会丢弃上一个目录的内存表,
70
+ /// 因此调用方(native_client.js)应为每个 Database 分配独立句柄。
64
71
  pub fn open(&mut self, dir: &str, mode: &str) -> Result<(), String> {
65
72
  self.mem.borrow_mut().clear();
66
73
  self.meta.borrow_mut().clear();