58b092669bbab7ab7667a912e0da0e4ef312e3ec
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.
28 * @deprecated 1.30 PHP extension 'mysql' was deprecated in PHP 5.5 and removed in PHP 7.0.
29 * @see PHP extension 'mysqli' and DatabaseMysqli
34 class DatabaseMysql
extends DatabaseMysqlBase
{
37 * @return resource False on error
39 protected function doQuery( $sql ) {
40 $conn = $this->getBindingHandle();
42 if ( $this->bufferResults() ) {
43 $ret = mysql_query( $sql, $conn );
45 $ret = mysql_unbuffered_query( $sql, $conn );
52 * @param string $realServer
53 * @return bool|resource MySQL Database connection or false on failure to connect
54 * @throws DBConnectionError
56 protected function mysqlConnect( $realServer ) {
57 # Avoid a suppressed fatal error, which is very hard to track down
58 if ( !extension_loaded( 'mysql' ) ) {
59 throw new DBConnectionError(
61 "MySQL functions missing, have you compiled PHP with the --with-mysql option?\n"
66 if ( $this->mFlags
& self
::DBO_SSL
) {
67 $connFlags |
= MYSQL_CLIENT_SSL
;
69 if ( $this->mFlags
& self
::DBO_COMPRESS
) {
70 $connFlags |
= MYSQL_CLIENT_COMPRESS
;
73 if ( ini_get( 'mysql.connect_timeout' ) <= 3 ) {
81 # The kernel's default SYN retransmission period is far too slow for us,
82 # so we use a short timeout plus a manual retry. Retrying means that a small
83 # but finite rate of SYN packet loss won't cause user-visible errors.
84 for ( $i = 0; $i < $numAttempts && !$conn; $i++
) {
88 if ( $this->mFlags
& self
::DBO_PERSISTENT
) {
89 $conn = mysql_pconnect( $realServer, $this->mUser
, $this->mPassword
, $connFlags );
91 # Create a new connection...
92 $conn = mysql_connect( $realServer, $this->mUser
, $this->mPassword
, true, $connFlags );
100 * @param string $charset
103 protected function mysqlSetCharset( $charset ) {
104 $conn = $this->getBindingHandle();
106 if ( function_exists( 'mysql_set_charset' ) ) {
107 return mysql_set_charset( $charset, $conn );
109 return $this->query( 'SET NAMES ' . $charset, __METHOD__
);
116 protected function closeConnection() {
117 $conn = $this->getBindingHandle();
119 return mysql_close( $conn );
125 function insertId() {
126 $conn = $this->getBindingHandle();
128 return mysql_insert_id( $conn );
134 function lastErrno() {
135 if ( $this->mConn
) {
136 return mysql_errno( $this->mConn
);
138 return mysql_errno();
145 function affectedRows() {
146 $conn = $this->getBindingHandle();
148 return mysql_affected_rows( $conn );
155 function selectDB( $db ) {
156 $conn = $this->getBindingHandle();
158 $this->mDBname
= $db;
160 return mysql_select_db( $db, $conn );
163 protected function mysqlFreeResult( $res ) {
164 return mysql_free_result( $res );
167 protected function mysqlFetchObject( $res ) {
168 return mysql_fetch_object( $res );
171 protected function mysqlFetchArray( $res ) {
172 return mysql_fetch_array( $res );
175 protected function mysqlNumRows( $res ) {
176 return mysql_num_rows( $res );
179 protected function mysqlNumFields( $res ) {
180 return mysql_num_fields( $res );
183 protected function mysqlFetchField( $res, $n ) {
184 return mysql_fetch_field( $res, $n );
187 protected function mysqlFieldName( $res, $n ) {
188 return mysql_field_name( $res, $n );
191 protected function mysqlFieldType( $res, $n ) {
192 return mysql_field_type( $res, $n );
195 protected function mysqlDataSeek( $res, $row ) {
196 return mysql_data_seek( $res, $row );
199 protected function mysqlError( $conn = null ) {
200 return ( $conn !== null ) ?
mysql_error( $conn ) : mysql_error(); // avoid warning
203 protected function mysqlRealEscapeString( $s ) {
204 $conn = $this->getBindingHandle();
206 return mysql_real_escape_string( (string)$s, $conn );
210 class_alias( DatabaseMysql
::class, 'DatabaseMysql' );