Add CollationFa
[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 # 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++ ) {
81 if ( $i > 1 ) {
82 usleep( 1000 );
83 }
84 if ( $this->mFlags & DBO_PERSISTENT ) {
85 $conn = mysql_pconnect( $realServer, $this->mUser, $this->mPassword, $connFlags );
86 } else {
87 # Create a new connection...
88 $conn = mysql_connect( $realServer, $this->mUser, $this->mPassword, true, $connFlags );
89 }
90 }
91
92 return $conn;
93 }
94
95 /**
96 * @param string $charset
97 * @return bool
98 */
99 protected function mysqlSetCharset( $charset ) {
100 $conn = $this->getBindingHandle();
101
102 if ( function_exists( 'mysql_set_charset' ) ) {
103 return mysql_set_charset( $charset, $conn );
104 } else {
105 return $this->query( 'SET NAMES ' . $charset, __METHOD__ );
106 }
107 }
108
109 /**
110 * @return bool
111 */
112 protected function closeConnection() {
113 $conn = $this->getBindingHandle();
114
115 return mysql_close( $conn );
116 }
117
118 /**
119 * @return int
120 */
121 function insertId() {
122 $conn = $this->getBindingHandle();
123
124 return mysql_insert_id( $conn );
125 }
126
127 /**
128 * @return int
129 */
130 function lastErrno() {
131 if ( $this->mConn ) {
132 return mysql_errno( $this->mConn );
133 } else {
134 return mysql_errno();
135 }
136 }
137
138 /**
139 * @return int
140 */
141 function affectedRows() {
142 $conn = $this->getBindingHandle();
143
144 return mysql_affected_rows( $conn );
145 }
146
147 /**
148 * @param string $db
149 * @return bool
150 */
151 function selectDB( $db ) {
152 $conn = $this->getBindingHandle();
153
154 $this->mDBname = $db;
155
156 return mysql_select_db( $db, $conn );
157 }
158
159 protected function mysqlFreeResult( $res ) {
160 return mysql_free_result( $res );
161 }
162
163 protected function mysqlFetchObject( $res ) {
164 return mysql_fetch_object( $res );
165 }
166
167 protected function mysqlFetchArray( $res ) {
168 return mysql_fetch_array( $res );
169 }
170
171 protected function mysqlNumRows( $res ) {
172 return mysql_num_rows( $res );
173 }
174
175 protected function mysqlNumFields( $res ) {
176 return mysql_num_fields( $res );
177 }
178
179 protected function mysqlFetchField( $res, $n ) {
180 return mysql_fetch_field( $res, $n );
181 }
182
183 protected function mysqlFieldName( $res, $n ) {
184 return mysql_field_name( $res, $n );
185 }
186
187 protected function mysqlFieldType( $res, $n ) {
188 return mysql_field_type( $res, $n );
189 }
190
191 protected function mysqlDataSeek( $res, $row ) {
192 return mysql_data_seek( $res, $row );
193 }
194
195 protected function mysqlError( $conn = null ) {
196 return ( $conn !== null ) ? mysql_error( $conn ) : mysql_error(); // avoid warning
197 }
198
199 protected function mysqlRealEscapeString( $s ) {
200 $conn = $this->getBindingHandle();
201
202 return mysql_real_escape_string( $s, $conn );
203 }
204
205 protected function mysqlPing() {
206 $conn = $this->getBindingHandle();
207
208 return mysql_ping( $conn );
209 }
210 }