Update documentation for backslashes to mention escape string syntax
authorBruce Momjian <bruce@momjian.us>
Tue, 30 Jan 2007 22:29:40 +0000 (22:29 +0000)
committerBruce Momjian <bruce@momjian.us>
Tue, 30 Jan 2007 22:29:40 +0000 (22:29 +0000)
more, and standard_conforming_strings less, because in the future non-E
strings will not treat backslashes specially.

Also use E'' strings where backslashes are used in examples. (The
existing examples would have drawn warnings.)

Backpatch to 8.2.X.

doc/src/sgml/array.sgml
doc/src/sgml/datatype.sgml
doc/src/sgml/func.sgml
doc/src/sgml/libpq.sgml
doc/src/sgml/plperl.sgml
doc/src/sgml/plpgsql.sgml
doc/src/sgml/pltcl.sgml
doc/src/sgml/rowtypes.sgml
doc/src/sgml/xfunc.sgml

index 35dbf6e5cf94246d6fc96e70ad68205750195630..cdd5529915566defec580291854ade0dae34dfaf 100644 (file)
@@ -597,17 +597,17 @@ SELECT f1[1][-2][3] AS e1, f1[1][-1][5] AS e2
   </para>
 
   <para>
-   As shown previously, when writing an array value you may write double
+   As shown previously, when writing an array value you can write double
    quotes around any individual array element. You <emphasis>must</> do so
    if the element value would otherwise confuse the array-value parser.
    For example, elements containing curly braces, commas (or whatever the
    delimiter character is), double quotes, backslashes, or leading or trailing
    whitespace must be double-quoted.  Empty strings and strings matching the
    word <literal>NULL</> must be quoted, too.  To put a double quote or
-   backslash in a
-   quoted array element value, precede it with a backslash. Alternatively, you
-   can use backslash-escaping to protect all data characters that would
-   otherwise be taken as array syntax.
+   backslash in a quoted array element value, use escape string syntax
+   and precede it with a backslash. Alternatively, you can use
+   backslash-escaping to protect all data characters that would otherwise
+   be taken as array syntax.
   </para>
 
   <para>
@@ -625,16 +625,16 @@ SELECT f1[1][-2][3] AS e1, f1[1][-1][5] AS e2
    backslashes you need.  For example, to insert a <type>text</> array
    value containing a backslash and a double quote, you'd need to write
 <programlisting>
-INSERT ... VALUES ('{"\\\\","\\""}');
+INSERT ... VALUES (E'{"\\\\","\\""}');
 </programlisting>
-   The string-literal processor removes one level of backslashes, so that
+   The escape string processor removes one level of backslashes, so that
    what arrives at the array-value parser looks like <literal>{"\\","\""}</>.
    In turn, the strings fed to the <type>text</> data type's input routine
    become <literal>\</> and <literal>"</> respectively.  (If we were working
    with a data type whose input routine also treated backslashes specially,
    <type>bytea</> for example, we might need as many as eight backslashes
    in the command to get one backslash into the stored array element.)
-   Dollar quoting (see <xref linkend="sql-syntax-dollar-quoting">) may be
+   Dollar quoting (see <xref linkend="sql-syntax-dollar-quoting">) can be
    used to avoid the need to double backslashes.
   </para>
  </note>
index 6b3a4e9d05b37a3411ecd6486a2d9d27398ba988..9dc09cd6047066a126dbbd2ad9fab29e808291f2 100644 (file)
@@ -1103,11 +1103,9 @@ SELECT b, char_length(b) FROM test2;
     of a string literal in an <acronym>SQL</acronym> statement. In
     general, to escape an octet, it is converted into the three-digit
     octal number equivalent of its decimal octet value, and preceded
-    by two backslashes (or one backslash if
-    <varname>standard_conforming_strings</> is <literal>off</>).
-    <xref linkend="datatype-binary-sqlesc"> shows the characters
-    that must be escaped, and gives the alternate escape sequences
-    where applicable.
+    by two backslashes.  <xref linkend="datatype-binary-sqlesc">
+    shows the characters that must be escaped, and gives the alternate
+    escape sequences where applicable.
    </para>
 
    <table id="datatype-binary-sqlesc">
@@ -1127,32 +1125,32 @@ SELECT b, char_length(b) FROM test2;
       <row>
        <entry>0</entry>
        <entry>zero octet</entry>
-       <entry><literal>'\\000'</literal></entry>
-       <entry><literal>SELECT '\\000'::bytea;</literal></entry>
+       <entry><literal>E'\\000'</literal></entry>
+       <entry><literal>SELECT E'\\000'::bytea;</literal></entry>
        <entry><literal>\000</literal></entry>
       </row>
 
       <row>
        <entry>39</entry>
        <entry>single quote</entry>
-       <entry><literal>'\''</literal> or <literal>'\\047'</literal></entry>
-       <entry><literal>SELECT '\''::bytea;</literal></entry>
+       <entry><literal>''''</literal> or <literal>E'\\047'</literal></entry>
+       <entry><literal>SELECT E'\''::bytea;</literal></entry>
        <entry><literal>'</literal></entry>
       </row>
 
       <row>
        <entry>92</entry>
        <entry>backslash</entry>
-       <entry><literal>'\\\\'</literal> or <literal>'\\134'</literal></entry>
-       <entry><literal>SELECT '\\\\'::bytea;</literal></entry>
+       <entry><literal>E'\\\\'</literal> or <literal>E'\\134'</literal></entry>
+       <entry><literal>SELECT E'\\\\'::bytea;</literal></entry>
        <entry><literal>\\</literal></entry>
       </row>
 
       <row>
        <entry>0 to 31 and 127 to 255</entry>
        <entry><quote>non-printable</quote> octets</entry>
-       <entry><literal>'\\<replaceable>xxx'</></literal> (octal value)</entry>
-       <entry><literal>SELECT '\\001'::bytea;</literal></entry>
+       <entry><literal>E'\\<replaceable>xxx'</></literal> (octal value)</entry>
+       <entry><literal>SELECT E'\\001'::bytea;</literal></entry>
        <entry><literal>\001</literal></entry>
       </row>
 
@@ -1175,18 +1173,18 @@ SELECT b, char_length(b) FROM test2;
     string written as a string literal must pass through two parse
     phases in the <productname>PostgreSQL</productname> server.
     The first backslash of each pair is interpreted as an escape
-    character by the string-literal parser (assuming
-    <varname>standard_conforming_strings</> is <literal>off</>)
-    and is therefore consumed, leaving the second backslash of the
-    pair.  The remaining backslash is then recognized by the
+    character by the string-literal parser (assuming escape string
+    syntax is used) and is therefore consumed, leaving the second backslash of the
+    pair.  (Dollar-quoted strings can be used to avoid this level
+    of escaping.)  The remaining backslash is then recognized by the
     <type>bytea</type> input function as starting either a three
     digit octal value or escaping another backslash.  For example,
-    a string literal passed to the server as <literal>'\\001'</literal>
+    a string literal passed to the server as <literal>E'\\001'</literal>
     becomes <literal>\001</literal> after passing through the
-    string-literal parser. The <literal>\001</literal> is then sent
+    escape string parser. The <literal>\001</literal> is then sent
     to the <type>bytea</type> input function, where it is converted
     to a single octet with a decimal value of 1.  Note that the
-    apostrophe character is not treated specially by <type>bytea</type>,
+    single-quote character is not treated specially by <type>bytea</type>,
     so it follows the normal rules for string literals.  (See also
     <xref linkend="sql-syntax-strings">.)
    </para>
@@ -1220,7 +1218,7 @@ SELECT b, char_length(b) FROM test2;
        <entry>92</entry>
        <entry>backslash</entry>
        <entry><literal>\\</literal></entry>
-       <entry><literal>SELECT '\\134'::bytea;</literal></entry>
+       <entry><literal>SELECT E'\\134'::bytea;</literal></entry>
        <entry><literal>\\</literal></entry>
       </row>
 
@@ -1228,7 +1226,7 @@ SELECT b, char_length(b) FROM test2;
        <entry>0 to 31 and 127 to 255</entry>
        <entry><quote>non-printable</quote> octets</entry>
        <entry><literal>\<replaceable>xxx</></literal> (octal value)</entry>
-       <entry><literal>SELECT '\\001'::bytea;</literal></entry>
+       <entry><literal>SELECT E'\\001'::bytea;</literal></entry>
        <entry><literal>\001</literal></entry>
       </row>
 
@@ -1236,7 +1234,7 @@ SELECT b, char_length(b) FROM test2;
        <entry>32 to 126</entry>
        <entry><quote>printable</quote> octets</entry>
        <entry>client character set representation</entry>
-       <entry><literal>SELECT '\\176'::bytea;</literal></entry>
+       <entry><literal>SELECT E'\\176'::bytea;</literal></entry>
        <entry><literal>~</literal></entry>
       </row>
 
index b98c9157daa1285cfc02ef1e32962302f869f43b..f4dbcb37288d1495f1a9576a4cc873fb588773ec 100644 (file)
         Encode binary data to <acronym>ASCII</acronym>-only representation.  Supported
         types are: <literal>base64</>, <literal>hex</>, <literal>escape</>.
        </entry>
-       <entry><literal>encode( '123\\000\\001', 'base64')</literal></entry>
+       <entry><literal>encode( E'123\\000\\001', 'base64')</literal></entry>
        <entry><literal>MTIzAAE=</literal></entry>
       </row>       
 
        <entry>
         Return the given string suitably quoted to be used as a string literal
         in an <acronym>SQL</acronym> statement string.
-        Embedded quotes and backslashes are properly doubled.
+        Embedded single-quotes and backslashes are properly doubled.
        </entry>
        <entry><literal>quote_literal( 'O\'Reilly')</literal></entry>
        <entry><literal>'O''Reilly'</literal></entry>
          <secondary>concatenation</secondary>
         </indexterm>
        </entry>
-       <entry><literal>'\\\\Post'::bytea || '\\047gres\\000'::bytea</literal></entry>
+       <entry><literal>E'\\\\Post'::bytea || E'\\047gres\\000'::bytea</literal></entry>
        <entry><literal>\\Post'gres\000</literal></entry>
       </row>
 
          <primary>get_bit</primary>
         </indexterm>
        </entry>
-       <entry><literal>get_bit('Th\\000omas'::bytea, 45)</literal></entry>
+       <entry><literal>get_bit(E'Th\\000omas'::bytea, 45)</literal></entry>
        <entry><literal>1</literal></entry>
       </row>
 
          <primary>get_byte</primary>
         </indexterm>
        </entry>
-       <entry><literal>get_byte('Th\\000omas'::bytea, 4)</literal></entry>
+       <entry><literal>get_byte(E'Th\\000omas'::bytea, 4)</literal></entry>
        <entry><literal>109</literal></entry>
       </row>
 
        <entry><literal><function>octet_length</function>(<parameter>string</parameter>)</literal></entry>
        <entry><type>int</type></entry>
        <entry>Number of bytes in binary string</entry>
-       <entry><literal>octet_length( 'jo\\000se'::bytea)</literal></entry>
+       <entry><literal>octet_length( E'jo\\000se'::bytea)</literal></entry>
        <entry><literal>5</literal></entry>
       </row>
 
        <entry><literal><function>position</function>(<parameter>substring</parameter> in <parameter>string</parameter>)</literal></entry>
        <entry><type>int</type></entry>
        <entry>Location of specified substring</entry>
-      <entry><literal>position('\\000om'::bytea in 'Th\\000omas'::bytea)</literal></entry>
+      <entry><literal>position(E'\\000om'::bytea in E'Th\\000omas'::bytea)</literal></entry>
        <entry><literal>3</literal></entry>
       </row>
 
          <primary>set_bit</primary>
         </indexterm>
        </entry>
-       <entry><literal>set_bit('Th\\000omas'::bytea, 45, 0)</literal></entry>
+       <entry><literal>set_bit(E'Th\\000omas'::bytea, 45, 0)</literal></entry>
        <entry><literal>Th\000omAs</literal></entry>
       </row>
 
          <primary>set_byte</primary>
         </indexterm>
        </entry>
-       <entry><literal>set_byte('Th\\000omas'::bytea, 4, 64)</literal></entry>
+       <entry><literal>set_byte(E'Th\\000omas'::bytea, 4, 64)</literal></entry>
        <entry><literal>Th\000o@as</literal></entry>
       </row>
 
          <primary>substring</primary>
         </indexterm>
        </entry>
-       <entry><literal>substring('Th\\000omas'::bytea from 2 for 3)</literal></entry>
+       <entry><literal>substring(E'Th\\000omas'::bytea from 2 for 3)</literal></entry>
        <entry><literal>h\000o</literal></entry>
       </row>
 
         <parameter>bytes</parameter> from the start
         and end of <parameter>string</parameter>
        </entry>
-       <entry><literal>trim('\\000'::bytea from '\\000Tom\\000'::bytea)</literal></entry>
+       <entry><literal>trim(E'\\000'::bytea from E'\\000Tom\\000'::bytea)</literal></entry>
        <entry><literal>Tom</literal></entry>
       </row>
      </tbody>
         in <parameter>bytes</parameter> from the start and end of
         <parameter>string</parameter>
       </entry>
-      <entry><literal>btrim('\\000trim\\000'::bytea, '\\000'::bytea)</literal></entry>
+      <entry><literal>btrim(E'\\000trim\\000'::bytea, E'\\000'::bytea)</literal></entry>
       <entry><literal>trim</literal></entry>
      </row>
 
        Decode binary string from <parameter>string</parameter> previously 
        encoded with <function>encode</>.  Parameter type is same as in <function>encode</>.
       </entry>
-      <entry><literal>decode('123\\000456', 'escape')</literal></entry>
+      <entry><literal>decode(E'123\\000456', 'escape')</literal></entry>
       <entry><literal>123\000456</literal></entry>
      </row>
 
        Encode binary string to <acronym>ASCII</acronym>-only representation.  Supported
        types are: <literal>base64</>, <literal>hex</>, <literal>escape</>.
       </entry>
-      <entry><literal>encode('123\\000456'::bytea, 'escape')</literal></entry>
+      <entry><literal>encode(E'123\\000456'::bytea, 'escape')</literal></entry>
       <entry><literal>123\000456</literal></entry>
      </row>
 
         <see>binary strings, length</see>
        </indexterm>
       </entry>
-      <entry><literal>length('jo\\000se'::bytea)</literal></entry>
+      <entry><literal>length(E'jo\\000se'::bytea)</literal></entry>
       <entry><literal>5</literal></entry>
      </row>
 
        Calculates the MD5 hash of <parameter>string</parameter>,
        returning the result in hexadecimal
       </entry>
-      <entry><literal>md5('Th\\000omas'::bytea)</literal></entry>
+      <entry><literal>md5(E'Th\\000omas'::bytea)</literal></entry>
       <entry><literal>8ab2d3c9689aaf18 b4958c334c82d8b1</literal></entry>
      </row>
     </tbody>
@@ -2802,7 +2802,8 @@ cast(-44 as bit(12))           <lineannotation>111111010100</lineannotation>
    <para>
     Note that the backslash already has a special meaning in string
     literals, so to write a pattern constant that contains a backslash
-    you must write two backslashes in an SQL statement.  Thus, writing a pattern
+    you must write two backslashes in an SQL statement (assuming escape
+    string syntax is used).  Thus, writing a pattern
     that actually matches a literal backslash means writing four backslashes
     in the statement.  You can avoid this by selecting a different escape
     character with <literal>ESCAPE</literal>; then a backslash is not special
@@ -3096,7 +3097,7 @@ substring('foobar' from 'o(.)b')   <lineannotation>o</lineannotation>
      substring matching the entire pattern should be inserted.  Write
      <literal>\\</> if you need to put a literal backslash in the replacement
      text.  (As always, remember to double backslashes written in literal
-     constant strings.)
+     constant strings, assuming escape string syntax is used.)
      The <replaceable>flags</> parameter is an optional text
      string containing zero or more single-letter flags that change the
      function's behavior.  Flag <literal>i</> specifies case-insensitive
@@ -3111,7 +3112,7 @@ regexp_replace('foobarbaz', 'b..', 'X')
                                    <lineannotation>fooXbaz</lineannotation>
 regexp_replace('foobarbaz', 'b..', 'X', 'g')
                                    <lineannotation>fooXX</lineannotation>
-regexp_replace('foobarbaz', 'b(..)', 'X\\1Y', 'g')
+regexp_replace('foobarbaz', 'b(..)', E'X\\1Y', 'g')
                                    <lineannotation>fooXarYXazY</lineannotation>
 </programlisting>
    </para>
@@ -3273,7 +3274,8 @@ regexp_replace('foobarbaz', 'b(..)', 'X\\1Y', 'g')
      Remember that the backslash (<literal>\</literal>) already has a special
      meaning in <productname>PostgreSQL</> string literals.
      To write a pattern constant that contains a backslash,
-     you must write two backslashes in the statement.
+     you must write two backslashes in the statement, assuming escape
+     string syntax is used.
     </para>
    </note>
 
@@ -3584,7 +3586,7 @@ regexp_replace('foobarbaz', 'b(..)', 'X\\1Y', 'g')
      Keep in mind that an escape's leading <literal>\</> will need to be
      doubled when entering the pattern as an SQL string constant.  For example:
 <programlisting>
-'123' ~ '^\\d{3}' <lineannotation>true</lineannotation>
+'123' ~ E'^\\d{3}' <lineannotation>true</lineannotation>
 </programlisting>
     </para>
    </note>
@@ -4746,10 +4748,10 @@ SELECT SUBSTRING('XY1234Z', 'Y*?([0-9]{1,3})');
      <listitem>
       <para>
        If you want to have a double quote in the output you must
-       precede it with a backslash, for example <literal>'\\"YYYY
+       precede it with a backslash, for example <literal>E'\\"YYYY
        Month\\"'</literal>. <!-- "" font-lock sanity :-) -->
        (Two backslashes are necessary because the backslash already
-       has a special meaning in a string constant.)
+       has a special meaning when using the escape string syntax.)
       </para>
      </listitem>
 
index 3c46fdd281d22ea8625c008798b1ae8c67f62ff4..c6e60724fd8488e7936352a059175535727f0871 100644 (file)
@@ -910,7 +910,7 @@ in a numeric form that is much easier to compare against.
 
 <para>
 If no value for <literal>standard_conforming_strings</> is reported,
-applications may assume it is <literal>false</>, that is, backslashes
+applications may assume it is <literal>off</>, that is, backslashes
 are treated as escapes in string literals.  Also, the presence of this
 parameter may be taken as an indication that the escape string syntax
 (<literal>E'...'</>) is accepted.
@@ -2488,7 +2488,7 @@ unsigned char *PQescapeByteaConn(PGconn *conn,
    of a <type>bytea</type> literal in an <acronym>SQL</acronym>
    statement. In general, to escape a byte, it is converted into the
    three digit octal number equal to the octet value, and preceded by
-   one or two backslashes. The single quote (<literal>'</>) and backslash
+   usually two backslashes. The single quote (<literal>'</>) and backslash
    (<literal>\</>) characters have special alternative escape
    sequences. See <xref linkend="datatype-binary"> for more
    information. <function>PQescapeByteaConn</function> performs this
index 198580de8765f63e07c0efa7b5b8e2c0b32b8b24..0436c73150b4c6ed1ce4ef4bb73a8a25997a15c9 100644 (file)
@@ -80,10 +80,10 @@ $$ LANGUAGE plperl;
    the function body to be written as a string constant.  It is usually
    most convenient to use dollar quoting (see <xref
    linkend="sql-syntax-dollar-quoting">) for the string constant.
-   If you choose to use regular single-quoted string constant syntax,
-   you must escape single quote marks (<literal>'</>) and backslashes
-   (<literal>\</>) used in the body of the function, typically by
-   doubling them (see <xref linkend="sql-syntax-strings">).
+   If you choose to use escape string syntax <literal>E''</>,
+   you must double the single quote marks (<literal>'</>) and backslashes
+   (<literal>\</>) used in the body of the function 
+   (see <xref linkend="sql-syntax-strings">).
   </para>
 
   <para>
index 2cb1ec7df35198552c305a50dc0afa0e61c84710..fa12a5715d26c4187b1b500ca99800251f948d33 100644 (file)
@@ -288,7 +288,8 @@ $$ LANGUAGE plpgsql;
     <command>CREATE FUNCTION</command> as a string literal.  If you
     write the string literal in the ordinary way with surrounding
     single quotes, then any single quotes inside the function body
-    must be doubled; likewise any backslashes must be doubled.
+    must be doubled; likewise any backslashes must be doubled (assuming
+    escape string syntax is used).
     Doubling quotes is at best tedious, and in more complicated cases
     the code can become downright incomprehensible, because you can
     easily find yourself needing half a dozen or more adjacent quote marks.
@@ -434,13 +435,6 @@ a_output := a_output || $$ if v_$$ || referrer_keys.kind || $$ like '$$
    </varlistentry>
   </variablelist>
 
-   <para>
-    A variant approach is to escape quotation marks in the function body
-    with a backslash rather than by doubling them.  With this method
-    you'll find yourself writing things like <literal>\'\'</> instead
-    of <literal>''''</>.  Some find this easier to keep track of, some
-    do not.
-   </para>
   </sect2>
  </sect1>
 
index 4509863cdcef114040f008751d0e54512501db15..a96c337cd8953d42b97cd5743500edbcb0717891 100644 (file)
@@ -387,11 +387,11 @@ CREATE FUNCTION t1_count(integer, integer) RETURNS integer AS $$
 $$ LANGUAGE pltcl;
 </programlisting>
 
-    We need backslashes inside the query string given to
-    <function>spi_prepare</> to ensure that the
-    <literal>$<replaceable>n</replaceable></> markers will be passed
-    through to <function>spi_prepare</> as-is, and not replaced by Tcl
-    variable substitution.
+        We need backslashes inside the query string given to
+        <function>spi_prepare</> to ensure that the
+        <literal>$<replaceable>n</replaceable></> markers will be passed
+        through to <function>spi_prepare</> as-is, and not replaced by Tcl
+        variable substitution.
 
        </para>
       </listitem>
index 1ba5c43faab602965bf2e952c727e5d391b8dfd7..349b3e43a262e1832b5d120ea434089e97f34ee2 100644 (file)
@@ -294,11 +294,12 @@ INSERT INTO mytab (complex_col.r, complex_col.i) VALUES(1.1, 2.2);
   <para>
    Remember that what you write in an SQL command will first be interpreted
    as a string literal, and then as a composite.  This doubles the number of
-   backslashes you need.  For example, to insert a <type>text</> field
+   backslashes you need (assuming escape string syntax is used).
+   For example, to insert a <type>text</> field
    containing a double quote and a backslash in a composite
    value, you'd need to write
 <programlisting>
-INSERT ... VALUES ('("\\"\\\\")');
+INSERT ... VALUES (E'("\\"\\\\")');
 </programlisting>
    The string-literal processor removes one level of backslashes, so that
    what arrives at the composite-value parser looks like
index 71fe3965da1e1dfd3b2f5346b28380a3ceb2bf28..7deb962cf47f851102683f7241bad2295ae0ea90 100644 (file)
@@ -147,9 +147,9 @@ SELECT clean_emp();
     most convenient to use dollar quoting (see <xref
     linkend="sql-syntax-dollar-quoting">) for the string constant.
     If you choose to use regular single-quoted string constant syntax,
-    you must escape single quote marks (<literal>'</>) and backslashes
-    (<literal>\</>) used in the body of the function, typically by
-    doubling them (see <xref linkend="sql-syntax-strings">).
+    you must double single quote marks (<literal>'</>) and backslashes
+    (<literal>\</>) (assuming escape string syntax) in the body of
+    the function (see <xref linkend="sql-syntax-strings">).
    </para>
 
    <para>