From 048cc2e6f19dec6e1d6e2101f00664aef25f845a Mon Sep 17 00:00:00 2001 From: konrad Date: Sun, 23 May 2010 20:15:03 +0000 Subject: [PATCH] better handling of NULL and booleans in Postgres adapter git-svn-id: https://silmor.de/svn/softmagic/pack/trunk@467 6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33 --- phpbase/db_pgsql.php | 29 +++++++++++++++++++++++++---- 1 files changed, 25 insertions(+), 4 deletions(-) diff --git a/phpbase/db_pgsql.php b/phpbase/db_pgsql.php index 9eac6b1..49aa9c8 100644 --- a/phpbase/db_pgsql.php +++ b/phpbase/db_pgsql.php @@ -138,10 +138,20 @@ class PGsqlEngine extends DbEngine $crw=array(); for($j=0;$j<$nf;$j++){ $fn=$fnames[$j]; - if($dbScheme->isIntColumn($table,$fn))$crw[$j]=$row[$j]+0;else - if($dbScheme->isBlobColumn($table,$fn))$crw[$j]=pg_unescape_bytea($row[$j]);else - if($dbScheme->isBoolColumn($table,$fn))$crw[$j]=$row[$j]=='t'; - else $crw[$j]=$row[$j]; + if(pg_field_is_null($res,$i,$j)) + $crw[$j]=null; + else + if($dbScheme->isIntColumn($table,$fn)) + $crw[$j]=$row[$j]+0; + else + if($dbScheme->isBlobColumn($table,$fn)) + $crw[$j]=pg_unescape_bytea($row[$j]); + else + if($dbScheme->isBoolColumn($table,$fn)) + $crw[$j]=$this->unescapeBool($row[$j]); + else //any other type: hope string is ok + $crw[$j]=$row[$j]; + //copy to named index $crw[$fn]=$crw[$j]; } $ret[]=$crw; @@ -275,6 +285,17 @@ class PGsqlEngine extends DbEngine if($s === false||$s===null) return "NULL"; return "'".pg_escape_bytea($this->dbhdl,$s)."'"; } + + /**internal: unescape a postgres bool towards PHP bool*/ + protected function unescapeBool($b) + { + if($b===null)return null; + if(is_bool($b))return $b; + if(is_numeric($b))return ($b+0)!=0; + $b=strtolower($b); + if($b=="t" || $b=="true" || $b=="yes" || $b=="y" || $b=="on")return true; + else return false; + } }; ?> \ No newline at end of file -- 1.7.2.5