Fix the tableExists method of MysqlBase
authormainframe98 <k.s.werf@hotmail.com>
Mon, 19 Jun 2017 17:20:43 +0000 (19:20 +0200)
committerAaron Schulz <aschulz@wikimedia.org>
Thu, 22 Jun 2017 21:12:47 +0000 (21:12 +0000)
The table name generated by Database::tableName() does not work in
MysqlBase's tableExist method, as the syntax of SHOW TABLES does not
function like other foreign db queries. It instead should use FROM
$database rather than the database name in front of the table name.

Bug: T168207
Change-Id: I7806090eaa647959fd34de8bc606eeb952161529

includes/libs/rdbms/database/DatabaseMysqlBase.php

index 50ead83..e237ef4 100644 (file)
@@ -532,11 +532,25 @@ abstract class DatabaseMysqlBase extends Database {
                        return true; // already known to exist and won't show in SHOW TABLES anyway
                }
 
+               // Split database and table into proper variables as Database::tableName() returns
+               // shared tables prefixed with their database, which do not work in SHOW TABLES statements
+               list( $database, $schema, $prefix, $table ) = $this->qualifiedTableComponents( $table );
+
+               $table = $prefix . $table;
+
                // We can't use buildLike() here, because it specifies an escape character
                // other than the backslash, which is the only one supported by SHOW TABLES
                $encLike = $this->escapeLikeInternal( $table, '\\' );
 
-               return $this->query( "SHOW TABLES LIKE '$encLike'", $fname )->numRows() > 0;
+               // If the database has been specified (such as for shared tables), add a FROM $database clause
+               if ( $database !== '' ) {
+                       $database = $this->addIdentifierQuotes( $database );
+                       $query = "SHOW TABLES FROM $database LIKE '$encLike'";
+               } else {
+                       $query = "SHOW TABLES LIKE '$encLike'";
+               }
+
+               return $this->query( $query, $fname )->numRows() > 0;
        }
 
        /**