2
0
mirror of https://git.postgresql.org/git/postgresql.git synced 2024-12-21 08:29:39 +08:00
postgresql/contrib/pg_dumplo/lo_import.c

102 lines
2.2 KiB
C
Raw Normal View History

/* -------------------------------------------------------------------------
* pg_dumplo
*
* $Header: /cvsroot/pgsql/contrib/pg_dumplo/Attic/lo_import.c,v 1.6 2001/10/25 05:49:19 momjian Exp $
*
* Karel Zak 1999-2000
* -------------------------------------------------------------------------
*/
2000-06-16 03:05:22 +08:00
2001-03-22 12:01:46 +08:00
#include <stdio.h>
2000-06-16 03:05:22 +08:00
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#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 <libpq-fe.h>
#include <libpq/libpq-fs.h>
#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
2001-03-22 12:01:46 +08:00
sprintf(lo_path, "%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
*/
2001-03-22 12:01:46 +08:00
sprintf(Qbuff, "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
}
}