rdbms: clean up and simplify toString() handle for Database
authorAaron Schulz <aschulz@wikimedia.org>
Sat, 15 Jun 2019 10:42:14 +0000 (11:42 +0100)
committerAaron Schulz <aschulz@wikimedia.org>
Sat, 15 Jun 2019 11:14:17 +0000 (12:14 +0100)
Change-Id: Ia95e25505785aad71b70a74f4ec7a07a73e419e1

includes/libs/rdbms/database/DBConnRef.php
includes/libs/rdbms/database/Database.php
includes/libs/rdbms/database/DatabaseMysqli.php
includes/libs/rdbms/database/DatabaseSqlite.php
includes/libs/rdbms/database/IDatabase.php
tests/phpunit/includes/db/DatabaseSqliteTest.php

index b216892..841a7ba 100644 (file)
@@ -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
         *
index c6b1662..b7b45bd 100644 (file)
@@ -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;
        }
 
        /**
index 1a5cdab..937f381 100644 (file)
@@ -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
         */
index 3722ef4..dcc7c46 100644 (file)
@@ -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
         */
index 89a66e8..802af15 100644 (file)
@@ -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();
 }
 
 /**
index 857988c..0f5c1f2 100644 (file)
@@ -540,7 +540,7 @@ class DatabaseSqliteTest extends MediaWikiTestCase {
 
                $toString = (string)$db;
 
-               $this->assertContains( 'SQLite ', $toString );
+               $this->assertContains( 'sqlite object', $toString );
        }
 
        /**