summaryrefslogtreecommitdiff
path: root/src/bin/psql/large_obj.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/bin/psql/large_obj.c')
-rw-r--r--src/bin/psql/large_obj.c262
1 files changed, 87 insertions, 175 deletions
diff --git a/src/bin/psql/large_obj.c b/src/bin/psql/large_obj.c
index 3d0b3a6eb96..a565d204784 100644
--- a/src/bin/psql/large_obj.c
+++ b/src/bin/psql/large_obj.c
@@ -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,70 +20,95 @@
/*
- * 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 bool
+start_lo_xact(const char *operation, bool *own_transaction)
+{
+ PGTransactionStatusType tstatus;
+ PGresult *res;
-static char notice[80];
+ *own_transaction = false;
-static void
-_my_notice_handler(void *arg, const char *message)
-{
- (void) arg;
- strncpy(notice, message, 79);
- notice[79] = '\0';
-}
+ if (!pset.db)
+ {
+ psql_error("%s: not connected to a database\n", operation);
+ return false;
+ }
+
+ 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
-handle_transaction(void)
+finish_lo_xact(const char *operation, bool own_transaction)
{
PGresult *res;
- bool commit = false;
- PQnoticeProcessor old_notice_hook;
- switch (SwitchVariable(pset.vars, "LO_TRANSACTION",
- "nothing",
- "commit",
- NULL))
+ if (own_transaction &&
+ GetVariableBool(pset.vars, "AUTOCOMMIT"))
{
- case 1: /* nothing */
- return true;
- case 2: /* commit */
- commit = true;
- break;
+ /* close out our own xact */
+ if (!(res = PSQLexec("COMMIT", false)))
+ {
+ res = PSQLexec("ROLLBACK", false);
+ PQclear(res);
+ return false;
+ }
+ PQclear(res);
}
- notice[0] = '\0';
- old_notice_hook = PQsetNoticeProcessor(pset.db, _my_notice_handler, NULL);
+ return true;
+}
- res = PSQLexec(commit ? "COMMIT" : "ROLLBACK", false);
- if (!res)
- return false;
+/*
+ * Clean up after a failed LO operation
+ */
+static bool
+fail_lo_xact(const char *operation, bool own_transaction)
+{
+ PGresult *res;
- if (notice[0])
+ if (own_transaction &&
+ GetVariableBool(pset.vars, "AUTOCOMMIT"))
{
- 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."));
+ /* close out our own xact */
+ res = PSQLexec("ROLLBACK", false);
+ PQclear(res);
}
- PQsetNoticeProcessor(pset.db, old_notice_hook, NULL);
- PQclear(res);
- return true;
+ return false; /* always */
}
-
/*
* do_lo_export()
*
@@ -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 false;
+ return fail_lo_xact("\\lo_export", own_transaction);
}
- if (own_transaction)
- {
- if (!(res = PSQLexec("COMMIT", false)))
- {
- res = PQexec(pset.db, "ROLLBACK");
- PQclear(res);
- return false;
- }
-
- PQclear(res);
- }
+ if (!finish_lo_xact("\\lo_export", own_transaction))
+ return false;
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);