rdbms: optimize Database::selectDomain() to avoid extra queries
authorAaron Schulz <aschulz@wikimedia.org>
Fri, 8 Mar 2019 22:04:54 +0000 (14:04 -0800)
committerAaron Schulz <aschulz@wikimedia.org>
Fri, 8 Mar 2019 22:11:41 +0000 (14:11 -0800)
Also clean up and align the mysql subclass versions of the method.
Enforce that raw "USE" queries are not passed in, since they would
break the tracking (even before this change).

Change-Id: I11c9145c6c0525f27a4ec8d94c500a22a712b320

includes/libs/rdbms/database/Database.php
includes/libs/rdbms/database/DatabaseMssql.php
includes/libs/rdbms/database/DatabaseMysqli.php

index bafdb44..3475b09 100644 (file)
@@ -1347,12 +1347,19 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware
        }
 
        /**
+        * Error out if the DB is not in a valid state for a query via query()
+        *
         * @param string $sql
         * @param string $fname
         * @throws DBTransactionStateError
         */
        private function assertTransactionStatus( $sql, $fname ) {
-               if ( $this->getQueryVerb( $sql ) === 'ROLLBACK' ) { // transaction/savepoint
+               $verb = $this->getQueryVerb( $sql );
+               if ( $verb === 'USE' ) {
+                       throw new DBUnexpectedError( $this, "Got USE query; use selectDomain() instead." );
+               }
+
+               if ( $verb === 'ROLLBACK' ) { // transaction/savepoint
                        return;
                }
 
index 6e50f5f..ad5cf98 100644 (file)
@@ -1167,8 +1167,18 @@ class DatabaseMssql extends Database {
        }
 
        protected function doSelectDomain( DatabaseDomain $domain ) {
-               $encDatabase = $this->addIdentifierQuotes( $domain->getDatabase() );
-               $this->query( "USE $encDatabase" );
+               if ( $domain->getSchema() !== null ) {
+                       throw new DBExpectedError( $this, __CLASS__ . ": domain schemas are not supported." );
+               }
+
+               $database = $domain->getDatabase();
+               if ( $database !== $this->getDBname() ) {
+                       $encDatabase = $this->addIdentifierQuotes( $database );
+                       $res = $this->doQuery( "USE $encDatabase" );
+                       if ( !$res ) {
+                               throw new DBExpectedError( $this, "Could not select database '$database'." );
+                       }
+               }
                // Update that domain fields on success (no exception thrown)
                $this->currentDomain = $domain;
 
index a3907ca..d0bd1b3 100644 (file)
@@ -185,15 +185,16 @@ class DatabaseMysqli extends DatabaseMysqlBase {
        }
 
        function doSelectDomain( DatabaseDomain $domain ) {
-               $conn = $this->getBindingHandle();
-
                if ( $domain->getSchema() !== null ) {
                        throw new DBExpectedError( $this, __CLASS__ . ": domain schemas are not supported." );
                }
 
                $database = $domain->getDatabase();
-               if ( !$conn->select_db( $database ) ) {
-                       throw new DBExpectedError( $this, "Could not select database '$database'." );
+               if ( $database !== $this->getDBname() ) {
+                       $conn = $this->getBindingHandle();
+                       if ( !$conn->select_db( $database ) ) {
+                               throw new DBExpectedError( $this, "Could not select database '$database'." );
+                       }
                }
 
                // Update that domain fields on success (no exception thrown)