fix image text
[pear] / MDB2.php
index f14c21f..a6a5372 100644 (file)
--- a/MDB2.php
+++ b/MDB2.php
@@ -43,7 +43,7 @@
 // | Author: Lukas Smith <smith@pooteeweet.org>                           |
 // +----------------------------------------------------------------------+
 //
-// $Id: MDB2.php,v 1.292 2007/04/25 09:31:01 quipo Exp $
+// $Id$
 //
 
 /**
@@ -100,6 +100,9 @@ define('MDB2_ERROR_MANAGER',            -32);
 define('MDB2_ERROR_MANAGER_PARSE',      -33);
 define('MDB2_ERROR_LOADMODULE',         -34);
 define('MDB2_ERROR_INSUFFICIENT_DATA',  -35);
+define('MDB2_ERROR_NO_PERMISSION',      -36);
+define('MDB2_ERROR_DISCONNECT_FAILED',  -37);
+
 // }}}
 // {{{ Verbose constants
 /**
@@ -264,7 +267,7 @@ $GLOBALS['_MDB2_dsninfo_default'] = array(
  */
 class MDB2
 {
-    // {{{ function setOptions(&$db, $options)
+    // {{{ function setOptions($db, $options)
 
     /**
      * set option array   in an exiting database object
@@ -276,7 +279,7 @@ class MDB2
      *
      * @access  public
      */
-    function setOptions(&$db, $options)
+    static function setOptions($db, $options)
     {
         if (is_array($options)) {
             foreach ($options as $option => $value) {
@@ -301,12 +304,9 @@ class MDB2
      * @static
      * @access  public
      */
-    function classExists($classname)
+    static function classExists($classname)
     {
-        if (version_compare(phpversion(), "5.0", ">=")) {
-            return class_exists($classname, false);
-        }
-        return class_exists($classname);
+        return class_exists($classname, false);
     }
 
     // }}}
@@ -322,7 +322,7 @@ class MDB2
      *
      * @access  public
      */
-    function loadClass($class_name, $debug)
+    static function loadClass($class_name, $debug)
     {
         if (!MDB2::classExists($class_name)) {
             $file_name = str_replace('_', DIRECTORY_SEPARATOR, $class_name).'.php';
@@ -340,26 +340,21 @@ class MDB2
                 $err = MDB2::raiseError(MDB2_ERROR_NOT_FOUND, null, null, $msg);
                 return $err;
             }
+            if (!MDB2::classExists($class_name)) {
+                $msg = "unable to load class '$class_name' from file '$file_name'";
+                $err = MDB2::raiseError(MDB2_ERROR_NOT_FOUND, null, null, $msg);
+                return $err;
+            }
         }
         return MDB2_OK;
     }
 
     // }}}
-    // {{{ function &factory($dsn, $options = false)
+    // {{{ function factory($dsn, $options = false)
 
     /**
      * Create a new MDB2 object for the specified database type
      *
-     * IMPORTANT: In order for MDB2 to work properly it is necessary that
-     * you make sure that you work with a reference of the original
-     * object instead of a copy (this is a PHP4 quirk).
-     *
-     * For example:
-     *     $db = MDB2::factory($dsn);
-     *          ^^
-     * And not:
-     *     $db = MDB2::factory($dsn);
-     *
      * @param   mixed   'data source name', see the MDB2::parseDSN
      *                      method for a description of the dsn format.
      *                      Can also be specified as an array of the
@@ -371,7 +366,7 @@ class MDB2
      *
      * @access  public
      */
-    function &factory($dsn, $options = false)
+    static function factory($dsn, $options = false)
     {
         $dsninfo = MDB2::parseDSN($dsn);
         if (empty($dsninfo['phptype'])) {
@@ -387,7 +382,7 @@ class MDB2
             return $err;
         }
 
-        $db =  new $class_name();
+        $db = new $class_name();
         $db->setDSN($dsninfo);
         $err = MDB2::setOptions($db, $options);
         if (PEAR::isError($err)) {
@@ -398,37 +393,26 @@ class MDB2
     }
 
     // }}}
-    // {{{ function &connect($dsn, $options = false)
+    // {{{ function connect($dsn, $options = false)
 
     /**
-     * Create a new MDB2 connection object and connect to the specified
+     * Create a new MDB2_Driver_* connection object and connect to the specified
      * database
      *
-     * IMPORTANT: In order for MDB2 to work properly it is necessary that
-     * you make sure that you work with a reference of the original
-     * object instead of a copy (this is a PHP4 quirk).
-     *
-     * For example:
-     *     $db = MDB2::connect($dsn);
-     *          ^^
-     * And not:
-     *     $db = MDB2::connect($dsn);
-     *          ^^
-     *
-     * @param   mixed   'data source name', see the MDB2::parseDSN
-     *                            method for a description of the dsn format.
-     *                            Can also be specified as an array of the
-     *                            format returned by MDB2::parseDSN.
-     * @param   array   An associative array of option names and
-     *                            their values.
+     * @param mixed $dsn     'data source name', see the MDB2::parseDSN
+     *                       method for a description of the dsn format.
+     *                       Can also be specified as an array of the
+     *                       format returned by MDB2::parseDSN.
+     * @param array $options An associative array of option names and
+     *                       their values.
      *
-     * @return  mixed   a newly created MDB2 connection object, or a MDB2
-     *                  error object on error
+     * @return mixed a newly created MDB2 connection object, or a MDB2
+     *               error object on error
      *
      * @access  public
      * @see     MDB2::parseDSN
      */
-    function &connect($dsn, $options = false)
+    static function connect($dsn, $options = false)
     {
         $db = MDB2::factory($dsn, $options);
         if (PEAR::isError($db)) {
@@ -447,7 +431,7 @@ class MDB2
     }
 
     // }}}
-    // {{{ function &singleton($dsn = null, $options = false)
+    // {{{ function singleton($dsn = null, $options = false)
 
     /**
      * Returns a MDB2 connection with the requested DSN.
@@ -467,7 +451,7 @@ class MDB2
      * @access  public
      * @see     MDB2::parseDSN
      */
-    function &singleton($dsn = null, $options = false)
+    static function singleton($dsn = null, $options = false)
     {
         if ($dsn) {
             $dsninfo = MDB2::parseDSN($dsn);
@@ -483,26 +467,50 @@ class MDB2
                 }
             }
         } elseif (is_array($GLOBALS['_MDB2_databases']) && reset($GLOBALS['_MDB2_databases'])) {
-            $db = $GLOBALS['_MDB2_databases'][key($GLOBALS['_MDB2_databases'])];
-            return $db;
+            return $GLOBALS['_MDB2_databases'][key($GLOBALS['_MDB2_databases'])];
         }
         $db = MDB2::factory($dsn, $options);
         return $db;
     }
 
+    // }}}
+    // {{{ function areEquals()
+
+    /**
+     * It looks like there's a memory leak in array_diff() in PHP 5.1.x,
+     * so use this method instead.
+     * @see http://pear.php.net/bugs/bug.php?id=11790
+     *
+     * @param array $arr1
+     * @param array $arr2
+     * @return boolean
+     */
+    static function areEquals($arr1, $arr2)
+    {
+        if (count($arr1) != count($arr2)) {
+            return false;
+        }
+        foreach (array_keys($arr1) as $k) {
+            if (!array_key_exists($k, $arr2) || $arr1[$k] != $arr2[$k]) {
+                return false;
+            }
+        }
+        return true;
+    }
+
     // }}}
     // {{{ function loadFile($file)
 
     /**
      * load a file (like 'Date')
      *
-     * @param   string  name of the file in the MDB2 directory (without '.php')
+     * @param string $file name of the file in the MDB2 directory (without '.php')
      *
-     * @return  string  name of the file that was included
+     * @return string name of the file that was included
      *
      * @access  public
      */
-    function loadFile($file)
+    static function loadFile($file)
     {
         $file_name = 'MDB2'.DIRECTORY_SEPARATOR.$file.'.php';
         if (!MDB2::fileExists($file_name)) {
@@ -528,7 +536,7 @@ class MDB2
      */
     function apiVersion()
     {
-        return '2.4.1';
+        return '@package_version@';
     }
 
     // }}}
@@ -558,7 +566,13 @@ class MDB2
      * @access  private
      * @see     PEAR_Error
      */
-    function &raiseError($code = null, $mode = null, $options = null, $userinfo = null)
+    function &raiseError($code = null,
+                         $mode = null,
+                         $options = null,
+                         $userinfo = null,
+                         $dummy1 = null,
+                         $dummy2 = null,
+                         $dummy3 = false)
     {
         $err =& PEAR::raiseError(null, $code, $mode, $options, $userinfo, 'MDB2_Error', true);
         return $err;
@@ -580,17 +594,16 @@ class MDB2
      *
      * @access  public
      */
-    function isError($data, $code = null)
+    static function isError($data, $code = null)
     {
-        if (is_a($data, 'MDB2_Error')) {
-            if (is_null($code)) {
+        if ($data instanceof MDB2_Error) {
+            if (null === $code) {
                 return true;
-            } elseif (is_string($code)) {
+            }
+            if (is_string($code)) {
                 return $data->getMessage() === $code;
-            } else {
-                $code = (array)$code;
-                return in_array($data->getCode(), $code);
             }
+            return in_array($data->getCode(), (array)$code);
         }
         return false;
     }
@@ -604,12 +617,11 @@ class MDB2
      * @param   mixed   value to test
      *
      * @return  bool    whether $value is a MDB2 connection
-     *
      * @access  public
      */
     function isConnection($value)
     {
-        return is_a($value, 'MDB2_Driver_Common');
+        return ($value instanceof MDB2_Driver_Common);
     }
 
     // }}}
@@ -618,15 +630,15 @@ class MDB2
     /**
      * Tell whether a value is a MDB2 result
      *
-     * @param   mixed   value to test
+     * @param mixed $value value to test
      *
-     * @return  bool    whether $value is a MDB2 result
+     * @return bool whether $value is a MDB2 result
      *
-     * @access  public
+     * @access public
      */
     function isResult($value)
     {
-        return is_a($value, 'MDB2_Result');
+        return ($value instanceof MDB2_Result);
     }
 
     // }}}
@@ -635,15 +647,15 @@ class MDB2
     /**
      * Tell whether a value is a MDB2 result implementing the common interface
      *
-     * @param   mixed   value to test
+     * @param mixed $value value to test
      *
-     * @return  bool    whether $value is a MDB2 result implementing the common interface
+     * @return bool whether $value is a MDB2 result implementing the common interface
      *
      * @access  public
      */
-    function isResultCommon($value)
+    static function isResultCommon($value)
     {
-        return is_a($value, 'MDB2_Result_Common');
+        return ($value instanceof MDB2_Result_Common);
     }
 
     // }}}
@@ -660,7 +672,7 @@ class MDB2
      */
     function isStatement($value)
     {
-        return is_a($value, 'MDB2_Statement');
+        return ($value instanceof MDB2_Statement_Common);
     }
 
     // }}}
@@ -678,7 +690,7 @@ class MDB2
      *
      * @access  public
      */
-    function errorMessage($value = null)
+    static function errorMessage($value = null)
     {
         static $errorMessages;
 
@@ -722,10 +734,12 @@ class MDB2
                 MDB2_ERROR_LOADMODULE         => 'error while including on demand module',
                 MDB2_ERROR_TRUNCATED          => 'truncated',
                 MDB2_ERROR_DEADLOCK           => 'deadlock detected',
+                MDB2_ERROR_NO_PERMISSION      => 'no permission',
+                MDB2_ERROR_DISCONNECT_FAILED  => 'disconnect failed',
             );
         }
 
-        if (is_null($value)) {
+        if (null === $value) {
             return $errorMessages;
         }
 
@@ -777,7 +791,7 @@ class MDB2
      * @access  public
      * @author  Tomas V.V.Cox <cox@idecnet.com>
      */
-    function parseDSN($dsn)
+    static function parseDSN($dsn)
     {
         $parsed = $GLOBALS['_MDB2_dsninfo_default'];
 
@@ -846,7 +860,9 @@ class MDB2
                 //"username/password@[//]host[:port][/service_name]"
                 //e.g. "scott/tiger@//mymachine:1521/oracle"
                 $proto_opts = $dsn;
-                $dsn = null;
+                $pos = strrpos($proto_opts, '/');
+                $dsn = substr($proto_opts, $pos + 1);
+                $proto_opts = substr($proto_opts, 0, $pos);
             } elseif (strpos($dsn, '/') !== false) {
                 list($proto_opts, $dsn) = explode('/', $dsn, 2);
             } else {
@@ -884,7 +900,7 @@ class MDB2
                 }
                 foreach ($opts as $opt) {
                     list($key, $value) = explode('=', $opt);
-                    if (!isset($parsed[$key])) {
+                    if (!array_key_exists($key, $parsed) || false === $parsed[$key]) {
                         // don't allow params overwrite
                         $parsed[$key] = rawurldecode($value);
                     }
@@ -907,7 +923,7 @@ class MDB2
      *
      * @access  public
      */
-    function fileExists($file)
+    static function fileExists($file)
     {
         // safe_mode does notwork with is_readable()
         if (!@ini_get('safe_mode')) {
@@ -950,15 +966,15 @@ class MDB2_Error extends PEAR_Error
      * @param   mixed   MDB2 error code, or string with error message.
      * @param   int     what 'error mode' to operate in
      * @param   int     what error level to use for $mode & PEAR_ERROR_TRIGGER
-     * @param   smixed   additional debug info, such as the last query
+     * @param   mixed   additional debug info, such as the last query
      */
-    function MDB2_Error($code = MDB2_ERROR, $mode = PEAR_ERROR_RETURN,
-              $level = E_USER_NOTICE, $debuginfo = null)
+    function __construct($code = MDB2_ERROR, $mode = PEAR_ERROR_RETURN,
+              $level = E_USER_NOTICE, $debuginfo = null, $dummy = null)
     {
-        if (is_null($code)) {
+        if (null === $code) {
             $code = MDB2_ERROR;
         }
-        $this->PEAR_Error('MDB2 Error: '.MDB2::errorMessage($code), $code,
+        parent::__construct('MDB2 Error: '.MDB2::errorMessage($code), $code,
             $mode, $level, $debuginfo);
     }
 
@@ -984,63 +1000,63 @@ class MDB2_Driver_Common extends PEAR
      * @var     int
      * @access  public
      */
-    var $db_index = 0;
+    public $db_index = 0;
 
     /**
      * DSN used for the next query
      * @var     array
      * @access  protected
      */
-    var $dsn = array();
+    public $dsn = array();
 
     /**
      * DSN that was used to create the current connection
      * @var     array
      * @access  protected
      */
-    var $connected_dsn = array();
+    public $connected_dsn = array();
 
     /**
      * connection resource
      * @var     mixed
      * @access  protected
      */
-    var $connection = 0;
+    public $connection = 0;
 
     /**
      * if the current opened connection is a persistent connection
      * @var     bool
      * @access  protected
      */
-    var $opened_persistent;
+    public $opened_persistent;
 
     /**
      * the name of the database for the next query
      * @var     string
-     * @access  protected
+     * @access  public
      */
-    var $database_name = '';
+    public $database_name = '';
 
     /**
      * the name of the database currently selected
      * @var     string
      * @access  protected
      */
-    var $connected_database_name = '';
+    public $connected_database_name = '';
 
     /**
      * server version information
      * @var     string
      * @access  protected
      */
-    var $connected_server_info = '';
+    public $connected_server_info = '';
 
     /**
      * list of all supported features of the given driver
      * @var     array
      * @access  public
      */
-    var $supported = array(
+    public $supported = array(
         'sequences' => false,
         'indexes' => false,
         'affected_rows' => false,
@@ -1053,6 +1069,7 @@ class MDB2_Driver_Common extends PEAR
         'LOBs' => false,
         'replace' => false,
         'sub_selects' => false,
+        'triggers' => false,
         'auto_increment' => false,
         'primary_key' => false,
         'result_introspection' => false,
@@ -1099,6 +1116,11 @@ class MDB2_Driver_Common extends PEAR
      *  <li>$options['emulate_prepared'] -> boolean: force prepared statements to be emulated</li>
      *  <li>$options['datatype_map'] -> array: map user defined datatypes to other primitive datatypes</li>
      *  <li>$options['datatype_map_callback'] -> array: callback function/method that should be called</li>
+     *  <li>$options['bindname_format'] -> string: regular expression pattern for named parameters</li>
+     *  <li>$options['multi_query'] -> boolean: determines if queries returning multiple result sets should be executed</li>
+     *  <li>$options['max_identifiers_length'] -> integer: max identifier length</li>
+     *  <li>$options['default_fk_action_onupdate'] -> string: default FOREIGN KEY ON UPDATE action ['RESTRICT'|'NO ACTION'|'SET DEFAULT'|'SET NULL'|'CASCADE']</li>
+     *  <li>$options['default_fk_action_ondelete'] -> string: default FOREIGN KEY ON DELETE action ['RESTRICT'|'NO ACTION'|'SET DEFAULT'|'SET NULL'|'CASCADE']</li>
      * </ul>
      *
      * @var     array
@@ -1108,7 +1130,7 @@ class MDB2_Driver_Common extends PEAR
      * @see     MDB2::singleton()
      * @see     MDB2_Driver_Common::setOption()
      */
-    var $options = array(
+    public $options = array(
         'ssl' => false,
         'field_case' => CASE_LOWER,
         'disable_query' => false,
@@ -1145,28 +1167,42 @@ class MDB2_Driver_Common extends PEAR
         'datatype_map' => array(),
         'datatype_map_callback' => array(),
         'nativetype_map_callback' => array(),
+        'lob_allow_url_include' => false,
+        'bindname_format' => '(?:\d+)|(?:[a-zA-Z][a-zA-Z0-9_]*)',
+        'max_identifiers_length' => 30,
+        'default_fk_action_onupdate' => 'RESTRICT',
+        'default_fk_action_ondelete' => 'RESTRICT',
     );
 
     /**
      * string array
      * @var     string
-     * @access  protected
+     * @access  public
      */
-    var $string_quoting = array('start' => "'", 'end' => "'", 'escape' => false, 'escape_pattern' => false);
+    public $string_quoting = array(
+        'start'  => "'",
+        'end'    => "'",
+        'escape' => false,
+        'escape_pattern' => false,
+    );
 
     /**
      * identifier quoting
      * @var     array
-     * @access  protected
+     * @access  public
      */
-    var $identifier_quoting = array('start' => '"', 'end' => '"', 'escape' => '"');
+    public $identifier_quoting = array(
+        'start'  => '"',
+        'end'    => '"',
+        'escape' => '"',
+    );
 
     /**
      * sql comments
      * @var     array
      * @access  protected
      */
-    var $sql_comments = array(
+    protected $sql_comments = array(
         array('start' => '--', 'end' => "\n", 'escape' => false),
         array('start' => '/*', 'end' => '*/', 'escape' => false),
     );
@@ -1176,105 +1212,105 @@ class MDB2_Driver_Common extends PEAR
      * @var     array
      * @access  protected
      */
-    var $wildcards = array('%', '_');
+    protected $wildcards = array('%', '_');
 
     /**
      * column alias keyword
      * @var     string
      * @access  protected
      */
-    var $as_keyword = ' AS ';
+    public $as_keyword = ' AS ';
 
     /**
      * warnings
      * @var     array
      * @access  protected
      */
-    var $warnings = array();
+    public $warnings = array();
 
     /**
      * string with the debugging information
      * @var     string
      * @access  public
      */
-    var $debug_output = '';
+    public $debug_output = '';
 
     /**
      * determine if there is an open transaction
      * @var     bool
      * @access  protected
      */
-    var $in_transaction = false;
+    public $in_transaction = false;
 
     /**
      * the smart transaction nesting depth
      * @var     int
      * @access  protected
      */
-    var $nested_transaction_counter = null;
+    public $nested_transaction_counter = null;
 
     /**
      * the first error that occured inside a nested transaction
      * @var     MDB2_Error|bool
      * @access  protected
      */
-    var $has_transaction_error = false;
+    protected $has_transaction_error = false;
 
     /**
      * result offset used in the next query
      * @var     int
-     * @access  protected
+     * @access  public
      */
-    var $offset = 0;
+    public $offset = 0;
 
     /**
      * result limit used in the next query
      * @var     int
-     * @access  protected
+     * @access  public
      */
-    var $limit = 0;
+    public $limit = 0;
 
     /**
      * Database backend used in PHP (mysql, odbc etc.)
      * @var     string
      * @access  public
      */
-    var $phptype;
+    public $phptype;
 
     /**
      * Database used with regards to SQL syntax etc.
      * @var     string
      * @access  public
      */
-    var $dbsyntax;
+    public $dbsyntax;
 
     /**
      * the last query sent to the driver
      * @var     string
      * @access  public
      */
-    var $last_query;
+    public $last_query;
 
     /**
      * the default fetchmode used
      * @var     int
-     * @access  protected
+     * @access  public
      */
-    var $fetchmode = MDB2_FETCHMODE_ORDERED;
+    public $fetchmode = MDB2_FETCHMODE_ORDERED;
 
     /**
      * array of module instances
      * @var     array
      * @access  protected
      */
-    var $modules = array();
+    protected $modules = array();
 
     /**
      * determines of the PHP4 destructor emulation has been enabled yet
      * @var     array
      * @access  protected
      */
-    var $destructor_registered = true;
+    protected $destructor_registered = true;
 
     // }}}
     // {{{ constructor: function __construct()
@@ -1290,18 +1326,6 @@ class MDB2_Driver_Common extends PEAR
         $this->db_index = $db_index;
     }
 
-    // }}}
-    // {{{ function MDB2_Driver_Common()
-
-    /**
-     * PHP 4 Constructor
-     */
-    function MDB2_Driver_Common()
-    {
-        $this->destructor_registered = false;
-        $this->__construct();
-    }
-
     // }}}
     // {{{ destructor: function __destruct()
 
@@ -1375,36 +1399,44 @@ class MDB2_Driver_Common extends PEAR
      * callbacks etc.  Basically a wrapper for PEAR::raiseError
      * without the message string.
      *
-     * @param   mixed   integer error code, or a PEAR error object (all other
-     *                  parameters are ignored if this parameter is an object
-     * @param   int     error mode, see PEAR_Error docs
-     * @param   mixed   If error mode is PEAR_ERROR_TRIGGER, this is the
-         *              error level (E_USER_NOTICE etc).  If error mode is
-     *                  PEAR_ERROR_CALLBACK, this is the callback function,
-     *                  either as a function name, or as an array of an
-     *                  object and method name.  For other error modes this
-     *                  parameter is ignored.
-     * @param   string  Extra debug information.  Defaults to the last
-     *                  query and native error code.
-     * @param   string  name of the method that triggered the error
-     *
-     * @return PEAR_Error   instance of a PEAR Error object
+     * @param mixed  $code     integer error code, or a PEAR error object (all 
+     *                         other parameters are ignored if this parameter is
+     *                         an object
+     * @param int    $mode     error mode, see PEAR_Error docs
+     * @param mixed  $options  If error mode is PEAR_ERROR_TRIGGER, this is the
+     *                         error level (E_USER_NOTICE etc). If error mode is
+     *                         PEAR_ERROR_CALLBACK, this is the callback function,
+     *                         either as a function name, or as an array of an
+     *                         object and method name. For other error modes this
+     *                         parameter is ignored.
+     * @param string $userinfo Extra debug information. Defaults to the last
+     *                         query and native error code.
+     * @param string $method   name of the method that triggered the error
+     * @param string $dummy1   not used
+     * @param bool   $dummy2   not used
      *
-     * @access  public
-     * @see     PEAR_Error
-     */
-    function &raiseError($code = null, $mode = null, $options = null, $userinfo = null, $method = null)
-    {
+     * @return PEAR_Error instance of a PEAR Error object
+     * @access public
+     * @see    PEAR_Error
+     */
+    function &raiseError($code = null,
+                         $mode = null,
+                         $options = null,
+                         $userinfo = null,
+                         $method = null,
+                         $dummy1 = null,
+                         $dummy2 = false
+    ) {
         $userinfo = "[Error message: $userinfo]\n";
         // The error is yet a MDB2 error object
         if (PEAR::isError($code)) {
             // because we use the static PEAR::raiseError, our global
             // handler should be used if it is set
-            if (is_null($mode) && !empty($this->_default_error_mode)) {
+            if ((null === $mode) && !empty($this->_default_error_mode)) {
                 $mode    = $this->_default_error_mode;
                 $options = $this->_default_error_options;
             }
-            if (is_null($userinfo)) {
+            if (null === $userinfo) {
                 $userinfo = $code->getUserinfo();
             }
             $code = $code->getCode();
@@ -1417,21 +1449,21 @@ class MDB2_Driver_Common extends PEAR
             }
             $native_errno = $native_msg = null;
             list($code, $native_errno, $native_msg) = $this->errorInfo($code);
-            if (!is_null($native_errno) && $native_errno !== '') {
+            if ((null !== $native_errno) && $native_errno !== '') {
                 $userinfo.= "[Native code: $native_errno]\n";
             }
-            if (!is_null($native_msg) && $native_msg !== '') {
+            if ((null !== $native_msg) && $native_msg !== '') {
                 $userinfo.= "[Native message: ". strip_tags($native_msg) ."]\n";
             }
-            if (!is_null($method)) {
+            if (null !== $method) {
                 $userinfo = $method.': '.$userinfo;
             }
         }
 
-        $err =& PEAR::raiseError(null, $code, $mode, $options, $userinfo, 'MDB2_Error', true);
+        $err = PEAR::raiseError(null, $code, $mode, $options, $userinfo, 'MDB2_Error', true);
         if ($err->getMode() !== PEAR_ERROR_RETURN
             && isset($this->nested_transaction_counter) && !$this->has_transaction_error) {
-            $this->has_transaction_error =& $err;
+            $this->has_transaction_error = $err;
         }
         return $err;
     }
@@ -1627,11 +1659,6 @@ class MDB2_Driver_Common extends PEAR
     /**
      * Quotes pattern (% and _) characters in a string)
      *
-     * EXPERIMENTAL
-     *
-     * WARNING: this function is experimental and may change signature at
-     * any time until labelled as non-experimental
-     *
      * @param   string  the input string to quote
      *
      * @return  string  quoted string
@@ -1661,6 +1688,9 @@ class MDB2_Driver_Common extends PEAR
      * you SHOULD use them.  In general, they end up causing way more
      * problems than they solve.
      *
+     * NOTE: if you have table names containing periods, don't use this method
+     * (@see bug #11906)
+     *
      * Portability is broken by using the following characters inside
      * delimited identifiers:
      *   + backtick (<kbd>`</kbd>) -- due to MySQL
@@ -1692,7 +1722,11 @@ class MDB2_Driver_Common extends PEAR
             return $str;
         }
         $str = str_replace($this->identifier_quoting['end'], $this->identifier_quoting['escape'] . $this->identifier_quoting['end'], $str);
-        return $this->identifier_quoting['start'] . $str . $this->identifier_quoting['end'];
+        $parts = explode('.', $str);
+        foreach (array_keys($parts) as $k) {
+            $parts[$k] = $this->identifier_quoting['start'] . $parts[$k] . $this->identifier_quoting['end'];
+        }
+        return implode('.', $parts);
     }
 
     // }}}
@@ -1810,7 +1844,7 @@ class MDB2_Driver_Common extends PEAR
     }
 
     // }}}
-    // {{{ function &loadModule($module, $property = null, $phptype_specific = null)
+    // {{{ function loadModule($module, $property = null, $phptype_specific = null)
 
     /**
      * loads a module
@@ -1826,7 +1860,7 @@ class MDB2_Driver_Common extends PEAR
      *
      * @access  public
      */
-    function &loadModule($module, $property = null, $phptype_specific = null)
+    function loadModule($module, $property = null, $phptype_specific = null)
     {
         if (!$property) {
             $property = strtolower($module);
@@ -1852,7 +1886,7 @@ class MDB2_Driver_Common extends PEAR
                 return $err;
             }
 
-            // load modul in a specific version
+            // load module in a specific version
             if ($version) {
                 if (method_exists($class_name, 'getClassName')) {
                     $class_name_new = call_user_func(array($class_name, 'getClassName'), $this->db_index);
@@ -1867,12 +1901,12 @@ class MDB2_Driver_Common extends PEAR
             }
 
             if (!MDB2::classExists($class_name)) {
-                $err =& $this->raiseError(MDB2_ERROR_LOADMODULE, null, null,
+                $err = $this->raiseError(MDB2_ERROR_LOADMODULE, null, null,
                     "unable to load module '$module' into property '$property'", __FUNCTION__);
                 return $err;
             }
             $this->{$property} = new $class_name($this->db_index);
-            $this->modules[$module] =& $this->{$property};
+            $this->modules[$module] = $this->{$property};
             if ($version) {
                 // this will be used in the connect method to determine if the module
                 // needs to be loaded with a different version if the server
@@ -1904,7 +1938,7 @@ class MDB2_Driver_Common extends PEAR
             $module = $this->options['modules'][$match[1]];
             $method = strtolower($match[2]).$match[3];
             if (!isset($this->modules[$module]) || !is_object($this->modules[$module])) {
-                $result =& $this->loadModule($module);
+                $result = $this->loadModule($module);
                 if (PEAR::isError($result)) {
                     return $result;
                 }
@@ -1919,7 +1953,7 @@ class MDB2_Driver_Common extends PEAR
                 }
             }
         }
-        if (!is_null($module)) {
+        if (null !== $module) {
             return call_user_func_array(array(&$this->modules[$module], $method), $params);
         }
         trigger_error(sprintf('Call to undefined function: %s::%s().', get_class($this), $method), E_USER_ERROR);
@@ -2039,11 +2073,6 @@ class MDB2_Driver_Common extends PEAR
     /**
      * Start a nested transaction.
      *
-     * EXPERIMENTAL
-     *
-     * WARNING: this function is experimental and may change signature at
-     * any time until labelled as non-experimental
-     *
      * @return  mixed   MDB2_OK on success/savepoint name, a MDB2 error on failure
      *
      * @access  public
@@ -2072,11 +2101,6 @@ class MDB2_Driver_Common extends PEAR
      * Finish a nested transaction by rolling back if an error occured or
      * committing otherwise.
      *
-     * EXPERIMENTAL
-     *
-     * WARNING: this function is experimental and may change signature at
-     * any time until labelled as non-experimental
-     *
      * @param   bool    if the transaction should be rolled back regardless
      *                  even if no error was set within the nested transaction
      * @return  mixed   MDB_OK on commit/counter decrementing, false on rollback
@@ -2130,11 +2154,6 @@ class MDB2_Driver_Common extends PEAR
     /**
      * Force setting nested transaction to failed.
      *
-     * EXPERIMENTAL
-     *
-     * WARNING: this function is experimental and may change signature at
-     * any time until labelled as non-experimental
-     *
      * @param   mixed   value to return in getNestededTransactionError()
      * @param   bool    if the transaction should be rolled back immediately
      * @return  bool    MDB2_OK
@@ -2144,7 +2163,7 @@ class MDB2_Driver_Common extends PEAR
      */
     function failNestedTransaction($error = null, $immediately = false)
     {
-        if (is_null($error)) {
+        if (null !== $error) {
             $error = $this->has_transaction_error ? $this->has_transaction_error : true;
         } elseif (!$error) {
             $error = true;
@@ -2162,11 +2181,6 @@ class MDB2_Driver_Common extends PEAR
     /**
      * The first error that occured since the transaction start.
      *
-     * EXPERIMENTAL
-     *
-     * WARNING: this function is experimental and may change signature at
-     * any time until labelled as non-experimental
-     *
      * @return  MDB2_Error|bool     MDB2 error object if an error occured or false.
      *
      * @access  public
@@ -2191,6 +2205,23 @@ class MDB2_Driver_Common extends PEAR
             'method not implemented', __FUNCTION__);
     }
 
+    // }}}
+    // {{{ databaseExists()
+
+    /**
+     * check if given database name is exists?
+     *
+     * @param string $name    name of the database that should be checked
+     *
+     * @return mixed true/false on success, a MDB2 error on failure
+     * @access public
+     */
+    function databaseExists($name)
+    {
+        return $this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
+            'method not implemented', __FUNCTION__);
+    }
+
     // }}}
     // {{{ setCharset($charset, $connection = null)
 
@@ -2214,11 +2245,10 @@ class MDB2_Driver_Common extends PEAR
     /**
      * Log out and disconnect from the database.
      *
-     * @param   bool    if the disconnect should be forced even if the
-     *                  connection is opened persistently
+     * @param boolean $force whether the disconnect should be forced even if the
+     *                       connection is opened persistently
      *
-     * @return  mixed   true on success, false if not connected and error
-     *                  object on error
+     * @return mixed true on success, false if not connected and error object on error
      *
      * @access  public
      */
@@ -2250,7 +2280,9 @@ class MDB2_Driver_Common extends PEAR
     {
         $previous_database_name = (isset($this->database_name)) ? $this->database_name : '';
         $this->database_name = $name;
-        $this->disconnect(false);
+        if (!empty($this->connected_database_name) && ($this->connected_database_name != $this->database_name)) {
+            $this->disconnect(false);
+        }
         return $previous_database_name;
     }
 
@@ -2331,6 +2363,26 @@ class MDB2_Driver_Common extends PEAR
         return $dsn;
     }
 
+    // }}}
+    // {{{ _isNewLinkSet()
+
+    /**
+     * Check if the 'new_link' option is set
+     *
+     * @return boolean
+     *
+     * @access protected
+     */
+    function _isNewLinkSet()
+    {
+        return (isset($this->dsn['new_link'])
+            && ($this->dsn['new_link'] === true
+             || (is_string($this->dsn['new_link']) && preg_match('/^true$/i', $this->dsn['new_link']))
+             || (is_numeric($this->dsn['new_link']) && 0 != (int)$this->dsn['new_link'])
+            )
+        );
+    }
+
     // }}}
     // {{{ function &standaloneQuery($query, $types = null, $is_manip = false)
 
@@ -2346,7 +2398,7 @@ class MDB2_Driver_Common extends PEAR
      *
      * @access  public
      */
-    function &standaloneQuery($query, $types = null, $is_manip = false)
+    function standaloneQuery($query, $types = null, $is_manip = false)
     {
         $offset = $this->offset;
         $limit = $this->limit;
@@ -2358,7 +2410,7 @@ class MDB2_Driver_Common extends PEAR
             return $connection;
         }
 
-        $result =& $this->_doQuery($query, $is_manip, $connection, false);
+        $result = $this->_doQuery($query, $is_manip, $connection, false);
         if (PEAR::isError($result)) {
             return $result;
         }
@@ -2367,7 +2419,7 @@ class MDB2_Driver_Common extends PEAR
             $affected_rows =  $this->_affectedRows($connection, $result);
             return $affected_rows;
         }
-        $result =& $this->_wrapResult($result, $types, true, false, $limit, $offset);
+        $result = $this->_wrapResult($result, $types, true, false, $limit, $offset);
         return $result;
     }
 
@@ -2405,7 +2457,7 @@ class MDB2_Driver_Common extends PEAR
      *
      * @access  protected
      */
-    function &_doQuery($query, $is_manip = false, $connection = null, $database_name = null)
+    function _doQuery($query, $is_manip = false, $connection = null, $database_name = null)
     {
         $this->last_query = $query;
         $result = $this->debug($query, 'query', array('is_manip' => $is_manip, 'when' => 'pre'));
@@ -2415,7 +2467,7 @@ class MDB2_Driver_Common extends PEAR
             }
             $query = $result;
         }
-        $err =& $this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
+        $err = $this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
             'method not implemented', __FUNCTION__);
         return $err;
     }
@@ -2451,7 +2503,7 @@ class MDB2_Driver_Common extends PEAR
      *
      * @access  public
      */
-    function &exec($query)
+    function exec($query)
     {
         $offset = $this->offset;
         $limit = $this->limit;
@@ -2463,7 +2515,7 @@ class MDB2_Driver_Common extends PEAR
             return $connection;
         }
 
-        $result =& $this->_doQuery($query, true, $connection, $this->database_name);
+        $result = $this->_doQuery($query, true, $connection, $this->database_name);
         if (PEAR::isError($result)) {
             return $result;
         }
@@ -2488,7 +2540,7 @@ class MDB2_Driver_Common extends PEAR
      *
      * @access  public
      */
-    function &query($query, $types = null, $result_class = true, $result_wrap_class = false)
+    function query($query, $types = null, $result_class = true, $result_wrap_class = false)
     {
         $offset = $this->offset;
         $limit = $this->limit;
@@ -2500,17 +2552,17 @@ class MDB2_Driver_Common extends PEAR
             return $connection;
         }
 
-        $result =& $this->_doQuery($query, false, $connection, $this->database_name);
+        $result = $this->_doQuery($query, false, $connection, $this->database_name);
         if (PEAR::isError($result)) {
             return $result;
         }
 
-        $result =& $this->_wrapResult($result, $types, $result_class, $result_wrap_class, $limit, $offset);
+        $result = $this->_wrapResult($result, $types, $result_class, $result_wrap_class, $limit, $offset);
         return $result;
     }
 
     // }}}
-    // {{{ function &_wrapResult($result, $types = array(), $result_class = true, $result_wrap_class = false, $limit = null, $offset = null)
+    // {{{ function _wrapResult($result_resource, $types = array(), $result_class = true, $result_wrap_class = false, $limit = null, $offset = null)
 
     /**
      * wrap a result set into the correct class
@@ -2527,19 +2579,21 @@ class MDB2_Driver_Common extends PEAR
      *
      * @access  protected
      */
-    function &_wrapResult($result, $types = array(), $result_class = true,
+    function _wrapResult($result_resource, $types = array(), $result_class = true,
         $result_wrap_class = false, $limit = null, $offset = null)
     {
         if ($types === true) {
             if ($this->supports('result_introspection')) {
                 $this->loadModule('Reverse', null, true);
-                $tableInfo = $this->reverse->tableInfo($result);
+                $tableInfo = $this->reverse->tableInfo($result_resource);
                 if (PEAR::isError($tableInfo)) {
                     return $tableInfo;
                 }
                 $types = array();
+                $types_assoc = array();
                 foreach ($tableInfo as $field) {
                     $types[] = $field['mdb2type'];
+                    $types_assoc[$field['name']] = $field['mdb2type'];
                 }
             } else {
                 $types = null;
@@ -2554,13 +2608,13 @@ class MDB2_Driver_Common extends PEAR
         if ($result_class) {
             $class_name = sprintf($result_class, $this->phptype);
             if (!MDB2::classExists($class_name)) {
-                $err =& $this->raiseError(MDB2_ERROR_NOT_FOUND, null, null,
+                $err = $this->raiseError(MDB2_ERROR_NOT_FOUND, null, null,
                     'result class does not exist '.$class_name, __FUNCTION__);
                 return $err;
             }
-            $result = new $class_name($this, $result, $limit, $offset);
+            $result = new $class_name($this, $result_resource, $limit, $offset);
             if (!MDB2::isResultCommon($result)) {
-                $err =& $this->raiseError(MDB2_ERROR_NOT_FOUND, null, null,
+                $err = $this->raiseError(MDB2_ERROR_NOT_FOUND, null, null,
                     'result class is not extended from MDB2_Result_Common', __FUNCTION__);
                 return $err;
             }
@@ -2571,17 +2625,24 @@ class MDB2_Driver_Common extends PEAR
                     return $err;
                 }
             }
+            if (!empty($types_assoc)) {
+                $err = $result->setResultTypesAssoc($types_assoc);
+                if (PEAR::isError($err)) {
+                    $result->free();
+                    return $err;
+                }
+            }
         }
         if ($result_wrap_class === true) {
             $result_wrap_class = $this->options['result_wrap_class'];
         }
         if ($result_wrap_class) {
             if (!MDB2::classExists($result_wrap_class)) {
-                $err =& $this->raiseError(MDB2_ERROR_NOT_FOUND, null, null,
+                $err = $this->raiseError(MDB2_ERROR_NOT_FOUND, null, null,
                     'result wrap class does not exist '.$result_wrap_class, __FUNCTION__);
                 return $err;
             }
-            $result = new $result_wrap_class($result, $this->fetchmode);
+            $result = new $result_wrap_class($result_resource, $this->fetchmode);
         }
         return $result;
     }
@@ -2629,7 +2690,7 @@ class MDB2_Driver_Common extends PEAR
                 'it was not specified a valid selected range row limit', __FUNCTION__);
         }
         $this->limit = $limit;
-        if (!is_null($offset)) {
+        if (null !== $offset) {
             $offset = (int)$offset;
             if ($offset < 0) {
                 return $this->raiseError(MDB2_ERROR_SYNTAX, null, null,
@@ -2686,8 +2747,7 @@ class MDB2_Driver_Common extends PEAR
     /**
      * Execute a SQL REPLACE query. A REPLACE query is identical to a INSERT
      * query, except that if there is already a row in the table with the same
-     * key field values, the REPLACE query just updates its values instead of
-     * inserting a new row.
+     * key field values, the old row is deleted before the new row is inserted.
      *
      * The REPLACE type of query does not make part of the SQL standards. Since
      * practically only MySQL and SQLite implement it natively, this type of
@@ -2768,7 +2828,7 @@ class MDB2_Driver_Common extends PEAR
                     return $this->raiseError(MDB2_ERROR_CANNOT_REPLACE, null, null,
                         'key value '.$name.' may not be NULL', __FUNCTION__);
                 }
-                $condition[] = $name . '=' . $value;
+                $condition[] = $this->quoteIdentifier($name, true) . '=' . $value;
             }
         }
         if (empty($condition)) {
@@ -2788,14 +2848,17 @@ class MDB2_Driver_Common extends PEAR
         }
 
         $condition = ' WHERE '.implode(' AND ', $condition);
-        $query = "DELETE FROM $table$condition";
-        $result =& $this->_doQuery($query, true, $connection);
+        $query = 'DELETE FROM ' . $this->quoteIdentifier($table, true) . $condition;
+        $result = $this->_doQuery($query, true, $connection);
         if (!PEAR::isError($result)) {
             $affected_rows = $this->_affectedRows($connection, $result);
-            $insert = implode(', ', array_keys($values));
+            $insert = '';
+            foreach ($values as $key => $value) {
+                $insert .= ($insert?', ':'') . $this->quoteIdentifier($key, true);
+            }
             $values = implode(', ', $values);
-            $query = "INSERT INTO $table ($insert) VALUES ($values)";
-            $result =& $this->_doQuery($query, true, $connection);
+            $query = 'INSERT INTO '. $this->quoteIdentifier($table, true) . "($insert) VALUES ($values)";
+            $result = $this->_doQuery($query, true, $connection);
             if (!PEAR::isError($result)) {
                 $affected_rows += $this->_affectedRows($connection, $result);;
             }
@@ -2825,8 +2888,9 @@ class MDB2_Driver_Common extends PEAR
      * prepare() requires a generic query as string like
      * 'INSERT INTO numbers VALUES(?,?)' or
      * 'INSERT INTO numbers VALUES(:foo,:bar)'.
-     * The ? and :[a-zA-Z] and  are placeholders which can be set using
-     * bindParam() and the query can be send off using the execute() method.
+     * The ? and :name and are placeholders which can be set using
+     * bindParam() and the query can be sent off using the execute() method.
+     * The allowed format for :name can be set with the 'bindname_format' option.
      *
      * @param   string  the query to prepare
      * @param   mixed   array that contains the types of the placeholders
@@ -2841,7 +2905,7 @@ class MDB2_Driver_Common extends PEAR
      * @access  public
      * @see     bindParam, execute
      */
-    function &prepare($query, $types = null, $result_types = null, $lobs = array())
+    function prepare($query, $types = null, $result_types = null, $lobs = array())
     {
         $is_manip = ($result_types === MDB2_PREPARE_MANIP);
         $offset = $this->offset;
@@ -2855,13 +2919,10 @@ class MDB2_Driver_Common extends PEAR
             $query = $result;
         }
         $placeholder_type_guess = $placeholder_type = null;
-        $question = '?';
-        $colon = ':';
+        $question  = '?';
+        $colon     = ':';
         $positions = array();
-        $position = 0;
-        $ignores = $this->sql_comments;
-        $ignores[] = $this->string_quoting;
-        $ignores[] = $this->identifier_quoting;
+        $position  = 0;
         while ($position < strlen($query)) {
             $q_position = strpos($query, $question, $position);
             $c_position = strpos($query, $colon, $position);
@@ -2874,7 +2935,7 @@ class MDB2_Driver_Common extends PEAR
             } else {
                 break;
             }
-            if (is_null($placeholder_type)) {
+            if (null === $placeholder_type) {
                 $placeholder_type_guess = $query[$p_position];
             }
 
@@ -2888,7 +2949,7 @@ class MDB2_Driver_Common extends PEAR
             }
 
             if ($query[$position] == $placeholder_type_guess) {
-                if (is_null($placeholder_type)) {
+                if (null === $placeholder_type) {
                     $placeholder_type = $query[$p_position];
                     $question = $colon = $placeholder_type;
                     if (!empty($types) && is_array($types)) {
@@ -2904,10 +2965,11 @@ class MDB2_Driver_Common extends PEAR
                     }
                 }
                 if ($placeholder_type == ':') {
-                    $parameter = preg_replace('/^.{'.($position+1).'}([a-z0-9_]+).*$/si', '\\1', $query);
+                    $regexp = '/^.{'.($position+1).'}('.$this->options['bindname_format'].').*$/s';
+                    $parameter = preg_replace($regexp, '\\1', $query);
                     if ($parameter === '') {
-                        $err =& $this->raiseError(MDB2_ERROR_SYNTAX, null, null,
-                            'named parameter with an empty name', __FUNCTION__);
+                        $err = $this->raiseError(MDB2_ERROR_SYNTAX, null, null,
+                            'named parameter name must match "bindname_format" option', __FUNCTION__);
                         return $err;
                     }
                     $positions[$p_position] = $parameter;
@@ -2951,9 +3013,10 @@ class MDB2_Driver_Common extends PEAR
      */
     function _skipDelimitedStrings($query, $position, $p_position)
     {
-        $ignores = $this->sql_comments;
+        $ignores = array();
         $ignores[] = $this->string_quoting;
         $ignores[] = $this->identifier_quoting;
+        $ignores = array_merge($ignores, $this->sql_comments);
         
         foreach ($ignores as $ignore) {
             if (!empty($ignore['start'])) {
@@ -2964,12 +3027,18 @@ class MDB2_Driver_Common extends PEAR
                             if ($ignore['end'] === "\n") {
                                 $end_quote = strlen($query) - 1;
                             } else {
-                                $err =& $this->raiseError(MDB2_ERROR_SYNTAX, null, null,
+                                $err = $this->raiseError(MDB2_ERROR_SYNTAX, null, null,
                                     'query with an unterminated text string specified', __FUNCTION__);
                                 return $err;
                             }
                         }
-                    } while ($ignore['escape'] && $query[($end_quote - 1)] == $ignore['escape']);
+                    } while ($ignore['escape']
+                        && $end_quote-1 != $start_quote
+                        && $query[($end_quote - 1)] == $ignore['escape']
+                        && (   $ignore['escape_pattern'] !== $ignore['escape']
+                            || $query[($end_quote - 2)] != $ignore['escape'])
+                    );
+
                     $position = $end_quote + 1;
                     return $position;
                 }
@@ -3091,7 +3160,7 @@ class MDB2_Driver_Common extends PEAR
     function getSequenceName($sqn)
     {
         return sprintf($this->options['seqname_format'],
-            preg_replace('/[^a-z0-9_\$.]/i', '_', $sqn));
+            preg_replace('/[^a-z0-9_\-\$.]/i', '_', $sqn));
     }
 
     // }}}
@@ -3109,7 +3178,7 @@ class MDB2_Driver_Common extends PEAR
     function getIndexName($idx)
     {
         return sprintf($this->options['idxname_format'],
-            preg_replace('/[^a-z0-9_\$]/i', '_', $idx));
+            preg_replace('/[^a-z0-9_\-\$.]/i', '_', $idx));
     }
 
     // }}}
@@ -3178,12 +3247,12 @@ class MDB2_Driver_Common extends PEAR
      * the first row of the result set and then frees
      * the result set.
      *
-     * @param   string  the SELECT query statement to be executed.
-     * @param   string  optional argument that specifies the expected
-     *       datatype of the result set field, so that an eventual conversion
-     *       may be performed. The default datatype is text, meaning that no
-     *       conversion is performed
-     * @param   int     the column number to fetch
+     * @param string $query  the SELECT query statement to be executed.
+     * @param string $type   optional argument that specifies the expected
+     *                       datatype of the result set field, so that an eventual
+     *                       conversion may be performed. The default datatype is
+     *                       text, meaning that no conversion is performed
+     * @param mixed  $colnum the column number (or name) to fetch
      *
      * @return  mixed   MDB2_OK or field value on success, a MDB2 error on failure
      *
@@ -3239,15 +3308,14 @@ class MDB2_Driver_Common extends PEAR
      * Execute the specified query, fetch the value from the first column of
      * each row of the result set into an array and then frees the result set.
      *
-     * @param   string  the SELECT query statement to be executed.
-     * @param   string  optional argument that specifies the expected
-     *       datatype of the result set field, so that an eventual conversion
-     *       may be performed. The default datatype is text, meaning that no
-     *       conversion is performed
-     * @param   int     the row number to fetch
+     * @param string $query  the SELECT query statement to be executed.
+     * @param string $type   optional argument that specifies the expected
+     *                       datatype of the result set field, so that an eventual
+     *                       conversion may be performed. The default datatype is text,
+     *                       meaning that no conversion is performed
+     * @param mixed  $colnum the column number (or name) to fetch
      *
      * @return  mixed   MDB2_OK or data array on success, a MDB2 error on failure
-     *
      * @access  public
      */
     function queryCol($query, $type = null, $colnum = 0)
@@ -3333,41 +3401,31 @@ class MDB2_Result_Common extends MDB2_Result
 {
     // {{{ Variables (Properties)
 
-    var $db;
-    var $result;
-    var $rownum = -1;
-    var $types = array();
-    var $values = array();
-    var $offset;
-    var $offset_count = 0;
-    var $limit;
-    var $column_names;
+    protected $db;
+    protected $result;
+    protected $rownum = -1;
+    protected $types = array();
+    protected $types_assoc = array();
+    protected $values = array();
+    protected $offset;
+    protected $offset_count = 0;
+    protected $limit;
+    protected $column_names;
 
     // }}}
-    // {{{ constructor: function __construct(&$db, &$result, $limit = 0, $offset = 0)
+    // {{{ constructor: function __construct($db, &$result, $limit = 0, $offset = 0)
 
     /**
      * Constructor
      */
-    function __construct(&$db, &$result, $limit = 0, $offset = 0)
+    function __construct($db, &$result, $limit = 0, $offset = 0)
     {
-        $this->db =& $db;
-        $this->result =& $result;
+        $this->db = $db;
+        $this->result = $result;
         $this->offset = $offset;
         $this->limit = max(0, $limit - 1);
     }
 
-    // }}}
-    // {{{ function MDB2_Result_Common(&$db, &$result, $limit = 0, $offset = 0)
-
-    /**
-     * PHP 4 Constructor
-     */
-    function MDB2_Result_Common(&$db, &$result, $limit = 0, $offset = 0)
-    {
-        $this->__construct($db, $result, $limit, $offset);
-    }
-
     // }}}
     // {{{ function setResultTypes($types)
 
@@ -3444,9 +3502,9 @@ class MDB2_Result_Common extends MDB2_Result
      *
      * @access  public
      */
-    function &fetchRow($fetchmode = MDB2_FETCHMODE_DEFAULT, $rownum = null)
+    function fetchRow($fetchmode = MDB2_FETCHMODE_DEFAULT, $rownum = null)
     {
-        $err =& $this->db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
+        $err = $this->db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
             'method not implemented', __FUNCTION__);
         return $err;
     }
@@ -3457,11 +3515,10 @@ class MDB2_Result_Common extends MDB2_Result
     /**
      * fetch single column from the next row from a result set
      *
-     * @param   int     the column number to fetch
-     * @param   int     number of the row where the data can be found
-     *
-     * @return  string  data on success, a MDB2 error on failure
+     * @param int|string the column number (or name) to fetch
+     * @param int        number of the row where the data can be found
      *
+     * @return string data on success, a MDB2 error on failure
      * @access  public
      */
     function fetchOne($colnum = 0, $rownum = null)
@@ -3484,10 +3541,9 @@ class MDB2_Result_Common extends MDB2_Result
     /**
      * Fetch and return a column from the current row pointer position
      *
-     * @param   int     the column number to fetch
-     *
-     * @return  mixed   data array on success, a MDB2 error on failure
+     * @param int|string the column number (or name) to fetch
      *
+     * @return  mixed data array on success, a MDB2 error on failure
      * @access  public
      */
     function fetchCol($colnum = 0)
@@ -3548,7 +3604,7 @@ class MDB2_Result_Common extends MDB2_Result
         }
 
         $shift_array = $rekey ? false : null;
-        if (!is_null($shift_array)) {
+        if (null !== $shift_array) {
             if (is_object($row)) {
                 $colnum = count(get_object_vars($row));
             } else {
@@ -3754,7 +3810,7 @@ class MDB2_Result_Common extends MDB2_Result
             $column = $column_names[$column];
         }
         $this->values[$column] =& $value;
-        if (!is_null($type)) {
+        if (null !== $type) {
             $this->types[$column] = $type;
         }
         return MDB2_OK;
@@ -3828,19 +3884,6 @@ class MDB2_Row
         }
     }
 
-    // }}}
-    // {{{ function MDB2_Row(&$row)
-
-    /**
-     * PHP 4 Constructor
-     *
-     * @param   resource    row data as array
-     */
-    function MDB2_Row(&$row)
-    {
-        $this->__construct($row);
-    }
-
     // }}}
 }
 
@@ -3869,15 +3912,15 @@ class MDB2_Statement_Common
     var $is_manip;
 
     // }}}
-    // {{{ constructor: function __construct(&$db, &$statement, $positions, $query, $types, $result_types, $is_manip = false, $limit = null, $offset = null)
+    // {{{ constructor: function __construct($db, $statement, $positions, $query, $types, $result_types, $is_manip = false, $limit = null, $offset = null)
 
     /**
      * Constructor
      */
-    function __construct(&$db, &$statement, $positions, $query, $types, $result_types, $is_manip = false, $limit = null, $offset = null)
+    function __construct($db, $statement, $positions, $query, $types, $result_types, $is_manip = false, $limit = null, $offset = null)
     {
-        $this->db =& $db;
-        $this->statement =& $statement;
+        $this->db = $db;
+        $this->statement = $statement;
         $this->positions = $positions;
         $this->query = $query;
         $this->types = (array)$types;
@@ -3887,17 +3930,6 @@ class MDB2_Statement_Common
         $this->offset = $offset;
     }
 
-    // }}}
-    // {{{ function MDB2_Statement_Common(&$db, &$statement, $positions, $query, $types, $result_types, $is_manip = false, $limit = null, $offset = null)
-
-    /**
-     * PHP 4 Constructor
-     */
-    function MDB2_Statement_Common(&$db, &$statement, $positions, $query, $types, $result_types, $is_manip = false, $limit = null, $offset = null)
-    {
-        $this->__construct($db, $statement, $positions, $query, $types, $result_types, $is_manip, $limit, $offset);
-    }
-
     // }}}
     // {{{ function bindValue($parameter, &$value, $type = null)
 
@@ -3917,14 +3949,16 @@ class MDB2_Statement_Common
     function bindValue($parameter, $value, $type = null)
     {
         if (!is_numeric($parameter)) {
-            $parameter = preg_replace('/^:(.*)$/', '\\1', $parameter);
+            if (strpos($parameter, ':') === 0) {
+                $parameter = substr($parameter, 1);
+            }
         }
         if (!in_array($parameter, $this->positions)) {
             return $this->db->raiseError(MDB2_ERROR_NOT_FOUND, null, null,
                 'Unable to bind to missing placeholder: '.$parameter, __FUNCTION__);
         }
         $this->values[$parameter] = $value;
-        if (!is_null($type)) {
+        if (null !== $type) {
             $this->types[$parameter] = $type;
         }
         return MDB2_OK;
@@ -3950,12 +3984,22 @@ class MDB2_Statement_Common
     {
         $types = is_array($types) ? array_values($types) : array_fill(0, count($values), null);
         $parameters = array_keys($values);
+        $this->db->pushErrorHandling(PEAR_ERROR_RETURN);
+        $this->db->expectError(MDB2_ERROR_NOT_FOUND);
         foreach ($parameters as $key => $parameter) {
             $err = $this->bindValue($parameter, $values[$parameter], $types[$key]);
             if (PEAR::isError($err)) {
+                if ($err->getCode() == MDB2_ERROR_NOT_FOUND) {
+                    //ignore (extra value for missing placeholder)
+                    continue;
+                }
+                $this->db->popExpect();
+                $this->db->popErrorHandling();
                 return $err;
             }
         }
+        $this->db->popExpect();
+        $this->db->popErrorHandling();
         return MDB2_OK;
     }
 
@@ -3978,14 +4022,16 @@ class MDB2_Statement_Common
     function bindParam($parameter, &$value, $type = null)
     {
         if (!is_numeric($parameter)) {
-            $parameter = preg_replace('/^:(.*)$/', '\\1', $parameter);
+            if (strpos($parameter, ':') === 0) {
+                $parameter = substr($parameter, 1);
+            }
         }
         if (!in_array($parameter, $this->positions)) {
             return $this->db->raiseError(MDB2_ERROR_NOT_FOUND, null, null,
                 'Unable to bind to missing placeholder: '.$parameter, __FUNCTION__);
         }
         $this->values[$parameter] =& $value;
-        if (!is_null($type)) {
+        if (null !== $type) {
             $this->types[$parameter] = $type;
         }
         return MDB2_OK;
@@ -4026,19 +4072,19 @@ class MDB2_Statement_Common
     /**
      * Execute a prepared query statement.
      *
-     * @param   array   specifies all necessary information
-     *       for bindParam() the array elements must use keys corresponding to
-     *       the number of the position of the parameter.
-     * @param   mixed   specifies which result class to use
-     * @param   mixed   specifies which class to wrap results in
-     *
-     * @return  mixed   a result handle or MDB2_OK on success, a MDB2 error on failure
+     * @param array specifies all necessary information
+     *              for bindParam() the array elements must use keys corresponding
+     *              to the number of the position of the parameter.
+     * @param mixed specifies which result class to use
+     * @param mixed specifies which class to wrap results in
      *
-     * @access  public
+     * @return mixed MDB2_Result or integer (affected rows) on success,
+     *               a MDB2 error on failure
+     * @access public
      */
-    function &execute($values = null, $result_class = true, $result_wrap_class = false)
+    function execute($values = null, $result_class = true, $result_wrap_class = false)
     {
-        if (is_null($this->positions)) {
+        if (null === $this->positions) {
             return $this->db->raiseError(MDB2_ERROR, null, null,
                 'Prepared statement has already been freed', __FUNCTION__);
         }
@@ -4051,12 +4097,12 @@ class MDB2_Statement_Common
                                             'Binding Values failed with message: ' . $err->getMessage(), __FUNCTION__);
             }
         }
-        $result =& $this->_execute($result_class, $result_wrap_class);
+        $result = $this->_execute($result_class, $result_wrap_class);
         return $result;
     }
 
     // }}}
-    // {{{ function &_execute($result_class = true, $result_wrap_class = false)
+    // {{{ function _execute($result_class = true, $result_wrap_class = false)
 
     /**
      * Execute a prepared query statement helper method.
@@ -4064,11 +4110,11 @@ class MDB2_Statement_Common
      * @param   mixed   specifies which result class to use
      * @param   mixed   specifies which class to wrap results in
      *
-     * @return  mixed   MDB2_Result or integer on success, a MDB2 error on failure
-     *
+     * @return mixed MDB2_Result or integer (affected rows) on success,
+     *               a MDB2 error on failure
      * @access  private
      */
-    function &_execute($result_class = true, $result_wrap_class = false)
+    function _execute($result_class = true, $result_wrap_class = false)
     {
         $this->last_query = $this->query;
         $query = '';
@@ -4099,7 +4145,7 @@ class MDB2_Statement_Common
         if ($this->is_manip) {
             $result = $this->db->exec($query);
         } else {
-            $result =& $this->db->query($query, $this->result_types, $result_class, $result_wrap_class);
+            $result = $this->db->query($query, $this->result_types, $result_class, $result_wrap_class);
         }
         return $result;
     }
@@ -4116,7 +4162,7 @@ class MDB2_Statement_Common
      */
     function free()
     {
-        if (is_null($this->positions)) {
+        if (null === $this->positions) {
             return $this->db->raiseError(MDB2_ERROR, null, null,
                 'Prepared statement has already been freed', __FUNCTION__);
         }
@@ -4158,7 +4204,7 @@ class MDB2_Module_Common
      * @var     int
      * @access  protected
      */
-    var $db_index;
+    protected $db_index;
 
     // }}}
     // {{{ constructor: function __construct($db_index)
@@ -4172,18 +4218,7 @@ class MDB2_Module_Common
     }
 
     // }}}
-    // {{{ function MDB2_Module_Common($db_index)
-
-    /**
-     * PHP 4 Constructor
-     */
-    function MDB2_Module_Common($db_index)
-    {
-        $this->__construct($db_index);
-    }
-
-    // }}}
-    // {{{ function &getDBInstance()
+    // {{{ function getDBInstance()
 
     /**
      * Get the instance of MDB2 associated with the module instance
@@ -4192,12 +4227,12 @@ class MDB2_Module_Common
      *
      * @access  public
      */
-    function &getDBInstance()
+    function getDBInstance()
     {
         if (isset($GLOBALS['_MDB2_databases'][$this->db_index])) {
-            $result =& $GLOBALS['_MDB2_databases'][$this->db_index];
+            $result = $GLOBALS['_MDB2_databases'][$this->db_index];
         } else {
-            $result =& MDB2::raiseError(MDB2_ERROR_NOT_FOUND, null, null,
+            $result = MDB2::raiseError(MDB2_ERROR_NOT_FOUND, null, null,
                 'could not find MDB2 instance');
         }
         return $result;
@@ -4257,4 +4292,3 @@ function MDB2_defaultDebugOutput(&$db, $scope, $message, $context = array())
 }
 
 // }}}
-?>
\ No newline at end of file