~vcs-imports/mammoth-replicator/trunk

« back to all changes in this revision

Viewing changes to contrib/pg_dumplo/lo_import.c

  • Committer: alvherre
  • Date: 2005-12-16 21:24:52 UTC
  • Revision ID: svn-v4:db760fc0-0f08-0410-9d63-cc6633f64896:trunk:1
Initial import of the REL8_0_3 sources from the Pgsql CVS repository.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -------------------------------------------------------------------------
 
2
 * pg_dumplo
 
3
 *
 
4
 * $PostgreSQL: pgsql/contrib/pg_dumplo/lo_import.c,v 1.11 2004-11-28 23:49:49 tgl Exp $
 
5
 *
 
6
 * Karel Zak 1999-2004
 
7
 * -------------------------------------------------------------------------
 
8
 */
 
9
 
 
10
#include "postgres_fe.h"
 
11
 
 
12
#include <fcntl.h>
 
13
#include <errno.h>
 
14
#include <time.h>
 
15
#include <unistd.h>
 
16
#include <sys/stat.h>
 
17
 
 
18
#include "libpq-fe.h"
 
19
#include "libpq/libpq-fs.h"
 
20
 
 
21
#include "pg_dumplo.h"
 
22
 
 
23
extern int      errno;
 
24
 
 
25
void
 
26
pglo_import(LODumpMaster * pgLO)
 
27
{
 
28
        LOlist          loa;
 
29
        Oid                     new_oid;
 
30
        int             ret, line=0;
 
31
        char            tab[MAX_TABLE_NAME],
 
32
                                attr[MAX_ATTR_NAME],
 
33
                                sch[MAX_SCHEMA_NAME],
 
34
                                path[BUFSIZ],
 
35
                                lo_path[BUFSIZ],
 
36
                                Qbuff[QUERY_BUFSIZ];
 
37
 
 
38
        while (fgets(Qbuff, QUERY_BUFSIZ, pgLO->index))
 
39
        {
 
40
                line++;
 
41
                
 
42
                if (*Qbuff == '#')
 
43
                        continue;
 
44
 
 
45
                if (!pgLO->remove && !pgLO->quiet)
 
46
                        printf(Qbuff);
 
47
 
 
48
                if ((ret=sscanf(Qbuff, "%u\t%s\t%s\t%s\t%s\n", &loa.lo_oid, tab, attr, path, sch)) < 5)
 
49
                {
 
50
                        /* backward compatible mode */
 
51
                        ret = sscanf(Qbuff, "%u\t%s\t%s\t%s\n", &loa.lo_oid, tab, attr, path);
 
52
                        strcpy(sch, "public");
 
53
                }
 
54
                if (ret < 4)
 
55
                {
 
56
                        fprintf(stderr, "%s: index file reading failed at line %d\n", progname, line);
 
57
                        PQexec(pgLO->conn, "ROLLBACK");
 
58
                        fprintf(stderr, "\n%s: ROLLBACK\n", progname);
 
59
                        exit(RE_ERROR);
 
60
                }
 
61
                
 
62
                loa.lo_schema = sch;
 
63
                loa.lo_table = tab;
 
64
                loa.lo_attr = attr;
 
65
 
 
66
                if (path && *path=='/')
 
67
                        /* absolute path */
 
68
                        snprintf(lo_path, BUFSIZ, "%s", path);
 
69
                else
 
70
                        snprintf(lo_path, BUFSIZ, "%s/%s", pgLO->space, path);
 
71
 
 
72
                /*
 
73
                 * Import LO
 
74
                 */
 
75
                if ((new_oid = lo_import(pgLO->conn, lo_path)) == 0)
 
76
                {
 
77
 
 
78
                        fprintf(stderr, "%s: %s\n", progname, PQerrorMessage(pgLO->conn));
 
79
 
 
80
                        PQexec(pgLO->conn, "ROLLBACK");
 
81
                        fprintf(stderr, "\n%s: ROLLBACK\n", progname);
 
82
                        exit(RE_ERROR);
 
83
                }
 
84
 
 
85
                if (pgLO->remove)
 
86
                {
 
87
                        notice(pgLO, FALSE);
 
88
                        if (lo_unlink(pgLO->conn, loa.lo_oid) < 0)
 
89
                                fprintf(stderr, "%s: can't remove LO %u:\n%s",
 
90
                                                progname, loa.lo_oid, PQerrorMessage(pgLO->conn));
 
91
 
 
92
                        else if (!pgLO->quiet)
 
93
                                printf("remove old %u and create new %u\n",
 
94
                                           loa.lo_oid, new_oid);
 
95
                        notice(pgLO, TRUE);
 
96
                }
 
97
 
 
98
                pgLO->counter++;
 
99
 
 
100
                /*
 
101
                 * UPDATE oid in tab
 
102
                 */
 
103
                snprintf(Qbuff, QUERY_BUFSIZ,
 
104
                                 "UPDATE \"%s\".\"%s\" SET \"%s\"=%u WHERE \"%s\"=%u",
 
105
                        loa.lo_schema, loa.lo_table, loa.lo_attr, new_oid, loa.lo_attr, loa.lo_oid);
 
106
 
 
107
                /*fprintf(stderr, Qbuff);*/
 
108
 
 
109
                pgLO->res = PQexec(pgLO->conn, Qbuff);
 
110
 
 
111
                if (PQresultStatus(pgLO->res) != PGRES_COMMAND_OK)
 
112
                {
 
113
                        fprintf(stderr, "%s: %s\n", progname, PQerrorMessage(pgLO->conn));
 
114
                        PQclear(pgLO->res);
 
115
                        PQexec(pgLO->conn, "ROLLBACK");
 
116
                        fprintf(stderr, "\n%s: ROLLBACK\n", progname);
 
117
                        exit(RE_ERROR);
 
118
                }
 
119
                PQclear(pgLO->res);
 
120
        }
 
121
}