2000-01-19 00:30:24 +01:00
|
|
|
/*
|
|
|
|
* psql - the PostgreSQL interactive terminal
|
|
|
|
*
|
2009-01-01 18:24:05 +01:00
|
|
|
* Copyright (c) 2000-2009, PostgreSQL Global Development Group
|
2000-01-19 00:30:24 +01:00
|
|
|
*
|
2009-01-01 18:24:05 +01:00
|
|
|
* $PostgreSQL: pgsql/src/bin/psql/large_obj.c,v 1.52 2009/01/01 17:23:55 momjian Exp $
|
2000-01-19 00:30:24 +01:00
|
|
|
*/
|
2001-02-10 03:31:31 +01:00
|
|
|
#include "postgres_fe.h"
|
1999-11-04 22:56:02 +01:00
|
|
|
#include "large_obj.h"
|
|
|
|
|
|
|
|
|
|
|
|
#include "settings.h"
|
|
|
|
#include "common.h"
|
|
|
|
|
2007-01-20 17:57:31 +01:00
|
|
|
static void
|
|
|
|
print_lo_result(const char *fmt,...)
|
|
|
|
__attribute__((format(printf, 1, 2)));
|
|
|
|
|
|
|
|
static void
|
|
|
|
print_lo_result(const char *fmt,...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
if (!pset.quiet)
|
|
|
|
{
|
|
|
|
if (pset.popt.topt.format == PRINT_HTML)
|
|
|
|
fputs("<p>", pset.queryFout);
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
vfprintf(pset.queryFout, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
if (pset.popt.topt.format == PRINT_HTML)
|
|
|
|
fputs("</p>\n", pset.queryFout);
|
|
|
|
else
|
|
|
|
fputs("\n", pset.queryFout);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pset.logfile)
|
|
|
|
{
|
|
|
|
va_start(ap, fmt);
|
|
|
|
vfprintf(pset.logfile, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
fputs("\n", pset.logfile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-11-04 22:56:02 +01:00
|
|
|
|
1999-11-05 00:14:30 +01:00
|
|
|
/*
|
2003-08-04 02:43:34 +02:00
|
|
|
* Prepare to do a large-object operation. We *must* be inside a transaction
|
2003-06-28 02:12:40 +02:00
|
|
|
* 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.
|
1999-11-04 22:56:02 +01:00
|
|
|
*/
|
2003-06-28 02:12:40 +02:00
|
|
|
static bool
|
|
|
|
start_lo_xact(const char *operation, bool *own_transaction)
|
|
|
|
{
|
|
|
|
PGTransactionStatusType tstatus;
|
|
|
|
PGresult *res;
|
1999-11-04 22:56:02 +01:00
|
|
|
|
2003-06-28 02:12:40 +02:00
|
|
|
*own_transaction = false;
|
1999-11-04 22:56:02 +01:00
|
|
|
|
2003-06-28 02:12:40 +02:00
|
|
|
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;
|
|
|
|
}
|
1999-11-04 22:56:02 +01:00
|
|
|
|
2003-06-28 02:12:40 +02:00
|
|
|
return true;
|
|
|
|
}
|
1999-11-04 22:56:02 +01:00
|
|
|
|
2003-06-28 02:12:40 +02:00
|
|
|
/*
|
|
|
|
* Clean up after a successful LO operation
|
|
|
|
*/
|
1999-11-04 22:56:02 +01:00
|
|
|
static bool
|
2003-06-28 02:12:40 +02:00
|
|
|
finish_lo_xact(const char *operation, bool own_transaction)
|
1999-11-04 22:56:02 +01:00
|
|
|
{
|
1999-11-05 00:14:30 +01:00
|
|
|
PGresult *res;
|
1999-11-04 22:56:02 +01:00
|
|
|
|
2006-08-29 17:19:51 +02:00
|
|
|
if (own_transaction && pset.autocommit)
|
2003-03-20 07:43:35 +01:00
|
|
|
{
|
2003-06-28 02:12:40 +02:00
|
|
|
/* close out our own xact */
|
|
|
|
if (!(res = PSQLexec("COMMIT", false)))
|
|
|
|
{
|
|
|
|
res = PSQLexec("ROLLBACK", false);
|
|
|
|
PQclear(res);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
PQclear(res);
|
2003-03-20 07:43:35 +01:00
|
|
|
}
|
1999-11-05 00:14:30 +01:00
|
|
|
|
2003-06-28 02:12:40 +02:00
|
|
|
return true;
|
|
|
|
}
|
1999-11-05 00:14:30 +01:00
|
|
|
|
2003-06-28 02:12:40 +02:00
|
|
|
/*
|
|
|
|
* Clean up after a failed LO operation
|
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
fail_lo_xact(const char *operation, bool own_transaction)
|
|
|
|
{
|
|
|
|
PGresult *res;
|
1999-11-04 22:56:02 +01:00
|
|
|
|
2006-08-29 17:19:51 +02:00
|
|
|
if (own_transaction && pset.autocommit)
|
1999-11-05 00:14:30 +01:00
|
|
|
{
|
2003-06-28 02:12:40 +02:00
|
|
|
/* close out our own xact */
|
|
|
|
res = PSQLexec("ROLLBACK", false);
|
|
|
|
PQclear(res);
|
1999-11-05 00:14:30 +01:00
|
|
|
}
|
|
|
|
|
2003-06-28 02:12:40 +02:00
|
|
|
return false; /* always */
|
1999-11-04 22:56:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* do_lo_export()
|
|
|
|
*
|
|
|
|
* Write a large object to a file
|
|
|
|
*/
|
|
|
|
bool
|
2000-01-14 23:18:03 +01:00
|
|
|
do_lo_export(const char *loid_arg, const char *filename_arg)
|
1999-11-04 22:56:02 +01:00
|
|
|
{
|
1999-11-05 00:14:30 +01:00
|
|
|
int status;
|
2003-03-20 07:43:35 +01:00
|
|
|
bool own_transaction;
|
1999-11-05 00:14:30 +01:00
|
|
|
|
2003-06-28 02:12:40 +02:00
|
|
|
if (!start_lo_xact("\\lo_export", &own_transaction))
|
1999-11-05 00:14:30 +01:00
|
|
|
return false;
|
1999-11-04 22:56:02 +01:00
|
|
|
|
2006-06-14 18:49:03 +02:00
|
|
|
SetCancelConn();
|
2001-10-05 00:39:34 +02:00
|
|
|
status = lo_export(pset.db, atooid(loid_arg), filename_arg);
|
2006-06-14 18:49:03 +02:00
|
|
|
ResetCancelConn();
|
|
|
|
|
|
|
|
/* of course this status is documented nowhere :( */
|
1999-11-05 00:14:30 +01:00
|
|
|
if (status != 1)
|
2006-06-14 18:49:03 +02:00
|
|
|
{
|
2000-01-14 23:18:03 +01:00
|
|
|
fputs(PQerrorMessage(pset.db), stderr);
|
2003-06-28 02:12:40 +02:00
|
|
|
return fail_lo_xact("\\lo_export", own_transaction);
|
1999-11-04 22:56:02 +01:00
|
|
|
}
|
1999-11-05 00:14:30 +01:00
|
|
|
|
2003-06-28 02:12:40 +02:00
|
|
|
if (!finish_lo_xact("\\lo_export", own_transaction))
|
|
|
|
return false;
|
1999-11-04 22:56:02 +01:00
|
|
|
|
2007-01-20 17:57:31 +01:00
|
|
|
print_lo_result("lo_export");
|
1999-11-04 22:56:02 +01:00
|
|
|
|
1999-11-05 00:14:30 +01:00
|
|
|
return true;
|
1999-11-04 22:56:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* do_lo_import()
|
|
|
|
*
|
|
|
|
* Copy large object from file to database
|
|
|
|
*/
|
|
|
|
bool
|
2000-01-14 23:18:03 +01:00
|
|
|
do_lo_import(const char *filename_arg, const char *comment_arg)
|
1999-11-04 22:56:02 +01:00
|
|
|
{
|
1999-11-05 00:14:30 +01:00
|
|
|
PGresult *res;
|
|
|
|
Oid loid;
|
2001-08-10 20:57:42 +02:00
|
|
|
char oidbuf[32];
|
2003-03-20 07:43:35 +01:00
|
|
|
bool own_transaction;
|
1999-11-05 00:14:30 +01:00
|
|
|
|
2003-06-28 02:12:40 +02:00
|
|
|
if (!start_lo_xact("\\lo_import", &own_transaction))
|
1999-11-05 00:14:30 +01:00
|
|
|
return false;
|
1999-11-04 22:56:02 +01:00
|
|
|
|
2006-06-14 18:49:03 +02:00
|
|
|
SetCancelConn();
|
2000-02-08 00:10:11 +01:00
|
|
|
loid = lo_import(pset.db, filename_arg);
|
2006-06-14 18:49:03 +02:00
|
|
|
ResetCancelConn();
|
|
|
|
|
1999-11-05 00:14:30 +01:00
|
|
|
if (loid == InvalidOid)
|
|
|
|
{
|
2000-01-14 23:18:03 +01:00
|
|
|
fputs(PQerrorMessage(pset.db), stderr);
|
2003-06-28 02:12:40 +02:00
|
|
|
return fail_lo_xact("\\lo_import", own_transaction);
|
1999-11-04 22:56:02 +01:00
|
|
|
}
|
|
|
|
|
1999-11-05 00:14:30 +01:00
|
|
|
/* insert description if given */
|
2003-11-21 23:32:49 +01:00
|
|
|
if (comment_arg)
|
1999-11-05 00:14:30 +01:00
|
|
|
{
|
2001-10-25 07:50:21 +02:00
|
|
|
char *cmdbuf;
|
|
|
|
char *bufptr;
|
2002-10-03 19:09:42 +02:00
|
|
|
size_t slen = strlen(comment_arg);
|
2001-08-10 20:57:42 +02:00
|
|
|
|
|
|
|
cmdbuf = malloc(slen * 2 + 256);
|
|
|
|
if (!cmdbuf)
|
2003-06-28 02:12:40 +02:00
|
|
|
return fail_lo_xact("\\lo_import", own_transaction);
|
2006-05-28 23:13:54 +02:00
|
|
|
sprintf(cmdbuf, "COMMENT ON LARGE OBJECT %u IS '", loid);
|
2001-08-10 20:57:42 +02:00
|
|
|
bufptr = cmdbuf + strlen(cmdbuf);
|
2006-05-28 23:13:54 +02:00
|
|
|
bufptr += PQescapeStringConn(pset.db, bufptr, comment_arg, slen, NULL);
|
2003-11-21 23:32:49 +01:00
|
|
|
strcpy(bufptr, "'");
|
2001-08-10 20:57:42 +02:00
|
|
|
|
2002-10-15 04:24:16 +02:00
|
|
|
if (!(res = PSQLexec(cmdbuf, false)))
|
2001-08-10 20:57:42 +02:00
|
|
|
{
|
|
|
|
free(cmdbuf);
|
2003-06-28 02:12:40 +02:00
|
|
|
return fail_lo_xact("\\lo_import", own_transaction);
|
2001-08-10 20:57:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
PQclear(res);
|
|
|
|
free(cmdbuf);
|
1999-11-05 00:14:30 +01:00
|
|
|
}
|
1999-11-04 22:56:02 +01:00
|
|
|
|
2003-06-28 02:12:40 +02:00
|
|
|
if (!finish_lo_xact("\\lo_import", own_transaction))
|
|
|
|
return false;
|
1999-11-05 00:14:30 +01:00
|
|
|
|
2007-01-20 17:57:31 +01:00
|
|
|
print_lo_result("lo_import %u", loid);
|
|
|
|
|
2001-08-10 20:57:42 +02:00
|
|
|
sprintf(oidbuf, "%u", loid);
|
|
|
|
SetVariable(pset.vars, "LASTOID", oidbuf);
|
1999-11-05 00:14:30 +01:00
|
|
|
|
|
|
|
return true;
|
1999-11-04 22:56:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* do_lo_unlink()
|
|
|
|
*
|
|
|
|
* removes a large object out of the database
|
|
|
|
*/
|
1999-11-05 00:14:30 +01:00
|
|
|
bool
|
2000-01-14 23:18:03 +01:00
|
|
|
do_lo_unlink(const char *loid_arg)
|
1999-11-04 22:56:02 +01:00
|
|
|
{
|
1999-11-05 00:14:30 +01:00
|
|
|
int status;
|
2001-10-05 00:39:34 +02:00
|
|
|
Oid loid = atooid(loid_arg);
|
2003-03-20 07:43:35 +01:00
|
|
|
bool own_transaction;
|
|
|
|
|
2003-06-28 02:12:40 +02:00
|
|
|
if (!start_lo_xact("\\lo_unlink", &own_transaction))
|
1999-11-05 00:14:30 +01:00
|
|
|
return false;
|
1999-11-04 22:56:02 +01:00
|
|
|
|
2006-06-14 18:49:03 +02:00
|
|
|
SetCancelConn();
|
2000-01-14 23:18:03 +01:00
|
|
|
status = lo_unlink(pset.db, loid);
|
2006-06-14 18:49:03 +02:00
|
|
|
ResetCancelConn();
|
|
|
|
|
1999-11-05 00:14:30 +01:00
|
|
|
if (status == -1)
|
|
|
|
{
|
2000-01-14 23:18:03 +01:00
|
|
|
fputs(PQerrorMessage(pset.db), stderr);
|
2003-06-28 02:12:40 +02:00
|
|
|
return fail_lo_xact("\\lo_unlink", own_transaction);
|
1999-11-04 22:56:02 +01:00
|
|
|
}
|
1999-11-05 00:14:30 +01:00
|
|
|
|
2003-06-28 02:12:40 +02:00
|
|
|
if (!finish_lo_xact("\\lo_unlink", own_transaction))
|
|
|
|
return false;
|
1999-11-04 22:56:02 +01:00
|
|
|
|
2007-01-20 17:57:31 +01:00
|
|
|
print_lo_result("lo_unlink %u", loid);
|
1999-11-04 22:56:02 +01:00
|
|
|
|
1999-11-05 00:14:30 +01:00
|
|
|
return true;
|
1999-11-04 22:56:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* do_lo_list()
|
|
|
|
*
|
2000-01-12 20:36:36 +01:00
|
|
|
* Show all large objects in database with comments
|
1999-11-04 22:56:02 +01:00
|
|
|
*/
|
1999-11-05 00:14:30 +01:00
|
|
|
bool
|
2000-01-14 23:18:03 +01:00
|
|
|
do_lo_list(void)
|
1999-11-04 22:56:02 +01:00
|
|
|
{
|
1999-11-05 00:14:30 +01:00
|
|
|
PGresult *res;
|
2000-01-12 20:36:36 +01:00
|
|
|
char buf[1024];
|
2000-01-14 23:18:03 +01:00
|
|
|
printQueryOpt myopt = pset.popt;
|
1999-11-05 00:14:30 +01:00
|
|
|
|
2001-06-02 20:25:18 +02:00
|
|
|
snprintf(buf, sizeof(buf),
|
2007-12-12 22:41:47 +01:00
|
|
|
"SELECT loid as \"%s\",\n"
|
2005-10-15 04:49:52 +02:00
|
|
|
" pg_catalog.obj_description(loid, 'pg_largeobject') as \"%s\"\n"
|
2004-09-10 06:10:53 +02:00
|
|
|
"FROM (SELECT DISTINCT loid FROM pg_catalog.pg_largeobject) x\n"
|
|
|
|
"ORDER BY 1",
|
2007-12-12 22:41:47 +01:00
|
|
|
gettext_noop("ID"),
|
|
|
|
gettext_noop("Description"));
|
1999-11-04 22:56:02 +01:00
|
|
|
|
2002-10-15 04:24:16 +02:00
|
|
|
res = PSQLexec(buf, false);
|
1999-11-05 00:14:30 +01:00
|
|
|
if (!res)
|
|
|
|
return false;
|
1999-11-04 22:56:02 +01:00
|
|
|
|
1999-11-05 00:14:30 +01:00
|
|
|
myopt.topt.tuples_only = false;
|
|
|
|
myopt.nullPrint = NULL;
|
2005-02-22 05:43:23 +01:00
|
|
|
myopt.title = _("Large objects");
|
2008-07-15 00:00:04 +02:00
|
|
|
myopt.translate_header = true;
|
1999-11-04 22:56:02 +01:00
|
|
|
|
2005-06-14 04:57:45 +02:00
|
|
|
printQuery(res, &myopt, pset.queryFout, pset.logfile);
|
1999-11-04 22:56:02 +01:00
|
|
|
|
1999-11-05 00:14:30 +01:00
|
|
|
PQclear(res);
|
|
|
|
return true;
|
1999-11-04 22:56:02 +01:00
|
|
|
}
|