From c3c3902611f884bef3f37b95c7a082f6f2b9a238 Mon Sep 17 00:00:00 2001 From: Bruce Momjian Date: Wed, 31 May 2006 11:35:17 +0000 Subject: [PATCH] Support '' for literal ' in psql single-quote strings, documentation update. --- doc/src/sgml/ref/psql-ref.sgml | 10 +++++----- src/bin/psql/psqlscan.l | 5 ++++- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/doc/src/sgml/ref/psql-ref.sgml b/doc/src/sgml/ref/psql-ref.sgml index f00b4c09f6..13c77595e6 100644 --- a/doc/src/sgml/ref/psql-ref.sgml +++ b/doc/src/sgml/ref/psql-ref.sgml @@ -1,5 +1,5 @@ @@ -2262,7 +2262,7 @@ testdb=> SELECT * FROM :foo; copy the contents of a file into a table column. First load the file into a variable and then proceed as above. -testdb=> \set content '\'' `cat my_file.txt` '\'' +testdb=> \set content '''' `cat my_file.txt` '''' testdb=> INSERT INTO my_table VALUES (:content); One possible problem with this approach is that my_file.txt @@ -2270,14 +2270,14 @@ testdb=> INSERT INTO my_table VALUES (:content); they don't cause a syntax error when the second line is processed. This could be done with the program sed: -testdb=> \set content '\'' `sed -e "s/'/\\\\\\'/g" < my_file.txt` '\'' +testdb=> \set content '''' `sed -e "s/'/\\\\''/g" < my_file.txt` '''' Observe the correct number of backslashes (6)! It works this way: After psql has parsed this - line, it passes sed -e "s/'/\\\'/g" < my_file.txt + line, it passes sed -e "s/'/\\''/g" < my_file.txt to the shell. The shell will do its own thing inside the double quotes and execute sed with the arguments - -e and s/'/\\'/g. When + -e and s/'/''/g. When sed parses this it will replace the two 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 diff --git a/src/bin/psql/psqlscan.l b/src/bin/psql/psqlscan.l index 4a344baf6b..2c19b7ae9e 100644 --- a/src/bin/psql/psqlscan.l +++ b/src/bin/psql/psqlscan.l @@ -33,7 +33,7 @@ * Portions Copyright (c) 1994, Regents of the University of California * * 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; } + /* We don't need a state here because we are already in a string */ +{xqdouble} { emit("'", 1); } + "\\n" { appendPQExpBufferChar(output_buf, '\n'); } "\\t" { appendPQExpBufferChar(output_buf, '\t'); } "\\b" { appendPQExpBufferChar(output_buf, '\b'); }