MDB2
[ class tree: MDB2 ] [ index: MDB2 ] [ all elements ]

Source for file mysqli.php

Documentation is available at mysqli.php

  1. <?php
  2. // vim: set et ts=4 sw=4 fdm=marker:
  3. // +----------------------------------------------------------------------+
  4. // | PHP versions 4 and 5                                                 |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1998-2006 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.                            |
  13. // |                                                                      |
  14. // | Redistribution and use in source and binary forms, with or without   |
  15. // | modification, are permitted provided that the following conditions   |
  16. // | are met:                                                             |
  17. // |                                                                      |
  18. // | Redistributions of source code must retain the above copyright       |
  19. // | notice, this list of conditions and the following disclaimer.        |
  20. // |                                                                      |
  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. |
  24. // |                                                                      |
  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.                                                  |
  29. // |                                                                      |
  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. // +----------------------------------------------------------------------+
  45. //
  46. // $Id: mysqli.php,v 1.192 2008/11/16 21:45:08 quipo Exp $
  47. //
  48.  
  49. /**
  50.  * MDB2 MySQLi driver
  51.  *
  52.  * @package MDB2
  53.  * @category Database
  54.  * @author  Lukas Smith <smith@pooteeweet.org>
  55.  */
  56. class MDB2_Driver_mysqli extends MDB2_Driver_Common
  57. {
  58.     // {{{ properties
  59.  
  60.     var $string_quoting = array('start' => "'"'end' => "'"'escape' => '\\''escape_pattern' => '\\');
  61.  
  62.     var $identifier_quoting = array('start' => '`''end' => '`''escape' => '`');
  63.  
  64.     var $sql_comments = array(
  65.         array('start' => '-- ''end' => "\n"'escape' => false),
  66.         array('start' => '#''end' => "\n"'escape' => false),
  67.         array('start' => '/*''end' => '*/''escape' => false),
  68.     );
  69.  
  70.     var $server_capabilities_checked = false;
  71.  
  72.     var $start_transaction = false;
  73.  
  74.     var $varchar_max_length = 255;
  75.  
  76.     // }}}
  77.     // {{{ constructor
  78.  
  79.     /**
  80.      * Constructor
  81.      */
  82.     function __construct()
  83.     {
  84.         parent::__construct();
  85.  
  86.         $this->phptype 'mysqli';
  87.         $this->dbsyntax 'mysql';
  88.  
  89.         $this->supported['sequences''emulated';
  90.         $this->supported['indexes'= true;
  91.         $this->supported['affected_rows'= true;
  92.         $this->supported['transactions'= false;
  93.         $this->supported['savepoints'= false;
  94.         $this->supported['summary_functions'= true;
  95.         $this->supported['order_by_text'= true;
  96.         $this->supported['current_id''emulated';
  97.         $this->supported['limit_queries'= true;
  98.         $this->supported['LOBs'= true;
  99.         $this->supported['replace'= true;
  100.         $this->supported['sub_selects''emulated';
  101.         $this->supported['triggers'= false;
  102.         $this->supported['auto_increment'= true;
  103.         $this->supported['primary_key'= true;
  104.         $this->supported['result_introspection'= true;
  105.         $this->supported['prepared_statements''emulated';
  106.         $this->supported['identifier_quoting'= true;
  107.         $this->supported['pattern_escaping'= true;
  108.         $this->supported['new_link'= true;
  109.  
  110.         $this->options['DBA_username'= false;
  111.         $this->options['DBA_password'= false;
  112.         $this->options['default_table_type''';
  113.         $this->options['multi_query'= false;
  114.         $this->options['max_identifiers_length'= 64;
  115.  
  116.         $this->_reCheckSupportedOptions();
  117.     }
  118.  
  119.     // }}}
  120.     // {{{ _reCheckSupportedOptions()
  121.  
  122.     /**
  123.      * If the user changes certain options, other capabilities may depend
  124.      * on the new settings, so we need to check them (again).
  125.      *
  126.      * @access private
  127.      */
  128.     function _reCheckSupportedOptions()
  129.     {
  130.         $this->supported['transactions'$this->options['use_transactions'];
  131.         $this->supported['savepoints']   $this->options['use_transactions'];
  132.         if ($this->options['default_table_type']{
  133.             switch (strtoupper($this->options['default_table_type'])) {
  134.             case 'BLACKHOLE':
  135.             case 'MEMORY':
  136.             case 'ARCHIVE':
  137.             case 'CSV':
  138.             case 'HEAP':
  139.             case 'ISAM':
  140.             case 'MERGE':
  141.             case 'MRG_ISAM':
  142.             case 'ISAM':
  143.             case 'MRG_MYISAM':
  144.             case 'MYISAM':
  145.                 $this->supported['savepoints']   = false;
  146.                 $this->supported['transactions'= false;
  147.                 $this->warnings[$this->options['default_table_type'.
  148.                     ' is not a supported default table type';
  149.                 break;
  150.             }
  151.         }
  152.     }
  153.  
  154.     // }}}
  155.     // {{{ function setOption($option, $value)
  156.  
  157.     /**
  158.      * set the option for the db class
  159.      *
  160.      * @param   string  option name
  161.      * @param   mixed   value for the option
  162.      *
  163.      * @return  mixed   MDB2_OK or MDB2 Error Object
  164.      *
  165.      * @access  public
  166.      */
  167.     function setOption($option$value)
  168.     {
  169.         $res = parent::setOption($option$value);
  170.         $this->_reCheckSupportedOptions();
  171.     }
  172.  
  173.     // }}}
  174.     // {{{ errorInfo()
  175.  
  176.     /**
  177.      * This method is used to collect information about an error
  178.      *
  179.      * @param integer $error 
  180.      * @return array 
  181.      * @access public
  182.      */
  183.     function errorInfo($error = null)
  184.     {
  185.         if ($this->connection{
  186.             $native_code @mysqli_errno($this->connection);
  187.             $native_msg  @mysqli_error($this->connection);
  188.         else {
  189.             $native_code @mysqli_connect_errno();
  190.             $native_msg  @mysqli_connect_error();
  191.         }
  192.         if (is_null($error)) {
  193.             static $ecode_map;
  194.             if (empty($ecode_map)) {
  195.                 $ecode_map = array(
  196.                     1000 => MDB2_ERROR_INVALID//hashchk
  197.                     1001 => MDB2_ERROR_INVALID//isamchk
  198.                     1004 => MDB2_ERROR_CANNOT_CREATE,
  199.                     1005 => MDB2_ERROR_CANNOT_CREATE,
  200.                     1006 => MDB2_ERROR_CANNOT_CREATE,
  201.                     1007 => MDB2_ERROR_ALREADY_EXISTS,
  202.                     1008 => MDB2_ERROR_CANNOT_DROP,
  203.                     1009 => MDB2_ERROR_CANNOT_DROP,
  204.                     1010 => MDB2_ERROR_CANNOT_DROP,
  205.                     1011 => MDB2_ERROR_CANNOT_DELETE,
  206.                     1022 => MDB2_ERROR_ALREADY_EXISTS,
  207.                     1029 => MDB2_ERROR_NOT_FOUND,
  208.                     1032 => MDB2_ERROR_NOT_FOUND,
  209.                     1044 => MDB2_ERROR_ACCESS_VIOLATION,
  210.                     1045 => MDB2_ERROR_ACCESS_VIOLATION,
  211.                     1046 => MDB2_ERROR_NODBSELECTED,
  212.                     1048 => MDB2_ERROR_CONSTRAINT,
  213.                     1049 => MDB2_ERROR_NOSUCHDB,
  214.                     1050 => MDB2_ERROR_ALREADY_EXISTS,
  215.                     1051 => MDB2_ERROR_NOSUCHTABLE,
  216.                     1054 => MDB2_ERROR_NOSUCHFIELD,
  217.                     1060 => MDB2_ERROR_ALREADY_EXISTS,
  218.                     1061 => MDB2_ERROR_ALREADY_EXISTS,
  219.                     1062 => MDB2_ERROR_ALREADY_EXISTS,
  220.                     1064 => MDB2_ERROR_SYNTAX,
  221.                     1067 => MDB2_ERROR_INVALID,
  222.                     1072 => MDB2_ERROR_NOT_FOUND,
  223.                     1086 => MDB2_ERROR_ALREADY_EXISTS,
  224.                     1091 => MDB2_ERROR_NOT_FOUND,
  225.                     1100 => MDB2_ERROR_NOT_LOCKED,
  226.                     1109 => MDB2_ERROR_NOT_FOUND,
  227.                     1125 => MDB2_ERROR_ALREADY_EXISTS,
  228.                     1136 => MDB2_ERROR_VALUE_COUNT_ON_ROW,
  229.                     1138 => MDB2_ERROR_INVALID,
  230.                     1142 => MDB2_ERROR_ACCESS_VIOLATION,
  231.                     1143 => MDB2_ERROR_ACCESS_VIOLATION,
  232.                     1146 => MDB2_ERROR_NOSUCHTABLE,
  233.                     1149 => MDB2_ERROR_SYNTAX,
  234.                     1169 => MDB2_ERROR_CONSTRAINT,
  235.                     1176 => MDB2_ERROR_NOT_FOUND,
  236.                     1177 => MDB2_ERROR_NOSUCHTABLE,
  237.                     1213 => MDB2_ERROR_DEADLOCK,
  238.                     1216 => MDB2_ERROR_CONSTRAINT,
  239.                     1217 => MDB2_ERROR_CONSTRAINT,
  240.                     1227 => MDB2_ERROR_ACCESS_VIOLATION,
  241.                     1235 => MDB2_ERROR_CANNOT_CREATE,
  242.                     1299 => MDB2_ERROR_INVALID_DATE,
  243.                     1300 => MDB2_ERROR_INVALID,
  244.                     1304 => MDB2_ERROR_ALREADY_EXISTS,
  245.                     1305 => MDB2_ERROR_NOT_FOUND,
  246.                     1306 => MDB2_ERROR_CANNOT_DROP,
  247.                     1307 => MDB2_ERROR_CANNOT_CREATE,
  248.                     1334 => MDB2_ERROR_CANNOT_ALTER,
  249.                     1339 => MDB2_ERROR_NOT_FOUND,
  250.                     1356 => MDB2_ERROR_INVALID,
  251.                     1359 => MDB2_ERROR_ALREADY_EXISTS,
  252.                     1360 => MDB2_ERROR_NOT_FOUND,
  253.                     1363 => MDB2_ERROR_NOT_FOUND,
  254.                     1365 => MDB2_ERROR_DIVZERO,
  255.                     1451 => MDB2_ERROR_CONSTRAINT,
  256.                     1452 => MDB2_ERROR_CONSTRAINT,
  257.                     1542 => MDB2_ERROR_CANNOT_DROP,
  258.                     1546 => MDB2_ERROR_CONSTRAINT,
  259.                     1582 => MDB2_ERROR_CONSTRAINT,
  260.                     2003 => MDB2_ERROR_CONNECT_FAILED,
  261.                     2019 => MDB2_ERROR_INVALID,
  262.                 );
  263.             }
  264.             if ($this->options['portability'MDB2_PORTABILITY_ERRORS{
  265.                 $ecode_map[1022= MDB2_ERROR_CONSTRAINT;
  266.                 $ecode_map[1048= MDB2_ERROR_CONSTRAINT_NOT_NULL;
  267.                 $ecode_map[1062= MDB2_ERROR_CONSTRAINT;
  268.             else {
  269.                 // Doing this in case mode changes during runtime.
  270.                 $ecode_map[1022= MDB2_ERROR_ALREADY_EXISTS;
  271.                 $ecode_map[1048= MDB2_ERROR_CONSTRAINT;
  272.                 $ecode_map[1062= MDB2_ERROR_ALREADY_EXISTS;
  273.             }
  274.             if (isset($ecode_map[$native_code])) {
  275.                 $error $ecode_map[$native_code];
  276.             }
  277.         }
  278.         return array($error$native_code$native_msg);
  279.     }
  280.  
  281.     // }}}
  282.     // {{{ escape()
  283.  
  284.     /**
  285.      * Quotes a string so it can be safely used in a query. It will quote
  286.      * the text so it can safely be used within a query.
  287.      *
  288.      * @param   string  the input string to quote
  289.      * @param   bool    escape wildcards
  290.      *
  291.      * @return  string  quoted string
  292.      *
  293.      * @access  public
  294.      */
  295.     function escape($text$escape_wildcards = false)
  296.     {
  297.         if ($escape_wildcards{
  298.             $text $this->escapePattern($text);
  299.         }
  300.         $connection $this->getConnection();
  301.         if (PEAR::isError($connection)) {
  302.             return $connection;
  303.         }
  304.         $text @mysqli_real_escape_string($connection$text);
  305.         return $text;
  306.     }
  307.  
  308.     // }}}
  309.     // {{{ beginTransaction()
  310.  
  311.     /**
  312.      * Start a transaction or set a savepoint.
  313.      *
  314.      * @param   string  name of a savepoint to set
  315.      * @return  mixed   MDB2_OK on success, a MDB2 error on failure
  316.      *
  317.      * @access  public
  318.      */
  319.     function beginTransaction($savepoint = null)
  320.     {
  321.         $this->debug('Starting transaction/savepoint'__FUNCTION__array('is_manip' => true'savepoint' => $savepoint));
  322.         $this->_getServerCapabilities();
  323.         if (!is_null($savepoint)) {
  324.             if (!$this->supports('savepoints')) {
  325.                 return $this->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  326.                     'savepoints are not supported'__FUNCTION__);
  327.             }
  328.             if (!$this->in_transaction{
  329.                 return $this->raiseError(MDB2_ERROR_INVALIDnullnull,
  330.                     'savepoint cannot be released when changes are auto committed'__FUNCTION__);
  331.             }
  332.             $query 'SAVEPOINT '.$savepoint;
  333.             return $this->_doQuery($querytrue);
  334.         elseif ($this->in_transaction{
  335.             return MDB2_OK;  //nothing to do
  336.         }
  337.         $query $this->start_transaction ? 'START TRANSACTION' 'SET AUTOCOMMIT = 0';
  338.         $result =$this->_doQuery($querytrue);
  339.         if (PEAR::isError($result)) {
  340.             return $result;
  341.         }
  342.         $this->in_transaction = true;
  343.         return MDB2_OK;
  344.     }
  345.  
  346.     // }}}
  347.     // {{{ commit()
  348.  
  349.     /**
  350.      * Commit the database changes done during a transaction that is in
  351.      * progress or release a savepoint. This function may only be called when
  352.      * auto-committing is disabled, otherwise it will fail. Therefore, a new
  353.      * transaction is implicitly started after committing the pending changes.
  354.      *
  355.      * @param   string  name of a savepoint to release
  356.      * @return  mixed   MDB2_OK on success, a MDB2 error on failure
  357.      *
  358.      * @access  public
  359.      */
  360.     function commit($savepoint = null)
  361.     {
  362.         $this->debug('Committing transaction/savepoint'__FUNCTION__array('is_manip' => true'savepoint' => $savepoint));
  363.         if (!$this->in_transaction{
  364.             return $this->raiseError(MDB2_ERROR_INVALIDnullnull,
  365.                 'commit/release savepoint cannot be done changes are auto committed'__FUNCTION__);
  366.         }
  367.         if (!is_null($savepoint)) {
  368.             if (!$this->supports('savepoints')) {
  369.                 return $this->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  370.                     'savepoints are not supported'__FUNCTION__);
  371.             }
  372.             $server_info $this->getServerVersion();
  373.             if (version_compare($server_info['major'].'.'.$server_info['minor'].'.'.$server_info['patch']'5.0.3''<')) {
  374.                 return MDB2_OK;
  375.             }
  376.             $query 'RELEASE SAVEPOINT '.$savepoint;
  377.             return $this->_doQuery($querytrue);
  378.         }
  379.  
  380.         if (!$this->supports('transactions')) {
  381.             return $this->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  382.                 'transactions are not supported'__FUNCTION__);
  383.         }
  384.  
  385.         $result =$this->_doQuery('COMMIT'true);
  386.         if (PEAR::isError($result)) {
  387.             return $result;
  388.         }
  389.         if (!$this->start_transaction{
  390.             $query 'SET AUTOCOMMIT = 1';
  391.             $result =$this->_doQuery($querytrue);
  392.             if (PEAR::isError($result)) {
  393.                 return $result;
  394.             }
  395.         }
  396.         $this->in_transaction = false;
  397.         return MDB2_OK;
  398.     }
  399.  
  400.     // }}}
  401.     // {{{ rollback()
  402.  
  403.     /**
  404.      * Cancel any database changes done during a transaction or since a specific
  405.      * savepoint that is in progress. This function may only be called when
  406.      * auto-committing is disabled, otherwise it will fail. Therefore, a new
  407.      * transaction is implicitly started after canceling the pending changes.
  408.      *
  409.      * @param   string  name of a savepoint to rollback to
  410.      * @return  mixed   MDB2_OK on success, a MDB2 error on failure
  411.      *
  412.      * @access  public
  413.      */
  414.     function rollback($savepoint = null)
  415.     {
  416.         $this->debug('Rolling back transaction/savepoint'__FUNCTION__array('is_manip' => true'savepoint' => $savepoint));
  417.         if (!$this->in_transaction{
  418.             return $this->raiseError(MDB2_ERROR_INVALIDnullnull,
  419.                 'rollback cannot be done changes are auto committed'__FUNCTION__);
  420.         }
  421.         if (!is_null($savepoint)) {
  422.             if (!$this->supports('savepoints')) {
  423.                 return $this->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  424.                     'savepoints are not supported'__FUNCTION__);
  425.             }
  426.             $query 'ROLLBACK TO SAVEPOINT '.$savepoint;
  427.             return $this->_doQuery($querytrue);
  428.         }
  429.  
  430.         $query 'ROLLBACK';
  431.         $result =$this->_doQuery($querytrue);
  432.         if (PEAR::isError($result)) {
  433.             return $result;
  434.         }
  435.         if (!$this->start_transaction{
  436.             $query 'SET AUTOCOMMIT = 1';
  437.             $result =$this->_doQuery($querytrue);
  438.             if (PEAR::isError($result)) {
  439.                 return $result;
  440.             }
  441.         }
  442.         $this->in_transaction = false;
  443.         return MDB2_OK;
  444.     }
  445.  
  446.     // }}}
  447.     // {{{ function setTransactionIsolation()
  448.  
  449.     /**
  450.      * Set the transacton isolation level.
  451.      *
  452.      * @param   string  standard isolation level
  453.      *                   READ UNCOMMITTED (allows dirty reads)
  454.      *                   READ COMMITTED (prevents dirty reads)
  455.      *                   REPEATABLE READ (prevents nonrepeatable reads)
  456.      *                   SERIALIZABLE (prevents phantom reads)
  457.      * @return  mixed   MDB2_OK on success, a MDB2 error on failure
  458.      *
  459.      * @access  public
  460.      * @since   2.1.1
  461.      */
  462.     function setTransactionIsolation($isolation)
  463.     {
  464.         $this->debug('Setting transaction isolation level'__FUNCTION__array('is_manip' => true));
  465.         if (!$this->supports('transactions')) {
  466.             return $this->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  467.                 'transactions are not supported'__FUNCTION__);
  468.         }
  469.         switch ($isolation{
  470.         case 'READ UNCOMMITTED':
  471.         case 'READ COMMITTED':
  472.         case 'REPEATABLE READ':
  473.         case 'SERIALIZABLE':
  474.             break;
  475.         default:
  476.             return $this->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  477.                 'isolation level is not supported: '.$isolation__FUNCTION__);
  478.         }
  479.  
  480.         $query = "SET SESSION TRANSACTION ISOLATION LEVEL $isolation";
  481.         return $this->_doQuery($querytrue);
  482.     }
  483.  
  484.     // }}}
  485.     // {{{ _doConnect()
  486.  
  487.     /**
  488.      * do the grunt work of the connect
  489.      *
  490.      * @return connection on success or MDB2 Error Object on failure
  491.      * @access protected
  492.      */
  493.     function _doConnect($username$password$persistent = false)
  494.     {
  495.         if (!PEAR::loadExtension($this->phptype)) {
  496.             return $this->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  497.                 'extension '.$this->phptype.' is not compiled into PHP'__FUNCTION__);
  498.         }
  499.  
  500.         $connection @mysqli_init();
  501.         if (!empty($this->dsn['charset']&& defined('MYSQLI_SET_CHARSET_NAME')) {
  502.             @mysqli_options($connectionMYSQLI_SET_CHARSET_NAME$this->dsn['charset']);
  503.         }
  504.  
  505.         if ($this->options['ssl']{
  506.             @mysqli_ssl_set(
  507.                 $connection,
  508.                 empty($this->dsn['key'])    ? null : $this->dsn['key'],
  509.                 empty($this->dsn['cert'])   ? null : $this->dsn['cert'],
  510.                 empty($this->dsn['ca'])     ? null : $this->dsn['ca'],
  511.                 empty($this->dsn['capath']? null : $this->dsn['capath'],
  512.                 empty($this->dsn['cipher']? null : $this->dsn['cipher']
  513.             );
  514.         }
  515.  
  516.         if (!@mysqli_real_connect(
  517.             $connection,
  518.             $this->dsn['hostspec'],
  519.             $username,
  520.             $password,
  521.             $this->database_name,
  522.             $this->dsn['port'],
  523.             $this->dsn['socket']
  524.         )) {
  525.             if (($err @mysqli_connect_error()) != ''{
  526.                 return $this->raiseError(null,
  527.                     nullnull$err__FUNCTION__);
  528.             else {
  529.                 return $this->raiseError(MDB2_ERROR_CONNECT_FAILEDnullnull,
  530.                     'unable to establish a connection'__FUNCTION__);
  531.             }
  532.         }
  533.  
  534.         if (!empty($this->dsn['charset']&& !defined('MYSQLI_SET_CHARSET_NAME')) {
  535.             $result $this->setCharset($this->dsn['charset']$connection);
  536.             if (PEAR::isError($result)) {
  537.                 return $result;
  538.             }
  539.         }
  540.  
  541.         return $connection;
  542.     }
  543.  
  544.     // }}}
  545.     // {{{ connect()
  546.  
  547.     /**
  548.      * Connect to the database
  549.      *
  550.      * @return true on success, MDB2 Error Object on failure
  551.      */
  552.     function connect()
  553.     {
  554.         if (is_object($this->connection)) {
  555.             //if (count(array_diff($this->connected_dsn, $this->dsn)) == 0) {
  556.             if (MDB2::areEquals($this->connected_dsn$this->dsn)) {
  557.                 return MDB2_OK;
  558.             }
  559.             $this->connection = 0;
  560.         }
  561.  
  562.         $connection $this->_doConnect(
  563.             $this->dsn['username'],
  564.             $this->dsn['password']
  565.         );
  566.         if (PEAR::isError($connection)) {
  567.             return $connection;
  568.         }
  569.  
  570.         $this->connection $connection;
  571.         $this->connected_dsn $this->dsn;
  572.         $this->connected_database_name $this->database_name;
  573.         $this->dbsyntax $this->dsn['dbsyntax'$this->dsn['dbsyntax'$this->phptype;
  574.  
  575.         $this->_getServerCapabilities();
  576.  
  577.         return MDB2_OK;
  578.     }
  579.  
  580.     // }}}
  581.     // {{{ setCharset()
  582.  
  583.     /**
  584.      * Set the charset on the current connection
  585.      *
  586.      * @param string    charset (or array(charset, collation))
  587.      * @param resource  connection handle
  588.      *
  589.      * @return true on success, MDB2 Error Object on failure
  590.      */
  591.     function setCharset($charset$connection = null)
  592.     {
  593.         if (is_null($connection)) {
  594.             $connection $this->getConnection();
  595.             if (PEAR::isError($connection)) {
  596.                 return $connection;
  597.             }
  598.         }
  599.         $collation = null;
  600.         if (is_array($charset&& 2 == count($charset)) {
  601.             $collation array_pop($charset);
  602.             $charset   array_pop($charset);
  603.         }
  604.         $client_info mysqli_get_client_version();
  605.         if (OS_WINDOWS && ((40111 > $client_info||
  606.             ((50000 <= $client_info&& (50006 > $client_info)))
  607.         {
  608.             $query "SET NAMES '".mysqli_real_escape_string($connection$charset)."'";
  609.             if (!is_null($collation)) {
  610.                 $query .= " COLLATE '".mysqli_real_escape_string($connection$collation)."'";
  611.             }
  612.             return $this->_doQuery($querytrue$connection);
  613.         }
  614.         if (!$result mysqli_set_charset($connection$charset)) {
  615.             $err =$this->raiseError(nullnullnull,
  616.                 'Could not set client character set'__FUNCTION__);
  617.             return $err;
  618.         }
  619.         return $result;
  620.     }
  621.  
  622.     // }}}
  623.     // {{{ databaseExists()
  624.  
  625.     /**
  626.      * check if given database name is exists?
  627.      *
  628.      * @param string $name    name of the database that should be checked
  629.      *
  630.      * @return mixed true/false on success, a MDB2 error on failure
  631.      * @access public
  632.      */
  633.     function databaseExists($name)
  634.     {
  635.         $connection $this->_doConnect($this->dsn['username'],
  636.                                         $this->dsn['password']);
  637.         if (PEAR::isError($connection)) {
  638.             return $connection;
  639.         }
  640.  
  641.         $result @mysqli_select_db($connection$name);
  642.         @mysqli_close($connection);
  643.  
  644.         return $result;
  645.     }
  646.  
  647.     // }}}
  648.     // {{{ disconnect()
  649.  
  650.     /**
  651.      * Log out and disconnect from the database.
  652.      *
  653.      * @param  boolean $force if the disconnect should be forced even if the
  654.      *                         connection is opened persistently
  655.      * @return mixed true on success, false if not connected and error
  656.      *                 object on error
  657.      * @access public
  658.      */
  659.     function disconnect($force = true)
  660.     {
  661.         if (is_object($this->connection)) {
  662.             if ($this->in_transaction{
  663.                 $dsn $this->dsn;
  664.                 $database_name $this->database_name;
  665.                 $persistent $this->options['persistent'];
  666.                 $this->dsn $this->connected_dsn;
  667.                 $this->database_name $this->connected_database_name;
  668.                 $this->options['persistent'$this->opened_persistent;
  669.                 $this->rollback();
  670.                 $this->dsn $dsn;
  671.                 $this->database_name $database_name;
  672.                 $this->options['persistent'$persistent;
  673.             }
  674.  
  675.             if ($force{
  676.                 $ok @mysqli_close($this->connection);
  677.                 if (!$ok{
  678.                     return $this->raiseError(MDB2_ERROR_DISCONNECT_FAILED,
  679.                            nullnullnull__FUNCTION__);
  680.                 }
  681.             }
  682.         else {
  683.             return false;
  684.         }
  685.         return parent::disconnect($force);
  686.     }
  687.  
  688.     // }}}
  689.     // {{{ standaloneQuery()
  690.  
  691.    /**
  692.      * execute a query as DBA
  693.      *
  694.      * @param string $query the SQL query
  695.      * @param mixed   $types  array that contains the types of the columns in
  696.      *                         the result set
  697.      * @param boolean $is_manip  if the query is a manipulation query
  698.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  699.      * @access public
  700.      */
  701.     function &standaloneQuery($query$types = null$is_manip = false)
  702.     {
  703.         $user $this->options['DBA_username']$this->options['DBA_username'$this->dsn['username'];
  704.         $pass $this->options['DBA_password']$this->options['DBA_password'$this->dsn['password'];
  705.         $connection $this->_doConnect($user$pass);
  706.         if (PEAR::isError($connection)) {
  707.             return $connection;
  708.         }
  709.  
  710.         $offset $this->offset;
  711.         $limit $this->limit;
  712.         $this->offset $this->limit = 0;
  713.         $query $this->_modifyQuery($query$is_manip$limit$offset);
  714.         
  715.         $result =$this->_doQuery($query$is_manip$connection$this->database_name);
  716.         if (!PEAR::isError($result)) {
  717.             $result $this->_affectedRows($connection$result);
  718.         }
  719.  
  720.         @mysqli_close($connection);
  721.         return $result;
  722.     }
  723.  
  724.     // }}}
  725.     // {{{ _doQuery()
  726.  
  727.     /**
  728.      * Execute a query
  729.      * @param string $query  query
  730.      * @param boolean $is_manip  if the query is a manipulation query
  731.      * @param resource $connection 
  732.      * @param string $database_name 
  733.      * @return result or error object
  734.      * @access protected
  735.      */
  736.     function &_doQuery($query$is_manip = false$connection = null$database_name = null)
  737.     {
  738.         $this->last_query $query;
  739.         $result $this->debug($query'query'array('is_manip' => $is_manip'when' => 'pre'));
  740.         if ($result{
  741.             if (PEAR::isError($result)) {
  742.                 return $result;
  743.             }
  744.             $query $result;
  745.         }
  746.         if ($this->options['disable_query']{
  747.             $result $is_manip ? 0 : null;
  748.             return $result;
  749.         }
  750.  
  751.         if (is_null($connection)) {
  752.             $connection $this->getConnection();
  753.             if (PEAR::isError($connection)) {
  754.                 return $connection;
  755.             }
  756.         }
  757.         if (is_null($database_name)) {
  758.             $database_name $this->database_name;
  759.         }
  760.  
  761.         if ($database_name{
  762.             if ($database_name != $this->connected_database_name{
  763.                 if (!@mysqli_select_db($connection$database_name)) {
  764.                     $err $this->raiseError(nullnullnull,
  765.                         'Could not select the database: '.$database_name__FUNCTION__);
  766.                     return $err;
  767.                 }
  768.                 $this->connected_database_name $database_name;
  769.             }
  770.         }
  771.  
  772.         if ($this->options['multi_query']{
  773.             $result mysqli_multi_query($connection$query);
  774.         else {
  775.             $resultmode $this->options['result_buffering'? MYSQLI_USE_RESULT : MYSQLI_USE_RESULT;
  776.             $result mysqli_query($connection$query);
  777.         }
  778.  
  779.         if (!$result{
  780.             $err =$this->raiseError(nullnullnull,
  781.                 'Could not execute statement'__FUNCTION__);
  782.             return $err;
  783.         }
  784.  
  785.         if ($this->options['multi_query']{
  786.             if ($this->options['result_buffering']{
  787.                 if (!($result @mysqli_store_result($connection))) {
  788.                     $err =$this->raiseError(nullnullnull,
  789.                         'Could not get the first result from a multi query'__FUNCTION__);
  790.                     return $err;
  791.                 }
  792.             elseif (!($result @mysqli_use_result($connection))) {
  793.                 $err =$this->raiseError(nullnullnull,
  794.                         'Could not get the first result from a multi query'__FUNCTION__);
  795.                 return $err;
  796.             }
  797.         }
  798.  
  799.         $this->debug($query'query'array('is_manip' => $is_manip'when' => 'post''result' => $result));
  800.         return $result;
  801.     }
  802.  
  803.     // }}}
  804.     // {{{ _affectedRows()
  805.  
  806.     /**
  807.      * Returns the number of rows affected
  808.      *
  809.      * @param resource $result 
  810.      * @param resource $connection 
  811.      * @return mixed MDB2 Error Object or the number of rows affected
  812.      * @access private
  813.      */
  814.     function _affectedRows($connection$result = null)
  815.     {
  816.         if (is_null($connection)) {
  817.             $connection $this->getConnection();
  818.             if (PEAR::isError($connection)) {
  819.                 return $connection;
  820.             }
  821.         }
  822.         return @mysqli_affected_rows($connection);
  823.     }
  824.  
  825.     // }}}
  826.     // {{{ _modifyQuery()
  827.  
  828.     /**
  829.      * Changes a query string for various DBMS specific reasons
  830.      *
  831.      * @param string $query  query to modify
  832.      * @param boolean $is_manip  if it is a DML query
  833.      * @param integer $limit  limit the number of rows
  834.      * @param integer $offset  start reading from given offset
  835.      * @return string modified query
  836.      * @access protected
  837.      */
  838.     function _modifyQuery($query$is_manip$limit$offset)
  839.     {
  840.         if ($this->options['portability'MDB2_PORTABILITY_DELETE_COUNT{
  841.             // "DELETE FROM table" gives 0 affected rows in MySQL.
  842.             // This little hack lets you know how many rows were deleted.
  843.             if (preg_match('/^\s*DELETE\s+FROM\s+(\S+)\s*$/i'$query)) {
  844.                 $query preg_replace('/^\s*DELETE\s+FROM\s+(\S+)\s*$/',
  845.                                       'DELETE FROM \1 WHERE 1=1'$query);
  846.             }
  847.         }
  848.         if ($limit > 0
  849.             && !preg_match('/LIMIT\s*\d(?:\s*(?:,|OFFSET)\s*\d+)?(?:[^\)]*)?$/i'$query)
  850.         {
  851.             $query rtrim($query);
  852.             if (substr($query-1== ';'{
  853.                 $query substr($query0-1);
  854.             }
  855.  
  856.             // LIMIT doesn't always come last in the query
  857.             // @see http://dev.mysql.com/doc/refman/5.0/en/select.html
  858.             $after '';
  859.             if (preg_match('/(\s+INTO\s+(?:OUT|DUMP)FILE\s.*)$/ims'$query$matches)) {
  860.                 $after $matches[0];
  861.                 $query preg_replace('/(\s+INTO\s+(?:OUT|DUMP)FILE\s.*)$/ims'''$query);
  862.             elseif (preg_match('/(\s+FOR\s+UPDATE\s*)$/i'$query$matches)) {
  863.                $after $matches[0];
  864.                $query preg_replace('/(\s+FOR\s+UPDATE\s*)$/im'''$query);
  865.             elseif (preg_match('/(\s+LOCK\s+IN\s+SHARE\s+MODE\s*)$/im'$query$matches)) {
  866.                $after $matches[0];
  867.                $query preg_replace('/(\s+LOCK\s+IN\s+SHARE\s+MODE\s*)$/im'''$query);
  868.             }
  869.  
  870.             if ($is_manip{
  871.                 return $query . " LIMIT $limit" . $after;
  872.             else {
  873.                 return $query . " LIMIT $offset$limit" . $after;
  874.             }
  875.         }
  876.         return $query;
  877.     }
  878.  
  879.     // }}}
  880.     // {{{ getServerVersion()
  881.  
  882.     /**
  883.      * return version information about the server
  884.      *
  885.      * @param bool   $native  determines if the raw version string should be returned
  886.      * @return mixed array/string with version information or MDB2 error object
  887.      * @access public
  888.      */
  889.     function getServerVersion($native = false)
  890.     {
  891.         $connection $this->getConnection();
  892.         if (PEAR::isError($connection)) {
  893.             return $connection;
  894.         }
  895.         if ($this->connected_server_info{
  896.             $server_info $this->connected_server_info;
  897.         else {
  898.             $server_info @mysqli_get_server_info($connection);
  899.         }
  900.         if (!$server_info{
  901.             return $this->raiseError(nullnullnull,
  902.                 'Could not get server information'__FUNCTION__);
  903.         }
  904.         // cache server_info
  905.         $this->connected_server_info $server_info;
  906.         if (!$native{
  907.             $tmp explode('.'$server_info3);
  908.             if (isset($tmp[2]&& strpos($tmp[2]'-')) {
  909.                 $tmp2 explode('-'@$tmp[2]2);
  910.             else {
  911.                 $tmp2[0= isset($tmp[2]$tmp[2: null;
  912.                 $tmp2[1= null;
  913.             }
  914.             $server_info = array(
  915.                 'major' => isset($tmp[0]$tmp[0: null,
  916.                 'minor' => isset($tmp[1]$tmp[1: null,
  917.                 'patch' => $tmp2[0],
  918.                 'extra' => $tmp2[1],
  919.                 'native' => $server_info,
  920.             );
  921.         }
  922.         return $server_info;
  923.     }
  924.  
  925.     // }}}
  926.     // {{{ _getServerCapabilities()
  927.  
  928.     /**
  929.      * Fetch some information about the server capabilities
  930.      * (transactions, subselects, prepared statements, etc).
  931.      *
  932.      * @access private
  933.      */
  934.     function _getServerCapabilities()
  935.     {
  936.         if (!$this->server_capabilities_checked{
  937.             $this->server_capabilities_checked = true;
  938.  
  939.             //set defaults
  940.             $this->supported['sub_selects''emulated';
  941.             $this->supported['prepared_statements''emulated';
  942.             $this->supported['triggers'= false;
  943.             $this->start_transaction = false;
  944.             $this->varchar_max_length = 255;
  945.  
  946.             $server_info $this->getServerVersion();
  947.             if (is_array($server_info)) {
  948.                 $server_version $server_info['major'].'.'.$server_info['minor'].'.'.$server_info['patch'];
  949.             
  950.                 if (!version_compare($server_version'4.1.0''<')) {
  951.                     $this->supported['sub_selects'= true;
  952.                     $this->supported['prepared_statements'= true;
  953.                 }
  954.  
  955.                 // SAVEPOINTs were introduced in MySQL 4.0.14 and 4.1.1 (InnoDB)
  956.                 if (version_compare($server_version'4.1.0''>=')) {
  957.                     if (version_compare($server_version'4.1.1''<')) {
  958.                         $this->supported['savepoints'= false;
  959.                     }
  960.                 elseif (version_compare($server_version'4.0.14''<')) {
  961.                     $this->supported['savepoints'= false;
  962.                 }
  963.  
  964.                 if (!version_compare($server_version'4.0.11''<')) {
  965.                     $this->start_transaction = true;
  966.                 }
  967.  
  968.                 if (!version_compare($server_version'5.0.3''<')) {
  969.                     $this->varchar_max_length = 65532;
  970.                 }
  971.  
  972.                 if (!version_compare($server_version'5.0.2''<')) {
  973.                     $this->supported['triggers'= true;
  974.                 }
  975.             }
  976.         }
  977.     }
  978.  
  979.     // }}}
  980.     // {{{ function _skipUserDefinedVariable($query, $position)
  981.  
  982.     /**
  983.      * Utility method, used by prepare() to avoid misinterpreting MySQL user
  984.      * defined variables (SELECT @x:=5) for placeholders.
  985.      * Check if the placeholder is a false positive, i.e. if it is an user defined
  986.      * variable instead. If so, skip it and advance the position, otherwise
  987.      * return the current position, which is valid
  988.      *
  989.      * @param string $query 
  990.      * @param integer $position current string cursor position
  991.      * @return integer $new_position
  992.      * @access protected
  993.      */
  994.     function _skipUserDefinedVariable($query$position)
  995.     {
  996.         $found strpos(strrev(substr($query0$position))'@');
  997.         if ($found === false{
  998.             return $position;
  999.         }
  1000.         $pos strlen($querystrlen(substr($query$position)) $found - 1;
  1001.         $substring substr($query$pos$position $pos + 2);
  1002.         if (preg_match('/^@\w+\s*:=$/'$substring)) {
  1003.             return $position + 1; //found an user defined variable: skip it
  1004.         }
  1005.         return $position;
  1006.     }
  1007.  
  1008.     // }}}
  1009.     // {{{ prepare()
  1010.  
  1011.     /**
  1012.      * Prepares a query for multiple execution with execute().
  1013.      * With some database backends, this is emulated.
  1014.      * prepare() requires a generic query as string like
  1015.      * 'INSERT INTO numbers VALUES(?,?)' or
  1016.      * 'INSERT INTO numbers VALUES(:foo,:bar)'.
  1017.      * The ? and :name and are placeholders which can be set using
  1018.      * bindParam() and the query can be sent off using the execute() method.
  1019.      * The allowed format for :name can be set with the 'bindname_format' option.
  1020.      *
  1021.      * @param string $query the query to prepare
  1022.      * @param mixed   $types  array that contains the types of the placeholders
  1023.      * @param mixed   $result_types  array that contains the types of the columns in
  1024.      *                         the result set or MDB2_PREPARE_RESULT, if set to
  1025.      *                         MDB2_PREPARE_MANIP the query is handled as a manipulation query
  1026.      * @param mixed   $lobs   key (field) value (parameter) pair for all lob placeholders
  1027.      * @return mixed resource handle for the prepared query on success, a MDB2
  1028.      *         error on failure
  1029.      * @access public
  1030.      * @see bindParam, execute
  1031.      */
  1032.     function &prepare($query$types = null$result_types = null$lobs = array())
  1033.     {
  1034.         if ($this->options['emulate_prepared']
  1035.             || $this->supported['prepared_statements'!== true
  1036.         {
  1037.             $obj =parent::prepare($query$types$result_types$lobs);
  1038.             return $obj;
  1039.         }
  1040.         $is_manip ($result_types === MDB2_PREPARE_MANIP);
  1041.         $offset $this->offset;
  1042.         $limit $this->limit;
  1043.         $this->offset $this->limit = 0;
  1044.         $query $this->_modifyQuery($query$is_manip$limit$offset);
  1045.         $result $this->debug($query__FUNCTION__array('is_manip' => $is_manip'when' => 'pre'));
  1046.         if ($result{
  1047.             if (PEAR::isError($result)) {
  1048.                 return $result;
  1049.             }
  1050.             $query $result;
  1051.         }
  1052.         $placeholder_type_guess $placeholder_type = null;
  1053.         $question '?';
  1054.         $colon ':';
  1055.         $positions = array();
  1056.         $position = 0;
  1057.         while ($position strlen($query)) {
  1058.             $q_position strpos($query$question$position);
  1059.             $c_position strpos($query$colon$position);
  1060.             if ($q_position && $c_position{
  1061.                 $p_position min($q_position$c_position);
  1062.             elseif ($q_position{
  1063.                 $p_position $q_position;
  1064.             elseif ($c_position{
  1065.                 $p_position $c_position;
  1066.             else {
  1067.                 break;
  1068.             }
  1069.             if (is_null($placeholder_type)) {
  1070.                 $placeholder_type_guess $query[$p_position];
  1071.             }
  1072.             
  1073.             $new_pos $this->_skipDelimitedStrings($query$position$p_position);
  1074.             if (PEAR::isError($new_pos)) {
  1075.                 return $new_pos;
  1076.             }
  1077.             if ($new_pos != $position{
  1078.                 $position $new_pos;
  1079.                 continue; //evaluate again starting from the new position
  1080.             }
  1081.             
  1082.             //make sure this is not part of an user defined variable
  1083.             $new_pos $this->_skipUserDefinedVariable($query$position);
  1084.             if ($new_pos != $position{
  1085.                 $position $new_pos;
  1086.                 continue; //evaluate again starting from the new position
  1087.             }
  1088.  
  1089.             if ($query[$position== $placeholder_type_guess{
  1090.                 if (is_null($placeholder_type)) {
  1091.                     $placeholder_type $query[$p_position];
  1092.                     $question $colon $placeholder_type;
  1093.                 }
  1094.                 if ($placeholder_type == ':'{
  1095.                     $regexp '/^.{'.($position+1).'}('.$this->options['bindname_format'].').*$/s';
  1096.                     $parameter preg_replace($regexp'\\1'$query);
  1097.                     if ($parameter === ''{
  1098.                         $err =$this->raiseError(MDB2_ERROR_SYNTAXnullnull,
  1099.                             'named parameter name must match "bindname_format" option'__FUNCTION__);
  1100.                         return $err;
  1101.                     }
  1102.                     $positions[$p_position$parameter;
  1103.                     $query substr_replace($query'?'$positionstrlen($parameter)+1);
  1104.                 else {
  1105.                     $positions[$p_positioncount($positions);
  1106.                 }
  1107.                 $position $p_position + 1;
  1108.             else {
  1109.                 $position $p_position;
  1110.             }
  1111.         }
  1112.         $connection $this->getConnection();
  1113.         if (PEAR::isError($connection)) {
  1114.             return $connection;
  1115.         }
  1116.  
  1117.         if (!$is_manip{
  1118.             static $prep_statement_counter = 1;
  1119.             $statement_name = sprintf($this->options['statement_format']$this->phptype$prep_statement_counter++ . sha1(microtime(+ mt_rand()));
  1120.             $statement_name substr(strtolower($statement_name)0$this->options['max_identifiers_length']);
  1121.             $query = "PREPARE $statement_name FROM ".$this->quote($query'text');
  1122.  
  1123.             $statement =$this->_doQuery($querytrue$connection);
  1124.             if (PEAR::isError($statement)) {
  1125.                 return $statement;
  1126.             }
  1127.             $statement $statement_name;
  1128.         else {
  1129.             $statement @mysqli_prepare($connection$query);
  1130.             if (!$statement{
  1131.                 $err =$this->raiseError(nullnullnull,
  1132.                     'Unable to create prepared statement handle'__FUNCTION__);
  1133.                 return $err;
  1134.             }
  1135.         }
  1136.  
  1137.         $class_name 'MDB2_Statement_'.$this->phptype;
  1138.         $obj = new $class_name($this$statement$positions$query$types$result_types$is_manip$limit$offset);
  1139.         $this->debug($query__FUNCTION__array('is_manip' => $is_manip'when' => 'post''result' => $obj));
  1140.         return $obj;
  1141.     }
  1142.  
  1143.     // }}}
  1144.     // {{{ replace()
  1145.  
  1146.     /**
  1147.      * Execute a SQL REPLACE query. A REPLACE query is identical to a INSERT
  1148.      * query, except that if there is already a row in the table with the same
  1149.      * key field values, the old row is deleted before the new row is inserted.
  1150.      *
  1151.      * The REPLACE type of query does not make part of the SQL standards. Since
  1152.      * practically only MySQL implements it natively, this type of query is
  1153.      * emulated through this method for other DBMS using standard types of
  1154.      * queries inside a transaction to assure the atomicity of the operation.
  1155.      *
  1156.      * @access public
  1157.      *
  1158.      * @param string $table name of the table on which the REPLACE query will
  1159.      *   be executed.
  1160.      * @param array $fields associative array that describes the fields and the
  1161.      *   values that will be inserted or updated in the specified table. The
  1162.      *   indexes of the array are the names of all the fields of the table. The
  1163.      *   values of the array are also associative arrays that describe the
  1164.      *   values and other properties of the table fields.
  1165.      *
  1166.      *   Here follows a list of field properties that need to be specified:
  1167.      *
  1168.      *     value:
  1169.      *           Value to be assigned to the specified field. This value may be
  1170.      *           of specified in database independent type format as this
  1171.      *           function can perform the necessary datatype conversions.
  1172.      *
  1173.      *     Default:
  1174.      *           this property is required unless the Null property
  1175.      *           is set to 1.
  1176.      *
  1177.      *     type
  1178.      *           Name of the type of the field. Currently, all types Metabase
  1179.      *           are supported except for clob and blob.
  1180.      *
  1181.      *     Default: no type conversion
  1182.      *
  1183.      *     null
  1184.      *           Boolean property that indicates that the value for this field
  1185.      *           should be set to null.
  1186.      *
  1187.      *           The default value for fields missing in INSERT queries may be
  1188.      *           specified the definition of a table. Often, the default value
  1189.      *           is already null, but since the REPLACE may be emulated using
  1190.      *           an UPDATE query, make sure that all fields of the table are
  1191.      *           listed in this function argument array.
  1192.      *
  1193.      *     Default: 0
  1194.      *
  1195.      *     key
  1196.      *           Boolean property that indicates that this field should be
  1197.      *           handled as a primary key or at least as part of the compound
  1198.      *           unique index of the table that will determine the row that will
  1199.      *           updated if it exists or inserted a new row otherwise.
  1200.      *
  1201.      *           This function will fail if no key field is specified or if the
  1202.      *           value of a key field is set to null because fields that are
  1203.      *           part of unique index they may not be null.
  1204.      *
  1205.      *     Default: 0
  1206.      *
  1207.      * @see http://dev.mysql.com/doc/refman/5.0/en/replace.html
  1208.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  1209.      */
  1210.     function replace($table$fields)
  1211.     {
  1212.         $count count($fields);
  1213.         $query $values '';
  1214.         $keys $colnum = 0;
  1215.         for (reset($fields)$colnum $countnext($fields)$colnum++{
  1216.             $name key($fields);
  1217.             if ($colnum > 0{
  1218.                 $query .= ',';
  1219.                 $values.= ',';
  1220.             }
  1221.             $query.= $this->quoteIdentifier($nametrue);
  1222.             if (isset($fields[$name]['null']&& $fields[$name]['null']{
  1223.                 $value 'NULL';
  1224.             else {
  1225.                 $type = isset($fields[$name]['type']$fields[$name]['type': null;
  1226.                 $value $this->quote($fields[$name]['value']$type);
  1227.                 if (PEAR::isError($value)) {
  1228.                     return $value;
  1229.                 }
  1230.             }
  1231.             $values.= $value;
  1232.             if (isset($fields[$name]['key']&& $fields[$name]['key']{
  1233.                 if ($value === 'NULL'{
  1234.                     return $this->raiseError(MDB2_ERROR_CANNOT_REPLACEnullnull,
  1235.                         'key value '.$name.' may not be NULL'__FUNCTION__);
  1236.                 }
  1237.                 $keys++;
  1238.             }
  1239.         }
  1240.         if ($keys == 0{
  1241.             return $this->raiseError(MDB2_ERROR_CANNOT_REPLACEnullnull,
  1242.                 'not specified which fields are keys'__FUNCTION__);
  1243.         }
  1244.  
  1245.         $connection $this->getConnection();
  1246.         if (PEAR::isError($connection)) {
  1247.             return $connection;
  1248.         }
  1249.  
  1250.         $table $this->quoteIdentifier($tabletrue);
  1251.         $query = "REPLACE INTO $table ($query) VALUES ($values)";
  1252.         $result =$this->_doQuery($querytrue$connection);
  1253.         if (PEAR::isError($result)) {
  1254.             return $result;
  1255.         }
  1256.         return $this->_affectedRows($connection$result);
  1257.     }
  1258.  
  1259.     // }}}
  1260.     // {{{ nextID()
  1261.  
  1262.     /**
  1263.      * Returns the next free id of a sequence
  1264.      *
  1265.      * @param string $seq_name name of the sequence
  1266.      * @param boolean $ondemand when true the sequence is
  1267.      *                           automatic created, if it
  1268.      *                           not exists
  1269.      *
  1270.      * @return mixed MDB2 Error Object or id
  1271.      * @access public
  1272.      */
  1273.     function nextID($seq_name$ondemand = true)
  1274.     {
  1275.         $sequence_name $this->quoteIdentifier($this->getSequenceName($seq_name)true);
  1276.         $seqcol_name $this->quoteIdentifier($this->options['seqcol_name']true);
  1277.         $query = "INSERT INTO $sequence_name ($seqcol_name) VALUES (NULL)";
  1278.         $this->pushErrorHandling(PEAR_ERROR_RETURN);
  1279.         $this->expectError(MDB2_ERROR_NOSUCHTABLE);
  1280.         $result =$this->_doQuery($querytrue);
  1281.         $this->popExpect();
  1282.         $this->popErrorHandling();
  1283.         if (PEAR::isError($result)) {
  1284.             if ($ondemand && $result->getCode(== MDB2_ERROR_NOSUCHTABLE{
  1285.                 $this->loadModule('Manager'nulltrue);
  1286.                 $result $this->manager->createSequence($seq_name);
  1287.                 if (PEAR::isError($result)) {
  1288.                     return $this->raiseError($resultnullnull,
  1289.                         'on demand sequence '.$seq_name.' could not be created'__FUNCTION__);
  1290.                 else {
  1291.                     return $this->nextID($seq_namefalse);
  1292.                 }
  1293.             }
  1294.             return $result;
  1295.         }
  1296.         $value $this->lastInsertID();
  1297.         if (is_numeric($value)) {
  1298.             $query = "DELETE FROM $sequence_name WHERE $seqcol_name < $value";
  1299.             $result =$this->_doQuery($querytrue);
  1300.             if (PEAR::isError($result)) {
  1301.                 $this->warnings['nextID: could not delete previous sequence table values from '.$seq_name;
  1302.             }
  1303.         }
  1304.         return $value;
  1305.     }
  1306.  
  1307.     // }}}
  1308.     // {{{ lastInsertID()
  1309.  
  1310.     /**
  1311.      * Returns the autoincrement ID if supported or $id or fetches the current
  1312.      * ID in a sequence called: $table.(empty($field) ? '' : '_'.$field)
  1313.      *
  1314.      * @param string $table name of the table into which a new row was inserted
  1315.      * @param string $field name of the field into which a new row was inserted
  1316.      * @return mixed MDB2 Error Object or id
  1317.      * @access public
  1318.      */
  1319.     function lastInsertID($table = null$field = null)
  1320.     {
  1321.         // not using mysql_insert_id() due to http://pear.php.net/bugs/bug.php?id=8051
  1322.         return $this->queryOne('SELECT LAST_INSERT_ID()''integer');
  1323.     }
  1324.  
  1325.     // }}}
  1326.     // {{{ currID()
  1327.  
  1328.     /**
  1329.      * Returns the current id of a sequence
  1330.      *
  1331.      * @param string $seq_name name of the sequence
  1332.      * @return mixed MDB2 Error Object or id
  1333.      * @access public
  1334.      */
  1335.     function currID($seq_name)
  1336.     {
  1337.         $sequence_name $this->quoteIdentifier($this->getSequenceName($seq_name)true);
  1338.         $seqcol_name $this->quoteIdentifier($this->options['seqcol_name']true);
  1339.         $query = "SELECT MAX($seqcol_name) FROM $sequence_name";
  1340.         return $this->queryOne($query'integer');
  1341.     }
  1342. }
  1343.  
  1344. /**
  1345.  * MDB2 MySQLi result driver
  1346.  *
  1347.  * @package MDB2
  1348.  * @category Database
  1349.  * @author  Lukas Smith <smith@pooteeweet.org>
  1350.  */
  1351. class MDB2_Result_mysqli extends MDB2_Result_Common
  1352. {
  1353.     // }}}
  1354.     // {{{ fetchRow()
  1355.  
  1356.     /**
  1357.      * Fetch a row and insert the data into an existing array.
  1358.      *
  1359.      * @param int       $fetchmode  how the array data should be indexed
  1360.      * @param int    $rownum    number of the row where the data can be found
  1361.      * @return int data array on success, a MDB2 error on failure
  1362.      * @access public
  1363.      */
  1364.     function &fetchRow($fetchmode = MDB2_FETCHMODE_DEFAULT$rownum = null)
  1365.     {
  1366.         if (!is_null($rownum)) {
  1367.             $seek $this->seek($rownum);
  1368.             if (PEAR::isError($seek)) {
  1369.                 return $seek;
  1370.             }
  1371.         }
  1372.         if ($fetchmode == MDB2_FETCHMODE_DEFAULT{
  1373.             $fetchmode $this->db->fetchmode;
  1374.         }
  1375.         if ($fetchmode MDB2_FETCHMODE_ASSOC{
  1376.             $row @mysqli_fetch_assoc($this->result);
  1377.             if (is_array($row)
  1378.                 && $this->db->options['portability'MDB2_PORTABILITY_FIX_CASE
  1379.             {
  1380.                 $row array_change_key_case($row$this->db->options['field_case']);
  1381.             }
  1382.         else {
  1383.            $row @mysqli_fetch_row($this->result);
  1384.         }
  1385.  
  1386.         if (!$row{
  1387.             if ($this->result === false{
  1388.                 $err =$this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  1389.                     'resultset has already been freed'__FUNCTION__);
  1390.                 return $err;
  1391.             }
  1392.             $null = null;
  1393.             return $null;
  1394.         }
  1395.         $mode $this->db->options['portability'MDB2_PORTABILITY_EMPTY_TO_NULL;
  1396.         $rtrim = false;
  1397.         if ($this->db->options['portability'MDB2_PORTABILITY_RTRIM{
  1398.             if (empty($this->types)) {
  1399.                 $mode += MDB2_PORTABILITY_RTRIM;
  1400.             else {
  1401.                 $rtrim = true;
  1402.             }
  1403.         }
  1404.         if ($mode{
  1405.             $this->db->_fixResultArrayValues($row$mode);
  1406.         }
  1407.         if (!empty($this->types)) {
  1408.             $row $this->db->datatype->convertResultRow($this->types$row$rtrim);
  1409.         }
  1410.         if (!empty($this->values)) {
  1411.             $this->_assignBindColumns($row);
  1412.         }
  1413.         if ($fetchmode === MDB2_FETCHMODE_OBJECT{
  1414.             $object_class $this->db->options['fetch_class'];
  1415.             if ($object_class == 'stdClass'{
  1416.                 $row = (object) $row;
  1417.             else {
  1418.                 $row &new $object_class($row);
  1419.             }
  1420.         }
  1421.         ++$this->rownum;
  1422.         return $row;
  1423.     }
  1424.  
  1425.     // }}}
  1426.     // {{{ _getColumnNames()
  1427.  
  1428.     /**
  1429.      * Retrieve the names of columns returned by the DBMS in a query result.
  1430.      *
  1431.      * @return  mixed   Array variable that holds the names of columns as keys
  1432.      *                   or an MDB2 error on failure.
  1433.      *                   Some DBMS may not return any columns when the result set
  1434.      *                   does not contain any rows.
  1435.      * @access private
  1436.      */
  1437.     function _getColumnNames()
  1438.     {
  1439.         $columns = array();
  1440.         $numcols $this->numCols();
  1441.         if (PEAR::isError($numcols)) {
  1442.             return $numcols;
  1443.         }
  1444.         for ($column = 0; $column $numcols$column++{
  1445.             $column_info @mysqli_fetch_field_direct($this->result$column);
  1446.             $columns[$column_info->name$column;
  1447.         }
  1448.         if ($this->db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  1449.             $columns array_change_key_case($columns$this->db->options['field_case']);
  1450.         }
  1451.         return $columns;
  1452.     }
  1453.  
  1454.     // }}}
  1455.     // {{{ numCols()
  1456.  
  1457.     /**
  1458.      * Count the number of columns returned by the DBMS in a query result.
  1459.      *
  1460.      * @return mixed integer value with the number of columns, a MDB2 error
  1461.      *                        on failure
  1462.      * @access public
  1463.      */
  1464.     function numCols()
  1465.     {
  1466.         $cols @mysqli_num_fields($this->result);
  1467.         if (is_null($cols)) {
  1468.             if ($this->result === false{
  1469.                 return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  1470.                     'resultset has already been freed'__FUNCTION__);
  1471.             elseif (is_null($this->result)) {
  1472.                 return count($this->types);
  1473.             }
  1474.             return $this->db->raiseError(nullnullnull,
  1475.                 'Could not get column count'__FUNCTION__);
  1476.         }
  1477.         return $cols;
  1478.     }
  1479.  
  1480.     // }}}
  1481.     // {{{ nextResult()
  1482.  
  1483.     /**
  1484.      * Move the internal result pointer to the next available result
  1485.      *
  1486.      * @return true on success, false if there is no more result set or an error object on failure
  1487.      * @access public
  1488.      */
  1489.     function nextResult()
  1490.     {
  1491.         $connection $this->db->getConnection();
  1492.         if (PEAR::isError($connection)) {
  1493.             return $connection;
  1494.         }
  1495.  
  1496.         if (!@mysqli_more_results($connection)) {
  1497.             return false;
  1498.         }
  1499.         if (!@mysqli_next_result($connection)) {
  1500.             return false;
  1501.         }
  1502.         if (!($this->result @mysqli_use_result($connection))) {
  1503.             return false;
  1504.         }
  1505.         return MDB2_OK;
  1506.     }
  1507.  
  1508.     // }}}
  1509.     // {{{ free()
  1510.  
  1511.     /**
  1512.      * Free the internal resources associated with result.
  1513.      *
  1514.      * @return boolean true on success, false if result is invalid
  1515.      * @access public
  1516.      */
  1517.     function free()
  1518.     {
  1519.         if (is_object($this->result&& $this->db->connection{
  1520.             $free @mysqli_free_result($this->result);
  1521.             if ($free === false{
  1522.                 return $this->db->raiseError(nullnullnull,
  1523.                     'Could not free result'__FUNCTION__);
  1524.             }
  1525.         }
  1526.         $this->result = false;
  1527.         return MDB2_OK;
  1528.     }
  1529. }
  1530.  
  1531. /**
  1532.  * MDB2 MySQLi buffered result driver
  1533.  *
  1534.  * @package MDB2
  1535.  * @category Database
  1536.  * @author  Lukas Smith <smith@pooteeweet.org>
  1537.  */
  1538. {
  1539.     // }}}
  1540.     // {{{ seek()
  1541.  
  1542.     /**
  1543.      * Seek to a specific row in a result set
  1544.      *
  1545.      * @param int    $rownum    number of the row where the data can be found
  1546.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  1547.      * @access public
  1548.      */
  1549.     function seek($rownum = 0)
  1550.     {
  1551.         if ($this->rownum != ($rownum - 1&& !@mysqli_data_seek($this->result$rownum)) {
  1552.             if ($this->result === false{
  1553.                 return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  1554.                     'resultset has already been freed'__FUNCTION__);
  1555.             elseif (is_null($this->result)) {
  1556.                 return MDB2_OK;
  1557.             }
  1558.             return $this->db->raiseError(MDB2_ERROR_INVALIDnullnull,
  1559.                 'tried to seek to an invalid row number ('.$rownum.')'__FUNCTION__);
  1560.         }
  1561.         $this->rownum $rownum - 1;
  1562.         return MDB2_OK;
  1563.     }
  1564.  
  1565.     // }}}
  1566.     // {{{ valid()
  1567.  
  1568.     /**
  1569.      * Check if the end of the result set has been reached
  1570.      *
  1571.      * @return mixed true or false on sucess, a MDB2 error on failure
  1572.      * @access public
  1573.      */
  1574.     function valid()
  1575.     {
  1576.         $numrows $this->numRows();
  1577.         if (PEAR::isError($numrows)) {
  1578.             return $numrows;
  1579.         }
  1580.         return $this->rownum ($numrows - 1);
  1581.     }
  1582.  
  1583.     // }}}
  1584.     // {{{ numRows()
  1585.  
  1586.     /**
  1587.      * Returns the number of rows in a result object
  1588.      *
  1589.      * @return mixed MDB2 Error Object or the number of rows
  1590.      * @access public
  1591.      */
  1592.     function numRows()
  1593.     {
  1594.         $rows @mysqli_num_rows($this->result);
  1595.         if (is_null($rows)) {
  1596.             if ($this->result === false{
  1597.                 return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  1598.                     'resultset has already been freed'__FUNCTION__);
  1599.             elseif (is_null($this->result)) {
  1600.                 return 0;
  1601.             }
  1602.             return $this->db->raiseError(nullnullnull,
  1603.                 'Could not get row count'__FUNCTION__);
  1604.         }
  1605.         return $rows;
  1606.     }
  1607.  
  1608.     // }}}
  1609.     // {{{ nextResult()
  1610.  
  1611.     /**
  1612.      * Move the internal result pointer to the next available result
  1613.      *
  1614.      * @param valid result resource
  1615.      * @return true on success, false if there is no more result set or an error object on failure
  1616.      * @access public
  1617.      */
  1618.     function nextResult()
  1619.     {
  1620.         $connection $this->db->getConnection();
  1621.         if (PEAR::isError($connection)) {
  1622.             return $connection;
  1623.         }
  1624.  
  1625.         if (!@mysqli_more_results($connection)) {
  1626.             return false;
  1627.         }
  1628.         if (!@mysqli_next_result($connection)) {
  1629.             return false;
  1630.         }
  1631.         if (!($this->result @mysqli_store_result($connection))) {
  1632.             return false;
  1633.         }
  1634.         return MDB2_OK;
  1635.     }
  1636. }
  1637.  
  1638. /**
  1639.  * MDB2 MySQLi statement driver
  1640.  *
  1641.  * @package MDB2
  1642.  * @category Database
  1643.  * @author  Lukas Smith <smith@pooteeweet.org>
  1644.  */
  1645. class MDB2_Statement_mysqli extends MDB2_Statement_Common
  1646. {
  1647.     // {{{ _execute()
  1648.  
  1649.     /**
  1650.      * Execute a prepared query statement helper method.
  1651.      *
  1652.      * @param mixed $result_class string which specifies which result class to use
  1653.      * @param mixed $result_wrap_class string which specifies which class to wrap results in
  1654.      *
  1655.      * @return mixed MDB2_Result or integer (affected rows) on success,
  1656.      *                a MDB2 error on failure
  1657.      * @access private
  1658.      */
  1659.     function &_execute($result_class = true$result_wrap_class = false)
  1660.     {
  1661.         if (is_null($this->statement)) {
  1662.             $result =parent::_execute($result_class$result_wrap_class);
  1663.             return $result;
  1664.         }
  1665.         $this->db->last_query = $this->query;
  1666.         $this->db->debug($this->query'execute'array('is_manip' => $this->is_manip'when' => 'pre''parameters' => $this->values));
  1667.         if ($this->db->getOption('disable_query')) {
  1668.             $result $this->is_manip ? 0 : null;
  1669.             return $result;
  1670.         }
  1671.  
  1672.         $connection $this->db->getConnection();
  1673.         if (PEAR::isError($connection)) {
  1674.             return $connection;
  1675.         }
  1676.  
  1677.         if (!is_object($this->statement)) {
  1678.             $query 'EXECUTE '.$this->statement;
  1679.         }
  1680.         if (!empty($this->positions)) {
  1681.             $parameters = array(0 => $this->statement1 => '');
  1682.             $lobs = array();
  1683.             $i = 0;
  1684.             foreach ($this->positions as $parameter{
  1685.                 if (!array_key_exists($parameter$this->values)) {
  1686.                     return $this->db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  1687.                         'Unable to bind to missing placeholder: '.$parameter__FUNCTION__);
  1688.                 }
  1689.                 $value $this->values[$parameter];
  1690.                 $type array_key_exists($parameter$this->types$this->types[$parameter: null;
  1691.                 if (!is_object($this->statement)) {
  1692.                     if (is_resource($value|| $type == 'clob' || $type == 'blob' && $this->db->options['lob_allow_url_include']{
  1693.                         if (!is_resource($value&& preg_match('/^(\w+:\/\/)(.*)$/'$value$match)) {
  1694.                             if ($match[1== 'file://'{
  1695.                                 $value $match[2];
  1696.                             }
  1697.                             $value @fopen($value'r');
  1698.                             $close = true;
  1699.                         }
  1700.                         if (is_resource($value)) {
  1701.                             $data '';
  1702.                             while (!@feof($value)) {
  1703.                                 $data.= @fread($value$this->db->options['lob_buffer_length']);
  1704.                             }
  1705.                             if ($close{
  1706.                                 @fclose($value);
  1707.                             }
  1708.                             $value $data;
  1709.                         }
  1710.                     }
  1711.                     $quoted $this->db->quote($value$type);
  1712.                     if (PEAR::isError($quoted)) {
  1713.                         return $quoted;
  1714.                     }
  1715.                     $param_query 'SET @'.$parameter.' = '.$quoted;
  1716.                     $result $this->db->_doQuery($param_querytrue$connection);
  1717.                     if (PEAR::isError($result)) {
  1718.                         return $result;
  1719.                     }
  1720.                 else {
  1721.                     if (is_resource($value|| $type == 'clob' || $type == 'blob'{
  1722.                         $parameters[= null;
  1723.                         $parameters[1].= 'b';
  1724.                         $lobs[$i$parameter;
  1725.                     else {
  1726.                         $quoted $this->db->quote($value$typefalse);
  1727.                         if (PEAR::isError($quoted)) {
  1728.                             return $quoted;
  1729.                         }
  1730.                         $parameters[$quoted;
  1731.                         $parameters[1].= $this->db->datatype->mapPrepareDatatype($type);
  1732.                     }
  1733.                     ++$i;
  1734.                 }
  1735.             }
  1736.  
  1737.             if (!is_object($this->statement)) {
  1738.                 $query.= ' USING @'.implode(', @'array_values($this->positions));
  1739.             else {
  1740.                 $result @call_user_func_array('mysqli_stmt_bind_param'$parameters);
  1741.                 if ($result === false{
  1742.                     $err =$this->db->raiseError(nullnullnull,
  1743.                         'Unable to bind parameters'__FUNCTION__);
  1744.                     return $err;
  1745.                 }
  1746.  
  1747.                 foreach ($lobs as $i => $parameter{
  1748.                     $value $this->values[$parameter];
  1749.                     $close = false;
  1750.                     if (!is_resource($value)) {
  1751.                         $close = true;
  1752.                         if (preg_match('/^(\w+:\/\/)(.*)$/'$value$match)) {
  1753.                             if ($match[1== 'file://'{
  1754.                                 $value $match[2];
  1755.                             }
  1756.                             $value @fopen($value'r');
  1757.                         else {
  1758.                             $fp @tmpfile();
  1759.                             @fwrite($fp$value);
  1760.                             @rewind($fp);
  1761.                             $value $fp;
  1762.                         }
  1763.                     }
  1764.                     while (!@feof($value)) {
  1765.                         $data @fread($value$this->db->options['lob_buffer_length']);
  1766.                         @mysqli_stmt_send_long_data($this->statement$i$data);
  1767.                     }
  1768.                     if ($close{
  1769.                         @fclose($value);
  1770.                     }
  1771.                 }
  1772.             }
  1773.         }
  1774.  
  1775.         if (!is_object($this->statement)) {
  1776.             $result $this->db->_doQuery($query$this->is_manip$connection);
  1777.             if (PEAR::isError($result)) {
  1778.                 return $result;
  1779.             }
  1780.  
  1781.             if ($this->is_manip{
  1782.                 $affected_rows $this->db->_affectedRows($connection$result);
  1783.                 return $affected_rows;
  1784.             }
  1785.  
  1786.             $result =$this->db->_wrapResult($result$this->result_types,
  1787.                 $result_class$result_wrap_class$this->limit$this->offset);
  1788.         else {
  1789.             if (!@mysqli_stmt_execute($this->statement)) {
  1790.                 $err =$this->db->raiseError(nullnullnull,
  1791.                     'Unable to execute statement'__FUNCTION__);
  1792.                 return $err;
  1793.             }
  1794.  
  1795.             if ($this->is_manip{
  1796.                 $affected_rows @mysqli_stmt_affected_rows($this->statement);
  1797.                 return $affected_rows;
  1798.             }
  1799.  
  1800.             if ($this->db->options['result_buffering']{
  1801.                 @mysqli_stmt_store_result($this->statement);
  1802.             }
  1803.  
  1804.             $result =$this->db->_wrapResult($this->statement$this->result_types,
  1805.                 $result_class$result_wrap_class$this->limit$this->offset);
  1806.         }
  1807.  
  1808.         $this->db->debug($this->query'execute'array('is_manip' => $this->is_manip'when' => 'post''result' => $result));
  1809.         return $result;
  1810.     }
  1811.  
  1812.     // }}}
  1813.     // {{{ free()
  1814.  
  1815.     /**
  1816.      * Release resources allocated for the specified prepared query.
  1817.      *
  1818.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  1819.      * @access public
  1820.      */
  1821.     function free()
  1822.     {
  1823.         if (is_null($this->positions)) {
  1824.             return $this->db->raiseError(MDB2_ERRORnullnull,
  1825.                 'Prepared statement has already been freed'__FUNCTION__);
  1826.         }
  1827.         $result = MDB2_OK;
  1828.  
  1829.         if (is_object($this->statement)) {
  1830.             if (!@mysqli_stmt_close($this->statement)) {
  1831.                 $result $this->db->raiseError(nullnullnull,
  1832.                     'Could not free statement'__FUNCTION__);
  1833.             }
  1834.         elseif (!is_null($this->statement)) {
  1835.             $connection $this->db->getConnection();
  1836.             if (PEAR::isError($connection)) {
  1837.                 return $connection;
  1838.             }
  1839.  
  1840.             $query 'DEALLOCATE PREPARE '.$this->statement;
  1841.             $result $this->db->_doQuery($querytrue$connection);
  1842.         }
  1843.  
  1844.         parent::free();
  1845.         return $result;
  1846.    }
  1847. }
  1848. ?>

Documentation generated on Mon, 11 Mar 2019 15:28:30 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.