OSDN Git Service

eaa19229e714f9de37fdeaa41a7d8426d2f85b9d
[pg-rex/syncrep.git] / src / test / examples / testlo2.c
1 /*-------------------------------------------------------------------------
2  *
3  * lotest.c
4  *        test using large objects with libpq
5  *
6  * Copyright (c) 1994, Regents of the University of California
7  *
8  *
9  * IDENTIFICATION
10  *        $Header: /cvsroot/pgsql/src/test/examples/Attic/testlo2.c,v 1.9 1999/02/13 23:22:46 momjian Exp $
11  *
12  *-------------------------------------------------------------------------
13  */
14 #include <stdlib.h>
15 #include <stdio.h>
16 #include <fcntl.h>
17 #include <unistd.h>
18 #include "libpq-fe.h"
19 #include "libpq/libpq-fs.h"
20
21 #define BUFSIZE                 1024
22
23 /*
24  * importFile -
25  *        import file "in_filename" into database as large object "lobjOid"
26  *
27  */
28 static Oid
29 importFile(PGconn *conn, char *filename)
30 {
31         Oid                     lobjId;
32         int                     lobj_fd;
33         char            buf[BUFSIZE];
34         int                     nbytes,
35                                 tmp;
36         int                     fd;
37
38         /*
39          * open the file to be read in
40          */
41         fd = open(filename, O_RDONLY, 0666);
42         if (fd < 0)
43         {                                                       /* error */
44                 fprintf(stderr, "can't open unix file\"%s\"\n", filename);
45         }
46
47         /*
48          * create the large object
49          */
50         lobjId = lo_creat(conn, INV_READ | INV_WRITE);
51         if (lobjId == 0)
52                 fprintf(stderr, "can't create large object");
53
54         lobj_fd = lo_open(conn, lobjId, INV_WRITE);
55
56         /*
57          * read in from the Unix file and write to the inversion file
58          */
59         while ((nbytes = read(fd, buf, BUFSIZE)) > 0)
60         {
61                 tmp = lo_write(conn, lobj_fd, buf, nbytes);
62                 if (tmp < nbytes)
63                         fprintf(stderr, "error while reading \"%s\"", filename);
64         }
65
66         close(fd);
67         lo_close(conn, lobj_fd);
68
69         return lobjId;
70 }
71
72 static void
73 pickout(PGconn *conn, Oid lobjId, int start, int len)
74 {
75         int                     lobj_fd;
76         char       *buf;
77         int                     nbytes;
78         int                     nread;
79
80         lobj_fd = lo_open(conn, lobjId, INV_READ);
81         if (lobj_fd < 0)
82         {
83                 fprintf(stderr, "can't open large object %d",
84                                 lobjId);
85         }
86
87         lo_lseek(conn, lobj_fd, start, SEEK_SET);
88         buf = malloc(len + 1);
89
90         nread = 0;
91         while (len - nread > 0)
92         {
93                 nbytes = lo_read(conn, lobj_fd, buf, len - nread);
94                 buf[nbytes] = '\0';
95                 fprintf(stderr, ">>> %s", buf);
96                 nread += nbytes;
97         }
98         fprintf(stderr, "\n");
99         lo_close(conn, lobj_fd);
100 }
101
102 static void
103 overwrite(PGconn *conn, Oid lobjId, int start, int len)
104 {
105         int                     lobj_fd;
106         char       *buf;
107         int                     nbytes;
108         int                     nwritten;
109         int                     i;
110
111         lobj_fd = lo_open(conn, lobjId, INV_READ);
112         if (lobj_fd < 0)
113         {
114                 fprintf(stderr, "can't open large object %d",
115                                 lobjId);
116         }
117
118         lo_lseek(conn, lobj_fd, start, SEEK_SET);
119         buf = malloc(len + 1);
120
121         for (i = 0; i < len; i++)
122                 buf[i] = 'X';
123         buf[i] = '\0';
124
125         nwritten = 0;
126         while (len - nwritten > 0)
127         {
128                 nbytes = lo_write(conn, lobj_fd, buf + nwritten, len - nwritten);
129                 nwritten += nbytes;
130         }
131         fprintf(stderr, "\n");
132         lo_close(conn, lobj_fd);
133 }
134
135
136 /*
137  * exportFile -
138  *        export large object "lobjOid" to file "out_filename"
139  *
140  */
141 static void
142 exportFile(PGconn *conn, Oid lobjId, char *filename)
143 {
144         int                     lobj_fd;
145         char            buf[BUFSIZE];
146         int                     nbytes,
147                                 tmp;
148         int                     fd;
149
150         /*
151          * create an inversion "object"
152          */
153         lobj_fd = lo_open(conn, lobjId, INV_READ);
154         if (lobj_fd < 0)
155         {
156                 fprintf(stderr, "can't open large object %d",
157                                 lobjId);
158         }
159
160         /*
161          * open the file to be written to
162          */
163         fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0666);
164         if (fd < 0)
165         {                                                       /* error */
166                 fprintf(stderr, "can't open unix file\"%s\"",
167                                 filename);
168         }
169
170         /*
171          * read in from the Unix file and write to the inversion file
172          */
173         while ((nbytes = lo_read(conn, lobj_fd, buf, BUFSIZE)) > 0)
174         {
175                 tmp = write(fd, buf, nbytes);
176                 if (tmp < nbytes)
177                 {
178                         fprintf(stderr, "error while writing \"%s\"",
179                                         filename);
180                 }
181         }
182
183         lo_close(conn, lobj_fd);
184         close(fd);
185
186         return;
187 }
188
189 static void
190 exit_nicely(PGconn *conn)
191 {
192         PQfinish(conn);
193         exit(1);
194 }
195
196 int
197 main(int argc, char **argv)
198 {
199         char       *in_filename,
200                            *out_filename;
201         char       *database;
202         Oid                     lobjOid;
203         PGconn     *conn;
204         PGresult   *res;
205
206         if (argc != 4)
207         {
208                 fprintf(stderr, "Usage: %s database_name in_filename out_filename\n",
209                                 argv[0]);
210                 exit(1);
211         }
212
213         database = argv[1];
214         in_filename = argv[2];
215         out_filename = argv[3];
216
217         /*
218          * set up the connection
219          */
220         conn = PQsetdb(NULL, NULL, NULL, NULL, database);
221
222         /* check to see that the backend connection was successfully made */
223         if (PQstatus(conn) == CONNECTION_BAD)
224         {
225                 fprintf(stderr, "Connection to database '%s' failed.\n", database);
226                 fprintf(stderr, "%s", PQerrorMessage(conn));
227                 exit_nicely(conn);
228         }
229
230         res = PQexec(conn, "begin");
231         PQclear(res);
232
233         printf("importing file \"%s\" ...\n", in_filename);
234 /*        lobjOid = importFile(conn, in_filename); */
235         lobjOid = lo_import(conn, in_filename);
236 /*
237         printf("\tas large object %d.\n", lobjOid);
238
239         printf("picking out bytes 1000-2000 of the large object\n");
240         pickout(conn, lobjOid, 1000, 1000);
241
242         printf("overwriting bytes 1000-2000 of the large object with X's\n");
243         overwrite(conn, lobjOid, 1000, 1000);
244 */
245
246         printf("exporting large object to file \"%s\" ...\n", out_filename);
247 /*        exportFile(conn, lobjOid, out_filename); */
248         lo_export(conn, lobjOid, out_filename);
249
250         res = PQexec(conn, "end");
251         PQclear(res);
252         PQfinish(conn);
253         exit(0);
254 }