Re: boolean values in postgresql

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wed, 2005-02-23 at 08:41, Martin Norland wrote:
> Bret Hughes wrote:
> > I am adding some functionality to a php/postgres based system and have a
> > value that I want to be a boolean.  digging around the pg doc I see the
> > boolean datatype.  Cool I say.  So I happily alter the table and add the
> > boolean column notify  all is good until I retrieve it using
> > pg_fetch_object and get a t or f back in myobject->notify
> > 
> > Rats says I.  a simple if ($myobject->notify) { blah } contruct won't
> > work.
> > 
> > So.. after reading a lot of stuff from both the php and pg doc and STFW
> > until my eyes are tired, I am coming to you guys for advice.
> > 
> > Assumption:  There is no easy way to map a pg boolean data type to a
> > value that will evaluate simply in php.  I know I can code it but I
> > would really like to keep this as simple as possible
> > 
> > Question:  Assuming the above assumption is correct, what is the most
> > efficient way to define a column that will take only two values who's
> > values will evaluate as intended in php?
> > 
> > I am going to punt an go with a smallint unlet there is a way to store a
> > bit.  I am  trying to get my head around bytea right now but thought I
> > would ask while I am reading.
> > 
> > Bret
> 
> Personally I would go with the smallint - but another option you have, 
> if you really want to use their boolean, is leaving the boolean and 
> always selecting it as:
> 
> SELECT (boolean_field = 't') AS boolean_field FROM table;
> 
> of course, using a smallint will work 'universally' if you ever changed 
> databases - which may or may not be of interest.

Thanks for all the comments guys,

I ended up going with a bit string with length 1.  tiny storage and
although the update is sort of cumbersome, It does not happen often ( so
far only in one place in the class that manages it ) the checking of the
value is as intuitive as I had wanted to make it.  
from \d in psql:

 notify               | bit(1)                      | 

a partial update statement:

$query = "update mytable" .
" set notify = B'" . rtrim($notify) . "'::bit(1) ".
"where whatever  = 'whoknows'"

and nothing has to be done special for selects and evaluation of the
value.


I would have liked it to work with true/false boolean values but I guess
you cannot have everything.

Since no one schema provided all the features I wanted as far as storage
space and interoperability with php I let the fact that I had not worked
with bit strings and figured there was something to be learned and the
space saving to sway my decision.  It is still in development and I may
change it later but for now I sort of like it.  

I have not read how the value is actually stored and would be surprised
if it is actually less than a byte bit still sort of fun.

Bret

-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux