postgresql/contrib/pg_dumplo/lo_import.c

122 lines
2.7 KiB
C
Raw Normal View History

/* -------------------------------------------------------------------------
* pg_dumplo
*
2004-11-29 07:49:49 +08:00
* $PostgreSQL: pgsql/contrib/pg_dumplo/lo_import.c,v 1.11 2004/11/28 23:49:49 tgl Exp $
*
2004-11-29 07:49:49 +08:00
* Karel Zak 1999-2004
* -------------------------------------------------------------------------
*/
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;
2004-11-29 07:49:49 +08:00
int ret, line=0;
2001-03-22 12:01:46 +08:00
char tab[MAX_TABLE_NAME],
attr[MAX_ATTR_NAME],
2004-11-29 07:49:49 +08:00
sch[MAX_SCHEMA_NAME],
2001-03-22 12:01:46 +08:00
path[BUFSIZ],
lo_path[BUFSIZ],
Qbuff[QUERY_BUFSIZ];
while (fgets(Qbuff, QUERY_BUFSIZ, pgLO->index))
{
2004-11-29 07:49:49 +08:00
line++;
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
2004-11-29 07:49:49 +08:00
if ((ret=sscanf(Qbuff, "%u\t%s\t%s\t%s\t%s\n", &loa.lo_oid, tab, attr, path, sch)) < 5)
{
/* backward compatible mode */
ret = sscanf(Qbuff, "%u\t%s\t%s\t%s\n", &loa.lo_oid, tab, attr, path);
strcpy(sch, "public");
}
if (ret < 4)
{
fprintf(stderr, "%s: index file reading failed at line %d\n", progname, line);
PQexec(pgLO->conn, "ROLLBACK");
fprintf(stderr, "\n%s: ROLLBACK\n", progname);
exit(RE_ERROR);
}
loa.lo_schema = sch;
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
2004-11-29 07:49:49 +08:00
if (path && *path=='/')
/* absolute path */
snprintf(lo_path, BUFSIZ, "%s", path);
else
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,
2004-11-29 07:49:49 +08:00
"UPDATE \"%s\".\"%s\" SET \"%s\"=%u WHERE \"%s\"=%u",
loa.lo_schema, loa.lo_table, loa.lo_attr, new_oid, loa.lo_attr, loa.lo_oid);
2000-06-16 03:05:22 +08:00
2004-11-29 07:49:49 +08:00
/*fprintf(stderr, Qbuff);*/
2001-03-22 12:01:46 +08:00
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
}
}