2 // vim: set et ts=4 sw=4 fdm=marker:
 
   3 // +----------------------------------------------------------------------+
 
   4 // | PHP versions 4 and 5                                                 |
 
   5 // +----------------------------------------------------------------------+
 
   6 // | Copyright (c) 1998-2007 Manuel Lemos, Tomas V.V.Cox,                 |
 
   7 // | Stig. S. Bakken, Lukas Smith                                         |
 
   8 // | All rights reserved.                                                 |
 
   9 // +----------------------------------------------------------------------+
 
  10 // | MDB2 is a merge of PEAR DB and Metabases that provides a unified DB  |
 
  11 // | API as well as database abstraction for PHP applications.            |
 
  12 // | This LICENSE is in the BSD license style.                            |
 
  14 // | Redistribution and use in source and binary forms, with or without   |
 
  15 // | modification, are permitted provided that the following conditions   |
 
  18 // | Redistributions of source code must retain the above copyright       |
 
  19 // | notice, this list of conditions and the following disclaimer.        |
 
  21 // | Redistributions in binary form must reproduce the above copyright    |
 
  22 // | notice, this list of conditions and the following disclaimer in the  |
 
  23 // | documentation and/or other materials provided with the distribution. |
 
  25 // | Neither the name of Manuel Lemos, Tomas V.V.Cox, Stig. S. Bakken,    |
 
  26 // | Lukas Smith nor the names of his contributors may be used to endorse |
 
  27 // | or promote products derived from this software without specific prior|
 
  28 // | written permission.                                                  |
 
  30 // | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS  |
 
  31 // | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT    |
 
  32 // | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS    |
 
  33 // | FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE      |
 
  34 // | REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,          |
 
  35 // | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
 
  36 // | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS|
 
  37 // |  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED  |
 
  38 // | AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT          |
 
  39 // | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY|
 
  40 // | WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE          |
 
  41 // | POSSIBILITY OF SUCH DAMAGE.                                          |
 
  42 // +----------------------------------------------------------------------+
 
  43 // | Author: Lukas Smith <smith@pooteeweet.org>                           |
 
  44 // +----------------------------------------------------------------------+
 
  46 // $Id: MDB2.php 328183 2012-10-29 15:10:42Z danielc $
 
  52  * @author      Lukas Smith <smith@pooteeweet.org>
 
  55 require_once 'PEAR.php';
 
  57 // {{{ Error constants
 
  60  * The method mapErrorCode in each MDB2_dbtype implementation maps
 
  61  * native error codes to one of these.
 
  63  * If you add an error code here, make sure you also add a textual
 
  64  * version of it in MDB2::errorMessage().
 
  67 define('MDB2_OK',                      true);
 
  68 define('MDB2_ERROR',                     -1);
 
  69 define('MDB2_ERROR_SYNTAX',              -2);
 
  70 define('MDB2_ERROR_CONSTRAINT',          -3);
 
  71 define('MDB2_ERROR_NOT_FOUND',           -4);
 
  72 define('MDB2_ERROR_ALREADY_EXISTS',      -5);
 
  73 define('MDB2_ERROR_UNSUPPORTED',         -6);
 
  74 define('MDB2_ERROR_MISMATCH',            -7);
 
  75 define('MDB2_ERROR_INVALID',             -8);
 
  76 define('MDB2_ERROR_NOT_CAPABLE',         -9);
 
  77 define('MDB2_ERROR_TRUNCATED',          -10);
 
  78 define('MDB2_ERROR_INVALID_NUMBER',     -11);
 
  79 define('MDB2_ERROR_INVALID_DATE',       -12);
 
  80 define('MDB2_ERROR_DIVZERO',            -13);
 
  81 define('MDB2_ERROR_NODBSELECTED',       -14);
 
  82 define('MDB2_ERROR_CANNOT_CREATE',      -15);
 
  83 define('MDB2_ERROR_CANNOT_DELETE',      -16);
 
  84 define('MDB2_ERROR_CANNOT_DROP',        -17);
 
  85 define('MDB2_ERROR_NOSUCHTABLE',        -18);
 
  86 define('MDB2_ERROR_NOSUCHFIELD',        -19);
 
  87 define('MDB2_ERROR_NEED_MORE_DATA',     -20);
 
  88 define('MDB2_ERROR_NOT_LOCKED',         -21);
 
  89 define('MDB2_ERROR_VALUE_COUNT_ON_ROW', -22);
 
  90 define('MDB2_ERROR_INVALID_DSN',        -23);
 
  91 define('MDB2_ERROR_CONNECT_FAILED',     -24);
 
  92 define('MDB2_ERROR_EXTENSION_NOT_FOUND',-25);
 
  93 define('MDB2_ERROR_NOSUCHDB',           -26);
 
  94 define('MDB2_ERROR_ACCESS_VIOLATION',   -27);
 
  95 define('MDB2_ERROR_CANNOT_REPLACE',     -28);
 
  96 define('MDB2_ERROR_CONSTRAINT_NOT_NULL',-29);
 
  97 define('MDB2_ERROR_DEADLOCK',           -30);
 
  98 define('MDB2_ERROR_CANNOT_ALTER',       -31);
 
  99 define('MDB2_ERROR_MANAGER',            -32);
 
 100 define('MDB2_ERROR_MANAGER_PARSE',      -33);
 
 101 define('MDB2_ERROR_LOADMODULE',         -34);
 
 102 define('MDB2_ERROR_INSUFFICIENT_DATA',  -35);
 
 103 define('MDB2_ERROR_NO_PERMISSION',      -36);
 
 104 define('MDB2_ERROR_DISCONNECT_FAILED',  -37);
 
 107 // {{{ Verbose constants
 
 109  * These are just helper constants to more verbosely express parameters to prepare()
 
 112 define('MDB2_PREPARE_MANIP', false);
 
 113 define('MDB2_PREPARE_RESULT', null);
 
 116 // {{{ Fetchmode constants
 
 119  * This is a special constant that tells MDB2 the user hasn't specified
 
 120  * any particular get mode, so the default should be used.
 
 122 define('MDB2_FETCHMODE_DEFAULT', 0);
 
 125  * Column data indexed by numbers, ordered from 0 and up
 
 127 define('MDB2_FETCHMODE_ORDERED', 1);
 
 130  * Column data indexed by column names
 
 132 define('MDB2_FETCHMODE_ASSOC', 2);
 
 135  * Column data as object properties
 
 137 define('MDB2_FETCHMODE_OBJECT', 3);
 
 140  * For multi-dimensional results: normally the first level of arrays
 
 141  * is the row number, and the second level indexed by column number or name.
 
 142  * MDB2_FETCHMODE_FLIPPED switches this order, so the first level of arrays
 
 143  * is the column name, and the second level the row number.
 
 145 define('MDB2_FETCHMODE_FLIPPED', 4);
 
 148 // {{{ Portability mode constants
 
 151  * Portability: turn off all portability features.
 
 152  * @see MDB2_Driver_Common::setOption()
 
 154 define('MDB2_PORTABILITY_NONE', 0);
 
 157  * Portability: convert names of tables and fields to case defined in the
 
 158  * "field_case" option when using the query*(), fetch*() and tableInfo() methods.
 
 159  * @see MDB2_Driver_Common::setOption()
 
 161 define('MDB2_PORTABILITY_FIX_CASE', 1);
 
 164  * Portability: right trim the data output by query*() and fetch*().
 
 165  * @see MDB2_Driver_Common::setOption()
 
 167 define('MDB2_PORTABILITY_RTRIM', 2);
 
 170  * Portability: force reporting the number of rows deleted.
 
 171  * @see MDB2_Driver_Common::setOption()
 
 173 define('MDB2_PORTABILITY_DELETE_COUNT', 4);
 
 176  * Portability: not needed in MDB2 (just left here for compatibility to DB)
 
 177  * @see MDB2_Driver_Common::setOption()
 
 179 define('MDB2_PORTABILITY_NUMROWS', 8);
 
 182  * Portability: makes certain error messages in certain drivers compatible
 
 183  * with those from other DBMS's.
 
 185  * + mysql, mysqli:  change unique/primary key constraints
 
 186  *   MDB2_ERROR_ALREADY_EXISTS -> MDB2_ERROR_CONSTRAINT
 
 188  * + odbc(access):  MS's ODBC driver reports 'no such field' as code
 
 189  *   07001, which means 'too few parameters.'  When this option is on
 
 190  *   that code gets mapped to MDB2_ERROR_NOSUCHFIELD.
 
 192  * @see MDB2_Driver_Common::setOption()
 
 194 define('MDB2_PORTABILITY_ERRORS', 16);
 
 197  * Portability: convert empty values to null strings in data output by
 
 198  * query*() and fetch*().
 
 199  * @see MDB2_Driver_Common::setOption()
 
 201 define('MDB2_PORTABILITY_EMPTY_TO_NULL', 32);
 
 204  * Portability: removes database/table qualifiers from associative indexes
 
 205  * @see MDB2_Driver_Common::setOption()
 
 207 define('MDB2_PORTABILITY_FIX_ASSOC_FIELD_NAMES', 64);
 
 210  * Portability: turn on all portability features.
 
 211  * @see MDB2_Driver_Common::setOption()
 
 213 define('MDB2_PORTABILITY_ALL', 127);
 
 216 // {{{ Globals for class instance tracking
 
 219  * These are global variables that are used to track the various class instances
 
 222 $GLOBALS['_MDB2_databases'] = array();
 
 223 $GLOBALS['_MDB2_dsninfo_default'] = array(
 
 240  * The main 'MDB2' class is simply a container class with some static
 
 241  * methods for creating DB objects as well as some utility functions
 
 242  * common to all parts of DB.
 
 244  * The object model of MDB2 is as follows (indentation means inheritance):
 
 246  * MDB2          The main MDB2 class.  This is simply a utility class
 
 247  *              with some 'static' methods for creating MDB2 objects as
 
 248  *              well as common utility functions for other MDB2 classes.
 
 250  * MDB2_Driver_Common   The base for each MDB2 implementation.  Provides default
 
 251  * |            implementations (in OO lingo virtual methods) for
 
 252  * |            the actual DB implementations as well as a bunch of
 
 253  * |            query utility functions.
 
 255  * +-MDB2_Driver_mysql  The MDB2 implementation for MySQL. Inherits MDB2_Driver_Common.
 
 256  *              When calling MDB2::factory or MDB2::connect for MySQL
 
 257  *              connections, the object returned is an instance of this
 
 259  * +-MDB2_Driver_pgsql  The MDB2 implementation for PostGreSQL. Inherits MDB2_Driver_Common.
 
 260  *              When calling MDB2::factory or MDB2::connect for PostGreSQL
 
 261  *              connections, the object returned is an instance of this
 
 266  * @author      Lukas Smith <smith@pooteeweet.org>
 
 270     // {{{ function setOptions($db, $options)
 
 273      * set option array   in an exiting database object
 
 275      * @param   MDB2_Driver_Common  MDB2 object
 
 276      * @param   array   An associative array of option names and their values.
 
 278      * @return mixed   MDB2_OK or a PEAR Error object
 
 282     static function setOptions($db, $options)
 
 284         if (is_array($options)) {
 
 285             foreach ($options as $option => $value) {
 
 286                 $test = $db->setOption($option, $value);
 
 287                 if (MDB2::isError($test)) {
 
 296     // {{{ function classExists($classname)
 
 299      * Checks if a class exists without triggering __autoload
 
 301      * @param   string  classname
 
 303      * @return  bool    true success and false on error
 
 307     static function classExists($classname)
 
 309         return class_exists($classname, false);
 
 313     // {{{ function loadClass($class_name, $debug)
 
 316      * Loads a PEAR class.
 
 318      * @param   string  classname to load
 
 319      * @param   bool    if errors should be suppressed
 
 321      * @return  mixed   true success or PEAR_Error on failure
 
 325     static function loadClass($class_name, $debug)
 
 327         if (!MDB2::classExists($class_name)) {
 
 328             $file_name = str_replace('_', DIRECTORY_SEPARATOR, $class_name).'.php';
 
 330                 $include = include_once($file_name);
 
 332                 $include = @include_once($file_name);
 
 335                 if (!MDB2::fileExists($file_name)) {
 
 336                     $msg = "unable to find package '$class_name' file '$file_name'";
 
 338                     $msg = "unable to load class '$class_name' from file '$file_name'";
 
 340                 $err = MDB2::raiseError(MDB2_ERROR_NOT_FOUND, null, null, $msg);
 
 343             if (!MDB2::classExists($class_name)) {
 
 344                 $msg = "unable to load class '$class_name' from file '$file_name'";
 
 345                 $err = MDB2::raiseError(MDB2_ERROR_NOT_FOUND, null, null, $msg);
 
 353     // {{{ function factory($dsn, $options = false)
 
 356      * Create a new MDB2 object for the specified database type
 
 358      * @param   mixed   'data source name', see the MDB2::parseDSN
 
 359      *                      method for a description of the dsn format.
 
 360      *                      Can also be specified as an array of the
 
 361      *                      format returned by MDB2::parseDSN.
 
 362      * @param   array   An associative array of option names and
 
 365      * @return  mixed   a newly created MDB2 object, or false on error
 
 369     static function factory($dsn, $options = false)
 
 371         $dsninfo = MDB2::parseDSN($dsn);
 
 372         if (empty($dsninfo['phptype'])) {
 
 373             $err = MDB2::raiseError(MDB2_ERROR_NOT_FOUND,
 
 374                 null, null, 'no RDBMS driver specified');
 
 377         $class_name = 'MDB2_Driver_'.$dsninfo['phptype'];
 
 379         $debug = (!empty($options['debug']));
 
 380         $err = MDB2::loadClass($class_name, $debug);
 
 381         if (MDB2::isError($err)) {
 
 385         $db = new $class_name();
 
 386         $db->setDSN($dsninfo);
 
 387         $err = MDB2::setOptions($db, $options);
 
 388         if (MDB2::isError($err)) {
 
 396     // {{{ function connect($dsn, $options = false)
 
 399      * Create a new MDB2_Driver_* connection object and connect to the specified
 
 402      * @param mixed $dsn     'data source name', see the MDB2::parseDSN
 
 403      *                       method for a description of the dsn format.
 
 404      *                       Can also be specified as an array of the
 
 405      *                       format returned by MDB2::parseDSN.
 
 406      * @param array $options An associative array of option names and
 
 409      * @return mixed a newly created MDB2 connection object, or a MDB2
 
 410      *               error object on error
 
 413      * @see     MDB2::parseDSN
 
 415     static function connect($dsn, $options = false)
 
 417         $db = MDB2::factory($dsn, $options);
 
 418         if (MDB2::isError($db)) {
 
 422         $err = $db->connect();
 
 423         if (MDB2::isError($err)) {
 
 424             $dsn = $db->getDSN('string', 'xxx');
 
 426             $err->addUserInfo($dsn);
 
 434     // {{{ function singleton($dsn = null, $options = false)
 
 437      * Returns a MDB2 connection with the requested DSN.
 
 438      * A new MDB2 connection object is only created if no object with the
 
 439      * requested DSN exists yet.
 
 441      * @param   mixed   'data source name', see the MDB2::parseDSN
 
 442      *                            method for a description of the dsn format.
 
 443      *                            Can also be specified as an array of the
 
 444      *                            format returned by MDB2::parseDSN.
 
 445      * @param   array   An associative array of option names and
 
 448      * @return  mixed   a newly created MDB2 connection object, or a MDB2
 
 449      *                  error object on error
 
 452      * @see     MDB2::parseDSN
 
 454     static function singleton($dsn = null, $options = false)
 
 457             $dsninfo = MDB2::parseDSN($dsn);
 
 458             $dsninfo = array_merge($GLOBALS['_MDB2_dsninfo_default'], $dsninfo);
 
 459             $keys = array_keys($GLOBALS['_MDB2_databases']);
 
 460             for ($i=0, $j=count($keys); $i<$j; ++$i) {
 
 461                 if (isset($GLOBALS['_MDB2_databases'][$keys[$i]])) {
 
 462                     $tmp_dsn = $GLOBALS['_MDB2_databases'][$keys[$i]]->getDSN('array');
 
 463                     if (count(array_diff_assoc($tmp_dsn, $dsninfo)) == 0) {
 
 464                         MDB2::setOptions($GLOBALS['_MDB2_databases'][$keys[$i]], $options);
 
 465                         return $GLOBALS['_MDB2_databases'][$keys[$i]];
 
 469         } elseif (is_array($GLOBALS['_MDB2_databases']) && reset($GLOBALS['_MDB2_databases'])) {
 
 470             return $GLOBALS['_MDB2_databases'][key($GLOBALS['_MDB2_databases'])];
 
 472         $db = MDB2::factory($dsn, $options);
 
 477     // {{{ function areEquals()
 
 480      * It looks like there's a memory leak in array_diff() in PHP 5.1.x,
 
 481      * so use this method instead.
 
 482      * @see http://pear.php.net/bugs/bug.php?id=11790
 
 488     static function areEquals($arr1, $arr2)
 
 490         if (count($arr1) != count($arr2)) {
 
 493         foreach (array_keys($arr1) as $k) {
 
 494             if (!array_key_exists($k, $arr2) || $arr1[$k] != $arr2[$k]) {
 
 502     // {{{ function loadFile($file)
 
 505      * load a file (like 'Date')
 
 507      * @param string $file name of the file in the MDB2 directory (without '.php')
 
 509      * @return string name of the file that was included
 
 513     static function loadFile($file)
 
 515         $file_name = 'MDB2'.DIRECTORY_SEPARATOR.$file.'.php';
 
 516         if (!MDB2::fileExists($file_name)) {
 
 517             return MDB2::raiseError(MDB2_ERROR_NOT_FOUND, null, null,
 
 518                 'unable to find: '.$file_name);
 
 520         if (!include_once($file_name)) {
 
 521             return MDB2::raiseError(MDB2_ERROR_NOT_FOUND, null, null,
 
 522                 'unable to load driver class: '.$file_name);
 
 528     // {{{ function apiVersion()
 
 531      * Return the MDB2 API version
 
 533      * @return  string  the MDB2 API version number
 
 537     static function apiVersion()
 
 543     // {{{ function &raiseError($code = null, $mode = null, $options = null, $userinfo = null)
 
 546      * This method is used to communicate an error and invoke error
 
 547      * callbacks etc.  Basically a wrapper for PEAR::raiseError
 
 548      * without the message string.
 
 550      * @param   mixed  int error code
 
 552      * @param   int    error mode, see PEAR_Error docs
 
 554      * @param   mixed  If error mode is PEAR_ERROR_TRIGGER, this is the
 
 555      *                 error level (E_USER_NOTICE etc).  If error mode is
 
 556      *                 PEAR_ERROR_CALLBACK, this is the callback function,
 
 557      *                 either as a function name, or as an array of an
 
 558      *                 object and method name.  For other error modes this
 
 559      *                 parameter is ignored.
 
 561      * @param   string Extra debug information.  Defaults to the last
 
 562      *                 query and native error code.
 
 564      * @return PEAR_Error instance of a PEAR Error object
 
 569     public static function &raiseError($code = null,
 
 578         $err =& $pear->raiseError(null, $code, $mode, $options, $userinfo, 'MDB2_Error', true);
 
 583     // {{{ function isError($data, $code = null)
 
 586      * Tell whether a value is a MDB2 error.
 
 588      * @param   mixed   the value to test
 
 589      * @param   int     if is an error object, return true
 
 590      *                        only if $code is a string and
 
 591      *                        $db->getMessage() == $code or
 
 592      *                        $code is an integer and $db->getCode() == $code
 
 594      * @return  bool    true if parameter is an error
 
 598     static function isError($data, $code = null)
 
 600         if ($data instanceof MDB2_Error) {
 
 601             if (null === $code) {
 
 604             if (is_string($code)) {
 
 605                 return $data->getMessage() === $code;
 
 607             return in_array($data->getCode(), (array)$code);
 
 613     // {{{ function isConnection($value)
 
 616      * Tell whether a value is a MDB2 connection
 
 618      * @param   mixed   value to test
 
 620      * @return  bool    whether $value is a MDB2 connection
 
 623     static function isConnection($value)
 
 625         return ($value instanceof MDB2_Driver_Common);
 
 629     // {{{ function isResult($value)
 
 632      * Tell whether a value is a MDB2 result
 
 634      * @param mixed $value value to test
 
 636      * @return bool whether $value is a MDB2 result
 
 640     static function isResult($value)
 
 642         return ($value instanceof MDB2_Result);
 
 646     // {{{ function isResultCommon($value)
 
 649      * Tell whether a value is a MDB2 result implementing the common interface
 
 651      * @param mixed $value value to test
 
 653      * @return bool whether $value is a MDB2 result implementing the common interface
 
 657     static function isResultCommon($value)
 
 659         return ($value instanceof MDB2_Result_Common);
 
 663     // {{{ function isStatement($value)
 
 666      * Tell whether a value is a MDB2 statement interface
 
 668      * @param   mixed   value to test
 
 670      * @return  bool    whether $value is a MDB2 statement interface
 
 674     static function isStatement($value)
 
 676         return ($value instanceof MDB2_Statement_Common);
 
 680     // {{{ function errorMessage($value = null)
 
 683      * Return a textual error message for a MDB2 error code
 
 685      * @param   int|array   integer error code,
 
 686                                 null to get the current error code-message map,
 
 687                                 or an array with a new error code-message map
 
 689      * @return  string  error message, or false if the error code was
 
 694     static function errorMessage($value = null)
 
 696         static $errorMessages;
 
 698         if (is_array($value)) {
 
 699             $errorMessages = $value;
 
 703         if (!isset($errorMessages)) {
 
 704             $errorMessages = array(
 
 705                 MDB2_OK                       => 'no error',
 
 706                 MDB2_ERROR                    => 'unknown error',
 
 707                 MDB2_ERROR_ALREADY_EXISTS     => 'already exists',
 
 708                 MDB2_ERROR_CANNOT_CREATE      => 'can not create',
 
 709                 MDB2_ERROR_CANNOT_ALTER       => 'can not alter',
 
 710                 MDB2_ERROR_CANNOT_REPLACE     => 'can not replace',
 
 711                 MDB2_ERROR_CANNOT_DELETE      => 'can not delete',
 
 712                 MDB2_ERROR_CANNOT_DROP        => 'can not drop',
 
 713                 MDB2_ERROR_CONSTRAINT         => 'constraint violation',
 
 714                 MDB2_ERROR_CONSTRAINT_NOT_NULL=> 'null value violates not-null constraint',
 
 715                 MDB2_ERROR_DIVZERO            => 'division by zero',
 
 716                 MDB2_ERROR_INVALID            => 'invalid',
 
 717                 MDB2_ERROR_INVALID_DATE       => 'invalid date or time',
 
 718                 MDB2_ERROR_INVALID_NUMBER     => 'invalid number',
 
 719                 MDB2_ERROR_MISMATCH           => 'mismatch',
 
 720                 MDB2_ERROR_NODBSELECTED       => 'no database selected',
 
 721                 MDB2_ERROR_NOSUCHFIELD        => 'no such field',
 
 722                 MDB2_ERROR_NOSUCHTABLE        => 'no such table',
 
 723                 MDB2_ERROR_NOT_CAPABLE        => 'MDB2 backend not capable',
 
 724                 MDB2_ERROR_NOT_FOUND          => 'not found',
 
 725                 MDB2_ERROR_NOT_LOCKED         => 'not locked',
 
 726                 MDB2_ERROR_SYNTAX             => 'syntax error',
 
 727                 MDB2_ERROR_UNSUPPORTED        => 'not supported',
 
 728                 MDB2_ERROR_VALUE_COUNT_ON_ROW => 'value count on row',
 
 729                 MDB2_ERROR_INVALID_DSN        => 'invalid DSN',
 
 730                 MDB2_ERROR_CONNECT_FAILED     => 'connect failed',
 
 731                 MDB2_ERROR_NEED_MORE_DATA     => 'insufficient data supplied',
 
 732                 MDB2_ERROR_EXTENSION_NOT_FOUND=> 'extension not found',
 
 733                 MDB2_ERROR_NOSUCHDB           => 'no such database',
 
 734                 MDB2_ERROR_ACCESS_VIOLATION   => 'insufficient permissions',
 
 735                 MDB2_ERROR_LOADMODULE         => 'error while including on demand module',
 
 736                 MDB2_ERROR_TRUNCATED          => 'truncated',
 
 737                 MDB2_ERROR_DEADLOCK           => 'deadlock detected',
 
 738                 MDB2_ERROR_NO_PERMISSION      => 'no permission',
 
 739                 MDB2_ERROR_DISCONNECT_FAILED  => 'disconnect failed',
 
 743         if (null === $value) {
 
 744             return $errorMessages;
 
 747         if (MDB2::isError($value)) {
 
 748             $value = $value->getCode();
 
 751         return isset($errorMessages[$value]) ?
 
 752            $errorMessages[$value] : $errorMessages[MDB2_ERROR];
 
 756     // {{{ function parseDSN($dsn)
 
 759      * Parse a data source name.
 
 761      * Additional keys can be added by appending a URI query string to the
 
 764      * The format of the supplied DSN is in its fullest form:
 
 766      *  phptype(dbsyntax)://username:password@protocol+hostspec/database?option=8&another=true
 
 769      * Most variations are allowed:
 
 771      *  phptype://username:password@protocol+hostspec:110//usr/db_file.db?mode=0644
 
 772      *  phptype://username:password@hostspec/database_name
 
 773      *  phptype://username:password@hostspec
 
 774      *  phptype://username@hostspec
 
 775      *  phptype://hostspec/database
 
 781      * @param   string  Data Source Name to be parsed
 
 783      * @return  array   an associative array with the following keys:
 
 784      *  + phptype:  Database backend used in PHP (mysql, odbc etc.)
 
 785      *  + dbsyntax: Database used with regards to SQL syntax etc.
 
 786      *  + protocol: Communication protocol to use (tcp, unix etc.)
 
 787      *  + hostspec: Host specification (hostname[:port])
 
 788      *  + database: Database to use on the DBMS server
 
 789      *  + username: User name for login
 
 790      *  + password: Password for login
 
 793      * @author  Tomas V.V.Cox <cox@idecnet.com>
 
 795     static function parseDSN($dsn)
 
 797         $parsed = $GLOBALS['_MDB2_dsninfo_default'];
 
 799         if (is_array($dsn)) {
 
 800             $dsn = array_merge($parsed, $dsn);
 
 801             if (!$dsn['dbsyntax']) {
 
 802                 $dsn['dbsyntax'] = $dsn['phptype'];
 
 807         // Find phptype and dbsyntax
 
 808         if (($pos = strpos($dsn, '://')) !== false) {
 
 809             $str = substr($dsn, 0, $pos);
 
 810             $dsn = substr($dsn, $pos + 3);
 
 816         // Get phptype and dbsyntax
 
 817         // $str => phptype(dbsyntax)
 
 818         if (preg_match('|^(.+?)\((.*?)\)$|', $str, $arr)) {
 
 819             $parsed['phptype']  = $arr[1];
 
 820             $parsed['dbsyntax'] = !$arr[2] ? $arr[1] : $arr[2];
 
 822             $parsed['phptype']  = $str;
 
 823             $parsed['dbsyntax'] = $str;
 
 830         // Get (if found): username and password
 
 831         // $dsn => username:password@protocol+hostspec/database
 
 832         if (($at = strrpos($dsn,'@')) !== false) {
 
 833             $str = substr($dsn, 0, $at);
 
 834             $dsn = substr($dsn, $at + 1);
 
 835             if (($pos = strpos($str, ':')) !== false) {
 
 836                 $parsed['username'] = rawurldecode(substr($str, 0, $pos));
 
 837                 $parsed['password'] = rawurldecode(substr($str, $pos + 1));
 
 839                 $parsed['username'] = rawurldecode($str);
 
 843         // Find protocol and hostspec
 
 845         // $dsn => proto(proto_opts)/database
 
 846         if (preg_match('|^([^(]+)\((.*?)\)/?(.*?)$|', $dsn, $match)) {
 
 848             $proto_opts  = $match[2] ? $match[2] : false;
 
 851         // $dsn => protocol+hostspec/database (old format)
 
 853             if (strpos($dsn, '+') !== false) {
 
 854                 list($proto, $dsn) = explode('+', $dsn, 2);
 
 856             if (   strpos($dsn, '//') === 0
 
 857                 && strpos($dsn, '/', 2) !== false
 
 858                 && $parsed['phptype'] == 'oci8'
 
 860                 //oracle's "Easy Connect" syntax:
 
 861                 //"username/password@[//]host[:port][/service_name]"
 
 862                 //e.g. "scott/tiger@//mymachine:1521/oracle"
 
 864                 $pos = strrpos($proto_opts, '/');
 
 865                 $dsn = substr($proto_opts, $pos + 1);
 
 866                 $proto_opts = substr($proto_opts, 0, $pos);
 
 867             } elseif (strpos($dsn, '/') !== false) {
 
 868                 list($proto_opts, $dsn) = explode('/', $dsn, 2);
 
 875         // process the different protocol options
 
 876         $parsed['protocol'] = (!empty($proto)) ? $proto : 'tcp';
 
 877         $proto_opts = rawurldecode($proto_opts);
 
 878         if (strpos($proto_opts, ':') !== false) {
 
 879             list($proto_opts, $parsed['port']) = explode(':', $proto_opts);
 
 881         if ($parsed['protocol'] == 'tcp') {
 
 882             $parsed['hostspec'] = $proto_opts;
 
 883         } elseif ($parsed['protocol'] == 'unix') {
 
 884             $parsed['socket'] = $proto_opts;
 
 891             if (($pos = strpos($dsn, '?')) === false) {
 
 892                 $parsed['database'] = rawurldecode($dsn);
 
 893             // /database?param1=value1¶m2=value2
 
 895                 $parsed['database'] = rawurldecode(substr($dsn, 0, $pos));
 
 896                 $dsn = substr($dsn, $pos + 1);
 
 897                 if (strpos($dsn, '&') !== false) {
 
 898                     $opts = explode('&', $dsn);
 
 899                 } else { // database?param1=value1
 
 902                 foreach ($opts as $opt) {
 
 903                     list($key, $value) = explode('=', $opt);
 
 904                     if (!array_key_exists($key, $parsed) || false === $parsed[$key]) {
 
 905                         // don't allow params overwrite
 
 906                         $parsed[$key] = rawurldecode($value);
 
 916     // {{{ function fileExists($file)
 
 919      * Checks if a file exists in the include path
 
 921      * @param   string  filename
 
 923      * @return  bool    true success and false on error
 
 927     static function fileExists($file)
 
 929         // safe_mode does notwork with is_readable()
 
 930         if (!@ini_get('safe_mode')) {
 
 931              $dirs = explode(PATH_SEPARATOR, ini_get('include_path'));
 
 932              foreach ($dirs as $dir) {
 
 933                  if (is_readable($dir . DIRECTORY_SEPARATOR . $file)) {
 
 938             $fp = @fopen($file, 'r', true);
 
 939             if (is_resource($fp)) {
 
 950 // {{{ class MDB2_Error extends PEAR_Error
 
 953  * MDB2_Error implements a class for reporting portable database error
 
 958  * @author Stig Bakken <ssb@fast.no>
 
 960 class MDB2_Error extends PEAR_Error
 
 962     // {{{ constructor: function MDB2_Error($code = MDB2_ERROR, $mode = PEAR_ERROR_RETURN, $level = E_USER_NOTICE, $debuginfo = null)
 
 965      * MDB2_Error constructor.
 
 967      * @param   mixed   MDB2 error code, or string with error message.
 
 968      * @param   int     what 'error mode' to operate in
 
 969      * @param   int     what error level to use for $mode & PEAR_ERROR_TRIGGER
 
 970      * @param   mixed   additional debug info, such as the last query
 
 972     function __construct($code = MDB2_ERROR, $mode = PEAR_ERROR_RETURN,
 
 973               $level = E_USER_NOTICE, $debuginfo = null, $dummy = null)
 
 975         if (null === $code) {
 
 978         $this->PEAR_Error('MDB2 Error: '.MDB2::errorMessage($code), $code,
 
 979             $mode, $level, $debuginfo);
 
 986 // {{{ class MDB2_Driver_Common extends PEAR
 
 989  * MDB2_Driver_Common: Base class that is extended by each MDB2 driver
 
 993  * @author      Lukas Smith <smith@pooteeweet.org>
 
 995 class MDB2_Driver_Common
 
 997     // {{{ Variables (Properties)
 
1000      * @var MDB2_Driver_Datatype_Common
 
1005      * @var MDB2_Extended
 
1010      * @var MDB2_Driver_Function_Common
 
1015      * @var MDB2_Driver_Manager_Common
 
1020      * @var MDB2_Driver_Native_Commonn
 
1025      * @var MDB2_Driver_Reverse_Common
 
1030      * index of the MDB2 object within the $GLOBALS['_MDB2_databases'] array
 
1034     public $db_index = 0;
 
1037      * DSN used for the next query
 
1041     public $dsn = array();
 
1044      * DSN that was used to create the current connection
 
1048     public $connected_dsn = array();
 
1051      * connection resource
 
1055     public $connection = 0;
 
1058      * if the current opened connection is a persistent connection
 
1062     public $opened_persistent;
 
1065      * the name of the database for the next query
 
1069     public $database_name = '';
 
1072      * the name of the database currently selected
 
1076     public $connected_database_name = '';
 
1079      * server version information
 
1083     public $connected_server_info = '';
 
1086      * list of all supported features of the given driver
 
1090     public $supported = array(
 
1091         'sequences' => false,
 
1093         'affected_rows' => false,
 
1094         'summary_functions' => false,
 
1095         'order_by_text' => false,
 
1096         'transactions' => false,
 
1097         'savepoints' => false,
 
1098         'current_id' => false,
 
1099         'limit_queries' => false,
 
1102         'sub_selects' => false,
 
1103         'triggers' => false,
 
1104         'auto_increment' => false,
 
1105         'primary_key' => false,
 
1106         'result_introspection' => false,
 
1107         'prepared_statements' => false,
 
1108         'identifier_quoting' => false,
 
1109         'pattern_escaping' => false,
 
1110         'new_link' => false,
 
1114      * Array of supported options that can be passed to the MDB2 instance.
 
1116      * The options can be set during object creation, using
 
1117      * MDB2::connect(), MDB2::factory() or MDB2::singleton(). The options can
 
1118      * also be set after the object is created, using MDB2::setOptions() or
 
1119      * MDB2_Driver_Common::setOption().
 
1120      * The list of available option includes:
 
1122      *  <li>$options['ssl'] -> boolean: determines if ssl should be used for connections</li>
 
1123      *  <li>$options['field_case'] -> CASE_LOWER|CASE_UPPER: determines what case to force on field/table names</li>
 
1124      *  <li>$options['disable_query'] -> boolean: determines if queries should be executed</li>
 
1125      *  <li>$options['result_class'] -> string: class used for result sets</li>
 
1126      *  <li>$options['buffered_result_class'] -> string: class used for buffered result sets</li>
 
1127      *  <li>$options['result_wrap_class'] -> string: class used to wrap result sets into</li>
 
1128      *  <li>$options['result_buffering'] -> boolean should results be buffered or not?</li>
 
1129      *  <li>$options['fetch_class'] -> string: class to use when fetch mode object is used</li>
 
1130      *  <li>$options['persistent'] -> boolean: persistent connection?</li>
 
1131      *  <li>$options['debug'] -> integer: numeric debug level</li>
 
1132      *  <li>$options['debug_handler'] -> string: function/method that captures debug messages</li>
 
1133      *  <li>$options['debug_expanded_output'] -> bool: BC option to determine if more context information should be send to the debug handler</li>
 
1134      *  <li>$options['default_text_field_length'] -> integer: default text field length to use</li>
 
1135      *  <li>$options['lob_buffer_length'] -> integer: LOB buffer length</li>
 
1136      *  <li>$options['log_line_break'] -> string: line-break format</li>
 
1137      *  <li>$options['idxname_format'] -> string: pattern for index name</li>
 
1138      *  <li>$options['seqname_format'] -> string: pattern for sequence name</li>
 
1139      *  <li>$options['savepoint_format'] -> string: pattern for auto generated savepoint names</li>
 
1140      *  <li>$options['statement_format'] -> string: pattern for prepared statement names</li>
 
1141      *  <li>$options['seqcol_name'] -> string: sequence column name</li>
 
1142      *  <li>$options['quote_identifier'] -> boolean: if identifier quoting should be done when check_option is used</li>
 
1143      *  <li>$options['use_transactions'] -> boolean: if transaction use should be enabled</li>
 
1144      *  <li>$options['decimal_places'] -> integer: number of decimal places to handle</li>
 
1145      *  <li>$options['portability'] -> integer: portability constant</li>
 
1146      *  <li>$options['modules'] -> array: short to long module name mapping for __call()</li>
 
1147      *  <li>$options['emulate_prepared'] -> boolean: force prepared statements to be emulated</li>
 
1148      *  <li>$options['datatype_map'] -> array: map user defined datatypes to other primitive datatypes</li>
 
1149      *  <li>$options['datatype_map_callback'] -> array: callback function/method that should be called</li>
 
1150      *  <li>$options['bindname_format'] -> string: regular expression pattern for named parameters</li>
 
1151      *  <li>$options['multi_query'] -> boolean: determines if queries returning multiple result sets should be executed</li>
 
1152      *  <li>$options['max_identifiers_length'] -> integer: max identifier length</li>
 
1153      *  <li>$options['default_fk_action_onupdate'] -> string: default FOREIGN KEY ON UPDATE action ['RESTRICT'|'NO ACTION'|'SET DEFAULT'|'SET NULL'|'CASCADE']</li>
 
1154      *  <li>$options['default_fk_action_ondelete'] -> string: default FOREIGN KEY ON DELETE action ['RESTRICT'|'NO ACTION'|'SET DEFAULT'|'SET NULL'|'CASCADE']</li>
 
1159      * @see     MDB2::connect()
 
1160      * @see     MDB2::factory()
 
1161      * @see     MDB2::singleton()
 
1162      * @see     MDB2_Driver_Common::setOption()
 
1164     public $options = array(
 
1166         'field_case' => CASE_LOWER,
 
1167         'disable_query' => false,
 
1168         'result_class' => 'MDB2_Result_%s',
 
1169         'buffered_result_class' => 'MDB2_BufferedResult_%s',
 
1170         'result_wrap_class' => false,
 
1171         'result_buffering' => true,
 
1172         'fetch_class' => 'stdClass',
 
1173         'persistent' => false,
 
1175         'debug_handler' => 'MDB2_defaultDebugOutput',
 
1176         'debug_expanded_output' => false,
 
1177         'default_text_field_length' => 4096,
 
1178         'lob_buffer_length' => 8192,
 
1179         'log_line_break' => "\n",
 
1180         'idxname_format' => '%s_idx',
 
1181         'seqname_format' => '%s_seq',
 
1182         'savepoint_format' => 'MDB2_SAVEPOINT_%s',
 
1183         'statement_format' => 'MDB2_STATEMENT_%1$s_%2$s',
 
1184         'seqcol_name' => 'sequence',
 
1185         'quote_identifier' => false,
 
1186         'use_transactions' => true,
 
1187         'decimal_places' => 2,
 
1188         'portability' => MDB2_PORTABILITY_ALL,
 
1197         'emulate_prepared' => false,
 
1198         'datatype_map' => array(),
 
1199         'datatype_map_callback' => array(),
 
1200         'nativetype_map_callback' => array(),
 
1201         'lob_allow_url_include' => false,
 
1202         'bindname_format' => '(?:\d+)|(?:[a-zA-Z][a-zA-Z0-9_]*)',
 
1203         'max_identifiers_length' => 30,
 
1204         'default_fk_action_onupdate' => 'RESTRICT',
 
1205         'default_fk_action_ondelete' => 'RESTRICT',
 
1213     public $string_quoting = array(
 
1217         'escape_pattern' => false,
 
1221      * identifier quoting
 
1225     public $identifier_quoting = array(
 
1236     public $sql_comments = array(
 
1237         array('start' => '--', 'end' => "\n", 'escape' => false),
 
1238         array('start' => '/*', 'end' => '*/', 'escape' => false),
 
1242      * comparision wildcards
 
1246     protected $wildcards = array('%', '_');
 
1249      * column alias keyword
 
1253     public $as_keyword = ' AS ';
 
1260     public $warnings = array();
 
1263      * string with the debugging information
 
1267     public $debug_output = '';
 
1270      * determine if there is an open transaction
 
1274     public $in_transaction = false;
 
1277      * the smart transaction nesting depth
 
1281     public $nested_transaction_counter = null;
 
1284      * the first error that occured inside a nested transaction
 
1285      * @var     MDB2_Error|bool
 
1288     protected $has_transaction_error = false;
 
1291      * result offset used in the next query
 
1298      * result limit used in the next query
 
1305      * Database backend used in PHP (mysql, odbc etc.)
 
1312      * Database used with regards to SQL syntax etc.
 
1319      * the last query sent to the driver
 
1326      * the default fetchmode used
 
1330     public $fetchmode = MDB2_FETCHMODE_ORDERED;
 
1333      * array of module instances
 
1337     protected $modules = array();
 
1340      * determines of the PHP4 destructor emulation has been enabled yet
 
1344     protected $destructor_registered = true;
 
1352     // {{{ constructor: function __construct()
 
1357     function __construct()
 
1359         end($GLOBALS['_MDB2_databases']);
 
1360         $db_index = key($GLOBALS['_MDB2_databases']) + 1;
 
1361         $GLOBALS['_MDB2_databases'][$db_index] = &$this;
 
1362         $this->db_index = $db_index;
 
1363         $this->pear = new PEAR;
 
1367     // {{{ destructor: function __destruct()
 
1372     function __destruct()
 
1374         $this->disconnect(false);
 
1378     // {{{ function free()
 
1381      * Free the internal references so that the instance can be destroyed
 
1383      * @return  bool    true on success, false if result is invalid
 
1389         unset($GLOBALS['_MDB2_databases'][$this->db_index]);
 
1390         unset($this->db_index);
 
1395     // {{{ function __toString()
 
1398      * String conversation
 
1400      * @return  string representation of the object
 
1404     function __toString()
 
1406         $info = get_class($this);
 
1407         $info.= ': (phptype = '.$this->phptype.', dbsyntax = '.$this->dbsyntax.')';
 
1408         if ($this->connection) {
 
1409             $info.= ' [connected]';
 
1415     // {{{ function errorInfo($error = null)
 
1418      * This method is used to collect information about an error
 
1420      * @param   mixed   error code or resource
 
1422      * @return  array   with MDB2 errorcode, native error code, native message
 
1426     function errorInfo($error = null)
 
1428         return array($error, null, null);
 
1432     // {{{ function &raiseError($code = null, $mode = null, $options = null, $userinfo = null)
 
1435      * This method is used to communicate an error and invoke error
 
1436      * callbacks etc.  Basically a wrapper for PEAR::raiseError
 
1437      * without the message string.
 
1439      * @param mixed  $code     integer error code, or a PEAR error object (all
 
1440      *                         other parameters are ignored if this parameter is
 
1442      * @param int    $mode     error mode, see PEAR_Error docs
 
1443      * @param mixed  $options  If error mode is PEAR_ERROR_TRIGGER, this is the
 
1444      *                         error level (E_USER_NOTICE etc). If error mode is
 
1445      *                         PEAR_ERROR_CALLBACK, this is the callback function,
 
1446      *                         either as a function name, or as an array of an
 
1447      *                         object and method name. For other error modes this
 
1448      *                         parameter is ignored.
 
1449      * @param string $userinfo Extra debug information. Defaults to the last
 
1450      *                         query and native error code.
 
1451      * @param string $method   name of the method that triggered the error
 
1452      * @param string $dummy1   not used
 
1453      * @param bool   $dummy2   not used
 
1455      * @return PEAR_Error instance of a PEAR Error object
 
1459     function &raiseError($code = null,
 
1467         $userinfo = "[Error message: $userinfo]\n";
 
1468         // The error is yet a MDB2 error object
 
1469         if (MDB2::isError($code)) {
 
1470             // because we use the static PEAR::raiseError, our global
 
1471             // handler should be used if it is set
 
1472             if ((null === $mode) && !empty($this->_default_error_mode)) {
 
1473                 $mode    = $this->_default_error_mode;
 
1474                 $options = $this->_default_error_options;
 
1476             if (null === $userinfo) {
 
1477                 $userinfo = $code->getUserinfo();
 
1479             $code = $code->getCode();
 
1480         } elseif ($code == MDB2_ERROR_NOT_FOUND) {
 
1481             // extension not loaded: don't call $this->errorInfo() or the script
 
1483         } elseif (isset($this->connection)) {
 
1484             if (!empty($this->last_query)) {
 
1485                 $userinfo.= "[Last executed query: {$this->last_query}]\n";
 
1487             $native_errno = $native_msg = null;
 
1488             list($code, $native_errno, $native_msg) = $this->errorInfo($code);
 
1489             if ((null !== $native_errno) && $native_errno !== '') {
 
1490                 $userinfo.= "[Native code: $native_errno]\n";
 
1492             if ((null !== $native_msg) && $native_msg !== '') {
 
1493                 $userinfo.= "[Native message: ". strip_tags($native_msg) ."]\n";
 
1495             if (null !== $method) {
 
1496                 $userinfo = $method.': '.$userinfo;
 
1500         $err = $this->pear->raiseError(null, $code, $mode, $options, $userinfo, 'MDB2_Error', true);
 
1501         if ($err->getMode() !== PEAR_ERROR_RETURN
 
1502             && isset($this->nested_transaction_counter) && !$this->has_transaction_error) {
 
1503             $this->has_transaction_error = $err;
 
1509     // {{{ function resetWarnings()
 
1512      * reset the warning array
 
1518     function resetWarnings()
 
1520         $this->warnings = array();
 
1524     // {{{ function getWarnings()
 
1527      * Get all warnings in reverse order.
 
1528      * This means that the last warning is the first element in the array
 
1530      * @return  array   with warnings
 
1533      * @see     resetWarnings()
 
1535     function getWarnings()
 
1537         return array_reverse($this->warnings);
 
1541     // {{{ function setFetchMode($fetchmode, $object_class = 'stdClass')
 
1544      * Sets which fetch mode should be used by default on queries
 
1545      * on this connection
 
1547      * @param   int     MDB2_FETCHMODE_ORDERED, MDB2_FETCHMODE_ASSOC
 
1548      *                               or MDB2_FETCHMODE_OBJECT
 
1549      * @param   string  the class name of the object to be returned
 
1550      *                               by the fetch methods when the
 
1551      *                               MDB2_FETCHMODE_OBJECT mode is selected.
 
1552      *                               If no class is specified by default a cast
 
1553      *                               to object from the assoc array row will be
 
1554      *                               done.  There is also the possibility to use
 
1555      *                               and extend the 'MDB2_row' class.
 
1557      * @return  mixed   MDB2_OK or MDB2 Error Object
 
1560      * @see     MDB2_FETCHMODE_ORDERED, MDB2_FETCHMODE_ASSOC, MDB2_FETCHMODE_OBJECT
 
1562     function setFetchMode($fetchmode, $object_class = 'stdClass')
 
1564         switch ($fetchmode) {
 
1565         case MDB2_FETCHMODE_OBJECT:
 
1566             $this->options['fetch_class'] = $object_class;
 
1567         case MDB2_FETCHMODE_ORDERED:
 
1568         case MDB2_FETCHMODE_ASSOC:
 
1569             $this->fetchmode = $fetchmode;
 
1572             return $this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
 
1573                 'invalid fetchmode mode', __FUNCTION__);
 
1580     // {{{ function setOption($option, $value)
 
1583      * set the option for the db class
 
1585      * @param   string  option name
 
1586      * @param   mixed   value for the option
 
1588      * @return  mixed   MDB2_OK or MDB2 Error Object
 
1592     function setOption($option, $value)
 
1594         if (array_key_exists($option, $this->options)) {
 
1595             $this->options[$option] = $value;
 
1598         return $this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
 
1599             "unknown option $option", __FUNCTION__);
 
1603     // {{{ function getOption($option)
 
1606      * Returns the value of an option
 
1608      * @param   string  option name
 
1610      * @return  mixed   the option value or error object
 
1614     function getOption($option)
 
1616         if (array_key_exists($option, $this->options)) {
 
1617             return $this->options[$option];
 
1619         return $this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
 
1620             "unknown option $option", __FUNCTION__);
 
1624     // {{{ function debug($message, $scope = '', $is_manip = null)
 
1627      * set a debug message
 
1629      * @param   string  message that should be appended to the debug variable
 
1630      * @param   string  usually the method name that triggered the debug call:
 
1631      *                  for example 'query', 'prepare', 'execute', 'parameters',
 
1632      *                  'beginTransaction', 'commit', 'rollback'
 
1633      * @param   array   contains context information about the debug() call
 
1634      *                  common keys are: is_manip, time, result etc.
 
1640     function debug($message, $scope = '', $context = array())
 
1642         if ($this->options['debug'] && $this->options['debug_handler']) {
 
1643             if (!$this->options['debug_expanded_output']) {
 
1644                 if (!empty($context['when']) && $context['when'] !== 'pre') {
 
1647                 $context = empty($context['is_manip']) ? false : $context['is_manip'];
 
1649             return call_user_func_array($this->options['debug_handler'], array(&$this, $scope, $message, $context));
 
1655     // {{{ function getDebugOutput()
 
1660      * @return  string  content of the debug_output class variable
 
1664     function getDebugOutput()
 
1666         return $this->debug_output;
 
1670     // {{{ function escape($text)
 
1673      * Quotes a string so it can be safely used in a query. It will quote
 
1674      * the text so it can safely be used within a query.
 
1676      * @param   string  the input string to quote
 
1677      * @param   bool    escape wildcards
 
1679      * @return  string  quoted string
 
1683     function escape($text, $escape_wildcards = false)
 
1685         if ($escape_wildcards) {
 
1686             $text = $this->escapePattern($text);
 
1689         $text = str_replace($this->string_quoting['end'], $this->string_quoting['escape'] . $this->string_quoting['end'], $text);
 
1694     // {{{ function escapePattern($text)
 
1697      * Quotes pattern (% and _) characters in a string)
 
1699      * @param   string  the input string to quote
 
1701      * @return  string  quoted string
 
1705     function escapePattern($text)
 
1707         if ($this->string_quoting['escape_pattern']) {
 
1708             $text = str_replace($this->string_quoting['escape_pattern'], $this->string_quoting['escape_pattern'] . $this->string_quoting['escape_pattern'], $text);
 
1709             foreach ($this->wildcards as $wildcard) {
 
1710                 $text = str_replace($wildcard, $this->string_quoting['escape_pattern'] . $wildcard, $text);
 
1717     // {{{ function quoteIdentifier($str, $check_option = false)
 
1720      * Quote a string so it can be safely used as a table or column name
 
1722      * Delimiting style depends on which database driver is being used.
 
1724      * NOTE: just because you CAN use delimited identifiers doesn't mean
 
1725      * you SHOULD use them.  In general, they end up causing way more
 
1726      * problems than they solve.
 
1728      * NOTE: if you have table names containing periods, don't use this method
 
1731      * Portability is broken by using the following characters inside
 
1732      * delimited identifiers:
 
1733      *   + backtick (<kbd>`</kbd>) -- due to MySQL
 
1734      *   + double quote (<kbd>"</kbd>) -- due to Oracle
 
1735      *   + brackets (<kbd>[</kbd> or <kbd>]</kbd>) -- due to Access
 
1737      * Delimited identifiers are known to generally work correctly under
 
1738      * the following drivers:
 
1746      * InterBase doesn't seem to be able to use delimited identifiers
 
1747      * via PHP 4.  They work fine under PHP 5.
 
1749      * @param   string  identifier name to be quoted
 
1750      * @param   bool    check the 'quote_identifier' option
 
1752      * @return  string  quoted identifier string
 
1756     function quoteIdentifier($str, $check_option = false)
 
1758         if ($check_option && !$this->options['quote_identifier']) {
 
1761         $str = str_replace($this->identifier_quoting['end'], $this->identifier_quoting['escape'] . $this->identifier_quoting['end'], $str);
 
1762         $parts = explode('.', $str);
 
1763         foreach (array_keys($parts) as $k) {
 
1764             $parts[$k] = $this->identifier_quoting['start'] . $parts[$k] . $this->identifier_quoting['end'];
 
1766         return implode('.', $parts);
 
1770     // {{{ function getAsKeyword()
 
1773      * Gets the string to alias column
 
1775      * @return string to use when aliasing a column
 
1777     function getAsKeyword()
 
1779         return $this->as_keyword;
 
1783     // {{{ function getConnection()
 
1786      * Returns a native connection
 
1788      * @return  mixed   a valid MDB2 connection object,
 
1789      *                  or a MDB2 error object on error
 
1793     function getConnection()
 
1795         $result = $this->connect();
 
1796         if (MDB2::isError($result)) {
 
1799         return $this->connection;
 
1803     // {{{ function _fixResultArrayValues(&$row, $mode)
 
1806      * Do all necessary conversions on result arrays to fix DBMS quirks
 
1808      * @param   array   the array to be fixed (passed by reference)
 
1809      * @param   array   bit-wise addition of the required portability modes
 
1815     function _fixResultArrayValues(&$row, $mode)
 
1818         case MDB2_PORTABILITY_EMPTY_TO_NULL:
 
1819             foreach ($row as $key => $value) {
 
1820                 if ($value === '') {
 
1825         case MDB2_PORTABILITY_RTRIM:
 
1826             foreach ($row as $key => $value) {
 
1827                 if (is_string($value)) {
 
1828                     $row[$key] = rtrim($value);
 
1832         case MDB2_PORTABILITY_FIX_ASSOC_FIELD_NAMES:
 
1834             foreach ($row as $key => $value) {
 
1835                 $tmp_row[preg_replace('/^(?:.*\.)?([^.]+)$/', '\\1', $key)] = $value;
 
1839         case (MDB2_PORTABILITY_RTRIM + MDB2_PORTABILITY_EMPTY_TO_NULL):
 
1840             foreach ($row as $key => $value) {
 
1841                 if ($value === '') {
 
1843                 } elseif (is_string($value)) {
 
1844                     $row[$key] = rtrim($value);
 
1848         case (MDB2_PORTABILITY_RTRIM + MDB2_PORTABILITY_FIX_ASSOC_FIELD_NAMES):
 
1850             foreach ($row as $key => $value) {
 
1851                 if (is_string($value)) {
 
1852                     $value = rtrim($value);
 
1854                 $tmp_row[preg_replace('/^(?:.*\.)?([^.]+)$/', '\\1', $key)] = $value;
 
1858         case (MDB2_PORTABILITY_EMPTY_TO_NULL + MDB2_PORTABILITY_FIX_ASSOC_FIELD_NAMES):
 
1860             foreach ($row as $key => $value) {
 
1861                 if ($value === '') {
 
1864                 $tmp_row[preg_replace('/^(?:.*\.)?([^.]+)$/', '\\1', $key)] = $value;
 
1868         case (MDB2_PORTABILITY_RTRIM + MDB2_PORTABILITY_EMPTY_TO_NULL + MDB2_PORTABILITY_FIX_ASSOC_FIELD_NAMES):
 
1870             foreach ($row as $key => $value) {
 
1871                 if ($value === '') {
 
1873                 } elseif (is_string($value)) {
 
1874                     $value = rtrim($value);
 
1876                 $tmp_row[preg_replace('/^(?:.*\.)?([^.]+)$/', '\\1', $key)] = $value;
 
1884     // {{{ function loadModule($module, $property = null, $phptype_specific = null)
 
1889      * @param   string  name of the module that should be loaded
 
1890      *                  (only used for error messages)
 
1891      * @param   string  name of the property into which the class will be loaded
 
1892      * @param   bool    if the class to load for the module is specific to the
 
1895      * @return  object  on success a reference to the given module is returned
 
1896      *                  and on failure a PEAR error
 
1900     function loadModule($module, $property = null, $phptype_specific = null)
 
1903             $property = strtolower($module);
 
1906         if (!isset($this->{$property})) {
 
1907             $version = $phptype_specific;
 
1908             if ($phptype_specific !== false) {
 
1910                 $class_name = 'MDB2_Driver_'.$module.'_'.$this->phptype;
 
1911                 $file_name = str_replace('_', DIRECTORY_SEPARATOR, $class_name).'.php';
 
1913             if ($phptype_specific === false
 
1914                 || (!MDB2::classExists($class_name) && !MDB2::fileExists($file_name))
 
1917                 $class_name = 'MDB2_'.$module;
 
1918                 $file_name = str_replace('_', DIRECTORY_SEPARATOR, $class_name).'.php';
 
1921             $err = MDB2::loadClass($class_name, $this->getOption('debug'));
 
1922             if (MDB2::isError($err)) {
 
1926             // load module in a specific version
 
1928                 if (method_exists($class_name, 'getClassName')) {
 
1929                     $class_name_new = call_user_func(array($class_name, 'getClassName'), $this->db_index);
 
1930                     if ($class_name != $class_name_new) {
 
1931                         $class_name = $class_name_new;
 
1932                         $err = MDB2::loadClass($class_name, $this->getOption('debug'));
 
1933                         if (MDB2::isError($err)) {
 
1940             if (!MDB2::classExists($class_name)) {
 
1941                 $err = $this->raiseError(MDB2_ERROR_LOADMODULE, null, null,
 
1942                     "unable to load module '$module' into property '$property'", __FUNCTION__);
 
1945             $this->{$property} = new $class_name($this->db_index);
 
1946             $this->modules[$module] = $this->{$property};
 
1948                 // this will be used in the connect method to determine if the module
 
1949                 // needs to be loaded with a different version if the server
 
1950                 // version changed in between connects
 
1951                 $this->loaded_version_modules[] = $property;
 
1955         return $this->{$property};
 
1959     // {{{ function __call($method, $params)
 
1962      * Calls a module method using the __call magic method
 
1964      * @param   string  Method name.
 
1965      * @param   array   Arguments.
 
1967      * @return  mixed   Returned value.
 
1969     function __call($method, $params)
 
1972         if (preg_match('/^([a-z]+)([A-Z])(.*)$/', $method, $match)
 
1973             && isset($this->options['modules'][$match[1]])
 
1975             $module = $this->options['modules'][$match[1]];
 
1976             $method = strtolower($match[2]).$match[3];
 
1977             if (!isset($this->modules[$module]) || !is_object($this->modules[$module])) {
 
1978                 $result = $this->loadModule($module);
 
1979                 if (MDB2::isError($result)) {
 
1984             foreach ($this->modules as $key => $foo) {
 
1985                 if (is_object($this->modules[$key])
 
1986                     && method_exists($this->modules[$key], $method)
 
1993         if (null !== $module) {
 
1994             return call_user_func_array(array(&$this->modules[$module], $method), $params);
 
1997         $class = get_class($this);
 
1998         $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
 
1999         $loc = 'in ' . $trace[0]['file'] . ' on line ' . $trace[0]['line'];
 
2000         if ($method == 'isError') {
 
2001             trigger_error("Deprecated: $class::isError() is deprecated, use MDB2::isError() $loc", E_USER_DEPRECATED);
 
2002             if (!array_key_exists(0, $params)) {
 
2003                 trigger_error("Missing argument 1 for $class::$method, called $loc", E_USER_ERROR);
 
2005             return MDB2::isError($params[0]);
 
2007         trigger_error("Call to undefined function: $class::$method() $loc.", E_USER_ERROR);
 
2011     // {{{ function __callStatic($method, $params)
 
2014      * Calls a module method using the __callStatic magic method
 
2016      * @param   string  Method name.
 
2017      * @param   array   Arguments.
 
2019      * @return  mixed   Returned value.
 
2021     public static function __callStatic($method, $params)
 
2023         $class = get_called_class();
 
2024         $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
 
2025         $loc = 'in ' . $trace[0]['file'] . ' on line ' . $trace[0]['line'];
 
2026         if ($method == 'isError') {
 
2027             trigger_error("Deprecated: $class::isError() is deprecated, use MDB2::isError() $loc", E_USER_DEPRECATED);
 
2028             if (!array_key_exists(0, $params)) {
 
2029                 trigger_error("Missing argument 1 for $class::$method, called $loc", E_USER_ERROR);
 
2031             return MDB2::isError($params[0]);
 
2033         trigger_error("Call to undefined function: $class::$method() $loc.", E_USER_ERROR);
 
2037     // {{{ function beginTransaction($savepoint = null)
 
2040      * Start a transaction or set a savepoint.
 
2042      * @param   string  name of a savepoint to set
 
2043      * @return  mixed   MDB2_OK on success, a MDB2 error on failure
 
2047     function beginTransaction($savepoint = null)
 
2049         $this->debug('Starting transaction', __FUNCTION__, array('is_manip' => true, 'savepoint' => $savepoint));
 
2050         return $this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
 
2051             'transactions are not supported', __FUNCTION__);
 
2055     // {{{ function commit($savepoint = null)
 
2058      * Commit the database changes done during a transaction that is in
 
2059      * progress or release a savepoint. This function may only be called when
 
2060      * auto-committing is disabled, otherwise it will fail. Therefore, a new
 
2061      * transaction is implicitly started after committing the pending changes.
 
2063      * @param   string  name of a savepoint to release
 
2064      * @return  mixed   MDB2_OK on success, a MDB2 error on failure
 
2068     function commit($savepoint = null)
 
2070         $this->debug('Committing transaction/savepoint', __FUNCTION__, array('is_manip' => true, 'savepoint' => $savepoint));
 
2071         return $this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
 
2072             'commiting transactions is not supported', __FUNCTION__);
 
2076     // {{{ function rollback($savepoint = null)
 
2079      * Cancel any database changes done during a transaction or since a specific
 
2080      * savepoint that is in progress. This function may only be called when
 
2081      * auto-committing is disabled, otherwise it will fail. Therefore, a new
 
2082      * transaction is implicitly started after canceling the pending changes.
 
2084      * @param   string  name of a savepoint to rollback to
 
2085      * @return  mixed   MDB2_OK on success, a MDB2 error on failure
 
2089     function rollback($savepoint = null)
 
2091         $this->debug('Rolling back transaction/savepoint', __FUNCTION__, array('is_manip' => true, 'savepoint' => $savepoint));
 
2092         return $this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
 
2093             'rolling back transactions is not supported', __FUNCTION__);
 
2097     // {{{ function inTransaction($ignore_nested = false)
 
2100      * If a transaction is currently open.
 
2102      * @param   bool    if the nested transaction count should be ignored
 
2103      * @return  int|bool    - an integer with the nesting depth is returned if a
 
2104      *                      nested transaction is open
 
2105      *                      - true is returned for a normal open transaction
 
2106      *                      - false is returned if no transaction is open
 
2110     function inTransaction($ignore_nested = false)
 
2112         if (!$ignore_nested && isset($this->nested_transaction_counter)) {
 
2113             return $this->nested_transaction_counter;
 
2115         return $this->in_transaction;
 
2119     // {{{ function setTransactionIsolation($isolation)
 
2122      * Set the transacton isolation level.
 
2124      * @param   string  standard isolation level
 
2125      *                  READ UNCOMMITTED (allows dirty reads)
 
2126      *                  READ COMMITTED (prevents dirty reads)
 
2127      *                  REPEATABLE READ (prevents nonrepeatable reads)
 
2128      *                  SERIALIZABLE (prevents phantom reads)
 
2129      * @param   array some transaction options:
 
2130      *                  'wait' => 'WAIT' | 'NO WAIT'
 
2131      *                  'rw'   => 'READ WRITE' | 'READ ONLY'
 
2132      * @return  mixed   MDB2_OK on success, a MDB2 error on failure
 
2137     function setTransactionIsolation($isolation, $options = array())
 
2139         $this->debug('Setting transaction isolation level', __FUNCTION__, array('is_manip' => true));
 
2140         return $this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
 
2141             'isolation level setting is not supported', __FUNCTION__);
 
2145     // {{{ function beginNestedTransaction($savepoint = false)
 
2148      * Start a nested transaction.
 
2150      * @return  mixed   MDB2_OK on success/savepoint name, a MDB2 error on failure
 
2155     function beginNestedTransaction()
 
2157         if ($this->in_transaction) {
 
2158             ++$this->nested_transaction_counter;
 
2159             $savepoint = sprintf($this->options['savepoint_format'], $this->nested_transaction_counter);
 
2160             if ($this->supports('savepoints') && $savepoint) {
 
2161                 return $this->beginTransaction($savepoint);
 
2165         $this->has_transaction_error = false;
 
2166         $result = $this->beginTransaction();
 
2167         $this->nested_transaction_counter = 1;
 
2172     // {{{ function completeNestedTransaction($force_rollback = false, $release = false)
 
2175      * Finish a nested transaction by rolling back if an error occured or
 
2176      * committing otherwise.
 
2178      * @param   bool    if the transaction should be rolled back regardless
 
2179      *                  even if no error was set within the nested transaction
 
2180      * @return  mixed   MDB_OK on commit/counter decrementing, false on rollback
 
2181      *                  and a MDB2 error on failure
 
2186     function completeNestedTransaction($force_rollback = false)
 
2188         if ($this->nested_transaction_counter > 1) {
 
2189             $savepoint = sprintf($this->options['savepoint_format'], $this->nested_transaction_counter);
 
2190             if ($this->supports('savepoints') && $savepoint) {
 
2191                 if ($force_rollback || $this->has_transaction_error) {
 
2192                     $result = $this->rollback($savepoint);
 
2193                     if (!MDB2::isError($result)) {
 
2195                         $this->has_transaction_error = false;
 
2198                     $result = $this->commit($savepoint);
 
2203             --$this->nested_transaction_counter;
 
2207         $this->nested_transaction_counter = null;
 
2210         // transaction has not yet been rolled back
 
2211         if ($this->in_transaction) {
 
2212             if ($force_rollback || $this->has_transaction_error) {
 
2213                 $result = $this->rollback();
 
2214                 if (!MDB2::isError($result)) {
 
2218                 $result = $this->commit();
 
2221         $this->has_transaction_error = false;
 
2226     // {{{ function failNestedTransaction($error = null, $immediately = false)
 
2229      * Force setting nested transaction to failed.
 
2231      * @param   mixed   value to return in getNestededTransactionError()
 
2232      * @param   bool    if the transaction should be rolled back immediately
 
2233      * @return  bool    MDB2_OK
 
2238     function failNestedTransaction($error = null, $immediately = false)
 
2240         if (null !== $error) {
 
2241             $error = $this->has_transaction_error ? $this->has_transaction_error : true;
 
2242         } elseif (!$error) {
 
2245         $this->has_transaction_error = $error;
 
2246         if (!$immediately) {
 
2249         return $this->rollback();
 
2253     // {{{ function getNestedTransactionError()
 
2256      * The first error that occured since the transaction start.
 
2258      * @return  MDB2_Error|bool     MDB2 error object if an error occured or false.
 
2263     function getNestedTransactionError()
 
2265         return $this->has_transaction_error;
 
2272      * Connect to the database
 
2274      * @return true on success, MDB2 Error Object on failure
 
2278         return $this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
 
2279             'method not implemented', __FUNCTION__);
 
2283     // {{{ databaseExists()
 
2286      * check if given database name is exists?
 
2288      * @param string $name    name of the database that should be checked
 
2290      * @return mixed true/false on success, a MDB2 error on failure
 
2293     function databaseExists($name)
 
2295         return $this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
 
2296             'method not implemented', __FUNCTION__);
 
2300     // {{{ setCharset($charset, $connection = null)
 
2303      * Set the charset on the current connection
 
2305      * @param string    charset
 
2306      * @param resource  connection handle
 
2308      * @return true on success, MDB2 Error Object on failure
 
2310     function setCharset($charset, $connection = null)
 
2312         return $this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
 
2313             'method not implemented', __FUNCTION__);
 
2317     // {{{ function disconnect($force = true)
 
2320      * Log out and disconnect from the database.
 
2322      * @param boolean $force whether the disconnect should be forced even if the
 
2323      *                       connection is opened persistently
 
2325      * @return mixed true on success, false if not connected and error object on error
 
2329     function disconnect($force = true)
 
2331         $this->connection = 0;
 
2332         $this->connected_dsn = array();
 
2333         $this->connected_database_name = '';
 
2334         $this->opened_persistent = null;
 
2335         $this->connected_server_info = '';
 
2336         $this->in_transaction = null;
 
2337         $this->nested_transaction_counter = null;
 
2342     // {{{ function setDatabase($name)
 
2345      * Select a different database
 
2347      * @param   string  name of the database that should be selected
 
2349      * @return  string  name of the database previously connected to
 
2353     function setDatabase($name)
 
2355         $previous_database_name = (isset($this->database_name)) ? $this->database_name : '';
 
2356         $this->database_name = $name;
 
2357         if (!empty($this->connected_database_name) && ($this->connected_database_name != $this->database_name)) {
 
2358             $this->disconnect(false);
 
2360         return $previous_database_name;
 
2364     // {{{ function getDatabase()
 
2367      * Get the current database
 
2369      * @return  string  name of the database
 
2373     function getDatabase()
 
2375         return $this->database_name;
 
2379     // {{{ function setDSN($dsn)
 
2384      * @param   mixed   DSN string or array
 
2390     function setDSN($dsn)
 
2392         $dsn_default = $GLOBALS['_MDB2_dsninfo_default'];
 
2393         $dsn = MDB2::parseDSN($dsn);
 
2394         if (array_key_exists('database', $dsn)) {
 
2395             $this->database_name = $dsn['database'];
 
2396             unset($dsn['database']);
 
2398         $this->dsn = array_merge($dsn_default, $dsn);
 
2399         return $this->disconnect(false);
 
2403     // {{{ function getDSN($type = 'string', $hidepw = false)
 
2406      * return the DSN as a string
 
2408      * @param   string  format to return ("array", "string")
 
2409      * @param   string  string to hide the password with
 
2411      * @return  mixed   DSN in the chosen type
 
2415     function getDSN($type = 'string', $hidepw = false)
 
2417         $dsn = array_merge($GLOBALS['_MDB2_dsninfo_default'], $this->dsn);
 
2418         $dsn['phptype'] = $this->phptype;
 
2419         $dsn['database'] = $this->database_name;
 
2421             $dsn['password'] = $hidepw;
 
2424         // expand to include all possible options
 
2426            $dsn = $dsn['phptype'].
 
2427                ($dsn['dbsyntax'] ? ('('.$dsn['dbsyntax'].')') : '').
 
2428                '://'.$dsn['username'].':'.
 
2429                 $dsn['password'].'@'.$dsn['hostspec'].
 
2430                 ($dsn['port'] ? (':'.$dsn['port']) : '').
 
2431                 '/'.$dsn['database'];
 
2441     // {{{ _isNewLinkSet()
 
2444      * Check if the 'new_link' option is set
 
2450     function _isNewLinkSet()
 
2452         return (isset($this->dsn['new_link'])
 
2453             && ($this->dsn['new_link'] === true
 
2454              || (is_string($this->dsn['new_link']) && preg_match('/^true$/i', $this->dsn['new_link']))
 
2455              || (is_numeric($this->dsn['new_link']) && 0 != (int)$this->dsn['new_link'])
 
2461     // {{{ function &standaloneQuery($query, $types = null, $is_manip = false)
 
2464      * execute a query as database administrator
 
2466      * @param   string  the SQL query
 
2467      * @param   mixed   array that contains the types of the columns in
 
2469      * @param   bool    if the query is a manipulation query
 
2471      * @return  mixed   MDB2_OK on success, a MDB2 error on failure
 
2475     function standaloneQuery($query, $types = null, $is_manip = false)
 
2477         $offset = $this->offset;
 
2478         $limit = $this->limit;
 
2479         $this->offset = $this->limit = 0;
 
2480         $query = $this->_modifyQuery($query, $is_manip, $limit, $offset);
 
2482         $connection = $this->getConnection();
 
2483         if (MDB2::isError($connection)) {
 
2487         $result = $this->_doQuery($query, $is_manip, $connection, false);
 
2488         if (MDB2::isError($result)) {
 
2493             $affected_rows =  $this->_affectedRows($connection, $result);
 
2494             return $affected_rows;
 
2496         $result = $this->_wrapResult($result, $types, true, true, $limit, $offset);
 
2501     // {{{ function _modifyQuery($query, $is_manip, $limit, $offset)
 
2504      * Changes a query string for various DBMS specific reasons
 
2506      * @param   string  query to modify
 
2507      * @param   bool    if it is a DML query
 
2508      * @param   int  limit the number of rows
 
2509      * @param   int  start reading from given offset
 
2511      * @return  string  modified query
 
2515     function _modifyQuery($query, $is_manip, $limit, $offset)
 
2521     // {{{ function &_doQuery($query, $is_manip = false, $connection = null, $database_name = null)
 
2525      * @param   string  query
 
2526      * @param   bool    if the query is a manipulation query
 
2527      * @param   resource connection handle
 
2528      * @param   string  database name
 
2530      * @return  result or error object
 
2534     function _doQuery($query, $is_manip = false, $connection = null, $database_name = null)
 
2536         $this->last_query = $query;
 
2537         $result = $this->debug($query, 'query', array('is_manip' => $is_manip, 'when' => 'pre'));
 
2539             if (MDB2::isError($result)) {
 
2544         $err = MDB2_Driver_Common::raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
 
2545             'method not implemented', __FUNCTION__);
 
2550     // {{{ function _affectedRows($connection, $result = null)
 
2553      * Returns the number of rows affected
 
2555      * @param   resource result handle
 
2556      * @param   resource connection handle
 
2558      * @return  mixed   MDB2 Error Object or the number of rows affected
 
2562     function _affectedRows($connection, $result = null)
 
2564         return MDB2_Driver_Common::raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
 
2565             'method not implemented', __FUNCTION__);
 
2569     // {{{ function &exec($query)
 
2572      * Execute a manipulation query to the database and return the number of affected rows
 
2574      * @param   string  the SQL query
 
2576      * @return  mixed   number of affected rows on success, a MDB2 error on failure
 
2580     function exec($query)
 
2582         $offset = $this->offset;
 
2583         $limit = $this->limit;
 
2584         $this->offset = $this->limit = 0;
 
2585         $query = $this->_modifyQuery($query, true, $limit, $offset);
 
2587         $connection = $this->getConnection();
 
2588         if (MDB2::isError($connection)) {
 
2592         $result = $this->_doQuery($query, true, $connection, $this->database_name);
 
2593         if (MDB2::isError($result)) {
 
2597         $affectedRows = $this->_affectedRows($connection, $result);
 
2598         return $affectedRows;
 
2602     // {{{ function &query($query, $types = null, $result_class = true, $result_wrap_class = false)
 
2605      * Send a query to the database and return any results
 
2607      * @param   string  the SQL query
 
2608      * @param   mixed   array that contains the types of the columns in
 
2610      * @param   mixed   string which specifies which result class to use
 
2611      * @param   mixed   string which specifies which class to wrap results in
 
2613      * @return mixed   an MDB2_Result handle on success, a MDB2 error on failure
 
2617     function query($query, $types = null, $result_class = true, $result_wrap_class = true)
 
2619         $offset = $this->offset;
 
2620         $limit = $this->limit;
 
2621         $this->offset = $this->limit = 0;
 
2622         $query = $this->_modifyQuery($query, false, $limit, $offset);
 
2624         $connection = $this->getConnection();
 
2625         if (MDB2::isError($connection)) {
 
2629         $result = $this->_doQuery($query, false, $connection, $this->database_name);
 
2630         if (MDB2::isError($result)) {
 
2634         $result = $this->_wrapResult($result, $types, $result_class, $result_wrap_class, $limit, $offset);
 
2639     // {{{ function _wrapResult($result_resource, $types = array(), $result_class = true, $result_wrap_class = false, $limit = null, $offset = null)
 
2642      * wrap a result set into the correct class
 
2644      * @param   resource result handle
 
2645      * @param   mixed   array that contains the types of the columns in
 
2647      * @param   mixed   string which specifies which result class to use
 
2648      * @param   mixed   string which specifies which class to wrap results in
 
2649      * @param   string  number of rows to select
 
2650      * @param   string  first row to select
 
2652      * @return mixed   an MDB2_Result, a MDB2 error on failure
 
2656     function _wrapResult($result_resource, $types = array(), $result_class = true,
 
2657         $result_wrap_class = true, $limit = null, $offset = null)
 
2659         if ($types === true) {
 
2660             if ($this->supports('result_introspection')) {
 
2661                 $this->loadModule('Reverse', null, true);
 
2662                 $tableInfo = $this->reverse->tableInfo($result_resource);
 
2663                 if (MDB2::isError($tableInfo)) {
 
2667                 $types_assoc = array();
 
2668                 foreach ($tableInfo as $field) {
 
2669                     $types[] = $field['mdb2type'];
 
2670                     $types_assoc[$field['name']] = $field['mdb2type'];
 
2677         if ($result_class === true) {
 
2678             $result_class = $this->options['result_buffering']
 
2679                 ? $this->options['buffered_result_class'] : $this->options['result_class'];
 
2682         if ($result_class) {
 
2683             $class_name = sprintf($result_class, $this->phptype);
 
2684             if (!MDB2::classExists($class_name)) {
 
2685                 $err = MDB2_Driver_Common::raiseError(MDB2_ERROR_NOT_FOUND, null, null,
 
2686                     'result class does not exist '.$class_name, __FUNCTION__);
 
2689             $result = new $class_name($this, $result_resource, $limit, $offset);
 
2690             if (!MDB2::isResultCommon($result)) {
 
2691                 $err = MDB2_Driver_Common::raiseError(MDB2_ERROR_NOT_FOUND, null, null,
 
2692                     'result class is not extended from MDB2_Result_Common', __FUNCTION__);
 
2696             if (!empty($types)) {
 
2697                 $err = $result->setResultTypes($types);
 
2698                 if (MDB2::isError($err)) {
 
2703             if (!empty($types_assoc)) {
 
2704                 $err = $result->setResultTypes($types_assoc);
 
2705                 if (MDB2::isError($err)) {
 
2711             if ($result_wrap_class === true) {
 
2712                 $result_wrap_class = $this->options['result_wrap_class'];
 
2714             if ($result_wrap_class) {
 
2715                 if (!MDB2::classExists($result_wrap_class)) {
 
2716                     $err = MDB2_Driver_Common::raiseError(MDB2_ERROR_NOT_FOUND, null, null,
 
2717                         'result wrap class does not exist '.$result_wrap_class, __FUNCTION__);
 
2720                 $result = new $result_wrap_class($result, $this->fetchmode);
 
2726         return $result_resource;
 
2730     // {{{ function getServerVersion($native = false)
 
2733      * return version information about the server
 
2735      * @param   bool    determines if the raw version string should be returned
 
2737      * @return  mixed   array with version information or row string
 
2741     function getServerVersion($native = false)
 
2743         return MDB2_Driver_Common::raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
 
2744             'method not implemented', __FUNCTION__);
 
2748     // {{{ function setLimit($limit, $offset = null)
 
2751      * set the range of the next query
 
2753      * @param   string  number of rows to select
 
2754      * @param   string  first row to select
 
2756      * @return  mixed   MDB2_OK on success, a MDB2 error on failure
 
2760     function setLimit($limit, $offset = null)
 
2762         if (!$this->supports('limit_queries')) {
 
2763             return MDB2_Driver_Common::raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
 
2764                 'limit is not supported by this driver', __FUNCTION__);
 
2766         $limit = (int)$limit;
 
2768             return MDB2_Driver_Common::raiseError(MDB2_ERROR_SYNTAX, null, null,
 
2769                 'it was not specified a valid selected range row limit', __FUNCTION__);
 
2771         $this->limit = $limit;
 
2772         if (null !== $offset) {
 
2773             $offset = (int)$offset;
 
2775                 return MDB2_Driver_Common::raiseError(MDB2_ERROR_SYNTAX, null, null,
 
2776                     'it was not specified a valid first selected range row', __FUNCTION__);
 
2778             $this->offset = $offset;
 
2784     // {{{ function subSelect($query, $type = false)
 
2787      * simple subselect emulation: leaves the query untouched for all RDBMS
 
2788      * that support subselects
 
2790      * @param   string  the SQL query for the subselect that may only
 
2792      * @param   string  determines type of the field
 
2794      * @return  string  the query
 
2798     function subSelect($query, $type = false)
 
2800         if ($this->supports('sub_selects') === true) {
 
2804         if (!$this->supports('sub_selects')) {
 
2805             return MDB2_Driver_Common::raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
 
2806                 'method not implemented', __FUNCTION__);
 
2809         $col = $this->queryCol($query, $type);
 
2810         if (MDB2::isError($col)) {
 
2813         if (!is_array($col) || count($col) == 0) {
 
2817             $this->loadModule('Datatype', null, true);
 
2818             return $this->datatype->implodeArray($col, $type);
 
2820         return implode(', ', $col);
 
2824     // {{{ function replace($table, $fields)
 
2827      * Execute a SQL REPLACE query. A REPLACE query is identical to a INSERT
 
2828      * query, except that if there is already a row in the table with the same
 
2829      * key field values, the old row is deleted before the new row is inserted.
 
2831      * The REPLACE type of query does not make part of the SQL standards. Since
 
2832      * practically only MySQL and SQLite implement it natively, this type of
 
2833      * query isemulated through this method for other DBMS using standard types
 
2834      * of queries inside a transaction to assure the atomicity of the operation.
 
2836      * @param   string  name of the table on which the REPLACE query will
 
2838      * @param   array   associative array   that describes the fields and the
 
2839      *       values that will be inserted or updated in the specified table. The
 
2840      *       indexes of the array are the names of all the fields of the table.
 
2841      *       The values of the array are also associative arrays that describe
 
2842      *       the values and other properties of the table fields.
 
2844      *       Here follows a list of field properties that need to be specified:
 
2847      *           Value to be assigned to the specified field. This value may be
 
2848      *           of specified in database independent type format as this
 
2849      *           function can perform the necessary datatype conversions.
 
2851      *           Default: this property is required unless the Null property is
 
2855      *           Name of the type of the field. Currently, all types MDB2
 
2856      *           are supported except for clob and blob.
 
2858      *           Default: no type conversion
 
2861      *           bool    property that indicates that the value for this field
 
2862      *           should be set to null.
 
2864      *           The default value for fields missing in INSERT queries may be
 
2865      *           specified the definition of a table. Often, the default value
 
2866      *           is already null, but since the REPLACE may be emulated using
 
2867      *           an UPDATE query, make sure that all fields of the table are
 
2868      *           listed in this function argument array.
 
2873      *           bool    property that indicates that this field should be
 
2874      *           handled as a primary key or at least as part of the compound
 
2875      *           unique index of the table that will determine the row that will
 
2876      *           updated if it exists or inserted a new row otherwise.
 
2878      *           This function will fail if no key field is specified or if the
 
2879      *           value of a key field is set to null because fields that are
 
2880      *           part of unique index they may not be null.
 
2884      * @return  mixed   MDB2_OK on success, a MDB2 error on failure
 
2888     function replace($table, $fields)
 
2890         if (!$this->supports('replace')) {
 
2891             return MDB2_Driver_Common::raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
 
2892                 'replace query is not supported', __FUNCTION__);
 
2894         $count = count($fields);
 
2895         $condition = $values = array();
 
2896         for ($colnum = 0, reset($fields); $colnum < $count; next($fields), $colnum++) {
 
2897             $name = key($fields);
 
2898             if (isset($fields[$name]['null']) && $fields[$name]['null']) {
 
2901                 $type = isset($fields[$name]['type']) ? $fields[$name]['type'] : null;
 
2902                 $value = $this->quote($fields[$name]['value'], $type);
 
2904             $values[$name] = $value;
 
2905             if (isset($fields[$name]['key']) && $fields[$name]['key']) {
 
2906                 if ($value === 'NULL') {
 
2907                     return MDB2_Driver_Common::raiseError(MDB2_ERROR_CANNOT_REPLACE, null, null,
 
2908                         'key value '.$name.' may not be NULL', __FUNCTION__);
 
2910                 $condition[] = $this->quoteIdentifier($name, true) . '=' . $value;
 
2913         if (empty($condition)) {
 
2914             return MDB2_Driver_Common::raiseError(MDB2_ERROR_CANNOT_REPLACE, null, null,
 
2915                 'not specified which fields are keys', __FUNCTION__);
 
2919         $in_transaction = $this->in_transaction;
 
2920         if (!$in_transaction && MDB2::isError($result = $this->beginTransaction())) {
 
2924         $connection = $this->getConnection();
 
2925         if (MDB2::isError($connection)) {
 
2929         $condition = ' WHERE '.implode(' AND ', $condition);
 
2930         $query = 'DELETE FROM ' . $this->quoteIdentifier($table, true) . $condition;
 
2931         $result = $this->_doQuery($query, true, $connection);
 
2932         if (!MDB2::isError($result)) {
 
2933             $affected_rows = $this->_affectedRows($connection, $result);
 
2935             foreach ($values as $key => $value) {
 
2936                 $insert .= ($insert?', ':'') . $this->quoteIdentifier($key, true);
 
2938             $values = implode(', ', $values);
 
2939             $query = 'INSERT INTO '. $this->quoteIdentifier($table, true) . "($insert) VALUES ($values)";
 
2940             $result = $this->_doQuery($query, true, $connection);
 
2941             if (!MDB2::isError($result)) {
 
2942                 $affected_rows += $this->_affectedRows($connection, $result);;
 
2946         if (!$in_transaction) {
 
2947             if (MDB2::isError($result)) {
 
2950                 $result = $this->commit();
 
2954         if (MDB2::isError($result)) {
 
2958         return $affected_rows;
 
2962     // {{{ function &prepare($query, $types = null, $result_types = null, $lobs = array())
 
2965      * Prepares a query for multiple execution with execute().
 
2966      * With some database backends, this is emulated.
 
2967      * prepare() requires a generic query as string like
 
2968      * 'INSERT INTO numbers VALUES(?,?)' or
 
2969      * 'INSERT INTO numbers VALUES(:foo,:bar)'.
 
2970      * The ? and :name and are placeholders which can be set using
 
2971      * bindParam() and the query can be sent off using the execute() method.
 
2972      * The allowed format for :name can be set with the 'bindname_format' option.
 
2974      * @param   string  the query to prepare
 
2975      * @param   mixed   array that contains the types of the placeholders
 
2976      * @param   mixed   array that contains the types of the columns in
 
2977      *                        the result set or MDB2_PREPARE_RESULT, if set to
 
2978      *                        MDB2_PREPARE_MANIP the query is handled as a manipulation query
 
2979      * @param   mixed   key (field) value (parameter) pair for all lob placeholders
 
2981      * @return  mixed   resource handle for the prepared query on success,
 
2982      *                  a MDB2 error on failure
 
2985      * @see     bindParam, execute
 
2987     function prepare($query, $types = null, $result_types = null, $lobs = array())
 
2989         $is_manip = ($result_types === MDB2_PREPARE_MANIP);
 
2990         $offset = $this->offset;
 
2991         $limit = $this->limit;
 
2992         $this->offset = $this->limit = 0;
 
2993         $result = $this->debug($query, __FUNCTION__, array('is_manip' => $is_manip, 'when' => 'pre'));
 
2995             if (MDB2::isError($result)) {
 
3000         $placeholder_type_guess = $placeholder_type = null;
 
3003         $positions = array();
 
3005         while ($position < strlen($query)) {
 
3006             $q_position = strpos($query, $question, $position);
 
3007             $c_position = strpos($query, $colon, $position);
 
3008             if ($q_position && $c_position) {
 
3009                 $p_position = min($q_position, $c_position);
 
3010             } elseif ($q_position) {
 
3011                 $p_position = $q_position;
 
3012             } elseif ($c_position) {
 
3013                 $p_position = $c_position;
 
3017             if (null === $placeholder_type) {
 
3018                 $placeholder_type_guess = $query[$p_position];
 
3021             $new_pos = $this->_skipDelimitedStrings($query, $position, $p_position);
 
3022             if (MDB2::isError($new_pos)) {
 
3025             if ($new_pos != $position) {
 
3026                 $position = $new_pos;
 
3027                 continue; //evaluate again starting from the new position
 
3030             if ($query[$position] == $placeholder_type_guess) {
 
3031                 if (null === $placeholder_type) {
 
3032                     $placeholder_type = $query[$p_position];
 
3033                     $question = $colon = $placeholder_type;
 
3034                     if (!empty($types) && is_array($types)) {
 
3035                         if ($placeholder_type == ':') {
 
3036                             if (is_int(key($types))) {
 
3037                                 $types_tmp = $types;
 
3042                             $types = array_values($types);
 
3046                 if ($placeholder_type == ':') {
 
3047                     $regexp = '/^.{'.($position+1).'}('.$this->options['bindname_format'].').*$/s';
 
3048                     $parameter = preg_replace($regexp, '\\1', $query);
 
3049                     if ($parameter === '') {
 
3050                         $err = MDB2_Driver_Common::raiseError(MDB2_ERROR_SYNTAX, null, null,
 
3051                             'named parameter name must match "bindname_format" option', __FUNCTION__);
 
3054                     $positions[$p_position] = $parameter;
 
3055                     $query = substr_replace($query, '?', $position, strlen($parameter)+1);
 
3056                     // use parameter name in type array
 
3057                     if (isset($count) && isset($types_tmp[++$count])) {
 
3058                         $types[$parameter] = $types_tmp[$count];
 
3061                     $positions[$p_position] = count($positions);
 
3063                 $position = $p_position + 1;
 
3065                 $position = $p_position;
 
3068         $class_name = 'MDB2_Statement_'.$this->phptype;
 
3070         $obj = new $class_name($this, $statement, $positions, $query, $types, $result_types, $is_manip, $limit, $offset);
 
3071         $this->debug($query, __FUNCTION__, array('is_manip' => $is_manip, 'when' => 'post', 'result' => $obj));
 
3076     // {{{ function _skipDelimitedStrings($query, $position, $p_position)
 
3079      * Utility method, used by prepare() to avoid replacing placeholders within delimited strings.
 
3080      * Check if the placeholder is contained within a delimited string.
 
3081      * If so, skip it and advance the position, otherwise return the current position,
 
3084      * @param string $query
 
3085      * @param integer $position current string cursor position
 
3086      * @param integer $p_position placeholder position
 
3088      * @return mixed integer $new_position on success
 
3089      *               MDB2_Error on failure
 
3093     function _skipDelimitedStrings($query, $position, $p_position)
 
3096         $ignores[] = $this->string_quoting;
 
3097         $ignores[] = $this->identifier_quoting;
 
3098         $ignores = array_merge($ignores, $this->sql_comments);
 
3100         foreach ($ignores as $ignore) {
 
3101             if (!empty($ignore['start'])) {
 
3102                 if (is_int($start_quote = strpos($query, $ignore['start'], $position)) && $start_quote < $p_position) {
 
3103                     $end_quote = $start_quote;
 
3105                         if (!is_int($end_quote = strpos($query, $ignore['end'], $end_quote + 1))) {
 
3106                             if ($ignore['end'] === "\n") {
 
3107                                 $end_quote = strlen($query) - 1;
 
3109                                 $err = MDB2_Driver_Common::raiseError(MDB2_ERROR_SYNTAX, null, null,
 
3110                                     'query with an unterminated text string specified', __FUNCTION__);
 
3114                     } while ($ignore['escape']
 
3115                         && $end_quote-1 != $start_quote
 
3116                         && $query[($end_quote - 1)] == $ignore['escape']
 
3117                         && (   $ignore['escape_pattern'] !== $ignore['escape']
 
3118                             || $query[($end_quote - 2)] != $ignore['escape'])
 
3121                     $position = $end_quote + 1;
 
3130     // {{{ function quote($value, $type = null, $quote = true)
 
3133      * Convert a text value into a DBMS specific format that is suitable to
 
3134      * compose query statements.
 
3136      * @param   string  text string value that is intended to be converted.
 
3137      * @param   string  type to which the value should be converted to
 
3139      * @param   bool    escape wildcards
 
3141      * @return  string  text string that represents the given argument value in
 
3142      *       a DBMS specific format.
 
3146     function quote($value, $type = null, $quote = true, $escape_wildcards = false)
 
3148         $result = $this->loadModule('Datatype', null, true);
 
3149         if (MDB2::isError($result)) {
 
3153         return $this->datatype->quote($value, $type, $quote, $escape_wildcards);
 
3157     // {{{ function getDeclaration($type, $name, $field)
 
3160      * Obtain DBMS specific SQL code portion needed to declare
 
3163      * @param   string  type to which the value should be converted to
 
3164      * @param   string  name the field to be declared.
 
3165      * @param   string  definition of the field
 
3167      * @return  string  DBMS specific SQL code portion that should be used to
 
3168      *                 declare the specified field.
 
3172     function getDeclaration($type, $name, $field)
 
3174         $result = $this->loadModule('Datatype', null, true);
 
3175         if (MDB2::isError($result)) {
 
3178         return $this->datatype->getDeclaration($type, $name, $field);
 
3182     // {{{ function compareDefinition($current, $previous)
 
3185      * Obtain an array of changes that may need to applied
 
3187      * @param   array   new definition
 
3188      * @param   array   old definition
 
3190      * @return  array   containing all changes that will need to be applied
 
3194     function compareDefinition($current, $previous)
 
3196         $result = $this->loadModule('Datatype', null, true);
 
3197         if (MDB2::isError($result)) {
 
3200         return $this->datatype->compareDefinition($current, $previous);
 
3204     // {{{ function supports($feature)
 
3207      * Tell whether a DB implementation or its backend extension
 
3208      * supports a given feature.
 
3210      * @param   string  name of the feature (see the MDB2 class doc)
 
3212      * @return  bool|string if this DB implementation supports a given feature
 
3213      *                      false means no, true means native,
 
3214      *                      'emulated' means emulated
 
3218     function supports($feature)
 
3220         if (array_key_exists($feature, $this->supported)) {
 
3221             return $this->supported[$feature];
 
3223         return MDB2_Driver_Common::raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
 
3224             "unknown support feature $feature", __FUNCTION__);
 
3228     // {{{ function getSequenceName($sqn)
 
3231      * adds sequence name formatting to a sequence name
 
3233      * @param   string  name of the sequence
 
3235      * @return  string  formatted sequence name
 
3239     function getSequenceName($sqn)
 
3241         return sprintf($this->options['seqname_format'],
 
3242             preg_replace('/[^a-z0-9_\-\$.]/i', '_', $sqn));
 
3246     // {{{ function getIndexName($idx)
 
3249      * adds index name formatting to a index name
 
3251      * @param   string  name of the index
 
3253      * @return  string  formatted index name
 
3257     function getIndexName($idx)
 
3259         return sprintf($this->options['idxname_format'],
 
3260             preg_replace('/[^a-z0-9_\-\$.]/i', '_', $idx));
 
3264     // {{{ function nextID($seq_name, $ondemand = true)
 
3267      * Returns the next free id of a sequence
 
3269      * @param   string  name of the sequence
 
3270      * @param   bool    when true missing sequences are automatic created
 
3272      * @return  mixed   MDB2 Error Object or id
 
3276     function nextID($seq_name, $ondemand = true)
 
3278         return MDB2_Driver_Common::raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
 
3279             'method not implemented', __FUNCTION__);
 
3283     // {{{ function lastInsertID($table = null, $field = null)
 
3286      * Returns the autoincrement ID if supported or $id or fetches the current
 
3287      * ID in a sequence called: $table.(empty($field) ? '' : '_'.$field)
 
3289      * @param   string  name of the table into which a new row was inserted
 
3290      * @param   string  name of the field into which a new row was inserted
 
3292      * @return  mixed   MDB2 Error Object or id
 
3296     function lastInsertID($table = null, $field = null)
 
3298         return MDB2_Driver_Common::raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
 
3299             'method not implemented', __FUNCTION__);
 
3303     // {{{ function currID($seq_name)
 
3306      * Returns the current id of a sequence
 
3308      * @param   string  name of the sequence
 
3310      * @return  mixed   MDB2 Error Object or id
 
3314     function currID($seq_name)
 
3316         $this->warnings[] = 'database does not support getting current
 
3317             sequence value, the sequence value was incremented';
 
3318         return $this->nextID($seq_name);
 
3322     // {{{ function queryOne($query, $type = null, $colnum = 0)
 
3325      * Execute the specified query, fetch the value from the first column of
 
3326      * the first row of the result set and then frees
 
3329      * @param string $query  the SELECT query statement to be executed.
 
3330      * @param string $type   optional argument that specifies the expected
 
3331      *                       datatype of the result set field, so that an eventual
 
3332      *                       conversion may be performed. The default datatype is
 
3333      *                       text, meaning that no conversion is performed
 
3334      * @param mixed  $colnum the column number (or name) to fetch
 
3336      * @return  mixed   MDB2_OK or field value on success, a MDB2 error on failure
 
3340     function queryOne($query, $type = null, $colnum = 0)
 
3342         $result = $this->query($query, $type);
 
3343         if (!MDB2::isResultCommon($result)) {
 
3347         $one = $result->fetchOne($colnum);
 
3353     // {{{ function queryRow($query, $types = null, $fetchmode = MDB2_FETCHMODE_DEFAULT)
 
3356      * Execute the specified query, fetch the values from the first
 
3357      * row of the result set into an array and then frees
 
3360      * @param   string  the SELECT query statement to be executed.
 
3361      * @param   array   optional array argument that specifies a list of
 
3362      *       expected datatypes of the result set columns, so that the eventual
 
3363      *       conversions may be performed. The default list of datatypes is
 
3364      *       empty, meaning that no conversion is performed.
 
3365      * @param   int     how the array data should be indexed
 
3367      * @return  mixed   MDB2_OK or data array on success, a MDB2 error on failure
 
3371     function queryRow($query, $types = null, $fetchmode = MDB2_FETCHMODE_DEFAULT)
 
3373         $result = $this->query($query, $types);
 
3374         if (!MDB2::isResultCommon($result)) {
 
3378         $row = $result->fetchRow($fetchmode);
 
3384     // {{{ function queryCol($query, $type = null, $colnum = 0)
 
3387      * Execute the specified query, fetch the value from the first column of
 
3388      * each row of the result set into an array and then frees the result set.
 
3390      * @param string $query  the SELECT query statement to be executed.
 
3391      * @param string $type   optional argument that specifies the expected
 
3392      *                       datatype of the result set field, so that an eventual
 
3393      *                       conversion may be performed. The default datatype is text,
 
3394      *                       meaning that no conversion is performed
 
3395      * @param mixed  $colnum the column number (or name) to fetch
 
3397      * @return  mixed   MDB2_OK or data array on success, a MDB2 error on failure
 
3400     function queryCol($query, $type = null, $colnum = 0)
 
3402         $result = $this->query($query, $type);
 
3403         if (!MDB2::isResultCommon($result)) {
 
3407         $col = $result->fetchCol($colnum);
 
3413     // {{{ function queryAll($query, $types = null, $fetchmode = MDB2_FETCHMODE_DEFAULT, $rekey = false, $force_array = false, $group = false)
 
3416      * Execute the specified query, fetch all the rows of the result set into
 
3417      * a two dimensional array and then frees the result set.
 
3419      * @param   string  the SELECT query statement to be executed.
 
3420      * @param   array   optional array argument that specifies a list of
 
3421      *       expected datatypes of the result set columns, so that the eventual
 
3422      *       conversions may be performed. The default list of datatypes is
 
3423      *       empty, meaning that no conversion is performed.
 
3424      * @param   int     how the array data should be indexed
 
3425      * @param   bool    if set to true, the $all will have the first
 
3426      *       column as its first dimension
 
3427      * @param   bool    used only when the query returns exactly
 
3428      *       two columns. If true, the values of the returned array will be
 
3429      *       one-element arrays instead of scalars.
 
3430      * @param   bool    if true, the values of the returned array is
 
3431      *       wrapped in another array.  If the same key value (in the first
 
3432      *       column) repeats itself, the values will be appended to this array
 
3433      *       instead of overwriting the existing values.
 
3435      * @return  mixed   MDB2_OK or data array on success, a MDB2 error on failure
 
3439     function queryAll($query, $types = null, $fetchmode = MDB2_FETCHMODE_DEFAULT,
 
3440         $rekey = false, $force_array = false, $group = false)
 
3442         $result = $this->query($query, $types);
 
3443         if (!MDB2::isResultCommon($result)) {
 
3447         $all = $result->fetchAll($fetchmode, $rekey, $force_array, $group);
 
3453     // {{{ function delExpect($error_code)
 
3456      * This method deletes all occurences of the specified element from
 
3457      * the expected error codes stack.
 
3459      * @param  mixed $error_code error code that should be deleted
 
3460      * @return mixed list of error codes that were deleted or error
 
3462      * @uses PEAR::delExpect()
 
3464     public function delExpect($error_code)
 
3466         return $this->pear->delExpect($error_code);
 
3470     // {{{ function expectError($code)
 
3473      * This method is used to tell which errors you expect to get.
 
3474      * Expected errors are always returned with error mode
 
3475      * PEAR_ERROR_RETURN.  Expected error codes are stored in a stack,
 
3476      * and this method pushes a new element onto it.  The list of
 
3477      * expected errors are in effect until they are popped off the
 
3478      * stack with the popExpect() method.
 
3480      * Note that this method can not be called statically
 
3482      * @param mixed $code a single error code or an array of error codes to expect
 
3484      * @return int     the new depth of the "expected errors" stack
 
3486      * @uses PEAR::expectError()
 
3488     public function expectError($code = '*')
 
3490         return $this->pear->expectError($code);
 
3494     // {{{ function getStaticProperty($class, $var)
 
3497      * If you have a class that's mostly/entirely static, and you need static
 
3498      * properties, you can use this method to simulate them. Eg. in your method(s)
 
3499      * do this: $myVar = &PEAR::getStaticProperty('myclass', 'myVar');
 
3500      * You MUST use a reference, or they will not persist!
 
3502      * @param  string $class  The calling classname, to prevent clashes
 
3503      * @param  string $var    The variable to retrieve.
 
3504      * @return mixed   A reference to the variable. If not set it will be
 
3505      *                 auto initialised to NULL.
 
3507      * @uses PEAR::getStaticProperty()
 
3509     public function &getStaticProperty($class, $var)
 
3511         $tmp =& $this->pear->getStaticProperty($class, $var);
 
3516     // {{{ function popErrorHandling()
 
3519      * Pop the last error handler used
 
3521      * @return bool Always true
 
3523      * @see PEAR::pushErrorHandling
 
3524      * @uses PEAR::popErrorHandling()
 
3526     public function popErrorHandling()
 
3528         return $this->pear->popErrorHandling();
 
3532     // {{{ function popExpect()
 
3535      * This method pops one element off the expected error codes
 
3538      * @return array   the list of error codes that were popped
 
3540      * @uses PEAR::popExpect()
 
3542     public function popExpect()
 
3544         return $this->pear->popExpect();
 
3548     // {{{ function pushErrorHandling($mode, $options = null)
 
3551      * Push a new error handler on top of the error handler options stack. With this
 
3552      * you can easily override the actual error handler for some code and restore
 
3553      * it later with popErrorHandling.
 
3555      * @param mixed $mode (same as setErrorHandling)
 
3556      * @param mixed $options (same as setErrorHandling)
 
3558      * @return bool Always true
 
3560      * @see PEAR::setErrorHandling
 
3561      * @uses PEAR::pushErrorHandling()
 
3563     public function pushErrorHandling($mode, $options = null)
 
3565         return $this->pear->pushErrorHandling($mode, $options);
 
3569     // {{{ function registerShutdownFunc($func, $args = array())
 
3572      * Use this function to register a shutdown method for static
 
3575      * @param  mixed $func  The function name (or array of class/method) to call
 
3576      * @param  mixed $args  The arguments to pass to the function
 
3579      * @uses PEAR::registerShutdownFunc()
 
3581     public function registerShutdownFunc($func, $args = array())
 
3583         return $this->pear->registerShutdownFunc($func, $args);
 
3587     // {{{ function setErrorHandling($mode = null, $options = null)
 
3590      * Sets how errors generated by this object should be handled.
 
3591      * Can be invoked both in objects and statically.  If called
 
3592      * statically, setErrorHandling sets the default behaviour for all
 
3593      * PEAR objects.  If called in an object, setErrorHandling sets
 
3594      * the default behaviour for that object.
 
3597      *        One of PEAR_ERROR_RETURN, PEAR_ERROR_PRINT,
 
3598      *        PEAR_ERROR_TRIGGER, PEAR_ERROR_DIE,
 
3599      *        PEAR_ERROR_CALLBACK or PEAR_ERROR_EXCEPTION.
 
3601      * @param mixed $options
 
3602      *        When $mode is PEAR_ERROR_TRIGGER, this is the error level (one
 
3603      *        of E_USER_NOTICE, E_USER_WARNING or E_USER_ERROR).
 
3605      *        When $mode is PEAR_ERROR_CALLBACK, this parameter is expected
 
3606      *        to be the callback function or method.  A callback
 
3607      *        function is a string with the name of the function, a
 
3608      *        callback method is an array of two elements: the element
 
3609      *        at index 0 is the object, and the element at index 1 is
 
3610      *        the name of the method to call in the object.
 
3612      *        When $mode is PEAR_ERROR_PRINT or PEAR_ERROR_DIE, this is
 
3613      *        a printf format string used when printing the error
 
3618      * @see PEAR_ERROR_RETURN
 
3619      * @see PEAR_ERROR_PRINT
 
3620      * @see PEAR_ERROR_TRIGGER
 
3621      * @see PEAR_ERROR_DIE
 
3622      * @see PEAR_ERROR_CALLBACK
 
3623      * @see PEAR_ERROR_EXCEPTION
 
3626      * @uses PEAR::setErrorHandling($mode, $options)
 
3628     public function setErrorHandling($mode = null, $options = null)
 
3630         return $this->pear->setErrorHandling($mode, $options);
 
3634      * @uses PEAR::staticPopErrorHandling() 
 
3636     public function staticPopErrorHandling()
 
3638         return $this->pear->staticPopErrorHandling();
 
3642     // {{{ function staticPushErrorHandling($mode, $options = null)
 
3645      * @uses PEAR::staticPushErrorHandling($mode, $options)
 
3647     public function staticPushErrorHandling($mode, $options = null)
 
3649         return $this->pear->staticPushErrorHandling($mode, $options);
 
3653     // {{{ function &throwError($message = null, $code = null, $userinfo = null)
 
3656      * Simpler form of raiseError with fewer options.  In most cases
 
3657      * message, code and userinfo are enough.
 
3659      * @param mixed $message a text error message or a PEAR error object
 
3661      * @param int $code      a numeric error code (it is up to your class
 
3662      *                  to define these if you want to use codes)
 
3664      * @param string $userinfo If you need to pass along for example debug
 
3665      *                  information, this parameter is meant for that.
 
3667      * @return object   a PEAR error object
 
3668      * @see PEAR::raiseError
 
3669      * @uses PEAR::&throwError()
 
3671     public function &throwError($message = null, $code = null, $userinfo = null)
 
3673         $tmp =& $this->pear->throwError($message, $code, $userinfo);
 
3681 // {{{ class MDB2_Result
 
3684  * The dummy class that all user space result classes should extend from
 
3687  * @category    Database
 
3688  * @author      Lukas Smith <smith@pooteeweet.org>
 
3695 // {{{ class MDB2_Result_Common extends MDB2_Result
 
3698  * The common result class for MDB2 result objects
 
3701  * @category    Database
 
3702  * @author      Lukas Smith <smith@pooteeweet.org>
 
3704 class MDB2_Result_Common extends MDB2_Result
 
3706     // {{{ Variables (Properties)
 
3710     public $rownum = -1;
 
3711     public $types = array();
 
3712     public $types_assoc = array();
 
3713     public $values = array();
 
3715     public $offset_count = 0;
 
3717     public $column_names;
 
3720     // {{{ constructor: function __construct($db, &$result, $limit = 0, $offset = 0)
 
3725     function __construct($db, &$result, $limit = 0, $offset = 0)
 
3728         $this->result = $result;
 
3729         $this->offset = $offset;
 
3730         $this->limit = max(0, $limit - 1);
 
3734     // {{{ function setResultTypes($types)
 
3737      * Define the list of types to be associated with the columns of a given
 
3740      * This function may be called before invoking fetchRow(), fetchOne(),
 
3741      * fetchCol() and fetchAll() so that the necessary data type
 
3742      * conversions are performed on the data to be retrieved by them. If this
 
3743      * function is not called, the type of all result set columns is assumed
 
3744      * to be text, thus leading to not perform any conversions.
 
3746      * @param   array   variable that lists the
 
3747      *       data types to be expected in the result set columns. If this array
 
3748      *       contains less types than the number of columns that are returned
 
3749      *       in the result set, the remaining columns are assumed to be of the
 
3750      *       type text. Currently, the types clob and blob are not fully
 
3753      * @return  mixed   MDB2_OK on success, a MDB2 error on failure
 
3757     function setResultTypes($types)
 
3759         $load = $this->db->loadModule('Datatype', null, true);
 
3760         if (MDB2::isError($load)) {
 
3763         $types = $this->db->datatype->checkResultTypes($types);
 
3764         if (MDB2::isError($types)) {
 
3767         foreach ($types as $key => $value) {
 
3768             if (is_numeric($key)) {
 
3769                 $this->types[$key] = $value;
 
3771                 $this->types_assoc[$key] = $value;
 
3778     // {{{ function seek($rownum = 0)
 
3781      * Seek to a specific row in a result set
 
3783      * @param   int     number of the row where the data can be found
 
3785      * @return  mixed   MDB2_OK on success, a MDB2 error on failure
 
3789     function seek($rownum = 0)
 
3791         $target_rownum = $rownum - 1;
 
3792         if ($this->rownum > $target_rownum) {
 
3793             return MDB2::raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
 
3794                 'seeking to previous rows not implemented', __FUNCTION__);
 
3796         while ($this->rownum < $target_rownum) {
 
3803     // {{{ function &fetchRow($fetchmode = MDB2_FETCHMODE_DEFAULT, $rownum = null)
 
3806      * Fetch and return a row of data
 
3808      * @param   int     how the array data should be indexed
 
3809      * @param   int     number of the row where the data can be found
 
3811      * @return  int     data array on success, a MDB2 error on failure
 
3815     function fetchRow($fetchmode = MDB2_FETCHMODE_DEFAULT, $rownum = null)
 
3817         $err = MDB2::raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
 
3818             'method not implemented', __FUNCTION__);
 
3823     // {{{ function fetchOne($colnum = 0)
 
3826      * fetch single column from the next row from a result set
 
3828      * @param int|string the column number (or name) to fetch
 
3829      * @param int        number of the row where the data can be found
 
3831      * @return string data on success, a MDB2 error on failure
 
3834     function fetchOne($colnum = 0, $rownum = null)
 
3836         $fetchmode = is_numeric($colnum) ? MDB2_FETCHMODE_ORDERED : MDB2_FETCHMODE_ASSOC;
 
3837         $row = $this->fetchRow($fetchmode, $rownum);
 
3838         if (!is_array($row) || MDB2::isError($row)) {
 
3841         if (!array_key_exists($colnum, $row)) {
 
3842             return MDB2::raiseError(MDB2_ERROR_TRUNCATED, null, null,
 
3843                 'column is not defined in the result set: '.$colnum, __FUNCTION__);
 
3845         return $row[$colnum];
 
3849     // {{{ function fetchCol($colnum = 0)
 
3852      * Fetch and return a column from the current row pointer position
 
3854      * @param int|string the column number (or name) to fetch
 
3856      * @return  mixed data array on success, a MDB2 error on failure
 
3859     function fetchCol($colnum = 0)
 
3862         $fetchmode = is_numeric($colnum) ? MDB2_FETCHMODE_ORDERED : MDB2_FETCHMODE_ASSOC;
 
3863         $row = $this->fetchRow($fetchmode);
 
3864         if (is_array($row)) {
 
3865             if (!array_key_exists($colnum, $row)) {
 
3866                 return MDB2::raiseError(MDB2_ERROR_TRUNCATED, null, null,
 
3867                     'column is not defined in the result set: '.$colnum, __FUNCTION__);
 
3870                 $column[] = $row[$colnum];
 
3871             } while (is_array($row = $this->fetchRow($fetchmode)));
 
3873         if (MDB2::isError($row)) {
 
3880     // {{{ function fetchAll($fetchmode = MDB2_FETCHMODE_DEFAULT, $rekey = false, $force_array = false, $group = false)
 
3883      * Fetch and return all rows from the current row pointer position
 
3885      * @param   int     $fetchmode  the fetch mode to use:
 
3886      *                            + MDB2_FETCHMODE_ORDERED
 
3887      *                            + MDB2_FETCHMODE_ASSOC
 
3888      *                            + MDB2_FETCHMODE_ORDERED | MDB2_FETCHMODE_FLIPPED
 
3889      *                            + MDB2_FETCHMODE_ASSOC | MDB2_FETCHMODE_FLIPPED
 
3890      * @param   bool    if set to true, the $all will have the first
 
3891      *       column as its first dimension
 
3892      * @param   bool    used only when the query returns exactly
 
3893      *       two columns. If true, the values of the returned array will be
 
3894      *       one-element arrays instead of scalars.
 
3895      * @param   bool    if true, the values of the returned array is
 
3896      *       wrapped in another array.  If the same key value (in the first
 
3897      *       column) repeats itself, the values will be appended to this array
 
3898      *       instead of overwriting the existing values.
 
3900      * @return  mixed   data array on success, a MDB2 error on failure
 
3905     function fetchAll($fetchmode = MDB2_FETCHMODE_DEFAULT, $rekey = false,
 
3906         $force_array = false, $group = false)
 
3909         $row = $this->fetchRow($fetchmode);
 
3910         if (MDB2::isError($row)) {
 
3916         $shift_array = $rekey ? false : null;
 
3917         if (null !== $shift_array) {
 
3918             if (is_object($row)) {
 
3919                 $colnum = count(get_object_vars($row));
 
3921                 $colnum = count($row);
 
3924                 return MDB2::raiseError(MDB2_ERROR_TRUNCATED, null, null,
 
3925                     'rekey feature requires atleast 2 column', __FUNCTION__);
 
3927             $shift_array = (!$force_array && $colnum == 2);
 
3932                 if (is_object($row)) {
 
3933                     $arr = get_object_vars($row);
 
3935                     unset($row->{$key});
 
3937                     if (   $fetchmode == MDB2_FETCHMODE_ASSOC
 
3938                         || $fetchmode == MDB2_FETCHMODE_OBJECT
 
3941                         unset($row[key($row)]);
 
3943                         $key = array_shift($row);
 
3946                         $row = array_shift($row);
 
3950                     $all[$key][] = $row;
 
3954             } while (($row = $this->fetchRow($fetchmode)));
 
3955         } elseif ($fetchmode == MDB2_FETCHMODE_FLIPPED) {
 
3957                 foreach ($row as $key => $val) {
 
3958                     $all[$key][] = $val;
 
3960             } while (($row = $this->fetchRow($fetchmode)));
 
3964             } while (($row = $this->fetchRow($fetchmode)));
 
3971     // {{{ function rowCount()
 
3973      * Returns the actual row number that was last fetched (count from 0)
 
3980         return $this->rownum + 1;
 
3984     // {{{ function numRows()
 
3987      * Returns the number of rows in a result object
 
3989      * @return  mixed   MDB2 Error Object or the number of rows
 
3995         return MDB2::raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
 
3996             'method not implemented', __FUNCTION__);
 
4000     // {{{ function nextResult()
 
4003      * Move the internal result pointer to the next available result
 
4005      * @return  true on success, false if there is no more result set or an error object on failure
 
4009     function nextResult()
 
4011         return MDB2::raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
 
4012             'method not implemented', __FUNCTION__);
 
4016     // {{{ function getColumnNames()
 
4019      * Retrieve the names of columns returned by the DBMS in a query result or
 
4022      * @param   bool    If set to true the values are the column names,
 
4023      *                  otherwise the names of the columns are the keys.
 
4024      * @return  mixed   Array variable that holds the names of columns or an
 
4025      *                  MDB2 error on failure.
 
4026      *                  Some DBMS may not return any columns when the result set
 
4027      *                  does not contain any rows.
 
4031     function getColumnNames($flip = false)
 
4033         if (!isset($this->column_names)) {
 
4034             $result = $this->_getColumnNames();
 
4035             if (MDB2::isError($result)) {
 
4038             $this->column_names = $result;
 
4041             return array_flip($this->column_names);
 
4043         return $this->column_names;
 
4047     // {{{ function _getColumnNames()
 
4050      * Retrieve the names of columns returned by the DBMS in a query result.
 
4052      * @return  mixed   Array variable that holds the names of columns as keys
 
4053      *                  or an MDB2 error on failure.
 
4054      *                  Some DBMS may not return any columns when the result set
 
4055      *                  does not contain any rows.
 
4059     function _getColumnNames()
 
4061         return MDB2::raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
 
4062             'method not implemented', __FUNCTION__);
 
4066     // {{{ function numCols()
 
4069      * Count the number of columns returned by the DBMS in a query result.
 
4071      * @return  mixed   integer value with the number of columns, a MDB2 error
 
4078         return MDB2::raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
 
4079             'method not implemented', __FUNCTION__);
 
4083     // {{{ function getResource()
 
4086      * return the resource associated with the result object
 
4092     function getResource()
 
4094         return $this->result;
 
4098     // {{{ function bindColumn($column, &$value, $type = null)
 
4101      * Set bind variable to a column.
 
4103      * @param   int     column number or name
 
4104      * @param   mixed   variable reference
 
4105      * @param   string  specifies the type of the field
 
4107      * @return  mixed   MDB2_OK on success, a MDB2 error on failure
 
4111     function bindColumn($column, &$value, $type = null)
 
4113         if (!is_numeric($column)) {
 
4114             $column_names = $this->getColumnNames();
 
4115             if ($this->db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
 
4116                 if ($this->db->options['field_case'] == CASE_LOWER) {
 
4117                     $column = strtolower($column);
 
4119                     $column = strtoupper($column);
 
4122             $column = $column_names[$column];
 
4124         $this->values[$column] =& $value;
 
4125         if (null !== $type) {
 
4126             $this->types[$column] = $type;
 
4132     // {{{ function _assignBindColumns($row)
 
4135      * Bind a variable to a value in the result row.
 
4137      * @param   array   row data
 
4139      * @return  mixed   MDB2_OK on success, a MDB2 error on failure
 
4143     function _assignBindColumns($row)
 
4145         $row = array_values($row);
 
4146         foreach ($row as $column => $value) {
 
4147             if (array_key_exists($column, $this->values)) {
 
4148                 $this->values[$column] = $value;
 
4155     // {{{ function free()
 
4158      * Free the internal resources associated with result.
 
4160      * @return  bool    true on success, false if result is invalid
 
4166         $this->result = false;
 
4174 // {{{ class MDB2_Row
 
4177  * The simple class that accepts row data as an array
 
4180  * @category    Database
 
4181  * @author      Lukas Smith <smith@pooteeweet.org>
 
4185     // {{{ constructor: function __construct(&$row)
 
4190      * @param   resource    row data as array
 
4192     function __construct(&$row)
 
4194         foreach ($row as $key => $value) {
 
4195             $this->$key = &$row[$key];
 
4203 // {{{ class MDB2_Statement_Common
 
4206  * The common statement class for MDB2 statement objects
 
4209  * @category    Database
 
4210  * @author      Lukas Smith <smith@pooteeweet.org>
 
4212 class MDB2_Statement_Common
 
4214     // {{{ Variables (Properties)
 
4221     var $values = array();
 
4227     // {{{ constructor: function __construct($db, $statement, $positions, $query, $types, $result_types, $is_manip = false, $limit = null, $offset = null)
 
4232     function __construct($db, $statement, $positions, $query, $types, $result_types, $is_manip = false, $limit = null, $offset = null)
 
4235         $this->statement = $statement;
 
4236         $this->positions = $positions;
 
4237         $this->query = $query;
 
4238         $this->types = (array)$types;
 
4239         $this->result_types = (array)$result_types;
 
4240         $this->limit = $limit;
 
4241         $this->is_manip = $is_manip;
 
4242         $this->offset = $offset;
 
4246     // {{{ function bindValue($parameter, &$value, $type = null)
 
4249      * Set the value of a parameter of a prepared query.
 
4251      * @param   int     the order number of the parameter in the query
 
4252      *       statement. The order number of the first parameter is 1.
 
4253      * @param   mixed   value that is meant to be assigned to specified
 
4254      *       parameter. The type of the value depends on the $type argument.
 
4255      * @param   string  specifies the type of the field
 
4257      * @return  mixed   MDB2_OK on success, a MDB2 error on failure
 
4261     function bindValue($parameter, $value, $type = null)
 
4263         if (!is_numeric($parameter)) {
 
4264             if (strpos($parameter, ':') === 0) {
 
4265                 $parameter = substr($parameter, 1);
 
4268         if (!in_array($parameter, $this->positions)) {
 
4269             return MDB2::raiseError(MDB2_ERROR_NOT_FOUND, null, null,
 
4270                 'Unable to bind to missing placeholder: '.$parameter, __FUNCTION__);
 
4272         $this->values[$parameter] = $value;
 
4273         if (null !== $type) {
 
4274             $this->types[$parameter] = $type;
 
4280     // {{{ function bindValueArray($values, $types = null)
 
4283      * Set the values of multiple a parameter of a prepared query in bulk.
 
4285      * @param   array   specifies all necessary information
 
4286      *       for bindValue() the array elements must use keys corresponding to
 
4287      *       the number of the position of the parameter.
 
4288      * @param   array   specifies the types of the fields
 
4290      * @return  mixed   MDB2_OK on success, a MDB2 error on failure
 
4295     function bindValueArray($values, $types = null)
 
4297         $types = is_array($types) ? array_values($types) : array_fill(0, count($values), null);
 
4298         $parameters = array_keys($values);
 
4299         $this->db->pushErrorHandling(PEAR_ERROR_RETURN);
 
4300         $this->db->expectError(MDB2_ERROR_NOT_FOUND);
 
4301         foreach ($parameters as $key => $parameter) {
 
4302             $err = $this->bindValue($parameter, $values[$parameter], $types[$key]);
 
4303             if (MDB2::isError($err)) {
 
4304                 if ($err->getCode() == MDB2_ERROR_NOT_FOUND) {
 
4305                     //ignore (extra value for missing placeholder)
 
4308                 $this->db->popExpect();
 
4309                 $this->db->popErrorHandling();
 
4313         $this->db->popExpect();
 
4314         $this->db->popErrorHandling();
 
4319     // {{{ function bindParam($parameter, &$value, $type = null)
 
4322      * Bind a variable to a parameter of a prepared query.
 
4324      * @param   int     the order number of the parameter in the query
 
4325      *       statement. The order number of the first parameter is 1.
 
4326      * @param   mixed   variable that is meant to be bound to specified
 
4327      *       parameter. The type of the value depends on the $type argument.
 
4328      * @param   string  specifies the type of the field
 
4330      * @return  mixed   MDB2_OK on success, a MDB2 error on failure
 
4334     function bindParam($parameter, &$value, $type = null)
 
4336         if (!is_numeric($parameter)) {
 
4337             if (strpos($parameter, ':') === 0) {
 
4338                 $parameter = substr($parameter, 1);
 
4341         if (!in_array($parameter, $this->positions)) {
 
4342             return MDB2::raiseError(MDB2_ERROR_NOT_FOUND, null, null,
 
4343                 'Unable to bind to missing placeholder: '.$parameter, __FUNCTION__);
 
4345         $this->values[$parameter] =& $value;
 
4346         if (null !== $type) {
 
4347             $this->types[$parameter] = $type;
 
4353     // {{{ function bindParamArray(&$values, $types = null)
 
4356      * Bind the variables of multiple a parameter of a prepared query in bulk.
 
4358      * @param   array   specifies all necessary information
 
4359      *       for bindParam() the array elements must use keys corresponding to
 
4360      *       the number of the position of the parameter.
 
4361      * @param   array   specifies the types of the fields
 
4363      * @return  mixed   MDB2_OK on success, a MDB2 error on failure
 
4368     function bindParamArray(&$values, $types = null)
 
4370         $types = is_array($types) ? array_values($types) : array_fill(0, count($values), null);
 
4371         $parameters = array_keys($values);
 
4372         foreach ($parameters as $key => $parameter) {
 
4373             $err = $this->bindParam($parameter, $values[$parameter], $types[$key]);
 
4374             if (MDB2::isError($err)) {
 
4382     // {{{ function &execute($values = null, $result_class = true, $result_wrap_class = false)
 
4385      * Execute a prepared query statement.
 
4387      * @param array specifies all necessary information
 
4388      *              for bindParam() the array elements must use keys corresponding
 
4389      *              to the number of the position of the parameter.
 
4390      * @param mixed specifies which result class to use
 
4391      * @param mixed specifies which class to wrap results in
 
4393      * @return mixed MDB2_Result or integer (affected rows) on success,
 
4394      *               a MDB2 error on failure
 
4397     function execute($values = null, $result_class = true, $result_wrap_class = false)
 
4399         if (null === $this->positions) {
 
4400             return MDB2::raiseError(MDB2_ERROR, null, null,
 
4401                 'Prepared statement has already been freed', __FUNCTION__);
 
4404         $values = (array)$values;
 
4405         if (!empty($values)) {
 
4406             $err = $this->bindValueArray($values);
 
4407             if (MDB2::isError($err)) {
 
4408                 return MDB2::raiseError(MDB2_ERROR, null, null,
 
4409                                             'Binding Values failed with message: ' . $err->getMessage(), __FUNCTION__);
 
4412         $result = $this->_execute($result_class, $result_wrap_class);
 
4417     // {{{ function _execute($result_class = true, $result_wrap_class = false)
 
4420      * Execute a prepared query statement helper method.
 
4422      * @param   mixed   specifies which result class to use
 
4423      * @param   mixed   specifies which class to wrap results in
 
4425      * @return mixed MDB2_Result or integer (affected rows) on success,
 
4426      *               a MDB2 error on failure
 
4429     function _execute($result_class = true, $result_wrap_class = false)
 
4431         $this->last_query = $this->query;
 
4434         foreach ($this->positions as $current_position => $parameter) {
 
4435             if (!array_key_exists($parameter, $this->values)) {
 
4436                 return MDB2::raiseError(MDB2_ERROR_NOT_FOUND, null, null,
 
4437                     'Unable to bind to missing placeholder: '.$parameter, __FUNCTION__);
 
4439             $value = $this->values[$parameter];
 
4440             $query.= substr($this->query, $last_position, $current_position - $last_position);
 
4441             if (!isset($value)) {
 
4442                 $value_quoted = 'NULL';
 
4444                 $type = !empty($this->types[$parameter]) ? $this->types[$parameter] : null;
 
4445                 $value_quoted = $this->db->quote($value, $type);
 
4446                 if (MDB2::isError($value_quoted)) {
 
4447                     return $value_quoted;
 
4450             $query.= $value_quoted;
 
4451             $last_position = $current_position + 1;
 
4453         $query.= substr($this->query, $last_position);
 
4455         $this->db->offset = $this->offset;
 
4456         $this->db->limit = $this->limit;
 
4457         if ($this->is_manip) {
 
4458             $result = $this->db->exec($query);
 
4460             $result = $this->db->query($query, $this->result_types, $result_class, $result_wrap_class);
 
4466     // {{{ function free()
 
4469      * Release resources allocated for the specified prepared query.
 
4471      * @return  mixed   MDB2_OK on success, a MDB2 error on failure
 
4477         if (null === $this->positions) {
 
4478             return MDB2::raiseError(MDB2_ERROR, null, null,
 
4479                 'Prepared statement has already been freed', __FUNCTION__);
 
4482         $this->statement = null;
 
4483         $this->positions = null;
 
4484         $this->query = null;
 
4485         $this->types = null;
 
4486         $this->result_types = null;
 
4487         $this->limit = null;
 
4488         $this->is_manip = null;
 
4489         $this->offset = null;
 
4490         $this->values = null;
 
4499 // {{{ class MDB2_Module_Common
 
4502  * The common modules class for MDB2 module objects
 
4505  * @category    Database
 
4506  * @author      Lukas Smith <smith@pooteeweet.org>
 
4508 class MDB2_Module_Common
 
4510     // {{{ Variables (Properties)
 
4513      * contains the key to the global MDB2 instance array of the associated
 
4519     protected $db_index;
 
4522     // {{{ constructor: function __construct($db_index)
 
4527     function __construct($db_index)
 
4529         $this->db_index = $db_index;
 
4533     // {{{ function getDBInstance()
 
4536      * Get the instance of MDB2 associated with the module instance
 
4538      * @return  object  MDB2 instance or a MDB2 error on failure
 
4542     function getDBInstance()
 
4544         if (isset($GLOBALS['_MDB2_databases'][$this->db_index])) {
 
4545             $result = $GLOBALS['_MDB2_databases'][$this->db_index];
 
4547             $result = MDB2::raiseError(MDB2_ERROR_NOT_FOUND, null, null,
 
4548                 'could not find MDB2 instance');
 
4557 // {{{ function MDB2_closeOpenTransactions()
 
4560  * Close any open transactions form persistent connections
 
4567 function MDB2_closeOpenTransactions()
 
4569     reset($GLOBALS['_MDB2_databases']);
 
4570     while (next($GLOBALS['_MDB2_databases'])) {
 
4571         $key = key($GLOBALS['_MDB2_databases']);
 
4572         if ($GLOBALS['_MDB2_databases'][$key]->opened_persistent
 
4573             && $GLOBALS['_MDB2_databases'][$key]->in_transaction
 
4575             $GLOBALS['_MDB2_databases'][$key]->rollback();
 
4581 // {{{ function MDB2_defaultDebugOutput(&$db, $scope, $message, $is_manip = null)
 
4584  * default debug output handler
 
4586  * @param   object  reference to an MDB2 database object
 
4587  * @param   string  usually the method name that triggered the debug call:
 
4588  *                  for example 'query', 'prepare', 'execute', 'parameters',
 
4589  *                  'beginTransaction', 'commit', 'rollback'
 
4590  * @param   string  message that should be appended to the debug variable
 
4591  * @param   array   contains context information about the debug() call
 
4592  *                  common keys are: is_manip, time, result etc.
 
4594  * @return  void|string optionally return a modified message, this allows
 
4595  *                      rewriting a query before being issued or prepared
 
4599 function MDB2_defaultDebugOutput(&$db, $scope, $message, $context = array())
 
4601     $db->debug_output.= $scope.'('.$db->db_index.'): ';
 
4602     $db->debug_output.= $message.$db->getOption('log_line_break');