Re: Backslashes in string literals

Started by Bruce Momjianalmost 20 years ago9 messages
#1Bruce Momjian
pgman@candle.pha.pa.us

Kevin Grittner wrote:

On Wed, Feb 1, 2006 at 10:50 am, in message

<200602011650.k11GoiU23147@candle.pha.pa.us>, Bruce Momjian
<pgman@candle.pha.pa.us> wrote:

(1) I couldn't figure out the best way to obtain a value for
standard_conforming_strings in the psql version of the scanner.

The proper way to do (1) is to call libpq's pqSaveParameterStatus()

from

psql. Take a look for psql's session_username(). It is called
everytime the prompt is printed if the username is required. One

great

feature of using pqSaveParameterStatus() is that it reads server

packets

and keeps the tracked value updated for you without query overhead.

My attempt to do as you suggest isn't working. It behaves as though
the standard_strings() function I added to common.c is always returning
false. (If I comment out the reference the function, and the else
clause, I can get psql to work with the "on" state; otherwise, no joy.
The back end is working fine in all my tests.) I tried to mimic the
technique in the existing functions. Can you give me a clue where I'm
going wrong?

OK, I got it working. The fix is to add GUC_REPORT to guc.c for
standard_conforming_strings. See the same flag on
session_authorization. That will cause libpq to see any changes made to
that variable. Sorry I didn't know that detail before.

-- 
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 359-1001
  +  If your life is a hard drive,     |  13 Roberts Road
  +  Christ can be your backup.        |  Newtown Square, Pennsylvania 19073
#2Kevin Grittner
Kevin.Grittner@wicourts.gov
In reply to: Bruce Momjian (#1)
1 attachment(s)

On Thu, Feb 9, 2006 at 10:31 pm, in message

<200602100431.k1A4VlY21635@candle.pha.pa.us>, Bruce Momjian
<pgman@candle.pha.pa.us> wrote:

OK, I got it working. The fix is to add GUC_REPORT to guc.c for
standard_conforming_strings. See the same flag on
session_authorization. That will cause libpq to see any changes made

to

that variable. Sorry I didn't know that detail before.

Fantastic! I added that flag on my end, and everything I've tried is
working perfectly, except: I found that I didn't get my "expected" file
100% right with my hand-crafted attempt. You're probably already on
that, but just in case it might save you a few minutes -- attached is a
good patch for the "expected" file to go with the new regression test
script for strings.

-Kevin

Attachments:

strings.out.patchapplication/octet-stream; name=strings.out.patchDownload
Index: strings.out
===================================================================
RCS file: /projects/cvsroot/pgsql/src/test/regress/expected/strings.out,v
retrieving revision 1.26
diff -c -r1.26 strings.out
*** strings.out	10 Jul 2005 04:54:33 -0000	1.26
--- strings.out	10 Feb 2006 15:41:46 -0000
***************
*** 193,205 ****
  (1 row)
  
  -- PostgreSQL extension to allow using back reference in replace string;
! SELECT regexp_replace('1112223333', '(\\d{3})(\\d{3})(\\d{4})', '(\\1) \\2-\\3');
   regexp_replace 
  ----------------
   (111) 222-3333
  (1 row)
  
! SELECT regexp_replace('AAA   BBB   CCC   ', '\\s+', ' ', 'g');
   regexp_replace 
  ----------------
   AAA BBB CCC 
--- 193,205 ----
  (1 row)
  
  -- PostgreSQL extension to allow using back reference in replace string;
! SELECT regexp_replace('1112223333', E'(\\d{3})(\\d{3})(\\d{4})', E'(\\1) \\2-\\3');
   regexp_replace 
  ----------------
   (111) 222-3333
  (1 row)
  
! SELECT regexp_replace('AAA   BBB   CCC   ', E'\\s+', ' ', 'g');
   regexp_replace 
  ----------------
   AAA BBB CCC 
***************
*** 895,897 ****
--- 895,980 ----
   t
  (1 row)
  
+ --
+ -- test behavior of escape_string_warning and standard_conforming_strings options
+ --
+ set escape_string_warning = off;
+ set standard_conforming_strings = off;
+ show escape_string_warning;
+  escape_string_warning 
+ -----------------------
+  off
+ (1 row)
+ 
+ show standard_conforming_strings;
+  standard_conforming_strings 
+ -----------------------------
+  off
+ (1 row)
+ 
+ set escape_string_warning = on;
+ set standard_conforming_strings = on;
+ show escape_string_warning;
+  escape_string_warning 
+ -----------------------
+  on
+ (1 row)
+ 
+ show standard_conforming_strings;
+  standard_conforming_strings 
+ -----------------------------
+  on
+ (1 row)
+ 
+ select 'a\bcd' as f1, 'a\b''cd' as f2, 'a\b''''cd' as f3, 'abcd\'   as f4, 'ab\''cd' as f5, '\\' as f6;
+ WARNING:  nonstandard use of escape in a string literal at character 8
+ HINT:  Use the escape string syntax for escapes, e.g., E'\r\n'.
+ WARNING:  nonstandard use of escape in a string literal at character 23
+ HINT:  Use the escape string syntax for escapes, e.g., E'\r\n'.
+ WARNING:  nonstandard use of escape in a string literal at character 40
+ HINT:  Use the escape string syntax for escapes, e.g., E'\r\n'.
+ WARNING:  nonstandard use of escape in a string literal at character 59
+ HINT:  Use the escape string syntax for escapes, e.g., E'\r\n'.
+ WARNING:  nonstandard use of escape in a string literal at character 76
+ HINT:  Use the escape string syntax for escapes, e.g., E'\r\n'.
+ WARNING:  nonstandard use of escape in a string literal at character 93
+ HINT:  Use the escape string syntax for escapes, e.g., E'\r\n'.
+   f1   |   f2   |   f3    |  f4   |   f5   | f6 
+ -------+--------+---------+-------+--------+----
+  a\bcd | a\b'cd | a\b''cd | abcd\ | ab\'cd | \\
+ (1 row)
+ 
+ set standard_conforming_strings = off;
+ select 'a\\bcd' as f1, 'a\\b\'cd' as f2, 'a\\b\'''cd' as f3, 'abcd\\'   as f4, 'ab\\\'cd' as f5, '\\\\' as f6;
+ WARNING:  nonstandard use of \\ in a string literal at character 8
+ HINT:  Use the escape string syntax for backslashes, e.g., E'\\'.
+ WARNING:  nonstandard use of \\ in a string literal at character 24
+ HINT:  Use the escape string syntax for backslashes, e.g., E'\\'.
+ WARNING:  nonstandard use of \\ in a string literal at character 42
+ HINT:  Use the escape string syntax for backslashes, e.g., E'\\'.
+ WARNING:  nonstandard use of \\ in a string literal at character 62
+ HINT:  Use the escape string syntax for backslashes, e.g., E'\\'.
+ WARNING:  nonstandard use of \\ in a string literal at character 80
+ HINT:  Use the escape string syntax for backslashes, e.g., E'\\'.
+ WARNING:  nonstandard use of \\ in a string literal at character 98
+ HINT:  Use the escape string syntax for backslashes, e.g., E'\\'.
+   f1   |   f2   |   f3    |  f4   |   f5   | f6 
+ -------+--------+---------+-------+--------+----
+  a\bcd | a\b'cd | a\b''cd | abcd\ | ab\'cd | \\
+ (1 row)
+ 
+ set escape_string_warning = off;
+ set standard_conforming_strings = on;
+ select 'a\bcd' as f1, 'a\b''cd' as f2, 'a\b''''cd' as f3, 'abcd\'   as f4, 'ab\''cd' as f5, '\\' as f6;
+   f1   |   f2   |   f3    |  f4   |   f5   | f6 
+ -------+--------+---------+-------+--------+----
+  a\bcd | a\b'cd | a\b''cd | abcd\ | ab\'cd | \\
+ (1 row)
+ 
+ set standard_conforming_strings = off;
+ select 'a\\bcd' as f1, 'a\\b\'cd' as f2, 'a\\b\'''cd' as f3, 'abcd\\'   as f4, 'ab\\\'cd' as f5, '\\\\' as f6;
+   f1   |   f2   |   f3    |  f4   |   f5   | f6 
+ -------+--------+---------+-------+--------+----
+  a\bcd | a\b'cd | a\b''cd | abcd\ | ab\'cd | \\
+ (1 row)
+ 
#3Bruce Momjian
pgman@candle.pha.pa.us
In reply to: Kevin Grittner (#2)

Kevin Grittner wrote:

On Thu, Feb 9, 2006 at 10:31 pm, in message

<200602100431.k1A4VlY21635@candle.pha.pa.us>, Bruce Momjian
<pgman@candle.pha.pa.us> wrote:

OK, I got it working. The fix is to add GUC_REPORT to guc.c for
standard_conforming_strings. See the same flag on
session_authorization. That will cause libpq to see any changes made

to

that variable. Sorry I didn't know that detail before.

Fantastic! I added that flag on my end, and everything I've tried is
working perfectly, except: I found that I didn't get my "expected" file
100% right with my hand-crafted attempt. You're probably already on
that, but just in case it might save you a few minutes -- attached is a
good patch for the "expected" file to go with the new regression test
script for strings.

Oh, what I normally do is to look at regression.diff, and if that looks
OK, I just apply it to the expected file like this:

cd expected
patch < ../regression.diff

-- 
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 359-1001
  +  If your life is a hard drive,     |  13 Roberts Road
  +  Christ can be your backup.        |  Newtown Square, Pennsylvania 19073
#4Tom Lane
tgl@sss.pgh.pa.us
In reply to: Bruce Momjian (#3)

Bruce Momjian <pgman@candle.pha.pa.us> writes:

Oh, what I normally do is to look at regression.diff, and if that looks
OK, I just apply it to the expected file like this:

cd expected
patch < ../regression.diff

Oh, that explains a few things ...

It's much better to just copy the result file over the expected file
once you've decided it's OK. The regression.diff file is inexact
because of the diff switches that are used.

If you need to update expected variants your machine doesn't generate,
make a fresh regular diff off the expected and actual, and apply that
to the other variants.

regards, tom lane

#5Bruce Momjian
pgman@candle.pha.pa.us
In reply to: Tom Lane (#4)

Tom Lane wrote:

Bruce Momjian <pgman@candle.pha.pa.us> writes:

Oh, what I normally do is to look at regression.diff, and if that looks
OK, I just apply it to the expected file like this:

cd expected
patch < ../regression.diff

Oh, that explains a few things ...

It's much better to just copy the result file over the expected file
once you've decided it's OK. The regression.diff file is inexact
because of the diff switches that are used.

I am confused. patch dosen't make an indentical file? Example?

If you need to update expected variants your machine doesn't generate,
make a fresh regular diff off the expected and actual, and apply that
to the other variants.

Ah, good point. I often forget about those.

-- 
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 359-1001
  +  If your life is a hard drive,     |  13 Roberts Road
  +  Christ can be your backup.        |  Newtown Square, Pennsylvania 19073
#6Tom Lane
tgl@sss.pgh.pa.us
In reply to: Bruce Momjian (#5)

Bruce Momjian <pgman@candle.pha.pa.us> writes:

Tom Lane wrote:

It's much better to just copy the result file over the expected file
once you've decided it's OK. The regression.diff file is inexact
because of the diff switches that are used.

I am confused. patch dosen't make an indentical file? Example?

Not when the diff it's given to work from ignores spaces ...

regards, tom lane

#7Bruce Momjian
pgman@candle.pha.pa.us
In reply to: Tom Lane (#6)

Tom Lane wrote:

Bruce Momjian <pgman@candle.pha.pa.us> writes:

Tom Lane wrote:

It's much better to just copy the result file over the expected file
once you've decided it's OK. The regression.diff file is inexact
because of the diff switches that are used.

I am confused. patch dosen't make an indentical file? Example?

Not when the diff it's given to work from ignores spaces ...

Ah, interesting. I had not realized that.

-- 
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 359-1001
  +  If your life is a hard drive,     |  13 Roberts Road
  +  Christ can be your backup.        |  Newtown Square, Pennsylvania 19073
#8Kevin Grittner
Kevin.Grittner@wicourts.gov
In reply to: Bruce Momjian (#7)

This patch doesn't leave the standard_conforming_strings entry in guc.c
with the GUC_REPORT flag, which it needs for psql to work right. Should
I submit one last patch with this fix and the proper "expected"
regression file? If so, where should I send it? (The hackers list
won't take a file as big as that patch.)

-Kevin

On Sun, Feb 12, 2006 at 3:17 pm, in message

<200602122117.k1CLHuU22118@candle.pha.pa.us>, Bruce Momjian
<pgman@candle.pha.pa.us> wrote:

Your patch has been added to the PostgreSQL unapplied patches list

at:

http://momjian.postgresql.org/cgi- bin/pgpatches

It will be applied as soon as one of the PostgreSQL committers

reviews

Show quoted text

and approves it.

#9Bruce Momjian
pgman@candle.pha.pa.us
In reply to: Kevin Grittner (#8)

Kevin Grittner wrote:

This patch doesn't leave the standard_conforming_strings entry in guc.c
with the GUC_REPORT flag, which it needs for psql to work right. Should
I submit one last patch with this fix and the proper "expected"
regression file? If so, where should I send it? (The hackers list
won't take a file as big as that patch.)

Oh, I was just going to add the GUC_REPORT when I applied the patch. I
put that email in the patch queue so I would not forget.

I you want, send a mega patch to the patches list,
pgsql-patches@postgresql.org. One large patch is usually safest to
apply.

-- 
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 359-1001
  +  If your life is a hard drive,     |  13 Roberts Road
  +  Christ can be your backup.        |  Newtown Square, Pennsylvania 19073