1
- <!-- $PostgreSQL: pgsql/doc/src/sgml/libpq.sgml,v 1.209 2006/05/17 21:50:54 momjian Exp $ -->
1
+ <!-- $PostgreSQL: pgsql/doc/src/sgml/libpq.sgml,v 1.210 2006/05/21 20:19:23 tgl Exp $ -->
2
2
3
3
<chapter id="libpq">
4
4
<title><application>libpq</application> - C Library</title>
@@ -2187,15 +2187,16 @@ It is not thread-safe.
2187
2187
<sect2 id="libpq-exec-escape-string">
2188
2188
<title>Escaping Strings for Inclusion in SQL Commands</title>
2189
2189
2190
+ <indexterm zone="libpq-exec-escape-string"><primary>PQescapeStringConn</></>
2190
2191
<indexterm zone="libpq-exec-escape-string"><primary>PQescapeString</></>
2191
2192
<indexterm zone="libpq-exec-escape-string"><primary>escaping strings</></>
2192
2193
2193
2194
<para>
2194
- <function>PQescapeString </function> escapes a string for use within an SQL
2195
+ <function>PQescapeStringConn </function> escapes a string for use within an SQL
2195
2196
command. This is useful when inserting data values as literal constants
2196
2197
in SQL commands. Certain characters (such as quotes and backslashes) must
2197
2198
be escaped to prevent them from being interpreted specially by the SQL parser.
2198
- <function>PQescapeString </> performs this operation.
2199
+ <function>PQescapeStringConn </> performs this operation.
2199
2200
</para>
2200
2201
2201
2202
<tip>
@@ -2213,36 +2214,68 @@ value is passed as a separate parameter in <function>PQexecParams</> or
2213
2214
its sibling routines.
2214
2215
2215
2216
<synopsis>
2216
- size_t PQescapeString (char *to, const char *from, size_t length);
2217
+ size_t PQescapeStringConn (PGconn *conn,
2218
+ char *to, const char *from, size_t length,
2219
+ int *error);
2217
2220
</synopsis>
2218
2221
</para>
2219
2222
2220
2223
<para>
2224
+ <function>PQescapeStringConn</> writes an escaped
2225
+ version of the <parameter>from</> string to the <parameter>to</>
2226
+ buffer, escaping special characters so that they cannot cause any
2227
+ harm, and adding a terminating zero byte. The single quotes that
2228
+ must surround <productname>PostgreSQL</> string literals are not
2229
+ included in the result string; they should be provided in the SQL
2230
+ command that the result is inserted into.
2221
2231
The parameter <parameter>from</> points to the first character of the string
2222
2232
that is to be escaped, and the <parameter>length</> parameter gives the
2223
- number of characters in this string. A terminating zero byte is not
2233
+ number of bytes in this string. A terminating zero byte is not
2224
2234
required, and should not be counted in <parameter>length</>. (If
2225
2235
a terminating zero byte is found before <parameter>length</> bytes are
2226
- processed, <function>PQescapeString </> stops at the zero; the behavior
2236
+ processed, <function>PQescapeStringConn </> stops at the zero; the behavior
2227
2237
is thus rather like <function>strncpy</>.)
2228
2238
<parameter>to</> shall point to a
2229
- buffer that is able to hold at least one more character than twice
2239
+ buffer that is able to hold at least one more byte than twice
2230
2240
the value of <parameter>length</>, otherwise the behavior is
2231
- undefined. A call to <function>PQescapeString</> writes an escaped
2232
- version of the <parameter>from</> string to the <parameter>to</>
2233
- buffer, replacing special characters so that they cannot cause any
2234
- harm, and adding a terminating zero byte. The single quotes that
2235
- must surround <productname>PostgreSQL</> string literals are not
2236
- included in the result string; they should be provided in the SQL
2237
- command that the result is inserted into.
2241
+ undefined.
2242
+ Behavior is likewise undefined if the <parameter>to</> and <parameter>from</>
2243
+ strings overlap.
2244
+ </para>
2245
+ <para>
2246
+ If the <parameter>error</> parameter is not NULL, then <literal>*error</>
2247
+ is set to zero on success, nonzero on error. Presently the only possible
2248
+ error conditions involve invalid multibyte encoding in the source string.
2249
+ The output string is still generated on error, but it can be expected that
2250
+ the server will reject it as malformed. On error, a suitable message is
2251
+ stored in the <parameter>conn</> object, whether or not <parameter>error</>
2252
+ is NULL.
2238
2253
</para>
2239
2254
<para>
2240
- <function>PQescapeString </> returns the number of characters written
2255
+ <function>PQescapeStringConn </> returns the number of bytes written
2241
2256
to <parameter>to</>, not including the terminating zero byte.
2242
2257
</para>
2258
+
2243
2259
<para>
2244
- Behavior is undefined if the <parameter>to</> and <parameter>from</>
2245
- strings overlap.
2260
+ <synopsis>
2261
+ size_t PQescapeString (char *to, const char *from, size_t length);
2262
+ </synopsis>
2263
+ </para>
2264
+
2265
+ <para>
2266
+ <function>PQescapeString</> is an older, deprecated version of
2267
+ <function>PQescapeStringConn</>; the difference is that it does not
2268
+ take <parameter>conn</> or <parameter>error</> parameters. Because of this,
2269
+ it cannot adjust its behavior depending on the connection properties (such as
2270
+ character encoding) and therefore <emphasis>it may give the wrong results</>.
2271
+ Also, it has no way to report error conditions.
2272
+ </para>
2273
+ <para>
2274
+ <function>PQescapeString</> can be used safely in single-threaded client
2275
+ programs that work with only one <productname>PostgreSQL</> connection at
2276
+ a time (in this case it can find out what it needs to know <quote>behind the
2277
+ scenes</>). In other contexts it is a security hazard and should be avoided
2278
+ in favor of <function>PQescapeStringConn</>.
2246
2279
</para>
2247
2280
</sect2>
2248
2281
@@ -2257,16 +2290,17 @@ strings overlap.
2257
2290
2258
2291
<variablelist>
2259
2292
<varlistentry>
2260
- <term><function>PQescapeBytea </function><indexterm><primary>PQescapeBytea </></></term>
2293
+ <term><function>PQescapeByteaConn </function><indexterm><primary>PQescapeByteaConn </></></term>
2261
2294
<listitem>
2262
2295
<para>
2263
2296
Escapes binary data for use within an SQL command with the type
2264
- <type>bytea</type>. As with <function>PQescapeString </function>,
2297
+ <type>bytea</type>. As with <function>PQescapeStringConn </function>,
2265
2298
this is only used when inserting data directly into an SQL command string.
2266
2299
<synopsis>
2267
- unsigned char *PQescapeBytea(const unsigned char *from,
2268
- size_t from_length,
2269
- size_t *to_length);
2300
+ unsigned char *PQescapeByteaConn(PGconn *conn,
2301
+ const unsigned char *from,
2302
+ size_t from_length,
2303
+ size_t *to_length);
2270
2304
</synopsis>
2271
2305
</para>
2272
2306
@@ -2276,10 +2310,10 @@ unsigned char *PQescapeBytea(const unsigned char *from,
2276
2310
of a <type>bytea</type> literal in an <acronym>SQL</acronym>
2277
2311
statement. In general, to escape a byte, it is converted into the
2278
2312
three digit octal number equal to the octet value, and preceded by
2279
- two backslashes. The single quote (<literal>'</>) and backslash
2313
+ one or two backslashes. The single quote (<literal>'</>) and backslash
2280
2314
(<literal>\</>) characters have special alternative escape
2281
2315
sequences. See <xref linkend="datatype-binary"> for more
2282
- information. <function>PQescapeBytea </function> performs this
2316
+ information. <function>PQescapeByteaConn </function> performs this
2283
2317
operation, escaping only the minimally required bytes.
2284
2318
</para>
2285
2319
@@ -2290,31 +2324,69 @@ unsigned char *PQescapeBytea(const unsigned char *from,
2290
2324
bytes in this binary string. (A terminating zero byte is
2291
2325
neither necessary nor counted.) The <parameter>to_length</parameter>
2292
2326
parameter points to a variable that will hold the resultant
2293
- escaped string length. The result string length includes the terminating
2327
+ escaped string length. This result string length includes the terminating
2294
2328
zero byte of the result.
2295
2329
</para>
2296
2330
2297
2331
<para>
2298
- <function>PQescapeBytea </> returns an escaped version of the
2332
+ <function>PQescapeByteaConn </> returns an escaped version of the
2299
2333
<parameter>from</parameter> parameter binary string in memory
2300
- allocated with <function>malloc()</> (a null pointer is returned if
2301
- memory could not be allocated). This memory must be freed using
2302
- <function>PQfreemem</> when the result is no longer needed. The
2334
+ allocated with <function>malloc()</>. This memory must be freed using
2335
+ <function>PQfreemem()</> when the result is no longer needed. The
2303
2336
return string has all special characters replaced so that they can
2304
2337
be properly processed by the <productname>PostgreSQL</productname>
2305
2338
string literal parser, and the <type>bytea</type> input function. A
2306
2339
terminating zero byte is also added. The single quotes that must
2307
2340
surround <productname>PostgreSQL</productname> string literals are
2308
2341
not part of the result string.
2309
2342
</para>
2343
+
2344
+ <para>
2345
+ On error, a NULL pointer is returned, and a suitable error message
2346
+ is stored in the <parameter>conn</> object. Currently, the only
2347
+ possible error is insufficient memory for the result string.
2348
+ </para>
2349
+ </listitem>
2350
+ </varlistentry>
2351
+
2352
+ <varlistentry>
2353
+ <term><function>PQescapeBytea</function><indexterm><primary>PQescapeBytea</></></term>
2354
+ <listitem>
2355
+ <para>
2356
+ <function>PQescapeBytea</> is an older, deprecated version of
2357
+ <function>PQescapeByteaConn</>.
2358
+ <synopsis>
2359
+ unsigned char *PQescapeBytea(const unsigned char *from,
2360
+ size_t from_length,
2361
+ size_t *to_length);
2362
+ </synopsis>
2363
+ </para>
2364
+
2365
+ <para>
2366
+ The only difference from <function>PQescapeByteaConn</> is that
2367
+ <function>PQescapeBytea</> does not
2368
+ take a <structname>PGconn</> parameter. Because of this, it cannot adjust
2369
+ its behavior depending on the connection properties (in particular,
2370
+ whether standard-conforming strings are enabled)
2371
+ and therefore <emphasis>it may give the wrong results</>. Also, it
2372
+ has no way to return an error message on failure.
2373
+ </para>
2374
+
2375
+ <para>
2376
+ <function>PQescapeBytea</> can be used safely in single-threaded client
2377
+ programs that work with only one <productname>PostgreSQL</> connection at
2378
+ a time (in this case it can find out what it needs to know <quote>behind the
2379
+ scenes</>). In other contexts it is a security hazard and should be
2380
+ avoided in favor of <function>PQescapeByteaConn</>.
2381
+ </para>
2310
2382
</listitem>
2311
2383
</varlistentry>
2312
2384
2313
2385
<varlistentry>
2314
2386
<term><function>PQunescapeBytea</function><indexterm><primary>PQunescapeBytea</></></term>
2315
2387
<listitem>
2316
2388
<para>
2317
- Converts an escaped string representation of binary data into binary
2389
+ Converts a string representation of binary data into binary
2318
2390
data — the reverse of <function>PQescapeBytea</function>.
2319
2391
This is needed when retrieving <type>bytea</type> data in text format,
2320
2392
but not when retrieving it in binary format.
@@ -2324,16 +2396,24 @@ unsigned char *PQunescapeBytea(const unsigned char *from, size_t *to_length);
2324
2396
</synopsis>
2325
2397
</para>
2326
2398
2327
- <para>
2328
- The <parameter>from</parameter> parameter points to an escaped string
2329
- such as might be returned by <function>PQgetvalue</function> when applied to a
2330
- <type>bytea</type> column. <function>PQunescapeBytea</function> converts
2331
- this string representation into its binary representation.
2399
+ <para>
2400
+ The <parameter>from</parameter> parameter points to a string
2401
+ such as might be returned by <function>PQgetvalue</function> when applied
2402
+ to a <type>bytea</type> column. <function>PQunescapeBytea</function>
2403
+ converts this string representation into its binary representation.
2332
2404
It returns a pointer to a buffer allocated with
2333
2405
<function>malloc()</function>, or null on error, and puts the size of
2334
2406
the buffer in <parameter>to_length</parameter>. The result must be
2335
2407
freed using <function>PQfreemem</> when it is no longer needed.
2336
2408
</para>
2409
+
2410
+ <para>
2411
+ This conversion is not exactly the inverse of
2412
+ <function>PQescapeBytea</function>, because the string is not expected
2413
+ to be <quote>escaped</> when received from <function>PQgetvalue</function>.
2414
+ In particular this means there is no need for string quoting considerations,
2415
+ and so no need for a <structname>PGconn</> parameter.
2416
+ </para>
2337
2417
</listitem>
2338
2418
</varlistentry>
2339
2419
@@ -2349,6 +2429,7 @@ void PQfreemem(void *ptr);
2349
2429
2350
2430
<para>
2351
2431
Frees memory allocated by <application>libpq</>, particularly
2432
+ <function>PQescapeByteaConn</function>,
2352
2433
<function>PQescapeBytea</function>,
2353
2434
<function>PQunescapeBytea</function>,
2354
2435
and <function>PQnotifies</function>.
@@ -4000,9 +4081,9 @@ current connection parameters will be used. (Therefore, put more-specific
4000
4081
entries first when you are using wildcards.)
4001
4082
If an entry needs to contain <literal>:</literal> or
4002
4083
<literal>\</literal>, escape this character with <literal>\</literal>.
4003
- A hostname of <literal>localhost</> matches both TCP <literal>host</> (hostname <literal>localhost</>)
4004
- and Unix domain socket <literal>local</> (<literal>pghost</> empty or the default socket directory)
4005
- connections coming from the local machine.
4084
+ A hostname of <literal>localhost</> matches both TCP (hostname
4085
+ <literal>localhost</>) and Unix domain socket (<literal>pghost</> empty or the
4086
+ default socket directory) connections coming from the local machine.
4006
4087
</para>
4007
4088
4008
4089
<para>
0 commit comments