Skip to content

Commit eaf746a

Browse files
committed
Make psql's "\pset format" command reject non-unique abbreviations.
The previous behavior of preferring the oldest match had the advantage of not breaking existing scripts when we add a conflicting format name; but that behavior was undocumented and fragile (it seems just luck that commit add9182 didn't break it). Let's go over to the less mistake- prone approach of complaining when there are multiple matches. Since this is a small compatibility break, no back-patch. Daniel Vérité Discussion: https://postgr.es/m/cb7e1caf-3ea6-450d-af28-f524903a030c@manitou-mail.org
1 parent 51eaaaf commit eaf746a

File tree

1 file changed

+41
-18
lines changed

1 file changed

+41
-18
lines changed

src/bin/psql/command.c

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3637,28 +3637,51 @@ do_pset(const char *param, const char *value, printQueryOpt *popt, bool quiet)
36373637
/* set format */
36383638
if (strcmp(param, "format") == 0)
36393639
{
3640+
static const struct fmt
3641+
{
3642+
const char *name;
3643+
enum printFormat number;
3644+
} formats[] =
3645+
{
3646+
/* remember to update error message below when adding more */
3647+
{"aligned", PRINT_ALIGNED},
3648+
{"asciidoc", PRINT_ASCIIDOC},
3649+
{"html", PRINT_HTML},
3650+
{"latex", PRINT_LATEX},
3651+
{"latex-longtable", PRINT_LATEX_LONGTABLE},
3652+
{"troff-ms", PRINT_TROFF_MS},
3653+
{"unaligned", PRINT_UNALIGNED},
3654+
{"wrapped", PRINT_WRAPPED}
3655+
};
3656+
36403657
if (!value)
36413658
;
3642-
else if (pg_strncasecmp("aligned", value, vallen) == 0)
3643-
popt->topt.format = PRINT_ALIGNED;
3644-
else if (pg_strncasecmp("asciidoc", value, vallen) == 0)
3645-
popt->topt.format = PRINT_ASCIIDOC;
3646-
else if (pg_strncasecmp("html", value, vallen) == 0)
3647-
popt->topt.format = PRINT_HTML;
3648-
else if (pg_strncasecmp("latex", value, vallen) == 0)
3649-
popt->topt.format = PRINT_LATEX;
3650-
else if (pg_strncasecmp("latex-longtable", value, vallen) == 0)
3651-
popt->topt.format = PRINT_LATEX_LONGTABLE;
3652-
else if (pg_strncasecmp("troff-ms", value, vallen) == 0)
3653-
popt->topt.format = PRINT_TROFF_MS;
3654-
else if (pg_strncasecmp("unaligned", value, vallen) == 0)
3655-
popt->topt.format = PRINT_UNALIGNED;
3656-
else if (pg_strncasecmp("wrapped", value, vallen) == 0)
3657-
popt->topt.format = PRINT_WRAPPED;
36583659
else
36593660
{
3660-
psql_error("\\pset: allowed formats are aligned, asciidoc, html, latex, latex-longtable, troff-ms, unaligned, wrapped\n");
3661-
return false;
3661+
int match_pos = -1;
3662+
3663+
for (int i = 0; i < lengthof(formats); i++)
3664+
{
3665+
if (pg_strncasecmp(formats[i].name, value, vallen) == 0)
3666+
{
3667+
if (match_pos < 0)
3668+
match_pos = i;
3669+
else
3670+
{
3671+
psql_error("\\pset: ambiguous abbreviation \"%s\" matches both \"%s\" and \"%s\"\n",
3672+
value,
3673+
formats[match_pos].name, formats[i].name);
3674+
return false;
3675+
}
3676+
}
3677+
}
3678+
if (match_pos < 0)
3679+
{
3680+
psql_error("\\pset: allowed formats are aligned, asciidoc, html, latex, latex-longtable, troff-ms, unaligned, wrapped\n");
3681+
return false;
3682+
}
3683+
else
3684+
popt->topt.format = formats[match_pos].number;
36623685
}
36633686
}
36643687

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy