Add support for IN as alternative to FROM in PL/PgSQL's FETCH statement,
authorNeil Conway <neilc@samurai.com>
Sat, 28 Apr 2007 23:54:59 +0000 (23:54 +0000)
committerNeil Conway <neilc@samurai.com>
Sat, 28 Apr 2007 23:54:59 +0000 (23:54 +0000)
for consistency with the backend's FETCH command. Patch from Pavel
Stehule, reviewed by Neil Conway.

doc/src/sgml/plpgsql.sgml
src/pl/plpgsql/src/gram.y
src/test/regress/expected/plpgsql.out
src/test/regress/sql/plpgsql.sql

index abfc8b6ec64de9d029eb35fad691403683e340d6..97090b7316df304e6901053cd0df4e0dc51373b1 100644 (file)
@@ -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>
index c04f5a9b4d7dd9e3aa67d1713fd4e0ba73135154..48c2ed678528bb1be309a59818701658a8235618 100644 (file)
@@ -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;
 }
index 02ef15c677082ec2df5ef5c46d624adf39e57ea6..669077edee7891245d908e8d5a8c1e52585ce929 100644 (file)
@@ -2245,7 +2245,7 @@ select refcursor_test1('test1');
  test1
 (1 row)
 
-fetch next from test1;
+fetch next in test1;
  a 
 ---
  5
index 1cc9df2de2d94caa56568b6f56206ce3c930c8cf..33637d9e7982a4bf61c051bc261fad0179a72f01 100644 (file)
@@ -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;