1 /*-------------------------------------------------------------------------
4 * test using large objects with libpq
6 * Copyright (c) 1994, Regents of the University of California
10 * $Header: /cvsroot/pgsql/src/test/examples/Attic/testlo2.c,v 1.9 1999/02/13 23:22:46 momjian Exp $
12 *-------------------------------------------------------------------------
19 #include "libpq/libpq-fs.h"
25 * import file "in_filename" into database as large object "lobjOid"
29 importFile(PGconn *conn, char *filename)
39 * open the file to be read in
41 fd = open(filename, O_RDONLY, 0666);
44 fprintf(stderr, "can't open unix file\"%s\"\n", filename);
48 * create the large object
50 lobjId = lo_creat(conn, INV_READ | INV_WRITE);
52 fprintf(stderr, "can't create large object");
54 lobj_fd = lo_open(conn, lobjId, INV_WRITE);
57 * read in from the Unix file and write to the inversion file
59 while ((nbytes = read(fd, buf, BUFSIZE)) > 0)
61 tmp = lo_write(conn, lobj_fd, buf, nbytes);
63 fprintf(stderr, "error while reading \"%s\"", filename);
67 lo_close(conn, lobj_fd);
73 pickout(PGconn *conn, Oid lobjId, int start, int len)
80 lobj_fd = lo_open(conn, lobjId, INV_READ);
83 fprintf(stderr, "can't open large object %d",
87 lo_lseek(conn, lobj_fd, start, SEEK_SET);
88 buf = malloc(len + 1);
91 while (len - nread > 0)
93 nbytes = lo_read(conn, lobj_fd, buf, len - nread);
95 fprintf(stderr, ">>> %s", buf);
98 fprintf(stderr, "\n");
99 lo_close(conn, lobj_fd);
103 overwrite(PGconn *conn, Oid lobjId, int start, int len)
111 lobj_fd = lo_open(conn, lobjId, INV_READ);
114 fprintf(stderr, "can't open large object %d",
118 lo_lseek(conn, lobj_fd, start, SEEK_SET);
119 buf = malloc(len + 1);
121 for (i = 0; i < len; i++)
126 while (len - nwritten > 0)
128 nbytes = lo_write(conn, lobj_fd, buf + nwritten, len - nwritten);
131 fprintf(stderr, "\n");
132 lo_close(conn, lobj_fd);
138 * export large object "lobjOid" to file "out_filename"
142 exportFile(PGconn *conn, Oid lobjId, char *filename)
151 * create an inversion "object"
153 lobj_fd = lo_open(conn, lobjId, INV_READ);
156 fprintf(stderr, "can't open large object %d",
161 * open the file to be written to
163 fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0666);
166 fprintf(stderr, "can't open unix file\"%s\"",
171 * read in from the Unix file and write to the inversion file
173 while ((nbytes = lo_read(conn, lobj_fd, buf, BUFSIZE)) > 0)
175 tmp = write(fd, buf, nbytes);
178 fprintf(stderr, "error while writing \"%s\"",
183 lo_close(conn, lobj_fd);
190 exit_nicely(PGconn *conn)
197 main(int argc, char **argv)
208 fprintf(stderr, "Usage: %s database_name in_filename out_filename\n",
214 in_filename = argv[2];
215 out_filename = argv[3];
218 * set up the connection
220 conn = PQsetdb(NULL, NULL, NULL, NULL, database);
222 /* check to see that the backend connection was successfully made */
223 if (PQstatus(conn) == CONNECTION_BAD)
225 fprintf(stderr, "Connection to database '%s' failed.\n", database);
226 fprintf(stderr, "%s", PQerrorMessage(conn));
230 res = PQexec(conn, "begin");
233 printf("importing file \"%s\" ...\n", in_filename);
234 /* lobjOid = importFile(conn, in_filename); */
235 lobjOid = lo_import(conn, in_filename);
237 printf("\tas large object %d.\n", lobjOid);
239 printf("picking out bytes 1000-2000 of the large object\n");
240 pickout(conn, lobjOid, 1000, 1000);
242 printf("overwriting bytes 1000-2000 of the large object with X's\n");
243 overwrite(conn, lobjOid, 1000, 1000);
246 printf("exporting large object to file \"%s\" ...\n", out_filename);
247 /* exportFile(conn, lobjOid, out_filename); */
248 lo_export(conn, lobjOid, out_filename);
250 res = PQexec(conn, "end");