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.
- package/README.md +242 -16
- package/analytics.js +5 -2
- package/commands/branches.js +9 -1
- package/commands/checkout.js +249 -0
- package/commands/connection_string.js +15 -2
- package/commands/data_api.js +286 -0
- package/commands/functions.js +277 -0
- package/commands/index.js +12 -0
- package/commands/link.js +667 -0
- package/commands/neon_auth.js +1013 -0
- package/commands/projects.js +9 -1
- package/commands/psql.js +62 -0
- package/commands/set_context.js +7 -2
- package/context.js +86 -14
- package/functions_api.js +44 -0
- package/index.js +3 -0
- package/package.json +60 -51
- package/psql/cli.js +51 -0
- package/psql/command/cmd_cond.js +437 -0
- package/psql/command/cmd_connect.js +815 -0
- package/psql/command/cmd_copy.js +1025 -0
- package/psql/command/cmd_describe.js +1810 -0
- package/psql/command/cmd_format.js +909 -0
- package/psql/command/cmd_io.js +2187 -0
- package/psql/command/cmd_lo.js +385 -0
- package/psql/command/cmd_meta.js +970 -0
- package/psql/command/cmd_misc.js +187 -0
- package/psql/command/cmd_pipeline.js +1141 -0
- package/psql/command/cmd_restrict.js +171 -0
- package/psql/command/cmd_show.js +751 -0
- package/psql/command/dispatch.js +343 -0
- package/psql/command/inputQueue.js +42 -0
- package/psql/command/shared.js +71 -0
- package/psql/complete/filenames.js +139 -0
- package/psql/complete/index.js +104 -0
- package/psql/complete/matcher.js +314 -0
- package/psql/complete/psqlVars.js +247 -0
- package/psql/complete/queries.js +491 -0
- package/psql/complete/rules.js +2387 -0
- package/psql/core/common.js +1250 -0
- package/psql/core/help.js +576 -0
- package/psql/core/mainloop.js +1353 -0
- package/psql/core/prompt.js +437 -0
- package/psql/core/settings.js +684 -0
- package/psql/core/sqlHelp.js +1066 -0
- package/psql/core/startup.js +840 -0
- package/psql/core/syncVars.js +116 -0
- package/psql/core/variables.js +287 -0
- package/psql/describe/formatters.js +1277 -0
- package/psql/describe/processNamePattern.js +270 -0
- package/psql/describe/queries.js +2373 -0
- package/psql/describe/versionGate.js +43 -0
- package/psql/index.js +2005 -0
- package/psql/io/history.js +299 -0
- package/psql/io/input.js +120 -0
- package/psql/io/lineEditor/buffer.js +323 -0
- package/psql/io/lineEditor/complete.js +227 -0
- package/psql/io/lineEditor/filename.js +159 -0
- package/psql/io/lineEditor/index.js +891 -0
- package/psql/io/lineEditor/keymap.js +738 -0
- package/psql/io/lineEditor/vt100.js +363 -0
- package/psql/io/pgpass.js +202 -0
- package/psql/io/pgservice.js +194 -0
- package/psql/io/psqlrc.js +422 -0
- package/psql/print/aligned.js +1756 -0
- package/psql/print/asciidoc.js +248 -0
- package/psql/print/crosstab.js +460 -0
- package/psql/print/csv.js +92 -0
- package/psql/print/html.js +258 -0
- package/psql/print/json.js +96 -0
- package/psql/print/latex.js +396 -0
- package/psql/print/pager.js +265 -0
- package/psql/print/troff.js +258 -0
- package/psql/print/unaligned.js +118 -0
- package/psql/print/units.js +135 -0
- package/psql/scanner/slash.js +513 -0
- package/psql/scanner/sql.js +910 -0
- package/psql/scanner/stringutils.js +390 -0
- package/psql/types/backslash.js +1 -0
- package/psql/types/connection.js +1 -0
- package/psql/types/index.js +7 -0
- package/psql/types/printer.js +1 -0
- package/psql/types/repl.js +1 -0
- package/psql/types/scanner.js +24 -0
- package/psql/types/settings.js +1 -0
- package/psql/types/variables.js +1 -0
- package/psql/wire/connection.js +2844 -0
- package/psql/wire/copy.js +108 -0
- package/psql/wire/notify.js +59 -0
- package/psql/wire/pipeline.js +519 -0
- package/psql/wire/protocol.js +466 -0
- package/psql/wire/sasl.js +296 -0
- package/psql/wire/tls.js +596 -0
- package/test_utils/fixtures.js +1 -0
- package/utils/enrichers.js +18 -1
- package/utils/esbuild.js +147 -0
- package/utils/middlewares.js +1 -1
- package/utils/psql.js +107 -11
- package/utils/zip.js +4 -0
- package/writer.js +1 -1
- package/commands/auth.test.js +0 -211
- package/commands/branches.test.js +0 -460
- package/commands/connection_string.test.js +0 -196
- package/commands/databases.test.js +0 -39
- package/commands/help.test.js +0 -9
- package/commands/init.test.js +0 -56
- package/commands/ip_allow.test.js +0 -59
- package/commands/operations.test.js +0 -7
- package/commands/orgs.test.js +0 -7
- package/commands/projects.test.js +0 -144
- package/commands/roles.test.js +0 -37
- package/commands/set_context.test.js +0 -159
- package/commands/vpc_endpoints.test.js +0 -69
- package/env.test.js +0 -55
- package/utils/formats.test.js +0 -32
- package/writer.test.js +0 -104
|
@@ -0,0 +1,684 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* psql Settings.
|
|
3
|
+
*
|
|
4
|
+
* TypeScript port of the `pset` initialization performed by PostgreSQL's
|
|
5
|
+
* `src/bin/psql/startup.c` and the defaults declared in `src/bin/psql/settings.h`.
|
|
6
|
+
*
|
|
7
|
+
* Two responsibilities:
|
|
8
|
+
*
|
|
9
|
+
* 1. `defaultSettings(varStore)` — build a fully-initialized `PsqlSettings`
|
|
10
|
+
* with the same defaults psql itself uses (prompts, verbosity, send mode,
|
|
11
|
+
* completion case, print options, etc.). The Connection (`settings.db`)
|
|
12
|
+
* stays `null` here; it is populated later by the startup/connect WP.
|
|
13
|
+
*
|
|
14
|
+
* 2. `applyEnvOverrides(settings, env?)` — bridge process environment to psql
|
|
15
|
+
* state. Some variables psql looks up "live" (e.g. PSQL_HISTORY when
|
|
16
|
+
* readline is initialized in `src/bin/psql/input.c`, PSQL_EDITOR/EDITOR/
|
|
17
|
+
* VISUAL when `\e` is invoked in `command.c`, PAGER/PSQL_PAGER from
|
|
18
|
+
* `fe_utils/print.c`). We capture them eagerly into the var store under
|
|
19
|
+
* stable names so later WPs can read them without re-reading
|
|
20
|
+
* `process.env`. The COLUMNS env var maps to `popt.topt.envColumns` to
|
|
21
|
+
* mirror upstream's `pset.popt.topt.env_columns` assignment.
|
|
22
|
+
*
|
|
23
|
+
* Deviations from upstream:
|
|
24
|
+
*
|
|
25
|
+
* - psql exposes `notty` based on isatty(stdin) && isatty(stdout). We have
|
|
26
|
+
* no stream context at construction time, so the default is `false`;
|
|
27
|
+
* higher-level WPs (startup) override this.
|
|
28
|
+
* - The PROMPT1/PROMPT2/PROMPT3 psql variables and `settings.prompt{1,2,3}`
|
|
29
|
+
* are seeded to the same defaults and kept in sync via `addHook` so
|
|
30
|
+
* `\set PROMPT1 …` reflects in `settings.prompt1`. Upstream achieves this
|
|
31
|
+
* with `prompt1_hook` etc. in `startup.c`.
|
|
32
|
+
* - We do not eagerly read PSQLRC (the rc loader lives in a later WP).
|
|
33
|
+
* Instead we surface PSQLRC verbatim under the `PSQLRC` psql variable.
|
|
34
|
+
*/
|
|
35
|
+
export const DEFAULT_PROMPT1 = '%/%R%x%# ';
|
|
36
|
+
export const DEFAULT_PROMPT2 = '%/%R%x%# ';
|
|
37
|
+
export const DEFAULT_PROMPT3 = '>> ';
|
|
38
|
+
export const DEFAULT_CSV_FIELD_SEP = ',';
|
|
39
|
+
export const DEFAULT_FIELD_SEP = '|';
|
|
40
|
+
export const DEFAULT_RECORD_SEP = '\n';
|
|
41
|
+
const DEFAULT_VERBOSITY = 'default';
|
|
42
|
+
const DEFAULT_SHOW_CONTEXT = 'errors';
|
|
43
|
+
const DEFAULT_ECHO = 'none';
|
|
44
|
+
const DEFAULT_ECHO_HIDDEN = 'off';
|
|
45
|
+
const DEFAULT_ON_ERROR_ROLLBACK = 'off';
|
|
46
|
+
const DEFAULT_COMP_CASE = 'preserve-upper';
|
|
47
|
+
const DEFAULT_SEND_MODE = 'extended-query';
|
|
48
|
+
const DEFAULT_HIST_CONTROL = 'none';
|
|
49
|
+
/** Upstream `DEFAULT_WATCH_INTERVAL` from settings.h (psql 18). */
|
|
50
|
+
const DEFAULT_WATCH_INTERVAL = '2';
|
|
51
|
+
/** Upstream `DEFAULT_WATCH_INTERVAL_MAX` (1e6 seconds). */
|
|
52
|
+
const WATCH_INTERVAL_MAX = 1000000;
|
|
53
|
+
/**
|
|
54
|
+
* Build the psql `PrintQueryOpts` with the same defaults as upstream's
|
|
55
|
+
* pset.popt initialization (see `startup.c` after the `pset.db = NULL`
|
|
56
|
+
* block). border=1, format=aligned, pager="on" (upstream
|
|
57
|
+
* `pset.popt.topt.pager = 1` — the printer paginates only on a TTY and
|
|
58
|
+
* only when output exceeds the screen height, so the default is harmless
|
|
59
|
+
* for non-TTY runs), start/stop_table=true, default_footer=true.
|
|
60
|
+
*/
|
|
61
|
+
const buildDefaultPrintOpts = () => ({
|
|
62
|
+
topt: {
|
|
63
|
+
format: 'aligned',
|
|
64
|
+
expanded: 'off',
|
|
65
|
+
border: 1,
|
|
66
|
+
pager: 'on',
|
|
67
|
+
pagerMinLines: 0,
|
|
68
|
+
tuplesOnly: false,
|
|
69
|
+
startTable: true,
|
|
70
|
+
stopTable: true,
|
|
71
|
+
defaultFooter: true,
|
|
72
|
+
prior: 0,
|
|
73
|
+
encoding: 'UTF8',
|
|
74
|
+
envColumns: 0,
|
|
75
|
+
columns: 0,
|
|
76
|
+
unicodeBorderLineStyle: 'ascii',
|
|
77
|
+
unicodeColumnLineStyle: 'ascii',
|
|
78
|
+
unicodeHeaderLineStyle: 'ascii',
|
|
79
|
+
// Upstream `popt.topt.unicode_{border,column,header}_linestyle` default
|
|
80
|
+
// to `UNICODE_LINESTYLE_SINGLE` (see `initialize_global_options` in
|
|
81
|
+
// `print.c`). Independent of `unicode*LineStyle` (ascii|unicode).
|
|
82
|
+
unicodeBorderStyle: 'single',
|
|
83
|
+
unicodeColumnStyle: 'single',
|
|
84
|
+
unicodeHeaderStyle: 'single',
|
|
85
|
+
fieldSep: DEFAULT_FIELD_SEP,
|
|
86
|
+
recordSep: DEFAULT_RECORD_SEP,
|
|
87
|
+
numericLocale: false,
|
|
88
|
+
tableAttr: null,
|
|
89
|
+
title: null,
|
|
90
|
+
footers: null,
|
|
91
|
+
translateHeader: false,
|
|
92
|
+
translateColumns: null,
|
|
93
|
+
nullPrint: '',
|
|
94
|
+
csvFieldSep: DEFAULT_CSV_FIELD_SEP,
|
|
95
|
+
// Upstream `pset.popt.topt.expanded_header_width_type =
|
|
96
|
+
// PRINT_XHEADER_FULL` — default rendered as the literal "full" in the
|
|
97
|
+
// bulk `\pset` view.
|
|
98
|
+
xheaderWidth: 'full',
|
|
99
|
+
},
|
|
100
|
+
nullPrint: '',
|
|
101
|
+
title: null,
|
|
102
|
+
footers: null,
|
|
103
|
+
translateHeader: false,
|
|
104
|
+
translateColumns: null,
|
|
105
|
+
nTranslateColumns: 0,
|
|
106
|
+
});
|
|
107
|
+
/**
|
|
108
|
+
* Build a fresh `PsqlSettings` with psql defaults, backed by the provided
|
|
109
|
+
* variable store. The store is mutated to:
|
|
110
|
+
*
|
|
111
|
+
* - seed `PROMPT1`/`PROMPT2`/`PROMPT3` to their default strings, and
|
|
112
|
+
* - wire substitute-style hooks so that subsequent `\set PROMPT1 …` calls
|
|
113
|
+
* update the corresponding `settings.prompt{1,2,3}` field.
|
|
114
|
+
*
|
|
115
|
+
* Connection (`settings.db`) is left `null`; it is the responsibility of
|
|
116
|
+
* the startup/connect WP to populate it after a successful libpq-equivalent
|
|
117
|
+
* connection.
|
|
118
|
+
*/
|
|
119
|
+
export const defaultSettings = (varStore) => {
|
|
120
|
+
const settings = {
|
|
121
|
+
db: null,
|
|
122
|
+
vars: varStore,
|
|
123
|
+
popt: buildDefaultPrintOpts(),
|
|
124
|
+
mainfile: null,
|
|
125
|
+
inputfile: null,
|
|
126
|
+
curCmdSource: 'stdin',
|
|
127
|
+
restrictedKey: null,
|
|
128
|
+
prompt1: DEFAULT_PROMPT1,
|
|
129
|
+
prompt2: DEFAULT_PROMPT2,
|
|
130
|
+
prompt3: DEFAULT_PROMPT3,
|
|
131
|
+
notty: false,
|
|
132
|
+
quiet: false,
|
|
133
|
+
singleline: false,
|
|
134
|
+
singlestep: false,
|
|
135
|
+
onErrorStop: false,
|
|
136
|
+
fetchCount: 0,
|
|
137
|
+
verbosity: DEFAULT_VERBOSITY,
|
|
138
|
+
showContext: DEFAULT_SHOW_CONTEXT,
|
|
139
|
+
echo: DEFAULT_ECHO,
|
|
140
|
+
echoHidden: DEFAULT_ECHO_HIDDEN,
|
|
141
|
+
onErrorRollback: DEFAULT_ON_ERROR_ROLLBACK,
|
|
142
|
+
compCase: DEFAULT_COMP_CASE,
|
|
143
|
+
sendMode: DEFAULT_SEND_MODE,
|
|
144
|
+
histControl: DEFAULT_HIST_CONTROL,
|
|
145
|
+
hideCompression: false,
|
|
146
|
+
hideTableam: false,
|
|
147
|
+
logfile: null,
|
|
148
|
+
timing: false,
|
|
149
|
+
lastErrorResult: null,
|
|
150
|
+
lastQuery: '',
|
|
151
|
+
};
|
|
152
|
+
// Seed the PROMPT psql variables and wire them to the settings fields so
|
|
153
|
+
// `\set PROMPT1 …` is reflected in `settings.prompt1`. Upstream does this
|
|
154
|
+
// via assign hooks (prompt1_hook / prompt2_hook / prompt3_hook).
|
|
155
|
+
varStore.addHook('PROMPT1', (newValue) => {
|
|
156
|
+
settings.prompt1 = newValue ?? '';
|
|
157
|
+
return true;
|
|
158
|
+
});
|
|
159
|
+
varStore.addHook('PROMPT2', (newValue) => {
|
|
160
|
+
settings.prompt2 = newValue ?? '';
|
|
161
|
+
return true;
|
|
162
|
+
});
|
|
163
|
+
varStore.addHook('PROMPT3', (newValue) => {
|
|
164
|
+
settings.prompt3 = newValue ?? '';
|
|
165
|
+
return true;
|
|
166
|
+
});
|
|
167
|
+
varStore.set('PROMPT1', DEFAULT_PROMPT1);
|
|
168
|
+
varStore.set('PROMPT2', DEFAULT_PROMPT2);
|
|
169
|
+
varStore.set('PROMPT3', DEFAULT_PROMPT3);
|
|
170
|
+
// Mirror upstream's small set of always-present session vars.
|
|
171
|
+
varStore.set('LAST_ERROR_MESSAGE', '');
|
|
172
|
+
varStore.set('LAST_ERROR_SQLSTATE', '00000');
|
|
173
|
+
// PIPELINE_SYNC_COUNT / PIPELINE_COMMAND_COUNT / PIPELINE_RESULT_COUNT —
|
|
174
|
+
// upstream `pset.piped_syncs` / `pset.piped_commands` / `pset.piped_results`
|
|
175
|
+
// are mirrored into psql variables of these names (used by `\echo
|
|
176
|
+
// :PIPELINE_*` throughout `regress/psql_pipeline.sql`). The three counters
|
|
177
|
+
// are plain string vars seeded to "0" at startup; `\startpipeline` /
|
|
178
|
+
// `\endpipeline` reset them to "0", and the pipeline driver bumps them
|
|
179
|
+
// per upstream's SendQuery / discardAbortedPipelineResults logic.
|
|
180
|
+
// Upstream installs no substitute/assign hooks on these — `\unset
|
|
181
|
+
// PIPELINE_SYNC_COUNT` actually drops the variable (verified empirically:
|
|
182
|
+
// `\echo :PIPELINE_SYNC_COUNT` then prints the literal `:NAME`). We
|
|
183
|
+
// mirror that here: plain seed, no hook.
|
|
184
|
+
varStore.set('PIPELINE_SYNC_COUNT', '0');
|
|
185
|
+
varStore.set('PIPELINE_COMMAND_COUNT', '0');
|
|
186
|
+
varStore.set('PIPELINE_RESULT_COUNT', '0');
|
|
187
|
+
// SHOW_ALL_RESULTS defaults to 'on' — when off ('0' / 'off') the REPL only
|
|
188
|
+
// prints the final result set of a multi-statement `\;`-separated batch
|
|
189
|
+
// (upstream `pset.show_all_results`, set in startup.c).
|
|
190
|
+
varStore.set('SHOW_ALL_RESULTS', 'on');
|
|
191
|
+
// ENCODING tracks the server's client_encoding ParameterStatus. We seed
|
|
192
|
+
// with the connection-default UTF8 here; mainloop refreshes it after the
|
|
193
|
+
// connection is bound (and again whenever `SET client_encoding` flips the
|
|
194
|
+
// server value). Mirrors `pset.encoding` / `SetVariable("ENCODING", ...)`
|
|
195
|
+
// in upstream startup.c / common.c.
|
|
196
|
+
varStore.set('ENCODING', 'UTF8');
|
|
197
|
+
// WATCH_INTERVAL — upstream `watch_interval_substitute_hook` (null →
|
|
198
|
+
// DEFAULT_WATCH_INTERVAL "2") + `watch_interval_hook` which calls
|
|
199
|
+
// `ParseVariableDouble("WATCH_INTERVAL", ..., 0, DEFAULT_WATCH_INTERVAL_MAX)`.
|
|
200
|
+
// Empty value reaches ParseVariableDouble which short-circuits with
|
|
201
|
+
// "invalid input syntax for variable \"WATCH_INTERVAL\"" without
|
|
202
|
+
// substituting; the previous value is preserved by `trySet`.
|
|
203
|
+
varStore.addHook('WATCH_INTERVAL', (newValue) => {
|
|
204
|
+
if (newValue === null) {
|
|
205
|
+
// `\unset WATCH_INTERVAL` re-seeds the default. The substitute also
|
|
206
|
+
// runs once on `addHook` registration so `\echo :WATCH_INTERVAL`
|
|
207
|
+
// prints "2" before the user touches it (no explicit seed needed).
|
|
208
|
+
return { substitute: DEFAULT_WATCH_INTERVAL };
|
|
209
|
+
}
|
|
210
|
+
if (newValue === '') {
|
|
211
|
+
return `invalid input syntax for variable "WATCH_INTERVAL"`;
|
|
212
|
+
}
|
|
213
|
+
const range = validateWatchInterval(newValue);
|
|
214
|
+
if (range !== null)
|
|
215
|
+
return range;
|
|
216
|
+
return true;
|
|
217
|
+
});
|
|
218
|
+
// ON_ERROR_STOP assign hook. Upstream `bool_substitute_hook` /
|
|
219
|
+
// `on_error_stop_assign_hook` (startup.c) keep `pset.on_error_stop` in
|
|
220
|
+
// lockstep with the variable so both `--on-error-stop` (which flips the
|
|
221
|
+
// flag directly) and `--set ON_ERROR_STOP=1` (which only writes the
|
|
222
|
+
// variable) take effect. Empty value → "on" (substitute), non-boolean →
|
|
223
|
+
// reject with upstream's wording.
|
|
224
|
+
varStore.addHook('ON_ERROR_STOP', makeBoolHook('ON_ERROR_STOP', (parsed) => {
|
|
225
|
+
settings.onErrorStop = parsed;
|
|
226
|
+
}));
|
|
227
|
+
// AUTOCOMMIT assign hook — upstream `bool_substitute_hook` +
|
|
228
|
+
// `autocommit_assign_hook`. Empty value → "on", non-boolean → reject
|
|
229
|
+
// with `unrecognized value "<value>" for "AUTOCOMMIT": Boolean expected`.
|
|
230
|
+
varStore.addHook('AUTOCOMMIT', makeBoolHook('AUTOCOMMIT'));
|
|
231
|
+
// Seed AUTOCOMMIT to "on" (upstream default; pset.autocommit = true).
|
|
232
|
+
varStore.set('AUTOCOMMIT', 'on');
|
|
233
|
+
// FETCH_COUNT — upstream `fetch_count_substitute_hook` (null → "0") +
|
|
234
|
+
// `fetch_count_assign_hook` which delegates to `ParseVariableNum`. Empty
|
|
235
|
+
// `\set FETCH_COUNT` reaches ParseVariableNum which fails with
|
|
236
|
+
// `invalid value "" for "FETCH_COUNT": integer expected`, preserving
|
|
237
|
+
// the prior value. The substitute fires on addHook registration so
|
|
238
|
+
// `\echo :FETCH_COUNT` prints "0" before the user touches it.
|
|
239
|
+
varStore.addHook('FETCH_COUNT', (newValue) => {
|
|
240
|
+
if (newValue === null) {
|
|
241
|
+
settings.fetchCount = 0;
|
|
242
|
+
return { substitute: '0' };
|
|
243
|
+
}
|
|
244
|
+
const n = parseIntOrNull(newValue);
|
|
245
|
+
if (n === null) {
|
|
246
|
+
return `invalid value "${newValue}" for "FETCH_COUNT": integer expected`;
|
|
247
|
+
}
|
|
248
|
+
settings.fetchCount = Math.max(0, n);
|
|
249
|
+
return true;
|
|
250
|
+
});
|
|
251
|
+
// ON_ERROR_ROLLBACK assign hook — upstream `on_error_rollback_substitute_hook`
|
|
252
|
+
// (null → "off", empty → "on") + `on_error_rollback_assign_hook`. Tri-state:
|
|
253
|
+
// on, off, interactive. Non-matching values get the multi-line diagnostic
|
|
254
|
+
// `unrecognized value "<value>" for "ON_ERROR_ROLLBACK"\nAvailable values
|
|
255
|
+
// are: on, off, interactive.`. The substitute on addHook seeds "off" so
|
|
256
|
+
// `\echo :ON_ERROR_ROLLBACK` works without explicit init.
|
|
257
|
+
varStore.addHook('ON_ERROR_ROLLBACK', makeOnErrorRollbackHook((parsed) => {
|
|
258
|
+
settings.onErrorRollback = parsed;
|
|
259
|
+
}));
|
|
260
|
+
// VERBOSITY — upstream `verbosity_substitute_hook` (empty → "default")
|
|
261
|
+
// + `verbosity_assign_hook`. Accepts default | verbose | terse | sqlstate.
|
|
262
|
+
varStore.addHook('VERBOSITY', makeEnumHook('VERBOSITY', ['default', 'verbose', 'terse', 'sqlstate'], 'default', (parsed) => {
|
|
263
|
+
settings.verbosity = parsed;
|
|
264
|
+
}));
|
|
265
|
+
// SHOW_CONTEXT — upstream `show_context_substitute_hook` (empty → "errors")
|
|
266
|
+
// + `show_context_assign_hook`. Accepts never | errors | always.
|
|
267
|
+
varStore.addHook('SHOW_CONTEXT', makeEnumHook('SHOW_CONTEXT', ['never', 'errors', 'always'], 'errors', (parsed) => {
|
|
268
|
+
settings.showContext = parsed;
|
|
269
|
+
}));
|
|
270
|
+
// ECHO — upstream `echo_substitute_hook` (empty → "none") +
|
|
271
|
+
// `echo_assign_hook`. Accepts none | errors | queries | all.
|
|
272
|
+
varStore.addHook('ECHO', makeEnumHook('ECHO', ['none', 'errors', 'queries', 'all'], 'none', (parsed) => {
|
|
273
|
+
settings.echo = parsed;
|
|
274
|
+
}));
|
|
275
|
+
// ECHO_HIDDEN — upstream `bool_substitute_hook` +
|
|
276
|
+
// `echo_hidden_assign_hook`. Tri-state: on / off / noexec. Empty → "on".
|
|
277
|
+
varStore.addHook('ECHO_HIDDEN', makeEchoHiddenHook((parsed) => {
|
|
278
|
+
settings.echoHidden = parsed;
|
|
279
|
+
}));
|
|
280
|
+
// COMP_KEYWORD_CASE assign hook. Upstream `assign_var_comp_keyword_case_hook`
|
|
281
|
+
// (in startup.c) reflects the spelling into `pset.comp_case`; the completer
|
|
282
|
+
// then consults that on every Tab to decide whether to upper/lower/preserve
|
|
283
|
+
// candidate casing. Accepts the four canonical spellings; the upstream
|
|
284
|
+
// diagnostic wording is `unrecognized value "<value>" for
|
|
285
|
+
// "COMP_KEYWORD_CASE"\nAvailable values are: lower, upper, preserve-lower,
|
|
286
|
+
// preserve-upper.`.
|
|
287
|
+
varStore.addHook('COMP_KEYWORD_CASE', makeEnumHook('COMP_KEYWORD_CASE', ['lower', 'upper', 'preserve-lower', 'preserve-upper'], DEFAULT_COMP_CASE, (parsed) => {
|
|
288
|
+
settings.compCase = parsed;
|
|
289
|
+
}));
|
|
290
|
+
// HISTCONTROL — upstream `histcontrol_substitute_hook` (empty → "none") +
|
|
291
|
+
// `histcontrol_assign_hook`. Accepts ignorespace | ignoredups | ignoreboth
|
|
292
|
+
// | none.
|
|
293
|
+
varStore.addHook('HISTCONTROL', makeEnumHook('HISTCONTROL', ['none', 'ignorespace', 'ignoredups', 'ignoreboth'], 'none', (parsed) => {
|
|
294
|
+
settings.histControl = parsed;
|
|
295
|
+
}));
|
|
296
|
+
// SHOW_ALL_RESULTS — upstream `bool_substitute_hook` +
|
|
297
|
+
// `show_all_results_assign_hook`. Strict boolean; empty → "on".
|
|
298
|
+
varStore.addHook('SHOW_ALL_RESULTS', makeBoolHook('SHOW_ALL_RESULTS'));
|
|
299
|
+
// QUIET — upstream `bool_substitute_hook` + `quiet_hook`. Mirrors into
|
|
300
|
+
// `pset.quiet`. Default ("off") matches the unset substitute, so addHook
|
|
301
|
+
// registration seeds it via the substitute.
|
|
302
|
+
varStore.addHook('QUIET', makeBoolHook('QUIET', (parsed) => {
|
|
303
|
+
settings.quiet = parsed;
|
|
304
|
+
}));
|
|
305
|
+
// SINGLELINE — upstream `bool_substitute_hook` + `singleline_hook`.
|
|
306
|
+
// Mirrors into `pset.singleline`.
|
|
307
|
+
varStore.addHook('SINGLELINE', makeBoolHook('SINGLELINE', (parsed) => {
|
|
308
|
+
settings.singleline = parsed;
|
|
309
|
+
}));
|
|
310
|
+
// SINGLESTEP — upstream `bool_substitute_hook` + `singlestep_hook`.
|
|
311
|
+
// Mirrors into `pset.singlestep`.
|
|
312
|
+
varStore.addHook('SINGLESTEP', makeBoolHook('SINGLESTEP', (parsed) => {
|
|
313
|
+
settings.singlestep = parsed;
|
|
314
|
+
}));
|
|
315
|
+
// HIDE_TOAST_COMPRESSION — upstream `bool_substitute_hook` +
|
|
316
|
+
// `hide_compression_hook`. Mirrors into `pset.hide_compression` (our
|
|
317
|
+
// `settings.hideCompression`). Note: the upstream variable name is
|
|
318
|
+
// HIDE_TOAST_COMPRESSION, not HIDE_COMPRESSION.
|
|
319
|
+
varStore.addHook('HIDE_TOAST_COMPRESSION', makeBoolHook('HIDE_TOAST_COMPRESSION', (parsed) => {
|
|
320
|
+
settings.hideCompression = parsed;
|
|
321
|
+
}));
|
|
322
|
+
// HIDE_TABLEAM — upstream `bool_substitute_hook` + `hide_tableam_hook`.
|
|
323
|
+
// Mirrors into `pset.hide_tableam` (our `settings.hideTableam`).
|
|
324
|
+
varStore.addHook('HIDE_TABLEAM', makeBoolHook('HIDE_TABLEAM', (parsed) => {
|
|
325
|
+
settings.hideTableam = parsed;
|
|
326
|
+
}));
|
|
327
|
+
// HISTSIZE — upstream `histsize_substitute_hook` (null → "500") +
|
|
328
|
+
// `histsize_hook` which calls `ParseVariableNum`. Empty `\set HISTSIZE`
|
|
329
|
+
// reaches ParseVariableNum and fails with the "integer expected" message,
|
|
330
|
+
// preserving the prior value. We don't drive a derived settings field
|
|
331
|
+
// (readline isn't implemented), but we still install the hook so the
|
|
332
|
+
// variable surface matches vanilla: `\echo :HISTSIZE` prints "500" on
|
|
333
|
+
// startup, accepts integer values, rejects junk.
|
|
334
|
+
varStore.addHook('HISTSIZE', (newValue) => {
|
|
335
|
+
if (newValue === null) {
|
|
336
|
+
return { substitute: '500' };
|
|
337
|
+
}
|
|
338
|
+
const n = parseIntOrNull(newValue);
|
|
339
|
+
if (n === null) {
|
|
340
|
+
return `invalid value "${newValue}" for "HISTSIZE": integer expected`;
|
|
341
|
+
}
|
|
342
|
+
return true;
|
|
343
|
+
});
|
|
344
|
+
// IGNOREEOF — upstream `ignoreeof_substitute_hook` + `ignoreeof_hook`.
|
|
345
|
+
// The substitute hook is unusual: it silently rewrites non-numeric values
|
|
346
|
+
// to "10" (bash's documented default) instead of rejecting them, and maps
|
|
347
|
+
// null → "0" (feature disabled). The assign hook never fails in practice
|
|
348
|
+
// because the substitute already normalized the value — it's
|
|
349
|
+
// `ParseVariableNum(newval, "IGNOREEOF", &pset.ignoreeof)` over a value
|
|
350
|
+
// guaranteed to parse. We have no derived `settings.ignoreeof` field (the
|
|
351
|
+
// EOF-counting logic lives in the REPL, which isn't implemented), so the
|
|
352
|
+
// hook just maintains the variable surface: `\echo :IGNOREEOF` prints "0"
|
|
353
|
+
// on startup, accepts integers (incl. hex / octal — `ParseVariableNum`
|
|
354
|
+
// uses `strtol(..., 0)`), silently rewrites junk to "10", and on `\unset`
|
|
355
|
+
// resubstitutes "0". This is the key behavior the regress/psql `\gset
|
|
356
|
+
// IGNORE` test relies on (it expects `:IGNOREEOF` to render "0" even
|
|
357
|
+
// though `\gset` rejected the assignment as specially-treated).
|
|
358
|
+
varStore.addHook('IGNOREEOF', (newValue) => {
|
|
359
|
+
if (newValue === null) {
|
|
360
|
+
return { substitute: '0' };
|
|
361
|
+
}
|
|
362
|
+
if (parseStrtolBase0OrNull(newValue) === null) {
|
|
363
|
+
return { substitute: '10' };
|
|
364
|
+
}
|
|
365
|
+
return true;
|
|
366
|
+
});
|
|
367
|
+
return settings;
|
|
368
|
+
};
|
|
369
|
+
/**
|
|
370
|
+
* Build a strict-boolean hook with upstream's `bool_substitute_hook` +
|
|
371
|
+
* `bool_assign_hook` semantics:
|
|
372
|
+
*
|
|
373
|
+
* - empty / null → substitute "on"
|
|
374
|
+
* - on/off/true/false/yes/no/1/0 (case-insensitive) → accepted
|
|
375
|
+
* - anything else → reject with `unrecognized value "<value>" for
|
|
376
|
+
* "<name>": Boolean expected`
|
|
377
|
+
*
|
|
378
|
+
* The optional `apply` callback receives the parsed boolean so callers can
|
|
379
|
+
* keep a derived `PsqlSettings` field in sync (e.g. `settings.onErrorStop`).
|
|
380
|
+
*/
|
|
381
|
+
const makeBoolHook = (name, apply) => {
|
|
382
|
+
return (newValue) => {
|
|
383
|
+
if (newValue === null) {
|
|
384
|
+
// Upstream `bool_substitute_hook`: on `\unset NAME`, re-store "off"
|
|
385
|
+
// so `\echo :NAME` shows the boolean default instead of the literal
|
|
386
|
+
// `:NAME` token. Verified empirically against vanilla psql 18:
|
|
387
|
+
// `\unset AUTOCOMMIT; \echo :AUTOCOMMIT` prints "off".
|
|
388
|
+
apply?.(false);
|
|
389
|
+
return { substitute: 'off' };
|
|
390
|
+
}
|
|
391
|
+
if (newValue === '') {
|
|
392
|
+
// `\set NAME` (empty value) substitutes "on". Verified empirically:
|
|
393
|
+
// `\set AUTOCOMMIT; \echo :AUTOCOMMIT` prints "on".
|
|
394
|
+
apply?.(true);
|
|
395
|
+
return { substitute: 'on' };
|
|
396
|
+
}
|
|
397
|
+
const parsed = parseOnOffBool(newValue);
|
|
398
|
+
if (parsed === null) {
|
|
399
|
+
return `unrecognized value "${newValue}" for "${name}": Boolean expected`;
|
|
400
|
+
}
|
|
401
|
+
apply?.(parsed);
|
|
402
|
+
return true;
|
|
403
|
+
};
|
|
404
|
+
};
|
|
405
|
+
/**
|
|
406
|
+
* Build an enum-style hook with upstream's substitute / assign pair (the
|
|
407
|
+
* VERBOSITY / SHOW_CONTEXT / ECHO / COMP_KEYWORD_CASE / HISTCONTROL pattern):
|
|
408
|
+
*
|
|
409
|
+
* - null → substitute the supplied `defaultValue` (matches
|
|
410
|
+
* `verbosity_substitute_hook` etc. — `\unset NAME` re-stores the default)
|
|
411
|
+
* - exact-match (case-insensitive) against `allowed` → accepted
|
|
412
|
+
* - empty / unrecognized → reject with `unrecognized value "<value>" for
|
|
413
|
+
* "<name>"\nAvailable values are: a, b, c.` (the substitute hooks only
|
|
414
|
+
* handle NULL upstream — empty falls through to the assign hook's
|
|
415
|
+
* PsqlVarEnumError path, preserving the prior value)
|
|
416
|
+
*
|
|
417
|
+
* `apply` is invoked with the canonical lowercase spelling whenever the
|
|
418
|
+
* variable is set (including the default-on-null path).
|
|
419
|
+
*/
|
|
420
|
+
const makeEnumHook = (name, allowed, defaultValue, apply) => {
|
|
421
|
+
const list = allowed.join(', ');
|
|
422
|
+
return (newValue) => {
|
|
423
|
+
if (newValue === null) {
|
|
424
|
+
apply?.(defaultValue);
|
|
425
|
+
return { substitute: defaultValue };
|
|
426
|
+
}
|
|
427
|
+
const lower = newValue.toLowerCase();
|
|
428
|
+
const match = allowed.find((a) => a.toLowerCase() === lower);
|
|
429
|
+
if (match === undefined) {
|
|
430
|
+
return `unrecognized value "${newValue}" for "${name}"\nAvailable values are: ${list}.`;
|
|
431
|
+
}
|
|
432
|
+
apply?.(match);
|
|
433
|
+
return true;
|
|
434
|
+
};
|
|
435
|
+
};
|
|
436
|
+
/**
|
|
437
|
+
* `ON_ERROR_ROLLBACK` is upstream's only tri-state boolean — `on` / `off` /
|
|
438
|
+
* `interactive`. Empty / null → substitute "on" (matches
|
|
439
|
+
* `on_error_rollback_substitute_hook`). Other values get the multi-line
|
|
440
|
+
* diagnostic upstream emits from `on_error_rollback_assign_hook`.
|
|
441
|
+
*/
|
|
442
|
+
const makeOnErrorRollbackHook = (apply) => {
|
|
443
|
+
return (newValue) => {
|
|
444
|
+
if (newValue === null) {
|
|
445
|
+
// Upstream `on_error_rollback_substitute_hook`:
|
|
446
|
+
// if (newval == NULL) newval = pg_strdup("off");
|
|
447
|
+
// — `\unset ON_ERROR_ROLLBACK` re-stores "off" so a follow-on
|
|
448
|
+
// `\echo :ON_ERROR_ROLLBACK` shows the default rather than the
|
|
449
|
+
// literal `:NAME` token.
|
|
450
|
+
apply('off');
|
|
451
|
+
return { substitute: 'off' };
|
|
452
|
+
}
|
|
453
|
+
if (newValue === '') {
|
|
454
|
+
apply('on');
|
|
455
|
+
return { substitute: 'on' };
|
|
456
|
+
}
|
|
457
|
+
const lower = newValue.toLowerCase();
|
|
458
|
+
if (lower === 'on' || lower === 'off' || lower === 'interactive') {
|
|
459
|
+
apply(lower);
|
|
460
|
+
return true;
|
|
461
|
+
}
|
|
462
|
+
return `unrecognized value "${newValue}" for "ON_ERROR_ROLLBACK"\nAvailable values are: on, off, interactive.`;
|
|
463
|
+
};
|
|
464
|
+
};
|
|
465
|
+
/**
|
|
466
|
+
* ECHO_HIDDEN tri-state: `on` / `off` / `noexec`. Upstream uses
|
|
467
|
+
* `bool_substitute_hook` (null → "off", empty → "on") plus
|
|
468
|
+
* `echo_hidden_hook` which accepts `noexec` or falls back to
|
|
469
|
+
* `ParseVariableBool`. Anything else gets the upstream "unrecognized
|
|
470
|
+
* value" line with the three-element list.
|
|
471
|
+
*/
|
|
472
|
+
const makeEchoHiddenHook = (apply) => {
|
|
473
|
+
return (newValue) => {
|
|
474
|
+
if (newValue === null) {
|
|
475
|
+
// Match `bool_substitute_hook(NULL)` → "off". On addHook registration
|
|
476
|
+
// this seeds `\echo :ECHO_HIDDEN` → "off" (parity with vanilla).
|
|
477
|
+
apply('off');
|
|
478
|
+
return { substitute: 'off' };
|
|
479
|
+
}
|
|
480
|
+
if (newValue === '') {
|
|
481
|
+
apply('on');
|
|
482
|
+
return { substitute: 'on' };
|
|
483
|
+
}
|
|
484
|
+
const lower = newValue.toLowerCase();
|
|
485
|
+
if (lower === 'noexec') {
|
|
486
|
+
apply('noexec');
|
|
487
|
+
return true;
|
|
488
|
+
}
|
|
489
|
+
const parsed = parseOnOffBool(newValue);
|
|
490
|
+
if (parsed === null) {
|
|
491
|
+
return `unrecognized value "${newValue}" for "ECHO_HIDDEN"\nAvailable values are: on, off, noexec.`;
|
|
492
|
+
}
|
|
493
|
+
apply(parsed ? 'on' : 'off');
|
|
494
|
+
return true;
|
|
495
|
+
};
|
|
496
|
+
};
|
|
497
|
+
/**
|
|
498
|
+
* Parse a non-empty string as a base-10 signed integer. Returns `null` on
|
|
499
|
+
* any junk. Used by FETCH_COUNT / HISTSIZE — both invoke
|
|
500
|
+
* `ParseVariableNum` upstream which internally uses `strtol(..., 0)`
|
|
501
|
+
* (accepting hex/octal/decimal); we tighten to decimal-only here. The
|
|
502
|
+
* deviation is observable for `\set FETCH_COUNT 0xff` (upstream accepts,
|
|
503
|
+
* we reject); intentional for now because the regress doesn't exercise it.
|
|
504
|
+
*/
|
|
505
|
+
const parseIntOrNull = (raw) => {
|
|
506
|
+
const trimmed = raw.trim();
|
|
507
|
+
if (trimmed.length === 0)
|
|
508
|
+
return null;
|
|
509
|
+
if (!/^[+-]?\d+$/.test(trimmed))
|
|
510
|
+
return null;
|
|
511
|
+
const n = parseInt(trimmed, 10);
|
|
512
|
+
if (!Number.isFinite(n))
|
|
513
|
+
return null;
|
|
514
|
+
if (n < -0x80000000 || n > 0x7fffffff)
|
|
515
|
+
return null;
|
|
516
|
+
return n;
|
|
517
|
+
};
|
|
518
|
+
/**
|
|
519
|
+
* Parse a non-empty string as a strtol-base-0 signed integer (decimal,
|
|
520
|
+
* `0x`/`0X` hex, leading-`0` octal). Returns `null` on any junk. Mirrors
|
|
521
|
+
* upstream `ParseVariableNum`'s `strtol(value, &end, 0)` syntax — used by
|
|
522
|
+
* IGNOREEOF's substitute hook, where the original string is preserved on
|
|
523
|
+
* success (the hook only checks parsability to decide whether to swap in
|
|
524
|
+
* "10").
|
|
525
|
+
*/
|
|
526
|
+
const parseStrtolBase0OrNull = (raw) => {
|
|
527
|
+
// strtol skips leading whitespace; mirror that.
|
|
528
|
+
const trimmed = raw.replace(/^\s+/, '');
|
|
529
|
+
if (trimmed.length === 0)
|
|
530
|
+
return null;
|
|
531
|
+
let body = trimmed;
|
|
532
|
+
let sign = 1;
|
|
533
|
+
if (body.startsWith('+')) {
|
|
534
|
+
body = body.slice(1);
|
|
535
|
+
}
|
|
536
|
+
else if (body.startsWith('-')) {
|
|
537
|
+
sign = -1;
|
|
538
|
+
body = body.slice(1);
|
|
539
|
+
}
|
|
540
|
+
if (body.length === 0)
|
|
541
|
+
return null;
|
|
542
|
+
let radix = 10;
|
|
543
|
+
if (body.startsWith('0x') || body.startsWith('0X')) {
|
|
544
|
+
radix = 16;
|
|
545
|
+
body = body.slice(2);
|
|
546
|
+
}
|
|
547
|
+
else if (body.length > 1 && body.startsWith('0')) {
|
|
548
|
+
// Leading zero on a multi-char body → octal (matches strtol base 0).
|
|
549
|
+
radix = 8;
|
|
550
|
+
body = body.slice(1);
|
|
551
|
+
}
|
|
552
|
+
if (body.length === 0)
|
|
553
|
+
return null;
|
|
554
|
+
// Reject trailing junk — strtol returns success only when the entire
|
|
555
|
+
// body is consumed (`*end == '\0'`), and ParseVariableNum requires that.
|
|
556
|
+
const digitRe = radix === 16 ? /^[0-9a-fA-F]+$/ : radix === 8 ? /^[0-7]+$/ : /^[0-9]+$/;
|
|
557
|
+
if (!digitRe.test(body))
|
|
558
|
+
return null;
|
|
559
|
+
const parsed = sign * parseInt(body, radix);
|
|
560
|
+
if (!Number.isFinite(parsed))
|
|
561
|
+
return null;
|
|
562
|
+
// 32-bit signed range (ParseVariableNum's `numval == (int) numval` check).
|
|
563
|
+
if (parsed < -0x80000000 || parsed > 0x7fffffff)
|
|
564
|
+
return null;
|
|
565
|
+
return parsed;
|
|
566
|
+
};
|
|
567
|
+
/**
|
|
568
|
+
* Parse the `on`/`off`/`true`/`false`/`yes`/`no`/`1`/`0` boolean spelling
|
|
569
|
+
* upstream uses for psql variables. Mirrors `ParseVariableBool` (without the
|
|
570
|
+
* error reporting — callers handle that). Returns null on unrecognised input.
|
|
571
|
+
*/
|
|
572
|
+
const parseOnOffBool = (raw) => {
|
|
573
|
+
const v = raw.toLowerCase().trim();
|
|
574
|
+
if (v === '' || v === 'on' || v === 'true' || v === 'yes' || v === '1') {
|
|
575
|
+
return true;
|
|
576
|
+
}
|
|
577
|
+
if (v === 'off' || v === 'false' || v === 'no' || v === '0') {
|
|
578
|
+
return false;
|
|
579
|
+
}
|
|
580
|
+
return null;
|
|
581
|
+
};
|
|
582
|
+
/**
|
|
583
|
+
* Strict validation for non-empty `WATCH_INTERVAL` values. Mirrors
|
|
584
|
+
* upstream's `ParseVariableDouble(name, ..., 0, DEFAULT_WATCH_INTERVAL_MAX)`
|
|
585
|
+
* three distinct error paths:
|
|
586
|
+
*
|
|
587
|
+
* - junk (strtod doesn't consume it) → `invalid value "<value>" for
|
|
588
|
+
* variable "WATCH_INTERVAL"`
|
|
589
|
+
* - parses but value < 0 → `must be greater than 0.00`
|
|
590
|
+
* - parses but value > 1000000 → `must be less than 1000000.00`
|
|
591
|
+
*
|
|
592
|
+
* Returns `null` if the value is valid; otherwise returns the upstream
|
|
593
|
+
* error string (sent through `trySet`'s hook-veto channel so the prior
|
|
594
|
+
* value is preserved).
|
|
595
|
+
*/
|
|
596
|
+
const validateWatchInterval = (raw) => {
|
|
597
|
+
// strtod-style parse: leading whitespace + sign + (digits[.digits]|.digits)
|
|
598
|
+
// [(eE)±digits]. We're stricter than strtod (no hex/inf), matching the
|
|
599
|
+
// ParseVariableDouble syntax check.
|
|
600
|
+
if (!/^[+-]?(\d+(\.\d*)?|\.\d+)([eE][+-]?\d+)?$/.test(raw)) {
|
|
601
|
+
return `invalid value "${raw}" for variable "WATCH_INTERVAL"`;
|
|
602
|
+
}
|
|
603
|
+
const value = parseFloat(raw);
|
|
604
|
+
// Upstream `ParseVariableDouble` consults `errno = ERANGE` after
|
|
605
|
+
// `strtod` to distinguish overflow from generic junk: the overflow
|
|
606
|
+
// case emits `is out of range` rather than the generic `invalid
|
|
607
|
+
// value`. JS `parseFloat` returns Infinity on overflow — use that as
|
|
608
|
+
// the ERANGE proxy.
|
|
609
|
+
if (Number.isNaN(value)) {
|
|
610
|
+
return `invalid value "${raw}" for variable "WATCH_INTERVAL"`;
|
|
611
|
+
}
|
|
612
|
+
if (!Number.isFinite(value)) {
|
|
613
|
+
return `invalid value "${raw}" for variable "WATCH_INTERVAL": is out of range`;
|
|
614
|
+
}
|
|
615
|
+
if (value < 0) {
|
|
616
|
+
return `invalid value "${raw}" for variable "WATCH_INTERVAL": must be greater than 0.00`;
|
|
617
|
+
}
|
|
618
|
+
if (value > WATCH_INTERVAL_MAX) {
|
|
619
|
+
return `invalid value "${raw}" for variable "WATCH_INTERVAL": must be less than ${WATCH_INTERVAL_MAX.toFixed(2)}`;
|
|
620
|
+
}
|
|
621
|
+
return null;
|
|
622
|
+
};
|
|
623
|
+
/**
|
|
624
|
+
* Bridge environment variables to psql state. Read-only with respect to the
|
|
625
|
+
* environment; mutates `settings` (and its var store) in place.
|
|
626
|
+
*
|
|
627
|
+
* The variable names below match upstream where it reads them lazily:
|
|
628
|
+
*
|
|
629
|
+
* - PAGER, PSQL_PAGER, PSQL_WATCH_PAGER — consulted by `fe_utils/print.c`
|
|
630
|
+
* (`PageOutput`) and `\watch`. Captured as psql vars so later WPs read
|
|
631
|
+
* a single source of truth.
|
|
632
|
+
* - EDITOR, VISUAL, PSQL_EDITOR, PSQL_EDITOR_LINENUMBER_ARG — consulted
|
|
633
|
+
* by `\e`/`\ef`/`\ev` in `command.c::editFile`.
|
|
634
|
+
* - PSQL_HISTORY — `src/bin/psql/input.c::initializeInput` uses it as the
|
|
635
|
+
* history file path when readline is active. Stored as HISTFILE for
|
|
636
|
+
* parity with psql's own `\set HISTFILE` convention.
|
|
637
|
+
* - PSQL_HISTSIZE / PSQL_HISTCONTROL — likewise from `input.c`, stored
|
|
638
|
+
* under the corresponding psql var names HISTSIZE / HISTCONTROL.
|
|
639
|
+
* - PSQLRC — origin of the rc-file path (`startup.c::process_psqlrc`).
|
|
640
|
+
* - COLUMNS — terminal width hint (`startup.c`: `pset.popt.topt.env_columns`).
|
|
641
|
+
* - NO_COLOR — `https://no-color.org/` convention; consulted by the
|
|
642
|
+
* printer/color code in upstream `fe_utils/print.c`. Captured here so
|
|
643
|
+
* later WPs can opt out of ANSI coloring without re-reading `process.env`.
|
|
644
|
+
*/
|
|
645
|
+
export const applyEnvOverrides = (settings, env = process.env) => {
|
|
646
|
+
const bridge = (envName, varName) => {
|
|
647
|
+
const value = env[envName];
|
|
648
|
+
if (value !== undefined && value !== '') {
|
|
649
|
+
settings.vars.set(varName, value);
|
|
650
|
+
}
|
|
651
|
+
};
|
|
652
|
+
// Pager group: PSQL_PAGER takes precedence over PAGER in psql, so set
|
|
653
|
+
// PAGER first and let PSQL_PAGER overwrite it.
|
|
654
|
+
bridge('PAGER', 'PAGER');
|
|
655
|
+
bridge('PSQL_PAGER', 'PAGER');
|
|
656
|
+
bridge('PSQL_WATCH_PAGER', 'PSQL_WATCH_PAGER');
|
|
657
|
+
// Editor group: PSQL_EDITOR > VISUAL > EDITOR in psql. Set in
|
|
658
|
+
// lowest-to-highest precedence so the final wins.
|
|
659
|
+
bridge('EDITOR', 'EDITOR');
|
|
660
|
+
bridge('VISUAL', 'EDITOR');
|
|
661
|
+
bridge('PSQL_EDITOR', 'EDITOR');
|
|
662
|
+
bridge('PSQL_EDITOR_LINENUMBER_ARG', 'EDITOR_LINENUMBER_ARG');
|
|
663
|
+
// History group.
|
|
664
|
+
bridge('PSQL_HISTORY', 'HISTFILE');
|
|
665
|
+
bridge('PSQL_HISTSIZE', 'HISTSIZE');
|
|
666
|
+
bridge('PSQL_HISTCONTROL', 'HISTCONTROL');
|
|
667
|
+
// RC file path.
|
|
668
|
+
bridge('PSQLRC', 'PSQLRC');
|
|
669
|
+
// NO_COLOR — captured under a psql-style var name. The convention is
|
|
670
|
+
// "any non-empty value disables color". We store the raw value so the
|
|
671
|
+
// printer can decide via VarStore.asBool / its own check.
|
|
672
|
+
if (env.NO_COLOR !== undefined && env.NO_COLOR !== '') {
|
|
673
|
+
settings.vars.set('NO_COLOR', env.NO_COLOR);
|
|
674
|
+
}
|
|
675
|
+
// COLUMNS — feed into popt.topt.envColumns (upstream:
|
|
676
|
+
// pset.popt.topt.env_columns = getenv("COLUMNS") ? atoi(...) : 0).
|
|
677
|
+
const cols = env.COLUMNS;
|
|
678
|
+
if (cols !== undefined && cols !== '') {
|
|
679
|
+
const parsed = parseInt(cols, 10);
|
|
680
|
+
if (Number.isFinite(parsed) && parsed >= 0) {
|
|
681
|
+
settings.popt.topt.envColumns = parsed;
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
};
|