mirror of
https://github.com/postgres/postgres.git
synced 2025-06-16 06:01:02 +03:00
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.
This commit is contained in:
@ -512,11 +512,11 @@ PostgreSQL documentation
|
|||||||
<term><option>--single-transaction</option></term>
|
<term><option>--single-transaction</option></term>
|
||||||
<listitem>
|
<listitem>
|
||||||
<para>
|
<para>
|
||||||
When <application>psql</application> executes a script with the
|
When <application>psql</application> executes a script, adding
|
||||||
<option>-f</> option, adding this option wraps
|
this option wraps <command>BEGIN</>/<command>COMMIT</> around the
|
||||||
<command>BEGIN</>/<command>COMMIT</> around the script to execute it
|
script to execute it as a single transaction. This ensures that
|
||||||
as a single transaction. This ensures that either all the commands
|
either all the commands complete successfully, or no changes are
|
||||||
complete successfully, or no changes are applied.
|
applied.
|
||||||
</para>
|
</para>
|
||||||
|
|
||||||
<para>
|
<para>
|
||||||
|
@ -2043,9 +2043,11 @@ process_file(char *filename, bool single_txn, bool use_relative_path)
|
|||||||
PGresult *res;
|
PGresult *res;
|
||||||
|
|
||||||
if (!filename)
|
if (!filename)
|
||||||
return EXIT_FAILURE;
|
{
|
||||||
|
fd = stdin;
|
||||||
if (strcmp(filename, "-") != 0)
|
filename = NULL;
|
||||||
|
}
|
||||||
|
else if (strcmp(filename, "-") != 0)
|
||||||
{
|
{
|
||||||
canonicalize_path(filename);
|
canonicalize_path(filename);
|
||||||
|
|
||||||
|
@ -97,7 +97,7 @@ usage(void)
|
|||||||
printf(_(" -V, --version output version information, then exit\n"));
|
printf(_(" -V, --version output version information, then exit\n"));
|
||||||
printf(_(" -X, --no-psqlrc do not read startup file (~/.psqlrc)\n"));
|
printf(_(" -X, --no-psqlrc do not read startup file (~/.psqlrc)\n"));
|
||||||
printf(_(" -1 (\"one\"), --single-transaction\n"
|
printf(_(" -1 (\"one\"), --single-transaction\n"
|
||||||
" execute command file as a single transaction\n"));
|
" execute as a single transaction (if non-interactive)\n"));
|
||||||
printf(_(" -?, --help show this help, then exit\n"));
|
printf(_(" -?, --help show this help, then exit\n"));
|
||||||
|
|
||||||
printf(_("\nInput and output options:\n"));
|
printf(_("\nInput and output options:\n"));
|
||||||
|
@ -150,6 +150,27 @@ main(int argc, char *argv[])
|
|||||||
|
|
||||||
parse_psql_options(argc, argv, &options);
|
parse_psql_options(argc, argv, &options);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* If no action was specified and we're in non-interactive mode, treat
|
||||||
|
* it as if the user had specified "-f -". This lets single-transaction
|
||||||
|
* mode work in this case.
|
||||||
|
*/
|
||||||
|
if (options.action == ACT_NOTHING && pset.notty)
|
||||||
|
{
|
||||||
|
options.action = ACT_FILE;
|
||||||
|
options.action_string = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Bail out if -1 was specified but will be ignored. */
|
||||||
|
if (options.single_txn && options.action != ACT_FILE)
|
||||||
|
{
|
||||||
|
if (options.action == ACT_NOTHING)
|
||||||
|
fprintf(stderr,_("%s: -1 can only be used in non-interactive mode\n"), pset.progname);
|
||||||
|
else
|
||||||
|
fprintf(stderr,_("%s: -1 is incompatible with -c and -l\n"), pset.progname);
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
if (!pset.popt.topt.fieldSep.separator &&
|
if (!pset.popt.topt.fieldSep.separator &&
|
||||||
!pset.popt.topt.fieldSep.separator_zero)
|
!pset.popt.topt.fieldSep.separator_zero)
|
||||||
{
|
{
|
||||||
@ -309,11 +330,9 @@ main(int argc, char *argv[])
|
|||||||
process_psqlrc(argv[0]);
|
process_psqlrc(argv[0]);
|
||||||
|
|
||||||
connection_warnings(true);
|
connection_warnings(true);
|
||||||
if (!pset.quiet && !pset.notty)
|
if (!pset.quiet)
|
||||||
printf(_("Type \"help\" for help.\n\n"));
|
printf(_("Type \"help\" for help.\n\n"));
|
||||||
if (!pset.notty)
|
initializeInput(options.no_readline ? 0 : 1);
|
||||||
initializeInput(options.no_readline ? 0 : 1);
|
|
||||||
|
|
||||||
successResult = MainLoop(stdin);
|
successResult = MainLoop(stdin);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user