3
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
4
<title>29.5.�Example Program</title>
5
<link rel="stylesheet" href="stylesheet.css" type="text/css">
6
<link rev="made" href="pgsql-docs@postgresql.org">
7
<meta name="generator" content="DocBook XSL Stylesheets V1.70.0">
8
<link rel="start" href="index.html" title="PostgreSQL 8.1.4 Documentation">
9
<link rel="up" href="largeobjects.html" title="Chapter�29.�Large Objects">
10
<link rel="prev" href="lo-funcs.html" title="29.4.�Server-Side Functions">
11
<link rel="next" href="ecpg.html" title="Chapter�30.�ECPG - Embedded SQL in C">
12
<link rel="copyright" href="ln-legalnotice.html" title="Legal Notice">
14
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="sect1" lang="en">
15
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
16
<a name="lo-examplesect"></a>29.5.�Example Program</h2></div></div></div>
17
<p> <a href="lo-examplesect.html#lo-example" title="Example�29.1.�Large Objects with libpq Example Program">Example�29.1, “Large Objects with <span class="application">libpq</span> Example Program”</a> is a sample program which shows how the large object
19
in <span class="application">libpq</span> can be used. Parts of the program are
20
commented out but are left in the source for the reader's
21
benefit. This program can also be found in
22
<code class="filename">src/test/examples/testlo.c</code> in the source distribution.</p>
24
<a name="lo-example"></a><p class="title"><b>Example�29.1.�Large Objects with <span class="application">libpq</span> Example Program</b></p>
25
<div class="example-contents"><pre class="programlisting">/*--------------------------------------------------------------
28
* test using large objects with libpq
30
* Copyright (c) 1994, Regents of the University of California
32
*--------------------------------------------------------------
34
#include <stdio.h>
36
#include "libpq/libpq-fs.h"
42
* import file "in_filename" into database as large object "lobjOid"
46
importFile(PGconn *conn, char *filename)
56
* open the file to be read in
58
fd = open(filename, O_RDONLY, 0666);
61
fprintf(stderr, "can't open unix file %s\n", filename);
65
* create the large object
67
lobjId = lo_creat(conn, INV_READ | INV_WRITE);
69
fprintf(stderr, "can't create large object\n");
71
lobj_fd = lo_open(conn, lobjId, INV_WRITE);
74
* read in from the Unix file and write to the inversion file
76
while ((nbytes = read(fd, buf, BUFSIZE)) > 0)
78
tmp = lo_write(conn, lobj_fd, buf, nbytes);
80
fprintf(stderr, "error while reading large object\n");
84
(void) lo_close(conn, lobj_fd);
90
pickout(PGconn *conn, Oid lobjId, int start, int len)
97
lobj_fd = lo_open(conn, lobjId, INV_READ);
100
fprintf(stderr, "can't open large object %d\n",
104
lo_lseek(conn, lobj_fd, start, SEEK_SET);
105
buf = malloc(len + 1);
108
while (len - nread > 0)
110
nbytes = lo_read(conn, lobj_fd, buf, len - nread);
112
fprintf(stderr, ">>> %s", buf);
116
fprintf(stderr, "\n");
117
lo_close(conn, lobj_fd);
121
overwrite(PGconn *conn, Oid lobjId, int start, int len)
129
lobj_fd = lo_open(conn, lobjId, INV_WRITE);
132
fprintf(stderr, "can't open large object %d\n",
136
lo_lseek(conn, lobj_fd, start, SEEK_SET);
137
buf = malloc(len + 1);
139
for (i = 0; i < len; i++)
144
while (len - nwritten > 0)
146
nbytes = lo_write(conn, lobj_fd, buf + nwritten, len - nwritten);
150
fprintf(stderr, "\n");
151
lo_close(conn, lobj_fd);
156
* export large object "lobjOid" to file "out_filename"
160
exportFile(PGconn *conn, Oid lobjId, char *filename)
169
* open the large object
171
lobj_fd = lo_open(conn, lobjId, INV_READ);
174
fprintf(stderr, "can't open large object %d\n",
179
* open the file to be written to
181
fd = open(filename, O_CREAT | O_WRONLY, 0666);
184
fprintf(stderr, "can't open unix file %s\n",
189
* read in from the inversion file and write to the Unix file
191
while ((nbytes = lo_read(conn, lobj_fd, buf, BUFSIZE)) > 0)
193
tmp = write(fd, buf, nbytes);
196
fprintf(stderr, "error while writing %s\n",
201
(void) lo_close(conn, lobj_fd);
208
exit_nicely(PGconn *conn)
215
main(int argc, char **argv)
226
fprintf(stderr, "Usage: %s database_name in_filename out_filename\n",
232
in_filename = argv[2];
233
out_filename = argv[3];
236
* set up the connection
238
conn = PQsetdb(NULL, NULL, NULL, NULL, database);
240
/* check to see that the backend connection was successfully made */
241
if (PQstatus(conn) == CONNECTION_BAD)
243
fprintf(stderr, "Connection to database '%s' failed.\n", database);
244
fprintf(stderr, "%s", PQerrorMessage(conn));
248
res = PQexec(conn, "begin");
251
printf("importing file %s\n", in_filename);
252
/* lobjOid = importFile(conn, in_filename); */
253
lobjOid = lo_import(conn, in_filename);
255
printf("as large object %d.\n", lobjOid);
257
printf("picking out bytes 1000-2000 of the large object\n");
258
pickout(conn, lobjOid, 1000, 1000);
260
printf("overwriting bytes 1000-2000 of the large object with X's\n");
261
overwrite(conn, lobjOid, 1000, 1000);
264
printf("exporting large object to file %s\n", out_filename);
265
/* exportFile(conn, lobjOid, out_filename); */
266
lo_export(conn, lobjOid, out_filename);
268
res = PQexec(conn, "end");
274
<br class="example-break">