database: Throw exceptions when dead mysql DB handles are used instead of fatals
[lhc/web/wiklou.git] / includes / db / DatabaseMysql.php
1 <?php
2 /**
3 * This is the MySQL database abstraction layer.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup Database
22 */
23
24 /**
25 * Database abstraction object for PHP extension mysql.
26 *
27 * @ingroup Database
28 * @see Database
29 */
30 class DatabaseMysql extends DatabaseMysqlBase {
31 /**
32 * @param string $sql
33 * @return resource False on error
34 */
35 protected function doQuery( $sql ) {
36 $conn = $this->getBindingHandle();
37
38 if ( $this->bufferResults() ) {
39 $ret = mysql_query( $sql, $conn );
40 } else {
41 $ret = mysql_unbuffered_query( $sql, $conn );
42 }
43
44 return $ret;
45 }
46
47 /**
48 * @param string $realServer
49 * @return bool|resource MySQL Database connection or false on failure to connect
50 * @throws DBConnectionError
51 */
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(
56 $this,
57 "MySQL functions missing, have you compiled PHP with the --with-mysql option?\n"
58 );
59 }
60
61 $connFlags = 0;
62 if ( $this->mFlags & DBO_SSL ) {
63 $connFlags |= MYSQL_CLIENT_SSL;
64 }
65 if ( $this->mFlags & DBO_COMPRESS ) {
66 $connFlags |= MYSQL_CLIENT_COMPRESS;
67 }
68
69 if ( ini_get( 'mysql.connect_timeout' ) <= 3 ) {
70 $numAttempts = 2;
71 } else {
72 $numAttempts = 1;
73 }
74
75 $conn = false;
76
77 for ( $i = 0; $i < $numAttempts && !$conn; $i++ ) {
78 if ( $i > 1 ) {
79 usleep( 1000 );
80 }
81 if ( $this->mFlags & DBO_PERSISTENT ) {
82 $conn = mysql_pconnect( $realServer, $this->mUser, $this->mPassword, $connFlags );
83 } else {
84 # Create a new connection...
85 $conn = mysql_connect( $realServer, $this->mUser, $this->mPassword, true, $connFlags );
86 }
87 }
88
89 return $conn;
90 }
91
92 /**
93 * @param string $charset
94 * @return bool
95 */
96 protected function mysqlSetCharset( $charset ) {
97 $conn = $this->getBindingHandle();
98
99 if ( function_exists( 'mysql_set_charset' ) ) {
100 return mysql_set_charset( $charset, $conn );
101 } else {
102 return $this->query( 'SET NAMES ' . $charset, __METHOD__ );
103 }
104 }
105
106 /**
107 * @return bool
108 */
109 protected function closeConnection() {
110 $conn = $this->getBindingHandle();
111
112 return mysql_close( $conn );
113 }
114
115 /**
116 * @return int
117 */
118 function insertId() {
119 $conn = $this->getBindingHandle();
120
121 return mysql_insert_id( $conn );
122 }
123
124 /**
125 * @return int
126 */
127 function lastErrno() {
128 if ( $this->mConn ) {
129 return mysql_errno( $this->mConn );
130 } else {
131 return mysql_errno();
132 }
133 }
134
135 /**
136 * @return int
137 */
138 function affectedRows() {
139 $conn = $this->getBindingHandle();
140
141 return mysql_affected_rows( $conn );
142 }
143
144 /**
145 * @param string $db
146 * @return bool
147 */
148 function selectDB( $db ) {
149 $conn = $this->getBindingHandle();
150
151 $this->mDBname = $db;
152
153 return mysql_select_db( $db, $conn );
154 }
155
156 protected function mysqlFreeResult( $res ) {
157 return mysql_free_result( $res );
158 }
159
160 protected function mysqlFetchObject( $res ) {
161 return mysql_fetch_object( $res );
162 }
163
164 protected function mysqlFetchArray( $res ) {
165 return mysql_fetch_array( $res );
166 }
167
168 protected function mysqlNumRows( $res ) {
169 return mysql_num_rows( $res );
170 }
171
172 protected function mysqlNumFields( $res ) {
173 return mysql_num_fields( $res );
174 }
175
176 protected function mysqlFetchField( $res, $n ) {
177 return mysql_fetch_field( $res, $n );
178 }
179
180 protected function mysqlFieldName( $res, $n ) {
181 return mysql_field_name( $res, $n );
182 }
183
184 protected function mysqlFieldType( $res, $n ) {
185 return mysql_field_type( $res, $n );
186 }
187
188 protected function mysqlDataSeek( $res, $row ) {
189 return mysql_data_seek( $res, $row );
190 }
191
192 protected function mysqlError( $conn = null ) {
193 return ( $conn !== null ) ? mysql_error( $conn ) : mysql_error(); // avoid warning
194 }
195
196 protected function mysqlRealEscapeString( $s ) {
197 $conn = $this->getBindingHandle();
198
199 return mysql_real_escape_string( $s, $conn );
200 }
201
202 protected function mysqlPing() {
203 $conn = $this->getBindingHandle();
204
205 return mysql_ping( $conn );
206 }
207 }