Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit c3c3902

Browse files
committed
Support '' for literal ' in psql single-quote strings, documentation update.
1 parent eaca117 commit c3c3902

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

doc/src/sgml/ref/psql-ref.sgml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$PostgreSQL: pgsql/doc/src/sgml/ref/psql-ref.sgml,v 1.162 2006/05/26 19:51:29 tgl Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/ref/psql-ref.sgml,v 1.163 2006/05/31 11:35:17 momjian Exp $
33
PostgreSQL documentation
44
-->
55

@@ -2262,22 +2262,22 @@ testdb=&gt; <userinput>SELECT * FROM :foo;</userinput>
22622262
copy the contents of a file into a table column. First load the file into a
22632263
variable and then proceed as above.
22642264
<programlisting>
2265-
testdb=&gt; <userinput>\set content '\'' `cat my_file.txt` '\''</userinput>
2265+
testdb=&gt; <userinput>\set content '''' `cat my_file.txt` ''''</userinput>
22662266
testdb=&gt; <userinput>INSERT INTO my_table VALUES (:content);</userinput>
22672267
</programlisting>
22682268
One possible problem with this approach is that <filename>my_file.txt</filename>
22692269
might contain single quotes. These need to be escaped so that
22702270
they don't cause a syntax error when the second line is processed. This
22712271
could be done with the program <command>sed</command>:
22722272
<programlisting>
2273-
testdb=&gt; <userinput>\set content '\'' `sed -e "s/'/\\\\\\'/g" &lt; my_file.txt` '\''</userinput>
2273+
testdb=&gt; <userinput>\set content '''' `sed -e "s/'/\\\\''/g" &lt; my_file.txt` ''''</userinput>
22742274
</programlisting>
22752275
Observe the correct number of backslashes (6)! It works
22762276
this way: After <application>psql</application> has parsed this
2277-
line, it passes <literal>sed -e "s/'/\\\'/g" &lt; my_file.txt</literal>
2277+
line, it passes <literal>sed -e "s/'/\\''/g" &lt; my_file.txt</literal>
22782278
to the shell. The shell will do its own thing inside the double
22792279
quotes and execute <command>sed</command> with the arguments
2280-
<literal>-e</literal> and <literal>s/'/\\'/g</literal>. When
2280+
<literal>-e</literal> and <literal>s/'/''/g</literal>. When
22812281
<command>sed</command> parses this it will replace the two
22822282
backslashes with a single one and then do the substitution. Perhaps
22832283
at one point you thought it was great that all Unix commands use the

src/bin/psql/psqlscan.l

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
* Portions Copyright (c) 1994, Regents of the University of California
3434
*
3535
* IDENTIFICATION
36-
* $PostgreSQL: pgsql/src/bin/psql/psqlscan.l,v 1.18 2006/05/11 19:15:35 tgl Exp $
36+
* $PostgreSQL: pgsql/src/bin/psql/psqlscan.l,v 1.19 2006/05/31 11:35:17 momjian Exp $
3737
*
3838
*-------------------------------------------------------------------------
3939
*/
@@ -861,6 +861,9 @@ other .
861861

862862
{quote} { return LEXRES_OK; }
863863

864+
/* We don't need a state here because we are already in a string */
865+
{xqdouble} { emit("'", 1); }
866+
864867
"\\n" { appendPQExpBufferChar(output_buf, '\n'); }
865868
"\\t" { appendPQExpBufferChar(output_buf, '\t'); }
866869
"\\b" { appendPQExpBufferChar(output_buf, '\b'); }

0 commit comments

Comments
 (0)