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