summaryrefslogtreecommitdiff
path: root/includes/db/DatabaseMysqli.php
diff options
context:
space:
mode:
Diffstat (limited to 'includes/db/DatabaseMysqli.php')
-rw-r--r--includes/db/DatabaseMysqli.php138
1 files changed, 123 insertions, 15 deletions
diff --git a/includes/db/DatabaseMysqli.php b/includes/db/DatabaseMysqli.php
index 0ec54314..a03c9aaf 100644
--- a/includes/db/DatabaseMysqli.php
+++ b/includes/db/DatabaseMysqli.php
@@ -29,9 +29,8 @@
* @see Database
*/
class DatabaseMysqli extends DatabaseMysqlBase {
-
/**
- * @param $sql string
+ * @param string $sql
* @return resource
*/
protected function doQuery( $sql ) {
@@ -40,10 +39,17 @@ class DatabaseMysqli extends DatabaseMysqlBase {
} else {
$ret = $this->mConn->query( $sql, MYSQLI_USE_RESULT );
}
+
return $ret;
}
+ /**
+ * @param string $realServer
+ * @return bool|mysqli
+ * @throws DBConnectionError
+ */
protected function mysqlConnect( $realServer ) {
+ global $wgDBmysql5;
# Fail now
# Otherwise we get a suppressed fatal error, which is very hard to track down
if ( !function_exists( 'mysqli_init' ) ) {
@@ -52,14 +58,22 @@ class DatabaseMysqli extends DatabaseMysqlBase {
}
// Other than mysql_connect, mysqli_real_connect expects an explicit port
- // parameter. So we need to parse the port out of $realServer
+ // and socket parameters. So we need to parse the port and socket out of
+ // $realServer
$port = null;
+ $socket = null;
$hostAndPort = IP::splitHostAndPort( $realServer );
if ( $hostAndPort ) {
$realServer = $hostAndPort[0];
if ( $hostAndPort[1] ) {
$port = $hostAndPort[1];
}
+ } elseif ( substr_count( $realServer, ':' ) == 1 ) {
+ // If we have a colon and something that's not a port number
+ // inside the hostname, assume it's the socket location
+ $hostAndSocket = explode( ':', $realServer );
+ $realServer = $hostAndSocket[0];
+ $socket = $hostAndSocket[1];
}
$connFlags = 0;
@@ -74,22 +88,41 @@ class DatabaseMysqli extends DatabaseMysqlBase {
}
$mysqli = mysqli_init();
- $numAttempts = 2;
+ if ( $wgDBmysql5 ) {
+ // Tell the server we're communicating with it in UTF-8.
+ // This may engage various charset conversions.
+ $mysqli->options( MYSQLI_SET_CHARSET_NAME, 'utf8' );
+ } else {
+ $mysqli->options( MYSQLI_SET_CHARSET_NAME, 'binary' );
+ }
+ $mysqli->options( MYSQLI_OPT_CONNECT_TIMEOUT, 3 );
- for ( $i = 0; $i < $numAttempts; $i++ ) {
- if ( $i > 1 ) {
- usleep( 1000 );
- }
- if ( $mysqli->real_connect( $realServer, $this->mUser,
- $this->mPassword, $this->mDBname, $port, null, $connFlags ) )
- {
- return $mysqli;
- }
+ if ( $mysqli->real_connect( $realServer, $this->mUser,
+ $this->mPassword, $this->mDBname, $port, $socket, $connFlags )
+ ) {
+ return $mysqli;
}
return false;
}
+ protected function connectInitCharset() {
+ // already done in mysqlConnect()
+ return true;
+ }
+
+ /**
+ * @param string $charset
+ * @return bool
+ */
+ protected function mysqlSetCharset( $charset ) {
+ if ( method_exists( $this->mConn, 'set_charset' ) ) {
+ return $this->mConn->set_charset( $charset );
+ } else {
+ return $this->query( 'SET NAMES ' . $charset, __METHOD__ );
+ }
+ }
+
/**
* @return bool
*/
@@ -123,11 +156,12 @@ class DatabaseMysqli extends DatabaseMysqlBase {
}
/**
- * @param $db
+ * @param string $db
* @return bool
*/
function selectDB( $db ) {
$this->mDBname = $db;
+
return $this->mConn->select_db( $db );
}
@@ -138,35 +172,63 @@ class DatabaseMysqli extends DatabaseMysqlBase {
return $this->mConn->server_info;
}
+ /**
+ * @param mysqli $res
+ * @return bool
+ */
protected function mysqlFreeResult( $res ) {
$res->free_result();
+
return true;
}
+ /**
+ * @param mysqli $res
+ * @return bool
+ */
protected function mysqlFetchObject( $res ) {
$object = $res->fetch_object();
if ( $object === null ) {
return false;
}
+
return $object;
}
+ /**
+ * @param mysqli $res
+ * @return bool
+ */
protected function mysqlFetchArray( $res ) {
$array = $res->fetch_array();
if ( $array === null ) {
return false;
}
+
return $array;
}
+ /**
+ * @param mysqli $res
+ * @return mixed
+ */
protected function mysqlNumRows( $res ) {
return $res->num_rows;
}
+ /**
+ * @param mysqli $res
+ * @return mixed
+ */
protected function mysqlNumFields( $res ) {
return $res->field_count;
}
+ /**
+ * @param mysqli $res
+ * @param int $n
+ * @return mixed
+ */
protected function mysqlFetchField( $res, $n ) {
$field = $res->fetch_field_direct( $n );
$field->not_null = $field->flags & MYSQLI_NOT_NULL_FLAG;
@@ -174,26 +236,58 @@ class DatabaseMysqli extends DatabaseMysqlBase {
$field->unique_key = $field->flags & MYSQLI_UNIQUE_KEY_FLAG;
$field->multiple_key = $field->flags & MYSQLI_MULTIPLE_KEY_FLAG;
$field->binary = $field->flags & MYSQLI_BINARY_FLAG;
+
return $field;
}
+ /**
+ * @param resource|ResultWrapper $res
+ * @param int $n
+ * @return mixed
+ */
protected function mysqlFieldName( $res, $n ) {
$field = $res->fetch_field_direct( $n );
+
return $field->name;
}
+ /**
+ * @param resource|ResultWrapper $res
+ * @param int $n
+ * @return mixed
+ */
+ protected function mysqlFieldType( $res, $n ) {
+ $field = $res->fetch_field_direct( $n );
+
+ return $field->type;
+ }
+
+ /**
+ * @param resource|ResultWrapper $res
+ * @param int $row
+ * @return mixed
+ */
protected function mysqlDataSeek( $res, $row ) {
return $res->data_seek( $row );
}
+ /**
+ * @param mysqli $conn Optional connection object
+ * @return string
+ */
protected function mysqlError( $conn = null ) {
- if ($conn === null) {
+ if ( $conn === null ) {
return mysqli_connect_error();
} else {
return $conn->error;
}
}
+ /**
+ * Escapes special characters in a string for use in an SQL statement
+ * @param string $s
+ * @return string
+ */
protected function mysqlRealEscapeString( $s ) {
return $this->mConn->real_escape_string( $s );
}
@@ -202,4 +296,18 @@ class DatabaseMysqli extends DatabaseMysqlBase {
return $this->mConn->ping();
}
+ /**
+ * Give an id for the connection
+ *
+ * mysql driver used resource id, but mysqli objects cannot be cast to string.
+ * @return string
+ */
+ public function __toString() {
+ if ( $this->mConn instanceof Mysqli ) {
+ return (string)$this->mConn->thread_id;
+ } else {
+ // mConn might be false or something.
+ return (string)$this->mConn;
+ }
+ }
}