Add support for mysqli extension
[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 /**
34 * @param $sql string
35 * @return resource
36 */
37 protected function doQuery( $sql ) {
38 if ( $this->bufferResults() ) {
39 $ret = $this->mConn->query( $sql );
40 } else {
41 $ret = $this->mConn->query( $sql, MYSQLI_USE_RESULT );
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 closeConnection() {
86 return $this->mConn->close();
87 }
88
89 /**
90 * @return int
91 */
92 function insertId() {
93 return $this->mConn->insert_id;
94 }
95
96 /**
97 * @return int
98 */
99 function lastErrno() {
100 if ( $this->mConn ) {
101 return $this->mConn->errno;
102 } else {
103 return mysqli_connect_errno();
104 }
105 }
106
107 /**
108 * @return int
109 */
110 function affectedRows() {
111 return $this->mConn->affected_rows;
112 }
113
114 /**
115 * @param $db
116 * @return bool
117 */
118 function selectDB( $db ) {
119 $this->mDBname = $db;
120 return $this->mConn->select_db( $db );
121 }
122
123 /**
124 * @return string
125 */
126 function getServerVersion() {
127 return $this->mConn->server_info;
128 }
129
130 protected function mysqlFreeResult( $res ) {
131 $res->free_result();
132 return true;
133 }
134
135 protected function mysqlFetchObject( $res ) {
136 $object = $res->fetch_object();
137 if ( $object === null ) {
138 return false;
139 }
140 return $object;
141 }
142
143 protected function mysqlFetchArray( $res ) {
144 $array = $res->fetch_array();
145 if ( $array === null ) {
146 return false;
147 }
148 return $array;
149 }
150
151 protected function mysqlNumRows( $res ) {
152 return $res->num_rows;
153 }
154
155 protected function mysqlNumFields( $res ) {
156 return $res->field_count;
157 }
158
159 protected function mysqlFetchField( $res, $n ) {
160 $field = $res->fetch_field_direct( $n );
161 $field->not_null = $field->flags & MYSQLI_NOT_NULL_FLAG;
162 $field->primary_key = $field->flags & MYSQLI_PRI_KEY_FLAG;
163 $field->unique_key = $field->flags & MYSQLI_UNIQUE_KEY_FLAG;
164 $field->multiple_key = $field->flags & MYSQLI_MULTIPLE_KEY_FLAG;
165 $field->binary = $field->flags & MYSQLI_BINARY_FLAG;
166 return $field;
167 }
168
169 protected function mysqlFieldName( $res, $n ) {
170 $field = $res->fetch_field_direct( $n );
171 return $field->name;
172 }
173
174 protected function mysqlDataSeek( $res, $row ) {
175 return $res->data_seek( $row );
176 }
177
178 protected function mysqlError( $conn = null ) {
179 if ($conn === null) {
180 return mysqli_connect_error();
181 } else {
182 return $conn->error;
183 }
184 }
185
186 protected function mysqlRealEscapeString( $s ) {
187 return $this->mConn->real_escape_string( $s );
188 }
189
190 protected function mysqlPing() {
191 return $this->mConn->ping();
192 }
193
194 }