2 /* vim: set expandtab tabstop=4 shiftwidth=4 foldmethod=marker: */
3 // +----------------------------------------------------------------------+
5 // +----------------------------------------------------------------------+
6 // | Copyright (c) 1997-2004 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 // | Author: Stig Bakken <ssb@php.net> |
17 // | Maintainer: Daniel Convissor <danielc@php.net> |
18 // +----------------------------------------------------------------------+
24 // XXX ERRORMSG: The error message from the mysql function should
25 // be registered here.
31 require_once 'DB/common.php';
34 * Database independent query interface definition for PHP's MySQL
37 * This is for MySQL versions 4.0 and below.
42 * @author Stig Bakken <ssb@php.net>
44 class DB_mysql extends DB_common
49 var $phptype, $dbsyntax;
50 var $prepare_tokens = array();
51 var $prepare_types = array();
52 var $num_rows = array();
53 var $transaction_opcount = 0;
54 var $autocommit = true;
55 var $fetchmode = DB_FETCHMODE_ORDERED; /* Default fetch mode */
62 * DB_mysql constructor.
69 $this->phptype = 'mysql';
70 $this->dbsyntax = 'mysql';
71 $this->features = array(
74 'transactions' => true,
77 $this->errorcode_map = array(
78 1004 => DB_ERROR_CANNOT_CREATE,
79 1005 => DB_ERROR_CANNOT_CREATE,
80 1006 => DB_ERROR_CANNOT_CREATE,
81 1007 => DB_ERROR_ALREADY_EXISTS,
82 1008 => DB_ERROR_CANNOT_DROP,
83 1022 => DB_ERROR_ALREADY_EXISTS,
84 1046 => DB_ERROR_NODBSELECTED,
85 1050 => DB_ERROR_ALREADY_EXISTS,
86 1051 => DB_ERROR_NOSUCHTABLE,
87 1054 => DB_ERROR_NOSUCHFIELD,
88 1062 => DB_ERROR_ALREADY_EXISTS,
89 1064 => DB_ERROR_SYNTAX,
90 1100 => DB_ERROR_NOT_LOCKED,
91 1136 => DB_ERROR_VALUE_COUNT_ON_ROW,
92 1146 => DB_ERROR_NOSUCHTABLE,
93 1048 => DB_ERROR_CONSTRAINT,
94 1216 => DB_ERROR_CONSTRAINT
102 * Connect to a database and log in as the specified user.
104 * @param $dsn the data source name (see DB::parseDSN for syntax)
105 * @param $persistent (optional) whether the connection should
108 * @return int DB_OK on success, a DB error on failure
110 function connect($dsninfo, $persistent = false)
112 if (!DB::assertExtension('mysql')) {
113 return $this->raiseError(DB_ERROR_EXTENSION_NOT_FOUND);
115 $this->dsn = $dsninfo;
116 if ($dsninfo['protocol'] && $dsninfo['protocol'] == 'unix') {
117 $dbhost = ':' . $dsninfo['socket'];
119 $dbhost = $dsninfo['hostspec'] ? $dsninfo['hostspec'] : 'localhost';
120 if ($dsninfo['port']) {
121 $dbhost .= ':' . $dsninfo['port'];
125 $connect_function = $persistent ? 'mysql_pconnect' : 'mysql_connect';
127 if ($dbhost && $dsninfo['username'] && isset($dsninfo['password'])) {
128 $conn = @$connect_function($dbhost, $dsninfo['username'],
129 $dsninfo['password']);
130 } elseif ($dbhost && $dsninfo['username']) {
131 $conn = @$connect_function($dbhost, $dsninfo['username']);
133 $conn = @$connect_function($dbhost);
138 if (($err = @mysql_error()) != '') {
139 return $this->raiseError(DB_ERROR_CONNECT_FAILED, null, null,
141 } elseif (empty($php_errormsg)) {
142 return $this->raiseError(DB_ERROR_CONNECT_FAILED);
144 return $this->raiseError(DB_ERROR_CONNECT_FAILED, null, null,
145 null, $php_errormsg);
149 if ($dsninfo['database']) {
150 if (!@mysql_select_db($dsninfo['database'], $conn)) {
151 switch(mysql_errno($conn)) {
153 return $this->raiseError(DB_ERROR_NOSUCHDB, null, null,
154 null, @mysql_error($conn));
156 return $this->raiseError(DB_ERROR_ACCESS_VIOLATION, null, null,
157 null, @mysql_error($conn));
159 return $this->raiseError(DB_ERROR, null, null,
160 null, @mysql_error($conn));
163 // fix to allow calls to different databases in the same script
164 $this->_db = $dsninfo['database'];
167 $this->connection = $conn;
175 * Log out and disconnect from the database.
179 * @return bool true on success, false if not connected.
181 function disconnect()
183 $ret = @mysql_close($this->connection);
184 $this->connection = null;
192 * Send a query to MySQL and return the results as a MySQL resource
195 * @param the SQL query
199 * @return mixed returns a valid MySQL result for successful SELECT
200 * queries, DB_OK for other successful queries. A DB error is
201 * returned on failure.
203 function simpleQuery($query)
205 $ismanip = DB::isManip($query);
206 $this->last_query = $query;
207 $query = $this->modifyQuery($query);
209 if (!@mysql_select_db($this->_db, $this->connection)) {
210 return $this->mysqlRaiseError(DB_ERROR_NODBSELECTED);
213 if (!$this->autocommit && $ismanip) {
214 if ($this->transaction_opcount == 0) {
215 $result = @mysql_query('SET AUTOCOMMIT=0', $this->connection);
216 $result = @mysql_query('BEGIN', $this->connection);
218 return $this->mysqlRaiseError();
221 $this->transaction_opcount++;
223 $result = @mysql_query($query, $this->connection);
225 return $this->mysqlRaiseError();
227 if (is_resource($result)) {
228 $numrows = $this->numrows($result);
229 if (is_object($numrows)) {
232 $this->num_rows[(int)$result] = $numrows;
242 * Move the internal mysql result pointer to the next available result
244 * This method has not been implemented yet.
246 * @param a valid sql result resource
252 function nextResult($result)
261 * Fetch a row and insert the data into an existing array.
263 * Formating of the array and the data therein are configurable.
264 * See DB_result::fetchInto() for more information.
266 * @param resource $result query result identifier
267 * @param array $arr (reference) array where data from the row
269 * @param int $fetchmode how the resulting array should be indexed
270 * @param int $rownum the row number to fetch
272 * @return mixed DB_OK on success, null when end of result set is
273 * reached or on failure
275 * @see DB_result::fetchInto()
278 function fetchInto($result, &$arr, $fetchmode, $rownum=null)
280 if ($rownum !== null) {
281 if (!@mysql_data_seek($result, $rownum)) {
285 if ($fetchmode & DB_FETCHMODE_ASSOC) {
286 $arr = @mysql_fetch_array($result, MYSQL_ASSOC);
287 if ($this->options['portability'] & DB_PORTABILITY_LOWERCASE && $arr) {
288 $arr = array_change_key_case($arr, CASE_LOWER);
291 $arr = @mysql_fetch_row($result);
294 // See: http://bugs.php.net/bug.php?id=22328
295 // for why we can't check errors on fetching
298 $errno = @mysql_errno($this->connection);
302 return $this->mysqlRaiseError($errno);
305 if ($this->options['portability'] & DB_PORTABILITY_RTRIM) {
307 * Even though this DBMS already trims output, we do this because
308 * a field might have intentional whitespace at the end that
309 * gets removed by DB_PORTABILITY_RTRIM under another driver.
311 $this->_rtrimArrayValues($arr);
313 if ($this->options['portability'] & DB_PORTABILITY_NULL_TO_EMPTY) {
314 $this->_convertNullArrayValuesToEmpty($arr);
323 * Free the internal resources associated with $result.
325 * @param $result MySQL result identifier
329 * @return bool true on success, false if $result is invalid
331 function freeResult($result)
333 unset($this->num_rows[(int)$result]);
334 return @mysql_free_result($result);
341 * Get the number of columns in a result set.
343 * @param $result MySQL result identifier
347 * @return int the number of columns per row in $result
349 function numCols($result)
351 $cols = @mysql_num_fields($result);
354 return $this->mysqlRaiseError();
364 * Get the number of rows in a result set.
366 * @param $result MySQL result identifier
370 * @return int the number of rows in $result
372 function numRows($result)
374 $rows = @mysql_num_rows($result);
375 if ($rows === null) {
376 return $this->mysqlRaiseError();
385 * Enable/disable automatic commits
387 function autoCommit($onoff = false)
389 // XXX if $this->transaction_opcount > 0, we should probably
390 // issue a warning here.
391 $this->autocommit = $onoff ? true : false;
399 * Commit the current transaction.
403 if ($this->transaction_opcount > 0) {
405 if (!@mysql_select_db($this->_db, $this->connection)) {
406 return $this->mysqlRaiseError(DB_ERROR_NODBSELECTED);
409 $result = @mysql_query('COMMIT', $this->connection);
410 $result = @mysql_query('SET AUTOCOMMIT=1', $this->connection);
411 $this->transaction_opcount = 0;
413 return $this->mysqlRaiseError();
423 * Roll back (undo) the current transaction.
427 if ($this->transaction_opcount > 0) {
429 if (!@mysql_select_db($this->_db, $this->connection)) {
430 return $this->mysqlRaiseError(DB_ERROR_NODBSELECTED);
433 $result = @mysql_query('ROLLBACK', $this->connection);
434 $result = @mysql_query('SET AUTOCOMMIT=1', $this->connection);
435 $this->transaction_opcount = 0;
437 return $this->mysqlRaiseError();
444 // {{{ affectedRows()
447 * Gets the number of rows affected by the data manipulation
448 * query. For other queries, this function returns 0.
450 * @return number of rows affected by the last query
452 function affectedRows()
454 if (DB::isManip($this->last_query)) {
455 return @mysql_affected_rows($this->connection);
465 * Get the native error code of the last error (if any) that
466 * occured on the current connection.
470 * @return int native MySQL error code
472 function errorNative()
474 return @mysql_errno($this->connection);
481 * Returns the next free id in a sequence
483 * @param string $seq_name name of the sequence
484 * @param boolean $ondemand when true, the seqence is automatically
485 * created if it does not exist
487 * @return int the next id number in the sequence. DB_Error if problem.
490 * @see DB_common::nextID()
493 function nextId($seq_name, $ondemand = true)
495 $seqname = $this->getSequenceName($seq_name);
498 $this->pushErrorHandling(PEAR_ERROR_RETURN);
499 $result = $this->query("UPDATE ${seqname} ".
500 'SET id=LAST_INSERT_ID(id+1)');
501 $this->popErrorHandling();
502 if ($result == DB_OK) {
504 $id = @mysql_insert_id($this->connection);
508 /** EMPTY SEQ TABLE **/
509 // Sequence table must be empty for some reason, so fill it and return 1
510 // Obtain a user-level lock
511 $result = $this->getOne("SELECT GET_LOCK('${seqname}_lock',10)");
512 if (DB::isError($result)) {
513 return $this->raiseError($result);
516 // Failed to get the lock, bail with a DB_ERROR_NOT_LOCKED error
517 return $this->mysqlRaiseError(DB_ERROR_NOT_LOCKED);
520 // add the default value
521 $result = $this->query("REPLACE INTO ${seqname} VALUES (0)");
522 if (DB::isError($result)) {
523 return $this->raiseError($result);
527 $result = $this->getOne("SELECT RELEASE_LOCK('${seqname}_lock')");
528 if (DB::isError($result)) {
529 return $this->raiseError($result);
531 // We know what the result will be, so no need to try again
534 /** ONDEMAND TABLE CREATION **/
535 } elseif ($ondemand && DB::isError($result) &&
536 $result->getCode() == DB_ERROR_NOSUCHTABLE)
538 $result = $this->createSequence($seq_name);
539 if (DB::isError($result)) {
540 return $this->raiseError($result);
545 /** BACKWARDS COMPAT **/
546 } elseif (DB::isError($result) &&
547 $result->getCode() == DB_ERROR_ALREADY_EXISTS)
549 // see _BCsequence() comment
550 $result = $this->_BCsequence($seqname);
551 if (DB::isError($result)) {
552 return $this->raiseError($result);
558 return $this->raiseError($result);
562 // {{{ createSequence()
565 * Creates a new sequence
567 * @param string $seq_name name of the new sequence
569 * @return int DB_OK on success. A DB_Error object is returned if
573 * @see DB_common::createSequence()
576 function createSequence($seq_name)
578 $seqname = $this->getSequenceName($seq_name);
579 $res = $this->query("CREATE TABLE ${seqname} ".
580 '(id INTEGER UNSIGNED AUTO_INCREMENT NOT NULL,'.
581 ' PRIMARY KEY(id))');
582 if (DB::isError($res)) {
585 // insert yields value 1, nextId call will generate ID 2
586 $res = $this->query("INSERT INTO ${seqname} VALUES(0)");
587 if (DB::isError($res)) {
591 return $this->query("UPDATE ${seqname} SET id = 0;");
595 // {{{ dropSequence()
600 * @param string $seq_name name of the sequence to be deleted
602 * @return int DB_OK on success. DB_Error if problems.
605 * @see DB_common::dropSequence()
608 function dropSequence($seq_name)
610 return $this->query('DROP TABLE ' . $this->getSequenceName($seq_name));
617 * Backwards compatibility with old sequence emulation implementation
618 * (clean up the dupes)
620 * @param string $seqname The sequence name to clean up
621 * @return mixed DB_Error or true
623 function _BCsequence($seqname)
625 // Obtain a user-level lock... this will release any previous
626 // application locks, but unlike LOCK TABLES, it does not abort
627 // the current transaction and is much less frequently used.
628 $result = $this->getOne("SELECT GET_LOCK('${seqname}_lock',10)");
629 if (DB::isError($result)) {
633 // Failed to get the lock, can't do the conversion, bail
634 // with a DB_ERROR_NOT_LOCKED error
635 return $this->mysqlRaiseError(DB_ERROR_NOT_LOCKED);
638 $highest_id = $this->getOne("SELECT MAX(id) FROM ${seqname}");
639 if (DB::isError($highest_id)) {
642 // This should kill all rows except the highest
643 // We should probably do something if $highest_id isn't
644 // numeric, but I'm at a loss as how to handle that...
645 $result = $this->query("DELETE FROM ${seqname} WHERE id <> $highest_id");
646 if (DB::isError($result)) {
650 // If another thread has been waiting for this lock,
651 // it will go thru the above procedure, but will have no
653 $result = $this->getOne("SELECT RELEASE_LOCK('${seqname}_lock')");
654 if (DB::isError($result)) {
661 // {{{ quoteIdentifier()
664 * Quote a string so it can be safely used as a table or column name
666 * Quoting style depends on which database driver is being used.
668 * MySQL can't handle the backtick character (<kbd>`</kbd>) in
669 * table or column names.
671 * @param string $str identifier name to be quoted
673 * @return string quoted identifier string
679 function quoteIdentifier($str)
681 return '`' . $str . '`';
688 * @deprecated Deprecated in release 1.6.0
691 function quote($str) {
692 return $this->quoteSmart($str);
696 // {{{ escapeSimple()
699 * Escape a string according to the current DBMS's standards
701 * @param string $str the string to be escaped
703 * @return string the escaped string
707 function escapeSimple($str) {
708 if (function_exists('mysql_real_escape_string')) {
709 return @mysql_real_escape_string($str, $this->connection);
711 return @mysql_escape_string($str);
718 function modifyQuery($query)
720 if ($this->options['portability'] & DB_PORTABILITY_DELETE_COUNT) {
721 // "DELETE FROM table" gives 0 affected rows in MySQL.
722 // This little hack lets you know how many rows were deleted.
723 if (preg_match('/^\s*DELETE\s+FROM\s+(\S+)\s*$/i', $query)) {
724 $query = preg_replace('/^\s*DELETE\s+FROM\s+(\S+)\s*$/',
725 'DELETE FROM \1 WHERE 1=1', $query);
732 // {{{ modifyLimitQuery()
734 function modifyLimitQuery($query, $from, $count)
736 if (DB::isManip($query)) {
737 return $query . " LIMIT $count";
739 return $query . " LIMIT $from, $count";
744 // {{{ mysqlRaiseError()
747 * Gather information about an error, then use that info to create a
748 * DB error object and finally return that object.
750 * @param integer $errno PEAR error number (usually a DB constant) if
751 * manually raising an error
752 * @return object DB error object
753 * @see DB_common::errorCode()
754 * @see DB_common::raiseError()
756 function mysqlRaiseError($errno = null)
758 if ($errno === null) {
759 if ($this->options['portability'] & DB_PORTABILITY_ERRORS) {
760 $this->errorcode_map[1022] = DB_ERROR_CONSTRAINT;
761 $this->errorcode_map[1048] = DB_ERROR_CONSTRAINT_NOT_NULL;
762 $this->errorcode_map[1062] = DB_ERROR_CONSTRAINT;
764 // Doing this in case mode changes during runtime.
765 $this->errorcode_map[1022] = DB_ERROR_ALREADY_EXISTS;
766 $this->errorcode_map[1048] = DB_ERROR_CONSTRAINT;
767 $this->errorcode_map[1062] = DB_ERROR_ALREADY_EXISTS;
769 $errno = $this->errorCode(mysql_errno($this->connection));
771 return $this->raiseError($errno, null, null, null,
772 @mysql_errno($this->connection) . ' ** ' .
773 @mysql_error($this->connection));
780 * Returns information about a table or a result set.
782 * @param object|string $result DB_result object from a query or a
783 * string containing the name of a table
784 * @param int $mode a valid tableInfo mode
785 * @return array an associative array with the information requested
786 * or an error object if something is wrong
789 * @see DB_common::tableInfo()
791 function tableInfo($result, $mode = null) {
792 if (isset($result->result)) {
794 * Probably received a result object.
795 * Extract the result resource identifier.
797 $id = $result->result;
799 } elseif (is_string($result)) {
801 * Probably received a table name.
802 * Create a result resource identifier.
804 $id = @mysql_list_fields($this->dsn['database'],
805 $result, $this->connection);
809 * Probably received a result resource identifier.
811 * Deprecated. Here for compatibility only.
817 if (!is_resource($id)) {
818 return $this->mysqlRaiseError(DB_ERROR_NEED_MORE_DATA);
821 if ($this->options['portability'] & DB_PORTABILITY_LOWERCASE) {
822 $case_func = 'strtolower';
824 $case_func = 'strval';
827 $count = @mysql_num_fields($id);
829 // made this IF due to performance (one if is faster than $count if's)
831 for ($i=0; $i<$count; $i++) {
832 $res[$i]['table'] = $case_func(@mysql_field_table($id, $i));
833 $res[$i]['name'] = $case_func(@mysql_field_name($id, $i));
834 $res[$i]['type'] = @mysql_field_type($id, $i);
835 $res[$i]['len'] = @mysql_field_len($id, $i);
836 $res[$i]['flags'] = @mysql_field_flags($id, $i);
839 $res['num_fields']= $count;
841 for ($i=0; $i<$count; $i++) {
842 $res[$i]['table'] = $case_func(@mysql_field_table($id, $i));
843 $res[$i]['name'] = $case_func(@mysql_field_name($id, $i));
844 $res[$i]['type'] = @mysql_field_type($id, $i);
845 $res[$i]['len'] = @mysql_field_len($id, $i);
846 $res[$i]['flags'] = @mysql_field_flags($id, $i);
848 if ($mode & DB_TABLEINFO_ORDER) {
849 $res['order'][$res[$i]['name']] = $i;
851 if ($mode & DB_TABLEINFO_ORDERTABLE) {
852 $res['ordertable'][$res[$i]['table']][$res[$i]['name']] = $i;
857 // free the result only if we were called on a table
859 @mysql_free_result($id);
865 // {{{ getSpecialQuery()
868 * Returns the query needed to get some backend info
869 * @param string $type What kind of info you want to retrieve
870 * @return string The SQL query string
872 function getSpecialQuery($type)
876 return 'SHOW TABLES';
878 return DB_ERROR_NOT_CAPABLE;
880 $sql = 'select distinct User from user';
881 if ($this->dsn['database'] != 'mysql') {
883 $dsn['database'] = 'mysql';
884 if (DB::isError($db = DB::connect($dsn))) {
887 $sql = $db->getCol($sql);
889 // XXX Fixme the mysql driver should take care of this
890 if (!@mysql_select_db($this->dsn['database'], $this->connection)) {
891 return $this->mysqlRaiseError(DB_ERROR_NODBSELECTED);
896 return 'SHOW DATABASES';