From a67897ac9e4ee66587a0ad6d427dfacd66a460dd Mon Sep 17 00:00:00 2001 From: Aaron Schulz Date: Sat, 15 Jun 2019 11:42:14 +0100 Subject: [PATCH] rdbms: clean up and simplify toString() handle for Database Change-Id: Ia95e25505785aad71b70a74f4ec7a07a73e419e1 --- includes/libs/rdbms/database/DBConnRef.php | 13 +++++++++++ includes/libs/rdbms/database/Database.php | 22 ++++++++++++++----- .../libs/rdbms/database/DatabaseMysqli.php | 15 ------------- .../libs/rdbms/database/DatabaseSqlite.php | 9 -------- includes/libs/rdbms/database/IDatabase.php | 9 ++++++++ .../includes/db/DatabaseSqliteTest.php | 2 +- 6 files changed, 40 insertions(+), 30 deletions(-) diff --git a/includes/libs/rdbms/database/DBConnRef.php b/includes/libs/rdbms/database/DBConnRef.php index b216892486..841a7ba40a 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 c6b1662007..b7b45bdc85 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 #" + } 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 1a5cdab78d..937f381ac1 100644 --- a/includes/libs/rdbms/database/DatabaseMysqli.php +++ b/includes/libs/rdbms/database/DatabaseMysqli.php @@ -307,21 +307,6 @@ class DatabaseMysqli extends DatabaseMysqlBase { return $conn->real_escape_string( (string)$s ); } - /** - * 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 */ diff --git a/includes/libs/rdbms/database/DatabaseSqlite.php b/includes/libs/rdbms/database/DatabaseSqlite.php index 3722ef430b..dcc7c46c3a 100644 --- a/includes/libs/rdbms/database/DatabaseSqlite.php +++ b/includes/libs/rdbms/database/DatabaseSqlite.php @@ -1119,15 +1119,6 @@ class DatabaseSqlite extends Database { return true; } - /** - * @return string - */ - public function __toString() { - return is_object( $this->conn ) - ? 'SQLite ' . (string)$this->conn->getAttribute( PDO::ATTR_SERVER_VERSION ) - : '(not connected)'; - } - /** * @return PDO */ diff --git a/includes/libs/rdbms/database/IDatabase.php b/includes/libs/rdbms/database/IDatabase.php index 89a66e82d0..802af15c5b 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 " object #" or " object # (resource/handle id #)" + * @since 1.34 + */ + public function __toString(); } /** diff --git a/tests/phpunit/includes/db/DatabaseSqliteTest.php b/tests/phpunit/includes/db/DatabaseSqliteTest.php index 857988c899..0f5c1f2f7f 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 ); } /** -- 2.20.1