3 * This is the MySQL database abstraction layer.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
25 * Database abstraction object for PHP extension mysql.
30 class DatabaseMysql
extends DatabaseMysqlBase
{
33 * @return resource False on error
35 protected function doQuery( $sql ) {
36 $conn = $this->getBindingHandle();
38 if ( $this->bufferResults() ) {
39 $ret = mysql_query( $sql, $conn );
41 $ret = mysql_unbuffered_query( $sql, $conn );
48 * @param string $realServer
49 * @return bool|resource MySQL Database connection or false on failure to connect
50 * @throws DBConnectionError
52 protected function mysqlConnect( $realServer ) {
53 # Avoid a suppressed fatal error, which is very hard to track down
54 if ( !extension_loaded( 'mysql' ) ) {
55 throw new DBConnectionError(
57 "MySQL functions missing, have you compiled PHP with the --with-mysql option?\n"
62 if ( $this->mFlags
& self
::DBO_SSL
) {
63 $connFlags |
= MYSQL_CLIENT_SSL
;
65 if ( $this->mFlags
& self
::DBO_COMPRESS
) {
66 $connFlags |
= MYSQL_CLIENT_COMPRESS
;
69 if ( ini_get( 'mysql.connect_timeout' ) <= 3 ) {
77 # The kernel's default SYN retransmission period is far too slow for us,
78 # so we use a short timeout plus a manual retry. Retrying means that a small
79 # but finite rate of SYN packet loss won't cause user-visible errors.
80 for ( $i = 0; $i < $numAttempts && !$conn; $i++
) {
84 if ( $this->mFlags
& self
::DBO_PERSISTENT
) {
85 $conn = mysql_pconnect( $realServer, $this->mUser
, $this->mPassword
, $connFlags );
87 # Create a new connection...
88 $conn = mysql_connect( $realServer, $this->mUser
, $this->mPassword
, true, $connFlags );
96 * @param string $charset
99 protected function mysqlSetCharset( $charset ) {
100 $conn = $this->getBindingHandle();
102 if ( function_exists( 'mysql_set_charset' ) ) {
103 return mysql_set_charset( $charset, $conn );
105 return $this->query( 'SET NAMES ' . $charset, __METHOD__
);
112 protected function closeConnection() {
113 $conn = $this->getBindingHandle();
115 return mysql_close( $conn );
121 function insertId() {
122 $conn = $this->getBindingHandle();
124 return mysql_insert_id( $conn );
130 function lastErrno() {
131 if ( $this->mConn
) {
132 return mysql_errno( $this->mConn
);
134 return mysql_errno();
141 function affectedRows() {
142 $conn = $this->getBindingHandle();
144 return mysql_affected_rows( $conn );
151 function selectDB( $db ) {
152 $conn = $this->getBindingHandle();
154 $this->mDBname
= $db;
156 return mysql_select_db( $db, $conn );
159 protected function mysqlFreeResult( $res ) {
160 return mysql_free_result( $res );
163 protected function mysqlFetchObject( $res ) {
164 return mysql_fetch_object( $res );
167 protected function mysqlFetchArray( $res ) {
168 return mysql_fetch_array( $res );
171 protected function mysqlNumRows( $res ) {
172 return mysql_num_rows( $res );
175 protected function mysqlNumFields( $res ) {
176 return mysql_num_fields( $res );
179 protected function mysqlFetchField( $res, $n ) {
180 return mysql_fetch_field( $res, $n );
183 protected function mysqlFieldName( $res, $n ) {
184 return mysql_field_name( $res, $n );
187 protected function mysqlFieldType( $res, $n ) {
188 return mysql_field_type( $res, $n );
191 protected function mysqlDataSeek( $res, $row ) {
192 return mysql_data_seek( $res, $row );
195 protected function mysqlError( $conn = null ) {
196 return ( $conn !== null ) ?
mysql_error( $conn ) : mysql_error(); // avoid warning
199 protected function mysqlRealEscapeString( $s ) {
200 $conn = $this->getBindingHandle();
202 return mysql_real_escape_string( $s, $conn );