2000-01-19 00:30:24 +01:00
|
|
|
/*
|
|
|
|
* psql - the PostgreSQL interactive terminal
|
|
|
|
*
|
2006-03-05 16:59:11 +01:00
|
|
|
* Copyright (c) 2000-2006, PostgreSQL Global Development Group
|
2000-01-19 00:30:24 +01:00
|
|
|
*
|
2006-05-27 01:48:54 +02:00
|
|
|
* $PostgreSQL: pgsql/src/bin/psql/large_obj.c,v 1.42 2006/05/26 23:48:54 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"
|
|
|
|
|
2000-02-16 14:15:26 +01:00
|
|
|
#include "libpq-fe.h"
|
1999-11-04 22:56:02 +01:00
|
|
|
|
|
|
|
#include "settings.h"
|
|
|
|
#include "variables.h"
|
|
|
|
#include "common.h"
|
|
|
|
#include "print.h"
|
|
|
|
|
|
|
|
|
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
|
|
|
|
2003-06-28 02:12:40 +02:00
|
|
|
if (own_transaction &&
|
|
|
|
GetVariableBool(pset.vars, "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
|
|
|
|
2003-06-28 02:12:40 +02:00
|
|
|
if (own_transaction &&
|
|
|
|
GetVariableBool(pset.vars, "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
|
|
|
|
2001-10-05 00:39:34 +02:00
|
|
|
status = lo_export(pset.db, atooid(loid_arg), filename_arg);
|
1999-11-05 00:14:30 +01:00
|
|
|
if (status != 1)
|
2005-10-15 04:49:52 +02:00
|
|
|
{ /* of course this status is documented nowhere
|
|
|
|
* :( */
|
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
|
|
|
|
2000-01-14 23:18:03 +01:00
|
|
|
fprintf(pset.queryFout, "lo_export\n");
|
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];
|
1999-11-05 00:14:30 +01:00
|
|
|
unsigned int i;
|
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
|
|
|
|
2000-02-08 00:10:11 +01:00
|
|
|
loid = lo_import(pset.db, filename_arg);
|
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);
|
2001-08-10 20:57:42 +02:00
|
|
|
sprintf(cmdbuf,
|
2005-07-02 19:01:59 +02:00
|
|
|
"COMMENT ON LARGE OBJECT %u IS ",
|
2002-08-10 05:56:24 +02:00
|
|
|
loid);
|
2001-08-10 20:57:42 +02:00
|
|
|
bufptr = cmdbuf + strlen(cmdbuf);
|
2005-07-02 19:01:59 +02:00
|
|
|
|
|
|
|
if (strchr(comment_arg, '\\') != NULL)
|
|
|
|
*bufptr++ = ESCAPE_STRING_SYNTAX;
|
2006-05-27 01:48:54 +02:00
|
|
|
|
2005-07-02 19:01:59 +02:00
|
|
|
*bufptr++ = '\'';
|
2001-08-10 20:57:42 +02:00
|
|
|
for (i = 0; i < slen; i++)
|
|
|
|
{
|
2006-05-27 01:48:54 +02:00
|
|
|
if (SQL_STR_DOUBLE(comment_arg[i], true))
|
2005-07-02 19:01:59 +02:00
|
|
|
*bufptr++ = comment_arg[i];
|
2001-08-10 20:57:42 +02:00
|
|
|
*bufptr++ = comment_arg[i];
|
|
|
|
}
|
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
|
|
|
|
2001-08-10 20:57:42 +02:00
|
|
|
fprintf(pset.queryFout, "lo_import %u\n", loid);
|
|
|
|
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
|
|
|
|
2000-01-14 23:18:03 +01:00
|
|
|
status = lo_unlink(pset.db, loid);
|
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
|
|
|
|
2001-10-05 00:39:34 +02:00
|
|
|
fprintf(pset.queryFout, "lo_unlink %u\n", 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),
|
2004-09-10 06:10:53 +02:00
|
|
|
"SELECT loid as \"ID\",\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",
|
2005-02-22 05:43:23 +01:00
|
|
|
_("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");
|
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
|
|
|
}
|