1999-11-04 22:56:02 +01:00
|
|
|
#include <config.h>
|
|
|
|
#include <c.h>
|
|
|
|
#include "large_obj.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <libpq-fe.h>
|
|
|
|
#include <postgres.h>
|
|
|
|
|
|
|
|
#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
|
1999-11-05 00:14:30 +01:00
|
|
|
handle_transaction(PsqlSettings *pset)
|
1999-11-04 22:56:02 +01:00
|
|
|
{
|
1999-11-05 00:14:30 +01:00
|
|
|
const char *var = GetVariable(pset->vars, "lo_transaction");
|
|
|
|
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';
|
|
|
|
old_notice_hook = PQsetNoticeProcessor(pset->db, _my_notice_handler, NULL);
|
|
|
|
|
|
|
|
res = PSQLexec(pset, commit ? "COMMIT" : "ROLLBACK");
|
|
|
|
if (!res)
|
|
|
|
return false;
|
1999-11-04 22:56:02 +01:00
|
|
|
|
1999-11-05 00:14:30 +01:00
|
|
|
if (notice[0])
|
|
|
|
{
|
|
|
|
if ((!commit && strcmp(notice, "NOTICE: UserAbortTransactionBlock and not in in-progress state\n") != 0) ||
|
|
|
|
(commit && strcmp(notice, "NOTICE: EndTransactionBlock and not inprogress/abort state\n") != 0))
|
|
|
|
fputs(notice, stderr);
|
|
|
|
}
|
|
|
|
else if (!GetVariableBool(pset->vars, "quiet"))
|
|
|
|
{
|
|
|
|
if (commit)
|
|
|
|
puts("Warning: Your transaction in progress has been committed.");
|
|
|
|
else
|
|
|
|
puts("Warning: Your transaction in progress has been rolled back.");
|
|
|
|
}
|
|
|
|
|
|
|
|
PQsetNoticeProcessor(pset->db, old_notice_hook, NULL);
|
|
|
|
return true;
|
1999-11-04 22:56:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* do_lo_export()
|
|
|
|
*
|
|
|
|
* Write a large object to a file
|
|
|
|
*/
|
|
|
|
bool
|
1999-11-05 00:14:30 +01:00
|
|
|
do_lo_export(PsqlSettings *pset, 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;
|
|
|
|
const char *var = GetVariable(pset->vars, "lo_transaction");
|
|
|
|
|
|
|
|
if (var && strcmp(var, "nothing") == 0)
|
|
|
|
own_transaction = false;
|
|
|
|
|
|
|
|
if (!pset->db)
|
|
|
|
{
|
|
|
|
fputs("You are not connected to a database.\n", stderr);
|
|
|
|
return false;
|
|
|
|
}
|
1999-11-04 22:56:02 +01:00
|
|
|
|
1999-11-05 00:14:30 +01:00
|
|
|
if (own_transaction)
|
|
|
|
{
|
|
|
|
if (!handle_transaction(pset))
|
|
|
|
return false;
|
1999-11-04 22:56:02 +01:00
|
|
|
|
1999-11-05 00:14:30 +01:00
|
|
|
if (!(res = PSQLexec(pset, "BEGIN")))
|
|
|
|
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
|
|
|
status = lo_export(pset->db, atol(loid_arg), (char *) 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;
|
1999-11-04 22:56:02 +01:00
|
|
|
}
|
1999-11-05 00:14:30 +01:00
|
|
|
|
|
|
|
if (own_transaction)
|
|
|
|
{
|
|
|
|
if (!(res = PSQLexec(pset, "COMMIT")))
|
|
|
|
{
|
|
|
|
res = PQexec(pset->db, "ROLLBACK");
|
|
|
|
PQclear(res);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
PQclear(res);
|
1999-11-04 22:56:02 +01:00
|
|
|
}
|
|
|
|
|
1999-11-05 00:14:30 +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
|
1999-11-05 00:14:30 +01:00
|
|
|
do_lo_import(PsqlSettings *pset, 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;
|
|
|
|
const char *var = GetVariable(pset->vars, "lo_transaction");
|
|
|
|
|
|
|
|
if (var && strcmp(var, "nothing") == 0)
|
|
|
|
own_transaction = false;
|
|
|
|
|
|
|
|
if (!pset->db)
|
|
|
|
{
|
|
|
|
fputs("You are not connected to a database.\n", stderr);
|
|
|
|
return false;
|
|
|
|
}
|
1999-11-04 22:56:02 +01:00
|
|
|
|
1999-11-05 00:14:30 +01:00
|
|
|
if (own_transaction)
|
|
|
|
{
|
|
|
|
if (!handle_transaction(pset))
|
|
|
|
return false;
|
1999-11-04 22:56:02 +01:00
|
|
|
|
1999-11-05 00:14:30 +01:00
|
|
|
if (!(res = PSQLexec(pset, "BEGIN")))
|
|
|
|
return false;
|
1999-11-04 22:56:02 +01:00
|
|
|
|
|
|
|
PQclear(res);
|
|
|
|
}
|
|
|
|
|
1999-11-05 00:14:30 +01:00
|
|
|
loid = lo_import(pset->db, (char *) filename_arg);
|
|
|
|
if (loid == InvalidOid)
|
|
|
|
{
|
|
|
|
fputs(PQerrorMessage(pset->db), stderr);
|
|
|
|
if (own_transaction)
|
|
|
|
{
|
|
|
|
res = PQexec(pset->db, "ROLLBACK");
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
sprintf(buf, "INSERT INTO pg_description VALUES (%d, '", loid);
|
|
|
|
for (i = 0; i < strlen(comment_arg); i++)
|
|
|
|
if (comment_arg[i] == '\'')
|
|
|
|
strcat(buf, "\\'");
|
|
|
|
else
|
|
|
|
strncat(buf, &comment_arg[i], 1);
|
|
|
|
strcat(buf, "')");
|
|
|
|
|
|
|
|
if (!(res = PSQLexec(pset, buf)))
|
|
|
|
{
|
|
|
|
if (own_transaction)
|
|
|
|
{
|
|
|
|
res = PQexec(pset->db, "ROLLBACK");
|
|
|
|
PQclear(res);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
1999-11-04 22:56:02 +01:00
|
|
|
|
1999-11-05 00:14:30 +01:00
|
|
|
if (own_transaction)
|
|
|
|
{
|
|
|
|
if (!(res = PSQLexec(pset, "COMMIT")))
|
|
|
|
{
|
|
|
|
res = PQexec(pset->db, "ROLLBACK");
|
|
|
|
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
|
|
|
|
|
|
|
fprintf(pset->queryFout, "lo_import %d\n", loid);
|
* Includes tab completion. It's not magic, but it's very cool. At any
rate
it's better than what used to be there.
* Does proper SQL "host variable" substitution as pointed out by Andreas
Zeugwetter (thanks): select * from :foo; Also some changes in how ':'
and ';' are treated (escape with \ to send to backend). This does
_not_
affect the '::' cast operator, but perhaps others that contain : or ;
(but there are none right now).
* To show description with a <something> listing, append '?' to command
name, e.g., \df?. This seemed to be the convenient and logical
solution.
Or append a '+' to see more useless information, e.g., \df+.
* Fixed fflush()'ing bug pointed out by Jan during the regression test
discussion.
* Added LastOid variable. This ought to take care of TODO item "Add a
function to return the last inserted oid, for use in psql scripts"
(under CLIENTS)
E.g.,
insert into foo values(...);
insert into bar values(..., :LastOid);
\echo $LastOid
* \d command shows constraints, rules, and triggers defined on the table
(in addition to indices)
* Various fixes, optimizations, corrections
* Documentation update as well
Note: This now requires snprintf(), which, if necessary, is taken from
src/backend/port. This is certainly a little weird, but it should
suffice
until a source tree cleanup is done.
Enjoy.
--
Peter Eisentraut Sernanders väg 10:115
1999-11-26 05:24:17 +01:00
|
|
|
pset->lastOid = loid;
|
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
|
|
|
|
do_lo_unlink(PsqlSettings *pset, 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;
|
|
|
|
const char *var = GetVariable(pset->vars, "lo_transaction");
|
|
|
|
|
|
|
|
if (var && strcmp(var, "nothing") == 0)
|
|
|
|
own_transaction = false;
|
|
|
|
|
|
|
|
if (!pset->db)
|
|
|
|
{
|
|
|
|
fputs("You are not connected to a database.\n", stderr);
|
|
|
|
return false;
|
|
|
|
}
|
1999-11-04 22:56:02 +01:00
|
|
|
|
1999-11-05 00:14:30 +01:00
|
|
|
if (own_transaction)
|
|
|
|
{
|
|
|
|
if (!handle_transaction(pset))
|
|
|
|
return false;
|
1999-11-04 22:56:02 +01:00
|
|
|
|
1999-11-05 00:14:30 +01:00
|
|
|
if (!(res = PSQLexec(pset, "BEGIN")))
|
|
|
|
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
|
|
|
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;
|
1999-11-04 22:56:02 +01:00
|
|
|
}
|
1999-11-05 00:14:30 +01:00
|
|
|
|
|
|
|
/* remove the comment as well */
|
|
|
|
sprintf(buf, "DELETE FROM pg_description WHERE objoid = %d", loid);
|
|
|
|
if (!(res = PSQLexec(pset, buf)))
|
|
|
|
{
|
|
|
|
if (own_transaction)
|
|
|
|
{
|
|
|
|
res = PQexec(pset->db, "ROLLBACK");
|
|
|
|
PQclear(res);
|
|
|
|
}
|
|
|
|
return false;
|
1999-11-04 22:56:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-05 00:14:30 +01:00
|
|
|
if (own_transaction)
|
|
|
|
{
|
|
|
|
if (!(res = PSQLexec(pset, "COMMIT")))
|
|
|
|
{
|
|
|
|
res = PQexec(pset->db, "ROLLBACK");
|
|
|
|
PQclear(res);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
PQclear(res);
|
1999-11-04 22:56:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-05 00:14:30 +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()
|
|
|
|
*
|
|
|
|
* Show all large objects in database, with comments if desired
|
|
|
|
*/
|
1999-11-05 00:14:30 +01:00
|
|
|
bool
|
* Includes tab completion. It's not magic, but it's very cool. At any
rate
it's better than what used to be there.
* Does proper SQL "host variable" substitution as pointed out by Andreas
Zeugwetter (thanks): select * from :foo; Also some changes in how ':'
and ';' are treated (escape with \ to send to backend). This does
_not_
affect the '::' cast operator, but perhaps others that contain : or ;
(but there are none right now).
* To show description with a <something> listing, append '?' to command
name, e.g., \df?. This seemed to be the convenient and logical
solution.
Or append a '+' to see more useless information, e.g., \df+.
* Fixed fflush()'ing bug pointed out by Jan during the regression test
discussion.
* Added LastOid variable. This ought to take care of TODO item "Add a
function to return the last inserted oid, for use in psql scripts"
(under CLIENTS)
E.g.,
insert into foo values(...);
insert into bar values(..., :LastOid);
\echo $LastOid
* \d command shows constraints, rules, and triggers defined on the table
(in addition to indices)
* Various fixes, optimizations, corrections
* Documentation update as well
Note: This now requires snprintf(), which, if necessary, is taken from
src/backend/port. This is certainly a little weird, but it should
suffice
until a source tree cleanup is done.
Enjoy.
--
Peter Eisentraut Sernanders väg 10:115
1999-11-26 05:24:17 +01:00
|
|
|
do_lo_list(PsqlSettings *pset, bool desc)
|
1999-11-04 22:56:02 +01:00
|
|
|
{
|
1999-11-05 00:14:30 +01:00
|
|
|
PGresult *res;
|
* Includes tab completion. It's not magic, but it's very cool. At any
rate
it's better than what used to be there.
* Does proper SQL "host variable" substitution as pointed out by Andreas
Zeugwetter (thanks): select * from :foo; Also some changes in how ':'
and ';' are treated (escape with \ to send to backend). This does
_not_
affect the '::' cast operator, but perhaps others that contain : or ;
(but there are none right now).
* To show description with a <something> listing, append '?' to command
name, e.g., \df?. This seemed to be the convenient and logical
solution.
Or append a '+' to see more useless information, e.g., \df+.
* Fixed fflush()'ing bug pointed out by Jan during the regression test
discussion.
* Added LastOid variable. This ought to take care of TODO item "Add a
function to return the last inserted oid, for use in psql scripts"
(under CLIENTS)
E.g.,
insert into foo values(...);
insert into bar values(..., :LastOid);
\echo $LastOid
* \d command shows constraints, rules, and triggers defined on the table
(in addition to indices)
* Various fixes, optimizations, corrections
* Documentation update as well
Note: This now requires snprintf(), which, if necessary, is taken from
src/backend/port. This is certainly a little weird, but it should
suffice
until a source tree cleanup is done.
Enjoy.
--
Peter Eisentraut Sernanders väg 10:115
1999-11-26 05:24:17 +01:00
|
|
|
char buf[512];
|
1999-11-05 00:14:30 +01:00
|
|
|
printQueryOpt myopt = pset->popt;
|
|
|
|
|
* Includes tab completion. It's not magic, but it's very cool. At any
rate
it's better than what used to be there.
* Does proper SQL "host variable" substitution as pointed out by Andreas
Zeugwetter (thanks): select * from :foo; Also some changes in how ':'
and ';' are treated (escape with \ to send to backend). This does
_not_
affect the '::' cast operator, but perhaps others that contain : or ;
(but there are none right now).
* To show description with a <something> listing, append '?' to command
name, e.g., \df?. This seemed to be the convenient and logical
solution.
Or append a '+' to see more useless information, e.g., \df+.
* Fixed fflush()'ing bug pointed out by Jan during the regression test
discussion.
* Added LastOid variable. This ought to take care of TODO item "Add a
function to return the last inserted oid, for use in psql scripts"
(under CLIENTS)
E.g.,
insert into foo values(...);
insert into bar values(..., :LastOid);
\echo $LastOid
* \d command shows constraints, rules, and triggers defined on the table
(in addition to indices)
* Various fixes, optimizations, corrections
* Documentation update as well
Note: This now requires snprintf(), which, if necessary, is taken from
src/backend/port. This is certainly a little weird, but it should
suffice
until a source tree cleanup is done.
Enjoy.
--
Peter Eisentraut Sernanders väg 10:115
1999-11-26 05:24:17 +01:00
|
|
|
strcpy(buf, "SELECT usename as \"Owner\", substring(relname from 5) as \"ID\"");
|
|
|
|
if (desc)
|
|
|
|
strcat(buf, ",\n obj_description(pg_class.oid) as \"Description\"");
|
|
|
|
strcat(buf, "\nFROM pg_class, pg_user\n"
|
1999-11-04 22:56:02 +01:00
|
|
|
"WHERE usesysid = relowner AND relkind = 'l'\n"
|
|
|
|
"ORDER BY \"ID\"");
|
|
|
|
|
* Includes tab completion. It's not magic, but it's very cool. At any
rate
it's better than what used to be there.
* Does proper SQL "host variable" substitution as pointed out by Andreas
Zeugwetter (thanks): select * from :foo; Also some changes in how ':'
and ';' are treated (escape with \ to send to backend). This does
_not_
affect the '::' cast operator, but perhaps others that contain : or ;
(but there are none right now).
* To show description with a <something> listing, append '?' to command
name, e.g., \df?. This seemed to be the convenient and logical
solution.
Or append a '+' to see more useless information, e.g., \df+.
* Fixed fflush()'ing bug pointed out by Jan during the regression test
discussion.
* Added LastOid variable. This ought to take care of TODO item "Add a
function to return the last inserted oid, for use in psql scripts"
(under CLIENTS)
E.g.,
insert into foo values(...);
insert into bar values(..., :LastOid);
\echo $LastOid
* \d command shows constraints, rules, and triggers defined on the table
(in addition to indices)
* Various fixes, optimizations, corrections
* Documentation update as well
Note: This now requires snprintf(), which, if necessary, is taken from
src/backend/port. This is certainly a little weird, but it should
suffice
until a source tree cleanup is done.
Enjoy.
--
Peter Eisentraut Sernanders väg 10:115
1999-11-26 05:24:17 +01:00
|
|
|
res = PSQLexec(pset, 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
|
|
|
|
1999-11-05 00:14:30 +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
|
|
|
}
|