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

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