pg.ddx.io  pgsql-interfaces@postgresql.org mailing list archive  
help / color / mirror / Atom feed
passing values to a function
4+ messages / 2 participants
[nested] [flat]

* passing values to a function
@ 2001-02-18 15:03  Cedar Cox <cedarc@visionforisrael.com>
  0 siblings, 1 reply; 4+ messages in thread

From: Cedar Cox @ 2001-02-18 15:03 UTC (permalink / raw)
  To: pgsql-interfaces


Why does this not work?  Am I doing something wrong?

CREATE FUNCTION uutest(text) RETURNS bool AS '
declare
  varop alias for $1;
begin
  raise notice '' varop is '',varop;
  return false;
end;
' LANGUAGE 'plpgsql';

Giving
 select uutest('INSERT');
returns

NOTICE:   varop is 
 uutest 
--------
 f
(1 row)

Has my PG server entered some weird state or something?

One other question.. Is there an easy way to pass an entire record to a
function (eg, the 'new' record)?  Presently I am passing all 12 fields as
separate parameters.

-Cedar




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

* Re: passing values to a function
@ 2001-02-19 19:32  Jan Wieck <janwieck@Yahoo.com>
  parent: Cedar Cox <cedarc@visionforisrael.com>
  0 siblings, 1 reply; 4+ messages in thread

From: Jan Wieck @ 2001-02-19 19:32 UTC (permalink / raw)
  To: Cedar Cox <cedarc@visionforisrael.com>; +Cc: pgsql-interfaces

Cedar Cox wrote:
>
> Why does this not work?  Am I doing something wrong?
>
> CREATE FUNCTION uutest(text) RETURNS bool AS '
> declare
>   varop alias for $1;
> begin
>   raise notice '' varop is '',varop;

    raise notice '' varop is %'', varop;

>   return false;
> end;
> ' LANGUAGE 'plpgsql';
> [...]
>
> One other question.. Is there an easy way to pass an entire record to a
> function (eg, the 'new' record)?  Presently I am passing all 12 fields as
> separate parameters.

    Yes and no.

    Yes,  PL/pgSQL functions can receive tuples as arguments. The
    only oddity is that they *must* get  aliased  for  access  of
    attributes. Your function would look like:

        CREATE FUNCTION myfunc (mytable) RETURNS text AS '
        DECLARE
            row ALIAS FOR %1;
        BEGIN
            return row.key || '' equals '' row.value;
        END;'
        LANGUAGE 'plpgsql';

    But unfortunately (and there's no really good reason why) no,
    a PL/pgSQL function  having  some  row  somehow  cannot  call
    another  function  handing that row over as a tuple argument.
    Something to work on for 7.2.


Jan

--

#======================================================================#
# It's easier to get forgiveness for being wrong than for being right. #
# Let's break this rule - forgive me.                                  #
#================================================== JanWieck@Yahoo.com #



_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com




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

* Re: passing values to a function
@ 2001-02-19 22:19  Cedar Cox <cedarc@visionforisrael.com>
  parent: Jan Wieck <janwieck@Yahoo.com>
  0 siblings, 1 reply; 4+ messages in thread

From: Cedar Cox @ 2001-02-19 22:19 UTC (permalink / raw)
  To: Jan Wieck <janwieck@Yahoo.com>; +Cc: pgsql-interfaces


> > Why does this not work?  Am I doing something wrong?
..
> >   raise notice '' varop is '',varop;
> 
>     raise notice '' varop is %'', varop;

Arrrg!  How did I miss that one.. I've been writing notice lines for
months now!

Ok, well I seem to have found the "real" problem this time ;)

CREATE FUNCTION update_sentitems(text,int4) RETURNS bool AS '
declare
  varop alias for $1;
begin
  raise notice '' varop = %'',varop;
  return false;
end;
' LANGUAGE 'plpgsql';

Giving
  select update_sentitems('INSERT',1);
returns
  NOTICE:   varop = INSERT
as expected, however if you give
  select update_sentitems('INSERT',null);
this is returned:
  NOTICE:   varop = <NULL>

BTW, it doesn't matter if both parameters are text or int4.  Also, it
doesn't matter which one is given as null, they both turn up null.  
Bottom line seems to be that if you call the function with any parameter
null, they are all null.  Sooo.. what's up here?  Is this a bug that's
been fixed in 7.0.3? (I'm still using 7.0.2).

Thanks
-Cedar




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

* Re: passing values to a function
@ 2001-02-20 09:19  Jan Wieck <janwieck@Yahoo.com>
  parent: Cedar Cox <cedarc@visionforisrael.com>
  0 siblings, 0 replies; 4+ messages in thread

From: Jan Wieck @ 2001-02-20 09:19 UTC (permalink / raw)
  To: Cedar Cox <cedarc@visionforisrael.com>; +Cc: Jan Wieck <janwieck@yahoo.com>; pgsql-interfaces

Cedar Cox wrote:
> Giving
>   select update_sentitems('INSERT',1);
> returns
>   NOTICE:   varop = INSERT
> as expected, however if you give
>   select update_sentitems('INSERT',null);
> this is returned:
>   NOTICE:   varop = <NULL>
>
> BTW, it doesn't matter if both parameters are text or int4.  Also, it
> doesn't matter which one is given as null, they both turn up null.
> Bottom line seems to be that if you call the function with any parameter
> null, they are all null.  Sooo.. what's up here?  Is this a bug that's
> been fixed in 7.0.3? (I'm still using 7.0.2).

    I  wouldn't call it a bug, it was a lack of capabilities with
    the pre7.1 function manager. It had only one flag for NULL to
    pass  to  the  function,  so  it said "there is a NULL, but I
    don't tell you which arg it is".

    7.1 is the first that does it correct.


Jan

--

#======================================================================#
# It's easier to get forgiveness for being wrong than for being right. #
# Let's break this rule - forgive me.                                  #
#================================================== JanWieck@Yahoo.com #



_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com




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


end of thread, other threads:[~2001-02-20 09:19 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2001-02-18 15:03 passing values to a function Cedar Cox <cedarc@visionforisrael.com>
2001-02-19 19:32 ` Jan Wieck <janwieck@Yahoo.com>
2001-02-19 22:19   ` Cedar Cox <cedarc@visionforisrael.com>
2001-02-20 09:19     ` Jan Wieck <janwieck@Yahoo.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