Thread: Re: [HACKERS] ALTER TABLE ( smallinto -> boolean ) ...

Re: [HACKERS] ALTER TABLE ( smallinto -> boolean ) ...

From
"Marc G. Fournier"
Date:
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 ... ?

----
Marc G. Fournier           Hub.Org Networking Services (http://www.hub.org)
Email: scrappy@hub.org           Yahoo!: yscrappy              ICQ: 7615664


Re: [HACKERS] ALTER TABLE ( smallinto -> boolean ) ...

From
Tom Lane
Date:
"Marc G. Fournier" <scrappy@postgresql.org> writes:
> 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' ...

No, you want one function from smallint to boolean, and the cast the
same way.  (The cast is really just syntactic sugar for invoking the
function.)

Depending on what you want this to do, you might have to make the cast
IMPLICIT rather than ASSIGNMENT.  I'd try ASSIGNMENT first, though,
since it's less likely to bite you when you weren't expecting it.
        regards, tom lane