1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>36.12. Large Objects</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><link rel="prev" href="ecpg-library.html" title="36.11. Library Functions" /><link rel="next" href="ecpg-cpp.html" title="36.13. C++ Applications" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">36.12. Large Objects</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="ecpg-library.html" title="36.11. Library Functions">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="ecpg.html" title="Chapter 36. ECPG — Embedded SQL in C">Up</a></td><th width="60%" align="center">Chapter 36. <span class="application">ECPG</span> — Embedded <acronym class="acronym">SQL</acronym> in C</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 16.2 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="ecpg-cpp.html" title="36.13. C++ Applications">Next</a></td></tr></table><hr /></div><div class="sect1" id="ECPG-LO"><div class="titlepage"><div><div><h2 class="title" style="clear: both">36.12. Large Objects <a href="#ECPG-LO" class="id_link">#</a></h2></div></div></div><p>
Large objects are not directly supported by ECPG, but ECPG
application can manipulate large objects through the libpq large
object functions, obtaining the necessary <code class="type">PGconn</code>
object by calling the <code class="function">ECPGget_PGconn()</code>
function. (However, use of
the <code class="function">ECPGget_PGconn()</code> function and touching
<code class="type">PGconn</code> objects directly should be done very carefully
and ideally not mixed with other ECPG database access calls.)
</p><p>
For more details about the <code class="function">ECPGget_PGconn()</code>, see
<a class="xref" href="ecpg-library.html" title="36.11. Library Functions">Section 36.11</a>. For information about the large
object function interface, see <a class="xref" href="largeobjects.html" title="Chapter 35. Large Objects">Chapter 35</a>.
</p><p>
Large object functions have to be called in a transaction block, so
when autocommit is off, <code class="command">BEGIN</code> commands have to
be issued explicitly.
</p><p>
<a class="xref" href="ecpg-lo.html#ECPG-LO-EXAMPLE" title="Example 36.2. ECPG Program Accessing Large Objects">Example 36.2</a> shows an example program that
illustrates how to create, write, and read a large object in an
ECPG application.
</p><div class="example" id="ECPG-LO-EXAMPLE"><p class="title"><strong>Example 36.2. ECPG Program Accessing Large Objects</strong></p><div class="example-contents"><pre class="programlisting">
#include <stdio.h>
#include <stdlib.h>
#include <libpq-fe.h>
#include <libpq/libpq-fs.h>
EXEC SQL WHENEVER SQLERROR STOP;
int
main(void)
{
PGconn *conn;
Oid loid;
int fd;
char buf[256];
int buflen = 256;
char buf2[256];
int rc;
memset(buf, 1, buflen);
EXEC SQL CONNECT TO testdb AS con1;
EXEC SQL SELECT pg_catalog.set_config('search_path', '', false); EXEC SQL COMMIT;
conn = ECPGget_PGconn("con1");
printf("conn = %p\n", conn);
/* create */
loid = lo_create(conn, 0);
if (loid &lt; 0)
printf("lo_create() failed: %s", PQerrorMessage(conn));
printf("loid = %d\n", loid);
/* write test */
fd = lo_open(conn, loid, INV_READ|INV_WRITE);
if (fd &lt; 0)
printf("lo_open() failed: %s", PQerrorMessage(conn));
printf("fd = %d\n", fd);
rc = lo_write(conn, fd, buf, buflen);
if (rc &lt; 0)
printf("lo_write() failed\n");
rc = lo_close(conn, fd);
if (rc &lt; 0)
printf("lo_close() failed: %s", PQerrorMessage(conn));
/* read test */
fd = lo_open(conn, loid, INV_READ);
if (fd &lt; 0)
printf("lo_open() failed: %s", PQerrorMessage(conn));
printf("fd = %d\n", fd);
rc = lo_read(conn, fd, buf2, buflen);
if (rc &lt; 0)
printf("lo_read() failed\n");
rc = lo_close(conn, fd);
if (rc &lt; 0)
printf("lo_close() failed: %s", PQerrorMessage(conn));
/* check */
rc = memcmp(buf, buf2, buflen);
printf("memcmp() = %d\n", rc);
/* cleanup */
rc = lo_unlink(conn, loid);
if (rc &lt; 0)
printf("lo_unlink() failed: %s", PQerrorMessage(conn));
EXEC SQL COMMIT;
EXEC SQL DISCONNECT ALL;
return 0;
}
</pre></div></div><br class="example-break" /></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ecpg-library.html" title="36.11. Library Functions">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="ecpg.html" title="Chapter 36. ECPG — Embedded SQL in C">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="ecpg-cpp.html" title="36.13. C++ Applications">Next</a></td></tr><tr><td width="40%" align="left" valign="top">36.11. Library Functions </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 16.2 Documentation">Home</a></td><td width="40%" align="right" valign="top"> 36.13. <acronym class="acronym">C++</acronym> Applications</td></tr></table></div></body></html>
|