mirror of
https://github.com/postgres/postgres.git
synced 2025-12-10 14:22:35 +03:00
Update psql for some features of new FE/BE protocol. There is a
client-side AUTOCOMMIT mode now: '\set AUTOCOMMIT off' supports SQL-spec commit behavior. Get rid of LO_TRANSACTION hack --- the LO operations just work now, using libpq's ability to track the transaction status. Add a VERBOSE variable to control verboseness of error message display, and add a %T prompt-string code to show current transaction-block status. Superuser state display in the prompt string correctly follows SET SESSION AUTHORIZATION commands. Control-C works to get out of COPY IN state.
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
*
|
||||
* Copyright 2000-2002 by PostgreSQL Global Development Group
|
||||
*
|
||||
* $Header: /cvsroot/pgsql/src/bin/psql/large_obj.c,v 1.26 2003/06/27 16:55:23 tgl Exp $
|
||||
* $Header: /cvsroot/pgsql/src/bin/psql/large_obj.c,v 1.27 2003/06/28 00:12:40 tgl Exp $
|
||||
*/
|
||||
#include "postgres_fe.h"
|
||||
#include "large_obj.h"
|
||||
@@ -20,68 +20,93 @@
|
||||
|
||||
|
||||
/*
|
||||
* Since all large object ops must be in a transaction, we must do some magic
|
||||
* here. You can set the variable lo_transaction to one of commit|rollback|
|
||||
* nothing to get your favourite behaviour regarding any transaction in
|
||||
* progress. Rollback is default.
|
||||
* Prepare to do a large-object operation. We *must* be inside a transaction
|
||||
* block for all these operations, so start one if needed.
|
||||
*
|
||||
* Returns TRUE if okay, FALSE if failed. *own_transaction is set to indicate
|
||||
* if we started our own transaction or not.
|
||||
*/
|
||||
|
||||
static char notice[80];
|
||||
|
||||
static void
|
||||
_my_notice_handler(void *arg, const char *message)
|
||||
{
|
||||
(void) arg;
|
||||
strncpy(notice, message, 79);
|
||||
notice[79] = '\0';
|
||||
}
|
||||
|
||||
|
||||
static bool
|
||||
handle_transaction(void)
|
||||
start_lo_xact(const char *operation, bool *own_transaction)
|
||||
{
|
||||
PGTransactionStatusType tstatus;
|
||||
PGresult *res;
|
||||
bool commit = false;
|
||||
PQnoticeProcessor old_notice_hook;
|
||||
|
||||
switch (SwitchVariable(pset.vars, "LO_TRANSACTION",
|
||||
"nothing",
|
||||
"commit",
|
||||
NULL))
|
||||
*own_transaction = false;
|
||||
|
||||
if (!pset.db)
|
||||
{
|
||||
case 1: /* nothing */
|
||||
return true;
|
||||
case 2: /* commit */
|
||||
commit = true;
|
||||
break;
|
||||
}
|
||||
|
||||
notice[0] = '\0';
|
||||
old_notice_hook = PQsetNoticeProcessor(pset.db, _my_notice_handler, NULL);
|
||||
|
||||
res = PSQLexec(commit ? "COMMIT" : "ROLLBACK", false);
|
||||
if (!res)
|
||||
psql_error("%s: not connected to a database\n", operation);
|
||||
return false;
|
||||
|
||||
if (notice[0])
|
||||
{
|
||||
if ((!commit && strcmp(notice, "WARNING: ROLLBACK: no transaction in progress\n") != 0) ||
|
||||
(commit && strcmp(notice, "WARNING: COMMIT: no transaction in progress\n") != 0))
|
||||
fputs(notice, stderr);
|
||||
}
|
||||
else if (!QUIET())
|
||||
{
|
||||
if (commit)
|
||||
puts(gettext("Warning: Your transaction in progress has been committed."));
|
||||
else
|
||||
puts(gettext("Warning: Your transaction in progress has been rolled back."));
|
||||
}
|
||||
|
||||
PQsetNoticeProcessor(pset.db, old_notice_hook, NULL);
|
||||
PQclear(res);
|
||||
tstatus = PQtransactionStatus(pset.db);
|
||||
|
||||
switch (tstatus)
|
||||
{
|
||||
case PQTRANS_IDLE:
|
||||
/* need to start our own xact */
|
||||
if (!(res = PSQLexec("BEGIN", false)))
|
||||
return false;
|
||||
PQclear(res);
|
||||
*own_transaction = true;
|
||||
break;
|
||||
case PQTRANS_INTRANS:
|
||||
/* use the existing xact */
|
||||
break;
|
||||
case PQTRANS_INERROR:
|
||||
psql_error("%s: current transaction is aborted\n", operation);
|
||||
return false;
|
||||
default:
|
||||
psql_error("%s: unknown transaction status\n", operation);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
* Clean up after a successful LO operation
|
||||
*/
|
||||
static bool
|
||||
finish_lo_xact(const char *operation, bool own_transaction)
|
||||
{
|
||||
PGresult *res;
|
||||
|
||||
if (own_transaction &&
|
||||
GetVariableBool(pset.vars, "AUTOCOMMIT"))
|
||||
{
|
||||
/* close out our own xact */
|
||||
if (!(res = PSQLexec("COMMIT", false)))
|
||||
{
|
||||
res = PSQLexec("ROLLBACK", false);
|
||||
PQclear(res);
|
||||
return false;
|
||||
}
|
||||
PQclear(res);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
* Clean up after a failed LO operation
|
||||
*/
|
||||
static bool
|
||||
fail_lo_xact(const char *operation, bool own_transaction)
|
||||
{
|
||||
PGresult *res;
|
||||
|
||||
if (own_transaction &&
|
||||
GetVariableBool(pset.vars, "AUTOCOMMIT"))
|
||||
{
|
||||
/* close out our own xact */
|
||||
res = PSQLexec("ROLLBACK", false);
|
||||
PQclear(res);
|
||||
}
|
||||
|
||||
return false; /* always */
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
@@ -92,53 +117,22 @@ handle_transaction(void)
|
||||
bool
|
||||
do_lo_export(const char *loid_arg, const char *filename_arg)
|
||||
{
|
||||
PGresult *res;
|
||||
int status;
|
||||
bool own_transaction;
|
||||
|
||||
own_transaction = !VariableEquals(pset.vars, "LO_TRANSACTION", "nothing");
|
||||
|
||||
if (!pset.db)
|
||||
{
|
||||
psql_error("\\lo_export: not connected to a database\n");
|
||||
if (!start_lo_xact("\\lo_export", &own_transaction))
|
||||
return false;
|
||||
}
|
||||
|
||||
if (own_transaction)
|
||||
{
|
||||
if (!handle_transaction())
|
||||
return false;
|
||||
|
||||
if (!(res = PSQLexec("BEGIN", false)))
|
||||
return false;
|
||||
|
||||
PQclear(res);
|
||||
}
|
||||
|
||||
status = lo_export(pset.db, atooid(loid_arg), filename_arg);
|
||||
if (status != 1)
|
||||
{ /* of course this status is documented
|
||||
* nowhere :( */
|
||||
fputs(PQerrorMessage(pset.db), stderr);
|
||||
if (own_transaction)
|
||||
{
|
||||
res = PQexec(pset.db, "ROLLBACK");
|
||||
PQclear(res);
|
||||
}
|
||||
return fail_lo_xact("\\lo_export", own_transaction);
|
||||
}
|
||||
|
||||
if (!finish_lo_xact("\\lo_export", own_transaction))
|
||||
return false;
|
||||
}
|
||||
|
||||
if (own_transaction)
|
||||
{
|
||||
if (!(res = PSQLexec("COMMIT", false)))
|
||||
{
|
||||
res = PQexec(pset.db, "ROLLBACK");
|
||||
PQclear(res);
|
||||
return false;
|
||||
}
|
||||
|
||||
PQclear(res);
|
||||
}
|
||||
|
||||
fprintf(pset.queryFout, "lo_export\n");
|
||||
|
||||
@@ -146,7 +140,6 @@ do_lo_export(const char *loid_arg, const char *filename_arg)
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* do_lo_import()
|
||||
*
|
||||
@@ -161,41 +154,20 @@ do_lo_import(const char *filename_arg, const char *comment_arg)
|
||||
unsigned int i;
|
||||
bool own_transaction;
|
||||
|
||||
own_transaction = !VariableEquals(pset.vars, "LO_TRANSACTION", "nothing");
|
||||
|
||||
if (!pset.db)
|
||||
{
|
||||
psql_error("\\lo_import: not connected to a database\n");
|
||||
if (!start_lo_xact("\\lo_import", &own_transaction))
|
||||
return false;
|
||||
}
|
||||
|
||||
if (own_transaction)
|
||||
{
|
||||
if (!handle_transaction())
|
||||
return false;
|
||||
|
||||
if (!(res = PSQLexec("BEGIN", false)))
|
||||
return false;
|
||||
|
||||
PQclear(res);
|
||||
}
|
||||
|
||||
loid = lo_import(pset.db, filename_arg);
|
||||
if (loid == InvalidOid)
|
||||
{
|
||||
fputs(PQerrorMessage(pset.db), stderr);
|
||||
if (own_transaction)
|
||||
{
|
||||
res = PQexec(pset.db, "ROLLBACK");
|
||||
PQclear(res);
|
||||
}
|
||||
return false;
|
||||
return fail_lo_xact("\\lo_import", own_transaction);
|
||||
}
|
||||
|
||||
/* insert description if given */
|
||||
/* XXX don't try to hack pg_description if not superuser */
|
||||
/* XXX ought to replace this with some kind of COMMENT command */
|
||||
if (comment_arg && pset.issuper)
|
||||
if (comment_arg && is_superuser())
|
||||
{
|
||||
char *cmdbuf;
|
||||
char *bufptr;
|
||||
@@ -203,14 +175,7 @@ do_lo_import(const char *filename_arg, const char *comment_arg)
|
||||
|
||||
cmdbuf = malloc(slen * 2 + 256);
|
||||
if (!cmdbuf)
|
||||
{
|
||||
if (own_transaction)
|
||||
{
|
||||
res = PQexec(pset.db, "ROLLBACK");
|
||||
PQclear(res);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return fail_lo_xact("\\lo_import", own_transaction);
|
||||
sprintf(cmdbuf,
|
||||
"INSERT INTO pg_catalog.pg_description VALUES ('%u', "
|
||||
"'pg_catalog.pg_largeobject'::regclass, "
|
||||
@@ -227,31 +192,16 @@ do_lo_import(const char *filename_arg, const char *comment_arg)
|
||||
|
||||
if (!(res = PSQLexec(cmdbuf, false)))
|
||||
{
|
||||
if (own_transaction)
|
||||
{
|
||||
res = PQexec(pset.db, "ROLLBACK");
|
||||
PQclear(res);
|
||||
}
|
||||
free(cmdbuf);
|
||||
return false;
|
||||
return fail_lo_xact("\\lo_import", own_transaction);
|
||||
}
|
||||
|
||||
PQclear(res);
|
||||
free(cmdbuf);
|
||||
}
|
||||
|
||||
if (own_transaction)
|
||||
{
|
||||
if (!(res = PSQLexec("COMMIT", false)))
|
||||
{
|
||||
res = PQexec(pset.db, "ROLLBACK");
|
||||
PQclear(res);
|
||||
return false;
|
||||
}
|
||||
|
||||
PQclear(res);
|
||||
}
|
||||
|
||||
if (!finish_lo_xact("\\lo_import", own_transaction))
|
||||
return false;
|
||||
|
||||
fprintf(pset.queryFout, "lo_import %u\n", loid);
|
||||
sprintf(oidbuf, "%u", loid);
|
||||
@@ -261,7 +211,6 @@ do_lo_import(const char *filename_arg, const char *comment_arg)
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* do_lo_unlink()
|
||||
*
|
||||
@@ -276,69 +225,32 @@ do_lo_unlink(const char *loid_arg)
|
||||
char buf[256];
|
||||
bool own_transaction;
|
||||
|
||||
own_transaction = !VariableEquals(pset.vars, "LO_TRANSACTION", "nothing");
|
||||
|
||||
if (!pset.db)
|
||||
{
|
||||
psql_error("\\lo_unlink: not connected to a database\n");
|
||||
if (!start_lo_xact("\\lo_unlink", &own_transaction))
|
||||
return false;
|
||||
}
|
||||
|
||||
if (own_transaction)
|
||||
{
|
||||
if (!handle_transaction())
|
||||
return false;
|
||||
|
||||
if (!(res = PSQLexec("BEGIN", false)))
|
||||
return false;
|
||||
|
||||
PQclear(res);
|
||||
}
|
||||
|
||||
status = lo_unlink(pset.db, loid);
|
||||
if (status == -1)
|
||||
{
|
||||
fputs(PQerrorMessage(pset.db), stderr);
|
||||
if (own_transaction)
|
||||
{
|
||||
res = PQexec(pset.db, "ROLLBACK");
|
||||
PQclear(res);
|
||||
}
|
||||
return false;
|
||||
return fail_lo_xact("\\lo_unlink", own_transaction);
|
||||
}
|
||||
|
||||
/* remove the comment as well */
|
||||
/* XXX don't try to hack pg_description if not superuser */
|
||||
/* XXX ought to replace this with some kind of COMMENT command */
|
||||
if (pset.issuper)
|
||||
if (is_superuser())
|
||||
{
|
||||
snprintf(buf, sizeof(buf),
|
||||
"DELETE FROM pg_catalog.pg_description WHERE objoid = '%u' "
|
||||
"AND classoid = 'pg_catalog.pg_largeobject'::regclass",
|
||||
loid);
|
||||
if (!(res = PSQLexec(buf, false)))
|
||||
{
|
||||
if (own_transaction)
|
||||
{
|
||||
res = PQexec(pset.db, "ROLLBACK");
|
||||
PQclear(res);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
PQclear(res);
|
||||
}
|
||||
|
||||
if (own_transaction)
|
||||
{
|
||||
if (!(res = PSQLexec("COMMIT", false)))
|
||||
{
|
||||
res = PQexec(pset.db, "ROLLBACK");
|
||||
PQclear(res);
|
||||
return false;
|
||||
}
|
||||
return fail_lo_xact("\\lo_unlink", own_transaction);
|
||||
PQclear(res);
|
||||
}
|
||||
|
||||
if (!finish_lo_xact("\\lo_unlink", own_transaction))
|
||||
return false;
|
||||
|
||||
fprintf(pset.queryFout, "lo_unlink %u\n", loid);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user