From f2321a3f37b74b42d20ec787afb7ee4a29655a3e Mon Sep 17 00:00:00 2001
From: Neil Conway <neilc@samurai.com>
Date: Sat, 28 Apr 2007 23:54:59 +0000
Subject: [PATCH] Add support for IN as alternative to FROM in PL/PgSQL's FETCH
 statement, for consistency with the backend's FETCH command. Patch from Pavel
 Stehule, reviewed by Neil Conway.

---
 doc/src/sgml/plpgsql.sgml             |  4 ++--
 src/pl/plpgsql/src/gram.y             | 25 +++++++++++++++++++------
 src/test/regress/expected/plpgsql.out |  2 +-
 src/test/regress/sql/plpgsql.sql      |  2 +-
 4 files changed, 23 insertions(+), 10 deletions(-)

diff --git a/doc/src/sgml/plpgsql.sgml b/doc/src/sgml/plpgsql.sgml
index abfc8b6ec64..97090b7316d 100644
--- a/doc/src/sgml/plpgsql.sgml
+++ b/doc/src/sgml/plpgsql.sgml
@@ -1,4 +1,4 @@
-<!-- $PostgreSQL: pgsql/doc/src/sgml/plpgsql.sgml,v 1.107 2007/04/16 17:21:22 tgl Exp $ -->
+<!-- $PostgreSQL: pgsql/doc/src/sgml/plpgsql.sgml,v 1.108 2007/04/28 23:54:58 neilc Exp $ -->
 
 <chapter id="plpgsql"> 
   <title><application>PL/pgSQL</application> - <acronym>SQL</acronym> Procedural Language</title>
@@ -2523,7 +2523,7 @@ OPEN curs3(42);
      <title><literal>FETCH</></title>
 
 <synopsis>
-FETCH <optional> <replaceable>direction</replaceable> FROM </optional> <replaceable>cursor</replaceable> INTO <replaceable>target</replaceable>;
+FETCH <optional> <replaceable>direction</replaceable> { FROM | IN } </optional> <replaceable>cursor</replaceable> INTO <replaceable>target</replaceable>;
 </synopsis>
 
     <para>
diff --git a/src/pl/plpgsql/src/gram.y b/src/pl/plpgsql/src/gram.y
index c04f5a9b4d7..48c2ed67852 100644
--- a/src/pl/plpgsql/src/gram.y
+++ b/src/pl/plpgsql/src/gram.y
@@ -9,7 +9,7 @@
  *
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/pl/plpgsql/src/gram.y,v 1.100 2007/04/16 17:21:23 tgl Exp $
+ *	  $PostgreSQL: pgsql/src/pl/plpgsql/src/gram.y,v 1.101 2007/04/28 23:54:59 neilc Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -2043,13 +2043,15 @@ read_fetch_direction(void)
 	else if (pg_strcasecmp(yytext, "absolute") == 0)
 	{
 		fetch->direction = FETCH_ABSOLUTE;
-		fetch->expr = plpgsql_read_expression(K_FROM, "FROM");
+		fetch->expr = read_sql_construct(K_FROM, K_IN, "FROM or IN",
+										 "SELECT ", true, true, NULL);
 		check_FROM = false;
 	}
 	else if (pg_strcasecmp(yytext, "relative") == 0)
 	{
 		fetch->direction = FETCH_RELATIVE;
-		fetch->expr = plpgsql_read_expression(K_FROM, "FROM");
+		fetch->expr = read_sql_construct(K_FROM, K_IN, "FROM or IN",
+										 "SELECT ", true, true, NULL);
 		check_FROM = false;
 	}
 	else if (pg_strcasecmp(yytext, "forward") == 0)
@@ -2060,6 +2062,13 @@ read_fetch_direction(void)
 	{
 		fetch->direction = FETCH_BACKWARD;
 	}
+	else if (tok != T_SCALAR)
+	{
+		plpgsql_push_back_token(tok);
+		fetch->expr = read_sql_construct(K_FROM, K_IN, "FROM or IN",
+										 "SELECT ", true, true, NULL);
+		check_FROM = false;
+	}
 	else
 	{
 		/* Assume there's no direction clause */
@@ -2067,9 +2076,13 @@ read_fetch_direction(void)
 		check_FROM = false;
 	}
 
-	/* check FROM keyword after direction's specification */
-	if (check_FROM && yylex() != K_FROM)
-		yyerror("expected \"FROM\"");
+	/* check FROM or IN keyword after direction's specification */
+	if (check_FROM)
+	{
+		tok = yylex();
+		if (tok != K_FROM && tok != K_IN)
+			yyerror("expected FROM or IN");
+	}
 
 	return fetch;
 }
diff --git a/src/test/regress/expected/plpgsql.out b/src/test/regress/expected/plpgsql.out
index 02ef15c6770..669077edee7 100644
--- a/src/test/regress/expected/plpgsql.out
+++ b/src/test/regress/expected/plpgsql.out
@@ -2245,7 +2245,7 @@ select refcursor_test1('test1');
  test1
 (1 row)
 
-fetch next from test1;
+fetch next in test1;
  a 
 ---
  5
diff --git a/src/test/regress/sql/plpgsql.sql b/src/test/regress/sql/plpgsql.sql
index 1cc9df2de2d..33637d9e798 100644
--- a/src/test/regress/sql/plpgsql.sql
+++ b/src/test/regress/sql/plpgsql.sql
@@ -1918,7 +1918,7 @@ $$ language plpgsql;
 begin;
 
 select refcursor_test1('test1');
-fetch next from test1;
+fetch next in test1;
 
 select refcursor_test1('test2');
 fetch all from test2;
-- 
GitLab