neonctl 2.22.0 → 2.23.0

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 (116) hide show
  1. package/README.md +242 -16
  2. package/analytics.js +5 -2
  3. package/commands/branches.js +9 -1
  4. package/commands/checkout.js +249 -0
  5. package/commands/connection_string.js +15 -2
  6. package/commands/data_api.js +286 -0
  7. package/commands/functions.js +277 -0
  8. package/commands/index.js +12 -0
  9. package/commands/link.js +667 -0
  10. package/commands/neon_auth.js +1013 -0
  11. package/commands/projects.js +9 -1
  12. package/commands/psql.js +62 -0
  13. package/commands/set_context.js +7 -2
  14. package/context.js +86 -14
  15. package/functions_api.js +44 -0
  16. package/index.js +3 -0
  17. package/package.json +60 -51
  18. package/psql/cli.js +51 -0
  19. package/psql/command/cmd_cond.js +437 -0
  20. package/psql/command/cmd_connect.js +815 -0
  21. package/psql/command/cmd_copy.js +1025 -0
  22. package/psql/command/cmd_describe.js +1810 -0
  23. package/psql/command/cmd_format.js +909 -0
  24. package/psql/command/cmd_io.js +2187 -0
  25. package/psql/command/cmd_lo.js +385 -0
  26. package/psql/command/cmd_meta.js +970 -0
  27. package/psql/command/cmd_misc.js +187 -0
  28. package/psql/command/cmd_pipeline.js +1141 -0
  29. package/psql/command/cmd_restrict.js +171 -0
  30. package/psql/command/cmd_show.js +751 -0
  31. package/psql/command/dispatch.js +343 -0
  32. package/psql/command/inputQueue.js +42 -0
  33. package/psql/command/shared.js +71 -0
  34. package/psql/complete/filenames.js +139 -0
  35. package/psql/complete/index.js +104 -0
  36. package/psql/complete/matcher.js +314 -0
  37. package/psql/complete/psqlVars.js +247 -0
  38. package/psql/complete/queries.js +491 -0
  39. package/psql/complete/rules.js +2387 -0
  40. package/psql/core/common.js +1250 -0
  41. package/psql/core/help.js +576 -0
  42. package/psql/core/mainloop.js +1353 -0
  43. package/psql/core/prompt.js +437 -0
  44. package/psql/core/settings.js +684 -0
  45. package/psql/core/sqlHelp.js +1066 -0
  46. package/psql/core/startup.js +840 -0
  47. package/psql/core/syncVars.js +116 -0
  48. package/psql/core/variables.js +287 -0
  49. package/psql/describe/formatters.js +1277 -0
  50. package/psql/describe/processNamePattern.js +270 -0
  51. package/psql/describe/queries.js +2373 -0
  52. package/psql/describe/versionGate.js +43 -0
  53. package/psql/index.js +2005 -0
  54. package/psql/io/history.js +299 -0
  55. package/psql/io/input.js +120 -0
  56. package/psql/io/lineEditor/buffer.js +323 -0
  57. package/psql/io/lineEditor/complete.js +227 -0
  58. package/psql/io/lineEditor/filename.js +159 -0
  59. package/psql/io/lineEditor/index.js +891 -0
  60. package/psql/io/lineEditor/keymap.js +738 -0
  61. package/psql/io/lineEditor/vt100.js +363 -0
  62. package/psql/io/pgpass.js +202 -0
  63. package/psql/io/pgservice.js +194 -0
  64. package/psql/io/psqlrc.js +422 -0
  65. package/psql/print/aligned.js +1756 -0
  66. package/psql/print/asciidoc.js +248 -0
  67. package/psql/print/crosstab.js +460 -0
  68. package/psql/print/csv.js +92 -0
  69. package/psql/print/html.js +258 -0
  70. package/psql/print/json.js +96 -0
  71. package/psql/print/latex.js +396 -0
  72. package/psql/print/pager.js +265 -0
  73. package/psql/print/troff.js +258 -0
  74. package/psql/print/unaligned.js +118 -0
  75. package/psql/print/units.js +135 -0
  76. package/psql/scanner/slash.js +513 -0
  77. package/psql/scanner/sql.js +910 -0
  78. package/psql/scanner/stringutils.js +390 -0
  79. package/psql/types/backslash.js +1 -0
  80. package/psql/types/connection.js +1 -0
  81. package/psql/types/index.js +7 -0
  82. package/psql/types/printer.js +1 -0
  83. package/psql/types/repl.js +1 -0
  84. package/psql/types/scanner.js +24 -0
  85. package/psql/types/settings.js +1 -0
  86. package/psql/types/variables.js +1 -0
  87. package/psql/wire/connection.js +2844 -0
  88. package/psql/wire/copy.js +108 -0
  89. package/psql/wire/notify.js +59 -0
  90. package/psql/wire/pipeline.js +519 -0
  91. package/psql/wire/protocol.js +466 -0
  92. package/psql/wire/sasl.js +296 -0
  93. package/psql/wire/tls.js +596 -0
  94. package/test_utils/fixtures.js +1 -0
  95. package/utils/enrichers.js +18 -1
  96. package/utils/esbuild.js +147 -0
  97. package/utils/middlewares.js +1 -1
  98. package/utils/psql.js +107 -11
  99. package/utils/zip.js +4 -0
  100. package/writer.js +1 -1
  101. package/commands/auth.test.js +0 -211
  102. package/commands/branches.test.js +0 -460
  103. package/commands/connection_string.test.js +0 -196
  104. package/commands/databases.test.js +0 -39
  105. package/commands/help.test.js +0 -9
  106. package/commands/init.test.js +0 -56
  107. package/commands/ip_allow.test.js +0 -59
  108. package/commands/operations.test.js +0 -7
  109. package/commands/orgs.test.js +0 -7
  110. package/commands/projects.test.js +0 -144
  111. package/commands/roles.test.js +0 -37
  112. package/commands/set_context.test.js +0 -159
  113. package/commands/vpc_endpoints.test.js +0 -69
  114. package/env.test.js +0 -55
  115. package/utils/formats.test.js +0 -32
  116. package/writer.test.js +0 -104
@@ -0,0 +1,1250 @@
1
+ /**
2
+ * psql common — the unified send-query / process-result pipeline.
3
+ *
4
+ * TypeScript port of selected functions in `src/bin/psql/common.c`:
5
+ *
6
+ * - {@link sendQuery} ← `SendQuery`
7
+ * - {@link executeAndPrint} ← `ExecQueryAndProcessResults` (the inner
8
+ * result-processing slice, without the
9
+ * AUTOCOMMIT / savepoint scaffolding)
10
+ * - {@link psqlExec} ← `PSQLexec`
11
+ *
12
+ * The minimal version of this logic was inlined in `mainloop.ts` after WP-12;
13
+ * this WP extracts and polishes it. The pieces wired in here:
14
+ *
15
+ * - AUTOCOMMIT: when the variable is 'off' (default 'on') and the current
16
+ * transaction is idle, prepend `BEGIN` once before the next non-exempt
17
+ * statement. The set of exempt commands mirrors upstream
18
+ * `command_no_begin()` — transaction-control verbs and a handful of
19
+ * non-transactional DDL.
20
+ *
21
+ * - ON_ERROR_ROLLBACK: 'off' (default) | 'on' | 'interactive'. When active
22
+ * and we're inside a transaction, issue
23
+ * `SAVEPOINT pg_psql_temporary_savepoint` before the statement; on error
24
+ * `ROLLBACK TO`, on success `RELEASE` (unless the statement is itself a
25
+ * savepoint-management verb that already drops/replaces it).
26
+ *
27
+ * - FETCH_COUNT: integer; when >0 and the statement is a single SELECT-ish
28
+ * query, wrap it in `DECLARE _psql_cursor CURSOR FOR ...; FETCH FORWARD
29
+ * N FROM _psql_cursor` until exhausted. Non-SELECT statements fall back
30
+ * to the simple path.
31
+ *
32
+ * - SINGLESTEP: when `settings.singlestep` is true, print the SQL to stderr
33
+ * with the upstream confirmation banner and read one line from stdin.
34
+ * Input that starts with 'x' cancels the statement.
35
+ *
36
+ * - Timing: when `settings.timing` is true, measure wallclock around the
37
+ * send/print path and write a `Time: X.XXX ms` line to stdout (mirrors
38
+ * upstream's `printf` in common.c, and matches the existing mainloop
39
+ * test expectations).
40
+ *
41
+ * What's deliberately not done here:
42
+ *
43
+ * - SINGLELINE (-S): treating LF as a semicolon is a scanner concern, so it
44
+ * lives there — `scanSql` honours `ScanOptions.singleline` and the mainloop
45
+ * passes `settings.singleline` through on each pass. `sendQuery` itself
46
+ * needs no SINGLELINE branch; by the time a statement reaches here the
47
+ * scanner has already drawn the boundary.
48
+ *
49
+ * - Pipeline mode (-X / `\startpipeline`): upstream gates SendQuery on
50
+ * `pset.send_mode == PSQL_SEND_PIPELINE`; that path is owned by WP-21.
51
+ *
52
+ * - COPY FROM STDIN / TO STDOUT: upstream's ProcessResult dispatches on
53
+ * `PGRES_COPY_IN/OUT`. That belongs to WP-16; here we surface a clear
54
+ * error message if we ever see a copy result come back through
55
+ * `execSimple`.
56
+ */
57
+ import { alignedPrinter } from '../print/aligned.js';
58
+ import { asciidocPrinter } from '../print/asciidoc.js';
59
+ import { csvPrinter } from '../print/csv.js';
60
+ import { htmlPrinter } from '../print/html.js';
61
+ import { jsonPrinter } from '../print/json.js';
62
+ import { latexLongtablePrinter, latexPrinter } from '../print/latex.js';
63
+ import { troffMsPrinter } from '../print/troff.js';
64
+ import { unalignedPrinter } from '../print/unaligned.js';
65
+ import { formatDurationMs } from '../print/units.js';
66
+ import { openPager, shouldPage } from '../print/pager.js';
67
+ import { getQueryFout } from '../command/cmd_io.js';
68
+ import { formatErrorReport, psqlErrorPrefix } from '../command/cmd_meta.js';
69
+ const readTxStatus = (conn) => {
70
+ const status = conn.txStatus;
71
+ return status ?? 'I';
72
+ };
73
+ // ---------------------------------------------------------------------------
74
+ // Statement classification.
75
+ //
76
+ // Upstream uses two predicates: `command_no_begin()` decides whether a
77
+ // statement is exempt from AUTOCOMMIT's implicit BEGIN, and
78
+ // `is_select_command()` decides whether FETCH_COUNT chunking applies. We
79
+ // mirror both with a lightweight prefix matcher — the SQL was already
80
+ // normalised by the scanner before reaching us.
81
+ // ---------------------------------------------------------------------------
82
+ const SAVEPOINT_NAME = 'pg_psql_temporary_savepoint';
83
+ const CURSOR_NAME = '_psql_cursor';
84
+ /**
85
+ * Strip leading whitespace and `--` line / slash-star block comments from
86
+ * `sql`. Mirrors what upstream psql's scanner advances past before handing a
87
+ * statement to `PQexec` — the server-reported error `position` is a 1-based
88
+ * offset into THAT trimmed buffer, so the `LINE N:` re-print computed from
89
+ * `count('\n')` in `sql.slice(0, position - 1)` aligns with vanilla output
90
+ * only when the same leading prelude is stripped here too.
91
+ *
92
+ * Block comments support nested depths (PG extension). Embedded comments
93
+ * mid-statement are intentionally NOT stripped — they participate in the
94
+ * line count of the executing statement, same as upstream.
95
+ *
96
+ * Exported for cmd_io / cmd_pipeline so backslash commands that capture or
97
+ * inspect `queryBuf` see the same shape that the wire and `lastQuery`
98
+ * receive.
99
+ */
100
+ export const stripLeadingCommentsAndWS = (sql) => {
101
+ let i = 0;
102
+ const n = sql.length;
103
+ while (i < n) {
104
+ const c = sql.charCodeAt(i);
105
+ // Whitespace per psql_scan: space, tab, CR, LF, form-feed, vertical-tab.
106
+ if (c === 0x20 ||
107
+ c === 0x09 ||
108
+ c === 0x0a ||
109
+ c === 0x0d ||
110
+ c === 0x0c ||
111
+ c === 0x0b) {
112
+ i++;
113
+ continue;
114
+ }
115
+ // `--` line comment: consume up to (but not including) the next \n.
116
+ if (c === 0x2d && sql.charCodeAt(i + 1) === 0x2d) {
117
+ i += 2;
118
+ while (i < n && sql.charCodeAt(i) !== 0x0a)
119
+ i++;
120
+ continue;
121
+ }
122
+ // `/* … */` block comment with nested depth tracking.
123
+ if (c === 0x2f && sql.charCodeAt(i + 1) === 0x2a) {
124
+ i += 2;
125
+ let depth = 1;
126
+ while (i < n && depth > 0) {
127
+ if (sql.charCodeAt(i) === 0x2f && sql.charCodeAt(i + 1) === 0x2a) {
128
+ depth++;
129
+ i += 2;
130
+ }
131
+ else if (sql.charCodeAt(i) === 0x2a &&
132
+ sql.charCodeAt(i + 1) === 0x2f) {
133
+ depth--;
134
+ i += 2;
135
+ }
136
+ else {
137
+ i++;
138
+ }
139
+ }
140
+ continue;
141
+ }
142
+ break;
143
+ }
144
+ return i === 0 ? sql : sql.slice(i);
145
+ };
146
+ /** Strip leading whitespace and SQL comments, then upper-case for matching. */
147
+ const peekKeywords = (sql, count = 3) => {
148
+ // Skip leading whitespace / SQL line + block comments so we look at the
149
+ // statement's verb regardless of surrounding boilerplate.
150
+ let i = 0;
151
+ while (i < sql.length) {
152
+ const ch = sql[i];
153
+ if (ch === ' ' || ch === '\t' || ch === '\n' || ch === '\r') {
154
+ i++;
155
+ continue;
156
+ }
157
+ if (ch === '-' && sql[i + 1] === '-') {
158
+ const nl = sql.indexOf('\n', i + 2);
159
+ if (nl === -1)
160
+ return [];
161
+ i = nl + 1;
162
+ continue;
163
+ }
164
+ if (ch === '/' && sql[i + 1] === '*') {
165
+ const end = sql.indexOf('*/', i + 2);
166
+ if (end === -1)
167
+ return [];
168
+ i = end + 2;
169
+ continue;
170
+ }
171
+ break;
172
+ }
173
+ const tail = sql.slice(i);
174
+ // Tokenise on whitespace + a small set of punctuation that can immediately
175
+ // follow a keyword (semicolon, comma, open-paren).
176
+ const words = tail.split(/[\s,;()]+/u, count + 1);
177
+ return words.slice(0, count).map((w) => w.toUpperCase());
178
+ };
179
+ /** Mirror of `command_no_begin()` in psql/common.c. */
180
+ const commandNoBegin = (sql) => {
181
+ const [w0, w1, w2, w3] = peekKeywords(sql, 4);
182
+ if (!w0)
183
+ return false;
184
+ switch (w0) {
185
+ case 'ABORT':
186
+ case 'BEGIN':
187
+ case 'COMMIT':
188
+ case 'END':
189
+ case 'ROLLBACK':
190
+ case 'START':
191
+ case 'SAVEPOINT':
192
+ case 'RELEASE':
193
+ return true;
194
+ case 'PREPARE':
195
+ return w1 === 'TRANSACTION';
196
+ case 'VACUUM':
197
+ return true;
198
+ case 'CLUSTER':
199
+ // CLUSTER without an explicit argument runs over the whole DB and
200
+ // cannot be transactional.
201
+ return w1 === undefined || w1 === '';
202
+ case 'CREATE':
203
+ if (w1 === 'DATABASE' || w1 === 'TABLESPACE')
204
+ return true;
205
+ // CREATE INDEX CONCURRENTLY / CREATE UNIQUE INDEX CONCURRENTLY cannot
206
+ // run inside a transaction block — psql must send them bare even with
207
+ // AUTOCOMMIT=off (review item #24).
208
+ if (w1 === 'INDEX' && w2 === 'CONCURRENTLY')
209
+ return true;
210
+ if (w1 === 'UNIQUE' && w2 === 'INDEX' && w3 === 'CONCURRENTLY') {
211
+ return true;
212
+ }
213
+ return false;
214
+ case 'DROP':
215
+ // DROP DATABASE / TABLESPACE / INDEX CONCURRENTLY. (There is no
216
+ // `DROP TABLE … CONCURRENTLY` in PostgreSQL — removed that bogus case.)
217
+ if (w1 === 'DATABASE' || w1 === 'TABLESPACE')
218
+ return true;
219
+ if (w1 === 'INDEX' && w2 === 'CONCURRENTLY')
220
+ return true;
221
+ return false;
222
+ case 'REINDEX':
223
+ // REINDEX DATABASE / SYSTEM / INDEX CONCURRENTLY / TABLE CONCURRENTLY.
224
+ if (w1 === 'DATABASE' || w1 === 'SYSTEM')
225
+ return true;
226
+ if (w1 === 'INDEX' && w2 === 'CONCURRENTLY')
227
+ return true;
228
+ if (w1 === 'TABLE' && w2 === 'CONCURRENTLY')
229
+ return true;
230
+ return false;
231
+ case 'ALTER':
232
+ return w1 === 'SYSTEM';
233
+ case 'DISCARD':
234
+ return w1 === 'ALL';
235
+ default:
236
+ return false;
237
+ }
238
+ };
239
+ /** True when the statement opens with SELECT / VALUES / TABLE / WITH. */
240
+ const isSelectCommand = (sql) => {
241
+ const [w0] = peekKeywords(sql, 1);
242
+ return w0 === 'SELECT' || w0 === 'VALUES' || w0 === 'TABLE' || w0 === 'WITH';
243
+ };
244
+ /**
245
+ * Does this statement effectively destroy / replace the temporary savepoint?
246
+ * Upstream's `svpt_gone` flag is set when the user's command is one of
247
+ * COMMIT / ROLLBACK / SAVEPOINT / RELEASE. In those cases we must skip the
248
+ * matching RELEASE because the named savepoint no longer exists.
249
+ */
250
+ const destroysSavepoint = (sql) => {
251
+ const [w0] = peekKeywords(sql, 1);
252
+ return (w0 === 'COMMIT' ||
253
+ w0 === 'ROLLBACK' ||
254
+ w0 === 'SAVEPOINT' ||
255
+ w0 === 'RELEASE');
256
+ };
257
+ // ---------------------------------------------------------------------------
258
+ // Printer selection. Routes to the printer for the active output format —
259
+ // every format in {@link OutputFormat} that we ship is wired here; `wrapped`
260
+ // falls back to the aligned printer (which renders `wrapped` mode itself
261
+ // via `topt`).
262
+ // ---------------------------------------------------------------------------
263
+ const pickPrinter = (settings) => {
264
+ switch (settings.popt.topt.format) {
265
+ case 'aligned':
266
+ case 'wrapped':
267
+ return alignedPrinter;
268
+ case 'unaligned':
269
+ return unalignedPrinter;
270
+ case 'csv':
271
+ return csvPrinter;
272
+ case 'json':
273
+ return jsonPrinter;
274
+ case 'html':
275
+ return htmlPrinter;
276
+ case 'asciidoc':
277
+ return asciidocPrinter;
278
+ case 'latex':
279
+ return latexPrinter;
280
+ case 'latex-longtable':
281
+ return latexLongtablePrinter;
282
+ case 'troff-ms':
283
+ return troffMsPrinter;
284
+ default:
285
+ return alignedPrinter;
286
+ }
287
+ };
288
+ /**
289
+ * Pick the output target for a query result.
290
+ *
291
+ * Precedence: explicit `oneShot` argument > the settings stash from
292
+ * `\o FILE` (WP-15) > the REPL context's `stdout`.
293
+ */
294
+ export const pickOut = (ctx, oneShot) => {
295
+ if (oneShot)
296
+ return oneShot;
297
+ return getQueryFout(ctx.settings) ?? ctx.stdout;
298
+ };
299
+ // ---------------------------------------------------------------------------
300
+ // Settings accessors.
301
+ //
302
+ // AUTOCOMMIT, ON_ERROR_ROLLBACK, FETCH_COUNT all live in the psql var store.
303
+ // Upstream reads them once at the start of SendQuery; we do the same so a
304
+ // hook that mutates them mid-query doesn't reshape our logic underneath us.
305
+ // ---------------------------------------------------------------------------
306
+ const readAutocommit = (settings) => settings.vars.asBool('AUTOCOMMIT', true);
307
+ const readOnErrorRollback = (settings) => {
308
+ const raw = settings.vars.get('ON_ERROR_ROLLBACK');
309
+ if (raw === undefined)
310
+ return settings.onErrorRollback;
311
+ const v = raw.toLowerCase();
312
+ if (v === 'interactive')
313
+ return 'interactive';
314
+ if (v === 'on' || v === 'true' || v === 'yes' || v === '1')
315
+ return 'on';
316
+ return 'off';
317
+ };
318
+ const readFetchCount = (settings) => {
319
+ const v = settings.vars.asInt('FETCH_COUNT', settings.fetchCount);
320
+ if (typeof v !== 'number')
321
+ return 0;
322
+ return Math.max(0, v | 0);
323
+ };
324
+ const readSinglestep = (settings) => settings.singlestep || settings.vars.asBool('SINGLESTEP', false);
325
+ /**
326
+ * SHOW_ALL_RESULTS controls multi-statement `\;` printing. Default 'on' —
327
+ * every result set is rendered. When 'off' / '0', only the LAST result set
328
+ * is printed (upstream's `pset.show_all_results` flag, consulted by
329
+ * `PrintQueryResults` in common.c).
330
+ */
331
+ const readShowAllResults = (settings) => settings.vars.asBool('SHOW_ALL_RESULTS', true);
332
+ // ---------------------------------------------------------------------------
333
+ // Error printing — mirrors mainloop's `writeError` format. We keep it local
334
+ // so callers other than the mainloop can still emit consistent errors.
335
+ //
336
+ // `writeError` handles client-side diagnostics (e.g., "no connection to the
337
+ // server") that have no server-side ErrorResponse payload — we emit a single
338
+ // `psql: ERROR: <msg>` line.
339
+ //
340
+ // `writeQueryError` is used after a thrown query error has been captured
341
+ // into `settings.lastErrorResult` by `recordError` / `captureLastError`. It
342
+ // dispatches through {@link formatErrorReport} so the verbosity and
343
+ // SHOW_CONTEXT settings decide whether to surface the SQLSTATE / LINE /
344
+ // caret / DETAIL / HINT / CONTEXT / LOCATION layers. Matches upstream
345
+ // psql's `pg_log_error` shape in `src/bin/psql/common.c`.
346
+ // ---------------------------------------------------------------------------
347
+ const writeError = (ctx, message) => {
348
+ ctx.stderr.write(`psql: ERROR: ${message}\n`);
349
+ };
350
+ /**
351
+ * Render the verbosity-aware error report for the most recently captured
352
+ * query error and write it to `ctx.stderr`. The leading severity line gets
353
+ * the same `psql:[<file>:<n>]:` diagnostic prefix that upstream's
354
+ * `pg_log_pre_callback` prepends — matching the format the regression-
355
+ * derived conformance suite expects (e.g. `psql:<stdin>:N: ERROR: ...`).
356
+ * Subsequent layers (`LINE N: ...`, caret, `DETAIL: ...`, ...) follow on
357
+ * their own lines per {@link formatErrorReport}, unprefixed, to match
358
+ * libpq's `PQresultErrorMessage` output shape. When the captured error is
359
+ * missing (defensive — callers should always pair this with a preceding
360
+ * `recordError`) we fall back to the plain client-side {@link writeError}
361
+ * form so we never swallow the message entirely.
362
+ *
363
+ * Exported so the bind-path in mainloop can share the renderer.
364
+ */
365
+ export const writeQueryError = (ctx, fallbackMessage) => {
366
+ const e = ctx.settings.lastErrorResult;
367
+ if (!e || (!e.message && !e.code && !e.sqlstate)) {
368
+ writeError(ctx, fallbackMessage);
369
+ return;
370
+ }
371
+ const lines = formatErrorReport(e, ctx.settings.verbosity, ctx.settings.showContext);
372
+ const prefix = psqlErrorPrefix(ctx.settings);
373
+ const prefixed = [prefix + lines[0], ...lines.slice(1)];
374
+ ctx.stderr.write(prefixed.join('\n') + '\n');
375
+ };
376
+ /**
377
+ * Strip leading whitespace from a query and rebase a 1-based server position
378
+ * to match. Mirrors upstream psql/mainloop.c's behaviour: a line containing
379
+ * only a backslash command does not leave a `\n` in the query buffer, so the
380
+ * subsequent SQL statement starts at "line 1" of its own context rather than
381
+ * inheriting a blank line. Our mainloop doesn't perform that strip, so the
382
+ * captured `sqlText` sometimes has a leading `\n` (e.g. after `\set
383
+ * FETCH_COUNT 1\nSELECT error;`). Without this normalisation, `\errverbose`
384
+ * would render `LINE 2: SELECT error;` where upstream renders `LINE 1: …`.
385
+ *
386
+ * Returns the trimmed text and the rebased position. If the rebased
387
+ * position would land outside the trimmed text, it is dropped so the
388
+ * formatter skips the `LINE`/caret block instead of mis-pointing.
389
+ */
390
+ const normaliseSqlAndPosition = (sqlText, position) => {
391
+ let leading = 0;
392
+ while (leading < sqlText.length) {
393
+ const ch = sqlText.charCodeAt(leading);
394
+ // Match psql_scan's whitespace set: space, tab, CR, LF, form-feed.
395
+ if (ch !== 0x20 &&
396
+ ch !== 0x09 &&
397
+ ch !== 0x0a &&
398
+ ch !== 0x0d &&
399
+ ch !== 0x0c) {
400
+ break;
401
+ }
402
+ leading++;
403
+ }
404
+ if (leading === 0)
405
+ return { sqlText, position };
406
+ const trimmed = sqlText.slice(leading);
407
+ if (typeof position !== 'string')
408
+ return { sqlText: trimmed, position };
409
+ const original = parseInt(position, 10);
410
+ if (!Number.isFinite(original) || original <= 0) {
411
+ return { sqlText: trimmed, position };
412
+ }
413
+ const rebased = original - leading;
414
+ if (rebased <= 0 || rebased > trimmed.length) {
415
+ return { sqlText: trimmed, position: undefined };
416
+ }
417
+ return { sqlText: trimmed, position: String(rebased) };
418
+ };
419
+ /**
420
+ * Capture the full ErrorResponse-shaped payload from a thrown error.
421
+ *
422
+ * Our wire layer copies every named field of the server's ErrorResponse
423
+ * (severity / code / detail / hint / position / file / line / routine /
424
+ * …) onto the thrown Error as own properties (see `asThrowable` in
425
+ * `wire/connection.ts`). We mirror those onto `settings.lastErrorResult`
426
+ * so `\errverbose` can re-render the error in VERBOSE form — including
427
+ * the `LINE N: …` re-print + `^` pointer and the `LOCATION:` footer.
428
+ *
429
+ * `sqlText` is the originating SQL text from the caller; required so the
430
+ * `^` pointer can be positioned under the failing character. Leading
431
+ * whitespace is stripped (and `position` is rebased) so the `LINE N`
432
+ * counter reflects offsets within the user's statement rather than any
433
+ * buffer noise carried over from prior backslash commands.
434
+ */
435
+ export const captureLastError = (settings, err, sqlText) => {
436
+ const fallbackMessage = err instanceof Error ? err.message : String(err);
437
+ const e = (err ?? {});
438
+ const code = e.code;
439
+ const normalised = normaliseSqlAndPosition(sqlText, e.position);
440
+ settings.lastErrorResult = {
441
+ severity: e.severity,
442
+ code,
443
+ // Keep `sqlstate` as an alias for legacy callers / tests.
444
+ sqlstate: code,
445
+ message: e.message ?? fallbackMessage,
446
+ detail: e.detail,
447
+ hint: e.hint,
448
+ position: normalised.position,
449
+ internalPosition: e.internalPosition,
450
+ internalQuery: e.internalQuery,
451
+ where: e.where,
452
+ schema: e.schema,
453
+ table: e.table,
454
+ column: e.column,
455
+ dataType: e.dataType,
456
+ constraint: e.constraint,
457
+ file: e.file,
458
+ line: e.line,
459
+ routine: e.routine,
460
+ sqlText: normalised.sqlText,
461
+ };
462
+ return settings.lastErrorResult.message ?? fallbackMessage;
463
+ };
464
+ const recordError = (ctx, err, sqlText = '') => captureLastError(ctx.settings, err, sqlText);
465
+ /**
466
+ * Update the per-statement diagnostic psql variables that upstream's
467
+ * `SetResultVariables` / `SetErrorVariables` in `src/bin/psql/common.c`
468
+ * maintains. Called after every dispatched statement (success and error
469
+ * paths) so `\echo :LAST_ERROR_MESSAGE` and friends produce the same
470
+ * values vanilla psql does.
471
+ *
472
+ * - `SQLSTATE` SQLSTATE of the *most recent* statement —
473
+ * `"00000"` on success, the server-reported code
474
+ * on error (defaults to `"XX000"` when missing).
475
+ * - `ERROR` `"true"` if the most recent statement failed,
476
+ * else `"false"`.
477
+ * - `ROW_COUNT` affected/returned row count of the most recent
478
+ * statement (from libpq's `PQcmdTuples`). `"0"`
479
+ * on error.
480
+ * - `LAST_ERROR_*` sticky — only mutated on error. Mirrors
481
+ * upstream's "preserve until the next failure"
482
+ * contract so a successful statement does not
483
+ * clobber the prior error info.
484
+ *
485
+ * Exported so mainloop's bind / pipeline paths (which bypass {@link
486
+ * sendQuery}) can share the same updater.
487
+ */
488
+ export const refreshErrorVars = (settings, outcome) => {
489
+ const { vars } = settings;
490
+ if (outcome.kind === 'error') {
491
+ const last = settings.lastErrorResult;
492
+ const code = last?.code ?? last?.sqlstate ?? 'XX000';
493
+ const message = last?.message ?? '';
494
+ vars.set('LAST_ERROR_MESSAGE', message);
495
+ vars.set('LAST_ERROR_SQLSTATE', code);
496
+ vars.set('SQLSTATE', code);
497
+ vars.set('ERROR', 'true');
498
+ vars.set('ROW_COUNT', '0');
499
+ return;
500
+ }
501
+ vars.set('SQLSTATE', '00000');
502
+ vars.set('ERROR', 'false');
503
+ const rc = outcome.rowCount ?? 0;
504
+ vars.set('ROW_COUNT', String(rc));
505
+ };
506
+ // ---------------------------------------------------------------------------
507
+ // SINGLESTEP confirmation.
508
+ //
509
+ // Upstream prints to stdout and reads a line from /dev/tty; we use ctx.stderr
510
+ // for the banner (so the SQL preview stays out of any redirected query
511
+ // output) and read one line from ctx.stdin. Returns true to proceed.
512
+ // ---------------------------------------------------------------------------
513
+ const readOneLine = (stdin) => new Promise((resolve) => {
514
+ let buf = '';
515
+ let resolved = false;
516
+ const onData = (chunk) => {
517
+ buf += chunk.toString();
518
+ const nl = buf.indexOf('\n');
519
+ if (nl !== -1) {
520
+ const line = buf.slice(0, nl);
521
+ cleanup();
522
+ if (!resolved) {
523
+ resolved = true;
524
+ resolve(line);
525
+ }
526
+ }
527
+ };
528
+ const onEnd = () => {
529
+ cleanup();
530
+ if (!resolved) {
531
+ resolved = true;
532
+ resolve(buf);
533
+ }
534
+ };
535
+ const cleanup = () => {
536
+ stdin.off('data', onData);
537
+ stdin.off('end', onEnd);
538
+ stdin.off('close', onEnd);
539
+ };
540
+ stdin.on('data', onData);
541
+ stdin.once('end', onEnd);
542
+ stdin.once('close', onEnd);
543
+ });
544
+ const confirmSinglestep = async (ctx, sql) => {
545
+ ctx.stderr.write(`***(Single step mode: verify command)*******************************************\n` +
546
+ `${sql}\n` +
547
+ `***(press return to proceed or enter x and return to cancel)********************\n`);
548
+ const line = await readOneLine(ctx.stdin);
549
+ return !line.trim().toLowerCase().startsWith('x');
550
+ };
551
+ // ---------------------------------------------------------------------------
552
+ // Result rendering. We tally rows printed / rows affected for the QueryStats
553
+ // return; both are best-effort against the libpq-shaped ResultSet (rowCount
554
+ // is null for DDL, rows.length is 0 for COPY etc.).
555
+ // ---------------------------------------------------------------------------
556
+ /**
557
+ * Reconstruct the libpq-style CommandComplete tag from the parsed parts our
558
+ * wire layer stores on a ResultSet. INSERT carries `oid` + `rowCount`; the
559
+ * other DML verbs (UPDATE/DELETE/MERGE/SELECT/MOVE/FETCH/COPY) carry just a
560
+ * `rowCount`; DDL has neither.
561
+ *
562
+ * Matches upstream psql's `PQcmdStatus(conn)` output (which is the raw tag
563
+ * the server sent — we round-trip it through our parser).
564
+ */
565
+ const formatCommandTag = (rs) => {
566
+ const command = (rs.command || '').trim();
567
+ if (command.length === 0)
568
+ return '';
569
+ if (command === 'INSERT') {
570
+ // INSERT is the only tag with the legacy oid in front of rowCount.
571
+ return `INSERT ${rs.oid ?? 0} ${rs.rowCount ?? 0}`;
572
+ }
573
+ if (rs.rowCount !== null && rs.rowCount !== undefined) {
574
+ return `${command} ${rs.rowCount}`;
575
+ }
576
+ return command;
577
+ };
578
+ /**
579
+ * Decide whether the pager should activate for the given batch of result
580
+ * sets. We page when ANY of the sets-that-will-be-printed (i.e. tuples-
581
+ * producing, and not gated out by SHOW_ALL_RESULTS) crosses the threshold.
582
+ * The decision is centralised here so callers can override individual
583
+ * decision inputs (e.g. tests that inject a fake `output`).
584
+ */
585
+ const pickPagerDecision = (ctx, results, out) => {
586
+ const popt = ctx.settings.popt.topt;
587
+ // Pager off → never page (cheap exit, no looping needed).
588
+ if (popt.pager === 'off')
589
+ return false;
590
+ // `\o FILE` (or `\g FILE`) wins over pager. If the queryFout is set, the
591
+ // pager must not activate even when popt.pager === 'always'.
592
+ const redirectedOutput = getQueryFout(ctx.settings) !== null;
593
+ if (redirectedOutput)
594
+ return false;
595
+ const showAll = readShowAllResults(ctx.settings);
596
+ const lastIdx = results.length - 1;
597
+ for (let i = 0; i < results.length; i++) {
598
+ const rs = results[i];
599
+ if (rs.fields.length === 0)
600
+ continue;
601
+ if (!(showAll || i === lastIdx))
602
+ continue;
603
+ const decision = shouldPage({
604
+ pager: popt.pager,
605
+ pagerMinLines: popt.pagerMinLines,
606
+ rowCount: rs.rows.length,
607
+ colCount: rs.fields.length,
608
+ output: out,
609
+ redirectedOutput,
610
+ });
611
+ if (decision)
612
+ return true;
613
+ }
614
+ return false;
615
+ };
616
+ const renderResultSets = async (ctx, results, out) => {
617
+ const printer = pickPrinter(ctx.settings);
618
+ let rowsAffected = 0;
619
+ let rowsPrinted = 0;
620
+ // When SHOW_ALL_RESULTS is off and we have a `\;`-separated batch, upstream
621
+ // only prints the LAST result set. The tally counters still walk every
622
+ // result so QueryStats stays consistent — only the printer call is gated.
623
+ const showAll = readShowAllResults(ctx.settings);
624
+ const lastIdx = results.length - 1;
625
+ const tuplesOnly = ctx.settings.popt.topt.tuplesOnly;
626
+ // Pager wrapping. If the active topt.pager + heuristics call for it, route
627
+ // the printer through a spawned pager (PAGER / PSQL_PAGER, default `less`
628
+ // on POSIX). The pager is opened ONCE per renderResultSets call so a `\;`
629
+ // batch ends up in a single pager session, matching upstream. SIGPIPE /
630
+ // EPIPE handling lives inside the pager module.
631
+ const wantPager = pickPagerDecision(ctx, results, out);
632
+ const pager = wantPager
633
+ ? openPager({
634
+ pager: ctx.settings.popt.topt.pager,
635
+ pagerMinLines: ctx.settings.popt.topt.pagerMinLines,
636
+ stdout: out,
637
+ // shouldPage already verified pager-on conditions; force-spawn at
638
+ // the openPager level by re-passing the topt setting.
639
+ })
640
+ : null;
641
+ const sink = pager?.spawned ? pager.out : out;
642
+ try {
643
+ for (let i = 0; i < results.length; i++) {
644
+ const rs = results[i];
645
+ const shouldEmit = showAll || i === lastIdx;
646
+ if (rs.copyOutBytes && rs.copyOutBytes.length > 0) {
647
+ // `COPY ... TO STDOUT` segment of a `\;`-chained batch — emit the
648
+ // accumulated CopyData payloads at the result's position in the
649
+ // chain (upstream `handleCopyOut` writes the bytes to
650
+ // `pset.queryFout`, which under a normal dispatch is the active
651
+ // stdout). Render unconditionally regardless of SHOW_ALL_RESULTS:
652
+ // upstream gates `\;`-chain row tables on `show_all_results`, but
653
+ // the COPY data flows directly to the output stream and is not
654
+ // affected by the flag. Matches the regress baseline ordering for
655
+ // `... \; COPY x TO STDOUT \; ...`.
656
+ for (const chunk of rs.copyOutBytes) {
657
+ sink.write(chunk);
658
+ }
659
+ }
660
+ if (rs.fields.length === 0) {
661
+ // Non-tuples-producing commands (INSERT/UPDATE/DELETE/DDL) — emit the
662
+ // CommandComplete tag instead of running the table printer (which
663
+ // would render an empty `(0 rows)` block). Suppressed in tuples-only
664
+ // mode (`\t`) and in `--quiet` mode to match upstream
665
+ // (PSQLexec calls SetResultVariables which only prints the tag
666
+ // when !pset.quiet). Also suppressed when the result represents a
667
+ // COPY-out segment whose bytes we already streamed above —
668
+ // upstream's `handleCopyOut` doesn't emit the `COPY N` tag on the
669
+ // queryFout stream; the tag goes to the status stream which we
670
+ // route through the diagnostic vars rather than stdout.
671
+ if (shouldEmit &&
672
+ !tuplesOnly &&
673
+ !ctx.settings.quiet &&
674
+ !rs.copyOutBytes) {
675
+ const tag = formatCommandTag(rs);
676
+ if (tag.length > 0)
677
+ sink.write(`${tag}\n`);
678
+ }
679
+ // rowCount is the affected-row total when libpq sets it.
680
+ rowsAffected += rs.rowCount ?? 0;
681
+ }
682
+ else {
683
+ if (shouldEmit) {
684
+ await printer.printQuery(rs, ctx.settings.popt, sink);
685
+ }
686
+ rowsPrinted += rs.rows.length;
687
+ }
688
+ }
689
+ }
690
+ finally {
691
+ if (pager?.spawned) {
692
+ // End the pager stdin and wait for it to exit. We swallow errors here:
693
+ // the user may have closed the pager early (SIGPIPE → EPIPE) and our
694
+ // callers should not see that as a query failure.
695
+ try {
696
+ await pager.close();
697
+ }
698
+ catch {
699
+ // ignore
700
+ }
701
+ }
702
+ }
703
+ // libpq's `PQcmdTuples(lastResult)` semantic: ROW_COUNT mirrors the LAST
704
+ // result set's affected-row count (or returned-row count for tuples-
705
+ // producing commands). For SELECT-shaped results the wire layer doesn't
706
+ // populate rs.rowCount until CommandComplete arrives, but the array shape
707
+ // (`rs.rows.length`) is the authoritative count.
708
+ const lastRowCount = results.length === 0
709
+ ? null
710
+ : (() => {
711
+ const rs = results[results.length - 1];
712
+ if (rs.fields.length > 0)
713
+ return rs.rows.length;
714
+ return rs.rowCount ?? null;
715
+ })();
716
+ return { rowsAffected, rowsPrinted, lastRowCount };
717
+ };
718
+ /**
719
+ * Render a single {@link ResultSet} through the active printer and the
720
+ * configured output target (respecting `\o FILE` redirects). Used by the
721
+ * `\bind` / extended-query path in {@link mainloop.dispatchSendQuery} which
722
+ * comes back with a single result instead of the array shape `execSimple`
723
+ * produces. Returns a tally consistent with {@link renderResultSets}.
724
+ */
725
+ export const renderResultSet = (ctx, rs, out) => {
726
+ return renderResultSets(ctx, [rs], out ?? pickOut(ctx));
727
+ };
728
+ // ---------------------------------------------------------------------------
729
+ // FETCH_COUNT cursor loop.
730
+ //
731
+ // Wrap `<sql>` in DECLARE/FETCH and stream chunks. We open the cursor inside
732
+ // a transaction (upstream relies on the surrounding implicit BEGIN); when
733
+ // AUTOCOMMIT is on and we're idle, we open one here and close it with a
734
+ // COMMIT on the happy path / ROLLBACK on error.
735
+ // ---------------------------------------------------------------------------
736
+ /**
737
+ * Re-base a server-side `position` field so it points into the user's
738
+ * original SQL rather than the synthetic statement we actually sent.
739
+ *
740
+ * The FETCH_COUNT path sends `DECLARE _psql_cursor NO SCROLL CURSOR FOR
741
+ * <userSql>` for the DECLARE leg and `FETCH FORWARD N FROM _psql_cursor`
742
+ * for each fetch. Server error positions (`P` field) come back in the
743
+ * coordinates of whatever query we sent:
744
+ *
745
+ * - DECLARE-time parser/planner errors carry a position into the DECLARE
746
+ * statement. We subtract the length of the prefix (`DECLARE … FOR `)
747
+ * so the caret lands under the failing token in `userSql`.
748
+ *
749
+ * - FETCH-time runtime errors come from executing the cursor's underlying
750
+ * query (which IS `userSql`). The server reports the position relative
751
+ * to that underlying query, so it's already in `userSql` coordinates
752
+ * and we leave it alone.
753
+ *
754
+ * If we can't rebase a DECLARE-coord position into `userSql` bounds, we
755
+ * strip it rather than render a caret pointing past end-of-line.
756
+ */
757
+ const rebasePositionForCursor = (err, wrapper, userSql) => {
758
+ if (!err || typeof err !== 'object')
759
+ return;
760
+ const e = err;
761
+ if (typeof e.position !== 'string')
762
+ return;
763
+ const original = parseInt(e.position, 10);
764
+ if (!Number.isFinite(original) || original <= 0)
765
+ return;
766
+ // Find the user's SQL inside the wrapper. If the wrapper *contains* the
767
+ // user's SQL verbatim (the DECLARE case), the prefix length tells us how
768
+ // far to shift. The trailing `;` is stripped before wrapping, so we
769
+ // search for the stripped form.
770
+ const stripped = userSql.replace(/;\s*$/u, '');
771
+ const offset = wrapper.indexOf(stripped);
772
+ if (offset === -1) {
773
+ // FETCH-leg failures: the wrapper is `FETCH FORWARD …` and the server
774
+ // reports the position relative to the cursor's underlying query
775
+ // (i.e. `userSql`), not the FETCH text. Leave the position alone —
776
+ // assuming it's already in user-sql coordinates is the right call,
777
+ // and if it isn't, the LINE/caret renderer clamps gracefully.
778
+ return;
779
+ }
780
+ const rebased = original - offset;
781
+ if (rebased <= 0 || rebased > userSql.length) {
782
+ // Position points outside the user's SQL — likely the parser blamed
783
+ // something inside the wrapper. Drop the field so the formatter skips
784
+ // the `LINE`/caret block instead of mis-pointing.
785
+ delete e.position;
786
+ return;
787
+ }
788
+ e.position = String(rebased);
789
+ };
790
+ const runCursorLoop = async (ctx, sql, fetchCount, out) => {
791
+ if (!ctx.settings.db)
792
+ throw new Error('no connection to the server');
793
+ const db = ctx.settings.db;
794
+ // Make sure we're in a transaction so the cursor survives between FETCH
795
+ // calls. If we're idle, open one here and remember to close it.
796
+ const initiallyIdle = readTxStatus(db) === 'I';
797
+ if (initiallyIdle) {
798
+ await db.execSimple('BEGIN');
799
+ }
800
+ // Strip trailing ';' from the user SQL so DECLARE CURSOR FOR <stmt> parses.
801
+ const stripped = sql.replace(/;\s*$/u, '');
802
+ const declared = `DECLARE ${CURSOR_NAME} NO SCROLL CURSOR FOR ${stripped}`;
803
+ const fetchSql = `FETCH FORWARD ${String(fetchCount)} FROM ${CURSOR_NAME}`;
804
+ const rowsAffected = 0;
805
+ let rowsPrinted = 0;
806
+ let cursorOpen = false;
807
+ // Track which synthetic statement is currently running so the catch block
808
+ // can rebase the server-side `position` into the user's SQL coordinates
809
+ // before throwing. Without this, `\errverbose` renders `LINE 1: <user-sql>`
810
+ // with the caret pointing past end-of-line.
811
+ let currentWrapper = declared;
812
+ const printer = pickPrinter(ctx.settings);
813
+ // Upstream's print_cursor.c walks the cursor in chunks and toggles libpq's
814
+ // `flag.start_table` / `flag.stop_table` so the table renders as one
815
+ // continuous block — header on the first chunk, footer on the last. Our
816
+ // `aligned` printer doesn't (yet) honour those toggles, so we merge every
817
+ // chunk into a single synthetic ResultSet and hand it to the printer once.
818
+ // The user-facing output is identical to the non-chunked path, which is
819
+ // what the regress baseline expects (one `(19 rows)` footer instead of
820
+ // `(10 rows)` + `(9 rows)`).
821
+ let merged = null;
822
+ try {
823
+ await db.execSimple(declared);
824
+ cursorOpen = true;
825
+ while (true) {
826
+ currentWrapper = fetchSql;
827
+ const sets = await db.execSimple(fetchSql);
828
+ if (sets.length === 0)
829
+ break;
830
+ const rs = sets[sets.length - 1];
831
+ const chunkRows = rs.rows.length;
832
+ if (chunkRows === 0)
833
+ break;
834
+ if (merged === null) {
835
+ merged = {
836
+ command: rs.command,
837
+ fields: rs.fields,
838
+ rows: rs.rows.slice(),
839
+ rowCount: rs.rowCount,
840
+ oid: rs.oid,
841
+ notices: rs.notices,
842
+ };
843
+ }
844
+ else {
845
+ for (const row of rs.rows)
846
+ merged.rows.push(row);
847
+ }
848
+ rowsPrinted += chunkRows;
849
+ if (chunkRows < fetchCount)
850
+ break;
851
+ }
852
+ if (merged !== null) {
853
+ // Patch the merged rowCount to reflect the actual aggregated row
854
+ // total so command-tag / `(N rows)` footers match the upstream
855
+ // single-statement output.
856
+ merged.rowCount = merged.rows.length;
857
+ await printer.printQuery(merged, ctx.settings.popt, out);
858
+ }
859
+ await db.execSimple(`CLOSE ${CURSOR_NAME}`);
860
+ cursorOpen = false;
861
+ if (initiallyIdle) {
862
+ await db.execSimple('COMMIT');
863
+ }
864
+ return { rowsAffected, rowsPrinted, lastRowCount: rowsPrinted };
865
+ }
866
+ catch (err) {
867
+ // Flush whatever chunks we successfully fetched before the error so the
868
+ // partial output lands ahead of the ERROR line. Mirrors upstream
869
+ // print_cursor.c: each chunk renders incrementally — when a later FETCH
870
+ // raises (e.g. division by zero on row 16 of a 10-row chunked stream),
871
+ // the first chunk's rows have already been printed. We accumulate into
872
+ // a single merged ResultSet here, so the partial flush is "print the
873
+ // merged buffer once, without the `(N rows)` footer the happy-path
874
+ // emits when the cursor completes cleanly". The footer is suppressed
875
+ // because the table is conceptually incomplete (upstream renders no
876
+ // `(N rows)` for the truncated chunk either).
877
+ if (merged !== null) {
878
+ merged.rowCount = merged.rows.length;
879
+ const partialOpts = {
880
+ ...ctx.settings.popt,
881
+ // `stopTable: false` mirrors upstream `print_cursor.c`'s
882
+ // mid-error flush: no `(N rows)` auto-footer, no trailing
883
+ // blank — the ERROR line should land flush against the last
884
+ // data row, not separated by an extra empty line.
885
+ topt: {
886
+ ...ctx.settings.popt.topt,
887
+ defaultFooter: false,
888
+ stopTable: false,
889
+ },
890
+ };
891
+ try {
892
+ await printer.printQuery(merged, partialOpts, out);
893
+ }
894
+ catch {
895
+ // ignore — surface the original error
896
+ }
897
+ }
898
+ // Rebase the server-reported `position` from the synthetic wrapper's
899
+ // coordinates into the user's SQL coordinates in place. Server error
900
+ // positions come back relative to whatever statement we sent (DECLARE
901
+ // `… FOR <user-sql>` or FETCH FORWARD `…`). Without this rewrite, the
902
+ // caller's `recordError(ctx, err, sql)` would stash a position that
903
+ // points past the end of `sql`, and `\errverbose` would render
904
+ // `LINE 1: <user-sql>` with the `^` caret in the wrong column.
905
+ rebasePositionForCursor(err, currentWrapper, sql);
906
+ if (cursorOpen) {
907
+ try {
908
+ await db.execSimple(`CLOSE ${CURSOR_NAME}`);
909
+ }
910
+ catch {
911
+ // ignore — surface the original error
912
+ }
913
+ }
914
+ if (initiallyIdle) {
915
+ try {
916
+ await db.execSimple('ROLLBACK');
917
+ }
918
+ catch {
919
+ // ignore
920
+ }
921
+ }
922
+ throw err;
923
+ }
924
+ };
925
+ // ---------------------------------------------------------------------------
926
+ // `executeAndPrint` — the inner pipeline: execSimple → render → tally.
927
+ // Used directly by `\watch` and `\gexec` (which manage their own transaction
928
+ // scaffolding). Caller is responsible for AUTOCOMMIT / savepoint state.
929
+ // ---------------------------------------------------------------------------
930
+ export const executeAndPrint = async (ctx, sqlRaw, opts = {}) => {
931
+ // Strip leading whitespace + `--` line / slash-star block comments before
932
+ // the wire send so server-reported `position` (1-based offset) and
933
+ // `LINE N:` re-prints align with upstream — vanilla psql's scanner
934
+ // advances past the same prelude before handing the buffer to `PQexec`.
935
+ const sql = stripLeadingCommentsAndWS(sqlRaw);
936
+ const started = ctx.settings.timing ? performance.now() : 0;
937
+ const stats = {
938
+ rowsAffected: 0,
939
+ rowsPrinted: 0,
940
+ fetched: false,
941
+ hadError: false,
942
+ durationMs: 0,
943
+ };
944
+ if (!ctx.settings.db) {
945
+ writeError(ctx, 'no connection to the server');
946
+ stats.hadError = true;
947
+ return stats;
948
+ }
949
+ const out = pickOut(ctx, opts.oneShotOut);
950
+ const fetchCount = readFetchCount(ctx.settings);
951
+ let lastRowCount = null;
952
+ try {
953
+ if (fetchCount > 0 && isSelectCommand(sql)) {
954
+ const r = await runCursorLoop(ctx, sql, fetchCount, out);
955
+ stats.rowsAffected = r.rowsAffected;
956
+ stats.rowsPrinted = r.rowsPrinted;
957
+ stats.fetched = true;
958
+ lastRowCount = r.lastRowCount;
959
+ }
960
+ else {
961
+ const results = await ctx.settings.db.execSimple(sql);
962
+ const r = await renderResultSets(ctx, results, out);
963
+ stats.rowsAffected = r.rowsAffected;
964
+ stats.rowsPrinted = r.rowsPrinted;
965
+ lastRowCount = r.lastRowCount;
966
+ }
967
+ }
968
+ catch (err) {
969
+ // `\;`-chained batches surface every result the server produced before
970
+ // the ErrorResponse on the thrown Error's `partialResults` field (set
971
+ // by the wire layer's ReadyForQuery handler). Render them in order
972
+ // before printing the error itself so the user sees the same shape
973
+ // upstream `PQgetResult` walks produce.
974
+ const partial = err
975
+ .partialResults;
976
+ if (partial && partial.length > 0) {
977
+ try {
978
+ const r = await renderResultSets(ctx, partial, out);
979
+ stats.rowsAffected = r.rowsAffected;
980
+ stats.rowsPrinted = r.rowsPrinted;
981
+ }
982
+ catch {
983
+ // Surface the original error; don't shadow it with a render failure.
984
+ }
985
+ }
986
+ const message = recordError(ctx, err, sql);
987
+ writeQueryError(ctx, message);
988
+ stats.hadError = true;
989
+ }
990
+ finally {
991
+ if (ctx.settings.timing) {
992
+ stats.durationMs = performance.now() - started;
993
+ ctx.stdout.write('\n' + formatDurationMs(stats.durationMs) + '\n');
994
+ }
995
+ }
996
+ // Mirror upstream's `SetResultVariables` / `SetErrorVariables` call at the
997
+ // tail of `SendQuery`: refresh the per-statement diagnostic psql vars so
998
+ // `\echo :SQLSTATE` and friends see the most recent outcome. ROW_COUNT
999
+ // tracks libpq's `PQcmdTuples` on the LAST result of a `\;` batch.
1000
+ refreshErrorVars(ctx.settings, stats.hadError
1001
+ ? { kind: 'error' }
1002
+ : { kind: 'success', rowCount: lastRowCount });
1003
+ return stats;
1004
+ };
1005
+ // ---------------------------------------------------------------------------
1006
+ // `sendQuery` — the full pipeline: SINGLESTEP confirmation + AUTOCOMMIT
1007
+ // implicit BEGIN + ON_ERROR_ROLLBACK savepoint + execute + savepoint
1008
+ // resolution. Mirrors `SendQuery` in common.c.
1009
+ // ---------------------------------------------------------------------------
1010
+ export const sendQuery = async (ctx, sqlRaw, opts = {}) => {
1011
+ // Strip leading whitespace + `--` line / slash-star block comments before
1012
+ // the wire send AND before storing into `pset.last_query`. Vanilla psql's
1013
+ // scanner advances past the same prelude before handing the buffer to
1014
+ // `PQexec`, so server-reported `position` (1-based) and `LINE N:`
1015
+ // re-prints align with vanilla only after we trim here. `\p` (which falls
1016
+ // back to `lastQuery`) also prints the stripped form so the regress
1017
+ // baseline's `\p` after `-- comment\nSELECT 1;` emits just `SELECT 1;`.
1018
+ const sql = stripLeadingCommentsAndWS(sqlRaw);
1019
+ const stats = {
1020
+ rowsAffected: 0,
1021
+ rowsPrinted: 0,
1022
+ fetched: false,
1023
+ hadError: false,
1024
+ durationMs: 0,
1025
+ };
1026
+ // Track the most recent SQL we're about to ship so `\g` / `\gx` with an
1027
+ // empty buffer can re-run it (upstream `pset.last_query`). Capture even
1028
+ // if the dispatch fails — upstream populates `last_query` before
1029
+ // `PSQLexec` and leaves it set on error.
1030
+ ctx.settings.lastQuery = sql;
1031
+ if (!ctx.settings.db) {
1032
+ writeError(ctx, 'no connection to the server');
1033
+ stats.hadError = true;
1034
+ return stats;
1035
+ }
1036
+ // SINGLESTEP: prompt before executing. 'x' aborts; anything else proceeds.
1037
+ if (readSinglestep(ctx.settings)) {
1038
+ const proceed = await confirmSinglestep(ctx, sql);
1039
+ if (!proceed) {
1040
+ // Upstream marks the statement as failed when the user cancels. We
1041
+ // mirror that so ON_ERROR_STOP halts a script.
1042
+ stats.hadError = true;
1043
+ ctx.settings.lastErrorResult = { message: 'command cancelled by user' };
1044
+ return stats;
1045
+ }
1046
+ }
1047
+ // SINGLELINE (-S): treating a newline as a semicolon is a scanner concern
1048
+ // and is wired through `scanSql`'s `ScanOptions.singleline` (the mainloop
1049
+ // forwards `ctx.settings.singleline` on each pass). No work is required in
1050
+ // `sendQuery`: the statement boundary has already been drawn before we get
1051
+ // here.
1052
+ const db = ctx.settings.db;
1053
+ const autocommit = readAutocommit(ctx.settings);
1054
+ const onErrorRollback = readOnErrorRollback(ctx.settings);
1055
+ const interactive = !ctx.settings.notty;
1056
+ const started = ctx.settings.timing ? performance.now() : 0;
1057
+ // ----- AUTOCOMMIT: implicit BEGIN ----------------------------------------
1058
+ let implicitBeginIssued = false;
1059
+ if (!autocommit && readTxStatus(db) === 'I' && !commandNoBegin(sql)) {
1060
+ try {
1061
+ await db.execSimple('BEGIN');
1062
+ implicitBeginIssued = true;
1063
+ }
1064
+ catch (err) {
1065
+ const message = recordError(ctx, err);
1066
+ writeQueryError(ctx, message);
1067
+ stats.hadError = true;
1068
+ if (ctx.settings.timing) {
1069
+ stats.durationMs = performance.now() - started;
1070
+ ctx.stdout.write('\n' + formatDurationMs(stats.durationMs) + '\n');
1071
+ }
1072
+ return stats;
1073
+ }
1074
+ }
1075
+ // ----- ON_ERROR_ROLLBACK: SAVEPOINT --------------------------------------
1076
+ const savepointActive = onErrorRollback !== 'off' &&
1077
+ (onErrorRollback === 'on' ||
1078
+ (onErrorRollback === 'interactive' && interactive)) &&
1079
+ readTxStatus(db) === 'T';
1080
+ let savepointIssued = false;
1081
+ if (savepointActive) {
1082
+ try {
1083
+ await db.execSimple(`SAVEPOINT ${SAVEPOINT_NAME}`);
1084
+ savepointIssued = true;
1085
+ }
1086
+ catch (err) {
1087
+ // Mirror upstream: failure to install the savepoint is a hard error.
1088
+ const message = recordError(ctx, err);
1089
+ writeQueryError(ctx, message);
1090
+ stats.hadError = true;
1091
+ if (ctx.settings.timing) {
1092
+ stats.durationMs = performance.now() - started;
1093
+ ctx.stdout.write('\n' + formatDurationMs(stats.durationMs) + '\n');
1094
+ }
1095
+ return stats;
1096
+ }
1097
+ }
1098
+ // ----- Execute + print ---------------------------------------------------
1099
+ const out = pickOut(ctx, opts.oneShotOut);
1100
+ const fetchCount = readFetchCount(ctx.settings);
1101
+ let lastRowCount = null;
1102
+ try {
1103
+ if (fetchCount > 0 && isSelectCommand(sql)) {
1104
+ const r = await runCursorLoop(ctx, sql, fetchCount, out);
1105
+ stats.rowsAffected = r.rowsAffected;
1106
+ stats.rowsPrinted = r.rowsPrinted;
1107
+ stats.fetched = true;
1108
+ lastRowCount = r.lastRowCount;
1109
+ }
1110
+ else {
1111
+ const results = await db.execSimple(sql);
1112
+ const r = await renderResultSets(ctx, results, out);
1113
+ stats.rowsAffected = r.rowsAffected;
1114
+ stats.rowsPrinted = r.rowsPrinted;
1115
+ lastRowCount = r.lastRowCount;
1116
+ }
1117
+ }
1118
+ catch (err) {
1119
+ // `\;`-chained batches surface every result the server produced before
1120
+ // the ErrorResponse on the thrown Error's `partialResults` field (set
1121
+ // by the wire layer's ReadyForQuery handler). Render them in order
1122
+ // before printing the error itself so the user sees the same shape
1123
+ // upstream `PQgetResult` walks produce.
1124
+ const partial = err
1125
+ .partialResults;
1126
+ if (partial && partial.length > 0) {
1127
+ try {
1128
+ const r = await renderResultSets(ctx, partial, out);
1129
+ stats.rowsAffected = r.rowsAffected;
1130
+ stats.rowsPrinted = r.rowsPrinted;
1131
+ }
1132
+ catch {
1133
+ // Surface the original error; don't shadow it with a render failure.
1134
+ }
1135
+ }
1136
+ const message = recordError(ctx, err, sql);
1137
+ writeQueryError(ctx, message);
1138
+ stats.hadError = true;
1139
+ }
1140
+ // ----- ON_ERROR_ROLLBACK: resolve the savepoint --------------------------
1141
+ if (savepointIssued) {
1142
+ try {
1143
+ if (stats.hadError) {
1144
+ await db.execSimple(`ROLLBACK TO SAVEPOINT ${SAVEPOINT_NAME}`);
1145
+ // Release the now-empty savepoint too, matching upstream.
1146
+ await db.execSimple(`RELEASE SAVEPOINT ${SAVEPOINT_NAME}`);
1147
+ }
1148
+ else if (!destroysSavepoint(sql) && readTxStatus(db) === 'T') {
1149
+ await db.execSimple(`RELEASE SAVEPOINT ${SAVEPOINT_NAME}`);
1150
+ }
1151
+ }
1152
+ catch (err) {
1153
+ // Don't shadow the original error; just record this one if we don't
1154
+ // already have one to report.
1155
+ if (!stats.hadError) {
1156
+ const message = recordError(ctx, err);
1157
+ writeQueryError(ctx, message);
1158
+ stats.hadError = true;
1159
+ }
1160
+ }
1161
+ }
1162
+ // If we issued an implicit BEGIN for AUTOCOMMIT=off and the statement
1163
+ // itself failed in such a way that we ended up idle again, there is
1164
+ // nothing to clean up — the server has already rolled back. We
1165
+ // intentionally do not COMMIT here: that's the user's responsibility under
1166
+ // AUTOCOMMIT=off.
1167
+ void implicitBeginIssued;
1168
+ // Mirror upstream `SendQuery` tail (common.c lines 1217-1218):
1169
+ //
1170
+ // if (!OK && pset.echo == PSQL_ECHO_ERRORS)
1171
+ // pg_log_info("STATEMENT: %s", query);
1172
+ //
1173
+ // When ECHO=errors and the dispatch failed, emit a `STATEMENT: <sql>`
1174
+ // line so the user can correlate the error with the input statement.
1175
+ // `pg_log_info` writes to stderr in upstream and strips one trailing
1176
+ // newline before tacking its own `\n` on the message — we mirror by
1177
+ // going through ctx.stderr and the explicit trim.
1178
+ if (stats.hadError && ctx.settings.echo === 'errors') {
1179
+ // Strip leading whitespace + `--`-style comments from queryBuf so the
1180
+ // STATEMENT echo matches upstream's shape. Upstream `psqlscan.l`'s
1181
+ // `{whitespace}` rule (which includes line comments) SUPPRESSES
1182
+ // queryBuf appends until non-whitespace content has been collected;
1183
+ // our scanner accumulates verbatim. The server still ignores the
1184
+ // leading noise for `LINE N:` counting, but the STATEMENT echo
1185
+ // re-prints the buffer as we hold it. Bring them in line by
1186
+ // stripping here. Also strip one trailing `\n` to match
1187
+ // `pg_log_info("STATEMENT: %s", query)` (one-newline-strip +
1188
+ // explicit `\n` append).
1189
+ let stmt = sql;
1190
+ while (true) {
1191
+ const before = stmt.length;
1192
+ // Leading whitespace including form-feed (matches psqlscan's
1193
+ // {space} = [ \t\n\r\f]).
1194
+ stmt = stmt.replace(/^[ \t\n\r\f]+/, '');
1195
+ // Leading `--`-style line comment, up to (but not including) the
1196
+ // next newline. The trailing newline is then eaten by the next
1197
+ // whitespace pass.
1198
+ stmt = stmt.replace(/^--[^\n\r]*/, '');
1199
+ if (stmt.length === before)
1200
+ break;
1201
+ }
1202
+ if (stmt.endsWith('\n'))
1203
+ stmt = stmt.slice(0, -1);
1204
+ ctx.stderr.write(`STATEMENT: ${stmt}\n`);
1205
+ }
1206
+ // Mirror upstream's `SetResultVariables` / `SetErrorVariables` call at the
1207
+ // tail of `SendQuery`. ROW_COUNT mirrors libpq's `PQcmdTuples` on the LAST
1208
+ // result of a `\;` batch; SQLSTATE / ERROR reset every statement; the
1209
+ // LAST_ERROR_* pair only changes on failure (sticky on success).
1210
+ refreshErrorVars(ctx.settings, stats.hadError
1211
+ ? { kind: 'error' }
1212
+ : { kind: 'success', rowCount: lastRowCount });
1213
+ if (ctx.settings.timing) {
1214
+ stats.durationMs = performance.now() - started;
1215
+ ctx.stdout.write('\n' + formatDurationMs(stats.durationMs) + '\n');
1216
+ }
1217
+ return stats;
1218
+ };
1219
+ // ---------------------------------------------------------------------------
1220
+ // `psqlExec` — silent catalog-style queries used by backslash commands.
1221
+ //
1222
+ // Upstream returns a PGresult; the caller is expected to inspect status and
1223
+ // `PQclear` it. We return the last ResultSet from execSimple (the catalog
1224
+ // queries upstream uses are always single-statement) or null on error when
1225
+ // ignoreError is true.
1226
+ // ---------------------------------------------------------------------------
1227
+ export const psqlExec = async (conn, sql, ignoreError = false) => {
1228
+ try {
1229
+ const sets = await conn.execSimple(sql);
1230
+ if (sets.length === 0)
1231
+ return null;
1232
+ return sets[sets.length - 1];
1233
+ }
1234
+ catch (err) {
1235
+ if (ignoreError)
1236
+ return null;
1237
+ throw err;
1238
+ }
1239
+ };
1240
+ // Internal exports re-used by mainloop. Kept on the public surface so other
1241
+ // future call sites (cmd_io for \gexec, cmd_describe for catalog queries)
1242
+ // can lean on the same primitives.
1243
+ export const __testing = {
1244
+ commandNoBegin,
1245
+ isSelectCommand,
1246
+ destroysSavepoint,
1247
+ peekKeywords,
1248
+ SAVEPOINT_NAME,
1249
+ CURSOR_NAME,
1250
+ };