OSDN Git Service

FIRST REPOSITORY
[eos/hostdependOTHERS.git] / I686LINUX / util / I686LINUX / doc / postgresql / html / lo-examplesect.html
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2 <HTML
3 ><HEAD
4 ><TITLE
5 >Example Program</TITLE
6 ><META
7 NAME="GENERATOR"
8 CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
9 REV="MADE"
10 HREF="mailto:pgsql-docs@postgresql.org"><LINK
11 REL="HOME"
12 TITLE="PostgreSQL 7.4.1 Documentation"
13 HREF="index.html"><LINK
14 REL="UP"
15 TITLE="Large Objects"
16 HREF="largeobjects.html"><LINK
17 REL="PREVIOUS"
18 TITLE="Server-Side Functions"
19 HREF="lo-funcs.html"><LINK
20 REL="NEXT"
21 TITLE="pgtcl - Tcl Binding Library"
22 HREF="pgtcl.html"><LINK
23 REL="STYLESHEET"
24 TYPE="text/css"
25 HREF="stylesheet.css"><META
26 NAME="creation"
27 CONTENT="2003-12-22T03:48:47"></HEAD
28 ><BODY
29 CLASS="SECT1"
30 ><DIV
31 CLASS="NAVHEADER"
32 ><TABLE
33 SUMMARY="Header navigation table"
34 WIDTH="100%"
35 BORDER="0"
36 CELLPADDING="0"
37 CELLSPACING="0"
38 ><TR
39 ><TH
40 COLSPAN="5"
41 ALIGN="center"
42 VALIGN="bottom"
43 >PostgreSQL 7.4.1 Documentation</TH
44 ></TR
45 ><TR
46 ><TD
47 WIDTH="10%"
48 ALIGN="left"
49 VALIGN="top"
50 ><A
51 HREF="lo-funcs.html"
52 ACCESSKEY="P"
53 >Prev</A
54 ></TD
55 ><TD
56 WIDTH="10%"
57 ALIGN="left"
58 VALIGN="top"
59 ><A
60 HREF="largeobjects.html"
61 >Fast Backward</A
62 ></TD
63 ><TD
64 WIDTH="60%"
65 ALIGN="center"
66 VALIGN="bottom"
67 >Chapter 28. Large Objects</TD
68 ><TD
69 WIDTH="10%"
70 ALIGN="right"
71 VALIGN="top"
72 ><A
73 HREF="largeobjects.html"
74 >Fast Forward</A
75 ></TD
76 ><TD
77 WIDTH="10%"
78 ALIGN="right"
79 VALIGN="top"
80 ><A
81 HREF="pgtcl.html"
82 ACCESSKEY="N"
83 >Next</A
84 ></TD
85 ></TR
86 ></TABLE
87 ><HR
88 ALIGN="LEFT"
89 WIDTH="100%"></DIV
90 ><DIV
91 CLASS="SECT1"
92 ><H1
93 CLASS="SECT1"
94 ><A
95 NAME="LO-EXAMPLESECT"
96 >28.5. Example Program</A
97 ></H1
98 ><P
99 >     <A
100 HREF="lo-examplesect.html#LO-EXAMPLE"
101 >Example 28-1</A
102 > is a sample program which shows how the large object  
103      interface
104      in  <SPAN
105 CLASS="APPLICATION"
106 >libpq</SPAN
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
110      <TT
111 CLASS="FILENAME"
112 >src/test/examples/testlo.c</TT
113 > in the source distribution.</P
114 ><DIV
115 CLASS="EXAMPLE"
116 ><A
117 NAME="LO-EXAMPLE"
118 ></A
119 ><P
120 ><B
121 >Example 28-1. Large Objects with <SPAN
122 CLASS="APPLICATION"
123 >libpq</SPAN
124 > Example Program</B
125 ></P
126 ><PRE
127 CLASS="PROGRAMLISTING"
128 >/*--------------------------------------------------------------
129  *
130  * testlo.c--
131  *    test using large objects with libpq
132  *
133  * Copyright (c) 1994, Regents of the University of California
134  *
135  *--------------------------------------------------------------
136  */
137 #include &lt;stdio.h&gt;
138 #include &quot;libpq-fe.h&quot;
139 #include &quot;libpq/libpq-fs.h&quot;
140
141 #define BUFSIZE          1024
142
143 /*
144  * importFile
145  *    import file &quot;in_filename&quot; into database as large object &quot;lobjOid&quot;
146  *
147  */
148 Oid
149 importFile(PGconn *conn, char *filename)
150 {
151     Oid         lobjId;
152     int         lobj_fd;
153     char        buf[BUFSIZE];
154     int         nbytes,
155                 tmp;
156     int         fd;
157
158     /*
159      * open the file to be read in
160      */
161     fd = open(filename, O_RDONLY, 0666);
162     if (fd &lt; 0)
163     {                           /* error */
164         fprintf(stderr, &quot;can't open unix file %s\n&quot;, filename);
165     }
166
167     /*
168      * create the large object
169      */
170     lobjId = lo_creat(conn, INV_READ | INV_WRITE);
171     if (lobjId == 0)
172         fprintf(stderr, &quot;can't create large object\n&quot;);
173
174     lobj_fd = lo_open(conn, lobjId, INV_WRITE);
175
176     /*
177      * read in from the Unix file and write to the inversion file
178      */
179     while ((nbytes = read(fd, buf, BUFSIZE)) &gt; 0)
180     {
181         tmp = lo_write(conn, lobj_fd, buf, nbytes);
182         if (tmp &lt; nbytes)
183             fprintf(stderr, &quot;error while reading large object\n&quot;);
184     }
185
186     (void) close(fd);
187     (void) lo_close(conn, lobj_fd);
188
189     return lobjId;
190 }
191
192 void
193 pickout(PGconn *conn, Oid lobjId, int start, int len)
194 {
195     int         lobj_fd;
196     char       *buf;
197     int         nbytes;
198     int         nread;
199
200     lobj_fd = lo_open(conn, lobjId, INV_READ);
201     if (lobj_fd &lt; 0)
202     {
203         fprintf(stderr, &quot;can't open large object %d\n&quot;,
204                 lobjId);
205     }
206
207     lo_lseek(conn, lobj_fd, start, SEEK_SET);
208     buf = malloc(len + 1);
209
210     nread = 0;
211     while (len - nread &gt; 0)
212     {
213         nbytes = lo_read(conn, lobj_fd, buf, len - nread);
214         buf[nbytes] = ' ';
215         fprintf(stderr, &quot;&gt;&gt;&gt; %s&quot;, buf);
216         nread += nbytes;
217     }
218     free(buf);
219     fprintf(stderr, &quot;\n&quot;);
220     lo_close(conn, lobj_fd);
221 }
222
223 void
224 overwrite(PGconn *conn, Oid lobjId, int start, int len)
225 {
226     int         lobj_fd;
227     char       *buf;
228     int         nbytes;
229     int         nwritten;
230     int         i;
231
232     lobj_fd = lo_open(conn, lobjId, INV_READ);
233     if (lobj_fd &lt; 0)
234     {
235         fprintf(stderr, &quot;can't open large object %d\n&quot;,
236                 lobjId);
237     }
238
239     lo_lseek(conn, lobj_fd, start, SEEK_SET);
240     buf = malloc(len + 1);
241
242     for (i = 0; i &lt; len; i++)
243         buf[i] = 'X';
244     buf[i] = ' ';
245
246     nwritten = 0;
247     while (len - nwritten &gt; 0)
248     {
249         nbytes = lo_write(conn, lobj_fd, buf + nwritten, len - nwritten);
250         nwritten += nbytes;
251     }
252     free(buf);
253     fprintf(stderr, &quot;\n&quot;);
254     lo_close(conn, lobj_fd);
255 }
256
257 /*
258  * exportFile *    export large object &quot;lobjOid&quot; to file &quot;out_filename&quot;
259  *
260  */
261 void
262 exportFile(PGconn *conn, Oid lobjId, char *filename)
263 {
264     int         lobj_fd;
265     char        buf[BUFSIZE];
266     int         nbytes,
267                 tmp;
268     int         fd;
269
270     /*
271      * create an inversion &quot;object&quot;
272      */
273     lobj_fd = lo_open(conn, lobjId, INV_READ);
274     if (lobj_fd &lt; 0)
275     {
276         fprintf(stderr, &quot;can't open large object %d\n&quot;,
277                 lobjId);
278     }
279
280     /*
281      * open the file to be written to
282      */
283     fd = open(filename, O_CREAT | O_WRONLY, 0666);
284     if (fd &lt; 0)
285     {                           /* error */
286         fprintf(stderr, &quot;can't open unix file %s\n&quot;,
287                 filename);
288     }
289
290     /*
291      * read in from the Unix file and write to the inversion file
292      */
293     while ((nbytes = lo_read(conn, lobj_fd, buf, BUFSIZE)) &gt; 0)
294     {
295         tmp = write(fd, buf, nbytes);
296         if (tmp &lt; nbytes)
297         {
298             fprintf(stderr, &quot;error while writing %s\n&quot;,
299                     filename);
300         }
301     }
302
303     (void) lo_close(conn, lobj_fd);
304     (void) close(fd);
305
306     return;
307 }
308
309 void
310 exit_nicely(PGconn *conn)
311 {
312     PQfinish(conn);
313     exit(1);
314 }
315
316 int
317 main(int argc, char **argv)
318 {
319     char       *in_filename,
320                *out_filename;
321     char       *database;
322     Oid         lobjOid;
323     PGconn     *conn;
324     PGresult   *res;
325
326     if (argc != 4)
327     {
328         fprintf(stderr, &quot;Usage: %s database_name in_filename out_filename\n&quot;,
329                 argv[0]);
330         exit(1);
331     }
332
333     database = argv[1];
334     in_filename = argv[2];
335     out_filename = argv[3];
336
337     /*
338      * set up the connection
339      */
340     conn = PQsetdb(NULL, NULL, NULL, NULL, database);
341
342     /* check to see that the backend connection was successfully made */
343     if (PQstatus(conn) == CONNECTION_BAD)
344     {
345         fprintf(stderr, &quot;Connection to database '%s' failed.\n&quot;, database);
346         fprintf(stderr, &quot;%s&quot;, PQerrorMessage(conn));
347         exit_nicely(conn);
348     }
349
350     res = PQexec(conn, &quot;begin&quot;);
351     PQclear(res);
352
353     printf(&quot;importing file %s\n&quot;, in_filename);
354 /*  lobjOid = importFile(conn, in_filename); */
355     lobjOid = lo_import(conn, in_filename);
356 /*
357     printf(&quot;as large object %d.\n&quot;, lobjOid);
358
359     printf(&quot;picking out bytes 1000-2000 of the large object\n&quot;);
360     pickout(conn, lobjOid, 1000, 1000);
361
362     printf(&quot;overwriting bytes 1000-2000 of the large object with X's\n&quot;);
363     overwrite(conn, lobjOid, 1000, 1000);
364 */
365
366     printf(&quot;exporting large object to file %s\n&quot;, out_filename);
367 /*    exportFile(conn, lobjOid, out_filename); */
368     lo_export(conn, lobjOid, out_filename);
369
370     res = PQexec(conn, &quot;end&quot;);
371     PQclear(res);
372     PQfinish(conn);
373     exit(0);
374 }</PRE
375 ></DIV
376 ></DIV
377 ><DIV
378 CLASS="NAVFOOTER"
379 ><HR
380 ALIGN="LEFT"
381 WIDTH="100%"><TABLE
382 SUMMARY="Footer navigation table"
383 WIDTH="100%"
384 BORDER="0"
385 CELLPADDING="0"
386 CELLSPACING="0"
387 ><TR
388 ><TD
389 WIDTH="33%"
390 ALIGN="left"
391 VALIGN="top"
392 ><A
393 HREF="lo-funcs.html"
394 ACCESSKEY="P"
395 >Prev</A
396 ></TD
397 ><TD
398 WIDTH="34%"
399 ALIGN="center"
400 VALIGN="top"
401 ><A
402 HREF="index.html"
403 ACCESSKEY="H"
404 >Home</A
405 ></TD
406 ><TD
407 WIDTH="33%"
408 ALIGN="right"
409 VALIGN="top"
410 ><A
411 HREF="pgtcl.html"
412 ACCESSKEY="N"
413 >Next</A
414 ></TD
415 ></TR
416 ><TR
417 ><TD
418 WIDTH="33%"
419 ALIGN="left"
420 VALIGN="top"
421 >Server-Side Functions</TD
422 ><TD
423 WIDTH="34%"
424 ALIGN="center"
425 VALIGN="top"
426 ><A
427 HREF="largeobjects.html"
428 ACCESSKEY="U"
429 >Up</A
430 ></TD
431 ><TD
432 WIDTH="33%"
433 ALIGN="right"
434 VALIGN="top"
435 ><SPAN
436 CLASS="APPLICATION"
437 >pgtcl</SPAN
438 > - Tcl Binding Library</TD
439 ></TR
440 ></TABLE
441 ></DIV
442 ></BODY
443 ></HTML
444 >