b600d9484693005a7d3e3f688694c98be3e9e29a
[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 $sql string
33 * @return resource
34 */
35 protected function doQuery( $sql ) {
36 if ( $this->bufferResults() ) {
37 $ret = mysql_query( $sql, $this->mConn );
38 } else {
39 $ret = mysql_unbuffered_query( $sql, $this->mConn );
40 }
41
42 return $ret;
43 }
44
45 protected function mysqlConnect( $realServer ) {
46 # Fail now
47 # Otherwise we get a suppressed fatal error, which is very hard to track down
48 if ( !extension_loaded( 'mysql' ) ) {
49 throw new DBConnectionError(
50 $this,
51 "MySQL functions missing, have you compiled PHP with the --with-mysql option?\n"
52 );
53 }
54
55 $connFlags = 0;
56 if ( $this->mFlags & DBO_SSL ) {
57 $connFlags |= MYSQL_CLIENT_SSL;
58 }
59 if ( $this->mFlags & DBO_COMPRESS ) {
60 $connFlags |= MYSQL_CLIENT_COMPRESS;
61 }
62
63 if ( ini_get( 'mysql.connect_timeout' ) <= 3 ) {
64 $numAttempts = 2;
65 } else {
66 $numAttempts = 1;
67 }
68
69 $conn = false;
70
71 for ( $i = 0; $i < $numAttempts && !$conn; $i++ ) {
72 if ( $i > 1 ) {
73 usleep( 1000 );
74 }
75 if ( $this->mFlags & DBO_PERSISTENT ) {
76 $conn = mysql_pconnect( $realServer, $this->mUser, $this->mPassword, $connFlags );
77 } else {
78 # Create a new connection...
79 $conn = mysql_connect( $realServer, $this->mUser, $this->mPassword, true, $connFlags );
80 }
81 }
82
83 return $conn;
84 }
85
86 /**
87 * @return bool
88 */
89 protected function mysqlSetCharset( $charset ) {
90 if ( function_exists( 'mysql_set_charset' ) ) {
91 return mysql_set_charset( $charset, $this->mConn );
92 } else {
93 return $this->query( 'SET NAMES ' . $charset, __METHOD__ );
94 }
95 }
96
97 /**
98 * @return bool
99 */
100 protected function closeConnection() {
101 return mysql_close( $this->mConn );
102 }
103
104 /**
105 * @return int
106 */
107 function insertId() {
108 return mysql_insert_id( $this->mConn );
109 }
110
111 /**
112 * @return int
113 */
114 function lastErrno() {
115 if ( $this->mConn ) {
116 return mysql_errno( $this->mConn );
117 } else {
118 return mysql_errno();
119 }
120 }
121
122 /**
123 * @return int
124 */
125 function affectedRows() {
126 return mysql_affected_rows( $this->mConn );
127 }
128
129 /**
130 * @param $db
131 * @return bool
132 */
133 function selectDB( $db ) {
134 $this->mDBname = $db;
135
136 return mysql_select_db( $db, $this->mConn );
137 }
138
139 /**
140 * @return string
141 */
142 function getServerVersion() {
143 return mysql_get_server_info( $this->mConn );
144 }
145
146 protected function mysqlFreeResult( $res ) {
147 return mysql_free_result( $res );
148 }
149
150 protected function mysqlFetchObject( $res ) {
151 return mysql_fetch_object( $res );
152 }
153
154 protected function mysqlFetchArray( $res ) {
155 return mysql_fetch_array( $res );
156 }
157
158 protected function mysqlNumRows( $res ) {
159 return mysql_num_rows( $res );
160 }
161
162 protected function mysqlNumFields( $res ) {
163 return mysql_num_fields( $res );
164 }
165
166 protected function mysqlFetchField( $res, $n ) {
167 return mysql_fetch_field( $res, $n );
168 }
169
170 protected function mysqlFieldName( $res, $n ) {
171 return mysql_field_name( $res, $n );
172 }
173
174 protected function mysqlFieldType( $res, $n ) {
175 return mysql_field_type( $res, $n );
176 }
177
178 protected function mysqlDataSeek( $res, $row ) {
179 return mysql_data_seek( $res, $row );
180 }
181
182 protected function mysqlError( $conn = null ) {
183 return ( $conn !== null ) ? mysql_error( $conn ) : mysql_error(); // avoid warning
184 }
185
186 protected function mysqlRealEscapeString( $s ) {
187 return mysql_real_escape_string( $s, $this->mConn );
188 }
189
190 protected function mysqlPing() {
191 return mysql_ping( $this->mConn );
192 }
193 }