Merge "Add one more missing directory to findHooks.php"
[lhc/web/wiklou.git] / includes / db / DatabaseMysqli.php
1 <?php
2 /**
3 * This is the MySQLi 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 mysqli.
26 *
27 * @ingroup Database
28 * @since 1.22
29 * @see Database
30 */
31 class DatabaseMysqli extends DatabaseMysqlBase {
32 /**
33 * @param $sql string
34 * @return resource
35 */
36 protected function doQuery( $sql ) {
37 if ( $this->bufferResults() ) {
38 $ret = $this->mConn->query( $sql );
39 } else {
40 $ret = $this->mConn->query( $sql, MYSQLI_USE_RESULT );
41 }
42
43 return $ret;
44 }
45
46 protected function mysqlConnect( $realServer ) {
47 # Fail now
48 # Otherwise we get a suppressed fatal error, which is very hard to track down
49 if ( !function_exists( 'mysqli_init' ) ) {
50 throw new DBConnectionError( $this, "MySQLi functions missing,"
51 . " have you compiled PHP with the --with-mysqli option?\n" );
52 }
53
54 $connFlags = 0;
55 if ( $this->mFlags & DBO_SSL ) {
56 $connFlags |= MYSQLI_CLIENT_SSL;
57 }
58 if ( $this->mFlags & DBO_COMPRESS ) {
59 $connFlags |= MYSQLI_CLIENT_COMPRESS;
60 }
61 if ( $this->mFlags & DBO_PERSISTENT ) {
62 $realServer = 'p:' . $realServer;
63 }
64
65 $mysqli = mysqli_init();
66 $numAttempts = 2;
67
68 for ( $i = 0; $i < $numAttempts; $i++ ) {
69 if ( $i > 1 ) {
70 usleep( 1000 );
71 }
72 if ( $mysqli->real_connect( $realServer, $this->mUser,
73 $this->mPassword, $this->mDBname, null, null, $connFlags )
74 ) {
75 return $mysqli;
76 }
77 }
78
79 return false;
80 }
81
82 /**
83 * @return bool
84 */
85 protected function mysqlSetCharset( $charset ) {
86 if ( method_exists( $this->mConn, 'set_charset' ) ) {
87 return $this->mConn->set_charset( $charset );
88 } else {
89 return $this->query( 'SET NAMES ' . $charset, __METHOD__ );
90 }
91 }
92
93 /**
94 * @return bool
95 */
96 protected function closeConnection() {
97 return $this->mConn->close();
98 }
99
100 /**
101 * @return int
102 */
103 function insertId() {
104 return $this->mConn->insert_id;
105 }
106
107 /**
108 * @return int
109 */
110 function lastErrno() {
111 if ( $this->mConn ) {
112 return $this->mConn->errno;
113 } else {
114 return mysqli_connect_errno();
115 }
116 }
117
118 /**
119 * @return int
120 */
121 function affectedRows() {
122 return $this->mConn->affected_rows;
123 }
124
125 /**
126 * @param $db
127 * @return bool
128 */
129 function selectDB( $db ) {
130 $this->mDBname = $db;
131
132 return $this->mConn->select_db( $db );
133 }
134
135 /**
136 * @return string
137 */
138 function getServerVersion() {
139 return $this->mConn->server_info;
140 }
141
142 protected function mysqlFreeResult( $res ) {
143 $res->free_result();
144
145 return true;
146 }
147
148 protected function mysqlFetchObject( $res ) {
149 $object = $res->fetch_object();
150 if ( $object === null ) {
151 return false;
152 }
153
154 return $object;
155 }
156
157 protected function mysqlFetchArray( $res ) {
158 $array = $res->fetch_array();
159 if ( $array === null ) {
160 return false;
161 }
162
163 return $array;
164 }
165
166 protected function mysqlNumRows( $res ) {
167 return $res->num_rows;
168 }
169
170 protected function mysqlNumFields( $res ) {
171 return $res->field_count;
172 }
173
174 protected function mysqlFetchField( $res, $n ) {
175 $field = $res->fetch_field_direct( $n );
176 $field->not_null = $field->flags & MYSQLI_NOT_NULL_FLAG;
177 $field->primary_key = $field->flags & MYSQLI_PRI_KEY_FLAG;
178 $field->unique_key = $field->flags & MYSQLI_UNIQUE_KEY_FLAG;
179 $field->multiple_key = $field->flags & MYSQLI_MULTIPLE_KEY_FLAG;
180 $field->binary = $field->flags & MYSQLI_BINARY_FLAG;
181
182 return $field;
183 }
184
185 protected function mysqlFieldName( $res, $n ) {
186 $field = $res->fetch_field_direct( $n );
187
188 return $field->name;
189 }
190
191 protected function mysqlFieldType( $res, $n ) {
192 $field = $res->fetch_field_direct( $n );
193 return $field->type;
194 }
195
196 protected function mysqlDataSeek( $res, $row ) {
197 return $res->data_seek( $row );
198 }
199
200 protected function mysqlError( $conn = null ) {
201 if ( $conn === null ) {
202 return mysqli_connect_error();
203 } else {
204 return $conn->error;
205 }
206 }
207
208 protected function mysqlRealEscapeString( $s ) {
209 return $this->mConn->real_escape_string( $s );
210 }
211
212 protected function mysqlPing() {
213 return $this->mConn->ping();
214 }
215 }