libpg_query icon indicating copy to clipboard operation
libpg_query copied to clipboard

Parsing plpgsql function with cursor returns an error

Open lelit opened this issue 1 year ago • 2 comments

Hi,

investigating on a similar issue reported against pglast, I found that effectively the following example

#include <pg_query.h>
#include <stdio.h>
#include <stdlib.h>

int main() {
  PgQueryPlpgsqlParseResult result;

  result = pg_query_parse_plpgsql("\
   CREATE FUNCTION foo(cmd TEXT) RETURNS void AS $$\
   DECLARE\
     i INT;\
     c CURSOR FOR SELECT generate_series(1,10);\
   BEGIN\
     FOR i IN c LOOP\
       RAISE NOTICE 'i is %',i;\
     END LOOP;\
   END\
   $$ LANGUAGE plpgsql;");
  if (result.error) {
    printf("error: %s at %d\n", result.error->message, result.error->cursorpos);
  } else {
    printf("%s\n", result.plpgsql_funcs);
  }

  pg_query_free_plpgsql_parse_result(result);

  // Optional, this ensures all memory is freed upon program exit (useful when running Valgrind)
  pg_query_exit();

  return 0;
}

emits

error: syntax error at or near "c" at 0

I tried looking at open issues targeting plpgsql, but failed to find one related to cursors.

lelit avatar Aug 26 '24 06:08 lelit

FYI, the OP referenced the PR #256 that apparently fixes this.

lelit avatar Aug 26 '24 10:08 lelit

Closing this since the mentioned PR #256 was merged.

lfittl avatar Apr 02 '25 05:04 lfittl