1 /*-------------------------------------------------------------------------
4 * test using large objects with libpq
6 * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * $Header: /cvsroot/pgsql/src/test/examples/Attic/testlo2.c,v 1.14 2000/04/25 16:39:07 momjian Exp $
13 *-------------------------------------------------------------------------
20 #include "libpq/libpq-fs.h"
26 * import file "in_filename" into database as large object "lobjOid"
30 importFile(PGconn *conn, char *filename)
40 * open the file to be read in
42 fd = open(filename, O_RDONLY, 0666);
45 fprintf(stderr, "can't open unix file\"%s\"\n", filename);
49 * create the large object
51 lobjId = lo_creat(conn, INV_READ | INV_WRITE);
53 fprintf(stderr, "can't create large object");
55 lobj_fd = lo_open(conn, lobjId, INV_WRITE);
58 * read in from the Unix file and write to the inversion file
60 while ((nbytes = read(fd, buf, BUFSIZE)) > 0)
62 tmp = lo_write(conn, lobj_fd, buf, nbytes);
64 fprintf(stderr, "error while reading \"%s\"", filename);
68 lo_close(conn, lobj_fd);
74 pickout(PGconn *conn, Oid lobjId, int start, int len)
81 lobj_fd = lo_open(conn, lobjId, INV_READ);
83 fprintf(stderr, "can't open large object %u", lobjId);
85 lo_lseek(conn, lobj_fd, start, SEEK_SET);
86 buf = malloc(len + 1);
89 while (len - nread > 0)
91 nbytes = lo_read(conn, lobj_fd, buf, len - nread);
93 fprintf(stderr, ">>> %s", buf);
96 fprintf(stderr, "\n");
97 lo_close(conn, lobj_fd);
101 overwrite(PGconn *conn, Oid lobjId, int start, int len)
109 lobj_fd = lo_open(conn, lobjId, INV_READ);
111 fprintf(stderr, "can't open large object %u", lobjId);
113 lo_lseek(conn, lobj_fd, start, SEEK_SET);
114 buf = malloc(len + 1);
116 for (i = 0; i < len; i++)
121 while (len - nwritten > 0)
123 nbytes = lo_write(conn, lobj_fd, buf + nwritten, len - nwritten);
126 fprintf(stderr, "\n");
127 lo_close(conn, lobj_fd);
133 * export large object "lobjOid" to file "out_filename"
137 exportFile(PGconn *conn, Oid lobjId, char *filename)
146 * create an inversion "object"
148 lobj_fd = lo_open(conn, lobjId, INV_READ);
150 fprintf(stderr, "can't open large object %u", lobjId);
153 * open the file to be written to
155 fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0666);
158 fprintf(stderr, "can't open unix file\"%s\"",
163 * read in from the Unix file and write to the inversion file
165 while ((nbytes = lo_read(conn, lobj_fd, buf, BUFSIZE)) > 0)
167 tmp = write(fd, buf, nbytes);
170 fprintf(stderr, "error while writing \"%s\"",
175 lo_close(conn, lobj_fd);
182 exit_nicely(PGconn *conn)
189 main(int argc, char **argv)
200 fprintf(stderr, "Usage: %s database_name in_filename out_filename\n",
206 in_filename = argv[2];
207 out_filename = argv[3];
210 * set up the connection
212 conn = PQsetdb(NULL, NULL, NULL, NULL, database);
214 /* check to see that the backend connection was successfully made */
215 if (PQstatus(conn) == CONNECTION_BAD)
217 fprintf(stderr, "Connection to database '%s' failed.\n", database);
218 fprintf(stderr, "%s", PQerrorMessage(conn));
222 res = PQexec(conn, "begin");
225 printf("importing file \"%s\" ...\n", in_filename);
226 /* lobjOid = importFile(conn, in_filename); */
227 lobjOid = lo_import(conn, in_filename);
229 printf("\tas large object %u.\n", lobjOid);
231 printf("picking out bytes 1000-2000 of the large object\n");
232 pickout(conn, lobjOid, 1000, 1000);
234 printf("overwriting bytes 1000-2000 of the large object with X's\n");
235 overwrite(conn, lobjOid, 1000, 1000);
237 printf("exporting large object to file \"%s\" ...\n", out_filename);
238 /* exportFile(conn, lobjOid, out_filename); */
239 lo_export(conn, lobjOid, out_filename);
241 res = PQexec(conn, "end");