Re: test datatype for ANY

From: Michael Fuhr <mike(at)fuhr(dot)org>
To: NosyMan <nosyman(at)gmail(dot)com>
Cc: pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: test datatype for ANY
Date: 2005-02-11 19:06:22
Message-ID: 20050211190622.GA32524@winnie.fuhr.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Fri, Feb 11, 2005 at 08:40:53PM +0000, NosyMan wrote:
>
> How can I test the type of a parameter passed to a function via ANY data type?
> I want something like this:
>
> CREATE OR REPLACE FUNCTION myfunction(_param ANY) RETURNS INTEGER AS $$
> BEGIN
> IF "_param IS OF INTEGER TYPE" THEN
> -- do something with INTEGER
> END IF;

PostgreSQL has an undocumented IS OF construct:

http://archives.postgresql.org/pgsql-general/2005-01/msg00398.php

Example:

IF param IS OF (integer) THEN
-- do integer stuff
ELSIF param IS OF (boolean) THEN
-- do boolean stuff
END IF;

Since IS OF is undocumented, I'd be careful about using it. I don't
know what plans the developers have for it, but I doubt they'll
feel sorry for you if your code breaks because they removed it or
changed its behavior.

See also the coltype() function I posted as part of the same thread
that mentioned IS OF:

http://archives.postgresql.org/pgsql-general/2005-01/msg00390.php

Using coltype(), the above code would look like this:

IF coltype(param) = 'integer'::regtype THEN
-- do integer stuff
ELSIF coltype(param) = 'boolean'::regtype THEN
-- do boolean stuff
END IF;

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2005-02-11 19:28:13 Re: ERROR: control reached end of function without RETURN
Previous Message Ignacio Colmenero 2005-02-11 18:51:14 ERROR: control reached end of function without RETURN