pganalyze / libpg_query

C library for accessing the PostgreSQL parser outside of the server environment
BSD 3-Clause "New" or "Revised" License
1.21k stars 182 forks source link

Parsing plpgsql function with cursor returns an error #257

Open lelit opened 2 months ago

lelit commented 2 months ago

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 commented 2 months ago

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