diff options
-rw-r--r-- | includes/database/database.inc | 9 | ||||
-rw-r--r-- | includes/database/mysql/database.inc | 6 | ||||
-rw-r--r-- | includes/database/pgsql/database.inc | 6 | ||||
-rw-r--r-- | modules/simpletest/tests/database_test.module | 24 | ||||
-rw-r--r-- | modules/simpletest/tests/database_test.test | 33 |
5 files changed, 65 insertions, 13 deletions
diff --git a/includes/database/database.inc b/includes/database/database.inc index 2a4fab9c3..f7edbd6f0 100644 --- a/includes/database/database.inc +++ b/includes/database/database.inc @@ -640,7 +640,7 @@ abstract class DatabaseConnection extends PDO { * 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 = array()); /** * Returns the type of database driver. @@ -1410,10 +1410,9 @@ function db_query_range($query, $args, $from = 0, $count = 0, $options = array() * 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. + * @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. */ diff --git a/includes/database/mysql/database.inc b/includes/database/mysql/database.inc index 7cf4f204c..d68951890 100644 --- a/includes/database/mysql/database.inc +++ b/includes/database/mysql/database.inc @@ -46,10 +46,8 @@ class DatabaseConnection_mysql extends DatabaseConnection { 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 = array()) { + return $this->query(preg_replace('/^SELECT/i', 'CREATE TEMPORARY TABLE ' . $tablename . ' Engine=MEMORY SELECT', $query), $args, $options); } public function driver() { diff --git a/includes/database/pgsql/database.inc b/includes/database/pgsql/database.inc index 793e97c74..c66956e3e 100644 --- a/includes/database/pgsql/database.inc +++ b/includes/database/pgsql/database.inc @@ -82,10 +82,8 @@ class DatabaseConnection_pgsql extends DatabaseConnection { 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 = array()) { + return $this->query(preg_replace('/^SELECT/i', 'CREATE TEMPORARY TABLE ' . $tablename . ' AS SELECT', $query), $args, $options); } public function driver() { diff --git a/modules/simpletest/tests/database_test.module b/modules/simpletest/tests/database_test.module index 48239fcbf..067e684ca 100644 --- a/modules/simpletest/tests/database_test.module +++ b/modules/simpletest/tests/database_test.module @@ -35,3 +35,27 @@ function database_test_query_alter(SelectQuery $query) { $expressions['double_age']['expression'] = 'age*3'; } } + +/** + * Implementation of hook_menu(). + */ +function database_test_menu() { + $items['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 that the table created is temporary, so we run it here, in a + * separate menu callback request; After this request is done, the temporary + * table should automatically dropped. + */ +function database_test_db_query_temporary() { + db_query_temporary('SELECT status FROM {system}', array(), 'temporary'); + print db_query('SELECT COUNT(*) FROM temporary')->fetchField(); + exit; +} diff --git a/modules/simpletest/tests/database_test.test b/modules/simpletest/tests/database_test.test index fa17db442..752b772f8 100644 --- a/modules/simpletest/tests/database_test.test +++ b/modules/simpletest/tests/database_test.test @@ -1884,3 +1884,36 @@ class DatabaseLoggingTestCase extends DatabaseTestCase { } } } + +/** + * Temporary query tests. + */ +class DatabaseTemporaryQueryTestCase extends DrupalWebTestCase { + + /** + * Define metadata for this test subclass. + */ + function getInfo() { + return array( + 'name' => t('Temporary query test'), + 'description' => t('Test the temporary query functionality.'), + 'group' => t('Database'), + ); + } + + /** + * Implementation of setUp(). + */ + function setUp() { + parent::setUp('database_test'); + } + + /** + * Confirm that temporary tables work and are limited to one request. + */ + function testTemporaryQuery() { + $this->drupalGet('database_test_db_query_temporary'); + $this->assertEqual(db_query('SELECT COUNT(*) FROM {system}')->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.')); + } +} |