kputnam / piggly

PL/pgSQL stored procedure code coverage tool
Other
69 stars 14 forks source link

Functions that declare a non-trivial cursor fail to compile #45

Open hawicz opened 4 years ago

hawicz commented 4 years ago

A function that looks like this:

create or replace function foo()
returns void
language plpgsql
as $$
DECLARE
   DECLARE mycur CURSOR FOR SELECT * from pg_database;
BEGIN
   return;
END;
$$;

Results in:

        ****
        Error compiling procedure public.foo
        Source: /home/user/piggly/cache/Dumper/511efb81e82810ddd8b9359d7c6a7758.plpgsql
        Exception Message:
        Expected one of [ \t\n\v\f\r], 'as', 'not', ':=', '=', '(', '[', [a-z\200-\377_0-9$%], '/*', '--', ';' at line 2, column 36 (byte 44) after declare

declare mycur cursor for select


On the other hand, changing it to say "... SELECT 1..." instead of "...SELECT *..." works (though of course it's not very useful to have a query that does effectively nothing).

kputnam commented 4 years ago

Thanks for reporting this. I wonder if the repeated DECLARE is causing the issue here. Can you try rewriting your example like so?

create or replace function foo()
returns void
language plpgsql
as $$
DECLARE
   mycur CURSOR FOR SELECT * from pg_database;
BEGIN
   return;
END;
$$;

If that doesn't work, it looks fixable with minor changes to the grammar specification. If you'd like to take a stab at it, I'll be happy to review and merge a PR. Otherwise, I will get to it but will probably take a week or two.

hawicz commented 4 years ago

Looks fairly simple to tweak, I'll take a look.