RES: Creating a function that acept any data type - Mailing list pgsql-general

From Alejandro Michelin Salomon \( Adinet \)
Subject RES: Creating a function that acept any data type
Date
Msg-id !~!UENERkVCMDkAAQACAAAAAAAAAAAAAAAAABgAAAAAAAAAhx491Ki2tUS42D/aMPFg1MKAAAAQAAAA6Bz6tzF2jkWRjTQmuqBraQEAAAAA@adinet.com.uy
Whole thread Raw
In response to Re: Creating a function that acept any data type  (Michael Fuhr <mike@fuhr.org>)
List pgsql-general
Thanks Michael and Tom.

Y try put anyelement and the function works perfectly.

Alejandro Michelin Salomon
Porto Alegre
Brasil


-----Mensagem original-----
De: pgsql-general-owner@postgresql.org
[mailto:pgsql-general-owner@postgresql.org] Em nome de Michael Fuhr
Enviada em: sexta-feira, 10 de março de 2006 17:43
Para: Alejandro Michelin Salomon ( Adinet )
Cc: Pgsql-General
Assunto: Re: [GENERAL] Creating a function that acept any data type


On Fri, Mar 10, 2006 at 05:12:53PM -0300, Alejandro Michelin Salomon (
Adinet ) wrote:
> I am working in a migration. Im am migrating systems based in mysql to
> postgresql.
>
> I am trying to create a function named IFNULL, to not migrate any
> ocurrence of this mysql function in my code.
>
> The IFNULL function is the same of COALESCE in postgresql.

Are you aware of the MySQL Compatibility Functions module?  It has IFNULL.

http://pgfoundry.org/projects/mysqlcompat/
http://software.newsforge.com/article.pl?sid=05/12/15/1611251&from=rss

> This code does not work.
>
> CREATE OR REPLACE FUNCTION IFNULL( xValor ANY, xPadrao ANY ) RETURNS
> ANY AS $$

Change ANY to ANYELEMENT and the code should work.  And for something this
simple you could use an SQL function:

CREATE OR REPLACE FUNCTION ifnull(anyelement, anyelement) RETURNS anyelement
AS $$
  SELECT COALESCE($1, $2);
$$ LANGUAGE sql IMMUTABLE;

You'll have to cast one of the arguments if their types can't be determined.

test=> SELECT ifnull('abc', 'xyz');
ERROR:  could not determine anyarray/anyelement type because input has type
"unknown"

test=> SELECT ifnull('abc', 'xyz'::text);
 ifnull
--------
 abc
(1 row)

test=> SELECT ifnull(NULL, 'xyz'::text);
 ifnull
--------
 xyz
(1 row)

--
Michael Fuhr

---------------------------(end of broadcast)---------------------------
TIP 2: Don't 'kill -9' the postmaster


--
No virus found in this incoming message.
Checked by AVG Free Edition.
Version: 7.1.385 / Virus Database: 268.2.2/280 - Release Date: 13/3/2006


--
No virus found in this incoming message.
Checked by AVG Free Edition.
Version: 7.1.385 / Virus Database: 268.2.2/280 - Release Date: 13/3/2006


--
No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.1.385 / Virus Database: 268.2.4/282 - Release Date: 15/3/2006




--
No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.1.385 / Virus Database: 268.2.4/282 - Release Date: 15/3/2006


pgsql-general by date:

Previous
From: Martijn van Oosterhout
Date:
Subject: Re: PostgreSQL Knowledge Base
Next
From: Abbath
Date:
Subject: Re: tsearch is slow