1- <!-- $PostgreSQL: pgsql/doc/src/sgml/libpq.sgml,v 1.294 2010/01/20 21:15:21 petere Exp $ -->
1+ <!-- $PostgreSQL: pgsql/doc/src/sgml/libpq.sgml,v 1.295 2010/01/21 14:58:52 rhaas Exp $ -->
22
33<chapter id="libpq">
44 <title><application>libpq</application> - C Library</title>
@@ -2933,20 +2933,48 @@ typedef struct {
29332933 <variablelist>
29342934 <varlistentry>
29352935 <term>
2936- <function>PQescapeStringConn </function>
2936+ <function>PQescapeLiteral </function>
29372937 <indexterm>
2938- <primary>PQescapeStringConn </primary>
2938+ <primary>PQescapeLiteral </primary>
29392939 </indexterm>
29402940 </term>
29412941
29422942 <listitem>
29432943 <para>
2944- <function>PQescapeStringConn</function> escapes a string for
2944+ <synopsis>
2945+ size_t PQescapeLiteral(PGconn *conn, char *str, size_t len)
2946+ </synopsis>
2947+ </para>
2948+
2949+ <para>
2950+ <function>PQescapeLiteral</function> escapes a string for
29452951 use within an SQL command. This is useful when inserting data
29462952 values as literal constants in SQL commands. Certain characters
29472953 (such as quotes and backslashes) must be escaped to prevent them
29482954 from being interpreted specially by the SQL parser.
2949- <function>PQescapeStringConn</> performs this operation.
2955+ <function>PQescapeLiteral</> performs this operation.
2956+ </para>
2957+
2958+ <para>
2959+ <function>PQescapeLiteral</> returns an escaped version of the
2960+ <parameter>str</parameter> parameter in memory allocated with
2961+ <function>malloc()</>. This memory should be freed using
2962+ <function>PQfreemem()</> when the result is no longer needed.
2963+ A terminating zero byte is not required, and should not be
2964+ counted in <parameter>length</>. (If a terminating zero byte is found
2965+ before <parameter>length</> bytes are processed,
2966+ <function>PQescapeLiteral</> stops at the zero; the behavior is
2967+ thus rather like <function>strncpy</>.) The
2968+ return string has all special characters replaced so that they can
2969+ be properly processed by the <productname>PostgreSQL</productname>
2970+ string literal parser. A terminating zero byte is also added. The
2971+ single quotes that must surround <productname>PostgreSQL</productname>
2972+ string literals are included in the result string.
2973+ </para>
2974+
2975+ <para>
2976+ On error, <function>PQescapeLiteral</> returns NULL and a suitable
2977+ message is stored in the <parameter>conn</> object.
29502978 </para>
29512979
29522980 <tip>
@@ -2963,7 +2991,75 @@ typedef struct {
29632991 Note that it is not necessary nor correct to do escaping when a data
29642992 value is passed as a separate parameter in <function>PQexecParams</> or
29652993 its sibling routines.
2994+ </para>
2995+ </listitem>
2996+ </varlistentry>
2997+
2998+ <varlistentry>
2999+ <term>
3000+ <function>PQescapeIdentifier</function>
3001+ <indexterm>
3002+ <primary>PQescapeIdentifier</primary>
3003+ </indexterm>
3004+ </term>
3005+
3006+ <listitem>
3007+ <para>
3008+ <synopsis>
3009+ size_t PQescapeIdentifier(PGconn *conn, char *str, size_t len)
3010+ </synopsis>
3011+ </para>
3012+
3013+ <para>
3014+ <function>PQescapeIndentifier</function> escapes a string for
3015+ use as an SQL identifier, such as a table, column, or function name.
3016+ This is useful when a user-supplied identifier might contain
3017+ special characters that would otherwise not be interpreted as part
3018+ of the identifier by the SQL parser, or when the identifier might
3019+ contain uppercase characters whose case should be preserved.
3020+ </para>
29663021
3022+ <para>
3023+ <function>PQescapeIdentifier</> returns a version of the
3024+ <parameter>str</parameter> parameter escaped as an SQL identifier
3025+ in memory allocated with <function>malloc()</>. This memory must be
3026+ freed using <function>PQfreemem()</> when the result is no longer
3027+ needed. A terminating zero byte is not required, and should not be
3028+ counted in <parameter>length</>. (If a terminating zero byte is found
3029+ before <parameter>length</> bytes are processed,
3030+ <function>PQescapeIdentifier</> stops at the zero; the behavior is
3031+ thus rather like <function>strncpy</>.) The
3032+ return string has all special characters replaced so that it
3033+ will be properly processed as an SQL identifier. A terminating zero byte
3034+ is also added. The return string will also be surrounded by double
3035+ quotes.
3036+ </para>
3037+
3038+ <para>
3039+ On error, <function>PQescapeIdentifier</> returns NULL and a suitable
3040+ message is stored in the <parameter>conn</> object.
3041+ </para>
3042+
3043+ <tip>
3044+ <para>
3045+ As with string literals, to prevent SQL injection attacks,
3046+ SQL identifiers must be escaped when they are received from an
3047+ untrustworthy source.
3048+ </para>
3049+ </tip>
3050+ </listitem>
3051+ </varlistentry>
3052+
3053+ <varlistentry>
3054+ <term>
3055+ <function>PQescapeStringConn</function>
3056+ <indexterm>
3057+ <primary>PQescapeStringConn</primary>
3058+ </indexterm>
3059+ </term>
3060+
3061+ <listitem>
3062+ <para>
29673063 <synopsis>
29683064 size_t PQescapeStringConn (PGconn *conn,
29693065 char *to, const char *from, size_t length,
@@ -2972,12 +3068,12 @@ typedef struct {
29723068 </para>
29733069
29743070 <para>
2975- <function>PQescapeStringConn</> writes an escaped version of the
2976- <parameter>from </> string to the <parameter>to </> buffer, escaping
2977- special characters so that they cannot cause any harm, and adding a
2978- terminating zero byte. The single quotes that must surround
2979- <productname>PostgreSQL</> string literals are not included in the
2980- result string ; they should be provided in the SQL command that the
3071+ <function>PQescapeStringConn</> escapes string literals, much like
3072+ <function>PQescapeLiteral </>. Unlike <function>PQescapeLiteral </>,
3073+ the caller is responsible for providing an appropriately sized buffer.
3074+ Furthermore, <function>PQescapeStringConn</> does not generate the
3075+ single quotes that must surround <productname>PostgreSQL</> string
3076+ literals ; they should be provided in the SQL command that the
29813077 result is inserted into. The parameter <parameter>from</> points to
29823078 the first character of the string that is to be escaped, and the
29833079 <parameter>length</> parameter gives the number of bytes in this
@@ -3093,7 +3189,7 @@ typedef struct {
30933189 <para>
30943190 <function>PQescapeByteaConn</> returns an escaped version of the
30953191 <parameter>from</parameter> parameter binary string in memory
3096- allocated with <function>malloc()</>. This memory must be freed using
3192+ allocated with <function>malloc()</>. This memory should be freed using
30973193 <function>PQfreemem()</> when the result is no longer needed. The
30983194 return string has all special characters replaced so that they can
30993195 be properly processed by the <productname>PostgreSQL</productname>
@@ -4285,7 +4381,7 @@ typedef struct {
42854381 be non-<symbol>NULL</symbol>. <parameter>*buffer</> is set to
42864382 point to the allocated memory, or to <symbol>NULL</symbol> in cases
42874383 where no buffer is returned. A non-<symbol>NULL</symbol> result
4288- buffer must be freed using <function>PQfreemem</> when no longer
4384+ buffer should be freed using <function>PQfreemem</> when no longer
42894385 needed.
42904386 </para>
42914387
0 commit comments