2000-01-19 00:30:24 +01:00
|
|
|
/*
|
|
|
|
* psql - the PostgreSQL interactive terminal
|
|
|
|
*
|
2000-01-29 17:58:54 +01:00
|
|
|
* Copyright 2000 by PostgreSQL Global Development Group
|
2000-01-19 00:30:24 +01:00
|
|
|
*
|
2001-03-22 05:01:46 +01:00
|
|
|
* $Header: /cvsroot/pgsql/src/bin/psql/large_obj.c,v 1.14 2001/03/22 04:00:20 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
|
|
|
/*
|
1999-11-04 22:56:02 +01:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static char notice[80];
|
|
|
|
|
|
|
|
static void
|
1999-11-05 00:14:30 +01:00
|
|
|
_my_notice_handler(void *arg, const char *message)
|
|
|
|
{
|
|
|
|
(void) arg;
|
|
|
|
strncpy(notice, message, 79);
|
|
|
|
notice[79] = '\0';
|
1999-11-04 22:56:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static bool
|
2000-01-14 23:18:03 +01:00
|
|
|
handle_transaction(void)
|
1999-11-04 22:56:02 +01:00
|
|
|
{
|
2000-01-14 23:18:03 +01:00
|
|
|
const char *var = GetVariable(pset.vars, "LO_TRANSACTION");
|
1999-11-05 00:14:30 +01:00
|
|
|
PGresult *res;
|
|
|
|
bool commit;
|
|
|
|
PQnoticeProcessor old_notice_hook;
|
1999-11-04 22:56:02 +01:00
|
|
|
|
1999-11-05 00:14:30 +01:00
|
|
|
if (var && strcmp(var, "nothing") == 0)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
commit = (var && strcmp(var, "commit") == 0);
|
|
|
|
|
|
|
|
notice[0] = '\0';
|
2000-01-14 23:18:03 +01:00
|
|
|
old_notice_hook = PQsetNoticeProcessor(pset.db, _my_notice_handler, NULL);
|
1999-11-05 00:14:30 +01:00
|
|
|
|
2000-01-14 23:18:03 +01:00
|
|
|
res = PSQLexec(commit ? "COMMIT" : "ROLLBACK");
|
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
|
|
|
if (notice[0])
|
|
|
|
{
|
2000-01-29 17:58:54 +01:00
|
|
|
if ((!commit && strcmp(notice, "NOTICE: ROLLBACK: no transaction in progress\n") != 0) ||
|
|
|
|
(commit && strcmp(notice, "NOTICE: COMMIT: no transaction in progress\n") != 0))
|
1999-11-05 00:14:30 +01:00
|
|
|
fputs(notice, stderr);
|
|
|
|
}
|
2000-01-14 23:18:03 +01:00
|
|
|
else if (!QUIET())
|
1999-11-05 00:14:30 +01:00
|
|
|
{
|
|
|
|
if (commit)
|
|
|
|
puts("Warning: Your transaction in progress has been committed.");
|
|
|
|
else
|
|
|
|
puts("Warning: Your transaction in progress has been rolled back.");
|
|
|
|
}
|
|
|
|
|
2000-01-14 23:18:03 +01:00
|
|
|
PQsetNoticeProcessor(pset.db, old_notice_hook, NULL);
|
1999-11-05 00:14:30 +01:00
|
|
|
return true;
|
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
|
|
|
PGresult *res;
|
|
|
|
int status;
|
|
|
|
bool own_transaction = true;
|
2000-01-14 23:18:03 +01:00
|
|
|
const char *var = GetVariable(pset.vars, "LO_TRANSACTION");
|
1999-11-05 00:14:30 +01:00
|
|
|
|
|
|
|
if (var && strcmp(var, "nothing") == 0)
|
|
|
|
own_transaction = false;
|
|
|
|
|
2000-01-14 23:18:03 +01:00
|
|
|
if (!pset.db)
|
1999-11-05 00:14:30 +01:00
|
|
|
{
|
2000-04-12 19:17:23 +02:00
|
|
|
if (!pset.cur_cmd_interactive)
|
|
|
|
fprintf(stderr, "%s: ", pset.progname);
|
2000-01-12 20:36:36 +01:00
|
|
|
fputs("\\lo_export: not connected to a database\n", stderr);
|
1999-11-05 00:14:30 +01:00
|
|
|
return false;
|
|
|
|
}
|
1999-11-04 22:56:02 +01:00
|
|
|
|
1999-11-05 00:14:30 +01:00
|
|
|
if (own_transaction)
|
|
|
|
{
|
2000-01-14 23:18:03 +01:00
|
|
|
if (!handle_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
|
|
|
if (!(res = PSQLexec("BEGIN")))
|
1999-11-05 00:14:30 +01:00
|
|
|
return false;
|
1999-11-04 22:56:02 +01:00
|
|
|
|
1999-11-05 00:14:30 +01:00
|
|
|
PQclear(res);
|
|
|
|
}
|
1999-11-04 22:56:02 +01:00
|
|
|
|
2000-02-08 00:10:11 +01:00
|
|
|
status = lo_export(pset.db, atol(loid_arg), filename_arg);
|
1999-11-05 00:14:30 +01:00
|
|
|
if (status != 1)
|
|
|
|
{ /* of course this status is documented
|
|
|
|
* nowhere :( */
|
2000-01-14 23:18:03 +01:00
|
|
|
fputs(PQerrorMessage(pset.db), stderr);
|
1999-11-05 00:14:30 +01:00
|
|
|
if (own_transaction)
|
|
|
|
{
|
2000-01-14 23:18:03 +01:00
|
|
|
res = PQexec(pset.db, "ROLLBACK");
|
1999-11-05 00:14:30 +01:00
|
|
|
PQclear(res);
|
|
|
|
}
|
|
|
|
return false;
|
1999-11-04 22:56:02 +01:00
|
|
|
}
|
1999-11-05 00:14:30 +01:00
|
|
|
|
|
|
|
if (own_transaction)
|
|
|
|
{
|
2000-01-14 23:18:03 +01:00
|
|
|
if (!(res = PSQLexec("COMMIT")))
|
1999-11-05 00:14:30 +01:00
|
|
|
{
|
2000-01-14 23:18:03 +01:00
|
|
|
res = PQexec(pset.db, "ROLLBACK");
|
1999-11-05 00:14:30 +01:00
|
|
|
PQclear(res);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
PQclear(res);
|
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;
|
|
|
|
char buf[1024];
|
|
|
|
unsigned int i;
|
|
|
|
bool own_transaction = true;
|
2000-01-14 23:18:03 +01:00
|
|
|
const char *var = GetVariable(pset.vars, "LO_TRANSACTION");
|
1999-11-05 00:14:30 +01:00
|
|
|
|
|
|
|
if (var && strcmp(var, "nothing") == 0)
|
|
|
|
own_transaction = false;
|
|
|
|
|
2000-01-14 23:18:03 +01:00
|
|
|
if (!pset.db)
|
1999-11-05 00:14:30 +01:00
|
|
|
{
|
2000-04-12 19:17:23 +02:00
|
|
|
if (!pset.cur_cmd_interactive)
|
|
|
|
fprintf(stderr, "%s: ", pset.progname);
|
2000-01-12 20:36:36 +01:00
|
|
|
fputs("\\lo_import: not connected to a database\n", stderr);
|
1999-11-05 00:14:30 +01:00
|
|
|
return false;
|
|
|
|
}
|
1999-11-04 22:56:02 +01:00
|
|
|
|
1999-11-05 00:14:30 +01:00
|
|
|
if (own_transaction)
|
|
|
|
{
|
2000-01-14 23:18:03 +01:00
|
|
|
if (!handle_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
|
|
|
if (!(res = PSQLexec("BEGIN")))
|
1999-11-05 00:14:30 +01:00
|
|
|
return false;
|
1999-11-04 22:56:02 +01:00
|
|
|
|
|
|
|
PQclear(res);
|
|
|
|
}
|
|
|
|
|
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);
|
1999-11-05 00:14:30 +01:00
|
|
|
if (own_transaction)
|
|
|
|
{
|
2000-01-14 23:18:03 +01:00
|
|
|
res = PQexec(pset.db, "ROLLBACK");
|
1999-11-05 00:14:30 +01:00
|
|
|
PQclear(res);
|
|
|
|
}
|
|
|
|
return false;
|
1999-11-04 22:56:02 +01:00
|
|
|
}
|
|
|
|
|
1999-11-05 00:14:30 +01:00
|
|
|
/* insert description if given */
|
|
|
|
if (comment_arg)
|
|
|
|
{
|
2000-10-24 03:38:44 +02:00
|
|
|
sprintf(buf, "INSERT INTO pg_description VALUES (%u, '", loid);
|
1999-11-05 00:14:30 +01:00
|
|
|
for (i = 0; i < strlen(comment_arg); i++)
|
|
|
|
if (comment_arg[i] == '\'')
|
|
|
|
strcat(buf, "\\'");
|
|
|
|
else
|
|
|
|
strncat(buf, &comment_arg[i], 1);
|
|
|
|
strcat(buf, "')");
|
|
|
|
|
2000-01-14 23:18:03 +01:00
|
|
|
if (!(res = PSQLexec(buf)))
|
1999-11-05 00:14:30 +01:00
|
|
|
{
|
|
|
|
if (own_transaction)
|
|
|
|
{
|
2000-01-14 23:18:03 +01:00
|
|
|
res = PQexec(pset.db, "ROLLBACK");
|
1999-11-05 00:14:30 +01:00
|
|
|
PQclear(res);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
1999-11-04 22:56:02 +01:00
|
|
|
|
1999-11-05 00:14:30 +01:00
|
|
|
if (own_transaction)
|
|
|
|
{
|
2000-01-14 23:18:03 +01:00
|
|
|
if (!(res = PSQLexec("COMMIT")))
|
1999-11-05 00:14:30 +01:00
|
|
|
{
|
2000-01-14 23:18:03 +01:00
|
|
|
res = PQexec(pset.db, "ROLLBACK");
|
1999-11-05 00:14:30 +01:00
|
|
|
PQclear(res);
|
|
|
|
return false;
|
|
|
|
}
|
1999-11-04 22:56:02 +01:00
|
|
|
|
1999-11-05 00:14:30 +01:00
|
|
|
PQclear(res);
|
|
|
|
}
|
1999-11-04 22:56:02 +01:00
|
|
|
|
1999-11-05 00:14:30 +01:00
|
|
|
|
2000-01-14 23:18:03 +01:00
|
|
|
fprintf(pset.queryFout, "lo_import %d\n", loid);
|
2000-04-12 19:17:23 +02:00
|
|
|
sprintf(buf, "%u", (unsigned int) loid);
|
|
|
|
SetVariable(pset.vars, "LASTOID", buf);
|
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
|
|
|
PGresult *res;
|
|
|
|
int status;
|
|
|
|
Oid loid = (Oid) atol(loid_arg);
|
|
|
|
char buf[256];
|
|
|
|
bool own_transaction = true;
|
2000-01-14 23:18:03 +01:00
|
|
|
const char *var = GetVariable(pset.vars, "LO_TRANSACTION");
|
1999-11-05 00:14:30 +01:00
|
|
|
|
|
|
|
if (var && strcmp(var, "nothing") == 0)
|
|
|
|
own_transaction = false;
|
|
|
|
|
2000-01-14 23:18:03 +01:00
|
|
|
if (!pset.db)
|
1999-11-05 00:14:30 +01:00
|
|
|
{
|
2000-04-12 19:17:23 +02:00
|
|
|
if (!pset.cur_cmd_interactive)
|
|
|
|
fprintf(stderr, "%s: ", pset.progname);
|
2000-01-12 20:36:36 +01:00
|
|
|
fputs("\\lo_unlink: not connected to a database\n", stderr);
|
1999-11-05 00:14:30 +01:00
|
|
|
return false;
|
|
|
|
}
|
1999-11-04 22:56:02 +01:00
|
|
|
|
1999-11-05 00:14:30 +01:00
|
|
|
if (own_transaction)
|
|
|
|
{
|
2000-01-14 23:18:03 +01:00
|
|
|
if (!handle_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
|
|
|
if (!(res = PSQLexec("BEGIN")))
|
1999-11-05 00:14:30 +01:00
|
|
|
return false;
|
1999-11-04 22:56:02 +01:00
|
|
|
|
1999-11-05 00:14:30 +01:00
|
|
|
PQclear(res);
|
|
|
|
}
|
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);
|
1999-11-05 00:14:30 +01:00
|
|
|
if (own_transaction)
|
|
|
|
{
|
2000-01-14 23:18:03 +01:00
|
|
|
res = PQexec(pset.db, "ROLLBACK");
|
1999-11-05 00:14:30 +01:00
|
|
|
PQclear(res);
|
|
|
|
}
|
|
|
|
return false;
|
1999-11-04 22:56:02 +01:00
|
|
|
}
|
1999-11-05 00:14:30 +01:00
|
|
|
|
|
|
|
/* remove the comment as well */
|
2000-10-24 03:38:44 +02:00
|
|
|
sprintf(buf, "DELETE FROM pg_description WHERE objoid = %u", loid);
|
2000-01-14 23:18:03 +01:00
|
|
|
if (!(res = PSQLexec(buf)))
|
1999-11-05 00:14:30 +01:00
|
|
|
{
|
|
|
|
if (own_transaction)
|
|
|
|
{
|
2000-01-14 23:18:03 +01:00
|
|
|
res = PQexec(pset.db, "ROLLBACK");
|
1999-11-05 00:14:30 +01:00
|
|
|
PQclear(res);
|
|
|
|
}
|
|
|
|
return false;
|
1999-11-04 22:56:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-05 00:14:30 +01:00
|
|
|
if (own_transaction)
|
|
|
|
{
|
2000-01-14 23:18:03 +01:00
|
|
|
if (!(res = PSQLexec("COMMIT")))
|
1999-11-05 00:14:30 +01:00
|
|
|
{
|
2000-01-14 23:18:03 +01:00
|
|
|
res = PQexec(pset.db, "ROLLBACK");
|
1999-11-05 00:14:30 +01:00
|
|
|
PQclear(res);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
PQclear(res);
|
1999-11-04 22:56:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-01-14 23:18:03 +01:00
|
|
|
fprintf(pset.queryFout, "lo_unlink %d\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
|
|
|
|
2000-01-12 20:36:36 +01:00
|
|
|
strcpy(buf,
|
2001-03-22 05:01:46 +01:00
|
|
|
"SELECT loid as \"ID\", obj_description(loid) as \"Description\"\n"
|
2000-10-24 06:13:28 +02:00
|
|
|
"FROM (SELECT DISTINCT loid FROM pg_largeobject) x\n"
|
1999-11-04 22:56:02 +01:00
|
|
|
"ORDER BY \"ID\"");
|
|
|
|
|
2000-01-14 23:18:03 +01:00
|
|
|
res = PSQLexec(buf);
|
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;
|
|
|
|
myopt.title = "Large objects";
|
1999-11-04 22:56:02 +01:00
|
|
|
|
2000-01-14 23:18:03 +01:00
|
|
|
printQuery(res, &myopt, pset.queryFout);
|
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
|
|
|
}
|