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

From Michael Fuhr
Subject Re: test datatype for ANY
Date
Msg-id 20050211202738.GA32941@winnie.fuhr.org
Whole thread Raw
In response to Re: test datatype for ANY  (Tom Lane <tgl@sss.pgh.pa.us>)
Responses Re: test datatype for ANY  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-general
On Fri, Feb 11, 2005 at 02:32:31PM -0500, Tom Lane wrote:

> There are some limited cases you could handle in plpgsql using the
> polymorphic-functions stuff (ie, ANYELEMENT not ANY) but it still has
> no concept of a run-time type test.

Eh?  What am I misunderstanding then?  The following done in 8.0.1:

CREATE FUNCTION argtype(param anyelement) RETURNS text AS $$
BEGIN
    IF param IS OF (integer) THEN
    RETURN 'integer';
    ELSIF param IS OF (numeric) THEN
    RETURN 'numeric';
    ELSIF param IS OF (boolean) THEN
    RETURN 'boolean';
    ELSIF param IS OF (text) THEN
    RETURN 'text';
    ELSIF param IS OF (date) THEN
    RETURN 'date';
    END IF;

   RETURN 'something else';
END;
$$ LANGUAGE plpgsql IMMUTABLE;

SELECT argtype(1);
 argtype
---------
 integer

SELECT argtype(1.2);
 argtype
---------
 numeric

SELECT argtype('test'::text);
 argtype
---------
 text

SELECT argtype(true);
 argtype
---------
 boolean

CREATE TABLE foo (id integer, foodate date);
INSERT INTO foo VALUES (1, current_date);
SELECT argtype(id) AS idtype, argtype(foodate) AS foodatetype FROM foo;
 idtype  | foodatetype
---------+-------------
 integer | date

SELECT argtype(x) FROM (SELECT foodate FROM foo) AS s(x);
 argtype
---------
 date

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

pgsql-general by date:

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