Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit2855179

Browse files
committed
Improve consistency of parsing of psql's magic variables.
For simple boolean variables such as ON_ERROR_STOP, psql has for a longtime recognized variant spellings of "on" and "off" (such as "1"/"0"),and it also made a point of warning you if you'd misspelled the setting.But these conveniences did not exist for other keyword-valued variables.In particular, though ECHO_HIDDEN and ON_ERROR_ROLLBACK include "on" and"off" as possible values, none of the alternative spellings for those wererecognized; and to make matters worse the code would just silently assume"on" was meant for any unrecognized spelling. Several people have reportedgetting bitten by this, so let's fix it. In detail, this patch:* Allows all spellings recognized by ParseVariableBool() for ECHO_HIDDENand ON_ERROR_ROLLBACK.* Reports a warning for unrecognized values for COMP_KEYWORD_CASE, ECHO,ECHO_HIDDEN, HISTCONTROL, ON_ERROR_ROLLBACK, and VERBOSITY.* Recognizes all values for all these variables case-insensitively;previously there was a mishmash of case-sensitive and case-insensitivebehaviors.Back-patch to all supported branches. There is a small risk of breakingexisting scripts that were accidentally failing to malfunction; but theconsensus is that the chance of detecting real problems and preventingfuture mistakes outweighs this.
1 parentba66c9d commit2855179

File tree

7 files changed

+124
-76
lines changed

7 files changed

+124
-76
lines changed

‎doc/src/sgml/ref/psql-ref.sgml

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,7 @@ EOF
173173
Echo the actual queries generated by <command>\d</command> and other backslash
174174
commands. You can use this to study <application>psql</application>'s
175175
internal operations. This is equivalent to
176-
setting the variable <varname>ECHO_HIDDEN</varname> from within
177-
<application>psql</application>.
176+
setting the variable <varname>ECHO_HIDDEN</varname> to <literal>on</>.
178177
</para>
179178
</listitem>
180179
</varlistentry>
@@ -333,8 +332,8 @@ EOF
333332
quietly. By default, it prints welcome messages and various
334333
informational output. If this option is used, none of this
335334
happens. This is useful with the <option>-c</option> option.
336-
Within <application>psql</application> you can also setthe
337-
<varname>QUIET</varname> variable to achieve the same effect.
335+
This is equivalent to settingthe variable <varname>QUIET</varname>
336+
to <literal>on</>.
338337
</para>
339338
</listitem>
340339
</varlistentry>
@@ -2884,8 +2883,9 @@ bar
28842883
<term><varname>ECHO_HIDDEN</varname></term>
28852884
<listitem>
28862885
<para>
2887-
When this variable is set and a backslash command queries the
2888-
database, the query is first shown. This way you can study the
2886+
When this variable is set to <literal>on</> and a backslash command
2887+
queries the database, the query is first shown.
2888+
This feature helps you to study
28892889
<productname>PostgreSQL</productname> internals and provide
28902890
similar functionality in your own programs. (To select this behavior
28912891
on program start-up, use the switch <option>-E</option>.) If you set
@@ -3046,16 +3046,16 @@ bar
30463046
</term>
30473047
<listitem>
30483048
<para>
3049-
When <literal>on</>, if a statement in a transaction block
3049+
Whenset to<literal>on</>, if a statement in a transaction block
30503050
generates an error, the error is ignored and the transaction
3051-
continues. When <literal>interactive</>, such errors are only
3051+
continues. Whenset to<literal>interactive</>, such errors are only
30523052
ignored in interactive sessions, and not when reading script
3053-
files. When <literal>off</> (the default), a statement in a
3053+
files. Whenunset or set to<literal>off</>, a statement in a
30543054
transaction block that generates an error aborts the entire
3055-
transaction. Theon_error_rollback-on mode works by issuing an
3055+
transaction. Theerror rollback mode works by issuing an
30563056
implicit <command>SAVEPOINT</> for you, just before each command
3057-
that is in a transaction block, androllsback to the savepoint
3058-
on error.
3057+
that is in a transaction block, andthen rollingback to the
3058+
savepoint if the command fails.
30593059
</para>
30603060
</listitem>
30613061
</varlistentry>
@@ -3065,7 +3065,8 @@ bar
30653065
<listitem>
30663066
<para>
30673067
By default, command processing continues after an error. When this
3068-
variable is set, it will instead stop immediately. In interactive mode,
3068+
variable is set to <literal>on</>, processing will instead stop
3069+
immediately. In interactive mode,
30693070
<application>psql</application> will return to the command prompt;
30703071
otherwise, <application>psql</application> will exit, returning
30713072
error code 3 to distinguish this case from fatal error
@@ -3107,8 +3108,8 @@ bar
31073108
<term><varname>QUIET</varname></term>
31083109
<listitem>
31093110
<para>
3110-
Thisvariable is equivalent to the command line option
3111-
<option>-q</option>. It is probably not too useful in
3111+
Setting thisvariableto <literal>on</>is equivalent to the command
3112+
line option<option>-q</option>. It is probably not too useful in
31123113
interactive mode.
31133114
</para>
31143115
</listitem>
@@ -3118,8 +3119,8 @@ bar
31183119
<term><varname>SINGLELINE</varname></term>
31193120
<listitem>
31203121
<para>
3121-
Thisvariable is equivalent to the command line option
3122-
<option>-S</option>.
3122+
Setting thisvariableto <literal>on</>is equivalent to the command
3123+
line option<option>-S</option>.
31233124
</para>
31243125
</listitem>
31253126
</varlistentry>
@@ -3128,8 +3129,8 @@ bar
31283129
<term><varname>SINGLESTEP</varname></term>
31293130
<listitem>
31303131
<para>
3131-
Thisvariable is equivalent to the command line option
3132-
<option>-s</option>.
3132+
Setting thisvariableto <literal>on</>is equivalent to the command
3133+
line option<option>-s</option>.
31333134
</para>
31343135
</listitem>
31353136
</varlistentry>

‎src/bin/psql/command.c

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1362,7 +1362,7 @@ exec_command(const char *cmd,
13621362
OT_NORMAL,NULL, false);
13631363

13641364
if (opt)
1365-
pset.timing=ParseVariableBool(opt);
1365+
pset.timing=ParseVariableBool(opt,"\\timing");
13661366
else
13671367
pset.timing= !pset.timing;
13681368
if (!pset.quiet)
@@ -2412,12 +2412,14 @@ do_pset(const char *param, const char *value, printQueryOpt *popt, bool quiet)
24122412
}
24132413

24142414
/* set expanded/vertical mode */
2415-
elseif (strcmp(param,"x")==0||strcmp(param,"expanded")==0||strcmp(param,"vertical")==0)
2415+
elseif (strcmp(param,"x")==0||
2416+
strcmp(param,"expanded")==0||
2417+
strcmp(param,"vertical")==0)
24162418
{
24172419
if (value&&pg_strcasecmp(value,"auto")==0)
24182420
popt->topt.expanded=2;
24192421
elseif (value)
2420-
popt->topt.expanded=ParseVariableBool(value);
2422+
popt->topt.expanded=ParseVariableBool(value,param);
24212423
else
24222424
popt->topt.expanded= !popt->topt.expanded;
24232425
}
@@ -2426,7 +2428,7 @@ do_pset(const char *param, const char *value, printQueryOpt *popt, bool quiet)
24262428
elseif (strcmp(param,"numericlocale")==0)
24272429
{
24282430
if (value)
2429-
popt->topt.numericLocale=ParseVariableBool(value);
2431+
popt->topt.numericLocale=ParseVariableBool(value,param);
24302432
else
24312433
popt->topt.numericLocale= !popt->topt.numericLocale;
24322434
}
@@ -2481,7 +2483,7 @@ do_pset(const char *param, const char *value, printQueryOpt *popt, bool quiet)
24812483
elseif (strcmp(param,"t")==0||strcmp(param,"tuples_only")==0)
24822484
{
24832485
if (value)
2484-
popt->topt.tuples_only=ParseVariableBool(value);
2486+
popt->topt.tuples_only=ParseVariableBool(value,param);
24852487
else
24862488
popt->topt.tuples_only= !popt->topt.tuples_only;
24872489
}
@@ -2512,10 +2514,12 @@ do_pset(const char *param, const char *value, printQueryOpt *popt, bool quiet)
25122514
if (value&&pg_strcasecmp(value,"always")==0)
25132515
popt->topt.pager=2;
25142516
elseif (value)
2515-
if (ParseVariableBool(value))
2517+
{
2518+
if (ParseVariableBool(value,param))
25162519
popt->topt.pager=1;
25172520
else
25182521
popt->topt.pager=0;
2522+
}
25192523
elseif (popt->topt.pager==1)
25202524
popt->topt.pager=0;
25212525
else
@@ -2526,7 +2530,7 @@ do_pset(const char *param, const char *value, printQueryOpt *popt, bool quiet)
25262530
elseif (strcmp(param,"footer")==0)
25272531
{
25282532
if (value)
2529-
popt->topt.default_footer=ParseVariableBool(value);
2533+
popt->topt.default_footer=ParseVariableBool(value,param);
25302534
else
25312535
popt->topt.default_footer= !popt->topt.default_footer;
25322536
}

‎src/bin/psql/settings.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@
2727
#defineDEFAULT_PROMPT2 "%/%R%# "
2828
#defineDEFAULT_PROMPT3 ">> "
2929

30+
/*
31+
* Note: these enums should generally be chosen so that zero corresponds
32+
* to the default behavior.
33+
*/
34+
3035
typedefenum
3136
{
3237
PSQL_ECHO_NONE,
@@ -49,6 +54,14 @@ typedef enum
4954
PSQL_ERROR_ROLLBACK_ON
5055
}PSQL_ERROR_ROLLBACK;
5156

57+
typedefenum
58+
{
59+
PSQL_COMP_CASE_PRESERVE_UPPER,
60+
PSQL_COMP_CASE_PRESERVE_LOWER,
61+
PSQL_COMP_CASE_UPPER,
62+
PSQL_COMP_CASE_LOWER
63+
}PSQL_COMP_CASE;
64+
5265
typedefenum
5366
{
5467
hctl_none=0,
@@ -110,6 +123,7 @@ typedef struct _psqlSettings
110123
PSQL_ECHOecho;
111124
PSQL_ECHO_HIDDENecho_hidden;
112125
PSQL_ERROR_ROLLBACKon_error_rollback;
126+
PSQL_COMP_CASEcomp_case;
113127
HistControlhistcontrol;
114128
constchar*prompt1;
115129
constchar*prompt2;

‎src/bin/psql/startup.c

Lines changed: 58 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -705,31 +705,31 @@ showVersion(void)
705705
staticvoid
706706
autocommit_hook(constchar*newval)
707707
{
708-
pset.autocommit=ParseVariableBool(newval);
708+
pset.autocommit=ParseVariableBool(newval,"AUTOCOMMIT");
709709
}
710710

711711
staticvoid
712712
on_error_stop_hook(constchar*newval)
713713
{
714-
pset.on_error_stop=ParseVariableBool(newval);
714+
pset.on_error_stop=ParseVariableBool(newval,"ON_ERROR_STOP");
715715
}
716716

717717
staticvoid
718718
quiet_hook(constchar*newval)
719719
{
720-
pset.quiet=ParseVariableBool(newval);
720+
pset.quiet=ParseVariableBool(newval,"QUIET");
721721
}
722722

723723
staticvoid
724724
singleline_hook(constchar*newval)
725725
{
726-
pset.singleline=ParseVariableBool(newval);
726+
pset.singleline=ParseVariableBool(newval,"SINGLELINE");
727727
}
728728

729729
staticvoid
730730
singlestep_hook(constchar*newval)
731731
{
732-
pset.singlestep=ParseVariableBool(newval);
732+
pset.singlestep=ParseVariableBool(newval,"SINGLESTEP");
733733
}
734734

735735
staticvoid
@@ -743,27 +743,33 @@ echo_hook(const char *newval)
743743
{
744744
if (newval==NULL)
745745
pset.echo=PSQL_ECHO_NONE;
746-
elseif (strcmp(newval,"queries")==0)
746+
elseif (pg_strcasecmp(newval,"queries")==0)
747747
pset.echo=PSQL_ECHO_QUERIES;
748-
elseif (strcmp(newval,"errors")==0)
748+
elseif (pg_strcasecmp(newval,"errors")==0)
749749
pset.echo=PSQL_ECHO_ERRORS;
750-
elseif (strcmp(newval,"all")==0)
750+
elseif (pg_strcasecmp(newval,"all")==0)
751751
pset.echo=PSQL_ECHO_ALL;
752+
elseif (pg_strcasecmp(newval,"none")==0)
753+
pset.echo=PSQL_ECHO_NONE;
752754
else
755+
{
756+
psql_error("unrecognized value \"%s\" for \"%s\"; assuming \"%s\"\n",
757+
newval,"ECHO","none");
753758
pset.echo=PSQL_ECHO_NONE;
759+
}
754760
}
755761

756762
staticvoid
757763
echo_hidden_hook(constchar*newval)
758764
{
759765
if (newval==NULL)
760766
pset.echo_hidden=PSQL_ECHO_HIDDEN_OFF;
761-
elseif (strcmp(newval,"noexec")==0)
767+
elseif (pg_strcasecmp(newval,"noexec")==0)
762768
pset.echo_hidden=PSQL_ECHO_HIDDEN_NOEXEC;
763-
elseif (pg_strcasecmp(newval,"off")==0)
764-
pset.echo_hidden=PSQL_ECHO_HIDDEN_OFF;
765-
else
769+
elseif (ParseVariableBool(newval,"ECHO_HIDDEN"))
766770
pset.echo_hidden=PSQL_ECHO_HIDDEN_ON;
771+
else/* ParseVariableBool printed msg if needed */
772+
pset.echo_hidden=PSQL_ECHO_HIDDEN_OFF;
767773
}
768774

769775
staticvoid
@@ -773,25 +779,52 @@ on_error_rollback_hook(const char *newval)
773779
pset.on_error_rollback=PSQL_ERROR_ROLLBACK_OFF;
774780
elseif (pg_strcasecmp(newval,"interactive")==0)
775781
pset.on_error_rollback=PSQL_ERROR_ROLLBACK_INTERACTIVE;
776-
elseif (pg_strcasecmp(newval,"off")==0)
782+
elseif (ParseVariableBool(newval,"ON_ERROR_ROLLBACK"))
783+
pset.on_error_rollback=PSQL_ERROR_ROLLBACK_ON;
784+
else/* ParseVariableBool printed msg if needed */
777785
pset.on_error_rollback=PSQL_ERROR_ROLLBACK_OFF;
786+
}
787+
788+
staticvoid
789+
comp_keyword_case_hook(constchar*newval)
790+
{
791+
if (newval==NULL)
792+
pset.comp_case=PSQL_COMP_CASE_PRESERVE_UPPER;
793+
elseif (pg_strcasecmp(newval,"preserve-upper")==0)
794+
pset.comp_case=PSQL_COMP_CASE_PRESERVE_UPPER;
795+
elseif (pg_strcasecmp(newval,"preserve-lower")==0)
796+
pset.comp_case=PSQL_COMP_CASE_PRESERVE_LOWER;
797+
elseif (pg_strcasecmp(newval,"upper")==0)
798+
pset.comp_case=PSQL_COMP_CASE_UPPER;
799+
elseif (pg_strcasecmp(newval,"lower")==0)
800+
pset.comp_case=PSQL_COMP_CASE_LOWER;
778801
else
779-
pset.on_error_rollback=PSQL_ERROR_ROLLBACK_ON;
802+
{
803+
psql_error("unrecognized value \"%s\" for \"%s\"; assuming \"%s\"\n",
804+
newval,"COMP_KEYWORD_CASE","preserve-upper");
805+
pset.comp_case=PSQL_COMP_CASE_PRESERVE_UPPER;
806+
}
780807
}
781808

782809
staticvoid
783810
histcontrol_hook(constchar*newval)
784811
{
785812
if (newval==NULL)
786813
pset.histcontrol=hctl_none;
787-
elseif (strcmp(newval,"ignorespace")==0)
814+
elseif (pg_strcasecmp(newval,"ignorespace")==0)
788815
pset.histcontrol=hctl_ignorespace;
789-
elseif (strcmp(newval,"ignoredups")==0)
816+
elseif (pg_strcasecmp(newval,"ignoredups")==0)
790817
pset.histcontrol=hctl_ignoredups;
791-
elseif (strcmp(newval,"ignoreboth")==0)
818+
elseif (pg_strcasecmp(newval,"ignoreboth")==0)
792819
pset.histcontrol=hctl_ignoreboth;
820+
elseif (pg_strcasecmp(newval,"none")==0)
821+
pset.histcontrol=hctl_none;
793822
else
823+
{
824+
psql_error("unrecognized value \"%s\" for \"%s\"; assuming \"%s\"\n",
825+
newval,"HISTCONTROL","none");
794826
pset.histcontrol=hctl_none;
827+
}
795828
}
796829

797830
staticvoid
@@ -817,14 +850,18 @@ verbosity_hook(const char *newval)
817850
{
818851
if (newval==NULL)
819852
pset.verbosity=PQERRORS_DEFAULT;
820-
elseif (strcmp(newval,"default")==0)
853+
elseif (pg_strcasecmp(newval,"default")==0)
821854
pset.verbosity=PQERRORS_DEFAULT;
822-
elseif (strcmp(newval,"terse")==0)
855+
elseif (pg_strcasecmp(newval,"terse")==0)
823856
pset.verbosity=PQERRORS_TERSE;
824-
elseif (strcmp(newval,"verbose")==0)
857+
elseif (pg_strcasecmp(newval,"verbose")==0)
825858
pset.verbosity=PQERRORS_VERBOSE;
826859
else
860+
{
861+
psql_error("unrecognized value \"%s\" for \"%s\"; assuming \"%s\"\n",
862+
newval,"VERBOSITY","default");
827863
pset.verbosity=PQERRORS_DEFAULT;
864+
}
828865

829866
if (pset.db)
830867
PQsetErrorVerbosity(pset.db,pset.verbosity);
@@ -845,6 +882,7 @@ EstablishVariableSpace(void)
845882
SetVariableAssignHook(pset.vars,"ECHO",echo_hook);
846883
SetVariableAssignHook(pset.vars,"ECHO_HIDDEN",echo_hidden_hook);
847884
SetVariableAssignHook(pset.vars,"ON_ERROR_ROLLBACK",on_error_rollback_hook);
885+
SetVariableAssignHook(pset.vars,"COMP_KEYWORD_CASE",comp_keyword_case_hook);
848886
SetVariableAssignHook(pset.vars,"HISTCONTROL",histcontrol_hook);
849887
SetVariableAssignHook(pset.vars,"PROMPT1",prompt1_hook);
850888
SetVariableAssignHook(pset.vars,"PROMPT2",prompt2_hook);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp