aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAaron Schulz <aschulz@wikimedia.org>2019-06-15 11:42:14 +0100
committerAaron Schulz <aschulz@wikimedia.org>2019-06-15 12:14:17 +0100
commita67897ac9e4ee66587a0ad6d427dfacd66a460dd (patch)
tree6c1febaf7a94f4ba6cd86f8d66bb824369909eb9
parent26fe4653c09cbc6de3d48939e802f40c1eeabdc1 (diff)
downloadmediawikicore-a67897ac9e4ee66587a0ad6d427dfacd66a460dd.tar.gz
mediawikicore-a67897ac9e4ee66587a0ad6d427dfacd66a460dd.zip
rdbms: clean up and simplify toString() handle for Database
Change-Id: Ia95e25505785aad71b70a74f4ec7a07a73e419e1
-rw-r--r--includes/libs/rdbms/database/DBConnRef.php13
-rw-r--r--includes/libs/rdbms/database/Database.php22
-rw-r--r--includes/libs/rdbms/database/DatabaseMysqli.php15
-rw-r--r--includes/libs/rdbms/database/DatabaseSqlite.php9
-rw-r--r--includes/libs/rdbms/database/IDatabase.php9
-rw-r--r--tests/phpunit/includes/db/DatabaseSqliteTest.php2
6 files changed, 40 insertions, 30 deletions
diff --git a/includes/libs/rdbms/database/DBConnRef.php b/includes/libs/rdbms/database/DBConnRef.php
index b21689248618..841a7ba40a75 100644
--- a/includes/libs/rdbms/database/DBConnRef.php
+++ b/includes/libs/rdbms/database/DBConnRef.php
@@ -740,6 +740,19 @@ class DBConnRef implements IDatabase {
return $this->__call( __FUNCTION__, func_get_args() );
}
+ public function __toString() {
+ if ( $this->conn === null ) {
+ // spl_object_id is PHP >= 7.2
+ $id = function_exists( 'spl_object_id' )
+ ? spl_object_id( $this )
+ : spl_object_hash( $this );
+
+ return $this->getType() . ' object #' . $id;
+ }
+
+ return $this->__call( __FUNCTION__, func_get_args() );
+ }
+
/**
* Error out if the role is not DB_MASTER
*
diff --git a/includes/libs/rdbms/database/Database.php b/includes/libs/rdbms/database/Database.php
index c6b166200723..b7b45bdc85eb 100644
--- a/includes/libs/rdbms/database/Database.php
+++ b/includes/libs/rdbms/database/Database.php
@@ -4666,12 +4666,24 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware
return $this->conn;
}
- /**
- * @since 1.19
- * @return string
- */
public function __toString() {
- return (string)$this->conn;
+ // spl_object_id is PHP >= 7.2
+ $id = function_exists( 'spl_object_id' )
+ ? spl_object_id( $this )
+ : spl_object_hash( $this );
+
+ $description = $this->getType() . ' object #' . $id;
+ if ( is_resource( $this->conn ) ) {
+ $description .= ' (' . (string)$this->conn . ')'; // "resource id #<ID>"
+ } elseif ( is_object( $this->conn ) ) {
+ // spl_object_id is PHP >= 7.2
+ $handleId = function_exists( 'spl_object_id' )
+ ? spl_object_id( $this->conn )
+ : spl_object_hash( $this->conn );
+ $description .= " (handle id #$handleId)";
+ }
+
+ return $description;
}
/**
diff --git a/includes/libs/rdbms/database/DatabaseMysqli.php b/includes/libs/rdbms/database/DatabaseMysqli.php
index 1a5cdab78dad..937f381ac14d 100644
--- a/includes/libs/rdbms/database/DatabaseMysqli.php
+++ b/includes/libs/rdbms/database/DatabaseMysqli.php
@@ -308,21 +308,6 @@ class DatabaseMysqli extends DatabaseMysqlBase {
}
/**
- * 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->conn instanceof mysqli ) {
- return (string)$this->conn->thread_id;
- } else {
- // mConn might be false or something.
- return (string)$this->conn;
- }
- }
-
- /**
* @return mysqli
*/
protected function getBindingHandle() {
diff --git a/includes/libs/rdbms/database/DatabaseSqlite.php b/includes/libs/rdbms/database/DatabaseSqlite.php
index 3722ef430bc6..dcc7c46c3aa6 100644
--- a/includes/libs/rdbms/database/DatabaseSqlite.php
+++ b/includes/libs/rdbms/database/DatabaseSqlite.php
@@ -1120,15 +1120,6 @@ class DatabaseSqlite extends Database {
}
/**
- * @return string
- */
- public function __toString() {
- return is_object( $this->conn )
- ? 'SQLite ' . (string)$this->conn->getAttribute( PDO::ATTR_SERVER_VERSION )
- : '(not connected)';
- }
-
- /**
* @return PDO
*/
protected function getBindingHandle() {
diff --git a/includes/libs/rdbms/database/IDatabase.php b/includes/libs/rdbms/database/IDatabase.php
index 89a66e82d094..802af15c5bf8 100644
--- a/includes/libs/rdbms/database/IDatabase.php
+++ b/includes/libs/rdbms/database/IDatabase.php
@@ -2199,6 +2199,15 @@ interface IDatabase {
* @since 1.31
*/
public function setIndexAliases( array $aliases );
+
+ /**
+ * Get a debugging string that mentions the database type, the ID of this instance,
+ * and the ID of any underlying connection resource or driver object if one is present
+ *
+ * @return string "<db type> object #<X>" or "<db type> object #<X> (resource/handle id #<Y>)"
+ * @since 1.34
+ */
+ public function __toString();
}
/**
diff --git a/tests/phpunit/includes/db/DatabaseSqliteTest.php b/tests/phpunit/includes/db/DatabaseSqliteTest.php
index 857988c8992c..0f5c1f2f7f4c 100644
--- a/tests/phpunit/includes/db/DatabaseSqliteTest.php
+++ b/tests/phpunit/includes/db/DatabaseSqliteTest.php
@@ -540,7 +540,7 @@ class DatabaseSqliteTest extends MediaWikiTestCase {
$toString = (string)$db;
- $this->assertContains( 'SQLite ', $toString );
+ $this->assertContains( 'sqlite object', $toString );
}
/**