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

Source for file mysql.php

Documentation is available at mysql.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@backendmedia.com>                         |
  44. // +----------------------------------------------------------------------+
  45. //
  46. // $Id: mysql.php,v 1.58 2005/03/06 18:53:19 lsmith Exp $
  47. //
  48.  
  49. /**
  50.  * MDB2 MySQL driver
  51.  *
  52.  * @package MDB2
  53.  * @category Database
  54.  * @author  Lukas Smith <smith@backendmedia.com>
  55.  */
  56. {
  57.     // {{{ properties
  58.     var $escape_quotes = "\\";
  59.  
  60.     var $dummy_primary_key = 'dummy_primary_key';
  61.  
  62.     // }}}
  63.     // {{{ constructor
  64.  
  65.     /**
  66.     * Constructor
  67.     */
  68.     function __construct()
  69.     {
  70.         parent::__construct();
  71.  
  72.         $this->phptype 'mysql';
  73.         $this->dbsyntax 'mysql';
  74.  
  75.         $this->supported['sequences'= true;
  76.         $this->supported['indexes'= true;
  77.         $this->supported['affected_rows'= true;
  78.         $this->supported['transactions'= false;
  79.         $this->supported['summary_functions'= true;
  80.         $this->supported['order_by_text'= true;
  81.         $this->supported['current_id'= true;
  82.         $this->supported['limit_queries'= true;
  83.         $this->supported['LOBs'= true;
  84.         $this->supported['replace'= true;
  85.         $this->supported['sub_selects'= false;
  86.         $this->supported['auto_increment'= true;
  87.  
  88.         $this->options['default_table_type'= null;
  89.     }
  90.  
  91.     // }}}
  92.     // {{{ errorInfo()
  93.  
  94.     /**
  95.      * This method is used to collect information about an error
  96.      *
  97.      * @param integer $error 
  98.      * @return array 
  99.      * @access public
  100.      */
  101.     function errorInfo($error = null)
  102.     {
  103.         if ($this->connection{
  104.             $native_code @mysql_errno($this->connection);
  105.             $native_msg  @mysql_error($this->connection);
  106.         else {
  107.             $native_code @mysql_errno();
  108.             $native_msg  @mysql_error();
  109.         }
  110.         if (is_null($error)) {
  111.             static $ecode_map;
  112.             if (empty($ecode_map)) {
  113.                 $ecode_map = array(
  114.                     1004 => MDB2_ERROR_CANNOT_CREATE,
  115.                     1005 => MDB2_ERROR_CANNOT_CREATE,
  116.                     1006 => MDB2_ERROR_CANNOT_CREATE,
  117.                     1007 => MDB2_ERROR_ALREADY_EXISTS,
  118.                     1008 => MDB2_ERROR_CANNOT_DROP,
  119.                     1022 => MDB2_ERROR_ALREADY_EXISTS,
  120.                     1044 => MDB2_ERROR_ACCESS_VIOLATION,
  121.                     1046 => MDB2_ERROR_NODBSELECTED,
  122.                     1048 => MDB2_ERROR_CONSTRAINT,
  123.                     1049 => MDB2_ERROR_NOSUCHDB,
  124.                     1050 => MDB2_ERROR_ALREADY_EXISTS,
  125.                     1051 => MDB2_ERROR_NOSUCHTABLE,
  126.                     1054 => MDB2_ERROR_NOSUCHFIELD,
  127.                     1061 => MDB2_ERROR_ALREADY_EXISTS,
  128.                     1062 => MDB2_ERROR_ALREADY_EXISTS,
  129.                     1064 => MDB2_ERROR_SYNTAX,
  130.                     1091 => MDB2_ERROR_NOT_FOUND,
  131.                     1100 => MDB2_ERROR_NOT_LOCKED,
  132.                     1136 => MDB2_ERROR_VALUE_COUNT_ON_ROW,
  133.                     1142 => MDB2_ERROR_ACCESS_VIOLATION,
  134.                     1146 => MDB2_ERROR_NOSUCHTABLE,
  135.                     1216 => MDB2_ERROR_CONSTRAINT,
  136.                     1217 => MDB2_ERROR_CONSTRAINT,
  137.                 );
  138.             }
  139.             if ($this->options['portability'MDB2_PORTABILITY_ERRORS{
  140.                 $ecode_map[1022MDB2_ERROR_CONSTRAINT;
  141.                 $ecode_map[1048MDB2_ERROR_CONSTRAINT_NOT_NULL;
  142.                 $ecode_map[1062MDB2_ERROR_CONSTRAINT;
  143.             else {
  144.                 // Doing this in case mode changes during runtime.
  145.                 $ecode_map[1022MDB2_ERROR_ALREADY_EXISTS;
  146.                 $ecode_map[1048MDB2_ERROR_CONSTRAINT;
  147.                 $ecode_map[1062MDB2_ERROR_ALREADY_EXISTS;
  148.             }
  149.             if (isset($ecode_map[$native_code])) {
  150.                 $error $ecode_map[$native_code];
  151.             }
  152.         }
  153.         return array($error$native_code$native_msg);
  154.     }
  155.  
  156.     // }}}
  157.     // {{{ escape()
  158.  
  159.     /**
  160.      * Quotes a string so it can be safely used in a query. It will quote
  161.      * the text so it can safely be used within a query.
  162.      *
  163.      * @param string $text the input string to quote
  164.      * @return string quoted string
  165.      * @access public
  166.      */
  167.     function escape($text)
  168.     {
  169.         return @mysql_escape_string($text);
  170.     }
  171.  
  172.     // }}}
  173.     // {{{ quoteIdentifier()
  174.  
  175.     /**
  176.      * Quote a string so it can be safely used as a table or column name
  177.      *
  178.      * Quoting style depends on which database driver is being used.
  179.      *
  180.      * MySQL can't handle the backtick character (<kbd>`</kbd>) in
  181.      * table or column names.
  182.      *
  183.      * @param string $str  identifier name to be quoted
  184.      *
  185.      * @return string  quoted identifier string
  186.      *
  187.      * @access public
  188.      * @internal
  189.      */
  190.     function quoteIdentifier($str)
  191.     {
  192.         return '`' $str '`';
  193.     }
  194.  
  195.     // }}}
  196.     // {{{ beginTransaction()
  197.  
  198.     /**
  199.      * Start a transaction.
  200.      *
  201.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  202.      * @access public
  203.      */
  204.     function beginTransaction()
  205.     {
  206.         $this->debug('starting transaction''beginTransaction');
  207.         if (!$this->supports('transactions')) {
  208.             return $this->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  209.                 'beginTransaction: transactions are not in use');
  210.         }
  211.         if ($this->in_transaction{
  212.             return MDB2_OK;  //nothing to do
  213.         }
  214.         if (!$this->destructor_registered && $this->opened_persistent{
  215.             $this->destructor_registered = true;
  216.             register_shutdown_function('MDB2_closeOpenTransactions');
  217.         }
  218.         $result $this->_doQuery('SET AUTOCOMMIT = 0'true);
  219.         if (MDB2::isError($result)) {
  220.             return $result;
  221.         }
  222.         $this->in_transaction = true;
  223.         return MDB2_OK;
  224.     }
  225.  
  226.     // }}}
  227.     // {{{ commit()
  228.  
  229.     /**
  230.      * Commit the database changes done during a transaction that is in
  231.      * progress.
  232.      *
  233.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  234.      * @access public
  235.      */
  236.     function commit()
  237.     {
  238.         $this->debug('commit transaction''commit');
  239.         if (!$this->supports('transactions')) {
  240.             return $this->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  241.                 'commit: transactions are not in use');
  242.         }
  243.         if (!$this->in_transaction{
  244.             return $this->raiseError(MDB2_ERRORnullnull,
  245.                 'commit: transaction changes are being auto committed');
  246.         }
  247.         $result $this->_doQuery('COMMIT'true);
  248.         if (MDB2::isError($result)) {
  249.             return $result;
  250.         }
  251.         $result $this->_doQuery('SET AUTOCOMMIT = 1'true);
  252.         if (MDB2::isError($result)) {
  253.             return $result;
  254.         }
  255.         $this->in_transaction = false;
  256.         return MDB2_OK;
  257.     }
  258.  
  259.     // }}}
  260.     // {{{ rollback()
  261.  
  262.     /**
  263.      * Cancel any database changes done during a transaction that is in
  264.      * progress.
  265.      *
  266.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  267.      * @access public
  268.      */
  269.     function rollback()
  270.     {
  271.         $this->debug('rolling back transaction''rollback');
  272.         if (!$this->supports('transactions')) {
  273.             return $this->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  274.                 'rollback: transactions are not in use');
  275.         }
  276.         if (!$this->in_transaction{
  277.             return $this->raiseError(MDB2_ERRORnullnull,
  278.                 'rollback: transactions can not be rolled back when changes are auto committed');
  279.         }
  280.         $result $this->_doQuery('ROLLBACK'true);
  281.         if (MDB2::isError($result)) {
  282.             return $result;
  283.         }
  284.         $result $this->_doQuery('SET AUTOCOMMIT = 1'true);
  285.         if (MDB2::isError($result)) {
  286.             return $result;
  287.         }
  288.         $this->in_transaction = false;
  289.         return MDB2_OK;
  290.  
  291.     }
  292.  
  293.     // }}}
  294.     // {{{ connect()
  295.  
  296.     /**
  297.      * Connect to the database
  298.      *
  299.      * @return true on success, MDB2 Error Object on failure
  300.      */
  301.     function connect()
  302.     {
  303.         if (is_resource($this->connection)) {
  304.             if (count(array_diff($this->connected_dsn$this->dsn)) == 0
  305.                 && $this->opened_persistent == $this->options['persistent']
  306.             {
  307.                 return MDB2_OK;
  308.             }
  309.             $this->disconnect(false);
  310.         }
  311.  
  312.         if (!PEAR::loadExtension($this->phptype)) {
  313.             return $this->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  314.                 'connect: extension '.$this->phptype.' is not compiled into PHP');
  315.         }
  316.  
  317.         $params = array();
  318.         if ($this->dsn['protocol'&& $this->dsn['protocol'== 'unix'{
  319.             $params[0':' $this->dsn['socket'];
  320.         else {
  321.             $params[0$this->dsn['hostspec'$this->dsn['hostspec']
  322.                          : 'localhost';
  323.             if ($this->dsn['port']{
  324.                 $params[0.= ':' $this->dsn['port'];
  325.             }
  326.         }
  327.         $params[$this->dsn['username'$this->dsn['username': null;
  328.         $params[$this->dsn['password'$this->dsn['password': null;
  329.         if (isset($this->dsn['new_link'])
  330.             && ($this->dsn['new_link'== 'true' || $this->dsn['new_link'=== true))
  331.         {
  332.             $params[= true;
  333.         else {
  334.             $params[= false;
  335.         }
  336.         if (version_compare(phpversion()'4.3.0''>=')) {
  337.             $params[= isset($this->dsn['client_flags'])
  338.                         ? $this->dsn['client_flags': null;
  339.         }
  340.  
  341.         $connect_function $this->options['persistent''mysql_pconnect' 'mysql_connect';
  342.  
  343.         @ini_set('track_errors'true);
  344.         $php_errormsg '';
  345.         $connection @call_user_func_array($connect_function$params);
  346.         @ini_restore('track_errors');
  347.         if (!$connection{
  348.             if (($err @mysql_error()) != ''{
  349.                 return $this->raiseError(MDB2_ERROR_CONNECT_FAILEDnullnull$err);
  350.             else {
  351.                 return $this->raiseError(MDB2_ERROR_CONNECT_FAILEDnullnull$php_errormsg);
  352.             }
  353.         }
  354.  
  355.         $this->connection $connection;
  356.         $this->connected_dsn $this->dsn;
  357.         $this->connected_database_name '';
  358.         $this->opened_persistent $this->options['persistent'];
  359.         $this->dbsyntax $this->dsn['dbsyntax'$this->dsn['dbsyntax'$this->phptype;
  360.  
  361.         $this->supported['transactions'= false;
  362.         if ($this->options['default_table_type']{
  363.             switch (strtoupper($this->options['default_table_type'])) {
  364.             case 'BERKELEYDB':
  365.                 $this->options['default_table_type''BDB';
  366.             case 'BDB':
  367.             case 'INNODB':
  368.             case 'GEMINI':
  369.                 $this->supported['transactions'= true;
  370.                 break;
  371.             case 'HEAP':
  372.             case 'ISAM':
  373.             case 'MERGE':
  374.             case 'MRG_MYISAM':
  375.             case 'MYISAM':
  376.                 break;
  377.             default:
  378.                 $this->warnings[$default_table_type.
  379.                     ' is not a supported default table type';
  380.             }
  381.         }
  382.  
  383.         if ($this->options['use_transactions'&& !$this->supports('transactions')) {
  384.             $this->warnings[$this->options['default_table_type'].
  385.                 ' is not a transaction-safe default table type; switched to INNODB';
  386.             $this->options['default_table_type''INNODB';
  387.             $this->supported['transactions'= true;
  388.         }
  389.  
  390.         return MDB2_OK;
  391.     }
  392.  
  393.     // }}}
  394.     // {{{ disconnect()
  395.  
  396.     /**
  397.      * Log out and disconnect from the database.
  398.      *
  399.      * @return mixed true on success, false if not connected and error
  400.      *                 object on error
  401.      * @access public
  402.      */
  403.     function disconnect($force = true)
  404.     {
  405.         if (is_resource($this->connection)) {
  406.             if (!$this->opened_persistent || $force{
  407.                 @mysql_close($this->connection);
  408.             }
  409.             $this->connection = 0;
  410.         }
  411.         return MDB2_OK;
  412.     }
  413.  
  414.     // }}}
  415.     // {{{ _doQuery()
  416.  
  417.     /**
  418.      * Execute a query
  419.      * @param string $query  query
  420.      * @param boolean $isManip  if the query is a manipulation query
  421.      * @param resource $connection 
  422.      * @param string $database_name 
  423.      * @return result or error object
  424.      * @access private
  425.      */
  426.     function _doQuery($query$isManip = false$connection = null$database_name = null)
  427.     {
  428.         $this->last_query = $query;
  429.         $this->debug($query'query');
  430.         if ($this->options['disable_query']{
  431.             if ($isManip{
  432.                 return MDB2_OK;
  433.             }
  434.             return null;
  435.         }
  436.  
  437.         if (is_null($connection)) {
  438.             $error $this->connect();
  439.             if (MDB2::isError($error)) {
  440.                 return $error;
  441.             }
  442.             $connection $this->connection;
  443.         }
  444.         if (is_null($database_name)) {
  445.             $database_name $this->database_name;
  446.         }
  447.  
  448.         if ($database_name{
  449.             if ($database_name != $this->connected_database_name{
  450.                 if (!@mysql_select_db($database_name$connection)) {
  451.                     return $this->raiseError();
  452.                 }
  453.                 $this->connected_database_name $database_name;
  454.             }
  455.         }
  456.  
  457.         $function $this->options['result_buffering']
  458.             ? 'mysql_query' 'mysql_unbuffered_query';
  459.         $result @$function($query$connection);
  460.         if (!$result{
  461.             return $this->raiseError();
  462.         }
  463.  
  464.         if ($isManip{
  465.             return @mysql_affected_rows($connection);
  466.         }
  467.         return $result;
  468.     }
  469.  
  470.     // }}}
  471.     // {{{ _modifyQuery()
  472.  
  473.     /**
  474.      * Changes a query string for various DBMS specific reasons
  475.      *
  476.      * @param string $query  query to modify
  477.      * @return the new (modified) query
  478.      * @access private
  479.      */
  480.     function _modifyQuery($query$isManip$limit$offset)
  481.     {
  482.         if ($this->options['portability'MDB2_PORTABILITY_DELETE_COUNT{
  483.             // "DELETE FROM table" gives 0 affected rows in MySQL.
  484.             // This little hack lets you know how many rows were deleted.
  485.             if (preg_match('/^\s*DELETE\s+FROM\s+(\S+)\s*$/i'$query)) {
  486.                 $query preg_replace('/^\s*DELETE\s+FROM\s+(\S+)\s*$/',
  487.                                       'DELETE FROM \1 WHERE 1=1'$query);
  488.             }
  489.         }
  490.         if ($limit > 0
  491.             && !preg_match('/LIMIT\s*\d(\s*(,|OFFSET)\s*\d+)?/i'$query)
  492.         {
  493.             $query rtrim($query);
  494.             if (substr($query-1== ';'{
  495.                 $query substr($query0-1);
  496.             }
  497.             if ($isManip{
  498.                 return $query . " LIMIT $limit";
  499.             else {
  500.                 return $query . " LIMIT $offset$limit";
  501.             }
  502.         }
  503.         return $query;
  504.     }
  505.  
  506.     // }}}
  507.     // {{{ subSelect()
  508.  
  509.     /**
  510.      * simple subselect emulation for Mysql
  511.      *
  512.      * @access public
  513.      *
  514.      * @param string $query the SQL query for the subselect that may only
  515.      *                       return a column
  516.      * @param string $type determines type of the field
  517.      *
  518.      * @return string comma seperated values
  519.      */
  520.     function subSelect($query$type = false)
  521.     {
  522.         if ($this->supports('sub_selects')) {
  523.             return $query;
  524.         }
  525.         $col $this->queryCol($query$type);
  526.         if (!is_array($col|| count($col== 0{
  527.             return 'NULL';
  528.         }
  529.         if ($type{
  530.             $this->loadModule('Datatype');
  531.             return $this->datatype->implodeArray($col$type);
  532.         }
  533.         return implode(', '$col);
  534.     }
  535.  
  536.     // }}}
  537.     // {{{ replace()
  538.  
  539.     /**
  540.      * Execute a SQL REPLACE query. A REPLACE query is identical to a INSERT
  541.      * query, except that if there is already a row in the table with the same
  542.      * key field values, the REPLACE query just updates its values instead of
  543.      * inserting a new row.
  544.      *
  545.      * The REPLACE type of query does not make part of the SQL standards. Since
  546.      * practically only MySQL implements it natively, this type of query is
  547.      * emulated through this method for other DBMS using standard types of
  548.      * queries inside a transaction to assure the atomicity of the operation.
  549.      *
  550.      * @access public
  551.      *
  552.      * @param string $table name of the table on which the REPLACE query will
  553.      *   be executed.
  554.      * @param array $fields associative array that describes the fields and the
  555.      *   values that will be inserted or updated in the specified table. The
  556.      *   indexes of the array are the names of all the fields of the table. The
  557.      *   values of the array are also associative arrays that describe the
  558.      *   values and other properties of the table fields.
  559.      *
  560.      *   Here follows a list of field properties that need to be specified:
  561.      *
  562.      *     value:
  563.      *           Value to be assigned to the specified field. This value may be
  564.      *           of specified in database independent type format as this
  565.      *           function can perform the necessary datatype conversions.
  566.      *
  567.      *     Default:
  568.      *           this property is required unless the Null property
  569.      *           is set to 1.
  570.      *
  571.      *     type
  572.      *           Name of the type of the field. Currently, all types Metabase
  573.      *           are supported except for clob and blob.
  574.      *
  575.      *     Default: no type conversion
  576.      *
  577.      *     null
  578.      *           Boolean property that indicates that the value for this field
  579.      *           should be set to null.
  580.      *
  581.      *           The default value for fields missing in INSERT queries may be
  582.      *           specified the definition of a table. Often, the default value
  583.      *           is already null, but since the REPLACE may be emulated using
  584.      *           an UPDATE query, make sure that all fields of the table are
  585.      *           listed in this function argument array.
  586.      *
  587.      *     Default: 0
  588.      *
  589.      *     key
  590.      *           Boolean property that indicates that this field should be
  591.      *           handled as a primary key or at least as part of the compound
  592.      *           unique index of the table that will determine the row that will
  593.      *           updated if it exists or inserted a new row otherwise.
  594.      *
  595.      *           This function will fail if no key field is specified or if the
  596.      *           value of a key field is set to null because fields that are
  597.      *           part of unique index they may not be null.
  598.      *
  599.      *     Default: 0
  600.      *
  601.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  602.      */
  603.     function replace($table$fields)
  604.     {
  605.         $count count($fields);
  606.         $query $values '';
  607.         $keys $colnum = 0;
  608.         for (reset($fields)$colnum $countnext($fields)$colnum++{
  609.             $name key($fields);
  610.             if ($colnum > 0{
  611.                 $query  .= ',';
  612.                 $values .= ',';
  613.             }
  614.             $query .= $name;
  615.             if (isset($fields[$name]['null']&& $fields[$name]['null']{
  616.                 $value 'NULL';
  617.             else {
  618.                 $value $this->quote($fields[$name]['value']$fields[$name]['type']);
  619.             }
  620.             $values .= $value;
  621.             if (isset($fields[$name]['key']&& $fields[$name]['key']{
  622.                 if ($value === 'NULL'{
  623.                     return $this->raiseError(MDB2_ERROR_CANNOT_REPLACEnullnull,
  624.                         'replace: key value '.$name.' may not be NULL');
  625.                 }
  626.                 $keys++;
  627.             }
  628.         }
  629.         if ($keys == 0{
  630.             return $this->raiseError(MDB2_ERROR_CANNOT_REPLACEnullnull,
  631.                 'replace: not specified which fields are keys');
  632.         }
  633.         $query = "REPLACE INTO $table ($query) VALUES ($values)";
  634.         $this->last_query = $query;
  635.         $this->debug($query'query');
  636.         return $this->_doQuery($querytrue);
  637.     }
  638.  
  639.     // }}}
  640.     // {{{ nextID()
  641.  
  642.     /**
  643.      * returns the next free id of a sequence
  644.      *
  645.      * @param string  $seq_name name of the sequence
  646.      * @param boolean $ondemand when true the seqence is
  647.      *                           automatic created, if it
  648.      *                           not exists
  649.      *
  650.      * @return mixed MDB2 Error Object or id
  651.      * @access public
  652.      */
  653.     function nextID($seq_name$ondemand = true)
  654.     {
  655.         $sequence_name $this->getSequenceName($seq_name);
  656.         $query = "INSERT INTO $sequence_name (".$this->options['seqname_col_name'].") VALUES (NULL)";
  657.         $this->expectError(MDB2_ERROR_NOSUCHTABLE);
  658.         $result $this->_doQuery($querytrue);
  659.         $this->popExpect();
  660.         if (MDB2::isError($result)) {
  661.             if ($ondemand && $result->getCode(== MDB2_ERROR_NOSUCHTABLE{
  662.                 $this->loadModule('Manager');
  663.                 // Since we are creating the sequence on demand
  664.                 // we know the first id = 1 so initialize the
  665.                 // sequence at 2
  666.                 $result $this->manager->createSequence($seq_name2);
  667.                 if (MDB2::isError($result)) {
  668.                     return $this->raiseError(MDB2_ERRORnullnull,
  669.                         'nextID: on demand sequence '.$seq_name.' could not be created');
  670.                 else {
  671.                     // First ID of a newly created sequence is 1
  672.                     return 1;
  673.                 }
  674.             }
  675.             return $result;
  676.         }
  677.         $value $this->queryOne('SELECT LAST_INSERT_ID()''integer');
  678.         if (is_numeric($value)) {
  679.             $query = "DELETE FROM $sequence_name WHERE ".$this->options['seqname_col_name']." < $value";
  680.             $result $this->_doQuery($querytrue);
  681.             if (MDB2::isError($result)) {
  682.                 $this->warnings['nextID: could not delete previous sequence table values from '.$seq_name;
  683.             }
  684.         }
  685.         return $value;
  686.     }
  687.  
  688.     // }}}
  689.     // {{{ getAfterID()
  690.  
  691.     /**
  692.      * returns the autoincrement ID if supported or $id
  693.      *
  694.      * @param mixed $id value as returned by getBeforeId()
  695.      * @param string $table name of the table into which a new row was inserted
  696.      * @return mixed MDB2 Error Object or id
  697.      * @access public
  698.      */
  699.     function getAfterID($id$table)
  700.     {
  701.         $this->loadModule('Native');
  702.         return $this->native->getInsertID();
  703.     }
  704.  
  705.     // }}}
  706.     // {{{ currID()
  707.  
  708.     /**
  709.      * returns the current id of a sequence
  710.      *
  711.      * @param string  $seq_name name of the sequence
  712.      * @return mixed MDB2 Error Object or id
  713.      * @access public
  714.      */
  715.     function currID($seq_name)
  716.     {
  717.         $sequence_name $this->getSequenceName($seq_name);
  718.         return $this->queryOne("SELECT MAX(".$this->options['seqname_col_name'].") FROM $sequence_name"'integer');
  719.     }
  720. }
  721.  
  722. class MDB2_Result_mysql extends MDB2_Result_Common
  723. {
  724.     // }}}
  725.     // {{{ fetchRow()
  726.  
  727.     /**
  728.      * Fetch a row and insert the data into an existing array.
  729.      *
  730.      * @param int       $fetchmode  how the array data should be indexed
  731.      * @param int    $rownum    number of the row where the data can be found
  732.      * @return int data array on success, a MDB2 error on failure
  733.      * @access public
  734.      */
  735.     function &fetchRow($fetchmode = MDB2_FETCHMODE_DEFAULT$rownum = null)
  736.     {
  737.         if (!is_null($rownum)) {
  738.             $seek $this->seek($rownum);
  739.             if (MDB2::isError($seek)) {
  740.                 return $seek;
  741.             }
  742.         }
  743.         if ($fetchmode == MDB2_FETCHMODE_DEFAULT{
  744.             $fetchmode $this->db->fetchmode;
  745.         }
  746.         if ($fetchmode MDB2_FETCHMODE_ASSOC{
  747.             $row @mysql_fetch_assoc($this->result);
  748.             if (is_array($row)
  749.                 && $this->db->options['portability'MDB2_PORTABILITY_LOWERCASE
  750.             {
  751.                 $row array_change_key_case($rowCASE_LOWER);
  752.             }
  753.         else {
  754.            $row @mysql_fetch_row($this->result);
  755.         }
  756.  
  757.         if (!$row{
  758.             if (is_null($this->result)) {
  759.                 return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  760.                     'fetchRow: resultset has already been freed');
  761.             }
  762.             return null;
  763.         }
  764.         if ($this->db->options['portability'MDB2_PORTABILITY_EMPTY_TO_NULL{
  765.             $this->db->_convertEmptyArrayValuesToNull($row);
  766.         }
  767.         if (isset($this->types)) {
  768.             $row $this->db->datatype->convertResultRow($this->types$row);
  769.         }
  770.         if ($fetchmode === MDB2_FETCHMODE_OBJECT{
  771.             $object_class $this->db->options['fetch_class'];
  772.             if ($object_class == 'stdClass'{
  773.                 $row = (object) $row;
  774.             else {
  775.                 $row &new $object_class($row);
  776.             }
  777.         }
  778.         ++$this->rownum;
  779.         return $row;
  780.     }
  781.  
  782.     // }}}
  783.     // {{{ getColumnNames()
  784.  
  785.     /**
  786.      * Retrieve the names of columns returned by the DBMS in a query result.
  787.      *
  788.      * @return mixed                an associative array variable
  789.      *                               that will hold the names of columns. The
  790.      *                               indexes of the array are the column names
  791.      *                               mapped to lower case and the values are the
  792.      *                               respective numbers of the columns starting
  793.      *                               from 0. Some DBMS may not return any
  794.      *                               columns when the result set does not
  795.      *                               contain any rows.
  796.      *
  797.      *                               a MDB2 error on failure
  798.      * @access public
  799.      */
  800.     function getColumnNames()
  801.     {
  802.         $columns = array();
  803.         $numcols $this->numCols();
  804.         if (MDB2::isError($numcols)) {
  805.             return $numcols;
  806.         }
  807.         for ($column = 0; $column $numcols$column++{
  808.             $column_name @mysql_field_name($this->result$column);
  809.             $columns[$column_name$column;
  810.         }
  811.         if ($this->db->options['portability'MDB2_PORTABILITY_LOWERCASE{
  812.             $columns array_change_key_case($columnsCASE_LOWER);
  813.         }
  814.         return $columns;
  815.     }
  816.  
  817.     // }}}
  818.     // {{{ numCols()
  819.  
  820.     /**
  821.      * Count the number of columns returned by the DBMS in a query result.
  822.      *
  823.      * @return mixed integer value with the number of columns, a MDB2 error
  824.      *                        on failure
  825.      * @access public
  826.      */
  827.     function numCols()
  828.     {
  829.         $cols @mysql_num_fields($this->result);
  830.         if (is_null($cols)) {
  831.             if (is_null($this->result)) {
  832.                 return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  833.                     'numCols: resultset has already been freed');
  834.             }
  835.             return $this->db->raiseError();
  836.         }
  837.         return $cols;
  838.     }
  839.  
  840.     // }}}
  841.     // {{{ free()
  842.  
  843.     /**
  844.      * Free the internal resources associated with result.
  845.      *
  846.      * @return boolean true on success, false if result is invalid
  847.      * @access public
  848.      */
  849.     function free()
  850.     {
  851.         $free @mysql_free_result($this->result);
  852.         if (!$free{
  853.             if (is_null($this->result)) {
  854.                 return MDB2_OK;
  855.             }
  856.             return $this->db->raiseError();
  857.         }
  858.         $this->result = null;
  859.         return MDB2_OK;
  860.     }
  861. }
  862.  
  863. class MDB2_BufferedResult_mysql extends MDB2_Result_mysql
  864. {
  865.     // }}}
  866.     // {{{ seek()
  867.  
  868.     /**
  869.     * seek to a specific row in a result set
  870.     *
  871.     * @param int    $rownum    number of the row where the data can be found
  872.     * @return mixed MDB2_OK on success, a MDB2 error on failure
  873.     * @access public
  874.     */
  875.     function seek($rownum = 0)
  876.     {
  877.         if ($this->rownum != ($rownum - 1&& !@mysql_data_seek($this->result$rownum)) {
  878.             if (is_null($this->result)) {
  879.                 return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  880.                     'seek: resultset has already been freed');
  881.             }
  882.             return $this->db->raiseError(MDB2_ERROR_INVALIDnullnull,
  883.                 'seek: tried to seek to an invalid row number ('.$rownum.')');
  884.         }
  885.         $this->rownum $rownum - 1;
  886.         return MDB2_OK;
  887.     }
  888.  
  889.     // }}}
  890.     // {{{ valid()
  891.  
  892.     /**
  893.     * check if the end of the result set has been reached
  894.     *
  895.     * @return mixed true or false on sucess, a MDB2 error on failure
  896.     * @access public
  897.     */
  898.     function valid()
  899.     {
  900.         $numrows $this->numRows();
  901.         if (MDB2::isError($numrows)) {
  902.             return $numrows;
  903.         }
  904.         return $this->rownum ($numrows - 1);
  905.     }
  906.  
  907.     // }}}
  908.     // {{{ numRows()
  909.  
  910.     /**
  911.     * returns the number of rows in a result object
  912.     *
  913.     * @return mixed MDB2 Error Object or the number of rows
  914.     * @access public
  915.     */
  916.     function numRows()
  917.     {
  918.         $rows @mysql_num_rows($this->result);
  919.         if (is_null($rows)) {
  920.             if (is_null($this->result)) {
  921.                 return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  922.                     'numRows: resultset has already been freed');
  923.             }
  924.             return $this->raiseError();
  925.         }
  926.         return $rows;
  927.     }
  928. }
  929.  
  930. class MDB2_Statement_mysql extends MDB2_Statement_Common
  931. {
  932.  
  933. }
  934. ?>

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