Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit be690e2

Browse files
committed
Make psql -1 < file behave as expected.
Previously, the -1 option was silently ignored. Also, emit an error if -1 is used in a context where it won't be respected, to avoid user confusion. Original patch by Fabien COELHO, but this version is quite different from the original submission.
1 parent 92ec037 commit be690e2

File tree

4 files changed

+34
-13
lines changed

4 files changed

+34
-13
lines changed

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -512,11 +512,11 @@ PostgreSQL documentation
512512
<term><option>--single-transaction</option></term>
513513
<listitem>
514514
<para>
515-
When <application>psql</application> executes a script with the
516-
<option>-f</> option, adding this option wraps
517-
<command>BEGIN</>/<command>COMMIT</> around the script to execute it
518-
as a single transaction. This ensures that either all the commands
519-
complete successfully, or no changes are applied.
515+
When <application>psql</application> executes a script, adding
516+
this option wraps <command>BEGIN</>/<command>COMMIT</> around the
517+
script to execute it as a single transaction. This ensures that
518+
either all the commands complete successfully, or no changes are
519+
applied.
520520
</para>
521521

522522
<para>

src/bin/psql/command.c

+5-3
Original file line numberDiff line numberDiff line change
@@ -2043,9 +2043,11 @@ process_file(char *filename, bool single_txn, bool use_relative_path)
20432043
PGresult *res;
20442044

20452045
if (!filename)
2046-
return EXIT_FAILURE;
2047-
2048-
if (strcmp(filename, "-") != 0)
2046+
{
2047+
fd = stdin;
2048+
filename = NULL;
2049+
}
2050+
else if (strcmp(filename, "-") != 0)
20492051
{
20502052
canonicalize_path(filename);
20512053

src/bin/psql/help.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ usage(void)
9797
printf(_(" -V, --version output version information, then exit\n"));
9898
printf(_(" -X, --no-psqlrc do not read startup file (~/.psqlrc)\n"));
9999
printf(_(" -1 (\"one\"), --single-transaction\n"
100-
" execute command file as a single transaction\n"));
100+
" execute as a single transaction (if non-interactive)\n"));
101101
printf(_(" -?, --help show this help, then exit\n"));
102102

103103
printf(_("\nInput and output options:\n"));

src/bin/psql/startup.c

+23-4
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,27 @@ main(int argc, char *argv[])
150150

151151
parse_psql_options(argc, argv, &options);
152152

153+
/*
154+
* If no action was specified and we're in non-interactive mode, treat
155+
* it as if the user had specified "-f -". This lets single-transaction
156+
* mode work in this case.
157+
*/
158+
if (options.action == ACT_NOTHING && pset.notty)
159+
{
160+
options.action = ACT_FILE;
161+
options.action_string = NULL;
162+
}
163+
164+
/* Bail out if -1 was specified but will be ignored. */
165+
if (options.single_txn && options.action != ACT_FILE)
166+
{
167+
if (options.action == ACT_NOTHING)
168+
fprintf(stderr,_("%s: -1 can only be used in non-interactive mode\n"), pset.progname);
169+
else
170+
fprintf(stderr,_("%s: -1 is incompatible with -c and -l\n"), pset.progname);
171+
exit(EXIT_FAILURE);
172+
}
173+
153174
if (!pset.popt.topt.fieldSep.separator &&
154175
!pset.popt.topt.fieldSep.separator_zero)
155176
{
@@ -309,11 +330,9 @@ main(int argc, char *argv[])
309330
process_psqlrc(argv[0]);
310331

311332
connection_warnings(true);
312-
if (!pset.quiet && !pset.notty)
333+
if (!pset.quiet)
313334
printf(_("Type \"help\" for help.\n\n"));
314-
if (!pset.notty)
315-
initializeInput(options.no_readline ? 0 : 1);
316-
335+
initializeInput(options.no_readline ? 0 : 1);
317336
successResult = MainLoop(stdin);
318337
}
319338

0 commit comments

Comments
 (0)