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
23 namespace Wikimedia\Rdbms
;
26 * Database abstraction object for PHP extension mysql.
31 class DatabaseMysql
extends DatabaseMysqlBase
{
34 * @return resource False on error
36 protected function doQuery( $sql ) {
37 $conn = $this->getBindingHandle();
39 if ( $this->bufferResults() ) {
40 $ret = mysql_query( $sql, $conn );
42 $ret = mysql_unbuffered_query( $sql, $conn );
49 * @param string $realServer
50 * @return bool|resource MySQL Database connection or false on failure to connect
51 * @throws DBConnectionError
53 protected function mysqlConnect( $realServer ) {
54 # Avoid a suppressed fatal error, which is very hard to track down
55 if ( !extension_loaded( 'mysql' ) ) {
56 throw new DBConnectionError(
58 "MySQL functions missing, have you compiled PHP with the --with-mysql option?\n"
63 if ( $this->mFlags
& self
::DBO_SSL
) {
64 $connFlags |
= MYSQL_CLIENT_SSL
;
66 if ( $this->mFlags
& self
::DBO_COMPRESS
) {
67 $connFlags |
= MYSQL_CLIENT_COMPRESS
;
70 if ( ini_get( 'mysql.connect_timeout' ) <= 3 ) {
78 # The kernel's default SYN retransmission period is far too slow for us,
79 # so we use a short timeout plus a manual retry. Retrying means that a small
80 # but finite rate of SYN packet loss won't cause user-visible errors.
81 for ( $i = 0; $i < $numAttempts && !$conn; $i++
) {
85 if ( $this->mFlags
& self
::DBO_PERSISTENT
) {
86 $conn = mysql_pconnect( $realServer, $this->mUser
, $this->mPassword
, $connFlags );
88 # Create a new connection...
89 $conn = mysql_connect( $realServer, $this->mUser
, $this->mPassword
, true, $connFlags );
97 * @param string $charset
100 protected function mysqlSetCharset( $charset ) {
101 $conn = $this->getBindingHandle();
103 if ( function_exists( 'mysql_set_charset' ) ) {
104 return mysql_set_charset( $charset, $conn );
106 return $this->query( 'SET NAMES ' . $charset, __METHOD__
);
113 protected function closeConnection() {
114 $conn = $this->getBindingHandle();
116 return mysql_close( $conn );
122 function insertId() {
123 $conn = $this->getBindingHandle();
125 return mysql_insert_id( $conn );
131 function lastErrno() {
132 if ( $this->mConn
) {
133 return mysql_errno( $this->mConn
);
135 return mysql_errno();
142 function affectedRows() {
143 $conn = $this->getBindingHandle();
145 return mysql_affected_rows( $conn );
152 function selectDB( $db ) {
153 $conn = $this->getBindingHandle();
155 $this->mDBname
= $db;
157 return mysql_select_db( $db, $conn );
160 protected function mysqlFreeResult( $res ) {
161 return mysql_free_result( $res );
164 protected function mysqlFetchObject( $res ) {
165 return mysql_fetch_object( $res );
168 protected function mysqlFetchArray( $res ) {
169 return mysql_fetch_array( $res );
172 protected function mysqlNumRows( $res ) {
173 return mysql_num_rows( $res );
176 protected function mysqlNumFields( $res ) {
177 return mysql_num_fields( $res );
180 protected function mysqlFetchField( $res, $n ) {
181 return mysql_fetch_field( $res, $n );
184 protected function mysqlFieldName( $res, $n ) {
185 return mysql_field_name( $res, $n );
188 protected function mysqlFieldType( $res, $n ) {
189 return mysql_field_type( $res, $n );
192 protected function mysqlDataSeek( $res, $row ) {
193 return mysql_data_seek( $res, $row );
196 protected function mysqlError( $conn = null ) {
197 return ( $conn !== null ) ?
mysql_error( $conn ) : mysql_error(); // avoid warning
200 protected function mysqlRealEscapeString( $s ) {
201 $conn = $this->getBindingHandle();
203 return mysql_real_escape_string( $s, $conn );
207 class_alias( DatabaseMysql
::class, 'DatabaseMysql' );