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

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