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

Documentation generated on Mon, 11 Mar 2019 14:48:09 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.