1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
5 >Example Program</TITLE
8 CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
10 HREF="mailto:pgsql-docs@postgresql.org"><LINK
12 TITLE="PostgreSQL 7.4.1 Documentation"
13 HREF="index.html"><LINK
16 HREF="largeobjects.html"><LINK
18 TITLE="Server-Side Functions"
19 HREF="lo-funcs.html"><LINK
21 TITLE="pgtcl - Tcl Binding Library"
22 HREF="pgtcl.html"><LINK
25 HREF="stylesheet.css"><META
27 CONTENT="2003-12-22T03:48:47"></HEAD
33 SUMMARY="Header navigation table"
43 >PostgreSQL 7.4.1 Documentation</TH
60 HREF="largeobjects.html"
67 >Chapter 28. Large Objects</TD
73 HREF="largeobjects.html"
96 >28.5. Example Program</A
100 HREF="lo-examplesect.html#LO-EXAMPLE"
102 > is a sample program which shows how the large object
107 > can be used. Parts of the program are
108 commented out but are left in the source for the reader's
109 benefit. This program can also be found in
112 >src/test/examples/testlo.c</TT
113 > in the source distribution.</P
121 >Example 28-1. Large Objects with <SPAN
127 CLASS="PROGRAMLISTING"
128 >/*--------------------------------------------------------------
131 * test using large objects with libpq
133 * Copyright (c) 1994, Regents of the University of California
135 *--------------------------------------------------------------
137 #include <stdio.h>
138 #include "libpq-fe.h"
139 #include "libpq/libpq-fs.h"
145 * import file "in_filename" into database as large object "lobjOid"
149 importFile(PGconn *conn, char *filename)
159 * open the file to be read in
161 fd = open(filename, O_RDONLY, 0666);
164 fprintf(stderr, "can't open unix file %s\n", filename);
168 * create the large object
170 lobjId = lo_creat(conn, INV_READ | INV_WRITE);
172 fprintf(stderr, "can't create large object\n");
174 lobj_fd = lo_open(conn, lobjId, INV_WRITE);
177 * read in from the Unix file and write to the inversion file
179 while ((nbytes = read(fd, buf, BUFSIZE)) > 0)
181 tmp = lo_write(conn, lobj_fd, buf, nbytes);
183 fprintf(stderr, "error while reading large object\n");
187 (void) lo_close(conn, lobj_fd);
193 pickout(PGconn *conn, Oid lobjId, int start, int len)
200 lobj_fd = lo_open(conn, lobjId, INV_READ);
203 fprintf(stderr, "can't open large object %d\n",
207 lo_lseek(conn, lobj_fd, start, SEEK_SET);
208 buf = malloc(len + 1);
211 while (len - nread > 0)
213 nbytes = lo_read(conn, lobj_fd, buf, len - nread);
215 fprintf(stderr, ">>> %s", buf);
219 fprintf(stderr, "\n");
220 lo_close(conn, lobj_fd);
224 overwrite(PGconn *conn, Oid lobjId, int start, int len)
232 lobj_fd = lo_open(conn, lobjId, INV_READ);
235 fprintf(stderr, "can't open large object %d\n",
239 lo_lseek(conn, lobj_fd, start, SEEK_SET);
240 buf = malloc(len + 1);
242 for (i = 0; i < len; i++)
247 while (len - nwritten > 0)
249 nbytes = lo_write(conn, lobj_fd, buf + nwritten, len - nwritten);
253 fprintf(stderr, "\n");
254 lo_close(conn, lobj_fd);
258 * exportFile * export large object "lobjOid" to file "out_filename"
262 exportFile(PGconn *conn, Oid lobjId, char *filename)
271 * create an inversion "object"
273 lobj_fd = lo_open(conn, lobjId, INV_READ);
276 fprintf(stderr, "can't open large object %d\n",
281 * open the file to be written to
283 fd = open(filename, O_CREAT | O_WRONLY, 0666);
286 fprintf(stderr, "can't open unix file %s\n",
291 * read in from the Unix file and write to the inversion file
293 while ((nbytes = lo_read(conn, lobj_fd, buf, BUFSIZE)) > 0)
295 tmp = write(fd, buf, nbytes);
298 fprintf(stderr, "error while writing %s\n",
303 (void) lo_close(conn, lobj_fd);
310 exit_nicely(PGconn *conn)
317 main(int argc, char **argv)
328 fprintf(stderr, "Usage: %s database_name in_filename out_filename\n",
334 in_filename = argv[2];
335 out_filename = argv[3];
338 * set up the connection
340 conn = PQsetdb(NULL, NULL, NULL, NULL, database);
342 /* check to see that the backend connection was successfully made */
343 if (PQstatus(conn) == CONNECTION_BAD)
345 fprintf(stderr, "Connection to database '%s' failed.\n", database);
346 fprintf(stderr, "%s", PQerrorMessage(conn));
350 res = PQexec(conn, "begin");
353 printf("importing file %s\n", in_filename);
354 /* lobjOid = importFile(conn, in_filename); */
355 lobjOid = lo_import(conn, in_filename);
357 printf("as large object %d.\n", lobjOid);
359 printf("picking out bytes 1000-2000 of the large object\n");
360 pickout(conn, lobjOid, 1000, 1000);
362 printf("overwriting bytes 1000-2000 of the large object with X's\n");
363 overwrite(conn, lobjOid, 1000, 1000);
366 printf("exporting large object to file %s\n", out_filename);
367 /* exportFile(conn, lobjOid, out_filename); */
368 lo_export(conn, lobjOid, out_filename);
370 res = PQexec(conn, "end");
382 SUMMARY="Footer navigation table"
421 >Server-Side Functions</TD
427 HREF="largeobjects.html"
438 > - Tcl Binding Library</TD