postgresql/contrib/pg_dumplo/lo_import.c

101 lines
2.1 KiB
C
Raw Normal View History

/* -------------------------------------------------------------------------
* pg_dumplo
*
* $Header: /cvsroot/pgsql/contrib/pg_dumplo/Attic/lo_import.c,v 1.9 2002/09/05 21:01:16 tgl Exp $
*
* Karel Zak 1999-2000
* -------------------------------------------------------------------------
*/
2000-06-16 03:05:22 +08:00
#include "postgres_fe.h"
2000-06-16 03:05:22 +08:00
#include <fcntl.h>
2001-03-22 12:01:46 +08:00
#include <errno.h>
2000-06-16 03:05:22 +08:00
#include <time.h>
#include <unistd.h>
#include <sys/stat.h>
2000-06-16 03:05:22 +08:00
#include "libpq-fe.h"
#include "libpq/libpq-fs.h"
2000-06-16 03:05:22 +08:00
#include "pg_dumplo.h"
2001-03-22 12:01:46 +08:00
extern int errno;
2000-06-16 03:05:22 +08:00
2001-03-22 12:01:46 +08:00
void
pglo_import(LODumpMaster * pgLO)
2000-06-16 03:05:22 +08:00
{
LOlist loa;
2001-03-22 12:01:46 +08:00
Oid new_oid;
char tab[MAX_TABLE_NAME],
attr[MAX_ATTR_NAME],
path[BUFSIZ],
lo_path[BUFSIZ],
Qbuff[QUERY_BUFSIZ];
while (fgets(Qbuff, QUERY_BUFSIZ, pgLO->index))
{
2000-06-16 03:05:22 +08:00
if (*Qbuff == '#')
continue;
2001-03-22 12:01:46 +08:00
if (!pgLO->remove && !pgLO->quiet)
2000-06-16 03:05:22 +08:00
printf(Qbuff);
2001-03-22 12:01:46 +08:00
sscanf(Qbuff, "%u\t%s\t%s\t%s\n", &loa.lo_oid, tab, attr, path);
2000-06-16 03:05:22 +08:00
loa.lo_table = tab;
2001-03-22 12:01:46 +08:00
loa.lo_attr = attr;
2000-06-16 03:05:22 +08:00
snprintf(lo_path, BUFSIZ, "%s/%s", pgLO->space, path);
2000-06-16 03:05:22 +08:00
/*
2000-06-16 03:05:22 +08:00
* Import LO
*/
2001-03-22 12:01:46 +08:00
if ((new_oid = lo_import(pgLO->conn, lo_path)) == 0)
{
2000-06-16 03:05:22 +08:00
fprintf(stderr, "%s: %s\n", progname, PQerrorMessage(pgLO->conn));
2001-03-22 12:01:46 +08:00
2000-06-16 03:05:22 +08:00
PQexec(pgLO->conn, "ROLLBACK");
fprintf(stderr, "\n%s: ROLLBACK\n", progname);
exit(RE_ERROR);
}
2001-03-22 12:01:46 +08:00
if (pgLO->remove)
{
2000-06-16 03:05:22 +08:00
notice(pgLO, FALSE);
2001-03-22 12:01:46 +08:00
if (lo_unlink(pgLO->conn, loa.lo_oid) < 0)
fprintf(stderr, "%s: can't remove LO %u:\n%s",
progname, loa.lo_oid, PQerrorMessage(pgLO->conn));
2000-06-16 03:05:22 +08:00
else if (!pgLO->quiet)
2001-03-22 12:01:46 +08:00
printf("remove old %u and create new %u\n",
loa.lo_oid, new_oid);
notice(pgLO, TRUE);
2000-06-16 03:05:22 +08:00
}
2001-03-22 12:01:46 +08:00
2000-06-16 03:05:22 +08:00
pgLO->counter++;
2001-03-22 12:01:46 +08:00
/*
2000-06-16 03:05:22 +08:00
* UPDATE oid in tab
*/
snprintf(Qbuff, QUERY_BUFSIZ,
2002-09-05 04:31:48 +08:00
"UPDATE \"%s\" SET \"%s\"=%u WHERE \"%s\"=%u",
2000-06-16 03:05:22 +08:00
loa.lo_table, loa.lo_attr, new_oid, loa.lo_attr, loa.lo_oid);
2001-03-22 12:01:46 +08:00
/* fprintf(stderr, Qbuff); */
2000-06-16 03:05:22 +08:00
pgLO->res = PQexec(pgLO->conn, Qbuff);
2001-03-22 12:01:46 +08:00
if (PQresultStatus(pgLO->res) != PGRES_COMMAND_OK)
{
fprintf(stderr, "%s: %s\n", progname, PQerrorMessage(pgLO->conn));
PQclear(pgLO->res);
PQexec(pgLO->conn, "ROLLBACK");
2000-06-16 03:05:22 +08:00
fprintf(stderr, "\n%s: ROLLBACK\n", progname);
exit(RE_ERROR);
2001-03-22 12:01:46 +08:00
}
PQclear(pgLO->res);
2001-03-22 12:01:46 +08:00
}
}