From: Owen Jacobson <ojacobson@osl.com>
To: pgsql-sql@postgresql.org
Subject: Re: [HACKERS] ALTER TABLE ( smallinto -> boolean ) ...
Date: Thu, 1 Sep 2005 14:24:59 -0700
Message-ID: <144D12D7DD4EC04F99241498BB4EEDCC08AB03@nelson.osl.com> (raw)
Marc G. Fournier wrote:
> Moved off of -hackers, since its long gotten out of that realm :)
>
> On Thu, 1 Sep 2005, Tom Lane wrote:
>
> > "Marc G. Fournier" <scrappy@postgresql.org> writes:
> >> On Mon, 29 Aug 2005, Tom Lane wrote:
> >>> No, because there's no built-in cast from smallint to bool.
> >
> >> 'k, I just took a read through the "CREATE CAST" man page,
> and don't think
> >> I can use that for this,
> >
> > Sure you can. Make a SQL or PLPGSQL function that does the
> conversion
> > you want and then create a cast using it.
>
> Ah, okay, I just re-read the man page and think I stumbled
> upon what I
> overlooked the first time ...
>
> all I want to do is:
>
> CREATE CAST ( 0 AS boolean )
> WITH FUNCTION <I have to create this>
> AS ASSIGNMENT;
>
> And then each time I try to insert a '0' into a BOOLEAN
> field, it will
> auto convert that (based on my function) to 'f' ...
>
> And I'd need to do a second one for 1 -> 't' ...
>
> Am I reading it right this time ... ?
Here's what I'm using:
CREATE OR REPLACE FUNCTION int2bool (INTEGER) RETURNS BOOLEAN AS $$
SELECT ($1 != 0);
$$ LANGUAGE 'SQL' IMMUTABLE STRICT;
CREATE OR REPLACE FUNCTION bool2int (BOOLEAN) RETURNS INTEGER AS $$
SELECT CASE WHEN $1 THEN 1
WHEN NOT $1 THEN 0
ELSE NULL END;
$$ LANGUAGE 'SQL' IMMUTABLE STRICT;
CREATE CAST (INTEGER AS BOOLEAN)
WITH FUNCTION int2bool (INTEGER)
AS ASSIGNMENT;
CREATE CAST (BOOLEAN AS INTEGER)
WITH FUNCTION bool2int (BOOLEAN)
AS ASSIGNMENT;
done.
Owen
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-sql@postgresql.org
Cc: ojacobson@osl.com
Subject: Re: [HACKERS] ALTER TABLE ( smallinto -> boolean ) ...
In-Reply-To: <144D12D7DD4EC04F99241498BB4EEDCC08AB03@nelson.osl.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
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