1 /*-------------------------------------------------------------------------
4 * test using large objects with libpq
6 * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
13 *-------------------------------------------------------------------------
18 #include <sys/types.h>
24 #include "libpq/libpq-fs.h"
30 * import file "in_filename" into database as large object "lobjOid"
34 importFile(PGconn
*conn
, char *filename
)
44 * open the file to be read in
46 fd
= open(filename
, O_RDONLY
, 0666);
49 fprintf(stderr
, "cannot open unix file\"%s\"\n", filename
);
53 * create the large object
55 lobjId
= lo_creat(conn
, INV_READ
| INV_WRITE
);
57 fprintf(stderr
, "cannot create large object");
59 lobj_fd
= lo_open(conn
, lobjId
, INV_WRITE
);
62 * read in from the Unix file and write to the inversion file
64 while ((nbytes
= read(fd
, buf
, BUFSIZE
)) > 0)
66 tmp
= lo_write(conn
, lobj_fd
, buf
, nbytes
);
68 fprintf(stderr
, "error while reading \"%s\"", filename
);
72 lo_close(conn
, lobj_fd
);
78 pickout(PGconn
*conn
, Oid lobjId
, int start
, int len
)
85 lobj_fd
= lo_open(conn
, lobjId
, INV_READ
);
87 fprintf(stderr
, "cannot open large object %u", lobjId
);
89 lo_lseek(conn
, lobj_fd
, start
, SEEK_SET
);
90 buf
= malloc(len
+ 1);
93 while (len
- nread
> 0)
95 nbytes
= lo_read(conn
, lobj_fd
, buf
, len
- nread
);
97 fprintf(stderr
, ">>> %s", buf
);
100 break; /* no more data? */
103 fprintf(stderr
, "\n");
104 lo_close(conn
, lobj_fd
);
108 overwrite(PGconn
*conn
, Oid lobjId
, int start
, int len
)
116 lobj_fd
= lo_open(conn
, lobjId
, INV_WRITE
);
118 fprintf(stderr
, "cannot open large object %u", lobjId
);
120 lo_lseek(conn
, lobj_fd
, start
, SEEK_SET
);
121 buf
= malloc(len
+ 1);
123 for (i
= 0; i
< len
; i
++)
128 while (len
- nwritten
> 0)
130 nbytes
= lo_write(conn
, lobj_fd
, buf
+ nwritten
, len
- nwritten
);
134 fprintf(stderr
, "\nWRITE FAILED!\n");
139 fprintf(stderr
, "\n");
140 lo_close(conn
, lobj_fd
);
146 * export large object "lobjOid" to file "out_filename"
150 exportFile(PGconn
*conn
, Oid lobjId
, char *filename
)
159 * open the large object
161 lobj_fd
= lo_open(conn
, lobjId
, INV_READ
);
163 fprintf(stderr
, "cannot open large object %u", lobjId
);
166 * open the file to be written to
168 fd
= open(filename
, O_CREAT
| O_WRONLY
| O_TRUNC
, 0666);
171 fprintf(stderr
, "cannot open unix file\"%s\"",
176 * read in from the inversion file and write to the Unix file
178 while ((nbytes
= lo_read(conn
, lobj_fd
, buf
, BUFSIZE
)) > 0)
180 tmp
= write(fd
, buf
, nbytes
);
183 fprintf(stderr
, "error while writing \"%s\"",
188 lo_close(conn
, lobj_fd
);
195 exit_nicely(PGconn
*conn
)
202 main(int argc
, char **argv
)
213 fprintf(stderr
, "Usage: %s database_name in_filename out_filename\n",
219 in_filename
= argv
[2];
220 out_filename
= argv
[3];
223 * set up the connection
225 conn
= PQsetdb(NULL
, NULL
, NULL
, NULL
, database
);
227 /* check to see that the backend connection was successfully made */
228 if (PQstatus(conn
) != CONNECTION_OK
)
230 fprintf(stderr
, "Connection to database failed: %s",
231 PQerrorMessage(conn
));
235 res
= PQexec(conn
, "begin");
237 printf("importing file \"%s\" ...\n", in_filename
);
238 /* lobjOid = importFile(conn, in_filename); */
239 lobjOid
= lo_import(conn
, in_filename
);
241 fprintf(stderr
, "%s\n", PQerrorMessage(conn
));
244 printf("\tas large object %u.\n", lobjOid
);
246 printf("picking out bytes 1000-2000 of the large object\n");
247 pickout(conn
, lobjOid
, 1000, 1000);
249 printf("overwriting bytes 1000-2000 of the large object with X's\n");
250 overwrite(conn
, lobjOid
, 1000, 1000);
252 printf("exporting large object to file \"%s\" ...\n", out_filename
);
253 /* exportFile(conn, lobjOid, out_filename); */
254 if (!lo_export(conn
, lobjOid
, out_filename
))
255 fprintf(stderr
, "%s\n", PQerrorMessage(conn
));
258 res
= PQexec(conn
, "end");