pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Request for change in PL/PGSQL function handler
3+ messages / 2 participants
[nested] [flat]

* Request for change in PL/PGSQL function handler
@ 2001-01-26 17:27  Josh Berkus <josh@agliodbs.com>
  0 siblings, 1 reply; 3+ messages in thread

From: Josh Berkus @ 2001-01-26 17:27 UTC (permalink / raw)
  To: pgsql-sql

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';
ELSE
	IF 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 THEN
	return_text := 'One';
ELSEIF b THEN
	return_text := 'Two';
ELSIF c THEN
	return_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 THEN
	RETURN 'One';
END IF;

IF b THEN
	RETURN '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 information technology      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



^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Request for change in PL/PGSQL function handler
@ 2001-01-26 19:04  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Josh Berkus <josh@agliodbs.com>
  0 siblings, 1 reply; 3+ messages in thread

From: Tom Lane @ 2001-01-26 19:04 UTC (permalink / raw)
  To: josh@agliodbs.com; +Cc: pgsql-sql

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



^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Request for change in PL/PGSQL function handler
@ 2001-01-26 19:06  Josh Berkus <josh@agliodbs.com>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 3+ messages in thread

From: Josh Berkus @ 2001-01-26 19:06 UTC (permalink / raw)
  To: ; +Cc: pgsql-sql

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 information technology      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



^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2001-01-26 19:06 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2001-01-26 17:27 Request for change in PL/PGSQL function handler Josh Berkus <josh@agliodbs.com>
2001-01-26 19:04 ` Tom Lane <tgl@sss.pgh.pa.us>
2001-01-26 19:06   ` Josh Berkus <josh@agliodbs.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox