home *** CD-ROM | disk | FTP | other *** search
/ PC World 2003 March / PCWorld_2003-03_cd.bin / Software / Vyzkuste / phptriad / phptriad2-2-1.exe / php / pear / DB / mysql.php < prev    next >
Encoding:
PHP Script  |  2001-11-13  |  24.9 KB  |  788 lines

  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP version 4.0                                                      |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2001 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.02 of the PHP license,      |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Stig Bakken <ssb@fast.no>                                   |
  17. // |                                                                      |
  18. // +----------------------------------------------------------------------+
  19. //
  20. // $Id: mysql.php,v 1.69.2.3 2001/11/13 01:26:42 ssb Exp $
  21. //
  22. // Database independent query interface definition for PHP's MySQL
  23. // extension.
  24. //
  25.  
  26. //
  27. // XXX legend:
  28. //
  29. // XXX ERRORMSG: The error message from the mysql function should
  30. //               be registered here.
  31. //
  32.  
  33. require_once "DB/common.php";
  34.  
  35. // global var to detect if the database has changed or not
  36. $GLOBALS['_DB_mysql_changed_database'] = false;
  37.  
  38. class DB_mysql extends DB_common
  39. {
  40.     // {{{ properties
  41.  
  42.     var $connection;
  43.     var $phptype, $dbsyntax;
  44.     var $prepare_tokens = array();
  45.     var $prepare_types = array();
  46.     var $num_rows = array();
  47.     var $transaction_opcount = 0;
  48.     var $autocommit = true;
  49.     var $fetchmode = DB_FETCHMODE_ORDERED; /* Default fetch mode */
  50.     var $_changed_db = false;
  51.  
  52.     // }}}
  53.     // {{{ constructor
  54.  
  55.     /**
  56.      * DB_mysql constructor.
  57.      *
  58.      * @access public
  59.      */
  60.  
  61.     function DB_mysql()
  62.     {
  63.         $this->DB_common();
  64.         $this->phptype = 'mysql';
  65.         $this->dbsyntax = 'mysql';
  66.         $this->features = array(
  67.             'prepare' => false,
  68.             'pconnect' => true,
  69.             'transactions' => true,
  70.             'limit' => 'alter'
  71.         );
  72.         $this->errorcode_map = array(
  73.             1004 => DB_ERROR_CANNOT_CREATE,
  74.             1005 => DB_ERROR_CANNOT_CREATE,
  75.             1006 => DB_ERROR_CANNOT_CREATE,
  76.             1007 => DB_ERROR_ALREADY_EXISTS,
  77.             1008 => DB_ERROR_CANNOT_DROP,
  78.             1046 => DB_ERROR_NODBSELECTED,
  79.             1050 => DB_ERROR_ALREADY_EXISTS,
  80.             1051 => DB_ERROR_NOSUCHTABLE,
  81.             1054 => DB_ERROR_NOSUCHFIELD,
  82.             1062 => DB_ERROR_ALREADY_EXISTS,
  83.             1064 => DB_ERROR_SYNTAX,
  84.             1100 => DB_ERROR_NOT_LOCKED,
  85.             1136 => DB_ERROR_VALUE_COUNT_ON_ROW,
  86.             1146 => DB_ERROR_NOSUCHTABLE,
  87.         );
  88.     }
  89.  
  90.     // }}}
  91.  
  92.     // {{{ connect()
  93.  
  94.     /**
  95.      * Connect to a database and log in as the specified user.
  96.      *
  97.      * @param $dsn the data source name (see DB::parseDSN for syntax)
  98.      * @param $persistent (optional) whether the connection should
  99.      *        be persistent
  100.      * @access public
  101.      * @return int DB_OK on success, a DB error on failure
  102.      */
  103.  
  104.     function connect($dsninfo, $persistent = false)
  105.     {
  106.         if (!DB::assertExtension('mysql'))
  107.             return $this->raiseError(DB_ERROR_EXTENSION_NOT_FOUND);
  108.  
  109.         $this->dsn = $dsninfo;
  110.         $dbhost = $dsninfo['hostspec'] ? $dsninfo['hostspec'] : 'localhost';
  111.         $user = $dsninfo['username'];
  112.         $pw = $dsninfo['password'];
  113.  
  114.         $connect_function = $persistent ? 'mysql_pconnect' : 'mysql_connect';
  115.  
  116.         @ini_set('track_errors', true);
  117.         if ($dbhost && $user && $pw) {
  118.             $conn = @$connect_function($dbhost, $user, $pw);
  119.         } elseif ($dbhost && $user) {
  120.             $conn = @$connect_function($dbhost, $user);
  121.         } elseif ($dbhost) {
  122.             $conn = @$connect_function($dbhost);
  123.         } else {
  124.             $conn = false;
  125.         }
  126.         @ini_restore('track_errors');
  127.         if (empty($conn)) {
  128.             if (($err = @mysql_error()) != '') {
  129.                 return $this->raiseError(DB_ERROR_CONNECT_FAILED, null, null,
  130.                                          null, $err);
  131.             } elseif (empty($php_errormsg)) {
  132.                 return $this->raiseError(DB_ERROR_CONNECT_FAILED);
  133.             } else {
  134.                 return $this->raiseError(DB_ERROR_CONNECT_FAILED, null, null,
  135.                                          null, $php_errormsg);
  136.             }
  137.         }
  138.  
  139.         if ($dsninfo['database']) {
  140.             // fix to allow calls to different databases in the same script
  141.             if (empty($GLOBALS['_DB_mysql_changed_database'])) {
  142.                 $this->_changed_db = false;
  143.                 if (!@mysql_select_db($dsninfo['database'], $conn)) {
  144.                     return $this->raiseError(DB_ERROR_NODBSELECTED, null, null,
  145.                                              null, mysql_error($conn));
  146.                 }
  147.             } else {
  148.                 $GLOBALS['_DB_mysql_changed_database'] = true;
  149.                 $this->_changed_db = $dsninfo['database'];
  150.             }
  151.         }
  152.  
  153.         $this->connection = $conn;
  154.         return DB_OK;
  155.     }
  156.  
  157.     // }}}
  158.     // {{{ disconnect()
  159.  
  160.     /**
  161.      * Log out and disconnect from the database.
  162.      *
  163.      * @access public
  164.      *
  165.      * @return bool TRUE on success, FALSE if not connected.
  166.      */
  167.     function disconnect()
  168.     {
  169.         $ret = mysql_close($this->connection);
  170.         $this->connection = null;
  171.         return $ret;
  172.     }
  173.  
  174.     // }}}
  175.     // {{{ simpleQuery()
  176.  
  177.     /**
  178.      * Send a query to MySQL and return the results as a MySQL resource
  179.      * identifier.
  180.      *
  181.      * @param the SQL query
  182.      *
  183.      * @access public
  184.      *
  185.      * @return mixed returns a valid MySQL result for successful SELECT
  186.      * queries, DB_OK for other successful queries.  A DB error is
  187.      * returned on failure.
  188.      */
  189.     function simpleQuery($query)
  190.     {
  191.         $ismanip = DB::isManip($query);
  192.         $this->last_query = $query;
  193.         $query = $this->modifyQuery($query);
  194.         if ($this->_changed_db) {
  195.             if (!@mysql_select_db($this->_changed_db, $this->connection)) {
  196.                 return $this->mysqlRaiseError(DB_ERROR_NODBSELECTED);
  197.             }
  198.         }
  199.         if (!$this->autocommit && $ismanip) {
  200.             if ($this->transaction_opcount == 0) {
  201.                 $result = @mysql_query('SET AUTOCOMMIT=0', $this->connection);
  202.                 $result = @mysql_query('BEGIN', $this->connection);
  203.                 if (!$result) {
  204.                     return $this->mysqlRaiseError();
  205.                 }
  206.             }
  207.             $this->transaction_opcount++;
  208.         }
  209.         $result = @mysql_query($query, $this->connection);
  210.         if (!$result) {
  211.             return $this->mysqlRaiseError();
  212.         }
  213.         if (is_resource($result)) {
  214.             $numrows = $this->numrows($result);
  215.             if (is_object($numrows)) {
  216.                 return $numrows;
  217.             }
  218.             $this->num_rows[$result] = $numrows;
  219.             return $result;
  220.         }
  221.         return DB_OK;
  222.     }
  223.  
  224.     // }}}
  225.     // {{{ nextResult()
  226.  
  227.     /**
  228.      * Move the internal mysql result pointer to the next available result
  229.      *
  230.      * @param a valid fbsql result resource
  231.      *
  232.      * @access public
  233.      *
  234.      * @return true if a result is available otherwise return false
  235.      */
  236.     function nextResult($result)
  237.     {
  238.         return false;
  239.     }
  240.  
  241.     // }}}
  242.     // {{{ fetchRow()
  243.  
  244.     /**
  245.      * Fetch and return a row of data (it uses fetchInto for that)
  246.      * @param $result MySQL result identifier
  247.      * @param   $fetchmode  format of fetched row array
  248.      * @param   $rownum     the absolute row number to fetch
  249.      *
  250.      * @return  array   a row of data, or false on error
  251.      */
  252.     function fetchRow($result, $fetchmode = DB_FETCHMODE_DEFAULT, $rownum=null)
  253.     {
  254.         if ($fetchmode == DB_FETCHMODE_DEFAULT) {
  255.             $fetchmode = $this->fetchmode;
  256.         }
  257.         $res = $this->fetchInto ($result, $arr, $fetchmode, $rownum);
  258.         if ($res !== DB_OK) {
  259.             return $res;
  260.         }
  261.         return $arr;
  262.     }
  263.  
  264.     // }}}
  265.     // {{{ fetchInto()
  266.  
  267.     /**
  268.      * Fetch a row and insert the data into an existing array.
  269.      *
  270.      * @param $result MySQL result identifier
  271.      * @param $arr (reference) array where data from the row is stored
  272.      * @param $fetchmode how the array data should be indexed
  273.      * @param   $rownum the row number to fetch
  274.      * @access public
  275.      *
  276.      * @return int DB_OK on success, a DB error on failure
  277.      */
  278.     function fetchInto($result, &$arr, $fetchmode, $rownum=null)
  279.     {
  280.         if ($rownum !== null) {
  281.             if (!@mysql_data_seek($result, $rownum)) {
  282.                 return null;
  283.             }
  284.         }
  285.         if ($fetchmode & DB_FETCHMODE_ASSOC) {
  286.             $arr = @mysql_fetch_array($result, MYSQL_ASSOC);
  287.         } else {
  288.             $arr = @mysql_fetch_row($result);
  289.         }
  290.         if (!$arr) {
  291.             $errno = @mysql_errno($this->connection);
  292.             if (!$errno) {
  293.                 return NULL;
  294.             }
  295.             return $this->mysqlRaiseError($errno);
  296.         }
  297.         return DB_OK;
  298.     }
  299.  
  300.     // }}}
  301.     // {{{ freeResult()
  302.  
  303.     /**
  304.      * Free the internal resources associated with $result.
  305.      *
  306.      * @param $result MySQL result identifier or DB statement identifier
  307.      *
  308.      * @access public
  309.      *
  310.      * @return bool TRUE on success, FALSE if $result is invalid
  311.      */
  312.     function freeResult($result)
  313.     {
  314.         if (is_resource($result)) {
  315.             return mysql_free_result($result);
  316.         }
  317.  
  318.         if (!isset($this->prepare_tokens[(int)$result])) {
  319.             return false;
  320.         }
  321.  
  322.         unset($this->prepare_tokens[(int)$result]);
  323.         unset($this->prepare_types[(int)$result]);
  324.  
  325.         return true;
  326.     }
  327.  
  328.     // }}}
  329.     // {{{ numCols()
  330.  
  331.     /**
  332.      * Get the number of columns in a result set.
  333.      *
  334.      * @param $result MySQL result identifier
  335.      *
  336.      * @access public
  337.      *
  338.      * @return int the number of columns per row in $result
  339.      */
  340.     function numCols($result)
  341.     {
  342.         $cols = @mysql_num_fields($result);
  343.  
  344.         if (!$cols) {
  345.             return $this->mysqlRaiseError();
  346.         }
  347.  
  348.         return $cols;
  349.     }
  350.  
  351.     // }}}
  352.     // {{{ numRows()
  353.  
  354.     /**
  355.      * Get the number of rows in a result set.
  356.      *
  357.      * @param $result MySQL result identifier
  358.      *
  359.      * @access public
  360.      *
  361.      * @return int the number of rows in $result
  362.      */
  363.     function numRows($result)
  364.     {
  365.         $rows = @mysql_num_rows($result);
  366.         if ($rows === null) {
  367.             return $this->mysqlRaiseError();
  368.         }
  369.         return $rows;
  370.     }
  371.  
  372.     // }}}
  373.     // {{{ autoCommit()
  374.  
  375.     /**
  376.      * Enable/disable automatic commits
  377.      */
  378.     function autoCommit($onoff = false)
  379.     {
  380.         // XXX if $this->transaction_opcount > 0, we should probably
  381.         // issue a warning here.
  382.         $this->autocommit = $onoff ? true : false;
  383.         return DB_OK;
  384.     }
  385.  
  386.     // }}}
  387.     // {{{ commit()
  388.  
  389.     /**
  390.      * Commit the current transaction.
  391.      */
  392.     function commit()
  393.     {
  394.         if ($this->transaction_opcount > 0) {
  395.             if ($this->_changed_db) {
  396.                 if (!@mysql_select_db($this->_changed_db, $this->connection)) {
  397.                     return $this->mysqlRaiseError(DB_ERROR_NODBSELECTED);
  398.                 }
  399.             }
  400.             $result = @mysql_query('COMMIT', $this->connection);
  401.             $result = @mysql_query('SET AUTOCOMMIT=1', $this->connection);
  402.             $this->transaction_opcount = 0;
  403.             if (!$result) {
  404.                 return $this->mysqlRaiseError();
  405.             }
  406.         }
  407.         return DB_OK;
  408.     }
  409.  
  410.     // }}}
  411.     // {{{ rollback()
  412.  
  413.     /**
  414.      * Roll back (undo) the current transaction.
  415.      */
  416.     function rollback()
  417.     {
  418.         if ($this->transaction_opcount > 0) {
  419.             if ($this->_changed_db) {
  420.                 if (!@mysql_select_db($this->_changed_db, $this->connection)) {
  421.                     return $this->mysqlRaiseError(DB_ERROR_NODBSELECTED);
  422.                 }
  423.             }
  424.             $result = @mysql_query('ROLLBACK', $this->connection);
  425.             $result = @mysql_query('SET AUTOCOMMIT=1', $this->connection);
  426.             $this->transaction_opcount = 0;
  427.             if (!$result) {
  428.                 return $this->mysqlRaiseError();
  429.             }
  430.         }
  431.         return DB_OK;
  432.     }
  433.  
  434.     // }}}
  435.     // {{{ affectedRows()
  436.  
  437.     /**
  438.      * Gets the number of rows affected by the data manipulation
  439.      * query.  For other queries, this function returns 0.
  440.      *
  441.      * @return number of rows affected by the last query
  442.      */
  443.  
  444.     function affectedRows()
  445.     {
  446.         if (DB::isManip($this->last_query)) {
  447.             $result = @mysql_affected_rows($this->connection);
  448.         } else {
  449.             $result = 0;
  450.         }
  451.         return $result;
  452.      }
  453.  
  454.     // }}}
  455.     // {{{ errorNative()
  456.  
  457.     /**
  458.      * Get the native error code of the last error (if any) that
  459.      * occured on the current connection.
  460.      *
  461.      * @access public
  462.      *
  463.      * @return int native MySQL error code
  464.      */
  465.  
  466.     function errorNative()
  467.     {
  468.         return mysql_errno($this->connection);
  469.     }
  470.  
  471.     // }}}
  472.     // {{{ nextId()
  473.  
  474.     /**
  475.      * Get the next value in a sequence.  We emulate sequences
  476.      * for MySQL.  Will create the sequence if it does not exist.
  477.      *
  478.      * @access public
  479.      *
  480.      * @param $seq_name the name of the sequence
  481.      *
  482.      * @param $ondemand whether to create the sequence table on demand
  483.      * (default is true)
  484.      *
  485.      * @return a sequence integer, or a DB error
  486.      */
  487.     function nextId($seq_name, $ondemand = true)
  488.     {
  489.         $sqn = preg_replace('/[^a-z0-9_]/i', '_', $seq_name);
  490.         $repeat = 0;
  491.         do {
  492.             // XXX HACK: temporarily force error mode to "return"
  493.             $tmp = $this->_default_error_mode;
  494.             $this->_default_error_mode = PEAR_ERROR_RETURN;
  495.             $result = $this->query("UPDATE ${sqn}_seq ".
  496.                                    'SET id=LAST_INSERT_ID(id+1)');
  497.             $this->_default_error_mode = $tmp;
  498.             if ($ondemand && DB::isError($result) &&
  499.                 $result->getCode() == DB_ERROR_NOSUCHTABLE) {
  500.                 $repeat = 1;
  501.                 $result = $this->createSequence($seq_name);
  502.                 // Since createSequence initializes the ID to be 1,
  503.                 // we do not need to retrieve the ID again (or we will get 2)
  504.                 if (DB::isError($result)) {
  505.                     return $result;
  506.                 } else {
  507.                     // First ID of a newly created sequence is 1
  508.                     return 1;
  509.                 }
  510.             } elseif (DB::isError($result) &&
  511.                 $result->getCode() == DB_ERROR_ALREADY_EXISTS) {
  512.                 // Must be using old sequence emulation implementation,
  513.                 // we need to clean up the dupes.
  514.  
  515.                 // Obtain a user-level lock... this will release any previous
  516.                 // application locks, but unlike LOCK TABLES, it does not abort
  517.                 // the current transaction and is much less frequently used.
  518.                 $result = $this->getOne("SELECT GET_LOCK('${sqn}_seq_lock',10)");
  519.                 if (DB::isError($result)) {
  520.                     return $result;
  521.                 }
  522.                 if ($result == 0) {
  523.                     // Failed to get the lock, can't do the conversion, bail
  524.                     // with a DB_ERROR_NOT_LOCKED error
  525.                     return $this->mysqlRaiseError(1100);
  526.                 }
  527.  
  528.                 $highest_id = $this->getOne("SELECT MAX(id) FROM ${sqn}_seq");
  529.                 if (DB::isError($highest_id)) {
  530.                     return $highest_id;
  531.                 }
  532.                 // We should probably do something if $highest_id isn't
  533.                 // numeric, but I'm at a loss as how to handle that...
  534.                 $result = $this->query("DELETE FROM ${sqn}_seq ".
  535.                                        "WHERE id <> $highest_id");
  536.                 if (DB::isError($result)) {
  537.                     return $result;
  538.                 }
  539.  
  540.                 // If another thread has been waiting for this lock,
  541.                 // it will go thru the above procedure, but will have no
  542.                 // real effect
  543.                 $result = $this->getOne("SELECT RELEASE_LOCK('${sqn}_seq_lock')");
  544.                 if (DB::isError($result)) {
  545.                     return $result;
  546.                 }
  547.  
  548.                 // This should kill all rows except the highest, now we
  549.                 // can try again
  550.                 $repeat = 1;
  551.             } else {
  552.                 $repeat = 0;
  553.             }
  554.         } while ($repeat);
  555.         if (DB::isError($result)) {
  556.             return $result;
  557.         }
  558.         return mysql_insert_id($this->connection);
  559.     }
  560.  
  561.     // }}}
  562.     // {{{ createSequence()
  563.  
  564.     function createSequence($seq_name)
  565.     {
  566.         $sqn = preg_replace('/[^a-z0-9_]/i', '_', $seq_name);
  567.         $res = $this->query("CREATE TABLE ${sqn}_seq ".
  568.                             '(id INTEGER UNSIGNED AUTO_INCREMENT NOT NULL,'.
  569.                             ' PRIMARY KEY(id))');
  570.         if (DB::isError($res)) {
  571.             return $res;
  572.         }
  573.         // insert yields value 1, nextId call will generate ID 2
  574.         return $this->query("INSERT INTO ${sqn}_seq VALUES(0)");
  575.     }
  576.  
  577.     // }}}
  578.     // {{{ dropSequence()
  579.  
  580.     function dropSequence($seq_name)
  581.     {
  582.         $sqn = preg_replace('/[^a-z0-9_]/i', '_', $seq_name);
  583.         return $this->query("DROP TABLE ${sqn}_seq");
  584.     }
  585.  
  586.     // }}}
  587.     // {{{ quote()
  588.     /**
  589.     * Quote the given string so it can be safely used within string delimiters
  590.     * in a query.
  591.     * @param $string mixed Data to be quoted
  592.     * @return mixed "NULL" string, quoted string or original data
  593.     */
  594.     function quote($str = null)
  595.     {
  596.         switch (strtolower(gettype($str))) {
  597.             case 'null':
  598.                 return 'NULL';
  599.             case 'integer':
  600.                 return $str;
  601.             case 'string':
  602.             default:
  603.                 return "'".mysql_escape_string($str)."'";
  604.         }
  605.     }
  606.     // }}}
  607.     // {{{ modifyQuery()
  608.  
  609.     function modifyQuery($query, $subject = null)
  610.     {
  611.         if ($this->options['optimize'] == 'portability') {
  612.             // "DELETE FROM table" gives 0 affected rows in MySQL.
  613.             // This little hack lets you know how many rows were deleted.
  614.             if (preg_match('/^\s*DELETE\s+FROM\s+(\S+)\s*$/i', $query)) {
  615.                 $query = preg_replace('/^\s*DELETE\s+FROM\s+(\S+)\s*$/',
  616.                                       'DELETE FROM \1 WHERE 1=1', $query);
  617.             }
  618.         }
  619.         return $query;
  620.     }
  621.  
  622.     // }}}
  623.     // {{{ modifyLimitQuery()
  624.  
  625.     function modifyLimitQuery($query, $from, $count)
  626.     {
  627.         $query = $query . " LIMIT $from, $count";
  628.         return $query;
  629.     }
  630.  
  631.     // }}}
  632.     // {{{ mysqlRaiseError()
  633.  
  634.     function mysqlRaiseError($errno = null)
  635.     {
  636.         if ($errno === null) {
  637.             $errno = $this->errorCode(mysql_errno($this->connection));
  638.         }
  639.         return $this->raiseError($errno, null, null, null,
  640.                         @mysql_error($this->connection));
  641.     }
  642.  
  643.     // }}}
  644.     // {{{ tableInfo()
  645.  
  646.     function tableInfo($result, $mode = null) {
  647.         $count = 0;
  648.         $id    = 0;
  649.         $res   = array();
  650.  
  651.         /*
  652.          * depending on $mode, metadata returns the following values:
  653.          *
  654.          * - mode is false (default):
  655.          * $result[]:
  656.          *   [0]["table"]  table name
  657.          *   [0]["name"]   field name
  658.          *   [0]["type"]   field type
  659.          *   [0]["len"]    field length
  660.          *   [0]["flags"]  field flags
  661.          *
  662.          * - mode is DB_TABLEINFO_ORDER
  663.          * $result[]:
  664.          *   ["num_fields"] number of metadata records
  665.          *   [0]["table"]  table name
  666.          *   [0]["name"]   field name
  667.          *   [0]["type"]   field type
  668.          *   [0]["len"]    field length
  669.          *   [0]["flags"]  field flags
  670.          *   ["order"][field name]  index of field named "field name"
  671.          *   The last one is used, if you have a field name, but no index.
  672.          *   Test:  if (isset($result['meta']['myfield'])) { ...
  673.          *
  674.          * - mode is DB_TABLEINFO_ORDERTABLE
  675.          *    the same as above. but additionally
  676.          *   ["ordertable"][table name][field name] index of field
  677.          *      named "field name"
  678.          *
  679.          *      this is, because if you have fields from different
  680.          *      tables with the same field name * they override each
  681.          *      other with DB_TABLEINFO_ORDER
  682.          *
  683.          *      you can combine DB_TABLEINFO_ORDER and
  684.          *      DB_TABLEINFO_ORDERTABLE with DB_TABLEINFO_ORDER |
  685.          *      DB_TABLEINFO_ORDERTABLE * or with DB_TABLEINFO_FULL
  686.          */
  687.  
  688.         // if $result is a string, then we want information about a
  689.         // table without a resultset
  690.         if (is_string($result)) {
  691.             $id = @mysql_list_fields($this->dsn['database'],
  692.                                      $result, $this->connection);
  693.             if (empty($id)) {
  694.                 return $this->mysqlRaiseError();
  695.             }
  696.         } else { // else we want information about a resultset
  697.             $id = $result;
  698.             if (empty($id)) {
  699.                 return $this->mysqlRaiseError();
  700.             }
  701.         }
  702.  
  703.         $count = @mysql_num_fields($id);
  704.  
  705.         // made this IF due to performance (one if is faster than $count if's)
  706.         if (empty($mode)) {
  707.             for ($i=0; $i<$count; $i++) {
  708.                 $res[$i]['table'] = @mysql_field_table ($id, $i);
  709.                 $res[$i]['name']  = @mysql_field_name  ($id, $i);
  710.                 $res[$i]['type']  = @mysql_field_type  ($id, $i);
  711.                 $res[$i]['len']   = @mysql_field_len   ($id, $i);
  712.                 $res[$i]['flags'] = @mysql_field_flags ($id, $i);
  713.             }
  714.         } else { // full
  715.             $res['num_fields']= $count;
  716.  
  717.             for ($i=0; $i<$count; $i++) {
  718.                 $res[$i]['table'] = @mysql_field_table ($id, $i);
  719.                 $res[$i]['name']  = @mysql_field_name  ($id, $i);
  720.                 $res[$i]['type']  = @mysql_field_type  ($id, $i);
  721.                 $res[$i]['len']   = @mysql_field_len   ($id, $i);
  722.                 $res[$i]['flags'] = @mysql_field_flags ($id, $i);
  723.                 if ($mode & DB_TABLEINFO_ORDER) {
  724.                     $res['order'][$res[$i]['name']] = $i;
  725.                 }
  726.                 if ($mode & DB_TABLEINFO_ORDERTABLE) {
  727.                     $res['ordertable'][$res[$i]['table']][$res[$i]['name']] = $i;
  728.                 }
  729.             }
  730.         }
  731.  
  732.         // free the result only if we were called on a table
  733.         if (is_string($result)) {
  734.             @mysql_free_result($id);
  735.         }
  736.         return $res;
  737.     }
  738.  
  739.     // }}}
  740.     // {{{ getTablesQuery()
  741.  
  742.     /**
  743.     * Returns the query needed to get some backend info
  744.     * @param string $type What kind of info you want to retrieve
  745.     * @return string The SQL query string
  746.     */
  747.     function getSpecialQuery($type)
  748.     {
  749.         switch ($type) {
  750.             case 'tables':
  751.                 $sql = "SHOW TABLES";
  752.                 break;
  753.             case 'views':
  754.                 return DB_ERROR_NOT_CAPABLE;
  755.             case 'users':
  756.                 $sql = "select distinct User from user";
  757.                 if($this->dsn['database'] != 'mysql') {
  758.                     $dsn = $this->dsn;
  759.                     $dsn['database'] = 'mysql';
  760.                     if (DB::isError($db = DB::connect($dsn))) {
  761.                         return $db;
  762.                     }
  763.                     $sql = $db->getCol($sql);
  764.                     $db->disconnect();
  765.                     // XXX Fixme the mysql driver should take care of this
  766.                     if (!@mysql_select_db($this->dsn['database'], $this->connection)) {
  767.                         return $this->mysqlRaiseError(DB_ERROR_NODBSELECTED);
  768.                     }
  769.                 }
  770.                 return $sql;
  771.                 break;
  772.             case 'databases':
  773.                 $sql = "SHOW DATABASES";
  774.                 break;
  775.             default:
  776.                 return null;
  777.         }
  778.         return $sql;
  779.     }
  780.  
  781.     // }}}
  782.  
  783.     // TODO/wishlist:
  784.     // longReadlen
  785.     // binmode
  786. }
  787.  
  788. ?>