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