Обсуждение: Request for change in PL/PGSQL function handler

Поиск
Список
Период
Сортировка

Request for change in PL/PGSQL function handler

От
Josh Berkus
Дата:
Jan, Tom, etc:
Currently (7.0.3) the PLPGSQL function compiler permits only one RETURN
statment, at the end of the function.  RETURN statements placed
elsewhere cause a compile error.
This, combined with the lack of an ELSEIF statement, has forced me into
sometimes 7 levels of nested IF..THEN statements.  WHile they work fine,
they're a bit hard to read and edit.  For example, say I want to test
for a, b, c, or d sequentially, under the 7.0.3 compiler, I must:

BEGIN
IF a THEN return_text := 'One';
ELSEIF b THEN     return_text := 'Two';ELSE    IF c THEN        return_text := 'Three';    ELSE        IF d THEN
   return_text := 'Four';        ELSE            return_text := 'Not Found';        END IF;    END IF;END IF;
 
END IF;
RETURN return_text;
END;

As you can see, this kind of structure gets kind of had to read and
maintain for more complex statments.  I have two suggested revisions to
the compiler that would make this much easier:

SUGGESTION A: Support of an ELSEIF statement, as:

IF a THENreturn_text := 'One';
ELSEIF b THENreturn_text := 'Two';
ELSIF c THENreturn_text := 'Three';
...etc.

SUGGESTION B: Allow more than one RETURN statment in the function text,
with funciton processing to terminate as soon as a RETURN is reached in
the program logic, but otherwise be ignored:

IF a THENRETURN 'One';
END IF;

IF b THENRETURN 'Two';
END IF;

...etc.

Both approaches would, from my perspective, make my code easier to read
and maintain.  And, of course, you may have already implemented one or
the other in 7.1 (which I have not yet got to run on an alternate port).

Thanks for your hard work and consideration towards us users.
-Josh Berkus
-- 
______AGLIO DATABASE SOLUTIONS___________________________                                       Josh Berkus  Complete
informationtechnology      josh@agliodbs.com   and data management solutions       (415) 436-9166  for law firms, small
businesses      fax  436-0137   and non-profit organizations.       pager 338-4078                               San
Francisco


Re: Request for change in PL/PGSQL function handler

От
Tom Lane
Дата:
Josh Berkus <josh@agliodbs.com> writes:
>     Currently (7.0.3) the PLPGSQL function compiler permits only one RETURN
> statment, at the end of the function.  RETURN statements placed
> elsewhere cause a compile error.

Say what?

regression=# create function foo(int) returns int as '
regression'# begin
regression'#   if $1 > 10 then return $1;
regression'#   end if;
regression'#   return $1 - 1;
regression'# end;' language 'plpgsql';
CREATE
regression=# select foo(1);foo
-----  0
(1 row)

regression=# select foo(100);foo
-----100
(1 row)

regression=#

Works fine for me in both 7.0.2 and current.
        regards, tom lane


Re: Request for change in PL/PGSQL function handler

От
Josh Berkus
Дата:
Tom,

> Say what?
> 
> regression=# create function foo(int) returns int as '
> regression'# begin
> regression'#   if $1 > 10 then return $1;
> regression'#   end if;
> regression'#   return $1 - 1;
> regression'# end;' language 'plpgsql';
> CREATE

Hmmm?  When I've tried creating similar functions, I got from the
compiler:

Error at or near 'END'

I'll try your code above as a test, then try re-modifying some of my own
functions.
                -Josh Berkus

-- 
______AGLIO DATABASE SOLUTIONS___________________________                                       Josh Berkus  Complete
informationtechnology      josh@agliodbs.com   and data management solutions       (415) 436-9166  for law firms, small
businesses      fax  436-0137   and non-profit organizations.       pager 338-4078                               San
Francisco