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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
|
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>29.3.Client Interfaces</title>
<link rel="stylesheet" href="stylesheet.css" type="text/css">
<link rev="made" href="pgsql-docs@postgresql.org">
<meta name="generator" content="DocBook XSL Stylesheets V1.70.0">
<link rel="start" href="index.html" title="PostgreSQL 8.1.4 Documentation">
<link rel="up" href="largeobjects.html" title="Chapter29.Large Objects">
<link rel="prev" href="lo-implementation.html" title="29.2.Implementation Features">
<link rel="next" href="lo-funcs.html" title="29.4.Server-Side Functions">
<link rel="copyright" href="ln-legalnotice.html" title="Legal Notice">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="sect1" lang="en">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="lo-interfaces"></a>29.3.Client Interfaces</h2></div></div></div>
<p> This section describes the facilities that
<span class="productname">PostgreSQL</span> client interface libraries
provide for accessing large objects. All large object
manipulation using these functions <span class="emphasis"><em>must</em></span> take
place within an SQL transaction block. (This requirement is
strictly enforced as of <span class="productname">PostgreSQL 6.5</span>, though it
has been an implicit requirement in previous versions, resulting
in misbehavior if ignored.)
The <span class="productname">PostgreSQL</span> large object interface is modeled after
the <acronym class="acronym">Unix</acronym> file-system interface, with analogues of
<code class="function">open</code>, <code class="function">read</code>,
<code class="function">write</code>,
<code class="function">lseek</code>, etc.
</p>
<p> Client applications which use the large object interface in
<span class="application">libpq</span> should include the header file
<code class="filename">libpq/libpq-fs.h</code> and link with the
<span class="application">libpq</span> library.
</p>
<div class="sect2" lang="en">
<div class="titlepage"><div><div><h3 class="title">
<a name="id688137"></a>29.3.1.Creating a Large Object</h3></div></div></div>
<p> The function
</p>
<pre class="synopsis">Oid lo_creat(PGconn *conn, int mode);</pre>
<p>
<a name="id688151"></a>
creates a new large object.
The return value is the OID that was assigned to the new large object,
or InvalidOid (zero) on failure.
<em class="replaceable"><code>mode</code></em> is unused and
ignored as of <span class="productname">PostgreSQL</span> 8.1; however, for
backwards compatibility with earlier releases it is best to
set it to <code class="symbol">INV_READ</code>, <code class="symbol">INV_WRITE</code>,
or <code class="symbol">INV_READ</code> <code class="literal">|</code> <code class="symbol">INV_WRITE</code>.
(These symbolic constants are defined
in the header file <code class="filename">libpq/libpq-fs.h</code>.)
</p>
<p> An example:
</p>
<pre class="programlisting">inv_oid = lo_creat(conn, INV_READ|INV_WRITE);</pre>
<p>
</p>
<p> The function
</p>
<pre class="synopsis">Oid lo_create(PGconn *conn, Oid lobjId);</pre>
<p>
<a name="id688228"></a>
also creates a new large object. The OID to be assigned can be
specified by <em class="replaceable"><code>lobjId</code></em>;
if so, failure occurs if that OID is already in use for some large
object. If <em class="replaceable"><code>lobjId</code></em>
is InvalidOid (zero) then <code class="function">lo_create</code> assigns an unused
OID (this is the same behavior as <code class="function">lo_creat</code>).
The return value is the OID that was assigned to the new large object,
or InvalidOid (zero) on failure.
</p>
<p> <code class="function">lo_create</code> is new as of <span class="productname">PostgreSQL</span>
8.1; if this function is run against an older server version, it will
fail and return InvalidOid.
</p>
<p> An example:
</p>
<pre class="programlisting">inv_oid = lo_create(conn, desired_oid);</pre>
<p>
</p>
</div>
<div class="sect2" lang="en">
<div class="titlepage"><div><div><h3 class="title">
<a name="id688296"></a>29.3.2.Importing a Large Object</h3></div></div></div>
<p> To import an operating system file as a large object, call
</p>
<pre class="synopsis">Oid lo_import(PGconn *conn, const char *filename);</pre>
<p>
<a name="id688311"></a>
<em class="replaceable"><code>filename</code></em>
specifies the operating system name of
the file to be imported as a large object.
The return value is the OID that was assigned to the new large object,
or InvalidOid (zero) on failure.
Note that the file is read by the client interface library, not by
the server; so it must exist in the client filesystem and be readable
by the client application.
</p>
</div>
<div class="sect2" lang="en">
<div class="titlepage"><div><div><h3 class="title">
<a name="id688332"></a>29.3.3.Exporting a Large Object</h3></div></div></div>
<p> To export a large object
into an operating system file, call
</p>
<pre class="synopsis">int lo_export(PGconn *conn, Oid lobjId, const char *filename);</pre>
<p>
<a name="id688348"></a>
The <em class="parameter"><code>lobjId</code></em> argument specifies the OID of the large
object to export and the <em class="parameter"><code>filename</code></em> argument
specifies the operating system name of the file. Note that the file is
written by the client interface library, not by the server. Returns 1
on success, -1 on failure.
</p>
</div>
<div class="sect2" lang="en">
<div class="titlepage"><div><div><h3 class="title">
<a name="id688373"></a>29.3.4.Opening an Existing Large Object</h3></div></div></div>
<p> To open an existing large object for reading or writing, call
</p>
<pre class="synopsis">int lo_open(PGconn *conn, Oid lobjId, int mode);</pre>
<p>
<a name="id688388"></a>
The <em class="parameter"><code>lobjId</code></em> argument specifies the OID of the large
object to open. The <em class="parameter"><code>mode</code></em> bits control whether the
object is opened for reading (<code class="symbol">INV_READ</code>), writing
(<code class="symbol">INV_WRITE</code>), or both.
(These symbolic constants are defined
in the header file <code class="filename">libpq/libpq-fs.h</code>.)
A large object cannot be opened before it is created.
<code class="function">lo_open</code> returns a (non-negative) large object
descriptor for later use in <code class="function">lo_read</code>,
<code class="function">lo_write</code>, <code class="function">lo_lseek</code>,
<code class="function">lo_tell</code>, and <code class="function">lo_close</code>.
The descriptor is only valid for
the duration of the current transaction.
On failure, -1 is returned.
</p>
<p> The server currently does not distinguish between modes
<code class="symbol">INV_WRITE</code> and <code class="symbol">INV_READ</code> <code class="literal">|</code>
<code class="symbol">INV_WRITE</code>: you are allowed to read from the descriptor
in either case. However there is a significant difference between
these modes and <code class="symbol">INV_READ</code> alone: with <code class="symbol">INV_READ</code>
you cannot write on the descriptor, and the data read from it will
reflect the contents of the large object at the time of the transaction
snapshot that was active when <code class="function">lo_open</code> was executed,
regardless of later writes by this or other transactions. Reading
from a descriptor opened with <code class="symbol">INV_WRITE</code> returns
data that reflects all writes of other committed transactions as well
as writes of the current transaction. This is similar to the behavior
of <code class="literal">SERIALIZABLE</code> versus <code class="literal">READ COMMITTED</code> transaction
modes for ordinary SQL <code class="command">SELECT</code> commands.
</p>
<p> An example:
</p>
<pre class="programlisting">inv_fd = lo_open(conn, inv_oid, INV_READ|INV_WRITE);</pre>
<p>
</p>
</div>
<div class="sect2" lang="en">
<div class="titlepage"><div><div><h3 class="title">
<a name="id688554"></a>29.3.5.Writing Data to a Large Object</h3></div></div></div>
<p> The function
</p>
<pre class="synopsis">int lo_write(PGconn *conn, int fd, const char *buf, size_t len);</pre>
<p>
<a name="id688569"></a> writes
<em class="parameter"><code>len</code></em> bytes from <em class="parameter"><code>buf</code></em>
to large object descriptor <em class="parameter"><code>fd</code></em>. The <em class="parameter"><code>fd</code></em>
argument must have been returned by a previous
<code class="function">lo_open</code>. The number of bytes actually
written is returned. In the event of an error, the return value
is negative.</p>
</div>
<div class="sect2" lang="en">
<div class="titlepage"><div><div><h3 class="title">
<a name="id688613"></a>29.3.6.Reading Data from a Large Object</h3></div></div></div>
<p> The function
</p>
<pre class="synopsis">int lo_read(PGconn *conn, int fd, char *buf, size_t len);</pre>
<p>
<a name="id688627"></a> reads
<em class="parameter"><code>len</code></em> bytes from large object descriptor
<em class="parameter"><code>fd</code></em> into <em class="parameter"><code>buf</code></em>. The
<em class="parameter"><code>fd</code></em> argument must have been returned by a
previous <code class="function">lo_open</code>. The number of bytes
actually read is returned. In the event of an error, the return
value is negative.</p>
</div>
<div class="sect2" lang="en">
<div class="titlepage"><div><div><h3 class="title">
<a name="id688670"></a>29.3.7.Seeking in a Large Object</h3></div></div></div>
<p> To change the current read or write location associated with a
large object descriptor, call
</p>
<pre class="synopsis">int lo_lseek(PGconn *conn, int fd, int offset, int whence);</pre>
<p>
<a name="id688686"></a> This function moves the
current location pointer for the large object descriptor identified by
<em class="parameter"><code>fd</code></em> to the new location specified by
<em class="parameter"><code>offset</code></em>. The valid values for <em class="parameter"><code>whence</code></em>
are <code class="symbol">SEEK_SET</code> (seek from object start),
<code class="symbol">SEEK_CUR</code> (seek from current position), and
<code class="symbol">SEEK_END</code> (seek from object end). The return value is
the new location pointer, or -1 on error.</p>
</div>
<div class="sect2" lang="en">
<div class="titlepage"><div><div><h3 class="title">
<a name="id688732"></a>29.3.8.Obtaining the Seek Position of a Large Object</h3></div></div></div>
<p> To obtain the current read or write location of a large object descriptor,
call
</p>
<pre class="synopsis">int lo_tell(PGconn *conn, int fd);</pre>
<p>
<a name="id688747"></a> If there is an error, the
return value is negative.</p>
</div>
<div class="sect2" lang="en">
<div class="titlepage"><div><div><h3 class="title">
<a name="id688756"></a>29.3.9.Closing a Large Object Descriptor</h3></div></div></div>
<p> A large object descriptor may be closed by calling
</p>
<pre class="synopsis">int lo_close(PGconn *conn, int fd);</pre>
<p>
<a name="id688771"></a> where <em class="parameter"><code>fd</code></em> is a
large object descriptor returned by <code class="function">lo_open</code>.
On success, <code class="function">lo_close</code> returns zero. On
error, the return value is negative.</p>
<p> Any large object descriptors that remain open at the end of a
transaction will be closed automatically.</p>
</div>
<div class="sect2" lang="en">
<div class="titlepage"><div><div><h3 class="title">
<a name="id688805"></a>29.3.10.Removing a Large Object</h3></div></div></div>
<p> To remove a large object from the database, call
</p>
<pre class="synopsis">int lo_unlink(PGconn *conn, Oid lobjId);</pre>
<p>
<a name="id688819"></a> The
<em class="parameter"><code>lobjId</code></em> argument specifies the OID of the
large object to remove. Returns 1 if successful, -1 on failure.
</p>
</div>
</div></body>
</html>
|