Commit 1632855a authored by webchick's avatar webchick
Browse files

#325895 by chx and hswong3i: Fix queryTemporary and add test case.

parent 6159c302
......@@ -640,7 +640,7 @@ abstract public function queryRange($query, Array $args, $from, $count, Array $o
* 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.
*/
......
......@@ -46,10 +46,8 @@ public function queryRange($query, Array $args, $from, $count, Array $options) {
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() {
......
......@@ -82,10 +82,8 @@ public function queryRange($query, Array $args, $from, $count, Array $options) {
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() {
......
......@@ -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;
}
......@@ -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.'));
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment