~ubuntu-branches/debian/squeeze/pgadmin3/squeeze

« back to all changes in this revision

Viewing changes to docs/en_US/pg/lo-examplesect.html

  • Committer: Bazaar Package Importer
  • Author(s): Lionel Porcheron
  • Date: 2008-02-07 00:56:22 UTC
  • mto: (2.1.6 hardy) (6.1.2 sid)
  • mto: This revision was merged to the branch mainline in revision 4.
  • Revision ID: james.westby@ubuntu.com-20080207005622-c2ail8p4d0sk3dnw
Tags: upstream-1.8.2
Import upstream version 1.8.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<html>
2
 
<head>
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">
13
 
</head>
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, &#8220;Large Objects with <span class="application">libpq</span> Example Program&#8221;</a> is a sample program which shows how the large object  
18
 
     interface
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>
23
 
<div class="example">
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">/*--------------------------------------------------------------
26
 
 *
27
 
 * testlo.c--
28
 
 *    test using large objects with libpq
29
 
 *
30
 
 * Copyright (c) 1994, Regents of the University of California
31
 
 *
32
 
 *--------------------------------------------------------------
33
 
 */
34
 
#include &lt;stdio.h&gt;
35
 
#include "libpq-fe.h"
36
 
#include "libpq/libpq-fs.h"
37
 
 
38
 
#define BUFSIZE          1024
39
 
 
40
 
/*
41
 
 * importFile
42
 
 *    import file "in_filename" into database as large object "lobjOid"
43
 
 *
44
 
 */
45
 
Oid
46
 
importFile(PGconn *conn, char *filename)
47
 
{
48
 
    Oid         lobjId;
49
 
    int         lobj_fd;
50
 
    char        buf[BUFSIZE];
51
 
    int         nbytes,
52
 
                tmp;
53
 
    int         fd;
54
 
 
55
 
    /*
56
 
     * open the file to be read in
57
 
     */
58
 
    fd = open(filename, O_RDONLY, 0666);
59
 
    if (fd &lt; 0)
60
 
    {                           /* error */
61
 
        fprintf(stderr, "can't open unix file %s\n", filename);
62
 
    }
63
 
 
64
 
    /*
65
 
     * create the large object
66
 
     */
67
 
    lobjId = lo_creat(conn, INV_READ | INV_WRITE);
68
 
    if (lobjId == 0)
69
 
        fprintf(stderr, "can't create large object\n");
70
 
 
71
 
    lobj_fd = lo_open(conn, lobjId, INV_WRITE);
72
 
 
73
 
    /*
74
 
     * read in from the Unix file and write to the inversion file
75
 
     */
76
 
    while ((nbytes = read(fd, buf, BUFSIZE)) &gt; 0)
77
 
    {
78
 
        tmp = lo_write(conn, lobj_fd, buf, nbytes);
79
 
        if (tmp &lt; nbytes)
80
 
            fprintf(stderr, "error while reading large object\n");
81
 
    }
82
 
 
83
 
    (void) close(fd);
84
 
    (void) lo_close(conn, lobj_fd);
85
 
 
86
 
    return lobjId;
87
 
}
88
 
 
89
 
void
90
 
pickout(PGconn *conn, Oid lobjId, int start, int len)
91
 
{
92
 
    int         lobj_fd;
93
 
    char       *buf;
94
 
    int         nbytes;
95
 
    int         nread;
96
 
 
97
 
    lobj_fd = lo_open(conn, lobjId, INV_READ);
98
 
    if (lobj_fd &lt; 0)
99
 
    {
100
 
        fprintf(stderr, "can't open large object %d\n",
101
 
                lobjId);
102
 
    }
103
 
 
104
 
    lo_lseek(conn, lobj_fd, start, SEEK_SET);
105
 
    buf = malloc(len + 1);
106
 
 
107
 
    nread = 0;
108
 
    while (len - nread &gt; 0)
109
 
    {
110
 
        nbytes = lo_read(conn, lobj_fd, buf, len - nread);
111
 
        buf[nbytes] = ' ';
112
 
        fprintf(stderr, "&gt;&gt;&gt; %s", buf);
113
 
        nread += nbytes;
114
 
    }
115
 
    free(buf);
116
 
    fprintf(stderr, "\n");
117
 
    lo_close(conn, lobj_fd);
118
 
}
119
 
 
120
 
void
121
 
overwrite(PGconn *conn, Oid lobjId, int start, int len)
122
 
{
123
 
    int         lobj_fd;
124
 
    char       *buf;
125
 
    int         nbytes;
126
 
    int         nwritten;
127
 
    int         i;
128
 
 
129
 
    lobj_fd = lo_open(conn, lobjId, INV_WRITE);
130
 
    if (lobj_fd &lt; 0)
131
 
    {
132
 
        fprintf(stderr, "can't open large object %d\n",
133
 
                lobjId);
134
 
    }
135
 
 
136
 
    lo_lseek(conn, lobj_fd, start, SEEK_SET);
137
 
    buf = malloc(len + 1);
138
 
 
139
 
    for (i = 0; i &lt; len; i++)
140
 
        buf[i] = 'X';
141
 
    buf[i] = ' ';
142
 
 
143
 
    nwritten = 0;
144
 
    while (len - nwritten &gt; 0)
145
 
    {
146
 
        nbytes = lo_write(conn, lobj_fd, buf + nwritten, len - nwritten);
147
 
        nwritten += nbytes;
148
 
    }
149
 
    free(buf);
150
 
    fprintf(stderr, "\n");
151
 
    lo_close(conn, lobj_fd);
152
 
}
153
 
 
154
 
/*
155
 
 * exportFile
156
 
 *    export large object "lobjOid" to file "out_filename"
157
 
 *
158
 
 */
159
 
void
160
 
exportFile(PGconn *conn, Oid lobjId, char *filename)
161
 
{
162
 
    int         lobj_fd;
163
 
    char        buf[BUFSIZE];
164
 
    int         nbytes,
165
 
                tmp;
166
 
    int         fd;
167
 
 
168
 
    /*
169
 
     * open the large object
170
 
     */
171
 
    lobj_fd = lo_open(conn, lobjId, INV_READ);
172
 
    if (lobj_fd &lt; 0)
173
 
    {
174
 
        fprintf(stderr, "can't open large object %d\n",
175
 
                lobjId);
176
 
    }
177
 
 
178
 
    /*
179
 
     * open the file to be written to
180
 
     */
181
 
    fd = open(filename, O_CREAT | O_WRONLY, 0666);
182
 
    if (fd &lt; 0)
183
 
    {                           /* error */
184
 
        fprintf(stderr, "can't open unix file %s\n",
185
 
                filename);
186
 
    }
187
 
 
188
 
    /*
189
 
     * read in from the inversion file and write to the Unix file
190
 
     */
191
 
    while ((nbytes = lo_read(conn, lobj_fd, buf, BUFSIZE)) &gt; 0)
192
 
    {
193
 
        tmp = write(fd, buf, nbytes);
194
 
        if (tmp &lt; nbytes)
195
 
        {
196
 
            fprintf(stderr, "error while writing %s\n",
197
 
                    filename);
198
 
        }
199
 
    }
200
 
 
201
 
    (void) lo_close(conn, lobj_fd);
202
 
    (void) close(fd);
203
 
 
204
 
    return;
205
 
}
206
 
 
207
 
void
208
 
exit_nicely(PGconn *conn)
209
 
{
210
 
    PQfinish(conn);
211
 
    exit(1);
212
 
}
213
 
 
214
 
int
215
 
main(int argc, char **argv)
216
 
{
217
 
    char       *in_filename,
218
 
               *out_filename;
219
 
    char       *database;
220
 
    Oid         lobjOid;
221
 
    PGconn     *conn;
222
 
    PGresult   *res;
223
 
 
224
 
    if (argc != 4)
225
 
    {
226
 
        fprintf(stderr, "Usage: %s database_name in_filename out_filename\n",
227
 
                argv[0]);
228
 
        exit(1);
229
 
    }
230
 
 
231
 
    database = argv[1];
232
 
    in_filename = argv[2];
233
 
    out_filename = argv[3];
234
 
 
235
 
    /*
236
 
     * set up the connection
237
 
     */
238
 
    conn = PQsetdb(NULL, NULL, NULL, NULL, database);
239
 
 
240
 
    /* check to see that the backend connection was successfully made */
241
 
    if (PQstatus(conn) == CONNECTION_BAD)
242
 
    {
243
 
        fprintf(stderr, "Connection to database '%s' failed.\n", database);
244
 
        fprintf(stderr, "%s", PQerrorMessage(conn));
245
 
        exit_nicely(conn);
246
 
    }
247
 
 
248
 
    res = PQexec(conn, "begin");
249
 
    PQclear(res);
250
 
 
251
 
    printf("importing file %s\n", in_filename);
252
 
/*  lobjOid = importFile(conn, in_filename); */
253
 
    lobjOid = lo_import(conn, in_filename);
254
 
/*
255
 
    printf("as large object %d.\n", lobjOid);
256
 
 
257
 
    printf("picking out bytes 1000-2000 of the large object\n");
258
 
    pickout(conn, lobjOid, 1000, 1000);
259
 
 
260
 
    printf("overwriting bytes 1000-2000 of the large object with X's\n");
261
 
    overwrite(conn, lobjOid, 1000, 1000);
262
 
*/
263
 
 
264
 
    printf("exporting large object to file %s\n", out_filename);
265
 
/*    exportFile(conn, lobjOid, out_filename); */
266
 
    lo_export(conn, lobjOid, out_filename);
267
 
 
268
 
    res = PQexec(conn, "end");
269
 
    PQclear(res);
270
 
    PQfinish(conn);
271
 
    exit(0);
272
 
}</pre></div>
273
 
</div>
274
 
<br class="example-break">
275
 
</div></body>
276
 
</html>