List Info

Thread: DB_PORTABILITY_FIX_BOOLEANS




DB_PORTABILITY_FIX_BOOLEANS
user name
2006-04-25 19:16:17
What have been done?
- Implemented a new portability option called
"DB_PORTABILITY_FIX_BOOLEANS"

What does it do?
- Convert the values returned from the DBMS's from fields
of type "bool" 
or "boolean" into real PHP booleans

Why did you do it?
- When reading PostGresSQL's boolean values, it returns a
string 
containing "t" for true and "f" for
false. If you use the returned 
values as is, expecting to get real boolean values, PHP will
always 
evaluates the strings "t" or "f" to
true. If you enable this portability 
option, the DB class will handle the conversion for the
bool-type 
fields. (bool-type fields are expected to be
"bool" or "boolean" ones, 
and this can be easily updated in the future releases by
adding new 
types into the specific array within the conversion method)

How does it work?
- I've created a new private method (function) into
common.php, which 
handles the conversion from "false booleans" to
"real booleans", and 
simply call this method from inside the
"fetchInto()" method of each 
driver, right after all other portability options.


Hope this can be used for future releases/updates.

Regards,
asm
? DB.diff
Index: DB.php
============================================================
=======
RCS file: /repository/pear/DB/DB.php,v
retrieving revision 1.81
diff -u -r1.81 DB.php
--- DB.php	1 Mar 2006 23:33:36 -0000	1.81
+++ DB.php	25 Apr 2006 18:54:36 -0000
 -384,9
+384,18 
 define('DB_PORTABILITY_NULL_TO_EMPTY', 32);
 
 /**
+ * Convert returned values from boolean fields to real
+ * PHP boolean values (eg. 1 to true, "f" to
false, etc.)
+ *
+ * + pgsql: convert the "t" or "f"
string returned by
+ * the bool-type fields into true or false PHP booleans.
+ */
+define('DB_PORTABILITY_FIX_BOOLEANS', 64);
+
+/**
  * Turn on all portability features
  */
-define('DB_PORTABILITY_ALL', 63);
+define('DB_PORTABILITY_ALL', 127);
 /**#-*/
 
 // }}}
Index: DB/common.php
============================================================
=======
RCS file: /repository/pear/DB/DB/common.php,v
retrieving revision 1.138
diff -u -r1.138 common.php
--- DB/common.php	14 Jul 2005 02:42:18 -0000	1.138
+++ DB/common.php	25 Apr 2006 18:54:36 -0000
 -2150,6
+2150,77 
     }
 
     // }}}
+    // {{{ _fixBooleanStringsToRealBooleans()
+
+    /**
+     * Convert bool-types return values from the resultset
into real PHP booleans
+     *
+     * param array  $array  the array containing the
resultset
+     *
+     * param resource  $result  the resource where the above
$array came from
+     *
+     * param integer  $fetchmode  the fetchmode used to
retrieve the above $array
+     *
+     * return void
+     *
+     * access protected
+     */
+    function _fixBooleanStringsToRealBooleans(&$array,
$result, $fetchmode)
+    {
+        $tblInfo =& $this->tableInfo($result);
+
+        $booleanTypes = array(
+            "bool",
+            "boolean"
+        );
+
+        $fixedBooleans = array(
+            // RETURNED VALUE FROM DBMS => CORRESPONDING
PHP BOOLEAN
+            "t"     => true,
+            "true"  => true,
+            "1"     => true,
+            1       => true,
+            "f"     => false,
+            "false" => false,
+            "0"     => false,
+            0       => false,
+        );
+
+        $booleanFields = array();
+
+        $k = 0;
+        foreach ($tblInfo as $key => $fieldData)
+        {
+            if
(in_array(strtolower($fieldData["type"]),
$booleanTypes))
+            {
+                $booleanFields[$k]["index"] =
$key;
+                $booleanFields[$k]["name"] =
$fieldData["name"];
+                $k++;
+            }
+        }
+
+        if (count($booleanFields) > 0)
+        {
+            foreach ($array as $colKey => $colValue)
+            {
+                if ($fetchmode & DB_FETCHMODE_ASSOC)
+                    $keyName = "name";
+                else
+                    $keyName = "index";
+
+                foreach ($booleanFields as $key =>
$value)
+                {
+                    if ($value[$keyName] == $colKey)
+                    {
+                        if
(array_key_exists(strtolower($colValue), $fixedBooleans))
+                            $array[$colKey] =
$fixedBooleans[$colValue];
+                    }
+                }
+            }
+        }
+    }
+    
+    // }}}
 }
 
 /*
Index: DB/dbase.php
============================================================
=======
RCS file: /repository/pear/DB/DB/dbase.php,v
retrieving revision 1.39
diff -u -r1.39 dbase.php
--- DB/dbase.php	19 Feb 2005 23:25:25 -0000	1.39
+++ DB/dbase.php	25 Apr 2006 18:54:37 -0000
 -322,6
+322,9 
         if ($this->options['portability'] &
DB_PORTABILITY_NULL_TO_EMPTY) {
             $this->_convertNullArrayValuesToEmpty($arr);
         }
+        if ($this->options['portability'] &
DB_PORTABILITY_FIX_BOOLEANS) {
+           
$this->_fixBooleanStringsToRealBooleans($arr, $result,
$fetchmode);
+        }
         return DB_OK;
     }
 
Index: DB/fbsql.php
============================================================
=======
RCS file: /repository/pear/DB/DB/fbsql.php,v
retrieving revision 1.82
diff -u -r1.82 fbsql.php
--- DB/fbsql.php	4 Mar 2005 23:12:36 -0000	1.82
+++ DB/fbsql.php	25 Apr 2006 18:54:37 -0000
 -299,6
+299,9 
         if ($this->options['portability'] &
DB_PORTABILITY_NULL_TO_EMPTY) {
             $this->_convertNullArrayValuesToEmpty($arr);
         }
+        if ($this->options['portability'] &
DB_PORTABILITY_FIX_BOOLEANS) {
+           
$this->_fixBooleanStringsToRealBooleans($arr, $result,
$fetchmode);
+        }
         return DB_OK;
     }
 
Index: DB/ibase.php
============================================================
=======
RCS file: /repository/pear/DB/DB/ibase.php,v
retrieving revision 1.109
diff -u -r1.109 ibase.php
--- DB/ibase.php	4 Mar 2005 23:12:36 -0000	1.109
+++ DB/ibase.php	25 Apr 2006 18:54:37 -0000
 -391,6
+391,9 
         if ($this->options['portability'] &
DB_PORTABILITY_NULL_TO_EMPTY) {
             $this->_convertNullArrayValuesToEmpty($arr);
         }
+        if ($this->options['portability'] &
DB_PORTABILITY_FIX_BOOLEANS) {
+           
$this->_fixBooleanStringsToRealBooleans($arr, $result,
$fetchmode);
+        }
         return DB_OK;
     }
 
Index: DB/ifx.php
============================================================
=======
RCS file: /repository/pear/DB/DB/ifx.php,v
retrieving revision 1.70
diff -u -r1.70 ifx.php
--- DB/ifx.php	20 Feb 2005 00:44:48 -0000	1.70
+++ DB/ifx.php	25 Apr 2006 18:54:38 -0000
 -375,6
+375,9 
         if ($this->options['portability'] &
DB_PORTABILITY_NULL_TO_EMPTY) {
             $this->_convertNullArrayValuesToEmpty($arr);
         }
+        if ($this->options['portability'] &
DB_PORTABILITY_FIX_BOOLEANS) {
+           
$this->_fixBooleanStringsToRealBooleans($arr, $result,
$fetchmode);
+        }
         return DB_OK;
     }
 
Index: DB/msql.php
============================================================
=======
RCS file: /repository/pear/DB/DB/msql.php,v
retrieving revision 1.57
diff -u -r1.57 msql.php
--- DB/msql.php	22 Feb 2005 07:26:46 -0000	1.57
+++ DB/msql.php	25 Apr 2006 18:54:38 -0000
 -329,6
+329,9 
         if ($this->options['portability'] &
DB_PORTABILITY_NULL_TO_EMPTY) {
             $this->_convertNullArrayValuesToEmpty($arr);
         }
+        if ($this->options['portability'] &
DB_PORTABILITY_FIX_BOOLEANS) {
+           
$this->_fixBooleanStringsToRealBooleans($arr, $result,
$fetchmode);
+        }
         return DB_OK;
     }
 
Index: DB/mssql.php
============================================================
=======
RCS file: /repository/pear/DB/DB/mssql.php,v
retrieving revision 1.85
diff -u -r1.85 mssql.php
--- DB/mssql.php	10 Jun 2005 13:29:40 -0000	1.85
+++ DB/mssql.php	25 Apr 2006 18:54:38 -0000
 -340,6
+340,9 
         if ($this->options['portability'] &
DB_PORTABILITY_NULL_TO_EMPTY) {
             $this->_convertNullArrayValuesToEmpty($arr);
         }
+        if ($this->options['portability'] &
DB_PORTABILITY_FIX_BOOLEANS) {
+           
$this->_fixBooleanStringsToRealBooleans($arr, $result,
$fetchmode);
+        }
         return DB_OK;
     }
 
Index: DB/mysql.php
============================================================
=======
RCS file: /repository/pear/DB/DB/mysql.php,v
retrieving revision 1.119
diff -u -r1.119 mysql.php
--- DB/mysql.php	8 Apr 2006 12:51:00 -0000	1.119
+++ DB/mysql.php	25 Apr 2006 18:54:38 -0000
 -398,6
+398,9 
         if ($this->options['portability'] &
DB_PORTABILITY_NULL_TO_EMPTY) {
             $this->_convertNullArrayValuesToEmpty($arr);
         }
+        if ($this->options['portability'] &
DB_PORTABILITY_FIX_BOOLEANS) {
+           
$this->_fixBooleanStringsToRealBooleans($arr, $result,
$fetchmode);
+        }
         return DB_OK;
     }
 
Index: DB/mysqli.php
============================================================
=======
RCS file: /repository/pear/DB/DB/mysqli.php,v
retrieving revision 1.71
diff -u -r1.71 mysqli.php
--- DB/mysqli.php	2 Mar 2006 08:12:17 -0000	1.71
+++ DB/mysqli.php	25 Apr 2006 18:54:39 -0000
 -469,6
+469,9 
         if ($this->options['portability'] &
DB_PORTABILITY_NULL_TO_EMPTY) {
             $this->_convertNullArrayValuesToEmpty($arr);
         }
+        if ($this->options['portability'] &
DB_PORTABILITY_FIX_BOOLEANS) {
+           
$this->_fixBooleanStringsToRealBooleans($arr, $result,
$fetchmode);
+        }
         return DB_OK;
     }
 
Index: DB/oci8.php
============================================================
=======
RCS file: /repository/pear/DB/DB/oci8.php,v
retrieving revision 1.103
diff -u -r1.103 oci8.php
--- DB/oci8.php	11 Apr 2005 15:10:22 -0000	1.103
+++ DB/oci8.php	25 Apr 2006 18:54:39 -0000
 -394,6
+394,9 
         if ($this->options['portability'] &
DB_PORTABILITY_NULL_TO_EMPTY) {
             $this->_convertNullArrayValuesToEmpty($arr);
         }
+        if ($this->options['portability'] &
DB_PORTABILITY_FIX_BOOLEANS) {
+           
$this->_fixBooleanStringsToRealBooleans($arr, $result,
$fetchmode);
+        }
         return DB_OK;
     }
 
Index: DB/odbc.php
============================================================
=======
RCS file: /repository/pear/DB/DB/odbc.php,v
retrieving revision 1.78
diff -u -r1.78 odbc.php
--- DB/odbc.php	28 Feb 2005 01:42:17 -0000	1.78
+++ DB/odbc.php	25 Apr 2006 18:54:39 -0000
 -346,6
+346,9 
         if ($this->options['portability'] &
DB_PORTABILITY_NULL_TO_EMPTY) {
             $this->_convertNullArrayValuesToEmpty($arr);
         }
+        if ($this->options['portability'] &
DB_PORTABILITY_FIX_BOOLEANS) {
+           
$this->_fixBooleanStringsToRealBooleans($arr, $result,
$fetchmode);
+        }
         return DB_OK;
     }
 
Index: DB/pgsql.php
============================================================
=======
RCS file: /repository/pear/DB/DB/pgsql.php,v
retrieving revision 1.129
diff -u -r1.129 pgsql.php
--- DB/pgsql.php	10 Jun 2005 14:31:45 -0000	1.129
+++ DB/pgsql.php	25 Apr 2006 18:54:40 -0000
 -434,6
+434,9 
         if ($this->options['portability'] &
DB_PORTABILITY_NULL_TO_EMPTY) {
             $this->_convertNullArrayValuesToEmpty($arr);
         }
+        if ($this->options['portability'] &
DB_PORTABILITY_FIX_BOOLEANS) {
+           
$this->_fixBooleanStringsToRealBooleans($arr, $result,
$fetchmode);
+        }
         $this->row[$result_int] = ++$rownum;
         return DB_OK;
     }
Index: DB/sqlite.php
============================================================
=======
RCS file: /repository/pear/DB/DB/sqlite.php,v
retrieving revision 1.110
diff -u -r1.110 sqlite.php
--- DB/sqlite.php	17 Mar 2006 08:37:33 -0000	1.110
+++ DB/sqlite.php	25 Apr 2006 18:54:40 -0000
 -376,6
+376,9 
         if ($this->options['portability'] &
DB_PORTABILITY_NULL_TO_EMPTY) {
             $this->_convertNullArrayValuesToEmpty($arr);
         }
+        if ($this->options['portability'] &
DB_PORTABILITY_FIX_BOOLEANS) {
+           
$this->_fixBooleanStringsToRealBooleans($arr, $result,
$fetchmode);
+        }
         return DB_OK;
     }
 
Index: DB/sybase.php
============================================================
=======
RCS file: /repository/pear/DB/DB/sybase.php,v
retrieving revision 1.79
diff -u -r1.79 sybase.php
--- DB/sybase.php	14 Jun 2005 12:42:28 -0000	1.79
+++ DB/sybase.php	25 Apr 2006 18:54:40 -0000
 -349,6
+349,9 
         if ($this->options['portability'] &
DB_PORTABILITY_NULL_TO_EMPTY) {
             $this->_convertNullArrayValuesToEmpty($arr);
         }
+        if ($this->options['portability'] &
DB_PORTABILITY_FIX_BOOLEANS) {
+           
$this->_fixBooleanStringsToRealBooleans($arr, $result,
$fetchmode);
+        }
         return DB_OK;
     }
 

-- 
PEAR Development Mailing List (http://pear.php.net/)
To unsubscribe, visit: http://www.php.net/unsub
.php
DB_PORTABILITY_FIX_BOOLEANS
user name
2006-04-25 20:10:53
Anderson Miranda wrote:
> What have been done?
> - Implemented a new portability option called
"DB_PORTABILITY_FIX_BOOLEANS"

DB is in maintaince mode. New features go into MDB2. As a
matter of fact 
  this feature is already implemented in MDB2.

regards,
Lukas

-- 
PEAR Development Mailing List (http://pear.php.net/)
To unsubscribe, visit: http://www.php.net/unsub
.php

[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )