better handling of NULL and booleans in Postgres adapter
authorkonrad <konrad@6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33>
Sun, 23 May 2010 20:15:03 +0000 (20:15 +0000)
committerkonrad <konrad@6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33>
Sun, 23 May 2010 20:15:03 +0000 (20:15 +0000)
git-svn-id: https://silmor.de/svn/softmagic/pack/trunk@467 6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33

phpbase/db_pgsql.php

index 9eac6b1..49aa9c8 100644 (file)
@@ -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