Support '' for literal ' in psql single-quote strings, documentation update.

This commit is contained in:
Bruce Momjian 2006-05-31 11:35:17 +00:00
parent eaca1175e9
commit c3c3902611
2 changed files with 9 additions and 6 deletions

View File

@ -1,5 +1,5 @@
<!-- <!--
$PostgreSQL: pgsql/doc/src/sgml/ref/psql-ref.sgml,v 1.162 2006/05/26 19:51:29 tgl Exp $ $PostgreSQL: pgsql/doc/src/sgml/ref/psql-ref.sgml,v 1.163 2006/05/31 11:35:17 momjian Exp $
PostgreSQL documentation PostgreSQL documentation
--> -->
@ -2262,7 +2262,7 @@ testdb=&gt; <userinput>SELECT * FROM :foo;</userinput>
copy the contents of a file into a table column. First load the file into a copy the contents of a file into a table column. First load the file into a
variable and then proceed as above. variable and then proceed as above.
<programlisting> <programlisting>
testdb=&gt; <userinput>\set content '\'' `cat my_file.txt` '\''</userinput> testdb=&gt; <userinput>\set content '''' `cat my_file.txt` ''''</userinput>
testdb=&gt; <userinput>INSERT INTO my_table VALUES (:content);</userinput> testdb=&gt; <userinput>INSERT INTO my_table VALUES (:content);</userinput>
</programlisting> </programlisting>
One possible problem with this approach is that <filename>my_file.txt</filename> One possible problem with this approach is that <filename>my_file.txt</filename>
@ -2270,14 +2270,14 @@ testdb=&gt; <userinput>INSERT INTO my_table VALUES (:content);</userinput>
they don't cause a syntax error when the second line is processed. This they don't cause a syntax error when the second line is processed. This
could be done with the program <command>sed</command>: could be done with the program <command>sed</command>:
<programlisting> <programlisting>
testdb=&gt; <userinput>\set content '\'' `sed -e "s/'/\\\\\\'/g" &lt; my_file.txt` '\''</userinput> testdb=&gt; <userinput>\set content '''' `sed -e "s/'/\\\\''/g" &lt; my_file.txt` ''''</userinput>
</programlisting> </programlisting>
Observe the correct number of backslashes (6)! It works Observe the correct number of backslashes (6)! It works
this way: After <application>psql</application> has parsed this this way: After <application>psql</application> has parsed this
line, it passes <literal>sed -e "s/'/\\\'/g" &lt; my_file.txt</literal> line, it passes <literal>sed -e "s/'/\\''/g" &lt; my_file.txt</literal>
to the shell. The shell will do its own thing inside the double to the shell. The shell will do its own thing inside the double
quotes and execute <command>sed</command> with the arguments quotes and execute <command>sed</command> with the arguments
<literal>-e</literal> and <literal>s/'/\\'/g</literal>. When <literal>-e</literal> and <literal>s/'/''/g</literal>. When
<command>sed</command> parses this it will replace the two <command>sed</command> parses this it will replace the two
backslashes with a single one and then do the substitution. Perhaps backslashes with a single one and then do the substitution. Perhaps
at one point you thought it was great that all Unix commands use the at one point you thought it was great that all Unix commands use the

View File

@ -33,7 +33,7 @@
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/bin/psql/psqlscan.l,v 1.18 2006/05/11 19:15:35 tgl Exp $ * $PostgreSQL: pgsql/src/bin/psql/psqlscan.l,v 1.19 2006/05/31 11:35:17 momjian Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -861,6 +861,9 @@ other .
{quote} { return LEXRES_OK; } {quote} { return LEXRES_OK; }
/* We don't need a state here because we are already in a string */
{xqdouble} { emit("'", 1); }
"\\n" { appendPQExpBufferChar(output_buf, '\n'); } "\\n" { appendPQExpBufferChar(output_buf, '\n'); }
"\\t" { appendPQExpBufferChar(output_buf, '\t'); } "\\t" { appendPQExpBufferChar(output_buf, '\t'); }
"\\b" { appendPQExpBufferChar(output_buf, '\b'); } "\\b" { appendPQExpBufferChar(output_buf, '\b'); }