neonctl 2.22.2 → 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 (113) hide show
  1. package/README.md +84 -0
  2. package/analytics.js +5 -2
  3. package/commands/branches.js +9 -1
  4. package/commands/connection_string.js +9 -1
  5. package/commands/functions.js +277 -0
  6. package/commands/index.js +4 -0
  7. package/commands/neon_auth.js +1013 -0
  8. package/commands/projects.js +9 -1
  9. package/commands/psql.js +6 -1
  10. package/functions_api.js +44 -0
  11. package/package.json +15 -5
  12. package/psql/cli.js +51 -0
  13. package/psql/command/cmd_cond.js +437 -0
  14. package/psql/command/cmd_connect.js +815 -0
  15. package/psql/command/cmd_copy.js +1025 -0
  16. package/psql/command/cmd_describe.js +1810 -0
  17. package/psql/command/cmd_format.js +909 -0
  18. package/psql/command/cmd_io.js +2187 -0
  19. package/psql/command/cmd_lo.js +385 -0
  20. package/psql/command/cmd_meta.js +970 -0
  21. package/psql/command/cmd_misc.js +187 -0
  22. package/psql/command/cmd_pipeline.js +1141 -0
  23. package/psql/command/cmd_restrict.js +171 -0
  24. package/psql/command/cmd_show.js +751 -0
  25. package/psql/command/dispatch.js +343 -0
  26. package/psql/command/inputQueue.js +42 -0
  27. package/psql/command/shared.js +71 -0
  28. package/psql/complete/filenames.js +139 -0
  29. package/psql/complete/index.js +104 -0
  30. package/psql/complete/matcher.js +314 -0
  31. package/psql/complete/psqlVars.js +247 -0
  32. package/psql/complete/queries.js +491 -0
  33. package/psql/complete/rules.js +2387 -0
  34. package/psql/core/common.js +1250 -0
  35. package/psql/core/help.js +576 -0
  36. package/psql/core/mainloop.js +1353 -0
  37. package/psql/core/prompt.js +437 -0
  38. package/psql/core/settings.js +684 -0
  39. package/psql/core/sqlHelp.js +1066 -0
  40. package/psql/core/startup.js +840 -0
  41. package/psql/core/syncVars.js +116 -0
  42. package/psql/core/variables.js +287 -0
  43. package/psql/describe/formatters.js +1277 -0
  44. package/psql/describe/processNamePattern.js +270 -0
  45. package/psql/describe/queries.js +2373 -0
  46. package/psql/describe/versionGate.js +43 -0
  47. package/psql/index.js +2005 -0
  48. package/psql/io/history.js +299 -0
  49. package/psql/io/input.js +120 -0
  50. package/psql/io/lineEditor/buffer.js +323 -0
  51. package/psql/io/lineEditor/complete.js +227 -0
  52. package/psql/io/lineEditor/filename.js +159 -0
  53. package/psql/io/lineEditor/index.js +891 -0
  54. package/psql/io/lineEditor/keymap.js +738 -0
  55. package/psql/io/lineEditor/vt100.js +363 -0
  56. package/psql/io/pgpass.js +202 -0
  57. package/psql/io/pgservice.js +194 -0
  58. package/psql/io/psqlrc.js +422 -0
  59. package/psql/print/aligned.js +1756 -0
  60. package/psql/print/asciidoc.js +248 -0
  61. package/psql/print/crosstab.js +460 -0
  62. package/psql/print/csv.js +92 -0
  63. package/psql/print/html.js +258 -0
  64. package/psql/print/json.js +96 -0
  65. package/psql/print/latex.js +396 -0
  66. package/psql/print/pager.js +265 -0
  67. package/psql/print/troff.js +258 -0
  68. package/psql/print/unaligned.js +118 -0
  69. package/psql/print/units.js +135 -0
  70. package/psql/scanner/slash.js +513 -0
  71. package/psql/scanner/sql.js +910 -0
  72. package/psql/scanner/stringutils.js +390 -0
  73. package/psql/types/backslash.js +1 -0
  74. package/psql/types/connection.js +1 -0
  75. package/psql/types/index.js +7 -0
  76. package/psql/types/printer.js +1 -0
  77. package/psql/types/repl.js +1 -0
  78. package/psql/types/scanner.js +24 -0
  79. package/psql/types/settings.js +1 -0
  80. package/psql/types/variables.js +1 -0
  81. package/psql/wire/connection.js +2844 -0
  82. package/psql/wire/copy.js +108 -0
  83. package/psql/wire/notify.js +59 -0
  84. package/psql/wire/pipeline.js +519 -0
  85. package/psql/wire/protocol.js +466 -0
  86. package/psql/wire/sasl.js +296 -0
  87. package/psql/wire/tls.js +596 -0
  88. package/test_utils/fixtures.js +1 -0
  89. package/utils/esbuild.js +147 -0
  90. package/utils/psql.js +107 -11
  91. package/utils/zip.js +4 -0
  92. package/writer.js +1 -1
  93. package/commands/auth.test.js +0 -211
  94. package/commands/branches.test.js +0 -460
  95. package/commands/checkout.test.js +0 -170
  96. package/commands/connection_string.test.js +0 -196
  97. package/commands/data_api.test.js +0 -169
  98. package/commands/databases.test.js +0 -39
  99. package/commands/help.test.js +0 -9
  100. package/commands/init.test.js +0 -56
  101. package/commands/ip_allow.test.js +0 -59
  102. package/commands/link.test.js +0 -381
  103. package/commands/operations.test.js +0 -7
  104. package/commands/orgs.test.js +0 -7
  105. package/commands/projects.test.js +0 -144
  106. package/commands/psql.test.js +0 -49
  107. package/commands/roles.test.js +0 -37
  108. package/commands/set_context.test.js +0 -159
  109. package/commands/vpc_endpoints.test.js +0 -69
  110. package/context.test.js +0 -119
  111. package/env.test.js +0 -55
  112. package/utils/formats.test.js +0 -32
  113. package/writer.test.js +0 -104
@@ -0,0 +1,187 @@
1
+ /**
2
+ * Miscellaneous backslash commands (WP-22).
3
+ *
4
+ * Hosts commands that don't fit the meta / format / I/O / describe / pipeline
5
+ * categories. For this WP that's only `\crosstabview`; future WPs hosting
6
+ * standalone "do one thing" commands (e.g. `\dconfig`, `\sf+`, `\watch+`)
7
+ * can land here without spinning up a new module per command.
8
+ *
9
+ * Convention for adding a misc command: export its {@link BackslashCmdSpec}
10
+ * (so tests can drive it in isolation) and add a `registry.register(...)` line
11
+ * in {@link registerMiscCommands} below — `cmdCrosstabview` is the worked
12
+ * example.
13
+ */
14
+ import { printCrosstab } from '../print/crosstab.js';
15
+ import { writeErr } from './shared.js';
16
+ // ---------------------------------------------------------------------------
17
+ // Helpers
18
+ // ---------------------------------------------------------------------------
19
+ const errResult = (ctx, message) => {
20
+ ctx.settings.lastErrorResult = { message };
21
+ writeErr(`\\${ctx.cmdName}: ${message}\n`);
22
+ // Mark the diagnostic as already-emitted so the mainloop's fallback
23
+ // does not double-print `psql: ERROR: <msg>`. Matches the pattern
24
+ // used by `cmd_io.ts::errResult` for the same reason. The conformance
25
+ // expected output has one error line per failure, not two.
26
+ return { status: 'error', errorWritten: true };
27
+ };
28
+ /**
29
+ * Decode a slash arg into a `CrosstabOptions` value (name or 1-based index).
30
+ *
31
+ * Upstream `\crosstabview` accepts either form per arg; we keep that and
32
+ * defer index/name disambiguation to `pivotResultSet` (which mirrors
33
+ * upstream's `indexOfColumn`). Args that look like signed integers
34
+ * (`/^[+-]?\d+$/`) are returned as numbers so the 4th arg's `+`/`-`
35
+ * direction prefix flows through.
36
+ */
37
+ const parseColRef = (raw) => {
38
+ const trimmed = raw.trim();
39
+ if (/^[+-]?\d+$/.test(trimmed)) {
40
+ return parseInt(trimmed, 10);
41
+ }
42
+ return trimmed;
43
+ };
44
+ /**
45
+ * Return `true` when `text` is just whitespace and SQL comments (no
46
+ * executable statement). Our SQL scanner accumulates comments into
47
+ * `queryBuf` verbatim so a line like `-- foo\n` followed by `\crosstabview`
48
+ * leaves a non-empty buffer that nonetheless has nothing to send. This
49
+ * mirrors upstream's `psql_scan_buffer_is_empty` heuristic used by
50
+ * `do_crosstabview` / `\g`-family commands to decide whether to fall
51
+ * back to `pset.last_query`.
52
+ *
53
+ * Strips `--` line comments and C-style block comments (with nesting),
54
+ * leaves quoted-string content alone (so a comment-looking sequence inside
55
+ * a string still counts as executable). If anything non-whitespace remains,
56
+ * returns `false`.
57
+ */
58
+ const isCommentOnly = (text) => {
59
+ const n = text.length;
60
+ let i = 0;
61
+ while (i < n) {
62
+ const c = text[i];
63
+ if (c === ' ' || c === '\t' || c === '\n' || c === '\r') {
64
+ i++;
65
+ continue;
66
+ }
67
+ if (c === '-' && text[i + 1] === '-') {
68
+ // Skip to end-of-line.
69
+ i += 2;
70
+ while (i < n && text[i] !== '\n' && text[i] !== '\r')
71
+ i++;
72
+ continue;
73
+ }
74
+ if (c === '/' && text[i + 1] === '*') {
75
+ // Skip nested block comment (PG extends C-style comments with nesting).
76
+ let depth = 1;
77
+ i += 2;
78
+ while (i < n && depth > 0) {
79
+ if (text[i] === '/' && text[i + 1] === '*') {
80
+ depth++;
81
+ i += 2;
82
+ continue;
83
+ }
84
+ if (text[i] === '*' && text[i + 1] === '/') {
85
+ depth--;
86
+ i += 2;
87
+ continue;
88
+ }
89
+ i++;
90
+ }
91
+ continue;
92
+ }
93
+ return false;
94
+ }
95
+ return true;
96
+ };
97
+ // ---------------------------------------------------------------------------
98
+ // \crosstabview [colV [colH [colD [sortColH]]]]
99
+ // ---------------------------------------------------------------------------
100
+ /**
101
+ * Pop the most recent ResultSet from a possibly multi-statement execution.
102
+ *
103
+ * Upstream's `do_crosstabview` operates on the last PGresult of the prior
104
+ * query (since `\crosstabview` is itself the trigger that submits the
105
+ * buffered SQL). `execSimple` returns an array (one ResultSet per
106
+ * statement); we take the last one — that's the result the user wants
107
+ * pivoted.
108
+ */
109
+ const lastTuplesResult = (results) => {
110
+ for (let i = results.length - 1; i >= 0; i--) {
111
+ // Heuristic: a SELECT/VALUES/SHOW result will have fields populated;
112
+ // a bare INSERT/UPDATE/DELETE without RETURNING will not. Upstream
113
+ // just checks PGRES_TUPLES_OK; we approximate that with non-empty
114
+ // fields, falling back to the last result if nothing matches.
115
+ if (results[i].fields.length > 0)
116
+ return results[i];
117
+ }
118
+ return results.length > 0 ? results[results.length - 1] : null;
119
+ };
120
+ export const cmdCrosstabview = {
121
+ name: 'crosstabview',
122
+ helpKey: 'crosstabview',
123
+ async run(ctx) {
124
+ if (!ctx.settings.db) {
125
+ return errResult(ctx, 'no connection to the server');
126
+ }
127
+ // Source SQL is the active query buffer; when that's effectively
128
+ // empty (whitespace + comments only — the common shape after a
129
+ // semicolon-terminated query followed by a trailing `-- comment`
130
+ // line), fall back to `pset.last_query` which upstream re-executes
131
+ // for both `\g`-family commands and `\crosstabview`.
132
+ let sql = ctx.queryBuf.trim();
133
+ if (sql.length === 0 || isCommentOnly(sql)) {
134
+ sql = ctx.settings.lastQuery.trim();
135
+ }
136
+ if (sql.length === 0) {
137
+ return errResult(ctx, 'no SQL command');
138
+ }
139
+ // Read up to four args from the slash arg stream. Each is optional;
140
+ // missing args fall back to the pivot defaults (1, 2, 3, none).
141
+ const args = [
142
+ undefined,
143
+ undefined,
144
+ undefined,
145
+ undefined,
146
+ ];
147
+ for (let i = 0; i < 4; i++) {
148
+ const a = ctx.nextArg('normal');
149
+ if (a === null)
150
+ break;
151
+ args[i] = parseColRef(a);
152
+ }
153
+ let results;
154
+ try {
155
+ results = await ctx.settings.db.execSimple(sql);
156
+ }
157
+ catch (err) {
158
+ const msg = err instanceof Error ? err.message : String(err);
159
+ return errResult(ctx, msg);
160
+ }
161
+ const target = lastTuplesResult(results);
162
+ if (!target) {
163
+ return errResult(ctx, 'statement did not return a result set');
164
+ }
165
+ if (target.fields.length === 0) {
166
+ return errResult(ctx, 'statement did not return a result set');
167
+ }
168
+ const opts = {
169
+ colV: args[0],
170
+ colH: args[1],
171
+ colD: args[2],
172
+ sortColH: args[3],
173
+ };
174
+ const err = await printCrosstab(target, opts, ctx.settings.popt, process.stdout);
175
+ if (err) {
176
+ return errResult(ctx, err.error);
177
+ }
178
+ return { status: 'reset-buf', newBuf: '' };
179
+ },
180
+ };
181
+ /**
182
+ * Register all misc commands on the supplied registry. Called from
183
+ * `dispatch.ts::defaultRegistry()`.
184
+ */
185
+ export const registerMiscCommands = (registry) => {
186
+ registry.register(cmdCrosstabview);
187
+ };