lo_import.c 2.17 KB
Newer Older
1 2 3
/* -------------------------------------------------------------------------
 * pg_dumplo
 *
4
 * $Header: /cvsroot/pgsql/contrib/pg_dumplo/Attic/lo_import.c,v 1.5 2001/03/22 06:16:06 momjian Exp $
5 6 7 8
 *
 *					Karel Zak 1999-2000
 * -------------------------------------------------------------------------
 */
9

10
#include <stdio.h>
11 12 13 14 15 16
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
17
#include <errno.h>
18 19 20 21 22 23 24
#include <time.h>

#include <libpq-fe.h>
#include <libpq/libpq-fs.h>

#include "pg_dumplo.h"

25
extern int	errno;
26

27 28
void
pglo_import(LODumpMaster * pgLO)
29 30
{
	LOlist		loa;
31 32 33 34 35 36 37 38 39 40
	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))
	{

41 42 43
		if (*Qbuff == '#')
			continue;

44
		if (!pgLO->remove && !pgLO->quiet)
45
			printf(Qbuff);
46 47

		sscanf(Qbuff, "%u\t%s\t%s\t%s\n", &loa.lo_oid, tab, attr, path);
48
		loa.lo_table = tab;
49
		loa.lo_attr = attr;
50

51
		sprintf(lo_path, "%s/%s", pgLO->space, path);
52

53
		/* 
54 55
		 * Import LO
		 */
56 57 58
		if ((new_oid = lo_import(pgLO->conn, lo_path)) == 0)
		{

59
			fprintf(stderr, "%s: %s\n", progname, PQerrorMessage(pgLO->conn));
60

61 62 63 64 65
			PQexec(pgLO->conn, "ROLLBACK");
			fprintf(stderr, "\n%s: ROLLBACK\n", progname);
			exit(RE_ERROR);
		}

66 67
		if (pgLO->remove)
		{
68
			notice(pgLO, FALSE);
69 70 71 72
			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));

73
			else if (!pgLO->quiet)
74 75 76
				printf("remove old %u and create new %u\n",
					   loa.lo_oid, new_oid);
			notice(pgLO, TRUE);
77
		}
78

79
		pgLO->counter++;
80

81
		/* 
82 83
		 * UPDATE oid in tab
		 */
84
		sprintf(Qbuff, "UPDATE \"%s\" SET \"%s\"=%u WHERE \"%s\"=%u",
85 86
			loa.lo_table, loa.lo_attr, new_oid, loa.lo_attr, loa.lo_oid);

87 88
		/* fprintf(stderr, Qbuff); */

89
		pgLO->res = PQexec(pgLO->conn, Qbuff);
90 91 92 93

		if (PQresultStatus(pgLO->res) != PGRES_COMMAND_OK)
		{
			fprintf(stderr, "%s: %s\n", progname, PQerrorMessage(pgLO->conn));
94 95
			PQclear(pgLO->res);
			PQexec(pgLO->conn, "ROLLBACK");
96 97
			fprintf(stderr, "\n%s: ROLLBACK\n", progname);
			exit(RE_ERROR);
98
		}
99
		PQclear(pgLO->res);
100 101
	}
}