Re: test datatype for ANY - Mailing list pgsql-general

From Michael Fuhr
Subject Re: test datatype for ANY
Date
Msg-id 20050211190622.GA32524@winnie.fuhr.org
Whole thread Raw
In response to test datatype for ANY  (NosyMan <nosyman@gmail.com>)
Responses Re: test datatype for ANY  (Tom Lane <tgl@sss.pgh.pa.us>)
List 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/

pgsql-general by date:

Previous
From: "Ignacio Colmenero"
Date:
Subject: ERROR: control reached end of function without RETURN
Next
From: Tom Lane
Date:
Subject: Re: ERROR: control reached end of function without RETURN