Thread: boolean to int

boolean to int

From
Mage
Date:
       Hello,

I'm wondering why pgsql doesn't support boolean typecasts like select
true::int;
Many client applications including php assign 1 to true and 0 to false

I see no use of pgsql boolean type with php, I use int2 or integer.

       Mage



Re: boolean to int

From
Pavel Stehule
Date:
Hello, you can use own cast.

create or replace function int2bool (integer) returns boolean as '
  select case when $1=1 then ''t''::boolean else ''f''::boolean end;
' language sql;

create or replace function bool2int (boolean) returns integer as '
  select case when $1 then 0 else 1 end; ' language sql;

create cast (integer as boolean) with function int2bool(integer)
  as implicit;

create cast (boolean as integer) with function bool2int(boolean)
  as implicit;

regards
Pavel Stehule


On Mon, 15 Mar 2004, Mage wrote:

>        Hello,
>
> I'm wondering why pgsql doesn't support boolean typecasts like select
> true::int;
> Many client applications including php assign 1 to true and 0 to false
>
> I see no use of pgsql boolean type with php, I use int2 or integer.
>
>        Mage
>
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 4: Don't 'kill -9' the postmaster
>


Re: boolean to int

From
"V i s h a l Kashyap @ [Sai Hertz And Control Systems]"
Date:
Dear Mage ,

>
> I'm wondering why pgsql doesn't support boolean typecasts like select
> true::int;
> Many client applications including php assign 1 to true and 0 to false

This was a issue PHP  < 4.2 +  < PostgreSQL 7.3.x  and supports it till
now for backward compatibility

--
Best Regards,
Vishal Kashyap
Director / Lead Developer,
Sai Hertz And Control Systems Pvt Ltd,
http://saihertz.rediffblogs.com
Jabber IM: vishalkashyap@jabber.org
ICQ :      264360076
Yahoo  IM: mailforvishal@yahoo.com
-----------------------------------------------
You yourself, as much as anybody in the entire
universe, deserve your love and affection.
- Buddha
---------------
pgsql=# select marital_status from vishals_life;

marital_status
------------------
Single not looking

1 Row(s) affected

                    ___
                   //\\\
                  ( 0_0 )
----------------o0o-----o0o---------------------


Re: boolean to int

From
Mage
Date:
Pavel Stehule wrote:

>Hello, you can use own cast.
>
>
>
I think I have to create an own type too, because I don't want to use
typecast in every select.
You gave me the idea, thank you.

       Mage





Re: boolean to int

From
Alex Satrapa
Date:
Pavel Stehule wrote:
> create or replace function int2bool (integer) returns boolean as '
>   select case when $1=1 then ''t''::boolean else ''f''::boolean end;
> ' language sql;

I'd do it slightly differently, if only to cater to the principle of least surprise:

create or replace function int2bool (integer) returns boolean as '
   select case when $1=0 then false else true end;
' language sql

That way, 0 maps to false, any non-zero value becomes true.

> create or replace function bool2int (boolean) returns integer as '
>   select case when $1 then 0 else 1 end; ' language sql;

And that's back-to-front ;)

create or replace function bool2int (boolean) returns integer as '
   select case when $1 then 1 else 0 end;
' language sql


Thanks for the example of the use of casts.

Alex Satrapa

Re: boolean to int

From
Mage
Date:
Alex Satrapa wrote:

>
>
> Thanks for the example of the use of casts.

This works fine for functions or inserts, but I still can't use in
select * statements. I need explicit cast or I have to deal with 't' and
'f' in php.
Although a new user defined boolean type and some c programming can
help. Any other way?

       Mage