? sites/default/modules
? sites/default/settings.php
Index: includes/database/database.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/database/database.inc,v
retrieving revision 1.16
diff -u -p -r1.16 database.inc
--- includes/database/database.inc	25 Oct 2008 02:03:54 -0000	1.16
+++ includes/database/database.inc	29 Oct 2008 10:07:27 -0000
@@ -638,7 +638,7 @@ abstract class DatabaseConnection extend
    *   A database query result resource, or FALSE if the query was not executed
    *   correctly.
    */
-   abstract function queryTemporary($query, Array $args, $tablename, $options = array());
+   abstract function queryTemporary($query, Array $args, $tablename, Array $options);
 
   /**
    * Returns the type of database driver.
@@ -1404,14 +1404,11 @@ function db_query_range($query, $args, $
  *   named and unnamed placeholders, named placeholders are strongly preferred
  *   as they are more self-documenting.
  * @param $args
- *   An array of values to substitute into the query.  If the query uses named
- *   placeholders, this is an associative array in any order.  If the query uses
- *   unnamed placeholders (?), this is an indexed array and the order must match
- *   the order of placeholders in the query string.
- * @param $from
- *   The first record from the result set to return.
- * @param $limit
- *   The number of records to return from the result set.
+ *   An array of values to substitute into the query. The query should use
+ *   named placeholders, and this is an associative array in any order.
+ * @param $tablename
+ *   The name of the temporary table to select into. This name will not be
+ *   prefixed as there is no risk of collision.
  * @param $options
  *   An array of options to control how the query operates.
  */
Index: includes/database/mysql/database.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/database/mysql/database.inc,v
retrieving revision 1.4
diff -u -p -r1.4 database.inc
--- includes/database/mysql/database.inc	25 Oct 2008 02:03:55 -0000	1.4
+++ includes/database/mysql/database.inc	29 Oct 2008 10:07:27 -0000
@@ -46,10 +46,8 @@ class DatabaseConnection_mysql extends D
     return $this->query($query . ' LIMIT ' . $from . ', ' . $count, $args, $options);
   }
 
-  public function queryTemporary($query, Array $args, $tablename, $options = array()) {
-    $query = preg_replace('/^SELECT/i', 'CREATE TEMPORARY TABLE ' . $tablename . ' Engine=HEAP SELECT', $this->prefixTables($query));
-
-    return $this->query($query, $args, $options);
+  public function queryTemporary($query, Array $args, $tablename, Array $options) {
+    return $this->query(preg_replace('/^SELECT/i', 'CREATE TEMPORARY TABLE ' . $tablename . ' Engine=HEAP SELECT', $query), $args, $options);
   }
 
   public function driver() {
Index: includes/database/pgsql/database.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/database/pgsql/database.inc,v
retrieving revision 1.4
diff -u -p -r1.4 database.inc
--- includes/database/pgsql/database.inc	25 Oct 2008 02:12:35 -0000	1.4
+++ includes/database/pgsql/database.inc	29 Oct 2008 10:07:27 -0000
@@ -82,10 +82,8 @@ class DatabaseConnection_pgsql extends D
     return $this->query($query . ' LIMIT ' . $count . ' OFFSET ' . $from, $args, $options);
   }
 
-  public function queryTemporary($query, Array $args, $tablename, $options = array()) {
-    $query = preg_replace('/^SELECT/i', 'CREATE TEMPORARY TABLE ' . $tablename . ' Engine=HEAP SELECT', $this->prefixTables($query));
-
-    return $this->query($query, $args, $options);
+  public function queryTemporary($query, Array $args, $tablename, Array $options) {
+    return $this->query(preg_replace('/^SELECT/i', 'CREATE TEMPORARY TABLE ' . $tablename . ' AS SELECT', $query), $args, $options);
   }
 
   public function driver() {
Index: modules/simpletest/tests/database_test.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/simpletest/tests/database_test.module,v
retrieving revision 1.2
diff -u -p -r1.2 database_test.module
--- modules/simpletest/tests/database_test.module	15 Sep 2008 20:48:09 -0000	1.2
+++ modules/simpletest/tests/database_test.module	29 Oct 2008 10:07:27 -0000
@@ -35,3 +35,27 @@ function database_test_query_alter(Selec
     $expressions['double_age']['expression'] = 'age*3';
   }
 }
+
+/**
+ * Implementation of hook_menu().
+ */
+function database_test_menu() {
+  $items['admin/build/testing/database_test/db_query_temporary'] = array(
+    'access callback' => TRUE,
+    'page callback' => 'database_test_db_query_temporary',
+  );
+  return $items;
+}
+
+/**
+ * Run a db_query_temporary and print the number of rows in the resulting table.
+ *
+ * We need to test if the table created is temporary, so we run it here, as a
+ * separate menu callback request; After this request is done, the tempory
+ * table should automatically dropped.
+ */
+function database_test_db_query_temporary() {
+  db_query_temporary('SELECT status FROM {system} WHERE type = :type', array(':type' => 'module'), 'temporary');
+  print db_query('SELECT COUNT(*) FROM temporary')->fetchField();
+  exit;
+}
Index: modules/simpletest/tests/database_test.test
===================================================================
RCS file: /cvs/drupal/drupal/modules/simpletest/tests/database_test.test,v
retrieving revision 1.13
diff -u -p -r1.13 database_test.test
--- modules/simpletest/tests/database_test.test	25 Oct 2008 04:26:30 -0000	1.13
+++ modules/simpletest/tests/database_test.test	29 Oct 2008 10:07:27 -0000
@@ -1884,3 +1884,26 @@ class DatabaseLoggingTestCase extends Da
     }
   }
 }
+
+/**
+ * Temporary query tests.
+ */
+class DatabaseTemporaryQueryTestCase extends DatabaseTestCase {
+
+  function getInfo() {
+    return array(
+      'name' => t('Temporary query test'),
+      'description' => t('Test the Temporary query builder.'),
+      'group' => t('Database'),
+    );
+  }
+
+  /**
+   * Confirm that temporary tables work and are limited to one request.
+   */
+  function testTemporaryQuery() {
+    $this->drupalGet('admin/build/testing/database_test/db_query_temporary');
+    $this->assertEqual(db_query('SELECT COUNT(*) FROM {system} WHERE type = :type', array(':type' => 'module'))->fetchField(), $this->_content, t('The temporary table exists and contains the correct amount of rows.'));
+    $this->assertFalse(db_table_exists('temporary'), t('The temporary table is, indeed, temporary.'));
+  }
+}
