From 35c7673caeff18080433191e461ccb7874baadc2 Mon Sep 17 00:00:00 2001
From: Dries Buytaert <dries@buytaert.net>
Date: Fri, 31 Oct 2008 11:13:31 +0000
Subject: [PATCH] - Patch #327480 by chx: remove DB specific code from
 simpletest.

---
 includes/database/database.inc       | 12 ++++++++++
 includes/database/schema.inc         | 18 ++++++++++++++
 modules/simpletest/simpletest.module | 36 +++-------------------------
 modules/system/system.test           |  6 ++---
 4 files changed, 36 insertions(+), 36 deletions(-)

diff --git a/includes/database/database.inc b/includes/database/database.inc
index b44846aa2cef..91c5ea4cb59a 100644
--- a/includes/database/database.inc
+++ b/includes/database/database.inc
@@ -1674,6 +1674,18 @@ function db_column_exists($table, $column) {
   return Database::getActiveConnection()->schema()->columnExists($table, $column);
 }
 
+ /**
+  * Find all tables that are like the specified base table name.
+  *
+  * @param table_expression
+  *   An SQL expression, for example simpletest% . BEWARE: this is not
+  *   prefixed, the caller should take care of that.
+  * @return
+  *   Array, both the keys and the values are the matching tables.
+  */
+function db_find_tables($table_expression) {
+  return Database::getActiveConnection()->schema()->findTables($table_expression);
+}
 
 /**
  * Given a Schema API field type, return the correct %-placeholder.
diff --git a/includes/database/schema.inc b/includes/database/schema.inc
index 03114d2cd9c9..391db547c422 100644
--- a/includes/database/schema.inc
+++ b/includes/database/schema.inc
@@ -405,6 +405,24 @@ public function fieldNames($fields) {
     return $ret;
   }
 
+  /**
+   * Find all tables that are like the specified base table name.
+   *
+   * @param table_expression
+   *   An SQL expression, for example "simpletest%" (without the quotes).
+   *   BEWARE: this is not prefixed, the caller should take care of that.
+   * @return
+   *   Array, both the keys and the values are the matching tables.
+   */
+  public function findTables($table_expression) {
+    global $db_prefix;
+    $info = Database::getConnectionInfo();
+    $result = db_query("SELECT table_name FROM information_schema.tables WHERE table_schema = :database AND table_name LIKE :table_name", array(
+      ':database' => $info['default']['database'],
+      ':table_name' => $table_expression,
+    ));
+    return $result->fetchAllKeyed(0, 0);
+  }
 }
 
 /**
diff --git a/modules/simpletest/simpletest.module b/modules/simpletest/simpletest.module
index 3f514c664f0c..bc51a68996cb 100644
--- a/modules/simpletest/simpletest.module
+++ b/modules/simpletest/simpletest.module
@@ -505,10 +505,10 @@ function simpletest_clean_environment() {
  * Removed prefixed talbes from the database that are left over from crashed tests.
  */
 function simpletest_clean_database() {
-  $tables = simpletest_get_like_tables();
-
+  $tables = db_find_tables(Database::getActiveConnection()->prefixTables('simpletest') . '%');
+  $schema = drupal_get_schema_unprocessed('simpletest');
   $ret = array();
-  foreach ($tables as $table) {
+  foreach (array_diff_key($tables, $schema) as $table) {
     db_drop_table($ret, $table);
   }
 
@@ -520,36 +520,6 @@ function simpletest_clean_database() {
   }
 }
 
-/**
- * Find all tables that are like the specified base table name.
- *
- * @param string $base_table Base table name.
- * @param boolean $count Return the table count instead of list of tables.
- * @return mixed Array of matching tables or count of tables.
- */
-function simpletest_get_like_tables($base_table = 'simpletest', $count = FALSE) {
-  global $db_prefix, $database;
-  $connection_info = Database::getConnectionInfo();
-  $database_name = $connection_info['default']['database'];
-  $select = $count ? 'COUNT(table_name)' : 'table_name';
-  $result = db_query("SELECT $select FROM information_schema.tables WHERE table_schema = :database AND table_name LIKE :table_name", array(
-    ':database' => $database_name,
-    ':table_name' => $db_prefix . $base_table . '%',
-  ));
-  $schema = drupal_get_schema_unprocessed('simpletest');
-
-  if ($count) {
-    return db_result($result);
-  }
-  $tables = array();
-  while ($table = db_result($result)) {
-    if (!isset($schema[$table])) {
-      $tables[] = $table;
-    }
-  }
-  return $tables;
-}
-
 /**
  * Find all left over temporary directories and remove them.
  */
diff --git a/modules/system/system.test b/modules/system/system.test
index 2aa7c9a7ece4..d3814aa8aff8 100644
--- a/modules/system/system.test
+++ b/modules/system/system.test
@@ -118,12 +118,12 @@ class EnableDisableCoreTestCase extends DrupalWebTestCase {
    * @return boolean Tables with specified base table.
    */
   function assertTableCount($base_table, $count) {
-    $match_count = simpletest_get_like_tables($base_table, TRUE);
+    $tables = db_find_tables(Database::getActiveConnection()->prefixTables($base_table) . '%');
 
     if ($count) {
-      return $this->assertTrue($match_count, t('Tables matching "@base_table" found.', array('@base_table' => $base_table)));
+      return $this->assertTrue($tables, t('Tables matching "@base_table" found.', array('@base_table' => $base_table)));
     }
-    return $this->assertFalse($match_count, t('Tables matching "@base_table" not found.', array('@base_table' => $base_table)));
+    return $this->assertFalse($tables, t('Tables matching "@base_table" not found.', array('@base_table' => $base_table)));
   }
 
   /**
-- 
GitLab