@poe-platform/safe-bash 0.1.788 → 0.1.789

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.
Files changed (180) hide show
  1. package/README.md +1 -1
  2. package/dist/safe-bash/chunks/{chunk-WHURJBET.js → chunk-2MUJ7RLZ.js} +1 -1
  3. package/dist/safe-bash/chunks/{chunk-PYTLIUST.js → chunk-CTUHLNJM.js} +2 -2
  4. package/dist/safe-bash/chunks/{chunk-Z3HX3ZBA.js → chunk-D4PHJK4T.js} +1 -1
  5. package/dist/safe-bash/chunks/{chunk-O6356Z27.js → chunk-H7MMJS26.js} +2 -2
  6. package/dist/safe-bash/chunks/{chunk-Q3E6C3DX.js → chunk-KHJGUF2W.js} +14 -0
  7. package/dist/safe-bash/chunks/{chunk-FJZNUUOQ.js → chunk-VUVKC6HV.js} +1 -1
  8. package/dist/safe-bash/chunks/{chunk-ZMYVCRAF.js → chunk-X5ZCECM6.js} +2 -2
  9. package/dist/safe-bash/chunks/{git-UFNPAOYZ.js → git-HO7WDPHZ.js} +2 -2
  10. package/dist/safe-bash/command-chunks/{chunk-PFBJR2PY.js → chunk-2KRKKW5S.js} +2 -2
  11. package/dist/safe-bash/command-chunks/{chunk-4DSJDVBM.js → chunk-32F3RBUW.js} +10 -749
  12. package/dist/safe-bash/command-chunks/{chunk-L3MLMXL4.js → chunk-3ZK5F23O.js} +1 -1
  13. package/dist/safe-bash/command-chunks/{chunk-4QX6XCDD.js → chunk-4RNIZ3RT.js} +79 -0
  14. package/dist/safe-bash/command-chunks/{chunk-D7TADAYG.js → chunk-64LG3EVJ.js} +17 -0
  15. package/dist/safe-bash/command-chunks/{chunk-JUDOV5MJ.js → chunk-6F7QJMJ3.js} +1 -1
  16. package/dist/safe-bash/command-chunks/{chunk-IJ3FXP4Q.js → chunk-A3ZX6SCY.js} +1 -1
  17. package/dist/safe-bash/command-chunks/{chunk-YNT4ZJHW.js → chunk-AT6ZSOE6.js} +3 -3
  18. package/dist/safe-bash/command-chunks/{chunk-KSIGF4AG.js → chunk-BK4CMEE6.js} +1 -1
  19. package/dist/safe-bash/command-chunks/{chunk-XGMWWUHV.js → chunk-CZ5DAK4K.js} +1 -1
  20. package/dist/safe-bash/command-chunks/{chunk-Z57TV6TC.js → chunk-EZ2DTOLI.js} +4 -2
  21. package/dist/safe-bash/command-chunks/{chunk-ADCGH6A7.js → chunk-FH3D75FC.js} +1 -1
  22. package/dist/safe-bash/command-chunks/{chunk-WYJTWJZ5.js → chunk-G3G6W5ZE.js} +1 -1
  23. package/dist/safe-bash/command-chunks/{chunk-3HDIDEUJ.js → chunk-GF54KE2M.js} +1 -1
  24. package/dist/safe-bash/command-chunks/{chunk-Z7TYZG3A.js → chunk-IHHTTQX6.js} +5 -3
  25. package/dist/safe-bash/command-chunks/{chunk-INDIFLBW.js → chunk-JTAGNROA.js} +4 -0
  26. package/dist/safe-bash/command-chunks/chunk-PGEYORBZ.js +25 -0
  27. package/dist/safe-bash/command-chunks/chunk-QM4UZF3C.js +753 -0
  28. package/dist/safe-bash/command-chunks/{chunk-TOSM3T3A.js → chunk-UJOO6DKY.js} +20 -9
  29. package/dist/safe-bash/command-chunks/{chunk-2LP3VMPA.js → chunk-WFDW4XXD.js} +1 -17
  30. package/dist/safe-bash/command-chunks/chunk-WR37IS4O.js +318 -0
  31. package/dist/safe-bash/command-chunks/{chunk-Y5IHIAY3.js → chunk-XNHSCU6I.js} +7 -3
  32. package/dist/safe-bash/commands/basic.js +148 -48
  33. package/dist/safe-bash/commands/bytes/compression/index.js +5 -6
  34. package/dist/safe-bash/commands/cmp.d.ts +2 -3
  35. package/dist/safe-bash/commands/cmp.js +2 -689
  36. package/dist/safe-bash/commands/docx/index.browser.js +3 -46
  37. package/dist/safe-bash/commands/docx/index.d.ts +2 -24
  38. package/dist/safe-bash/commands/docx/index.js +2 -41
  39. package/dist/safe-bash/commands/docx/io.d.ts +1 -14
  40. package/dist/safe-bash/commands/docx/io.js +1 -15
  41. package/dist/safe-bash/commands/llm/index.browser.js +3 -3
  42. package/dist/safe-bash/commands/network/index.browser.js +3 -3
  43. package/dist/safe-bash/commands/node/index.browser.js +3 -3
  44. package/dist/safe-bash/commands/pptx/index.d.ts +2 -28
  45. package/dist/safe-bash/commands/pptx/index.js +2 -152
  46. package/dist/safe-bash/commands/printf-float.d.ts +1 -0
  47. package/dist/safe-bash/commands/printf-float.js +48 -38
  48. package/dist/safe-bash/commands/printf-format.js +9 -6
  49. package/dist/safe-bash/commands/printf-quote.d.ts +3 -0
  50. package/dist/safe-bash/commands/printf-quote.js +62 -0
  51. package/dist/safe-bash/commands/printf-wide.d.ts +5 -0
  52. package/dist/safe-bash/commands/printf-wide.js +41 -0
  53. package/dist/safe-bash/commands/python/index.browser.js +2 -2
  54. package/dist/safe-bash/commands/truncate.d.ts +2 -3
  55. package/dist/safe-bash/commands/truncate.js +2 -686
  56. package/dist/safe-bash/contracts/diagnostics.d.ts +2 -0
  57. package/dist/safe-bash/contracts/diagnostics.js +2 -0
  58. package/dist/safe-bash/contracts/runtime-control.d.ts +2 -0
  59. package/dist/safe-bash/contracts/runtime-control.js +2 -0
  60. package/dist/safe-bash/core.browser.d.ts +1 -1
  61. package/dist/safe-bash/core.browser.js +2651 -4871
  62. package/dist/safe-bash/core.d.ts +10 -0
  63. package/dist/safe-bash/opt-in/commands/cmp/index.d.ts +6 -2
  64. package/dist/safe-bash/opt-in/commands/cmp/index.js +776 -79
  65. package/dist/safe-bash/opt-in/commands/cmp/options.d.ts +0 -12
  66. package/dist/safe-bash/opt-in/commands/cmp/options.js +0 -153
  67. package/dist/safe-bash/opt-in/commands/truncate/index.d.ts +8 -3
  68. package/dist/safe-bash/opt-in/commands/truncate/index.js +790 -272
  69. package/dist/safe-bash/opt-in/commands/truncate/sync.d.ts +1 -0
  70. package/dist/safe-bash/opt-in/commands/truncate/sync.js +60 -0
  71. package/dist/safe-bash/optional-host.browser.js +9 -1
  72. package/dist/safe-bash/optional-host.d.ts +3 -1
  73. package/dist/safe-bash/optional-host.js +3 -1
  74. package/dist/safe-bash/shell/runtime.js +10 -6
  75. package/dist/safe-bash-checksum-engine/index.d.ts +1 -1
  76. package/dist/safe-bash-checksum-engine/index.js +14 -2
  77. package/dist/safe-bash-command-bzip2/index.js +2 -1
  78. package/dist/safe-bash-command-cmp/index.d.ts +10 -0
  79. package/dist/safe-bash-command-cmp/index.js +796 -0
  80. package/dist/safe-bash-command-cmp/options.d.ts +12 -0
  81. package/dist/safe-bash-command-cmp/options.js +42 -0
  82. package/dist/safe-bash-command-csvcut/index.js +11 -8
  83. package/dist/safe-bash-command-csvgrep/index.js +35 -7
  84. package/dist/safe-bash-command-csvkit/codecs/python.js +4 -3
  85. package/dist/safe-bash-command-csvkit/codecs/utf8.js +3 -2
  86. package/dist/safe-bash-command-csvkit/index.js +80 -324
  87. package/dist/safe-bash-command-csvkit/python-regex.d.ts +5 -0
  88. package/dist/safe-bash-command-csvkit/python-regex.js +14 -0
  89. package/dist/safe-bash-command-csvkit/runtime.d.ts +1 -0
  90. package/dist/safe-bash-command-csvkit/sync-support.js +4 -2
  91. package/dist/safe-bash-command-diff3/index.js +3 -0
  92. package/dist/safe-bash-command-docx/LICENSE +21 -0
  93. package/dist/safe-bash-command-docx/index.d.ts +30 -0
  94. package/dist/safe-bash-command-docx/index.js +67 -0
  95. package/dist/safe-bash-command-docx/io.d.ts +14 -0
  96. package/dist/safe-bash-command-docx/io.js +26 -0
  97. package/dist/safe-bash-command-envsubst/index.js +34 -3
  98. package/dist/safe-bash-command-exiftool/index.js +3 -0
  99. package/dist/safe-bash-command-ffmpeg/index.js +1102 -1088
  100. package/dist/safe-bash-command-fold/family.js +1 -1
  101. package/dist/safe-bash-command-fold/index.js +1 -1
  102. package/dist/safe-bash-command-htmlq/index.js +5 -0
  103. package/dist/safe-bash-command-imagemagick/index.d.ts +25 -21
  104. package/dist/safe-bash-command-imagemagick/index.js +669 -271
  105. package/dist/safe-bash-command-mmdc/index.js +48 -10
  106. package/dist/safe-bash-command-mmdc/svg.d.ts +1 -0
  107. package/dist/safe-bash-command-op/index.js +19 -14
  108. package/dist/safe-bash-command-pandoc/command.js +1 -1
  109. package/dist/safe-bash-command-pandoc/index.js +1 -1
  110. package/dist/safe-bash-command-pdfimages/index.d.ts +7 -1
  111. package/dist/safe-bash-command-pdfimages/index.js +63 -33
  112. package/dist/safe-bash-command-pdfinfo/index.d.ts +24 -20
  113. package/dist/safe-bash-command-pdfinfo/index.js +370 -161
  114. package/dist/safe-bash-command-pdftk/index.d.ts +7 -3
  115. package/dist/safe-bash-command-pdftk/index.js +114 -45
  116. package/dist/safe-bash-command-pdftoppm/index.d.ts +15 -7
  117. package/dist/safe-bash-command-pdftoppm/index.js +179 -109
  118. package/dist/safe-bash-command-pdftotext/index.d.ts +9 -5
  119. package/dist/safe-bash-command-pdftotext/index.js +90 -27
  120. package/dist/safe-bash-command-pptx/LICENSE +21 -0
  121. package/dist/safe-bash-command-pptx/index.d.ts +33 -0
  122. package/dist/safe-bash-command-pptx/index.js +168 -0
  123. package/dist/safe-bash-command-qpdf/index.d.ts +9 -3
  124. package/dist/safe-bash-command-qpdf/index.js +80 -29
  125. package/dist/safe-bash-command-sha512sum/index.js +12 -475
  126. package/dist/safe-bash-command-sips/index.d.ts +9 -5
  127. package/dist/safe-bash-command-sips/index.js +63 -44
  128. package/dist/safe-bash-command-soffice/index.d.ts +9 -3
  129. package/dist/safe-bash-command-soffice/index.js +65 -24
  130. package/dist/safe-bash-command-sqlite3/engine.d.ts +9 -0
  131. package/dist/safe-bash-command-sqlite3/index.d.ts +1 -0
  132. package/dist/safe-bash-command-sqlite3/index.js +851 -352
  133. package/dist/safe-bash-command-ssconvert/formats/xls.js +3 -3
  134. package/dist/safe-bash-command-timeout/index.js +3 -3
  135. package/dist/safe-bash-command-truncate/index.d.ts +8 -3
  136. package/dist/safe-bash-command-truncate/index.js +790 -272
  137. package/dist/safe-bash-command-truncate/sync.d.ts +1 -0
  138. package/dist/safe-bash-command-truncate/sync.js +60 -0
  139. package/dist/safe-bash-command-wkhtmltopdf/index.js +92 -38
  140. package/dist/safe-bash-command-wkhtmltopdf/pdf-renderer.d.ts +1 -1
  141. package/dist/safe-bash-command-xxd/index.js +78 -20
  142. package/dist/safe-bash-command-xz/command.d.ts +4 -0
  143. package/dist/safe-bash-command-xz/index.d.ts +1 -1
  144. package/dist/safe-bash-command-xz/index.js +1 -1
  145. package/dist/safe-bash-compression-engine/bounded-codec.js +3 -3
  146. package/dist/safe-bash-compression-engine/codec-loader.js +2 -2
  147. package/dist/safe-bash-compression-engine/files.js +6 -6
  148. package/dist/safe-bash-compression-engine/index.js +5 -5
  149. package/dist/safe-bash-compression-engine/operand.js +6 -6
  150. package/dist/safe-bash-compression-engine/stream.js +5 -5
  151. package/dist/safe-bash-compression-engine/zstd-decode.js +4 -4
  152. package/dist/safe-bash-contracts/command.js +3 -3
  153. package/dist/safe-bash-contracts/diagnostics.js +3 -3
  154. package/dist/safe-bash-contracts/escaping.js +9 -9
  155. package/dist/safe-bash-contracts/filesystem-descriptor.js +3 -3
  156. package/dist/safe-bash-contracts/filesystem-output.js +8 -8
  157. package/dist/safe-bash-contracts/index.js +21 -19
  158. package/dist/safe-bash-contracts/io.d.ts +12 -0
  159. package/dist/safe-bash-contracts/io.js +4 -1
  160. package/dist/safe-bash-contracts/output.js +3 -2
  161. package/dist/safe-bash-contracts/runtime-control.js +2 -2
  162. package/dist/safe-bash-contracts/signals.js +1 -1
  163. package/dist/safe-bash-contracts/yield.d.ts +1 -0
  164. package/dist/safe-bash-contracts/yield.js +3 -1
  165. package/dist/safe-bash-csv-engine/index.d.ts +10 -1
  166. package/dist/safe-bash-csv-engine/index.js +78 -6
  167. package/dist/safe-bash-io-engine/commands/search/requirements.js +3 -3
  168. package/dist/safe-bash-io-engine/commands/text-programs/shared.js +3 -3
  169. package/dist/safe-bash-io-engine/gnu-information.js +1 -1
  170. package/dist/safe-bash-io-engine/internal.d.ts +1 -1
  171. package/dist/safe-bash-io-engine/internal.js +2 -2
  172. package/dist/safe-bash-query-engine/index.js +4 -4
  173. package/dist/safe-bash-query-engine/interpreter.js +2 -2
  174. package/dist/safe-bash-query-engine/query-core.js +4 -4
  175. package/package.json +2 -2
  176. package/dist/safe-bash/opt-in/commands/cmp/compare.js +0 -95
  177. package/dist/safe-bash/opt-in/commands/cmp/io.js +0 -218
  178. package/dist/safe-bash/command-chunks/{chunk-FXYWQ6S4.js → chunk-2VTMZTV3.js} +0 -0
  179. package/dist/safe-bash/command-chunks/{chunk-RTOJNFFL.js → chunk-BJ6DGYJA.js} +4 -4
  180. package/dist/safe-bash/command-chunks/{chunk-DEH3CMGL.js → chunk-QJ3ONEBI.js} +3 -3
@@ -4,12 +4,103 @@ import {
4
4
 
5
5
  // packages/safe-bash-command-sqlite3/dist/index.js
6
6
  init_buffer();
7
+ import { yieldTurn as yieldTurn2 } from "../safe-bash-contracts/yield.js";
7
8
  import { writeFileOutput } from "../safe-bash-contracts/filesystem-output-budget.js";
8
9
  import { collectBytes, commandRuntimeIdentity, writeText } from "../safe-bash-contracts/index.js";
9
10
 
10
11
  // packages/safe-bash-command-sqlite3/dist/engine.js
11
12
  init_buffer();
12
13
 
14
+ // packages/safe-bash-command-sqlite3/dist/execution.js
15
+ init_buffer();
16
+ import { yieldTurn } from "../safe-bash-contracts/yield.js";
17
+ function runSynchronously(steps) {
18
+ let step = steps.next();
19
+ while (!step.done)
20
+ step = steps.next();
21
+ return step.value;
22
+ }
23
+ async function runCooperatively(steps, signal) {
24
+ let work = 0;
25
+ try {
26
+ signal.throwIfAborted();
27
+ let step = steps.next();
28
+ while (!step.done) {
29
+ signal.throwIfAborted();
30
+ if (++work === 2048) {
31
+ work = 0;
32
+ await yieldTurn(signal);
33
+ }
34
+ step = steps.next();
35
+ }
36
+ signal.throwIfAborted();
37
+ return step.value;
38
+ } finally {
39
+ steps.return(void 0);
40
+ }
41
+ }
42
+ function* stepMap(values, callback, context) {
43
+ const result = [];
44
+ for (let i = 0; i < values.length; i++) {
45
+ yield;
46
+ result.push(yield* callback.call(context, values[i], i, values));
47
+ }
48
+ return result;
49
+ }
50
+ function* stepFilter(values, callback, context) {
51
+ const result = [];
52
+ for (let i = 0; i < values.length; i++) {
53
+ yield;
54
+ if (yield* callback.call(context, values[i], i, values))
55
+ result.push(values[i]);
56
+ }
57
+ return result;
58
+ }
59
+ function* stepFlatMap(values, callback, context) {
60
+ const result = [];
61
+ for (let i = 0; i < values.length; i++) {
62
+ yield;
63
+ for (const value of yield* callback.call(context, values[i], i, values)) {
64
+ yield;
65
+ result.push(value);
66
+ }
67
+ }
68
+ return result;
69
+ }
70
+ function* stepReduce(values, callback, initial, context) {
71
+ let result = initial;
72
+ for (const value of values) {
73
+ yield;
74
+ result = yield* callback.call(context, result, value);
75
+ }
76
+ return result;
77
+ }
78
+ function* stepSort(values, compare, context) {
79
+ let source = values.slice();
80
+ let target = new Array(values.length);
81
+ for (let width = 1; width < values.length; width *= 2) {
82
+ for (let start = 0; start < values.length; start += 2 * width) {
83
+ const middle = Math.min(start + width, values.length);
84
+ const end = Math.min(start + 2 * width, values.length);
85
+ let left = start;
86
+ let right = middle;
87
+ for (let i = start; i < end; i++) {
88
+ yield;
89
+ if (left < middle && (right >= end || (yield* compare.call(context, source[left], source[right])) <= 0))
90
+ target[i] = source[left++];
91
+ else
92
+ target[i] = source[right++];
93
+ }
94
+ }
95
+ [source, target] = [target, source];
96
+ }
97
+ for (let i = 0; i < values.length; i++) {
98
+ yield;
99
+ values[i] = source[i];
100
+ }
101
+ return values;
102
+ }
103
+
13
104
  // packages/safe-bash-command-sqlite3/dist/btree.js
14
105
  init_buffer();
15
106
  var SQLITE_MAGIC = "SQLite format 3\0";
@@ -2029,6 +2120,17 @@ function sqlPrintf(format, args) {
2029
2120
  });
2030
2121
  }
2031
2122
  var SqliteDatabase = class {
2123
+ maxRows;
2124
+ constructor(maxRows = Infinity) {
2125
+ this.maxRows = maxRows;
2126
+ }
2127
+ checkRowCount(count) {
2128
+ if (count > this.maxRows)
2129
+ throw new RangeError("maxRows limit exceeded");
2130
+ }
2131
+ async executeStatementAsync(sql, signal) {
2132
+ return runCooperatively(this.executeStatementSteps(sql), signal);
2133
+ }
2032
2134
  tables = /* @__PURE__ */ new Map();
2033
2135
  indexes = /* @__PURE__ */ new Map();
2034
2136
  views = /* @__PURE__ */ new Map();
@@ -2049,9 +2151,10 @@ var SqliteDatabase = class {
2049
2151
  preparing = false;
2050
2152
  txSnapshot = null;
2051
2153
  savepoints = /* @__PURE__ */ new Map();
2052
- captureSnapshot() {
2154
+ *captureSnapshot() {
2053
2155
  const tables = /* @__PURE__ */ new Map();
2054
2156
  for (const [k, v] of this.tables) {
2157
+ yield;
2055
2158
  tables.set(k, cloneTableDef(v));
2056
2159
  }
2057
2160
  return {
@@ -2064,9 +2167,10 @@ var SqliteDatabase = class {
2064
2167
  schemaCookie: this.schemaCookie
2065
2168
  };
2066
2169
  }
2067
- restoreSnapshot(snap) {
2170
+ *restoreSnapshot(snap) {
2068
2171
  this.tables = /* @__PURE__ */ new Map();
2069
2172
  for (const [k, v] of snap.tables) {
2173
+ yield;
2070
2174
  this.tables.set(k, cloneTableDef(v));
2071
2175
  }
2072
2176
  this.indexes = new Map(snap.indexes);
@@ -2077,6 +2181,9 @@ var SqliteDatabase = class {
2077
2181
  this.schemaCookie = snap.schemaCookie;
2078
2182
  }
2079
2183
  loadFromBytes(bytes) {
2184
+ return runSynchronously(this.loadFromBytesSteps(bytes));
2185
+ }
2186
+ *loadFromBytesSteps(bytes) {
2080
2187
  if (bytes.byteLength === 0) {
2081
2188
  return;
2082
2189
  }
@@ -2084,7 +2191,7 @@ var SqliteDatabase = class {
2084
2191
  if (!image) {
2085
2192
  const text = textDecoder2.decode(bytes);
2086
2193
  if (/^\s*(CREATE|INSERT|BEGIN|PRAGMA|--)/i.test(text)) {
2087
- this.exec(text);
2194
+ yield* this.execSteps(text);
2088
2195
  return;
2089
2196
  }
2090
2197
  throw new Error("file is not a database");
@@ -2092,21 +2199,27 @@ var SqliteDatabase = class {
2092
2199
  this.userVersion = image.userVersion;
2093
2200
  this.applicationId = image.applicationId;
2094
2201
  this.schemaCookie = image.schemaCookie;
2095
- this.loadedMaster = image.master.map((m) => ({ ...m }));
2096
- const preservedMaster = image.master.map((m) => ({ ...m }));
2202
+ this.loadedMaster = image.master.map((m) => {
2203
+ return { ...m };
2204
+ });
2205
+ const preservedMaster = image.master.map((m) => {
2206
+ return { ...m };
2207
+ });
2097
2208
  for (const m of image.master) {
2209
+ yield;
2098
2210
  if (m.name.startsWith("sqlite_autoindex_") || m.name.toLowerCase() === "sqlite_sequence") {
2099
2211
  continue;
2100
2212
  }
2101
2213
  if (m.sql) {
2102
2214
  try {
2103
- this.exec(m.sql);
2215
+ yield* this.execSteps(m.sql);
2104
2216
  } catch {
2105
2217
  }
2106
2218
  }
2107
2219
  }
2108
2220
  this.loadedMaster = preservedMaster;
2109
2221
  for (const [tblName, rawRows] of image.tableRows.entries()) {
2222
+ yield;
2110
2223
  const tbl = this.findTable(tblName);
2111
2224
  if (!tbl) {
2112
2225
  continue;
@@ -2114,9 +2227,11 @@ var SqliteDatabase = class {
2114
2227
  tbl.rows = [];
2115
2228
  let maxRowid = 0;
2116
2229
  for (const r of rawRows) {
2230
+ yield;
2117
2231
  const data = {};
2118
2232
  let valIdx = 0;
2119
2233
  for (const col of tbl.columns) {
2234
+ yield;
2120
2235
  if (col.primaryKey && col.type.toUpperCase() === "INTEGER" && !tbl.withoutRowId && tbl.primaryKeyCols.length === 1) {
2121
2236
  const cellVal = r.values[valIdx];
2122
2237
  data[col.name] = cellVal === null || cellVal === void 0 ? r.rowid : cellVal;
@@ -2140,6 +2255,7 @@ var SqliteDatabase = class {
2140
2255
  const seqRows = image.tableRows.get("sqlite_sequence");
2141
2256
  if (seqRows) {
2142
2257
  for (const r of seqRows) {
2258
+ yield;
2143
2259
  const tName = String(r.values[0] ?? "");
2144
2260
  const seqVal = Number(r.values[1] ?? 0);
2145
2261
  const tbl = this.findTable(tName);
@@ -2154,6 +2270,7 @@ var SqliteDatabase = class {
2154
2270
  const meta = JSON.parse(image.metaJson);
2155
2271
  if (meta.autoInc) {
2156
2272
  for (const [k, v] of Object.entries(meta.autoInc)) {
2273
+ yield;
2157
2274
  const tbl = this.findTable(k);
2158
2275
  if (tbl) {
2159
2276
  tbl.maxAutoInc = Math.max(tbl.maxAutoInc, v);
@@ -2166,6 +2283,9 @@ var SqliteDatabase = class {
2166
2283
  }
2167
2284
  }
2168
2285
  serializeToBytes() {
2286
+ return runSynchronously(this.serializeToBytesSteps());
2287
+ }
2288
+ *serializeToBytesSteps() {
2169
2289
  const master = [];
2170
2290
  const tableRows = /* @__PURE__ */ new Map();
2171
2291
  const indexRows = /* @__PURE__ */ new Map();
@@ -2173,6 +2293,7 @@ var SqliteDatabase = class {
2173
2293
  let hasAutoIncTable = false;
2174
2294
  let rootPageCounter = 2;
2175
2295
  for (const tbl of this.tables.values()) {
2296
+ yield;
2176
2297
  master.push({
2177
2298
  type: "table",
2178
2299
  name: tbl.name,
@@ -2180,8 +2301,12 @@ var SqliteDatabase = class {
2180
2301
  rootpage: rootPageCounter++,
2181
2302
  sql: tbl.sql
2182
2303
  });
2183
- if (tbl.columns.some((c) => c.autoIncrement) || tbl.maxAutoInc > 0) {
2184
- if (tbl.columns.some((c) => c.autoIncrement)) {
2304
+ if (tbl.columns.some((c) => {
2305
+ return c.autoIncrement;
2306
+ }) || tbl.maxAutoInc > 0) {
2307
+ if (tbl.columns.some((c) => {
2308
+ return c.autoIncrement;
2309
+ })) {
2185
2310
  hasAutoIncTable = true;
2186
2311
  }
2187
2312
  if (tbl.maxAutoInc > 0) {
@@ -2191,13 +2316,16 @@ var SqliteDatabase = class {
2191
2316
  });
2192
2317
  }
2193
2318
  }
2194
- const rows = tbl.rows.map((r) => ({
2195
- rowid: r.rowid,
2196
- values: tbl.columns.map((c) => {
2197
- const isRowidAlias = c.primaryKey && c.type.toUpperCase() === "INTEGER" && !tbl.withoutRowId && tbl.primaryKeyCols.length === 1;
2198
- return isRowidAlias ? null : r.data[c.name] ?? null;
2199
- })
2200
- }));
2319
+ const rows = yield* stepMap(tbl.rows, function* (r) {
2320
+ yield;
2321
+ return {
2322
+ rowid: r.rowid,
2323
+ values: tbl.columns.map((c) => {
2324
+ const isRowidAlias = c.primaryKey && c.type.toUpperCase() === "INTEGER" && !tbl.withoutRowId && tbl.primaryKeyCols.length === 1;
2325
+ return isRowidAlias ? null : r.data[c.name] ?? null;
2326
+ })
2327
+ };
2328
+ }, this);
2201
2329
  tableRows.set(tbl.name, rows);
2202
2330
  }
2203
2331
  if ((hasAutoIncTable || seqRows.length > 0) && !this.findTable("sqlite_sequence")) {
@@ -2211,6 +2339,7 @@ var SqliteDatabase = class {
2211
2339
  tableRows.set("sqlite_sequence", seqRows);
2212
2340
  }
2213
2341
  for (const idx of this.indexes.values()) {
2342
+ yield;
2214
2343
  master.push({
2215
2344
  type: "index",
2216
2345
  name: idx.name,
@@ -2220,18 +2349,24 @@ var SqliteDatabase = class {
2220
2349
  });
2221
2350
  const tbl = this.findTable(idx.tableName);
2222
2351
  if (tbl) {
2223
- const entries = tbl.rows.map((r) => [
2224
- ...idx.columns.map((colName) => {
2225
- const cleaned = colName.replace(/\s+(ASC|DESC)$/i, "").trim();
2226
- const matchCol = tbl.columns.find((c) => c.name.toLowerCase() === cleaned.toLowerCase());
2227
- return matchCol ? r.data[matchCol.name] ?? null : null;
2228
- }),
2229
- r.rowid
2230
- ]);
2352
+ const entries = yield* stepMap(tbl.rows, function* (r) {
2353
+ yield;
2354
+ return [
2355
+ ...idx.columns.map((colName) => {
2356
+ const cleaned = colName.replace(/\s+(ASC|DESC)$/i, "").trim();
2357
+ const matchCol = tbl.columns.find((c) => {
2358
+ return c.name.toLowerCase() === cleaned.toLowerCase();
2359
+ });
2360
+ return matchCol ? r.data[matchCol.name] ?? null : null;
2361
+ }),
2362
+ r.rowid
2363
+ ];
2364
+ }, this);
2231
2365
  indexRows.set(idx.name, entries);
2232
2366
  }
2233
2367
  }
2234
2368
  for (const v of this.views.values()) {
2369
+ yield;
2235
2370
  master.push({
2236
2371
  type: "view",
2237
2372
  name: v.name,
@@ -2241,6 +2376,7 @@ var SqliteDatabase = class {
2241
2376
  });
2242
2377
  }
2243
2378
  for (const tr of this.triggers.values()) {
2379
+ yield;
2244
2380
  master.push({
2245
2381
  type: "trigger",
2246
2382
  name: tr.name,
@@ -2277,10 +2413,14 @@ var SqliteDatabase = class {
2277
2413
  return void 0;
2278
2414
  }
2279
2415
  exec(sql, positionalParams = []) {
2416
+ return runSynchronously(this.execSteps(sql, positionalParams));
2417
+ }
2418
+ *execSteps(sql, positionalParams = []) {
2280
2419
  const stmts = splitSqlStatements(sql);
2281
2420
  const results = [];
2282
2421
  for (const stmt of stmts) {
2283
- const res = this.executeStatement(stmt, positionalParams);
2422
+ yield;
2423
+ const res = yield* this.executeStatementSteps(stmt, positionalParams);
2284
2424
  if (res) {
2285
2425
  results.push(res);
2286
2426
  }
@@ -2288,6 +2428,9 @@ var SqliteDatabase = class {
2288
2428
  return results;
2289
2429
  }
2290
2430
  executeStatement(rawSql, positionalParams = [], cteScope = /* @__PURE__ */ new Map()) {
2431
+ return runSynchronously(this.executeStatementSteps(rawSql, positionalParams, cteScope));
2432
+ }
2433
+ *executeStatementSteps(rawSql, positionalParams = [], cteScope = /* @__PURE__ */ new Map()) {
2291
2434
  const sql = rawSql.trim().replace(/;+\s*$/, "");
2292
2435
  if (!sql) {
2293
2436
  return null;
@@ -2305,7 +2448,7 @@ var SqliteDatabase = class {
2305
2448
  }
2306
2449
  if (first === "BEGIN") {
2307
2450
  if (!this.inTransaction) {
2308
- this.txSnapshot = this.captureSnapshot();
2451
+ this.txSnapshot = yield* this.captureSnapshot();
2309
2452
  this.inTransaction = true;
2310
2453
  }
2311
2454
  return null;
@@ -2317,7 +2460,9 @@ var SqliteDatabase = class {
2317
2460
  return null;
2318
2461
  }
2319
2462
  if (first === "ROLLBACK") {
2320
- const toIdx = tokens.findIndex((t) => t.value.toUpperCase() === "TO");
2463
+ const toIdx = tokens.findIndex((t) => {
2464
+ return t.value.toUpperCase() === "TO";
2465
+ });
2321
2466
  if (toIdx !== -1) {
2322
2467
  let spIdx = toIdx + 1;
2323
2468
  if (tokens[spIdx]?.value.toUpperCase() === "SAVEPOINT") {
@@ -2328,11 +2473,11 @@ var SqliteDatabase = class {
2328
2473
  if (!snap) {
2329
2474
  throw new Error(`no such savepoint: ${spName}`);
2330
2475
  }
2331
- this.restoreSnapshot(snap);
2476
+ yield* this.restoreSnapshot(snap);
2332
2477
  return null;
2333
2478
  }
2334
2479
  if (this.txSnapshot) {
2335
- this.restoreSnapshot(this.txSnapshot);
2480
+ yield* this.restoreSnapshot(this.txSnapshot);
2336
2481
  }
2337
2482
  this.inTransaction = false;
2338
2483
  this.txSnapshot = null;
@@ -2341,7 +2486,7 @@ var SqliteDatabase = class {
2341
2486
  }
2342
2487
  if (first === "SAVEPOINT") {
2343
2488
  const spName = (tokens[1]?.value ?? "").toLowerCase();
2344
- this.savepoints.set(spName, this.captureSnapshot());
2489
+ this.savepoints.set(spName, yield* this.captureSnapshot());
2345
2490
  return null;
2346
2491
  }
2347
2492
  if (first === "RELEASE") {
@@ -2354,10 +2499,10 @@ var SqliteDatabase = class {
2354
2499
  return null;
2355
2500
  }
2356
2501
  if (first === "PRAGMA") {
2357
- return this.executePragma(tokens);
2502
+ return yield* this.executePragma(tokens);
2358
2503
  }
2359
2504
  if (first === "CREATE") {
2360
- this.executeCreate(sql, tokens, positionalParams);
2505
+ yield* this.executeCreate(sql, tokens, positionalParams);
2361
2506
  this.schemaCookie += 1;
2362
2507
  return null;
2363
2508
  }
@@ -2367,28 +2512,28 @@ var SqliteDatabase = class {
2367
2512
  return null;
2368
2513
  }
2369
2514
  if (first === "ALTER") {
2370
- this.executeAlter(tokens);
2515
+ yield* this.executeAlter(tokens);
2371
2516
  this.schemaCookie += 1;
2372
2517
  return null;
2373
2518
  }
2374
2519
  if (first === "INSERT" || first === "REPLACE") {
2375
- return this.executeInsert(sql, tokens, positionalParams, cteScope);
2520
+ return yield* this.executeInsert(sql, tokens, positionalParams, cteScope);
2376
2521
  }
2377
2522
  if (first === "UPDATE") {
2378
- return this.executeUpdate(tokens, positionalParams, cteScope);
2523
+ return yield* this.executeUpdate(tokens, positionalParams, cteScope);
2379
2524
  }
2380
2525
  if (first === "DELETE") {
2381
- return this.executeDelete(tokens, positionalParams, cteScope);
2526
+ return yield* this.executeDelete(tokens, positionalParams, cteScope);
2382
2527
  }
2383
2528
  if (first === "WITH") {
2384
- return this.executeWith(tokens, positionalParams, cteScope);
2529
+ return yield* this.executeWith(tokens, positionalParams, cteScope);
2385
2530
  }
2386
2531
  if (first === "SELECT" || first === "VALUES") {
2387
- return this.executeSelectCompound(tokens, positionalParams, cteScope);
2532
+ return yield* this.executeSelectCompound(tokens, positionalParams, cteScope);
2388
2533
  }
2389
2534
  throw new Error(`near "${tokens[0].raw}": syntax error`);
2390
2535
  }
2391
- executePragma(tokens) {
2536
+ *executePragma(tokens) {
2392
2537
  let idx = 1;
2393
2538
  if (tokens[idx + 1]?.value === ".") {
2394
2539
  idx += 2;
@@ -2468,7 +2613,9 @@ var SqliteDatabase = class {
2468
2613
  return { columns: ["cid", "name", "type", "notnull", "dflt_value", "pk"], rows: [] };
2469
2614
  }
2470
2615
  const rows = tbl.columns.map((c, i) => {
2471
- const pkIdx = tbl.primaryKeyCols.findIndex((p) => p.toLowerCase() === c.name.toLowerCase());
2616
+ const pkIdx = tbl.primaryKeyCols.findIndex((p) => {
2617
+ return p.toLowerCase() === c.name.toLowerCase();
2618
+ });
2472
2619
  const base = [
2473
2620
  i,
2474
2621
  c.name,
@@ -2490,6 +2637,7 @@ var SqliteDatabase = class {
2490
2637
  const rows = [];
2491
2638
  let seq = 0;
2492
2639
  for (const idxDef of this.indexes.values()) {
2640
+ yield;
2493
2641
  if (idxDef.tableName.toLowerCase() === tName) {
2494
2642
  rows.push([seq++, idxDef.name, idxDef.unique ? 1 : 0, "c", 0]);
2495
2643
  }
@@ -2499,6 +2647,7 @@ var SqliteDatabase = class {
2499
2647
  if (name === "index_info") {
2500
2648
  const iName = (arg ?? "").toLowerCase();
2501
2649
  for (const idxDef of this.indexes.values()) {
2650
+ yield;
2502
2651
  if (idxDef.name.toLowerCase() === iName) {
2503
2652
  const tbl = this.findTable(idxDef.tableName);
2504
2653
  const rows = idxDef.columns.map((colName, seqno) => {
@@ -2511,17 +2660,26 @@ var SqliteDatabase = class {
2511
2660
  return { columns: ["seqno", "cid", "name"], rows: [] };
2512
2661
  }
2513
2662
  if (name === "foreign_key_list") {
2514
- return { columns: ["id", "seq", "table", "from", "to", "on_update", "on_delete", "match"], rows: [] };
2663
+ return {
2664
+ columns: ["id", "seq", "table", "from", "to", "on_update", "on_delete", "match"],
2665
+ rows: []
2666
+ };
2515
2667
  }
2516
2668
  if (name === "compile_options") {
2517
2669
  return {
2518
2670
  columns: ["compile_options"],
2519
- rows: [["ENABLE_FTS5"], ["ENABLE_JSON1"], ["ENABLE_MATH_FUNCTIONS"], ["ENABLE_RTREE"], ["THREADSAFE=1"]]
2671
+ rows: [
2672
+ ["ENABLE_FTS5"],
2673
+ ["ENABLE_JSON1"],
2674
+ ["ENABLE_MATH_FUNCTIONS"],
2675
+ ["ENABLE_RTREE"],
2676
+ ["THREADSAFE=1"]
2677
+ ]
2520
2678
  };
2521
2679
  }
2522
2680
  return null;
2523
2681
  }
2524
- executeCreate(sql, tokens, positionalParams) {
2682
+ *executeCreate(sql, tokens, positionalParams) {
2525
2683
  let idx = 1;
2526
2684
  let unique = false;
2527
2685
  if (tokens[idx]?.value.toUpperCase() === "TEMP" || tokens[idx]?.value.toUpperCase() === "TEMPORARY") {
@@ -2550,28 +2708,35 @@ var SqliteDatabase = class {
2550
2708
  }
2551
2709
  throw new Error(`table ${objName} already exists`);
2552
2710
  }
2553
- const asIdx = tokens.findIndex((t, i) => i >= idx && t.value.toUpperCase() === "AS");
2711
+ const asIdx = tokens.findIndex((t, i) => {
2712
+ return i >= idx && t.value.toUpperCase() === "AS";
2713
+ });
2554
2714
  if (asIdx !== -1 && tokens[idx]?.value !== "(") {
2555
2715
  const selectTokens = tokens.slice(asIdx + 1);
2556
- const res = this.executeSelectCompound(selectTokens, positionalParams, /* @__PURE__ */ new Map());
2557
- const columns2 = res.columns.map((c) => ({
2558
- name: c,
2559
- type: "TEXT",
2560
- notNull: false,
2561
- primaryKey: false,
2562
- autoIncrement: false,
2563
- unique: false
2564
- }));
2565
- const rows = res.rows.map((r, rIdx) => {
2716
+ const res = yield* this.executeSelectCompound(selectTokens, positionalParams, /* @__PURE__ */ new Map());
2717
+ const columns2 = res.columns.map((c) => {
2718
+ return {
2719
+ name: c,
2720
+ type: "TEXT",
2721
+ notNull: false,
2722
+ primaryKey: false,
2723
+ autoIncrement: false,
2724
+ unique: false
2725
+ };
2726
+ });
2727
+ const rows = yield* stepMap(res.rows, function* (r, rIdx) {
2728
+ yield;
2566
2729
  const data = {};
2567
2730
  res.columns.forEach((c, cIdx) => {
2568
2731
  data[c] = r[cIdx] ?? null;
2569
2732
  });
2570
2733
  return { rowid: rIdx + 1, data };
2571
- });
2734
+ }, this);
2572
2735
  this.tables.set(objName, {
2573
2736
  name: objName,
2574
- sql: `CREATE TABLE ${objName}(${columns2.map((c) => `"${c.name}"`).join(",")})`,
2737
+ sql: `CREATE TABLE ${objName}(${columns2.map((c) => {
2738
+ return `"${c.name}"`;
2739
+ }).join(",")})`,
2575
2740
  columns: columns2,
2576
2741
  rows,
2577
2742
  nextRowId: rows.length + 1,
@@ -2585,6 +2750,7 @@ var SqliteDatabase = class {
2585
2750
  }
2586
2751
  let openParen = idx;
2587
2752
  while (openParen < tokens.length && tokens[openParen]?.value !== "(") {
2753
+ yield;
2588
2754
  openParen += 1;
2589
2755
  }
2590
2756
  let depth = 1;
@@ -2592,6 +2758,7 @@ var SqliteDatabase = class {
2592
2758
  const bodyParts = [];
2593
2759
  let curPart = [];
2594
2760
  while (closeParen < tokens.length && depth > 0) {
2761
+ yield;
2595
2762
  const t = tokens[closeParen];
2596
2763
  if (t.value === "(") {
2597
2764
  depth += 1;
@@ -2613,13 +2780,16 @@ var SqliteDatabase = class {
2613
2780
  }
2614
2781
  closeParen += 1;
2615
2782
  }
2616
- const trailingTokens = tokens.slice(closeParen + 1).map((t) => t.value.toUpperCase());
2783
+ const trailingTokens = tokens.slice(closeParen + 1).map((t) => {
2784
+ return t.value.toUpperCase();
2785
+ });
2617
2786
  const withoutRowId = trailingTokens.includes("WITHOUT") && trailingTokens.includes("ROWID");
2618
2787
  const strict = trailingTokens.includes("STRICT");
2619
2788
  const columns = [];
2620
2789
  const primaryKeyCols = [];
2621
2790
  const uniqueColSets = [];
2622
2791
  for (const part of bodyParts) {
2792
+ yield;
2623
2793
  if (part.length === 0) {
2624
2794
  continue;
2625
2795
  }
@@ -2633,6 +2803,7 @@ var SqliteDatabase = class {
2633
2803
  if (part[pIdx]?.value === "(") {
2634
2804
  pIdx += 1;
2635
2805
  while (pIdx < part.length && part[pIdx]?.value !== ")") {
2806
+ yield;
2636
2807
  const colName = part[pIdx].value;
2637
2808
  if (colName !== "," && colName.toUpperCase() !== "ASC" && colName.toUpperCase() !== "DESC") {
2638
2809
  primaryKeyCols.push(colName);
@@ -2648,6 +2819,7 @@ var SqliteDatabase = class {
2648
2819
  if (part[pIdx]?.value === "(") {
2649
2820
  pIdx += 1;
2650
2821
  while (pIdx < part.length && part[pIdx]?.value !== ")") {
2822
+ yield;
2651
2823
  const colName = part[pIdx].value;
2652
2824
  if (colName !== "," && colName.toUpperCase() !== "ASC" && colName.toUpperCase() !== "DESC") {
2653
2825
  uCols.push(colName);
@@ -2673,7 +2845,9 @@ var SqliteDatabase = class {
2673
2845
  }
2674
2846
  }
2675
2847
  if (primaryKeyCols.length === 1) {
2676
- const pkCol = columns.find((c) => c.name.toLowerCase() === primaryKeyCols[0].toLowerCase());
2848
+ const pkCol = columns.find((c) => {
2849
+ return c.name.toLowerCase() === primaryKeyCols[0].toLowerCase();
2850
+ });
2677
2851
  if (pkCol) {
2678
2852
  pkCol.primaryKey = true;
2679
2853
  }
@@ -2699,13 +2873,16 @@ var SqliteDatabase = class {
2699
2873
  }
2700
2874
  throw new Error(`index ${objName} already exists`);
2701
2875
  }
2702
- const onIdx = tokens.findIndex((t, i) => i >= idx && t.value.toUpperCase() === "ON");
2876
+ const onIdx = tokens.findIndex((t, i) => {
2877
+ return i >= idx && t.value.toUpperCase() === "ON";
2878
+ });
2703
2879
  const tableName = tokens[onIdx + 1]?.value ?? "";
2704
2880
  const cols = [];
2705
2881
  let p = onIdx + 2;
2706
2882
  if (tokens[p]?.value === "(") {
2707
2883
  p += 1;
2708
2884
  while (p < tokens.length && tokens[p]?.value !== ")") {
2885
+ yield;
2709
2886
  const v = tokens[p].value;
2710
2887
  if (v !== "," && v.toUpperCase() !== "ASC" && v.toUpperCase() !== "DESC" && v.toUpperCase() !== "COLLATE") {
2711
2888
  cols.push(v);
@@ -2740,6 +2917,7 @@ var SqliteDatabase = class {
2740
2917
  viewCols = [];
2741
2918
  idx += 1;
2742
2919
  while (idx < tokens.length && tokens[idx]?.value !== ")") {
2920
+ yield;
2743
2921
  if (tokens[idx].value !== ",") {
2744
2922
  viewCols.push(tokens[idx].value);
2745
2923
  }
@@ -2747,7 +2925,9 @@ var SqliteDatabase = class {
2747
2925
  }
2748
2926
  idx += 1;
2749
2927
  }
2750
- const asIdx = tokens.findIndex((t, i) => i >= idx && t.value.toUpperCase() === "AS");
2928
+ const asIdx = tokens.findIndex((t, i) => {
2929
+ return i >= idx && t.value.toUpperCase() === "AS";
2930
+ });
2751
2931
  const selectSql = reconstructTokensSql(tokens.slice(asIdx + 1));
2752
2932
  this.views.set(objName, {
2753
2933
  name: objName,
@@ -2785,6 +2965,7 @@ var SqliteDatabase = class {
2785
2965
  if (tokens[idx]?.value.toUpperCase() === "OF") {
2786
2966
  idx += 1;
2787
2967
  while (idx < tokens.length && tokens[idx]?.value.toUpperCase() !== "ON") {
2968
+ yield;
2788
2969
  idx += 1;
2789
2970
  }
2790
2971
  }
@@ -2801,6 +2982,7 @@ var SqliteDatabase = class {
2801
2982
  idx += 1;
2802
2983
  const whenToks = [];
2803
2984
  while (idx < tokens.length && tokens[idx]?.value.toUpperCase() !== "BEGIN") {
2985
+ yield;
2804
2986
  whenToks.push(tokens[idx]);
2805
2987
  idx += 1;
2806
2988
  }
@@ -3045,7 +3227,7 @@ var SqliteDatabase = class {
3045
3227
  this.triggers.delete(found);
3046
3228
  }
3047
3229
  }
3048
- executeAlter(tokens) {
3230
+ *executeAlter(tokens) {
3049
3231
  this.loadedMaster = null;
3050
3232
  let idx = 2;
3051
3233
  if (tokens[idx + 1]?.value === ".") {
@@ -3073,13 +3255,16 @@ var SqliteDatabase = class {
3073
3255
  }
3074
3256
  const oldCol = tokens[idx]?.value ?? "";
3075
3257
  const newCol = tokens[idx + 2]?.value ?? "";
3076
- const colObj = tbl.columns.find((c) => c.name.toLowerCase() === oldCol.toLowerCase());
3258
+ const colObj = tbl.columns.find((c) => {
3259
+ return c.name.toLowerCase() === oldCol.toLowerCase();
3260
+ });
3077
3261
  if (!colObj) {
3078
3262
  throw new Error(`in prepare, no such column: "${oldCol}"`);
3079
3263
  }
3080
3264
  const realOld = colObj.name;
3081
3265
  colObj.name = newCol;
3082
3266
  for (const r of tbl.rows) {
3267
+ yield;
3083
3268
  r.data[newCol] = r.data[realOld] ?? null;
3084
3269
  delete r.data[realOld];
3085
3270
  }
@@ -3091,8 +3276,9 @@ var SqliteDatabase = class {
3091
3276
  }
3092
3277
  const colDef = this.parseColumnDefTokens(tokens.slice(idx));
3093
3278
  tbl.columns.push(colDef);
3094
- const defVal = colDef.defaultExpr ? this.evalScalarSql(colDef.defaultExpr, {}, []) : null;
3279
+ const defVal = colDef.defaultExpr ? yield* this.evalScalarSql(colDef.defaultExpr, {}, []) : null;
3095
3280
  for (const r of tbl.rows) {
3281
+ yield;
3096
3282
  r.data[colDef.name] = defVal;
3097
3283
  }
3098
3284
  tbl.sql = tbl.sql.replace(/\)\s*$/, `, ${colDef.name} ${colDef.type})`);
@@ -3103,35 +3289,41 @@ var SqliteDatabase = class {
3103
3289
  idx += 1;
3104
3290
  }
3105
3291
  const dropCol = tokens[idx]?.value ?? "";
3106
- const colIdx = tbl.columns.findIndex((c) => c.name.toLowerCase() === dropCol.toLowerCase());
3292
+ const colIdx = tbl.columns.findIndex((c) => {
3293
+ return c.name.toLowerCase() === dropCol.toLowerCase();
3294
+ });
3107
3295
  if (colIdx === -1) {
3108
3296
  throw new Error(`in prepare, no such column: "${dropCol}"`);
3109
3297
  }
3110
3298
  const realName = tbl.columns[colIdx].name;
3111
3299
  tbl.columns.splice(colIdx, 1);
3112
3300
  for (const r of tbl.rows) {
3301
+ yield;
3113
3302
  delete r.data[realName];
3114
3303
  }
3115
3304
  }
3116
3305
  }
3117
- fireTriggers(tableName, timing, event, oldRow, newRow) {
3306
+ *fireTriggers(tableName, timing, event, oldRow, newRow) {
3118
3307
  for (const tr of this.triggers.values()) {
3308
+ yield;
3119
3309
  if (tr.tableName.toLowerCase() === tableName.toLowerCase() && tr.timing === timing && tr.event === event) {
3120
3310
  const ctx = {};
3121
3311
  if (oldRow) {
3122
3312
  for (const [k, v] of Object.entries(oldRow)) {
3313
+ yield;
3123
3314
  ctx[`OLD.${k}`] = v;
3124
3315
  ctx[`old.${k}`] = v;
3125
3316
  }
3126
3317
  }
3127
3318
  if (newRow) {
3128
3319
  for (const [k, v] of Object.entries(newRow)) {
3320
+ yield;
3129
3321
  ctx[`NEW.${k}`] = v;
3130
3322
  ctx[`new.${k}`] = v;
3131
3323
  }
3132
3324
  }
3133
3325
  if (tr.whenExpr) {
3134
- const cond = this.evalExpr(parseExprSql(tr.whenExpr), ctx, []);
3326
+ const cond = yield* this.evalExprSteps(parseExprSql(tr.whenExpr), ctx, []);
3135
3327
  if (!isTruthy(cond)) {
3136
3328
  continue;
3137
3329
  }
@@ -3146,7 +3338,7 @@ var SqliteDatabase = class {
3146
3338
  const val = matchKey ? source[matchKey] : null;
3147
3339
  return this.toSqlLiteral(val ?? null);
3148
3340
  });
3149
- this.exec(body);
3341
+ yield* this.execSteps(body);
3150
3342
  }
3151
3343
  }
3152
3344
  }
@@ -3173,8 +3365,9 @@ var SqliteDatabase = class {
3173
3365
  }
3174
3366
  return `X'${hex}'`;
3175
3367
  }
3176
- checkConstraintsAndConflicts(tbl, candidate, excludeRowid) {
3368
+ *checkConstraintsAndConflicts(tbl, candidate, excludeRowid) {
3177
3369
  for (const col of tbl.columns) {
3370
+ yield;
3178
3371
  const val = candidate.data[col.name] ?? null;
3179
3372
  if (col.notNull && val === null) {
3180
3373
  if (col.primaryKey && col.type.toUpperCase() === "INTEGER" && !tbl.withoutRowId) {
@@ -3184,7 +3377,7 @@ var SqliteDatabase = class {
3184
3377
  }
3185
3378
  }
3186
3379
  if (col.checkExpr && val !== null) {
3187
- const ok = this.evalExpr(parseExprSql(col.checkExpr), candidate.data, []);
3380
+ const ok = yield* this.evalExprSteps(parseExprSql(col.checkExpr), candidate.data, []);
3188
3381
  if (ok !== null && !isTruthy(ok)) {
3189
3382
  throw new Error(`CHECK constraint failed: ${col.checkExpr}`);
3190
3383
  }
@@ -3195,16 +3388,23 @@ var SqliteDatabase = class {
3195
3388
  keySets.push(tbl.primaryKeyCols);
3196
3389
  }
3197
3390
  for (const u of tbl.uniqueColSets) {
3391
+ yield;
3198
3392
  keySets.push(u);
3199
3393
  }
3200
3394
  if (keySets.length === 0 && candidate.rowid >= tbl.nextRowId) {
3201
3395
  return null;
3202
3396
  }
3203
- const resolvedKeySets = keySets.map((kSet) => kSet.map((colName) => {
3204
- const realCol = tbl.columns.find((c) => c.name.toLowerCase() === colName.toLowerCase());
3205
- return { cKey: realCol ? realCol.name : colName, collate: realCol?.collate ?? "BINARY" };
3206
- }));
3397
+ const resolvedKeySets = yield* stepMap(keySets, function* (kSet) {
3398
+ yield;
3399
+ return kSet.map((colName) => {
3400
+ const realCol = tbl.columns.find((c) => {
3401
+ return c.name.toLowerCase() === colName.toLowerCase();
3402
+ });
3403
+ return { cKey: realCol ? realCol.name : colName, collate: realCol?.collate ?? "BINARY" };
3404
+ });
3405
+ }, this);
3207
3406
  for (const existing of tbl.rows) {
3407
+ yield;
3208
3408
  if (excludeRowid !== void 0 && existing.rowid === excludeRowid) {
3209
3409
  continue;
3210
3410
  }
@@ -3212,8 +3412,10 @@ var SqliteDatabase = class {
3212
3412
  return existing;
3213
3413
  }
3214
3414
  for (const kSet of resolvedKeySets) {
3415
+ yield;
3215
3416
  let allEqual = true;
3216
3417
  for (const { cKey, collate } of kSet) {
3418
+ yield;
3217
3419
  const v1 = candidate.data[cKey] ?? null;
3218
3420
  const v2 = existing.data[cKey] ?? null;
3219
3421
  if (v1 === null || v2 === null || !sqlEquals(v1, v2, collate)) {
@@ -3228,7 +3430,7 @@ var SqliteDatabase = class {
3228
3430
  }
3229
3431
  return null;
3230
3432
  }
3231
- executeInsert(_sql, tokens, positionalParams, cteScope) {
3433
+ *executeInsert(_sql, tokens, positionalParams, cteScope) {
3232
3434
  let idx = 0;
3233
3435
  let conflictAction = "ABORT";
3234
3436
  if (tokens[idx]?.value.toUpperCase() === "REPLACE") {
@@ -3265,14 +3467,19 @@ var SqliteDatabase = class {
3265
3467
  throw new Error("near AS: syntax error");
3266
3468
  idx += 2;
3267
3469
  }
3268
- let targetCols = tbl.columns.map((c) => c.name);
3470
+ let targetCols = tbl.columns.map((c) => {
3471
+ return c.name;
3472
+ });
3269
3473
  if (tokens[idx]?.value === "(") {
3270
3474
  targetCols = [];
3271
3475
  idx += 1;
3272
3476
  while (idx < tokens.length && tokens[idx]?.value !== ")") {
3477
+ yield;
3273
3478
  if (tokens[idx].value !== ",") {
3274
3479
  const cName = tokens[idx].value;
3275
- const colMatch = tbl.columns.find((c) => c.name.toLowerCase() === cName.toLowerCase());
3480
+ const colMatch = tbl.columns.find((c) => {
3481
+ return c.name.toLowerCase() === cName.toLowerCase();
3482
+ });
3276
3483
  targetCols.push(colMatch ? colMatch.name : cName);
3277
3484
  }
3278
3485
  idx += 1;
@@ -3283,6 +3490,7 @@ var SqliteDatabase = class {
3283
3490
  let onConflictIdx = -1;
3284
3491
  let pDepth = 0;
3285
3492
  for (let i = idx; i < tokens.length; i += 1) {
3493
+ yield;
3286
3494
  if (tokens[i].value === "(") {
3287
3495
  pDepth += 1;
3288
3496
  } else if (tokens[i].value === ")") {
@@ -3304,12 +3512,14 @@ var SqliteDatabase = class {
3304
3512
  } else if (tokens[idx]?.value.toUpperCase() === "VALUES") {
3305
3513
  idx += 1;
3306
3514
  while (idx < endValuesIdx) {
3515
+ yield;
3307
3516
  if (tokens[idx]?.value === "(") {
3308
3517
  idx += 1;
3309
3518
  const rowToks = [];
3310
3519
  let cur = [];
3311
3520
  let d = 1;
3312
3521
  while (idx < endValuesIdx && d > 0) {
3522
+ yield;
3313
3523
  const t = tokens[idx++];
3314
3524
  if (t.value === "(") {
3315
3525
  d += 1;
@@ -3328,15 +3538,18 @@ var SqliteDatabase = class {
3328
3538
  cur.push(t);
3329
3539
  }
3330
3540
  }
3331
- const evaledRow = rowToks.map((rt) => this.evalExpr(new ExprParser(rt).parseExpression(), {}, positionalParams));
3541
+ const evaledRow = yield* stepMap(rowToks, function* (rt) {
3542
+ return yield* this.evalExprSteps(new ExprParser(rt).parseExpression(), {}, positionalParams);
3543
+ }, this);
3332
3544
  valueRows.push(evaledRow);
3333
3545
  } else {
3334
3546
  idx += 1;
3335
3547
  }
3336
3548
  }
3337
3549
  } else {
3338
- const selectRes = this.executeSelectCompound(tokens.slice(idx, endValuesIdx), positionalParams, cteScope);
3550
+ const selectRes = yield* this.executeSelectCompound(tokens.slice(idx, endValuesIdx), positionalParams, cteScope);
3339
3551
  for (const r of selectRes.rows) {
3552
+ yield;
3340
3553
  valueRows.push(r);
3341
3554
  }
3342
3555
  }
@@ -3347,12 +3560,14 @@ var SqliteDatabase = class {
3347
3560
  let uIdx = onConflictIdx + 2;
3348
3561
  if (tokens[uIdx]?.value === "(") {
3349
3562
  while (uIdx < tokens.length && tokens[uIdx]?.value !== ")") {
3563
+ yield;
3350
3564
  uIdx += 1;
3351
3565
  }
3352
3566
  uIdx += 1;
3353
3567
  }
3354
3568
  if (tokens[uIdx]?.value.toUpperCase() === "WHERE") {
3355
3569
  while (uIdx < tokens.length && tokens[uIdx]?.value.toUpperCase() !== "DO") {
3570
+ yield;
3356
3571
  uIdx += 1;
3357
3572
  }
3358
3573
  }
@@ -3371,32 +3586,40 @@ var SqliteDatabase = class {
3371
3586
  }
3372
3587
  const targetBindings = this.tableBindings(tbl, void 0, alias);
3373
3588
  const upsertBindings = { ...targetBindings };
3374
- for (const column of tbl.columns)
3589
+ for (const column of tbl.columns) {
3590
+ yield;
3375
3591
  upsertBindings[`excluded.${column.name}`] = null;
3592
+ }
3376
3593
  for (const assignment of upsertSetPairs) {
3377
- this.validateColumns({ kind: "column", name: assignment.col }, targetBindings, positionalParams, cteScope);
3378
- this.validateColumns(assignment.expr, upsertBindings, positionalParams, cteScope);
3594
+ yield;
3595
+ yield* this.validateColumns({ kind: "column", name: assignment.col }, targetBindings, positionalParams, cteScope);
3596
+ yield* this.validateColumns(assignment.expr, upsertBindings, positionalParams, cteScope);
3379
3597
  }
3380
- this.validateColumns(upsertWhere, upsertBindings, positionalParams, cteScope);
3598
+ yield* this.validateColumns(upsertWhere, upsertBindings, positionalParams, cteScope);
3381
3599
  let insertedCount = 0;
3382
3600
  const affectedRows = [];
3383
3601
  for (const vRow of valueRows) {
3602
+ yield;
3384
3603
  const data = {};
3385
3604
  for (const col of tbl.columns) {
3605
+ yield;
3386
3606
  if (col.defaultExpr !== void 0) {
3387
- data[col.name] = this.evalScalarSql(col.defaultExpr, {}, positionalParams);
3607
+ data[col.name] = yield* this.evalScalarSql(col.defaultExpr, {}, positionalParams);
3388
3608
  } else {
3389
3609
  data[col.name] = null;
3390
3610
  }
3391
3611
  }
3392
3612
  for (let c = 0; c < targetCols.length; c += 1) {
3613
+ yield;
3393
3614
  data[targetCols[c]] = vRow[c] ?? null;
3394
3615
  }
3395
3616
  for (const col of tbl.columns) {
3617
+ yield;
3396
3618
  data[col.name] = applyColumnAffinity(data[col.name] ?? null, col.type);
3397
3619
  }
3398
3620
  let rowid = tbl.nextRowId;
3399
3621
  for (const col of tbl.columns) {
3622
+ yield;
3400
3623
  if (col.autoIncrement) {
3401
3624
  rowid = Math.max(rowid, tbl.maxAutoInc + 1);
3402
3625
  }
@@ -3410,13 +3633,14 @@ var SqliteDatabase = class {
3410
3633
  }
3411
3634
  }
3412
3635
  for (const col of tbl.columns) {
3636
+ yield;
3413
3637
  if (col.generatedExpr) {
3414
- data[col.name] = applyColumnAffinity(this.evalScalarSql(col.generatedExpr, data, positionalParams), col.type);
3638
+ data[col.name] = applyColumnAffinity(yield* this.evalScalarSql(col.generatedExpr, data, positionalParams), col.type);
3415
3639
  }
3416
3640
  }
3417
3641
  const candidate = { rowid, data };
3418
- this.fireTriggers(tbl.name, "BEFORE", "INSERT", void 0, candidate.data);
3419
- const conflictRow = this.checkConstraintsAndConflicts(tbl, candidate);
3642
+ yield* this.fireTriggers(tbl.name, "BEFORE", "INSERT", void 0, candidate.data);
3643
+ const conflictRow = yield* this.checkConstraintsAndConflicts(tbl, candidate);
3420
3644
  if (conflictRow) {
3421
3645
  if (upsertDoNothing || conflictAction === "IGNORE") {
3422
3646
  continue;
@@ -3424,22 +3648,27 @@ var SqliteDatabase = class {
3424
3648
  if (upsertSetPairs.length > 0) {
3425
3649
  const ctx = this.tableBindings(tbl, conflictRow, alias);
3426
3650
  for (const [k, v] of Object.entries(conflictRow.data)) {
3651
+ yield;
3427
3652
  ctx[`${tbl.name}.${k}`] = v;
3428
3653
  }
3429
3654
  for (const [k, v] of Object.entries(candidate.data)) {
3655
+ yield;
3430
3656
  ctx[`excluded.${k}`] = v;
3431
3657
  ctx[`EXCLUDED.${k}`] = v;
3432
3658
  }
3433
3659
  if (upsertWhere) {
3434
- const cond = this.evalExpr(upsertWhere, ctx, positionalParams);
3660
+ const cond = yield* this.evalExprSteps(upsertWhere, ctx, positionalParams);
3435
3661
  if (!isTruthy(cond)) {
3436
3662
  continue;
3437
3663
  }
3438
3664
  }
3439
3665
  for (const assign of upsertSetPairs) {
3440
- const realCol = tbl.columns.find((c) => c.name.toLowerCase() === assign.col.toLowerCase());
3666
+ yield;
3667
+ const realCol = tbl.columns.find((c) => {
3668
+ return c.name.toLowerCase() === assign.col.toLowerCase();
3669
+ });
3441
3670
  const colKey = realCol ? realCol.name : assign.col;
3442
- conflictRow.data[colKey] = applyColumnAffinity(this.evalExpr(assign.expr, ctx, positionalParams), realCol?.type ?? "");
3671
+ conflictRow.data[colKey] = applyColumnAffinity(yield* this.evalExprSteps(assign.expr, ctx, positionalParams), realCol?.type ?? "");
3443
3672
  }
3444
3673
  insertedCount += 1;
3445
3674
  affectedRows.push(conflictRow);
@@ -3464,12 +3693,12 @@ var SqliteDatabase = class {
3464
3693
  this.lastInsertRowid = rowid;
3465
3694
  insertedCount += 1;
3466
3695
  affectedRows.push(candidate);
3467
- this.fireTriggers(tbl.name, "AFTER", "INSERT", void 0, candidate.data);
3696
+ yield* this.fireTriggers(tbl.name, "AFTER", "INSERT", void 0, candidate.data);
3468
3697
  }
3469
3698
  this.lastChanges = insertedCount;
3470
3699
  this.totalChanges += insertedCount;
3471
3700
  if (returningIdx !== -1) {
3472
- return this.evaluateReturning(tbl, affectedRows, tokens.slice(returningIdx + 1), positionalParams, cteScope);
3701
+ return yield* this.evaluateReturning(tbl, affectedRows, tokens.slice(returningIdx + 1), positionalParams, cteScope);
3473
3702
  }
3474
3703
  return null;
3475
3704
  }
@@ -3561,7 +3790,7 @@ var SqliteDatabase = class {
3561
3790
  }
3562
3791
  return bindings;
3563
3792
  }
3564
- executeUpdate(tokens, positionalParams, _cteScope) {
3793
+ *executeUpdate(tokens, positionalParams, _cteScope) {
3565
3794
  let idx = 1;
3566
3795
  if (tokens[idx]?.value.toUpperCase() === "OR") {
3567
3796
  idx += 2;
@@ -3586,6 +3815,7 @@ var SqliteDatabase = class {
3586
3815
  let returningIdx = -1;
3587
3816
  let pDepth = 0;
3588
3817
  for (let i = idx; i < tokens.length; i += 1) {
3818
+ yield;
3589
3819
  if (tokens[i].value === "(") {
3590
3820
  pDepth += 1;
3591
3821
  } else if (tokens[i].value === ")") {
@@ -3600,6 +3830,7 @@ var SqliteDatabase = class {
3600
3830
  let whereIdx = sliceEnd;
3601
3831
  let depth = 0;
3602
3832
  for (let i = idx; i < sliceEnd; i += 1) {
3833
+ yield;
3603
3834
  const token = tokens[i].value.toUpperCase();
3604
3835
  if (token === "(")
3605
3836
  depth += 1;
@@ -3612,65 +3843,74 @@ var SqliteDatabase = class {
3612
3843
  }
3613
3844
  const setTokens = fromIdx === -1 ? tokens.slice(idx, sliceEnd) : [...tokens.slice(idx, fromIdx), ...tokens.slice(whereIdx, sliceEnd)];
3614
3845
  const { assignments, whereExpr } = this.parseSetAssignments(setTokens);
3615
- const source = fromIdx === -1 ? { rows: [{}], schema: [] } : this.evaluateFromClause(tokens.slice(fromIdx + 1, whereIdx), positionalParams, _cteScope);
3846
+ const source = fromIdx === -1 ? { rows: [{}], schema: [] } : yield* this.evaluateFromClause(tokens.slice(fromIdx + 1, whereIdx), positionalParams, _cteScope);
3616
3847
  const targetBindings = this.tableBindings(tbl, void 0, alias);
3617
3848
  const bindings = { ...targetBindings };
3618
3849
  for (const table of source.schema) {
3850
+ yield;
3619
3851
  for (const column of table.columns) {
3852
+ yield;
3620
3853
  bindings[column] = null;
3621
3854
  bindings[`${table.tableAlias}.${column}`] = null;
3622
3855
  }
3623
3856
  }
3624
3857
  for (const assignment of assignments) {
3625
- this.validateColumns({ kind: "column", name: assignment.col }, targetBindings, positionalParams, _cteScope);
3626
- this.validateColumns(assignment.expr, bindings, positionalParams, _cteScope);
3858
+ yield;
3859
+ yield* this.validateColumns({ kind: "column", name: assignment.col }, targetBindings, positionalParams, _cteScope);
3860
+ yield* this.validateColumns(assignment.expr, bindings, positionalParams, _cteScope);
3627
3861
  }
3628
- this.validateColumns(whereExpr, bindings, positionalParams, _cteScope);
3862
+ yield* this.validateColumns(whereExpr, bindings, positionalParams, _cteScope);
3629
3863
  if (returningIdx !== -1)
3630
- this.evaluateReturning(tbl, [], tokens.slice(returningIdx + 1), positionalParams, _cteScope);
3864
+ yield* this.evaluateReturning(tbl, [], tokens.slice(returningIdx + 1), positionalParams, _cteScope);
3631
3865
  let updated = 0;
3632
3866
  const affectedRows = [];
3633
- const matches = tbl.rows.flatMap((row) => {
3867
+ const matches = yield* stepFlatMap(tbl.rows, function* (row) {
3634
3868
  const target = this.tableBindings(tbl, row, alias);
3635
3869
  for (const other of source.rows) {
3870
+ yield;
3636
3871
  const ctx = { ...other, ...target };
3637
- if (!whereExpr || isTruthy(this.evalExpr(whereExpr, ctx, positionalParams, _cteScope)))
3872
+ if (!whereExpr || isTruthy(yield* this.evalExprSteps(whereExpr, ctx, positionalParams, _cteScope)))
3638
3873
  return [{ row, ctx }];
3639
3874
  }
3640
3875
  return [];
3641
- });
3876
+ }, this);
3642
3877
  for (const { row, ctx } of matches) {
3878
+ yield;
3643
3879
  const oldData = { ...row.data };
3644
3880
  const newData = { ...row.data };
3645
3881
  for (const assign of assignments) {
3646
- const realCol = tbl.columns.find((c) => c.name.toLowerCase() === assign.col.toLowerCase());
3882
+ yield;
3883
+ const realCol = tbl.columns.find((c) => {
3884
+ return c.name.toLowerCase() === assign.col.toLowerCase();
3885
+ });
3647
3886
  const colKey = realCol ? realCol.name : assign.col;
3648
- newData[colKey] = applyColumnAffinity(this.evalExpr(assign.expr, ctx, positionalParams, _cteScope), realCol?.type ?? "");
3887
+ newData[colKey] = applyColumnAffinity(yield* this.evalExprSteps(assign.expr, ctx, positionalParams, _cteScope), realCol?.type ?? "");
3649
3888
  }
3650
3889
  for (const col of tbl.columns) {
3890
+ yield;
3651
3891
  if (col.generatedExpr) {
3652
- newData[col.name] = applyColumnAffinity(this.evalScalarSql(col.generatedExpr, newData, positionalParams), col.type);
3892
+ newData[col.name] = applyColumnAffinity(yield* this.evalScalarSql(col.generatedExpr, newData, positionalParams), col.type);
3653
3893
  }
3654
3894
  }
3655
- this.fireTriggers(tbl.name, "BEFORE", "UPDATE", oldData, newData);
3895
+ yield* this.fireTriggers(tbl.name, "BEFORE", "UPDATE", oldData, newData);
3656
3896
  const candidate = { rowid: row.rowid, data: newData };
3657
- const conflict = this.checkConstraintsAndConflicts(tbl, candidate, row.rowid);
3897
+ const conflict = yield* this.checkConstraintsAndConflicts(tbl, candidate, row.rowid);
3658
3898
  if (conflict) {
3659
3899
  throw new Error(`UNIQUE constraint failed: ${tbl.name}`);
3660
3900
  }
3661
3901
  row.data = newData;
3662
3902
  updated += 1;
3663
3903
  affectedRows.push(row);
3664
- this.fireTriggers(tbl.name, "AFTER", "UPDATE", oldData, newData);
3904
+ yield* this.fireTriggers(tbl.name, "AFTER", "UPDATE", oldData, newData);
3665
3905
  }
3666
3906
  this.lastChanges = updated;
3667
3907
  this.totalChanges += updated;
3668
3908
  if (returningIdx !== -1) {
3669
- return this.evaluateReturning(tbl, affectedRows, tokens.slice(returningIdx + 1), positionalParams, _cteScope);
3909
+ return yield* this.evaluateReturning(tbl, affectedRows, tokens.slice(returningIdx + 1), positionalParams, _cteScope);
3670
3910
  }
3671
3911
  return null;
3672
3912
  }
3673
- executeDelete(tokens, positionalParams, _cteScope) {
3913
+ *executeDelete(tokens, positionalParams, _cteScope) {
3674
3914
  let idx = 1;
3675
3915
  if (tokens[idx]?.value.toUpperCase() === "FROM") {
3676
3916
  idx += 1;
@@ -3699,6 +3939,7 @@ var SqliteDatabase = class {
3699
3939
  let returningIdx = -1;
3700
3940
  let pDepth = 0;
3701
3941
  for (let i = idx; i < tokens.length; i += 1) {
3942
+ yield;
3702
3943
  if (tokens[i].value === "(") {
3703
3944
  pDepth += 1;
3704
3945
  } else if (tokens[i].value === ")") {
@@ -3713,39 +3954,42 @@ var SqliteDatabase = class {
3713
3954
  if (idx < sliceEnd && tokens[idx]?.value.toUpperCase() === "WHERE") {
3714
3955
  whereExpr = new ExprParser(tokens.slice(idx + 1, sliceEnd)).parseExpression();
3715
3956
  }
3716
- this.validateColumns(whereExpr, this.tableBindings(tbl, void 0, alias), positionalParams, _cteScope);
3957
+ yield* this.validateColumns(whereExpr, this.tableBindings(tbl, void 0, alias), positionalParams, _cteScope);
3717
3958
  if (returningIdx !== -1)
3718
- this.evaluateReturning(tbl, [], tokens.slice(returningIdx + 1), positionalParams, _cteScope);
3959
+ yield* this.evaluateReturning(tbl, [], tokens.slice(returningIdx + 1), positionalParams, _cteScope);
3719
3960
  const kept = [];
3720
3961
  const deletedRows = [];
3721
3962
  for (const row of tbl.rows) {
3963
+ yield;
3722
3964
  const ctx = this.tableBindings(tbl, row, alias);
3723
3965
  if (whereExpr) {
3724
- const cond = this.evalExpr(whereExpr, ctx, positionalParams, _cteScope);
3966
+ const cond = yield* this.evalExprSteps(whereExpr, ctx, positionalParams, _cteScope);
3725
3967
  if (!isTruthy(cond)) {
3726
3968
  kept.push(row);
3727
3969
  continue;
3728
3970
  }
3729
3971
  }
3730
- this.fireTriggers(tbl.name, "BEFORE", "DELETE", row.data, void 0);
3972
+ yield* this.fireTriggers(tbl.name, "BEFORE", "DELETE", row.data, void 0);
3731
3973
  deletedRows.push(row);
3732
- this.fireTriggers(tbl.name, "AFTER", "DELETE", row.data, void 0);
3974
+ yield* this.fireTriggers(tbl.name, "AFTER", "DELETE", row.data, void 0);
3733
3975
  }
3734
3976
  tbl.rows = kept;
3735
3977
  this.lastChanges = deletedRows.length;
3736
3978
  this.totalChanges += deletedRows.length;
3737
3979
  if (returningIdx !== -1) {
3738
- return this.evaluateReturning(tbl, deletedRows, tokens.slice(returningIdx + 1), positionalParams, _cteScope);
3980
+ return yield* this.evaluateReturning(tbl, deletedRows, tokens.slice(returningIdx + 1), positionalParams, _cteScope);
3739
3981
  }
3740
3982
  return null;
3741
3983
  }
3742
- evaluateReturning(tbl, rows, returningTokens, positionalParams, cteScope = /* @__PURE__ */ new Map()) {
3984
+ *evaluateReturning(tbl, rows, returningTokens, positionalParams, cteScope = /* @__PURE__ */ new Map()) {
3743
3985
  const items = this.splitTopLevelComma(returningTokens);
3744
3986
  const outCols = [];
3745
3987
  const exprs = [];
3746
3988
  for (const itemToks of items) {
3989
+ yield;
3747
3990
  if (itemToks.length === 1 && itemToks[0].value === "*") {
3748
3991
  for (const c of tbl.columns) {
3992
+ yield;
3749
3993
  outCols.push(c.name);
3750
3994
  exprs.push({ kind: "column", name: c.name });
3751
3995
  }
@@ -3756,15 +4000,19 @@ var SqliteDatabase = class {
3756
4000
  }
3757
4001
  }
3758
4002
  const bindings = this.tableBindings(tbl);
3759
- for (const expr of exprs)
3760
- this.validateColumns(expr, bindings, positionalParams, cteScope);
3761
- const outRows = rows.map((r) => {
4003
+ for (const expr of exprs) {
4004
+ yield;
4005
+ yield* this.validateColumns(expr, bindings, positionalParams, cteScope);
4006
+ }
4007
+ const outRows = yield* stepMap(rows, function* (r) {
3762
4008
  const ctx = this.tableBindings(tbl, r);
3763
- return exprs.map((e) => this.evalExpr(e, ctx, positionalParams, cteScope));
3764
- });
4009
+ return yield* stepMap(exprs, function* (e) {
4010
+ return yield* this.evalExprSteps(e, ctx, positionalParams, cteScope);
4011
+ }, this);
4012
+ }, this);
3765
4013
  return { columns: outCols, rows: outRows };
3766
4014
  }
3767
- executeWith(tokens, positionalParams, outerCtes) {
4015
+ *executeWith(tokens, positionalParams, outerCtes) {
3768
4016
  const cteMap = new Map(outerCtes);
3769
4017
  let idx = 1;
3770
4018
  let isRecursive = false;
@@ -3773,6 +4021,7 @@ var SqliteDatabase = class {
3773
4021
  idx += 1;
3774
4022
  }
3775
4023
  while (idx < tokens.length) {
4024
+ yield;
3776
4025
  const cteName = tokens[idx].value;
3777
4026
  idx += 1;
3778
4027
  let explicitCols;
@@ -3780,6 +4029,7 @@ var SqliteDatabase = class {
3780
4029
  explicitCols = [];
3781
4030
  idx += 1;
3782
4031
  while (idx < tokens.length && tokens[idx]?.value !== ")") {
4032
+ yield;
3783
4033
  if (tokens[idx].value !== ",") {
3784
4034
  explicitCols.push(tokens[idx].value);
3785
4035
  }
@@ -3802,6 +4052,7 @@ var SqliteDatabase = class {
3802
4052
  let depth = 1;
3803
4053
  const bodyTokens = [];
3804
4054
  while (idx < tokens.length && depth > 0) {
4055
+ yield;
3805
4056
  const t = tokens[idx++];
3806
4057
  if (t.value === "(") {
3807
4058
  depth += 1;
@@ -3814,10 +4065,10 @@ var SqliteDatabase = class {
3814
4065
  bodyTokens.push(t);
3815
4066
  }
3816
4067
  if (isRecursive && this.containsTokenWord(bodyTokens, cteName)) {
3817
- const evaluated = this.evalRecursiveCte(cteName, explicitCols, bodyTokens, positionalParams, cteMap);
4068
+ const evaluated = yield* this.evalRecursiveCte(cteName, explicitCols, bodyTokens, positionalParams, cteMap);
3818
4069
  cteMap.set(cteName.toLowerCase(), evaluated);
3819
4070
  } else {
3820
- const res = this.executeSelectCompound(bodyTokens, positionalParams, cteMap);
4071
+ const res = yield* this.executeSelectCompound(bodyTokens, positionalParams, cteMap);
3821
4072
  const cols = explicitCols && explicitCols.length > 0 ? explicitCols : res.columns;
3822
4073
  cteMap.set(cteName.toLowerCase(), { columns: cols, rows: res.rows });
3823
4074
  }
@@ -3829,17 +4080,18 @@ var SqliteDatabase = class {
3829
4080
  }
3830
4081
  const mainTokens = tokens.slice(idx);
3831
4082
  const mainSql = reconstructTokensSql(mainTokens);
3832
- return this.executeStatement(mainSql, positionalParams, cteMap);
4083
+ return yield* this.executeStatementSteps(mainSql, positionalParams, cteMap);
3833
4084
  }
3834
4085
  containsTokenWord(tokens, word) {
3835
4086
  const lower = word.toLowerCase();
3836
4087
  return tokens.some((t) => t.value.toLowerCase() === lower);
3837
4088
  }
3838
- evalRecursiveCte(cteName, explicitCols, bodyTokens, positionalParams, cteMap) {
4089
+ *evalRecursiveCte(cteName, explicitCols, bodyTokens, positionalParams, cteMap) {
3839
4090
  let d = 0;
3840
4091
  let unionIdx = -1;
3841
4092
  let unionAll = false;
3842
4093
  for (let i = 0; i < bodyTokens.length; i += 1) {
4094
+ yield;
3843
4095
  const t = bodyTokens[i];
3844
4096
  if (t.value === "(") {
3845
4097
  d += 1;
@@ -3852,41 +4104,43 @@ var SqliteDatabase = class {
3852
4104
  }
3853
4105
  }
3854
4106
  if (unionIdx === -1) {
3855
- const res = this.executeSelectCompound(bodyTokens, positionalParams, cteMap);
4107
+ const res = yield* this.executeSelectCompound(bodyTokens, positionalParams, cteMap);
3856
4108
  return { columns: explicitCols ?? res.columns, rows: res.rows };
3857
4109
  }
3858
4110
  const anchorTokens = bodyTokens.slice(0, unionIdx);
3859
4111
  const recTokens = bodyTokens.slice(unionIdx + (unionAll ? 2 : 1));
3860
- const anchorRes = this.executeSelectCompound(anchorTokens, positionalParams, cteMap);
4112
+ const anchorRes = yield* this.executeSelectCompound(anchorTokens, positionalParams, cteMap);
3861
4113
  const cols = explicitCols && explicitCols.length > 0 ? explicitCols : anchorRes.columns;
3862
4114
  const scope = new Map(cteMap);
3863
4115
  scope.set(cteName.toLowerCase(), { columns: cols, rows: [] });
3864
4116
  const preparing = this.preparing;
3865
4117
  this.preparing = true;
3866
4118
  try {
3867
- this.executeSelectCompound(recTokens, positionalParams, scope);
4119
+ yield* this.executeSelectCompound(recTokens, positionalParams, scope);
3868
4120
  } finally {
3869
4121
  this.preparing = preparing;
3870
4122
  }
3871
4123
  if (this.preparing)
3872
4124
  return { columns: cols, rows: [] };
4125
+ this.checkRowCount(anchorRes.rows.length);
3873
4126
  const allRows = [...anchorRes.rows];
3874
4127
  let workingRows = [...anchorRes.rows];
3875
4128
  const seenKeys = /* @__PURE__ */ new Set();
3876
4129
  if (!unionAll) {
3877
4130
  for (const r of allRows) {
4131
+ yield;
3878
4132
  seenKeys.add(serializeSqlJson(r, false));
3879
4133
  }
3880
4134
  }
3881
- let iterations = 0;
3882
- const stepMap = new Map(cteMap);
4135
+ const stepMap2 = new Map(cteMap);
3883
4136
  const cteKey = cteName.toLowerCase();
3884
- while (workingRows.length > 0 && iterations < 5e3) {
3885
- iterations += 1;
3886
- stepMap.set(cteKey, { columns: cols, rows: workingRows });
3887
- const nextRes = this.executeSelectCompound(recTokens, positionalParams, stepMap);
4137
+ while (workingRows.length > 0) {
4138
+ yield;
4139
+ stepMap2.set(cteKey, { columns: cols, rows: workingRows });
4140
+ const nextRes = yield* this.executeSelectCompound(recTokens, positionalParams, stepMap2);
3888
4141
  const nextWorking = [];
3889
4142
  for (const r of nextRes.rows) {
4143
+ yield;
3890
4144
  if (!unionAll) {
3891
4145
  const k = serializeSqlJson(r, false);
3892
4146
  if (seenKeys.has(k)) {
@@ -3894,6 +4148,7 @@ var SqliteDatabase = class {
3894
4148
  }
3895
4149
  seenKeys.add(k);
3896
4150
  }
4151
+ this.checkRowCount(allRows.length + 1);
3897
4152
  allRows.push(r);
3898
4153
  nextWorking.push(r);
3899
4154
  }
@@ -3902,15 +4157,19 @@ var SqliteDatabase = class {
3902
4157
  return { columns: cols, rows: allRows };
3903
4158
  }
3904
4159
  singleSelectCache = /* @__PURE__ */ new WeakMap();
3905
- executeSelectCompound(tokens, positionalParams, cteScope) {
4160
+ *executeSelectCompound(tokens, positionalParams, cteScope) {
3906
4161
  if (tokens[0]?.value.toUpperCase() === "WITH") {
3907
- return this.executeWith(tokens, positionalParams, cteScope) ?? { columns: [], rows: [] };
4162
+ return (yield* this.executeWith(tokens, positionalParams, cteScope)) ?? {
4163
+ columns: [],
4164
+ rows: []
4165
+ };
3908
4166
  }
3909
4167
  const segments = [];
3910
4168
  let d = 0;
3911
4169
  let start = 0;
3912
4170
  let pendingOp = "NONE";
3913
4171
  for (let i = 0; i < tokens.length; i += 1) {
4172
+ yield;
3914
4173
  const t = tokens[i];
3915
4174
  if (t.value === "(") {
3916
4175
  d += 1;
@@ -3931,12 +4190,13 @@ var SqliteDatabase = class {
3931
4190
  }
3932
4191
  }
3933
4192
  if (segments.length === 0) {
3934
- return this.executeSingleSelect(tokens, positionalParams, cteScope);
4193
+ return yield* this.executeSingleSelect(tokens, positionalParams, cteScope);
3935
4194
  }
3936
4195
  const lastSegTokens = tokens.slice(start);
3937
4196
  let orderLimitStart = -1;
3938
4197
  d = 0;
3939
4198
  for (let i = 0; i < lastSegTokens.length; i += 1) {
4199
+ yield;
3940
4200
  const t = lastSegTokens[i];
3941
4201
  if (t.value === "(") {
3942
4202
  d += 1;
@@ -3955,34 +4215,49 @@ var SqliteDatabase = class {
3955
4215
  segments.push({ op: pendingOp, tokens: cleanLastTokens });
3956
4216
  let acc = { columns: [], rows: [] };
3957
4217
  for (const seg of segments) {
3958
- const res = this.executeSingleSelect(seg.tokens, positionalParams, cteScope);
4218
+ yield;
4219
+ const res = yield* this.executeSingleSelect(seg.tokens, positionalParams, cteScope);
3959
4220
  if (seg.op === "NONE") {
3960
4221
  acc = res;
3961
4222
  } else if (seg.op === "UNION ALL") {
4223
+ this.checkRowCount(acc.rows.length + res.rows.length);
3962
4224
  acc.rows.push(...res.rows);
3963
4225
  } else if (seg.op === "UNION") {
3964
4226
  const combined = [...acc.rows, ...res.rows];
3965
- acc.rows = this.deduplicateResultRows(combined);
4227
+ acc.rows = yield* this.deduplicateResultRows(combined);
3966
4228
  } else if (seg.op === "INTERSECT") {
3967
- const rightSet = new Set(res.rows.map((r) => serializeSqlJson(r, false)));
3968
- acc.rows = this.deduplicateResultRows(acc.rows.filter((r) => rightSet.has(serializeSqlJson(r, false))));
4229
+ const rightSet = new Set(yield* stepMap(res.rows, function* (r) {
4230
+ yield;
4231
+ return serializeSqlJson(r, false);
4232
+ }, this));
4233
+ acc.rows = yield* this.deduplicateResultRows(yield* stepFilter(acc.rows, function* (r) {
4234
+ yield;
4235
+ return rightSet.has(serializeSqlJson(r, false));
4236
+ }, this));
3969
4237
  } else if (seg.op === "EXCEPT") {
3970
- const rightSet = new Set(res.rows.map((r) => serializeSqlJson(r, false)));
3971
- acc.rows = this.deduplicateResultRows(acc.rows.filter((r) => !rightSet.has(serializeSqlJson(r, false))));
4238
+ const rightSet = new Set(yield* stepMap(res.rows, function* (r) {
4239
+ yield;
4240
+ return serializeSqlJson(r, false);
4241
+ }, this));
4242
+ acc.rows = yield* this.deduplicateResultRows(yield* stepFilter(acc.rows, function* (r) {
4243
+ yield;
4244
+ return !rightSet.has(serializeSqlJson(r, false));
4245
+ }, this));
3972
4246
  }
3973
4247
  }
3974
4248
  if (tailClauses.length > 0) {
3975
4249
  const tmpScope = new Map(cteScope);
3976
4250
  tmpScope.set("__compound_res__", acc);
3977
4251
  const wrapToks = tokenizeSql(`SELECT * FROM __compound_res__ ${reconstructTokensSql(tailClauses)}`);
3978
- return this.executeSingleSelect(wrapToks, positionalParams, tmpScope);
4252
+ return yield* this.executeSingleSelect(wrapToks, positionalParams, tmpScope);
3979
4253
  }
3980
4254
  return acc;
3981
4255
  }
3982
- deduplicateResultRows(rows) {
4256
+ *deduplicateResultRows(rows) {
3983
4257
  const out = [];
3984
4258
  const seen = /* @__PURE__ */ new Set();
3985
4259
  for (const r of rows) {
4260
+ yield;
3986
4261
  const k = serializeSqlJson(r, false);
3987
4262
  if (!seen.has(k)) {
3988
4263
  seen.add(k);
@@ -3991,18 +4266,20 @@ var SqliteDatabase = class {
3991
4266
  }
3992
4267
  return out;
3993
4268
  }
3994
- executeSingleSelect(tokens, positionalParams, cteScope) {
4269
+ *executeSingleSelect(tokens, positionalParams, cteScope) {
3995
4270
  if (tokens[0]?.value.toUpperCase() === "VALUES") {
3996
4271
  let idx = 1;
3997
4272
  const rows = [];
3998
4273
  let maxCols = 0;
3999
4274
  while (idx < tokens.length) {
4275
+ yield;
4000
4276
  if (tokens[idx]?.value === "(") {
4001
4277
  idx += 1;
4002
4278
  const exprs = [];
4003
4279
  let cur = [];
4004
4280
  let d = 1;
4005
4281
  while (idx < tokens.length && d > 0) {
4282
+ yield;
4006
4283
  const t = tokens[idx++];
4007
4284
  if (t.value === "(") {
4008
4285
  d += 1;
@@ -4021,7 +4298,9 @@ var SqliteDatabase = class {
4021
4298
  cur.push(t);
4022
4299
  }
4023
4300
  }
4024
- const r = exprs.map((et) => this.evalExpr(new ExprParser(et).parseExpression(), {}, positionalParams));
4301
+ const r = yield* stepMap(exprs, function* (et) {
4302
+ return yield* this.evalExprSteps(new ExprParser(et).parseExpression(), {}, positionalParams);
4303
+ }, this);
4025
4304
  maxCols = Math.max(maxCols, r.length);
4026
4305
  rows.push(r);
4027
4306
  } else {
@@ -4044,6 +4323,7 @@ var SqliteDatabase = class {
4044
4323
  const clausePositions = [];
4045
4324
  let d = 0;
4046
4325
  for (let i = idx; i < tokens.length; i += 1) {
4326
+ yield;
4047
4327
  const t = tokens[i];
4048
4328
  if (t.value === "(") {
4049
4329
  d += 1;
@@ -4081,8 +4361,12 @@ var SqliteDatabase = class {
4081
4361
  const limitTokens2 = getClauseTokens("LIMIT");
4082
4362
  const whereExpr2 = whereTokens && whereTokens.length > 0 ? new ExprParser(whereTokens).parseExpression() : void 0;
4083
4363
  const targetItems2 = this.splitTopLevelComma(selectListTokens);
4084
- const hasWildcard = targetItems2.some((itemToks) => itemToks.length === 1 && itemToks[0].value === "*" || itemToks.length === 3 && itemToks[1].value === "." && itemToks[2].value === "*");
4085
- const staticTargets2 = hasWildcard ? void 0 : targetItems2.map((itemToks) => this.parseSelectTarget(itemToks));
4364
+ const hasWildcard = targetItems2.some((itemToks) => {
4365
+ return itemToks.length === 1 && itemToks[0].value === "*" || itemToks.length === 3 && itemToks[1].value === "." && itemToks[2].value === "*";
4366
+ });
4367
+ const staticTargets2 = hasWildcard ? void 0 : targetItems2.map((itemToks) => {
4368
+ return this.parseSelectTarget(itemToks);
4369
+ });
4086
4370
  plan = {
4087
4371
  distinct: distinct2,
4088
4372
  fromTokens: fromTokens2,
@@ -4100,7 +4384,7 @@ var SqliteDatabase = class {
4100
4384
  let workingRows = [{}];
4101
4385
  let sourceSchema = [];
4102
4386
  if (fromTokens && fromTokens.length > 0) {
4103
- const built = this.evaluateFromClause(fromTokens, positionalParams, cteScope);
4387
+ const built = yield* this.evaluateFromClause(fromTokens, positionalParams, cteScope);
4104
4388
  workingRows = built.rows;
4105
4389
  sourceSchema = built.schema;
4106
4390
  }
@@ -4110,9 +4394,12 @@ var SqliteDatabase = class {
4110
4394
  } else {
4111
4395
  selectTargets = [];
4112
4396
  for (const itemToks of targetItems) {
4397
+ yield;
4113
4398
  if (itemToks.length === 1 && itemToks[0].value === "*") {
4114
4399
  for (const src of sourceSchema) {
4400
+ yield;
4115
4401
  for (const col of src.columns) {
4402
+ yield;
4116
4403
  selectTargets.push({
4117
4404
  expr: { kind: "column", table: src.tableAlias || void 0, name: col },
4118
4405
  alias: col
@@ -4121,9 +4408,12 @@ var SqliteDatabase = class {
4121
4408
  }
4122
4409
  } else if (itemToks.length === 3 && itemToks[1].value === "." && itemToks[2].value === "*") {
4123
4410
  const tblAlias = itemToks[0].value;
4124
- const src = sourceSchema.find((s) => s.tableAlias.toLowerCase() === tblAlias.toLowerCase());
4411
+ const src = sourceSchema.find((s) => {
4412
+ return s.tableAlias.toLowerCase() === tblAlias.toLowerCase();
4413
+ });
4125
4414
  if (src) {
4126
4415
  for (const col of src.columns) {
4416
+ yield;
4127
4417
  selectTargets.push({
4128
4418
  expr: { kind: "column", table: src.tableAlias, name: col },
4129
4419
  alias: col
@@ -4137,21 +4427,29 @@ var SqliteDatabase = class {
4137
4427
  }
4138
4428
  const bindings = {};
4139
4429
  for (const source of sourceSchema) {
4430
+ yield;
4140
4431
  for (const column of [...source.columns, "rowid", "_rowid_", "oid"]) {
4432
+ yield;
4141
4433
  bindings[column] = null;
4142
4434
  bindings[`${source.tableAlias}.${column}`] = null;
4143
4435
  }
4144
4436
  }
4145
- for (const target of selectTargets)
4146
- this.validateColumns(target.expr, bindings, positionalParams, cteScope);
4437
+ for (const target of selectTargets) {
4438
+ yield;
4439
+ yield* this.validateColumns(target.expr, bindings, positionalParams, cteScope);
4440
+ }
4147
4441
  const aliasBindings = { ...bindings };
4148
- for (const target of selectTargets)
4442
+ for (const target of selectTargets) {
4443
+ yield;
4149
4444
  aliasBindings[target.alias] = null;
4150
- this.validateColumns(whereExpr, aliasBindings, positionalParams, cteScope);
4445
+ }
4446
+ yield* this.validateColumns(whereExpr, aliasBindings, positionalParams, cteScope);
4151
4447
  for (const clause of [groupByTokens, havingTokens, orderByTokens]) {
4448
+ yield;
4152
4449
  if (clause) {
4153
4450
  for (const expression of this.splitTopLevelComma(clause)) {
4154
- this.validateColumns(new ExprParser(expression).parseExpression(), aliasBindings, positionalParams, cteScope);
4451
+ yield;
4452
+ yield* this.validateColumns(new ExprParser(expression).parseExpression(), aliasBindings, positionalParams, cteScope);
4155
4453
  }
4156
4454
  }
4157
4455
  }
@@ -4159,42 +4457,57 @@ var SqliteDatabase = class {
4159
4457
  let depth = 0;
4160
4458
  let start = 0;
4161
4459
  for (let i = 0; i <= limitTokens.length; i += 1) {
4460
+ yield;
4162
4461
  const token = limitTokens[i]?.value;
4163
4462
  if (token === "(")
4164
4463
  depth += 1;
4165
4464
  if (token === ")")
4166
4465
  depth -= 1;
4167
4466
  if (i === limitTokens.length || depth === 0 && (token === "," || token?.toUpperCase() === "OFFSET")) {
4168
- this.validateColumns(new ExprParser(limitTokens.slice(start, i)).parseExpression(), {}, positionalParams, cteScope);
4467
+ yield* this.validateColumns(new ExprParser(limitTokens.slice(start, i)).parseExpression(), {}, positionalParams, cteScope);
4169
4468
  start = i + 1;
4170
4469
  }
4171
4470
  }
4172
4471
  }
4173
4472
  if (this.preparing)
4174
- return { columns: selectTargets.map((target) => target.alias), rows: [] };
4473
+ return {
4474
+ columns: selectTargets.map((target) => {
4475
+ return target.alias;
4476
+ }),
4477
+ rows: []
4478
+ };
4175
4479
  if (whereExpr) {
4176
- workingRows = workingRows.filter((row) => isTruthy(this.evalExpr(whereExpr, row, positionalParams, cteScope)));
4480
+ workingRows = yield* stepFilter(workingRows, function* (row) {
4481
+ return isTruthy(yield* this.evalExprSteps(whereExpr, row, positionalParams, cteScope));
4482
+ }, this);
4177
4483
  }
4178
4484
  const hasGroupBy = Boolean(groupByTokens && groupByTokens.length > 0);
4179
- const hasAggregateInSelect = selectTargets.some((t) => this.containsAggregate(t.expr));
4485
+ const hasAggregateInSelect = selectTargets.some((t) => {
4486
+ return this.containsAggregate(t.expr);
4487
+ });
4180
4488
  const hasAggregateInHaving = havingTokens ? this.containsAggregate(new ExprParser(havingTokens).parseExpression()) : false;
4181
4489
  const isAggregatedQuery = hasGroupBy || hasAggregateInSelect || hasAggregateInHaving;
4182
4490
  let groupedRows = [];
4183
4491
  if (isAggregatedQuery) {
4184
4492
  if (hasGroupBy) {
4185
- const groupExprs = this.splitTopLevelComma(groupByTokens).map((gt) => new ExprParser(gt).parseExpression());
4493
+ const groupExprs = this.splitTopLevelComma(groupByTokens).map((gt) => {
4494
+ return new ExprParser(gt).parseExpression();
4495
+ });
4186
4496
  const groups = /* @__PURE__ */ new Map();
4187
4497
  for (const row of workingRows) {
4188
- const keyVals = groupExprs.map((ge) => {
4498
+ yield;
4499
+ const keyVals = yield* stepMap(groupExprs, function* (ge) {
4189
4500
  if (ge.kind === "literal" && typeof ge.value === "number" && Number.isInteger(ge.value)) {
4190
4501
  const idx1 = ge.value - 1;
4191
4502
  if (selectTargets[idx1]) {
4192
- return this.evalExpr(selectTargets[idx1].expr, row, positionalParams, cteScope);
4503
+ return yield* this.evalExprSteps(selectTargets[idx1].expr, row, positionalParams, cteScope);
4193
4504
  }
4194
4505
  }
4195
- return this.evalExpr(ge, row, positionalParams, cteScope);
4196
- });
4197
- const keyStr = serializeSqlJson(keyVals.map((v) => [typeof v, v]), false);
4506
+ return yield* this.evalExprSteps(ge, row, positionalParams, cteScope);
4507
+ }, this);
4508
+ const keyStr = serializeSqlJson(keyVals.map((v) => {
4509
+ return [typeof v, v];
4510
+ }), false);
4198
4511
  const existing = groups.get(keyStr);
4199
4512
  if (existing) {
4200
4513
  existing.group.push(row);
@@ -4208,45 +4521,51 @@ var SqliteDatabase = class {
4208
4521
  }
4209
4522
  if (havingTokens && havingTokens.length > 0) {
4210
4523
  const havingExpr = new ExprParser(havingTokens).parseExpression();
4211
- groupedRows = groupedRows.filter((g) => {
4524
+ groupedRows = yield* stepFilter(groupedRows, function* (g) {
4212
4525
  const repCtx = { ...g.representative };
4213
4526
  for (const st of selectTargets) {
4527
+ yield;
4214
4528
  if (!(st.alias in repCtx)) {
4215
- repCtx[st.alias] = this.evalExprWithAgg(st.expr, g.representative, g.group, positionalParams, cteScope);
4529
+ repCtx[st.alias] = yield* this.evalExprWithAgg(st.expr, g.representative, g.group, positionalParams, cteScope);
4216
4530
  }
4217
4531
  }
4218
- return isTruthy(this.evalExprWithAgg(havingExpr, repCtx, g.group, positionalParams, cteScope));
4219
- });
4532
+ return isTruthy(yield* this.evalExprWithAgg(havingExpr, repCtx, g.group, positionalParams, cteScope));
4533
+ }, this);
4220
4534
  }
4221
4535
  } else {
4222
- groupedRows = workingRows.map((r) => ({ representative: r, group: [r] }));
4536
+ groupedRows = yield* stepMap(workingRows, function* (r) {
4537
+ yield;
4538
+ return { representative: r, group: [r] };
4539
+ }, this);
4223
4540
  }
4224
4541
  const windowResults = /* @__PURE__ */ new Map();
4225
4542
  for (const st of selectTargets) {
4226
- this.collectWindowExprs(st.expr, groupedRows, positionalParams, cteScope, windowResults);
4543
+ yield;
4544
+ yield* this.collectWindowExprs(st.expr, groupedRows, positionalParams, cteScope, windowResults);
4227
4545
  }
4228
- const projected = groupedRows.map((g, rowIdx) => {
4546
+ const projected = yield* stepMap(groupedRows, function* (g, rowIdx) {
4229
4547
  const repCtx = { ...g.representative };
4230
- const values = selectTargets.map((st) => {
4231
- const val = this.evalExprWithAggAndWindow(st.expr, repCtx, g.group, rowIdx, windowResults, positionalParams, cteScope);
4548
+ const values = yield* stepMap(selectTargets, function* (st) {
4549
+ const val = yield* this.evalExprWithAggAndWindow(st.expr, repCtx, g.group, rowIdx, windowResults, positionalParams, cteScope);
4232
4550
  if (!(st.alias in repCtx)) {
4233
4551
  repCtx[st.alias] = val;
4234
4552
  }
4235
4553
  return val;
4236
- });
4554
+ }, this);
4237
4555
  return { values, ctx: repCtx, group: g.group };
4238
- });
4556
+ }, this);
4239
4557
  let finalProjected = projected;
4240
4558
  if (distinct) {
4241
4559
  const seen = /* @__PURE__ */ new Set();
4242
- finalProjected = projected.filter((p) => {
4560
+ finalProjected = yield* stepFilter(projected, function* (p) {
4561
+ yield;
4243
4562
  const k = serializeSqlJson(p.values, false);
4244
4563
  if (seen.has(k)) {
4245
4564
  return false;
4246
4565
  }
4247
4566
  seen.add(k);
4248
4567
  return true;
4249
- });
4568
+ }, this);
4250
4569
  }
4251
4570
  if (orderByTokens && orderByTokens.length > 0) {
4252
4571
  const orderSpecs = this.splitTopLevelComma(orderByTokens).map((ot) => {
@@ -4272,8 +4591,9 @@ var SqliteDatabase = class {
4272
4591
  }
4273
4592
  return { expr, desc, nulls, collation };
4274
4593
  });
4275
- finalProjected.sort((a, b) => {
4594
+ yield* stepSort(finalProjected, function* (a, b) {
4276
4595
  for (const spec of orderSpecs) {
4596
+ yield;
4277
4597
  let vA;
4278
4598
  let vB;
4279
4599
  if (spec.expr.kind === "literal" && typeof spec.expr.value === "number" && Number.isInteger(spec.expr.value)) {
@@ -4281,8 +4601,8 @@ var SqliteDatabase = class {
4281
4601
  vA = a.values[colIdx] ?? null;
4282
4602
  vB = b.values[colIdx] ?? null;
4283
4603
  } else {
4284
- vA = this.evalExprWithAgg(spec.expr, a.ctx, a.group, positionalParams, cteScope);
4285
- vB = this.evalExprWithAgg(spec.expr, b.ctx, b.group, positionalParams, cteScope);
4604
+ vA = yield* this.evalExprWithAgg(spec.expr, a.ctx, a.group, positionalParams, cteScope);
4605
+ vB = yield* this.evalExprWithAgg(spec.expr, b.ctx, b.group, positionalParams, cteScope);
4286
4606
  }
4287
4607
  if (vA === null && vB === null) {
4288
4608
  continue;
@@ -4311,21 +4631,25 @@ var SqliteDatabase = class {
4311
4631
  }
4312
4632
  }
4313
4633
  return 0;
4314
- });
4634
+ }, this);
4315
4635
  }
4316
4636
  if (limitTokens && limitTokens.length > 0) {
4317
- const offsetIdx = limitTokens.findIndex((t) => t.value.toUpperCase() === "OFFSET");
4318
- const commaIdx = limitTokens.findIndex((t) => t.value === ",");
4637
+ const offsetIdx = limitTokens.findIndex((t) => {
4638
+ return t.value.toUpperCase() === "OFFSET";
4639
+ });
4640
+ const commaIdx = limitTokens.findIndex((t) => {
4641
+ return t.value === ",";
4642
+ });
4319
4643
  let limitVal = -1;
4320
4644
  let offsetVal = 0;
4321
4645
  if (offsetIdx !== -1) {
4322
- limitVal = Math.trunc(toSqlNumber(this.evalExpr(new ExprParser(limitTokens.slice(0, offsetIdx)).parseExpression(), {}, positionalParams)));
4323
- offsetVal = Math.trunc(toSqlNumber(this.evalExpr(new ExprParser(limitTokens.slice(offsetIdx + 1)).parseExpression(), {}, positionalParams)));
4646
+ limitVal = Math.trunc(toSqlNumber(yield* this.evalExprSteps(new ExprParser(limitTokens.slice(0, offsetIdx)).parseExpression(), {}, positionalParams)));
4647
+ offsetVal = Math.trunc(toSqlNumber(yield* this.evalExprSteps(new ExprParser(limitTokens.slice(offsetIdx + 1)).parseExpression(), {}, positionalParams)));
4324
4648
  } else if (commaIdx !== -1) {
4325
- offsetVal = Math.trunc(toSqlNumber(this.evalExpr(new ExprParser(limitTokens.slice(0, commaIdx)).parseExpression(), {}, positionalParams)));
4326
- limitVal = Math.trunc(toSqlNumber(this.evalExpr(new ExprParser(limitTokens.slice(commaIdx + 1)).parseExpression(), {}, positionalParams)));
4649
+ offsetVal = Math.trunc(toSqlNumber(yield* this.evalExprSteps(new ExprParser(limitTokens.slice(0, commaIdx)).parseExpression(), {}, positionalParams)));
4650
+ limitVal = Math.trunc(toSqlNumber(yield* this.evalExprSteps(new ExprParser(limitTokens.slice(commaIdx + 1)).parseExpression(), {}, positionalParams)));
4327
4651
  } else {
4328
- limitVal = Math.trunc(toSqlNumber(this.evalExpr(new ExprParser(limitTokens).parseExpression(), {}, positionalParams)));
4652
+ limitVal = Math.trunc(toSqlNumber(yield* this.evalExprSteps(new ExprParser(limitTokens).parseExpression(), {}, positionalParams)));
4329
4653
  }
4330
4654
  if (offsetVal > 0) {
4331
4655
  finalProjected = finalProjected.slice(offsetVal);
@@ -4335,11 +4659,15 @@ var SqliteDatabase = class {
4335
4659
  }
4336
4660
  }
4337
4661
  return {
4338
- columns: selectTargets.map((t) => t.alias),
4339
- rows: finalProjected.map((p) => p.values)
4662
+ columns: selectTargets.map((t) => {
4663
+ return t.alias;
4664
+ }),
4665
+ rows: finalProjected.map((p) => {
4666
+ return p.values;
4667
+ })
4340
4668
  };
4341
4669
  }
4342
- evaluateFromClause(fromTokens, positionalParams, cteScope) {
4670
+ *evaluateFromClause(fromTokens, positionalParams, cteScope) {
4343
4671
  const items = [];
4344
4672
  let i = 0;
4345
4673
  const consumeJoinSource = () => {
@@ -4404,6 +4732,7 @@ var SqliteDatabase = class {
4404
4732
  const firstSrc = consumeJoinSource();
4405
4733
  items.push({ joinType: "INNER", natural: false, sourceTokens: firstSrc });
4406
4734
  while (i < fromTokens.length) {
4735
+ yield;
4407
4736
  let joinType = "INNER";
4408
4737
  let natural = false;
4409
4738
  if (fromTokens[i]?.value === ",") {
@@ -4411,6 +4740,7 @@ var SqliteDatabase = class {
4411
4740
  i += 1;
4412
4741
  } else {
4413
4742
  while (i < fromTokens.length) {
4743
+ yield;
4414
4744
  const u = fromTokens[i].value.toUpperCase();
4415
4745
  if (u === "NATURAL") {
4416
4746
  natural = true;
@@ -4450,18 +4780,26 @@ var SqliteDatabase = class {
4450
4780
  let currentRows = [];
4451
4781
  const schema = [];
4452
4782
  for (let itemIdx = 0; itemIdx < items.length; itemIdx += 1) {
4783
+ yield;
4453
4784
  const item = items[itemIdx];
4454
4785
  const preparationScope = {};
4455
4786
  if (this.preparing || currentRows.length === 0) {
4456
4787
  for (const source of schema) {
4788
+ yield;
4457
4789
  for (const column of [...source.columns, "rowid", "_rowid_", "oid"]) {
4790
+ yield;
4458
4791
  preparationScope[column] = null;
4459
4792
  preparationScope[`${source.tableAlias}.${column}`] = null;
4460
4793
  }
4461
4794
  }
4462
4795
  }
4463
- const resolved = this.resolveSingleTableSource(item.sourceTokens, this.preparing || currentRows.length === 0 ? [preparationScope] : currentRows, positionalParams, cteScope, itemIdx > 0 && currentRows.length === 0);
4464
- const prevCols = new Set(schema.flatMap((s) => s.columns.map((c) => c.toLowerCase())));
4796
+ const resolved = yield* this.resolveSingleTableSource(item.sourceTokens, this.preparing || currentRows.length === 0 ? [preparationScope] : currentRows, positionalParams, cteScope, itemIdx > 0 && currentRows.length === 0);
4797
+ const prevCols = new Set(yield* stepFlatMap(schema, function* (s) {
4798
+ yield;
4799
+ return s.columns.map((c) => {
4800
+ return c.toLowerCase();
4801
+ });
4802
+ }, this));
4465
4803
  schema.push({ tableAlias: resolved.alias, columns: resolved.columns });
4466
4804
  if (itemIdx === 0) {
4467
4805
  currentRows = resolved.rows;
@@ -4471,30 +4809,39 @@ var SqliteDatabase = class {
4471
4809
  const onExpr = item.onTokens ? new ExprParser(item.onTokens).parseExpression() : void 0;
4472
4810
  const joinBindings = {};
4473
4811
  for (const source of schema) {
4812
+ yield;
4474
4813
  for (const column of [...source.columns, "rowid", "_rowid_", "oid"]) {
4814
+ yield;
4475
4815
  joinBindings[column] = null;
4476
4816
  joinBindings[`${source.tableAlias}.${column}`] = null;
4477
4817
  }
4478
4818
  }
4479
- this.validateColumns(onExpr, joinBindings, positionalParams, cteScope);
4480
- const usingList = item.natural ? resolved.columns.filter((c) => prevCols.has(c.toLowerCase())) : item.usingCols;
4819
+ yield* this.validateColumns(onExpr, joinBindings, positionalParams, cteScope);
4820
+ const usingList = item.natural ? yield* stepFilter(resolved.columns, function* (c) {
4821
+ yield;
4822
+ return prevCols.has(c.toLowerCase());
4823
+ }, this) : item.usingCols;
4481
4824
  const rightMatched = /* @__PURE__ */ new Set();
4482
4825
  for (const leftRow of currentRows) {
4483
- const candidateRightRows = resolved.isCorrelated ? this.resolveSingleTableSource(item.sourceTokens, [leftRow], positionalParams, cteScope).rows : resolved.rows;
4826
+ yield;
4827
+ const candidateRightRows = resolved.isCorrelated ? (yield* this.resolveSingleTableSource(item.sourceTokens, [leftRow], positionalParams, cteScope)).rows : resolved.rows;
4484
4828
  let matchedLeft = false;
4485
4829
  for (let rIdx = 0; rIdx < candidateRightRows.length; rIdx += 1) {
4830
+ yield;
4486
4831
  const rightRow = candidateRightRows[rIdx];
4487
4832
  const merged = { ...leftRow };
4488
4833
  for (const [k, v] of Object.entries(rightRow)) {
4834
+ yield;
4489
4835
  if (k.includes(".") || !(k in merged) || merged[k] === null) {
4490
4836
  merged[k] = v;
4491
4837
  }
4492
4838
  }
4493
4839
  let matches = true;
4494
4840
  if (onExpr) {
4495
- matches = isTruthy(this.evalExpr(onExpr, merged, positionalParams, cteScope));
4841
+ matches = isTruthy(yield* this.evalExprSteps(onExpr, merged, positionalParams, cteScope));
4496
4842
  } else if (usingList && usingList.length > 0) {
4497
4843
  for (const uCol of usingList) {
4844
+ yield;
4498
4845
  const lVal = this.lookupColInRow(leftRow, void 0, uCol);
4499
4846
  const rVal = this.lookupColInRow(rightRow, resolved.alias, uCol);
4500
4847
  if (lVal === null || rVal === null || !sqlEquals(lVal, rVal)) {
@@ -4506,32 +4853,39 @@ var SqliteDatabase = class {
4506
4853
  if (matches) {
4507
4854
  matchedLeft = true;
4508
4855
  rightMatched.add(rIdx);
4856
+ this.checkRowCount(nextRows.length + 1);
4509
4857
  nextRows.push(merged);
4510
4858
  }
4511
4859
  }
4512
4860
  if (!matchedLeft && (item.joinType === "LEFT" || item.joinType === "FULL")) {
4513
4861
  const nullPadded = { ...leftRow };
4514
4862
  for (const col of resolved.columns) {
4863
+ yield;
4515
4864
  nullPadded[`${resolved.alias}.${col}`] = null;
4516
4865
  if (!(col in nullPadded)) {
4517
4866
  nullPadded[col] = null;
4518
4867
  }
4519
4868
  }
4869
+ this.checkRowCount(nextRows.length + 1);
4520
4870
  nextRows.push(nullPadded);
4521
4871
  }
4522
4872
  }
4523
4873
  if (item.joinType === "RIGHT" || item.joinType === "FULL") {
4524
4874
  for (let rIdx = 0; rIdx < resolved.rows.length; rIdx += 1) {
4875
+ yield;
4525
4876
  if (!rightMatched.has(rIdx)) {
4526
4877
  const rightRow = resolved.rows[rIdx];
4527
4878
  const nullPadded = {};
4528
4879
  for (const s of schema.slice(0, -1)) {
4880
+ yield;
4529
4881
  for (const col of s.columns) {
4882
+ yield;
4530
4883
  nullPadded[`${s.tableAlias}.${col}`] = null;
4531
4884
  nullPadded[col] = null;
4532
4885
  }
4533
4886
  }
4534
4887
  Object.assign(nullPadded, rightRow);
4888
+ this.checkRowCount(nextRows.length + 1);
4535
4889
  nextRows.push(nullPadded);
4536
4890
  }
4537
4891
  }
@@ -4540,7 +4894,7 @@ var SqliteDatabase = class {
4540
4894
  }
4541
4895
  return { rows: currentRows, schema };
4542
4896
  }
4543
- resolveSingleTableSource(srcTokens, outerRows, positionalParams, cteScope, prepareTvf = false) {
4897
+ *resolveSingleTableSource(srcTokens, outerRows, positionalParams, cteScope, prepareTvf = false) {
4544
4898
  if (srcTokens.length === 0) {
4545
4899
  return { alias: "", columns: [], rows: [{}] };
4546
4900
  }
@@ -4549,6 +4903,7 @@ var SqliteDatabase = class {
4549
4903
  let p = 1;
4550
4904
  const inner = [];
4551
4905
  while (p < srcTokens.length && d > 0) {
4906
+ yield;
4552
4907
  const t = srcTokens[p++];
4553
4908
  if (t.value === "(") {
4554
4909
  d += 1;
@@ -4564,18 +4919,19 @@ var SqliteDatabase = class {
4564
4919
  p += 1;
4565
4920
  }
4566
4921
  const alias2 = srcTokens[p]?.value ?? "subquery";
4567
- const res = this.executeStatement(reconstructTokensSql(inner), positionalParams, cteScope) ?? {
4922
+ const res = (yield* this.executeStatementSteps(reconstructTokensSql(inner), positionalParams, cteScope)) ?? {
4568
4923
  columns: [],
4569
4924
  rows: []
4570
4925
  };
4571
- const rows2 = res.rows.map((r) => {
4926
+ const rows2 = yield* stepMap(res.rows, function* (r) {
4927
+ yield;
4572
4928
  const obj = {};
4573
4929
  res.columns.forEach((c, idx2) => {
4574
4930
  obj[c] = r[idx2] ?? null;
4575
4931
  obj[`${alias2}.${c}`] = r[idx2] ?? null;
4576
4932
  });
4577
4933
  return obj;
4578
- });
4934
+ }, this);
4579
4935
  return { alias: alias2, columns: res.columns, rows: rows2 };
4580
4936
  }
4581
4937
  let idx = 0;
@@ -4590,6 +4946,7 @@ var SqliteDatabase = class {
4590
4946
  let cur = [];
4591
4947
  let d = 1;
4592
4948
  while (idx < srcTokens.length && d > 0) {
4949
+ yield;
4593
4950
  const t = srcTokens[idx++];
4594
4951
  if (t.value === "(") {
4595
4952
  d += 1;
@@ -4615,25 +4972,30 @@ var SqliteDatabase = class {
4615
4972
  }
4616
4973
  const alias2 = srcTokens[idx]?.value ?? name;
4617
4974
  const outerCtx = outerRows[0] ?? {};
4618
- const isCorrelated = argToks.some((at) => at.some((t) => t.type === "word" || t.type === "ident"));
4975
+ const isCorrelated = argToks.some((at) => {
4976
+ return at.some((t) => {
4977
+ return t.type === "word" || t.type === "ident";
4978
+ });
4979
+ });
4619
4980
  const prepare = this.preparing || prepareTvf && isCorrelated;
4620
- const args = argToks.map((at) => {
4981
+ const args = yield* stepMap(argToks, function* (at) {
4621
4982
  const expression = new ExprParser(at).parseExpression();
4622
4983
  if (prepare) {
4623
- this.validateColumns(expression, outerCtx, positionalParams, cteScope);
4984
+ yield* this.validateColumns(expression, outerCtx, positionalParams, cteScope);
4624
4985
  return null;
4625
4986
  }
4626
- return this.evalExpr(expression, outerCtx, positionalParams, cteScope);
4627
- });
4987
+ return yield* this.evalExprSteps(expression, outerCtx, positionalParams, cteScope);
4988
+ }, this);
4628
4989
  const preparing = this.preparing;
4629
4990
  this.preparing = prepare;
4630
4991
  let tvf;
4631
4992
  try {
4632
- tvf = this.evaluateTableValuedFunction(name, args);
4993
+ tvf = yield* this.evaluateTableValuedFunction(name, args);
4633
4994
  } finally {
4634
4995
  this.preparing = preparing;
4635
4996
  }
4636
- const rows2 = tvf.rows.map((r) => {
4997
+ const rows2 = yield* stepMap(tvf.rows, function* (r) {
4998
+ yield;
4637
4999
  const obj = {};
4638
5000
  tvf.columns.forEach((c, cIdx) => {
4639
5001
  obj[c] = r[cIdx] ?? null;
@@ -4641,7 +5003,7 @@ var SqliteDatabase = class {
4641
5003
  obj[`${name}.${c}`] = r[cIdx] ?? null;
4642
5004
  });
4643
5005
  return obj;
4644
- });
5006
+ }, this);
4645
5007
  return { alias: alias2, columns: tvf.columns, rows: rows2, isCorrelated };
4646
5008
  }
4647
5009
  if (srcTokens[idx]?.value.toUpperCase() === "AS") {
@@ -4650,7 +5012,8 @@ var SqliteDatabase = class {
4650
5012
  const alias = srcTokens[idx]?.value ?? name;
4651
5013
  const cte = cteScope.get(name.toLowerCase());
4652
5014
  if (cte) {
4653
- const rows2 = cte.rows.map((r) => {
5015
+ const rows2 = yield* stepMap(cte.rows, function* (r) {
5016
+ yield;
4654
5017
  const obj = {};
4655
5018
  cte.columns.forEach((c, cIdx) => {
4656
5019
  obj[c] = r[cIdx] ?? null;
@@ -4658,7 +5021,7 @@ var SqliteDatabase = class {
4658
5021
  obj[`${name}.${c}`] = r[cIdx] ?? null;
4659
5022
  });
4660
5023
  return obj;
4661
- });
5024
+ }, this);
4662
5025
  return { alias, columns: cte.columns, rows: rows2 };
4663
5026
  }
4664
5027
  const lowerName = name.toLowerCase();
@@ -4666,7 +5029,10 @@ var SqliteDatabase = class {
4666
5029
  const cols2 = ["name", "seq"];
4667
5030
  const seqResultRows = [];
4668
5031
  for (const tbl2 of this.tables.values()) {
4669
- if ((tbl2.columns.some((c) => c.autoIncrement) || tbl2.maxAutoInc > 0) && tbl2.maxAutoInc > 0) {
5032
+ yield;
5033
+ if ((tbl2.columns.some((c) => {
5034
+ return c.autoIncrement;
5035
+ }) || tbl2.maxAutoInc > 0) && tbl2.maxAutoInc > 0) {
4670
5036
  const entry = {
4671
5037
  name: tbl2.name,
4672
5038
  seq: tbl2.maxAutoInc,
@@ -4683,6 +5049,7 @@ var SqliteDatabase = class {
4683
5049
  const masterRows = [];
4684
5050
  if (this.loadedMaster) {
4685
5051
  for (const m of this.loadedMaster) {
5052
+ yield;
4686
5053
  const entry = {
4687
5054
  type: m.type,
4688
5055
  name: m.name,
@@ -4691,6 +5058,7 @@ var SqliteDatabase = class {
4691
5058
  sql: m.sql || null
4692
5059
  };
4693
5060
  for (const c of cols2) {
5061
+ yield;
4694
5062
  entry[`${alias}.${c}`] = entry[c];
4695
5063
  }
4696
5064
  masterRows.push(entry);
@@ -4699,6 +5067,7 @@ var SqliteDatabase = class {
4699
5067
  }
4700
5068
  let rp = 2;
4701
5069
  for (const tbl2 of this.tables.values()) {
5070
+ yield;
4702
5071
  const entry = {
4703
5072
  type: "table",
4704
5073
  name: tbl2.name,
@@ -4707,11 +5076,13 @@ var SqliteDatabase = class {
4707
5076
  sql: tbl2.sql
4708
5077
  };
4709
5078
  for (const c of cols2) {
5079
+ yield;
4710
5080
  entry[`${alias}.${c}`] = entry[c];
4711
5081
  }
4712
5082
  masterRows.push(entry);
4713
5083
  }
4714
5084
  for (const idxDef of this.indexes.values()) {
5085
+ yield;
4715
5086
  const entry = {
4716
5087
  type: "index",
4717
5088
  name: idxDef.name,
@@ -4720,11 +5091,13 @@ var SqliteDatabase = class {
4720
5091
  sql: idxDef.sql
4721
5092
  };
4722
5093
  for (const c of cols2) {
5094
+ yield;
4723
5095
  entry[`${alias}.${c}`] = entry[c];
4724
5096
  }
4725
5097
  masterRows.push(entry);
4726
5098
  }
4727
5099
  for (const vDef of this.views.values()) {
5100
+ yield;
4728
5101
  const entry = {
4729
5102
  type: "view",
4730
5103
  name: vDef.name,
@@ -4733,11 +5106,13 @@ var SqliteDatabase = class {
4733
5106
  sql: vDef.sql
4734
5107
  };
4735
5108
  for (const c of cols2) {
5109
+ yield;
4736
5110
  entry[`${alias}.${c}`] = entry[c];
4737
5111
  }
4738
5112
  masterRows.push(entry);
4739
5113
  }
4740
5114
  for (const trDef of this.triggers.values()) {
5115
+ yield;
4741
5116
  const entry = {
4742
5117
  type: "trigger",
4743
5118
  name: trDef.name,
@@ -4746,6 +5121,7 @@ var SqliteDatabase = class {
4746
5121
  sql: trDef.sql
4747
5122
  };
4748
5123
  for (const c of cols2) {
5124
+ yield;
4749
5125
  entry[`${alias}.${c}`] = entry[c];
4750
5126
  }
4751
5127
  masterRows.push(entry);
@@ -4754,9 +5130,10 @@ var SqliteDatabase = class {
4754
5130
  }
4755
5131
  const view = this.findView(name);
4756
5132
  if (view) {
4757
- const res = this.executeStatement(view.selectSql, positionalParams, cteScope) ?? { columns: [], rows: [] };
5133
+ const res = (yield* this.executeStatementSteps(view.selectSql, positionalParams, cteScope)) ?? { columns: [], rows: [] };
4758
5134
  const cols2 = view.columns && view.columns.length > 0 ? view.columns : res.columns;
4759
- const rows2 = res.rows.map((r) => {
5135
+ const rows2 = yield* stepMap(res.rows, function* (r) {
5136
+ yield;
4760
5137
  const obj = {};
4761
5138
  cols2.forEach((c, cIdx) => {
4762
5139
  obj[c] = r[cIdx] ?? null;
@@ -4764,15 +5141,17 @@ var SqliteDatabase = class {
4764
5141
  obj[`${name}.${c}`] = r[cIdx] ?? null;
4765
5142
  });
4766
5143
  return obj;
4767
- });
5144
+ }, this);
4768
5145
  return { alias, columns: cols2, rows: rows2 };
4769
5146
  }
4770
5147
  const tbl = this.findTable(name);
4771
5148
  if (!tbl) {
4772
5149
  throw new Error(`no such table: ${name}`);
4773
5150
  }
4774
- const cols = tbl.columns.map((c) => c.name);
4775
- const rows = (this.preparing ? [] : tbl.rows).map((r) => {
5151
+ const cols = tbl.columns.map((c) => {
5152
+ return c.name;
5153
+ });
5154
+ const rows = yield* stepMap(this.preparing ? [] : tbl.rows, function* (r) {
4776
5155
  const obj = {
4777
5156
  rowid: r.rowid,
4778
5157
  _rowid_: r.rowid,
@@ -4781,16 +5160,17 @@ var SqliteDatabase = class {
4781
5160
  [`${name}.rowid`]: r.rowid
4782
5161
  };
4783
5162
  for (const c of cols) {
5163
+ yield;
4784
5164
  const val = r.data[c] ?? null;
4785
5165
  obj[c] = val;
4786
5166
  obj[`${alias}.${c}`] = val;
4787
5167
  obj[`${name}.${c}`] = val;
4788
5168
  }
4789
5169
  return obj;
4790
- });
5170
+ }, this);
4791
5171
  return { alias, columns: cols, rows };
4792
5172
  }
4793
- evaluateTableValuedFunction(fnName, args) {
5173
+ *evaluateTableValuedFunction(fnName, args) {
4794
5174
  const lower = fnName.toLowerCase();
4795
5175
  if (lower === "generate_series") {
4796
5176
  if (this.preparing)
@@ -4800,11 +5180,15 @@ var SqliteDatabase = class {
4800
5180
  const step = args[2] !== void 0 ? Math.trunc(toSqlNumber(args[2])) : 1;
4801
5181
  const rows = [];
4802
5182
  if (step > 0) {
4803
- for (let v = start; v <= stop && rows.length < 1e5; v += step) {
5183
+ for (let v = start; v <= stop; v += step) {
5184
+ yield;
5185
+ this.checkRowCount(rows.length + 1);
4804
5186
  rows.push([v]);
4805
5187
  }
4806
5188
  } else if (step < 0) {
4807
- for (let v = start; v >= stop && rows.length < 1e5; v += step) {
5189
+ for (let v = start; v >= stop; v += step) {
5190
+ yield;
5191
+ this.checkRowCount(rows.length + 1);
4808
5192
  rows.push([v]);
4809
5193
  }
4810
5194
  }
@@ -4852,6 +5236,7 @@ var SqliteDatabase = class {
4852
5236
  const isContainer = tStr === "array" || tStr === "object";
4853
5237
  const sqlVal = isContainer ? serializeSqlJson(val) : val === null ? null : typeof val === "boolean" ? val ? 1 : 0 : val;
4854
5238
  const atom = isContainer ? null : sqlVal;
5239
+ this.checkRowCount(rows.length + 1);
4855
5240
  rows.push([key, sqlVal, tStr, atom, myId, parentId, fullkey, pathStr]);
4856
5241
  return myId;
4857
5242
  };
@@ -4862,6 +5247,7 @@ var SqliteDatabase = class {
4862
5247
  });
4863
5248
  } else if (target && typeof target === "object") {
4864
5249
  for (const [k, item] of Object.entries(target)) {
5250
+ yield;
4865
5251
  toRow(k, item, null, `${rootPath}.${k}`, rootPath);
4866
5252
  }
4867
5253
  } else if (target !== void 0) {
@@ -4973,43 +5359,48 @@ var SqliteDatabase = class {
4973
5359
  }
4974
5360
  return false;
4975
5361
  }
4976
- collectWindowExprs(expr, groupedRows, positionalParams, cteScope, out) {
5362
+ *collectWindowExprs(expr, groupedRows, positionalParams, cteScope, out) {
4977
5363
  if (expr.kind === "func" && expr.over) {
4978
- const values = this.evaluateWindowFunc(expr, groupedRows, positionalParams, cteScope);
5364
+ const values = yield* this.evaluateWindowFunc(expr, groupedRows, positionalParams, cteScope);
4979
5365
  out.set(expr, values);
4980
5366
  return;
4981
5367
  }
4982
5368
  if (expr.kind === "unary") {
4983
- this.collectWindowExprs(expr.expr, groupedRows, positionalParams, cteScope, out);
5369
+ yield* this.collectWindowExprs(expr.expr, groupedRows, positionalParams, cteScope, out);
4984
5370
  } else if (expr.kind === "binary") {
4985
- this.collectWindowExprs(expr.left, groupedRows, positionalParams, cteScope, out);
4986
- this.collectWindowExprs(expr.right, groupedRows, positionalParams, cteScope, out);
5371
+ yield* this.collectWindowExprs(expr.left, groupedRows, positionalParams, cteScope, out);
5372
+ yield* this.collectWindowExprs(expr.right, groupedRows, positionalParams, cteScope, out);
4987
5373
  } else if (expr.kind === "func") {
4988
5374
  for (const a of expr.args) {
4989
- this.collectWindowExprs(a, groupedRows, positionalParams, cteScope, out);
5375
+ yield;
5376
+ yield* this.collectWindowExprs(a, groupedRows, positionalParams, cteScope, out);
4990
5377
  }
4991
5378
  } else if (expr.kind === "case") {
4992
5379
  if (expr.base) {
4993
- this.collectWindowExprs(expr.base, groupedRows, positionalParams, cteScope, out);
5380
+ yield* this.collectWindowExprs(expr.base, groupedRows, positionalParams, cteScope, out);
4994
5381
  }
4995
5382
  for (const b of expr.branches) {
4996
- this.collectWindowExprs(b.when, groupedRows, positionalParams, cteScope, out);
4997
- this.collectWindowExprs(b.then, groupedRows, positionalParams, cteScope, out);
5383
+ yield;
5384
+ yield* this.collectWindowExprs(b.when, groupedRows, positionalParams, cteScope, out);
5385
+ yield* this.collectWindowExprs(b.then, groupedRows, positionalParams, cteScope, out);
4998
5386
  }
4999
5387
  if (expr.elseExpr) {
5000
- this.collectWindowExprs(expr.elseExpr, groupedRows, positionalParams, cteScope, out);
5388
+ yield* this.collectWindowExprs(expr.elseExpr, groupedRows, positionalParams, cteScope, out);
5001
5389
  }
5002
5390
  } else if (expr.kind === "cast") {
5003
- this.collectWindowExprs(expr.expr, groupedRows, positionalParams, cteScope, out);
5391
+ yield* this.collectWindowExprs(expr.expr, groupedRows, positionalParams, cteScope, out);
5004
5392
  }
5005
5393
  }
5006
- evaluateWindowFunc(fnExpr, groupedRows, positionalParams, cteScope) {
5394
+ *evaluateWindowFunc(fnExpr, groupedRows, positionalParams, cteScope) {
5007
5395
  const result = new Array(groupedRows.length).fill(null);
5008
5396
  const over = fnExpr.over;
5009
5397
  const partitions = /* @__PURE__ */ new Map();
5010
5398
  for (let i = 0; i < groupedRows.length; i += 1) {
5399
+ yield;
5011
5400
  const g = groupedRows[i];
5012
- const pKey = serializeSqlJson(over.partitionBy.map((pe) => this.evalExprWithAgg(pe, g.representative, g.group, positionalParams, cteScope)), false);
5401
+ const pKey = serializeSqlJson(yield* stepMap(over.partitionBy, function* (pe) {
5402
+ return yield* this.evalExprWithAgg(pe, g.representative, g.group, positionalParams, cteScope);
5403
+ }, this), false);
5013
5404
  const arr = partitions.get(pKey);
5014
5405
  if (arr) {
5015
5406
  arr.push(i);
@@ -5019,28 +5410,33 @@ var SqliteDatabase = class {
5019
5410
  }
5020
5411
  const fnUpper = fnExpr.name.toUpperCase();
5021
5412
  for (const indices of partitions.values()) {
5413
+ yield;
5022
5414
  if (over.orderBy.length > 0) {
5023
- indices.sort((iA, iB) => {
5415
+ yield* stepSort(indices, function* (iA, iB) {
5024
5416
  const gA = groupedRows[iA];
5025
5417
  const gB = groupedRows[iB];
5026
5418
  for (const ob of over.orderBy) {
5027
- const vA = this.evalExprWithAgg(ob.expr, gA.representative, gA.group, positionalParams, cteScope);
5028
- const vB = this.evalExprWithAgg(ob.expr, gB.representative, gB.group, positionalParams, cteScope);
5419
+ yield;
5420
+ const vA = yield* this.evalExprWithAgg(ob.expr, gA.representative, gA.group, positionalParams, cteScope);
5421
+ const vB = yield* this.evalExprWithAgg(ob.expr, gB.representative, gB.group, positionalParams, cteScope);
5029
5422
  const cmp = compareSqlValues(vA, vB);
5030
5423
  if (cmp !== 0) {
5031
5424
  return ob.desc ? -cmp : cmp;
5032
5425
  }
5033
5426
  }
5034
5427
  return iA - iB;
5035
- });
5428
+ }, this);
5036
5429
  }
5037
- const orderPeerKeys = indices.map((idx) => {
5430
+ const orderPeerKeys = yield* stepMap(indices, function* (idx) {
5038
5431
  const g = groupedRows[idx];
5039
- return serializeSqlJson(over.orderBy.map((ob) => this.evalExprWithAgg(ob.expr, g.representative, g.group, positionalParams, cteScope)), false);
5040
- });
5432
+ return serializeSqlJson(yield* stepMap(over.orderBy, function* (ob) {
5433
+ return yield* this.evalExprWithAgg(ob.expr, g.representative, g.group, positionalParams, cteScope);
5434
+ }, this), false);
5435
+ }, this);
5041
5436
  let currentRank = 1;
5042
5437
  let currentDenseRank = 1;
5043
5438
  for (let pPos = 0; pPos < indices.length; pPos += 1) {
5439
+ yield;
5044
5440
  const rowIdx = indices[pPos];
5045
5441
  const g = groupedRows[rowIdx];
5046
5442
  if (pPos > 0 && orderPeerKeys[pPos] !== orderPeerKeys[pPos - 1]) {
@@ -5054,77 +5450,85 @@ var SqliteDatabase = class {
5054
5450
  } else if (fnUpper === "DENSE_RANK") {
5055
5451
  result[rowIdx] = currentDenseRank;
5056
5452
  } else if (fnUpper === "NTILE") {
5057
- const buckets = Math.max(1, Math.trunc(toSqlNumber(this.evalExprWithAgg(fnExpr.args[0], g.representative, g.group, positionalParams, cteScope))));
5453
+ const buckets = Math.max(1, Math.trunc(toSqlNumber(yield* this.evalExprWithAgg(fnExpr.args[0], g.representative, g.group, positionalParams, cteScope))));
5058
5454
  result[rowIdx] = Math.floor(pPos * buckets / indices.length) + 1;
5059
5455
  } else if (fnUpper === "PERCENT_RANK") {
5060
5456
  result[rowIdx] = indices.length <= 1 ? 0 : (currentRank - 1) / (indices.length - 1);
5061
5457
  } else if (fnUpper === "CUME_DIST") {
5062
5458
  let peerEnd = pPos;
5063
5459
  while (peerEnd + 1 < indices.length && orderPeerKeys[peerEnd + 1] === orderPeerKeys[pPos]) {
5460
+ yield;
5064
5461
  peerEnd += 1;
5065
5462
  }
5066
5463
  result[rowIdx] = (peerEnd + 1) / indices.length;
5067
5464
  } else if (fnUpper === "LAG" || fnUpper === "LEAD") {
5068
- const offset = fnExpr.args[1] ? Math.trunc(toSqlNumber(this.evalExprWithAgg(fnExpr.args[1], g.representative, g.group, positionalParams, cteScope))) : 1;
5069
- const defVal = fnExpr.args[2] ? this.evalExprWithAgg(fnExpr.args[2], g.representative, g.group, positionalParams, cteScope) : null;
5465
+ const offset = fnExpr.args[1] ? Math.trunc(toSqlNumber(yield* this.evalExprWithAgg(fnExpr.args[1], g.representative, g.group, positionalParams, cteScope))) : 1;
5466
+ const defVal = fnExpr.args[2] ? yield* this.evalExprWithAgg(fnExpr.args[2], g.representative, g.group, positionalParams, cteScope) : null;
5070
5467
  const targetPos = fnUpper === "LAG" ? pPos - offset : pPos + offset;
5071
5468
  if (targetPos < 0 || targetPos >= indices.length) {
5072
5469
  result[rowIdx] = defVal;
5073
5470
  } else {
5074
5471
  const targetG = groupedRows[indices[targetPos]];
5075
- result[rowIdx] = this.evalExprWithAgg(fnExpr.args[0], targetG.representative, targetG.group, positionalParams, cteScope);
5472
+ result[rowIdx] = yield* this.evalExprWithAgg(fnExpr.args[0], targetG.representative, targetG.group, positionalParams, cteScope);
5076
5473
  }
5077
5474
  } else if (fnUpper === "FIRST_VALUE") {
5078
5475
  const firstG = groupedRows[indices[0]];
5079
- result[rowIdx] = this.evalExprWithAgg(fnExpr.args[0], firstG.representative, firstG.group, positionalParams, cteScope);
5476
+ result[rowIdx] = yield* this.evalExprWithAgg(fnExpr.args[0], firstG.representative, firstG.group, positionalParams, cteScope);
5080
5477
  } else if (fnUpper === "LAST_VALUE") {
5081
5478
  let peerEnd = over.orderBy.length > 0 ? pPos : indices.length - 1;
5082
5479
  while (over.orderBy.length > 0 && peerEnd + 1 < indices.length && orderPeerKeys[peerEnd + 1] === orderPeerKeys[pPos]) {
5480
+ yield;
5083
5481
  peerEnd += 1;
5084
5482
  }
5085
5483
  const lastG = groupedRows[indices[peerEnd]];
5086
- result[rowIdx] = this.evalExprWithAgg(fnExpr.args[0], lastG.representative, lastG.group, positionalParams, cteScope);
5484
+ result[rowIdx] = yield* this.evalExprWithAgg(fnExpr.args[0], lastG.representative, lastG.group, positionalParams, cteScope);
5087
5485
  } else if (fnUpper === "NTH_VALUE") {
5088
- const n = Math.trunc(toSqlNumber(this.evalExprWithAgg(fnExpr.args[1], g.representative, g.group, positionalParams, cteScope)));
5486
+ const n = Math.trunc(toSqlNumber(yield* this.evalExprWithAgg(fnExpr.args[1], g.representative, g.group, positionalParams, cteScope)));
5089
5487
  if (n >= 1 && n <= indices.length) {
5090
5488
  const nthG = groupedRows[indices[n - 1]];
5091
- result[rowIdx] = this.evalExprWithAgg(fnExpr.args[0], nthG.representative, nthG.group, positionalParams, cteScope);
5489
+ result[rowIdx] = yield* this.evalExprWithAgg(fnExpr.args[0], nthG.representative, nthG.group, positionalParams, cteScope);
5092
5490
  } else {
5093
5491
  result[rowIdx] = null;
5094
5492
  }
5095
5493
  } else {
5096
5494
  let frameEnd = over.orderBy.length > 0 ? pPos : indices.length - 1;
5097
5495
  while (over.orderBy.length > 0 && frameEnd + 1 < indices.length && orderPeerKeys[frameEnd + 1] === orderPeerKeys[pPos]) {
5496
+ yield;
5098
5497
  frameEnd += 1;
5099
5498
  }
5100
- const frameRows = indices.slice(0, frameEnd + 1).flatMap((idx) => groupedRows[idx].group);
5499
+ const frameRows = yield* stepFlatMap(indices.slice(0, frameEnd + 1), function* (idx) {
5500
+ yield;
5501
+ return groupedRows[idx].group;
5502
+ }, this);
5101
5503
  const strippedFn = { ...fnExpr, over: void 0 };
5102
- result[rowIdx] = this.evalAggregateFunction(strippedFn, frameRows, positionalParams, cteScope);
5504
+ result[rowIdx] = yield* this.evalAggregateFunction(strippedFn, frameRows, positionalParams, cteScope);
5103
5505
  }
5104
5506
  }
5105
5507
  }
5106
5508
  return result;
5107
5509
  }
5108
- evalExprWithAggAndWindow(expr, rep, group, rowIdx, windowResults, positionalParams, cteScope) {
5510
+ *evalExprWithAggAndWindow(expr, rep, group, rowIdx, windowResults, positionalParams, cteScope) {
5109
5511
  if (windowResults.has(expr)) {
5110
5512
  return windowResults.get(expr)[rowIdx] ?? null;
5111
5513
  }
5112
5514
  if (expr.kind === "unary") {
5113
- const v = this.evalExprWithAggAndWindow(expr.expr, rep, group, rowIdx, windowResults, positionalParams, cteScope);
5515
+ const v = yield* this.evalExprWithAggAndWindow(expr.expr, rep, group, rowIdx, windowResults, positionalParams, cteScope);
5114
5516
  return this.applyUnary(expr.op, v);
5115
5517
  }
5116
5518
  if (expr.kind === "binary") {
5117
- const l = this.evalExprWithAggAndWindow(expr.left, rep, group, rowIdx, windowResults, positionalParams, cteScope);
5118
- const r = this.evalExprWithAggAndWindow(expr.right, rep, group, rowIdx, windowResults, positionalParams, cteScope);
5519
+ const l = yield* this.evalExprWithAggAndWindow(expr.left, rep, group, rowIdx, windowResults, positionalParams, cteScope);
5520
+ const r = yield* this.evalExprWithAggAndWindow(expr.right, rep, group, rowIdx, windowResults, positionalParams, cteScope);
5119
5521
  return this.applyBinary(expr.op, l, r);
5120
5522
  }
5121
5523
  if (expr.kind === "func" && !this.containsAggregate(expr) && !expr.over) {
5122
- const args = expr.args.map((a) => this.evalExprWithAggAndWindow(a, rep, group, rowIdx, windowResults, positionalParams, cteScope));
5123
- return this.evalScalarFunction(expr.name, args);
5524
+ const args = yield* stepMap(expr.args, function* (a) {
5525
+ return yield* this.evalExprWithAggAndWindow(a, rep, group, rowIdx, windowResults, positionalParams, cteScope);
5526
+ }, this);
5527
+ return yield* this.evalScalarFunction(expr.name, args);
5124
5528
  }
5125
- return this.evalExprWithAgg(expr, rep, group, positionalParams, cteScope);
5529
+ return yield* this.evalExprWithAgg(expr, rep, group, positionalParams, cteScope);
5126
5530
  }
5127
- evalExprWithAgg(expr, rep, group, positionalParams, cteScope) {
5531
+ *evalExprWithAgg(expr, rep, group, positionalParams, cteScope) {
5128
5532
  if (expr.kind === "func") {
5129
5533
  const u = expr.name.toUpperCase();
5130
5534
  if ([
@@ -5139,11 +5543,12 @@ var SqliteDatabase = class {
5139
5543
  "JSON_GROUP_ARRAY",
5140
5544
  "JSON_GROUP_OBJECT"
5141
5545
  ].includes(u) && !((u === "MIN" || u === "MAX") && expr.args.length > 1)) {
5142
- return this.evalAggregateFunction(expr, group, positionalParams, cteScope);
5546
+ return yield* this.evalAggregateFunction(expr, group, positionalParams, cteScope);
5143
5547
  }
5144
5548
  if (u === "COALESCE" || u === "IFNULL") {
5145
5549
  for (const a of expr.args) {
5146
- const v = this.evalExprWithAgg(a, rep, group, positionalParams, cteScope);
5550
+ yield;
5551
+ const v = yield* this.evalExprWithAgg(a, rep, group, positionalParams, cteScope);
5147
5552
  if (v !== null && v !== void 0) {
5148
5553
  return v;
5149
5554
  }
@@ -5151,28 +5556,30 @@ var SqliteDatabase = class {
5151
5556
  return null;
5152
5557
  }
5153
5558
  if (u === "IIF") {
5154
- const cond = this.evalExprWithAgg(expr.args[0], rep, group, positionalParams, cteScope);
5155
- return isTruthy(cond) ? this.evalExprWithAgg(expr.args[1], rep, group, positionalParams, cteScope) : expr.args[2] ? this.evalExprWithAgg(expr.args[2], rep, group, positionalParams, cteScope) : null;
5559
+ const cond = yield* this.evalExprWithAgg(expr.args[0], rep, group, positionalParams, cteScope);
5560
+ return isTruthy(cond) ? yield* this.evalExprWithAgg(expr.args[1], rep, group, positionalParams, cteScope) : expr.args[2] ? yield* this.evalExprWithAgg(expr.args[2], rep, group, positionalParams, cteScope) : null;
5156
5561
  }
5157
- const args = expr.args.map((a) => this.evalExprWithAgg(a, rep, group, positionalParams, cteScope));
5158
- return this.evalScalarFunction(expr.name, args);
5562
+ const args = yield* stepMap(expr.args, function* (a) {
5563
+ return yield* this.evalExprWithAgg(a, rep, group, positionalParams, cteScope);
5564
+ }, this);
5565
+ return yield* this.evalScalarFunction(expr.name, args);
5159
5566
  }
5160
5567
  if (expr.kind === "unary") {
5161
- return this.applyUnary(expr.op, this.evalExprWithAgg(expr.expr, rep, group, positionalParams, cteScope));
5568
+ return this.applyUnary(expr.op, yield* this.evalExprWithAgg(expr.expr, rep, group, positionalParams, cteScope));
5162
5569
  }
5163
5570
  if (expr.kind === "binary") {
5164
- const l = this.evalExprWithAgg(expr.left, rep, group, positionalParams, cteScope);
5165
- const r = this.evalExprWithAgg(expr.right, rep, group, positionalParams, cteScope);
5571
+ const l = yield* this.evalExprWithAgg(expr.left, rep, group, positionalParams, cteScope);
5572
+ const r = yield* this.evalExprWithAgg(expr.right, rep, group, positionalParams, cteScope);
5166
5573
  return this.applyBinary(expr.op, l, r);
5167
5574
  }
5168
5575
  if (expr.kind === "is_null") {
5169
- const v = this.evalExprWithAgg(expr.expr, rep, group, positionalParams, cteScope);
5576
+ const v = yield* this.evalExprWithAgg(expr.expr, rep, group, positionalParams, cteScope);
5170
5577
  return (expr.not ? v !== null : v === null) ? 1 : 0;
5171
5578
  }
5172
5579
  if (expr.kind === "between") {
5173
- const v = this.evalExprWithAgg(expr.expr, rep, group, positionalParams, cteScope);
5174
- const lo = this.evalExprWithAgg(expr.low, rep, group, positionalParams, cteScope);
5175
- const hi = this.evalExprWithAgg(expr.high, rep, group, positionalParams, cteScope);
5580
+ const v = yield* this.evalExprWithAgg(expr.expr, rep, group, positionalParams, cteScope);
5581
+ const lo = yield* this.evalExprWithAgg(expr.low, rep, group, positionalParams, cteScope);
5582
+ const hi = yield* this.evalExprWithAgg(expr.high, rep, group, positionalParams, cteScope);
5176
5583
  if (v === null || lo === null || hi === null) {
5177
5584
  return null;
5178
5585
  }
@@ -5180,76 +5587,93 @@ var SqliteDatabase = class {
5180
5587
  return (expr.not ? !inside : inside) ? 1 : 0;
5181
5588
  }
5182
5589
  if (expr.kind === "case") {
5183
- const baseVal = expr.base ? this.evalExprWithAgg(expr.base, rep, group, positionalParams, cteScope) : void 0;
5590
+ const baseVal = expr.base ? yield* this.evalExprWithAgg(expr.base, rep, group, positionalParams, cteScope) : void 0;
5184
5591
  for (const b of expr.branches) {
5185
- const wVal = this.evalExprWithAgg(b.when, rep, group, positionalParams, cteScope);
5592
+ yield;
5593
+ const wVal = yield* this.evalExprWithAgg(b.when, rep, group, positionalParams, cteScope);
5186
5594
  const matched = expr.base ? sqlEquals(baseVal ?? null, wVal) === true : isTruthy(wVal);
5187
5595
  if (matched) {
5188
- return this.evalExprWithAgg(b.then, rep, group, positionalParams, cteScope);
5596
+ return yield* this.evalExprWithAgg(b.then, rep, group, positionalParams, cteScope);
5189
5597
  }
5190
5598
  }
5191
- return expr.elseExpr ? this.evalExprWithAgg(expr.elseExpr, rep, group, positionalParams, cteScope) : null;
5599
+ return expr.elseExpr ? yield* this.evalExprWithAgg(expr.elseExpr, rep, group, positionalParams, cteScope) : null;
5192
5600
  }
5193
5601
  if (expr.kind === "cast") {
5194
- const v = this.evalExprWithAgg(expr.expr, rep, group, positionalParams, cteScope);
5602
+ const v = yield* this.evalExprWithAgg(expr.expr, rep, group, positionalParams, cteScope);
5195
5603
  return this.applyCast(v, expr.targetType);
5196
5604
  }
5197
- return this.evalExpr(expr, rep, positionalParams, cteScope);
5605
+ return yield* this.evalExprSteps(expr, rep, positionalParams, cteScope);
5198
5606
  }
5199
- evalAggregateFunction(expr, group, positionalParams, cteScope) {
5607
+ *evalAggregateFunction(expr, group, positionalParams, cteScope) {
5200
5608
  const u = expr.name.toUpperCase();
5201
5609
  let filteredGroup = group;
5202
5610
  if (expr.filterWhere) {
5203
- filteredGroup = group.filter((r) => isTruthy(this.evalExpr(expr.filterWhere, r, positionalParams, cteScope)));
5611
+ filteredGroup = yield* stepFilter(group, function* (r) {
5612
+ return isTruthy(yield* this.evalExprSteps(expr.filterWhere, r, positionalParams, cteScope));
5613
+ }, this);
5204
5614
  }
5205
5615
  if (u === "COUNT") {
5206
5616
  if (expr.star || expr.args.length === 0) {
5207
5617
  return filteredGroup.length;
5208
5618
  }
5209
- const vals2 = filteredGroup.map((r) => this.evalExpr(expr.args[0], r, positionalParams, cteScope)).filter((v) => v !== null && v !== void 0);
5619
+ const vals2 = yield* stepFilter(yield* stepMap(filteredGroup, function* (r) {
5620
+ return yield* this.evalExprSteps(expr.args[0], r, positionalParams, cteScope);
5621
+ }, this), function* (v) {
5622
+ yield;
5623
+ return v !== null && v !== void 0;
5624
+ }, this);
5210
5625
  if (expr.distinct) {
5211
- return new Set(vals2.map((v) => serializeSqlJson(v, false))).size;
5626
+ return new Set(vals2.map((v) => {
5627
+ return serializeSqlJson(v, false);
5628
+ })).size;
5212
5629
  }
5213
5630
  return vals2.length;
5214
5631
  }
5215
5632
  if (u === "JSON_GROUP_OBJECT") {
5216
5633
  const obj = {};
5217
5634
  for (const r of filteredGroup) {
5218
- const k = this.evalExpr(expr.args[0], r, positionalParams, cteScope);
5219
- const v = this.evalExpr(expr.args[1], r, positionalParams, cteScope);
5635
+ yield;
5636
+ const k = yield* this.evalExprSteps(expr.args[0], r, positionalParams, cteScope);
5637
+ const v = yield* this.evalExprSteps(expr.args[1], r, positionalParams, cteScope);
5220
5638
  if (k !== null && k !== void 0) {
5221
5639
  obj[toSqlString(k)] = this.sqlValToJsJson(v);
5222
5640
  }
5223
5641
  }
5224
5642
  return serializeSqlJson(obj);
5225
5643
  }
5226
- let vals = filteredGroup.map((r) => expr.args[0] ? this.evalExpr(expr.args[0], r, positionalParams, cteScope) : null);
5644
+ let vals = yield* stepMap(filteredGroup, function* (r) {
5645
+ return expr.args[0] ? yield* this.evalExprSteps(expr.args[0], r, positionalParams, cteScope) : null;
5646
+ }, this);
5227
5647
  if (u === "JSON_GROUP_ARRAY") {
5228
5648
  if (expr.distinct) {
5229
5649
  const seen = /* @__PURE__ */ new Set();
5230
- vals = vals.filter((v) => {
5650
+ vals = yield* stepFilter(vals, function* (v) {
5651
+ yield;
5231
5652
  const k = serializeSqlJson(v, false);
5232
5653
  if (seen.has(k)) {
5233
5654
  return false;
5234
5655
  }
5235
5656
  seen.add(k);
5236
5657
  return true;
5237
- });
5658
+ }, this);
5238
5659
  }
5239
- return serializeSqlJson(vals.map((v) => this.sqlValToJsJson(v)));
5660
+ return serializeSqlJson(vals.map((v) => {
5661
+ return this.sqlValToJsJson(v);
5662
+ }));
5240
5663
  }
5241
5664
  const nonNull = vals.filter((v) => v !== null && v !== void 0);
5242
5665
  let activeVals = nonNull;
5243
5666
  if (expr.distinct) {
5244
5667
  const seen = /* @__PURE__ */ new Set();
5245
- activeVals = nonNull.filter((v) => {
5668
+ activeVals = yield* stepFilter(nonNull, function* (v) {
5669
+ yield;
5246
5670
  const k = serializeSqlJson(v, false);
5247
5671
  if (seen.has(k)) {
5248
5672
  return false;
5249
5673
  }
5250
5674
  seen.add(k);
5251
5675
  return true;
5252
- });
5676
+ }, this);
5253
5677
  }
5254
5678
  if (u === "SUM") {
5255
5679
  if (activeVals.length === 0) {
@@ -5258,6 +5682,7 @@ var SqliteDatabase = class {
5258
5682
  let exact = 0n;
5259
5683
  let realSum;
5260
5684
  for (const value of activeVals) {
5685
+ yield;
5261
5686
  const numeric = arithmeticNumber(value);
5262
5687
  if (realSum !== void 0) {
5263
5688
  realSum += Number(numeric);
@@ -5272,33 +5697,47 @@ var SqliteDatabase = class {
5272
5697
  return realSum === void 0 ? integerResult(exact) : new Number(realSum);
5273
5698
  }
5274
5699
  if (u === "TOTAL") {
5275
- return new Number(activeVals.reduce((sum, v) => sum + toSqlNumber(v), 0));
5700
+ return new Number(yield* stepReduce(activeVals, function* (sum, v) {
5701
+ yield;
5702
+ return sum + toSqlNumber(v);
5703
+ }, 0, this));
5276
5704
  }
5277
5705
  if (u === "AVG") {
5278
5706
  if (activeVals.length === 0) {
5279
5707
  return null;
5280
5708
  }
5281
- const sum = activeVals.reduce((s, v) => s + toSqlNumber(v), 0);
5709
+ const sum = yield* stepReduce(activeVals, function* (s, v) {
5710
+ yield;
5711
+ return s + toSqlNumber(v);
5712
+ }, 0, this);
5282
5713
  return new Number(sum / activeVals.length);
5283
5714
  }
5284
5715
  if (u === "MIN") {
5285
5716
  if (activeVals.length === 0) {
5286
5717
  return null;
5287
5718
  }
5288
- return activeVals.reduce((min, v) => compareSqlValues(v, min) < 0 ? v : min, activeVals[0]);
5719
+ return yield* stepReduce(activeVals, function* (min, v) {
5720
+ yield;
5721
+ return compareSqlValues(v, min) < 0 ? v : min;
5722
+ }, activeVals[0], this);
5289
5723
  }
5290
5724
  if (u === "MAX") {
5291
5725
  if (activeVals.length === 0) {
5292
5726
  return null;
5293
5727
  }
5294
- return activeVals.reduce((max, v) => compareSqlValues(v, max) > 0 ? v : max, activeVals[0]);
5728
+ return yield* stepReduce(activeVals, function* (max, v) {
5729
+ yield;
5730
+ return compareSqlValues(v, max) > 0 ? v : max;
5731
+ }, activeVals[0], this);
5295
5732
  }
5296
5733
  if (u === "GROUP_CONCAT" || u === "STRING_AGG") {
5297
5734
  if (activeVals.length === 0) {
5298
5735
  return null;
5299
5736
  }
5300
- const sep = expr.args[1] ? toSqlString(this.evalExpr(expr.args[1], filteredGroup[0] ?? {}, positionalParams, cteScope)) : ",";
5301
- return activeVals.map((v) => toSqlString(v)).join(sep);
5737
+ const sep = expr.args[1] ? toSqlString(yield* this.evalExprSteps(expr.args[1], filteredGroup[0] ?? {}, positionalParams, cteScope)) : ",";
5738
+ return activeVals.map((v) => {
5739
+ return toSqlString(v);
5740
+ }).join(sep);
5302
5741
  }
5303
5742
  return null;
5304
5743
  }
@@ -5319,7 +5758,7 @@ var SqliteDatabase = class {
5319
5758
  }
5320
5759
  return v;
5321
5760
  }
5322
- validateColumns(node, scope, positionalParams, cteScope) {
5761
+ *validateColumns(node, scope, positionalParams, cteScope) {
5323
5762
  if (!node || typeof node !== "object")
5324
5763
  return;
5325
5764
  if ("kind" in node && node.kind === "column") {
@@ -5333,7 +5772,7 @@ var SqliteDatabase = class {
5333
5772
  this.preparing = true;
5334
5773
  this.outerRows.push(scope);
5335
5774
  try {
5336
- const result = this.executeStatement(subquery.kind === "subquery" ? subquery.sql : subquery.subquerySql, positionalParams, cteScope);
5775
+ const result = yield* this.executeStatementSteps(subquery.kind === "subquery" ? subquery.sql : subquery.subquerySql, positionalParams, cteScope);
5337
5776
  if (!(subquery.kind === "subquery" && subquery.exists) && result && result.columns.length !== 1) {
5338
5777
  throw new Error(`sub-select returns ${result.columns.length} columns - expected 1`);
5339
5778
  }
@@ -5342,8 +5781,10 @@ var SqliteDatabase = class {
5342
5781
  this.preparing = preparing;
5343
5782
  }
5344
5783
  }
5345
- for (const child of Object.values(node))
5346
- this.validateColumns(child, scope, positionalParams, cteScope);
5784
+ for (const child of Object.values(node)) {
5785
+ yield;
5786
+ yield* this.validateColumns(child, scope, positionalParams, cteScope);
5787
+ }
5347
5788
  }
5348
5789
  lookupColInRow(row, table, name, doubleQuoted = false) {
5349
5790
  if (table) {
@@ -5377,10 +5818,13 @@ var SqliteDatabase = class {
5377
5818
  return name;
5378
5819
  throw new Error(`in prepare, no such column: ${table ? `${table}.` : ""}${name}`);
5379
5820
  }
5380
- evalScalarSql(sql, row, positionalParams) {
5381
- return this.evalExpr(parseExprSql(sql), row, positionalParams);
5821
+ *evalScalarSql(sql, row, positionalParams) {
5822
+ return yield* this.evalExprSteps(parseExprSql(sql), row, positionalParams);
5382
5823
  }
5383
5824
  evalExpr(expr, row, positionalParams, cteScope = /* @__PURE__ */ new Map()) {
5825
+ return runSynchronously(this.evalExprSteps(expr, row, positionalParams, cteScope));
5826
+ }
5827
+ *evalExprSteps(expr, row, positionalParams, cteScope = /* @__PURE__ */ new Map()) {
5384
5828
  switch (expr.kind) {
5385
5829
  case "literal":
5386
5830
  return expr.value;
@@ -5399,16 +5843,16 @@ var SqliteDatabase = class {
5399
5843
  return this.parameters.get(expr.name) ?? this.parameters.get(expr.name.slice(1)) ?? positionalParams[expr.index - 1] ?? null;
5400
5844
  }
5401
5845
  case "collate":
5402
- return this.evalExpr(expr.expr, row, positionalParams, cteScope);
5846
+ return yield* this.evalExprSteps(expr.expr, row, positionalParams, cteScope);
5403
5847
  case "unary":
5404
- return this.applyUnary(expr.op, this.evalExpr(expr.expr, row, positionalParams, cteScope));
5848
+ return this.applyUnary(expr.op, yield* this.evalExprSteps(expr.expr, row, positionalParams, cteScope));
5405
5849
  case "binary": {
5406
5850
  if (expr.op === "AND") {
5407
- const l2 = this.evalExpr(expr.left, row, positionalParams, cteScope);
5851
+ const l2 = yield* this.evalExprSteps(expr.left, row, positionalParams, cteScope);
5408
5852
  if (l2 !== null && !isTruthy(l2)) {
5409
5853
  return 0;
5410
5854
  }
5411
- const r2 = this.evalExpr(expr.right, row, positionalParams, cteScope);
5855
+ const r2 = yield* this.evalExprSteps(expr.right, row, positionalParams, cteScope);
5412
5856
  if (r2 !== null && !isTruthy(r2)) {
5413
5857
  return 0;
5414
5858
  }
@@ -5418,11 +5862,11 @@ var SqliteDatabase = class {
5418
5862
  return 1;
5419
5863
  }
5420
5864
  if (expr.op === "OR") {
5421
- const l2 = this.evalExpr(expr.left, row, positionalParams, cteScope);
5865
+ const l2 = yield* this.evalExprSteps(expr.left, row, positionalParams, cteScope);
5422
5866
  if (l2 !== null && isTruthy(l2)) {
5423
5867
  return 1;
5424
5868
  }
5425
- const r2 = this.evalExpr(expr.right, row, positionalParams, cteScope);
5869
+ const r2 = yield* this.evalExprSteps(expr.right, row, positionalParams, cteScope);
5426
5870
  if (r2 !== null && isTruthy(r2)) {
5427
5871
  return 1;
5428
5872
  }
@@ -5432,22 +5876,22 @@ var SqliteDatabase = class {
5432
5876
  return 0;
5433
5877
  }
5434
5878
  const collation = expr.left.kind === "collate" ? expr.left.collation : expr.right.kind === "collate" ? expr.right.collation : "BINARY";
5435
- const l = this.evalExpr(expr.left, row, positionalParams, cteScope);
5879
+ const l = yield* this.evalExprSteps(expr.left, row, positionalParams, cteScope);
5436
5880
  if ((expr.op === "LIKE" || expr.op === "GLOB") && expr.right.kind === "func" && expr.right.name === "__like_escape__") {
5437
- const pat = this.evalExpr(expr.right.args[0], row, positionalParams, cteScope);
5438
- const esc = this.evalExpr(expr.right.args[1], row, positionalParams, cteScope);
5881
+ const pat = yield* this.evalExprSteps(expr.right.args[0], row, positionalParams, cteScope);
5882
+ const esc = yield* this.evalExprSteps(expr.right.args[1], row, positionalParams, cteScope);
5439
5883
  if (l === null || pat === null || esc === null) {
5440
5884
  return null;
5441
5885
  }
5442
5886
  return matchLike(toSqlString(l), toSqlString(pat), expr.op === "LIKE", toSqlString(esc)) ? 1 : 0;
5443
5887
  }
5444
- const r = this.evalExpr(expr.right, row, positionalParams, cteScope);
5888
+ const r = yield* this.evalExprSteps(expr.right, row, positionalParams, cteScope);
5445
5889
  return this.applyBinary(expr.op, l, r, collation);
5446
5890
  }
5447
5891
  case "between": {
5448
- const v = this.evalExpr(expr.expr, row, positionalParams, cteScope);
5449
- const lo = this.evalExpr(expr.low, row, positionalParams, cteScope);
5450
- const hi = this.evalExpr(expr.high, row, positionalParams, cteScope);
5892
+ const v = yield* this.evalExprSteps(expr.expr, row, positionalParams, cteScope);
5893
+ const lo = yield* this.evalExprSteps(expr.low, row, positionalParams, cteScope);
5894
+ const hi = yield* this.evalExprSteps(expr.high, row, positionalParams, cteScope);
5451
5895
  if (v === null || lo === null || hi === null) {
5452
5896
  return null;
5453
5897
  }
@@ -5455,13 +5899,14 @@ var SqliteDatabase = class {
5455
5899
  return (expr.not ? !inside : inside) ? 1 : 0;
5456
5900
  }
5457
5901
  case "in_list": {
5458
- const v = this.evalExpr(expr.expr, row, positionalParams, cteScope);
5902
+ const v = yield* this.evalExprSteps(expr.expr, row, positionalParams, cteScope);
5459
5903
  if (v === null) {
5460
5904
  return expr.list.length === 0 ? expr.not ? 1 : 0 : null;
5461
5905
  }
5462
5906
  let sawNull = false;
5463
5907
  for (const item of expr.list) {
5464
- const iv = this.evalExpr(item, row, positionalParams, cteScope);
5908
+ yield;
5909
+ const iv = yield* this.evalExprSteps(item, row, positionalParams, cteScope);
5465
5910
  if (iv === null) {
5466
5911
  sawNull = true;
5467
5912
  } else if (sqlEquals(v, iv) === true) {
@@ -5474,11 +5919,11 @@ var SqliteDatabase = class {
5474
5919
  return expr.not ? 1 : 0;
5475
5920
  }
5476
5921
  case "in_subquery": {
5477
- const v = this.evalExpr(expr.expr, row, positionalParams, cteScope);
5922
+ const v = yield* this.evalExprSteps(expr.expr, row, positionalParams, cteScope);
5478
5923
  this.outerRows.push(row);
5479
5924
  let res;
5480
5925
  try {
5481
- res = this.executeStatement(expr.subquerySql, positionalParams, cteScope) ?? { columns: [], rows: [] };
5926
+ res = (yield* this.executeStatementSteps(expr.subquerySql, positionalParams, cteScope)) ?? { columns: [], rows: [] };
5482
5927
  } finally {
5483
5928
  this.outerRows.pop();
5484
5929
  }
@@ -5489,6 +5934,7 @@ var SqliteDatabase = class {
5489
5934
  }
5490
5935
  let sawNull = false;
5491
5936
  for (const r of res.rows) {
5937
+ yield;
5492
5938
  const iv = r[0] ?? null;
5493
5939
  if (iv === null) {
5494
5940
  sawNull = true;
@@ -5502,29 +5948,33 @@ var SqliteDatabase = class {
5502
5948
  return expr.not ? 1 : 0;
5503
5949
  }
5504
5950
  case "is_null": {
5505
- const v = this.evalExpr(expr.expr, row, positionalParams, cteScope);
5951
+ const v = yield* this.evalExprSteps(expr.expr, row, positionalParams, cteScope);
5506
5952
  return (expr.not ? v !== null : v === null) ? 1 : 0;
5507
5953
  }
5508
5954
  case "case": {
5509
- const baseVal = expr.base ? this.evalExpr(expr.base, row, positionalParams, cteScope) : void 0;
5955
+ const baseVal = expr.base ? yield* this.evalExprSteps(expr.base, row, positionalParams, cteScope) : void 0;
5510
5956
  for (const b of expr.branches) {
5511
- const wVal = this.evalExpr(b.when, row, positionalParams, cteScope);
5957
+ yield;
5958
+ const wVal = yield* this.evalExprSteps(b.when, row, positionalParams, cteScope);
5512
5959
  const matched = expr.base ? sqlEquals(baseVal ?? null, wVal) === true : isTruthy(wVal);
5513
5960
  if (matched) {
5514
- return this.evalExpr(b.then, row, positionalParams, cteScope);
5961
+ return yield* this.evalExprSteps(b.then, row, positionalParams, cteScope);
5515
5962
  }
5516
5963
  }
5517
- return expr.elseExpr ? this.evalExpr(expr.elseExpr, row, positionalParams, cteScope) : null;
5964
+ return expr.elseExpr ? yield* this.evalExprSteps(expr.elseExpr, row, positionalParams, cteScope) : null;
5518
5965
  }
5519
5966
  case "cast": {
5520
- const v = this.evalExpr(expr.expr, row, positionalParams, cteScope);
5967
+ const v = yield* this.evalExprSteps(expr.expr, row, positionalParams, cteScope);
5521
5968
  return this.applyCast(v, expr.targetType);
5522
5969
  }
5523
5970
  case "subquery": {
5524
5971
  this.outerRows.push(row);
5525
5972
  let res;
5526
5973
  try {
5527
- res = this.executeStatement(expr.sql, positionalParams, cteScope) ?? { columns: [], rows: [] };
5974
+ res = (yield* this.executeStatementSteps(expr.sql, positionalParams, cteScope)) ?? {
5975
+ columns: [],
5976
+ rows: []
5977
+ };
5528
5978
  } finally {
5529
5979
  this.outerRows.pop();
5530
5980
  }
@@ -5540,7 +5990,8 @@ var SqliteDatabase = class {
5540
5990
  const u = expr.name.toUpperCase();
5541
5991
  if (u === "COALESCE" || u === "IFNULL") {
5542
5992
  for (const a of expr.args) {
5543
- const v = this.evalExpr(a, row, positionalParams, cteScope);
5993
+ yield;
5994
+ const v = yield* this.evalExprSteps(a, row, positionalParams, cteScope);
5544
5995
  if (v !== null && v !== void 0) {
5545
5996
  return v;
5546
5997
  }
@@ -5548,11 +5999,13 @@ var SqliteDatabase = class {
5548
5999
  return null;
5549
6000
  }
5550
6001
  if (u === "IIF") {
5551
- const cond = this.evalExpr(expr.args[0], row, positionalParams, cteScope);
5552
- return isTruthy(cond) ? this.evalExpr(expr.args[1], row, positionalParams, cteScope) : expr.args[2] ? this.evalExpr(expr.args[2], row, positionalParams, cteScope) : null;
6002
+ const cond = yield* this.evalExprSteps(expr.args[0], row, positionalParams, cteScope);
6003
+ return isTruthy(cond) ? yield* this.evalExprSteps(expr.args[1], row, positionalParams, cteScope) : expr.args[2] ? yield* this.evalExprSteps(expr.args[2], row, positionalParams, cteScope) : null;
5553
6004
  }
5554
- const args = expr.args.map((a) => this.evalExpr(a, row, positionalParams, cteScope));
5555
- return this.evalScalarFunction(expr.name, args);
6005
+ const args = yield* stepMap(expr.args, function* (a) {
6006
+ return yield* this.evalExprSteps(a, row, positionalParams, cteScope);
6007
+ }, this);
6008
+ return yield* this.evalScalarFunction(expr.name, args);
5556
6009
  }
5557
6010
  }
5558
6011
  }
@@ -5691,7 +6144,7 @@ var SqliteDatabase = class {
5691
6144
  const n = toSqlNumber(v);
5692
6145
  return Number.isInteger(n) ? n : n;
5693
6146
  }
5694
- evalScalarFunction(name, args) {
6147
+ *evalScalarFunction(name, args) {
5695
6148
  const u = name.toUpperCase();
5696
6149
  const a0 = args[0] ?? null;
5697
6150
  const a1 = args[1] ?? null;
@@ -5701,6 +6154,7 @@ var SqliteDatabase = class {
5701
6154
  case "COALESCE":
5702
6155
  case "IFNULL": {
5703
6156
  for (const a of args) {
6157
+ yield;
5704
6158
  if (a !== null && a !== void 0) {
5705
6159
  return a;
5706
6160
  }
@@ -5759,11 +6213,13 @@ var SqliteDatabase = class {
5759
6213
  let end = s.length;
5760
6214
  if (u === "TRIM" || u === "LTRIM") {
5761
6215
  while (start < end && set.has(s[start])) {
6216
+ yield;
5762
6217
  start += 1;
5763
6218
  }
5764
6219
  }
5765
6220
  if (u === "TRIM" || u === "RTRIM") {
5766
6221
  while (end > start && set.has(s[end - 1])) {
6222
+ yield;
5767
6223
  end -= 1;
5768
6224
  }
5769
6225
  }
@@ -5817,13 +6273,20 @@ var SqliteDatabase = class {
5817
6273
  }
5818
6274
  return sqlPrintf(toSqlString(a0), args.slice(1));
5819
6275
  case "CONCAT":
5820
- return args.map((x) => x === null || x === void 0 ? "" : toSqlString(x)).join("");
6276
+ return args.map((x) => {
6277
+ return x === null || x === void 0 ? "" : toSqlString(x);
6278
+ }).join("");
5821
6279
  case "CONCAT_WS": {
5822
6280
  if (a0 === null) {
5823
6281
  return null;
5824
6282
  }
5825
6283
  const sep = toSqlString(a0);
5826
- return args.slice(1).filter((x) => x !== null && x !== void 0).map((x) => toSqlString(x)).join(sep);
6284
+ return (yield* stepFilter(args.slice(1), function* (x) {
6285
+ yield;
6286
+ return x !== null && x !== void 0;
6287
+ }, this)).map((x) => {
6288
+ return toSqlString(x);
6289
+ }).join(sep);
5827
6290
  }
5828
6291
  case "REVERSE":
5829
6292
  return a0 === null ? null : Array.from(toSqlString(a0)).reverse().join("");
@@ -5834,7 +6297,9 @@ var SqliteDatabase = class {
5834
6297
  case "RPAD":
5835
6298
  return a0 === null || a1 === null ? null : toSqlString(a0).padEnd(Math.trunc(toSqlNumber(a1)), args[2] ? toSqlString(args[2]) : " ");
5836
6299
  case "CHAR":
5837
- return args.map((x) => String.fromCodePoint(Math.max(0, Math.trunc(toSqlNumber(x))))).join("");
6300
+ return args.map((x) => {
6301
+ return String.fromCodePoint(Math.max(0, Math.trunc(toSqlNumber(x))));
6302
+ }).join("");
5838
6303
  case "UNICODE":
5839
6304
  if (a0 === null) {
5840
6305
  return null;
@@ -5847,6 +6312,7 @@ var SqliteDatabase = class {
5847
6312
  const bytes = a0 instanceof Uint8Array ? a0 : textEncoder2.encode(toSqlString(a0));
5848
6313
  let out = "";
5849
6314
  for (const b of bytes) {
6315
+ yield;
5850
6316
  out += b.toString(16).toUpperCase().padStart(2, "0");
5851
6317
  }
5852
6318
  return out;
@@ -5861,6 +6327,7 @@ var SqliteDatabase = class {
5861
6327
  }
5862
6328
  const out = new Uint8Array(h.length / 2);
5863
6329
  for (let i = 0; i < out.length; i += 1) {
6330
+ yield;
5864
6331
  out[i] = Number.parseInt(h.slice(i * 2, i * 2 + 2), 16);
5865
6332
  }
5866
6333
  return out;
@@ -5936,27 +6403,38 @@ var SqliteDatabase = class {
5936
6403
  case "MOD":
5937
6404
  return a0 === null || a1 === null || toSqlNumber(a1) === 0 ? null : toSqlNumber(a0) % toSqlNumber(a1);
5938
6405
  case "MIN":
5939
- if (args.some((x) => x === null || x === void 0)) {
6406
+ if (args.some((x) => {
6407
+ return x === null || x === void 0;
6408
+ })) {
5940
6409
  return null;
5941
6410
  }
5942
- return args.reduce((m, x) => compareSqlValues(x, m) < 0 ? x : m, args[0] ?? null);
6411
+ return yield* stepReduce(args, function* (m, x) {
6412
+ yield;
6413
+ return compareSqlValues(x, m) < 0 ? x : m;
6414
+ }, args[0] ?? null, this);
5943
6415
  case "MAX":
5944
- if (args.some((x) => x === null || x === void 0)) {
6416
+ if (args.some((x) => {
6417
+ return x === null || x === void 0;
6418
+ })) {
5945
6419
  return null;
5946
6420
  }
5947
- return args.reduce((m, x) => compareSqlValues(x, m) > 0 ? x : m, args[0] ?? null);
6421
+ return yield* stepReduce(args, function* (m, x) {
6422
+ yield;
6423
+ return compareSqlValues(x, m) > 0 ? x : m;
6424
+ }, args[0] ?? null, this);
5948
6425
  case "RANDOM":
5949
6426
  return Math.floor((Math.random() - 0.5) * 2 * Number.MAX_SAFE_INTEGER);
5950
6427
  case "RANDOMBLOB": {
5951
- const n = Math.max(0, Math.min(65536, Math.trunc(toSqlNumber(a0 ?? 0))));
6428
+ const n = Math.max(0, Math.trunc(toSqlNumber(a0 ?? 0)));
5952
6429
  const b = new Uint8Array(n);
5953
6430
  for (let i = 0; i < n; i += 1) {
6431
+ yield;
5954
6432
  b[i] = Math.floor(Math.random() * 256);
5955
6433
  }
5956
6434
  return b;
5957
6435
  }
5958
6436
  case "ZEROBLOB": {
5959
- const n = Math.max(0, Math.min(65536, Math.trunc(toSqlNumber(a0 ?? 0))));
6437
+ const n = Math.max(0, Math.trunc(toSqlNumber(a0 ?? 0)));
5960
6438
  return new Uint8Array(n);
5961
6439
  }
5962
6440
  case "CHANGES":
@@ -6024,10 +6502,13 @@ var SqliteDatabase = class {
6024
6502
  case "JSON_QUOTE":
6025
6503
  return a0 === null ? "null" : serializeSqlJson(this.sqlValToJsJson(a0));
6026
6504
  case "JSON_ARRAY":
6027
- return serializeSqlJson(args.map((x) => this.sqlValToJsJson(x)));
6505
+ return serializeSqlJson(args.map((x) => {
6506
+ return this.sqlValToJsJson(x);
6507
+ }));
6028
6508
  case "JSON_OBJECT": {
6029
6509
  const obj = {};
6030
6510
  for (let i = 0; i + 1 < args.length; i += 2) {
6511
+ yield;
6031
6512
  if (args[i] !== null && args[i] !== void 0) {
6032
6513
  obj[toSqlString(args[i])] = this.sqlValToJsJson(args[i + 1] ?? null);
6033
6514
  }
@@ -6105,6 +6586,7 @@ var SqliteDatabase = class {
6105
6586
  let cur = JSON.parse(toSqlString(a0));
6106
6587
  const mode = u === "JSON_SET" ? "set" : u === "JSON_INSERT" ? "insert" : "replace";
6107
6588
  for (let i = 1; i + 1 < args.length; i += 2) {
6589
+ yield;
6108
6590
  const p = toSqlString(args[i] ?? "$");
6109
6591
  const v = this.sqlValToJsJson(args[i + 1] ?? null);
6110
6592
  cur = setByJsonPath(cur, p, v, mode);
@@ -6121,6 +6603,7 @@ var SqliteDatabase = class {
6121
6603
  try {
6122
6604
  let cur = JSON.parse(toSqlString(a0));
6123
6605
  for (let i = 1; i < args.length; i += 1) {
6606
+ yield;
6124
6607
  cur = removeByJsonPath(cur, toSqlString(args[i] ?? "$"));
6125
6608
  }
6126
6609
  return serializeSqlJson(cur);
@@ -6379,8 +6862,7 @@ function formatQueryResult(res, state) {
6379
6862
  for (const r of rows) {
6380
6863
  lines.push(r.map((v) => formatCsvCell(formatCellValue(v, state.nullValue), sep)).join(sep));
6381
6864
  }
6382
- return `${lines.join("\n")}
6383
- `;
6865
+ return lines.map((line) => line + state.rowSeparator).join("");
6384
6866
  }
6385
6867
  case "json": {
6386
6868
  const objects = rows.map((row) => "{" + columns.map((column, index) => {
@@ -6804,17 +7286,25 @@ Options:
6804
7286
  const raw = typeof options.engine === "function" ? await options.engine({ dbPath, readonly, context }) : options.engine;
6805
7287
  return normalizeInjectedEngine(raw);
6806
7288
  }
6807
- return new SqliteDatabase();
7289
+ return new SqliteDatabase(limits.maxRows);
6808
7290
  };
6809
7291
  const db = await createDbInstance(state.dbPath, state.readonly);
6810
7292
  const execSingleStmt = async (stmt) => {
7293
+ await yieldTurn2(context.signal);
6811
7294
  let sets = [];
6812
- if (typeof db.executeStatement === "function") {
6813
- const result = await db.executeStatement(stmt);
6814
- if (result)
6815
- sets = [result];
6816
- } else if (typeof db.exec === "function") {
6817
- sets = await db.exec(stmt);
7295
+ try {
7296
+ if (typeof db.executeStatement === "function") {
7297
+ const result = await (db.executeStatementAsync ? db.executeStatementAsync(stmt, context.signal) : db.executeStatement(stmt));
7298
+ if (result)
7299
+ sets = [result];
7300
+ } else if (typeof db.exec === "function") {
7301
+ sets = await db.exec(stmt);
7302
+ }
7303
+ } catch (error) {
7304
+ context.signal.throwIfAborted();
7305
+ if (error instanceof RangeError && error.message === "maxRows limit exceeded")
7306
+ enforceLimit("maxRows", Infinity);
7307
+ throw error;
6818
7308
  }
6819
7309
  for (const result of sets)
6820
7310
  enforceLimit("maxRows", result.rows.length);
@@ -6878,6 +7368,7 @@ Options:
6878
7368
  }
6879
7369
  }
6880
7370
  } catch (err) {
7371
+ context.signal.throwIfAborted();
6881
7372
  const msg = err instanceof Error ? err.message : String(err);
6882
7373
  throw new Error(`Error: in prepare, ${msg}`);
6883
7374
  }
@@ -6918,6 +7409,7 @@ Options:
6918
7409
  try {
6919
7410
  await loadDbFromDisk(state.dbPath);
6920
7411
  } catch (err) {
7412
+ context.signal.throwIfAborted();
6921
7413
  await writeText(context.stderr, `${err instanceof Error ? err.message : String(err)}
6922
7414
  `);
6923
7415
  return { exitCode: 1 };
@@ -6971,8 +7463,10 @@ Options:
6971
7463
  state.mode = newMode;
6972
7464
  if (newMode === "list") {
6973
7465
  state.colSeparator = "|";
7466
+ state.rowSeparator = "\n";
6974
7467
  } else if (newMode === "csv") {
6975
7468
  state.colSeparator = ",";
7469
+ state.rowSeparator = "\r\n";
6976
7470
  } else if (newMode === "tabs") {
6977
7471
  state.colSeparator = " ";
6978
7472
  } else if (newMode === "insert" && parts[2]) {
@@ -7376,6 +7870,7 @@ Options:
7376
7870
  try {
7377
7871
  await runSqlStatement(st);
7378
7872
  } catch (err) {
7873
+ context.signal.throwIfAborted();
7379
7874
  const msg = err instanceof Error ? err.message : String(err);
7380
7875
  await writeText(context.stderr, `Error: ${msg}
7381
7876
  `);
@@ -7397,6 +7892,7 @@ Options:
7397
7892
  try {
7398
7893
  await executeDotCommand(trimmed);
7399
7894
  } catch (err) {
7895
+ context.signal.throwIfAborted();
7400
7896
  const msg = err instanceof Error ? err.message : String(err);
7401
7897
  await writeText(context.stderr, `Error: ${msg}
7402
7898
  `);
@@ -7425,6 +7921,7 @@ Options:
7425
7921
  const initContent = textDecoder3.decode(await readInputFile(resolveVfsPath(context.cwd, initFile)));
7426
7922
  await processScript(initContent, false);
7427
7923
  } catch (err) {
7924
+ context.signal.throwIfAborted();
7428
7925
  await writeText(context.stderr, `Error: cannot read init file "${initFile}": ${err instanceof Error ? err.message : String(err)}
7429
7926
  `);
7430
7927
  return { exitCode: 1 };
@@ -7451,6 +7948,7 @@ Options:
7451
7948
  try {
7452
7949
  stdinBytes = await collectBytes(context.stdin, { signal: context.signal, maxBytes: limits.maxInputBytes - inputBytes });
7453
7950
  } catch (error) {
7951
+ context.signal.throwIfAborted();
7454
7952
  if (error instanceof Error && "code" in error && error.code === "EFBIG") {
7455
7953
  enforceLimit("maxInputBytes", Infinity);
7456
7954
  }
@@ -7468,6 +7966,7 @@ Options:
7468
7966
  }
7469
7967
  return { exitCode: limitExceeded ? 1 : state.exitCode };
7470
7968
  } catch (error) {
7969
+ context.signal.throwIfAborted();
7471
7970
  await writeText(context.stderr, `Error: ${error instanceof Error ? error.message : String(error)}
7472
7971
  `);
7473
7972
  return { exitCode: 1 };