On 2012-04-04 17:10, Tom Lane wrote:
Yeb Havinga<yebhavi...@gmail.com>  writes:
Using a cursor argument name equal to another plpgsql variable results
in the error:
cursor .. has no argument named ""
I think a better way would be to temporarily set
plpgsql_IdentifierLookup to IDENTIFIER_LOOKUP_DECLARE, so as to suppress
variable name lookup in the first place.  The patch as you have it seems
to me to make too many assumptions about what name lookup might produce.



Thank you for looking at it. Attached is a patch that implements your suggestion.

regards,
Yeb

>From b762f447615795c65136d02495cb72f4a1293af3 Mon Sep 17 00:00:00 2001
From: Willem & Yeb <w...@mgrid.net>
Date: Thu, 5 Apr 2012 09:53:38 +0200
Subject: [PATCH] Fix cursor has no argument named "" error.

---
 src/pl/plpgsql/src/gram.y             |    3 +++
 src/test/regress/expected/plpgsql.out |   18 ++++++++++++++++++
 src/test/regress/sql/plpgsql.sql      |   15 +++++++++++++++
 3 files changed, 36 insertions(+), 0 deletions(-)

diff --git a/src/pl/plpgsql/src/gram.y b/src/pl/plpgsql/src/gram.y
index 5a555af..0f37c4f 100644
--- a/src/pl/plpgsql/src/gram.y
+++ b/src/pl/plpgsql/src/gram.y
@@ -3445,9 +3445,12 @@ read_cursor_args(PLpgSQL_var *cursor, int until, const char *expected)
 		if (tok1 == IDENT && tok2 == COLON_EQUALS)
 		{
 			char   *argname;
+			IdentifierLookup oldlookup = plpgsql_IdentifierLookup;
 
 			/* Read the argument name, and find its position */
+			plpgsql_IdentifierLookup = IDENTIFIER_LOOKUP_DECLARE;
 			yylex();
+			plpgsql_IdentifierLookup = oldlookup;
 			argname = yylval.str;
 
 			for (argpos = 0; argpos < row->nfields; argpos++)
diff --git a/src/test/regress/expected/plpgsql.out b/src/test/regress/expected/plpgsql.out
index 5455ade..56cfa57 100644
--- a/src/test/regress/expected/plpgsql.out
+++ b/src/test/regress/expected/plpgsql.out
@@ -2420,6 +2420,24 @@ select namedparmcursor_test8();
                      0
 (1 row)
 
+-- cursor parameter named the same as other plpgsql variables
+create or replace function namedparmcursor_test9(p1 int) returns int4 as $$
+declare
+  c1 cursor (p1 int, p2 int) for
+    select count(*) from tenk1 where thousand = p1 and tenthous = p2;
+  n int4;
+  p2 int4 := 1006;
+begin
+  open c1 (p1 := p1,  p2 := p2);
+  fetch c1 into n;
+  return n;
+end $$ language plpgsql;
+select namedparmcursor_test9(6);
+ namedparmcursor_test9 
+-----------------------
+                     1
+(1 row)
+
 --
 -- tests for "raise" processing
 --
diff --git a/src/test/regress/sql/plpgsql.sql b/src/test/regress/sql/plpgsql.sql
index f577dc3..6b9795d 100644
--- a/src/test/regress/sql/plpgsql.sql
+++ b/src/test/regress/sql/plpgsql.sql
@@ -2053,6 +2053,21 @@ begin
 end $$ language plpgsql;
 select namedparmcursor_test8();
 
+-- cursor parameter named the same as other plpgsql variables
+create or replace function namedparmcursor_test9(p1 int) returns int4 as $$
+declare
+  c1 cursor (p1 int, p2 int) for
+    select count(*) from tenk1 where thousand = p1 and tenthous = p2;
+  n int4;
+  p2 int4 := 1006;
+begin
+  open c1 (p1 := p1,  p2 := p2);
+  fetch c1 into n;
+  return n;
+end $$ language plpgsql;
+
+select namedparmcursor_test9(6);
+
 --
 -- tests for "raise" processing
 --
-- 
1.7.1

-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

Reply via email to