Index: includes/database/schema.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/database/schema.inc,v
retrieving revision 1.4
diff -u -9 -p -r1.4 schema.inc
--- includes/database/schema.inc	31 Oct 2008 15:46:16 -0000	1.4
+++ includes/database/schema.inc	25 Nov 2008 21:09:09 -0000
@@ -121,24 +121,60 @@ abstract class DatabaseSchema {
   protected $connection;
 
   public function __construct($connection) {
     $this->connection = $connection;
   }
 
   /**
    * Check if a table exists.
    */
-  abstract public function tableExists($table);
+  public function tableExists($table) {
+    $split = explode('.', $this->connection->prefixTables('{' . $table . '}'), 2);
+    if (count($split) == 2) {
+      $args = array(
+        ':database' => $split[0],
+        ':table' => $split[1],
+      );
+    }
+    else {
+      $info = Database::getConnectionInfo();
+      $args = array(
+        ':database' => $info['default']['database'],
+        ':table' => $split[0],
+      );
+    }
+    $result = db_query_range('SELECT 1 FROM information_schema.tables WHERE table_schema = :database AND table_name = :table', $args, 0, 1);
+    return (bool)$result->fetchField();
+  }
 
   /**
    * Check if a column exists in the given table.
    */
-  abstract public function columnExists($table, $column);
+  public function columnExists($table, $column) {
+    $split = explode('.', $this->connection->prefixTables('{' . $table . '}'), 2);
+    if (count($split) == 2) {
+      $args = array(
+        ':database' => $split[0],
+        ':table' => $split[1],
+        ':column' => $column,
+      );
+    }
+    else {
+      $info = Database::getConnectionInfo();
+      $args = array(
+        ':database' => $info['default']['database'],
+        ':table' => $split[0],
+        ':column' => $column,
+      );
+    }
+    $result = db_query_range('SELECT 1 FROM information_schema.columns WHERE table_schema = :database AND table_name = :table AND column_name = :column', $args, 0, 1);
+    return (bool)$result->fetchField();
+  }
 
   /**
    * This maps a generic data type in combination with its data size
    * to the engine-specific data type.
    */
   abstract public function getFieldTypeMap();
 
   /**
    * Rename a table.
Index: includes/database/mysql/schema.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/database/mysql/schema.inc,v
retrieving revision 1.6
diff -u -9 -p -r1.6 schema.inc
--- includes/database/mysql/schema.inc	13 Nov 2008 20:52:13 -0000	1.6
+++ includes/database/mysql/schema.inc	25 Nov 2008 21:09:09 -0000
@@ -8,27 +8,18 @@
 
 
 /**
  * @ingroup schemaapi
  * @{
  */
 
 class DatabaseSchema_mysql extends DatabaseSchema {
 
-  public function tableExists($table) {
-    return (bool) $this->connection->query("SHOW TABLES LIKE '{" . $table . "}'", array(), array())->fetchField();
-  }
-
-  public function columnExists($table, $column) {
-    return (bool) $this->connection->query("SHOW COLUMNS FROM {" . $this->connection->escapeTable($table) . "} LIKE '" . $this->connection->escapeTable($column) . "'", array(), array())->fetchField();
-  }
-
-
   /**
    * Generate SQL to create a new table from a Drupal schema definition.
    *
    * @param $name
    *   The name of the table to create.
    * @param $table
    *   A Schema API table definition array.
    * @return
    *   An array of SQL statements to create the table.
Index: includes/database/pgsql/schema.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/database/pgsql/schema.inc,v
retrieving revision 1.2
diff -u -9 -p -r1.2 schema.inc
--- includes/database/pgsql/schema.inc	15 Sep 2008 20:48:07 -0000	1.2
+++ includes/database/pgsql/schema.inc	25 Nov 2008 21:09:09 -0000
@@ -7,26 +7,18 @@
  */
 
 /**
  * @ingroup schemaapi
  * @{
  */
 
 class DatabaseSchema_pgsql extends DatabaseSchema {
 
-  public function tableExists($table) {
-    return (bool) db_result(db_query("SELECT COUNT(*) FROM pg_class WHERE relname = '{" . db_escape_table($table) . "}'"));
-  }
-
-  public function columnExists($table, $column) {
-    return (bool) db_result(db_query("SELECT COUNT(pg_attribute.attname) FROM pg_class, pg_attribute WHERE pg_attribute.attrelid = pg_class.oid AND pg_class.relname = '{" . db_escape_table($table) . "}' AND attname = '" . db_escape_table($column) . "'"));
-  }
-
   /**
    * Generate SQL to create a new table from a Drupal schema definition.
    *
    * @param $name
    *   The name of the table to create.
    * @param $table
    *   A Schema API table definition array.
    * @return
    *   An array of SQL statements to create the table.
Index: modules/simpletest/tests/database_test.test
===================================================================
RCS file: /cvs/drupal/drupal/modules/simpletest/tests/database_test.test,v
retrieving revision 1.22
diff -u -9 -p -r1.22 database_test.test
--- modules/simpletest/tests/database_test.test	25 Nov 2008 13:14:28 -0000	1.22
+++ modules/simpletest/tests/database_test.test	25 Nov 2008 21:09:09 -0000
@@ -1804,28 +1804,50 @@ class DatabaseRegressionTestCase extends
 
     $from_database = db_query("SELECT name FROM {test} WHERE name = :name", array(':name' => $name))->fetchField();
     $this->assertIdentical($name, $from_database, t("The database handles UTF-8 characters cleanly."));
   }
 
   /**
    * Test the db_column_exists() function.
    */
   function testDBColumnExists() {
+    global $db_prefix;
     $this->assertTrue(db_column_exists('node', 'nid'), t('Returns true for existent column.'));
     $this->assertFalse(db_column_exists('node', 'nosuchcolumn'), t('Returns false for nonexistent column.'));
+    // Test with "database.table" prefix.
+    $db_prefix_original = $db_prefix;
+    $connection_info = Database::getConnectionInfo('default');
+    $db_prefix = $connection_info['default']['database'] . '.' . $db_prefix;
+    $this->assertEqual(Database::getActiveConnection()->prefixTables('{node}'), $db_prefix . 'node', t('Table name was prefixed with database name.'));
+    $this->assertTrue(db_column_exists('node', 'nid'), t('Returns true for existent column.'));
+    $this->assertFalse(db_column_exists('node', 'nosuchcolumn'), t('Returns false for nonexistent column.'));
+    $db_prefix = 'nosuchdatabase.' . $db_prefix;
+    $this->assertFalse(db_column_exists('node', 'nid'), t('Returns false for nonexistent database.'));
+    $db_prefix = $db_prefix_original;
   }
 
   /**
    * Test the db_table_exists() function.
    */
   function testDBTableExists() {
+    global $db_prefix;
     $this->assertTrue(db_table_exists('node'), t('Returns true for existent table.'));
     $this->assertFalse(db_table_exists('nosuchtable'), t('Returns false for nonexistent table.'));
+    // Test with "database.table" prefix.
+    $db_prefix_original = $db_prefix;
+    $connection_info = Database::getConnectionInfo('default');
+    $db_prefix = $connection_info['default']['database'] . '.' . $db_prefix;
+    $this->assertEqual(Database::getActiveConnection()->prefixTables('{node}'), $db_prefix . 'node', t('Table name was prefixed with database name.'));
+    $this->assertTrue(db_table_exists('node'), t('Returns true for existent table.'));
+    $this->assertFalse(db_table_exists('nosuchtable'), t('Returns false for nonexistent table.'));
+    $db_prefix = 'nosuchdatabase.' . $db_prefix;
+    $this->assertFalse(db_table_exists('node'), t('Returns false for nonexistent database.'));
+    $db_prefix = $db_prefix_original;
   }
 }
 
 /**
  * Query logging tests.
  */
 class DatabaseLoggingTestCase extends DatabaseTestCase {
 
   function getInfo() {
