optionally stores the query's output in <replaceable
class="parameter">filename</replaceable> or pipes the output
into a separate Unix shell executing <replaceable
- class="parameter">command</replaceable>. A bare
- <literal>\g</literal> is virtually equivalent to a semicolon. A
- <literal>\g</literal> with argument is a <quote>one-shot</quote>
+ class="parameter">command</replaceable>. The file or command is
+ written to only if the query successfully returns zero or more tuples,
+ not if the query fails or is a non-data-returning SQL command.
+ </para>
+ <para>
+ A bare <literal>\g</literal> is essentially equivalent to a semicolon.
+ A <literal>\g</literal> with argument is a <quote>one-shot</quote>
alternative to the <command>\o</command> command.
</para>
</listitem>
free(fname);
}
- /* \g means send query */
+ /* \g [filename] means send query, optionally with output to file/pipe */
else if (strcmp(cmd, "g") == 0)
{
char *fname = psql_scan_slash_option(scan_state,
pset.queryFout = queryFout_copy;
pset.queryFoutPipe = queryFoutPipe_copy;
-
- free(pset.gfname);
- pset.gfname = NULL;
}
else
printQuery(results, &my_popt, pset.queryFout, pset.logfile);
PGresult *results;
PGTransactionStatusType transaction_status;
double elapsed_msec = 0;
- bool OK,
- on_error_rollback_savepoint = false;
+ bool OK = false;
+ bool on_error_rollback_savepoint = false;
static bool on_error_rollback_warning = false;
if (!pset.db)
{
psql_error("You are currently not connected to a database.\n");
- return false;
+ goto sendquery_cleanup;
}
if (pset.singlestep)
fflush(stdout);
if (fgets(buf, sizeof(buf), stdin) != NULL)
if (buf[0] == 'x')
- return false;
+ goto sendquery_cleanup;
}
else if (pset.echo == PSQL_ECHO_QUERIES)
{
psql_error("%s", PQerrorMessage(pset.db));
PQclear(results);
ResetCancelConn();
- return false;
+ goto sendquery_cleanup;
}
PQclear(results);
transaction_status = PQtransactionStatus(pset.db);
psql_error("%s", PQerrorMessage(pset.db));
PQclear(results);
ResetCancelConn();
- return false;
+ goto sendquery_cleanup;
}
PQclear(results);
on_error_rollback_savepoint = true;
{
psql_error("%s", PQerrorMessage(pset.db));
PQclear(svptres);
+ OK = false;
PQclear(results);
ResetCancelConn();
- return false;
+ goto sendquery_cleanup;
}
PQclear(svptres);
}
PrintNotifications();
+ /* perform cleanup that should occur after any attempted query */
+
+sendquery_cleanup:
+
+ /* reset \g's output-to-filename trigger */
+ if (pset.gfname)
+ {
+ free(pset.gfname);
+ pset.gfname = NULL;
+ }
+
return OK;
}
pset.queryFout = queryFout_copy;
pset.queryFoutPipe = queryFoutPipe_copy;
-
- free(pset.gfname);
- pset.gfname = NULL;
}
else if (did_pager)
{