summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Pott2014-01-25 18:55:03 (GMT)
committerAlex Pott2014-01-25 18:55:03 (GMT)
commit03a7c55fdbf793ce2db330ea87e1226e725327c1 (patch)
tree10c5712f097d657225d2726707fef5e417801077
parent9da7e4cdc7c2c289b9af079d22fcfd862121a903 (diff)
Issue #1868972 by jweowu: db_query_temporary() fails to create a table when the SQL has leading whitespace.
-rw-r--r--core/lib/Drupal/Core/Database/Driver/mysql/Connection.php2
-rw-r--r--core/lib/Drupal/Core/Database/Driver/pgsql/Connection.php2
-rw-r--r--core/lib/Drupal/Core/Database/Driver/sqlite/Connection.php2
-rw-r--r--core/modules/system/lib/Drupal/system/Tests/Database/TemporaryQueryTest.php9
4 files changed, 12 insertions, 3 deletions
diff --git a/core/lib/Drupal/Core/Database/Driver/mysql/Connection.php b/core/lib/Drupal/Core/Database/Driver/mysql/Connection.php
index 620b829..66cd34f 100644
--- a/core/lib/Drupal/Core/Database/Driver/mysql/Connection.php
+++ b/core/lib/Drupal/Core/Database/Driver/mysql/Connection.php
@@ -140,7 +140,7 @@ class Connection extends DatabaseConnection {
public function queryTemporary($query, array $args = array(), array $options = array()) {
$tablename = $this->generateTemporaryTableName();
- $this->query(preg_replace('/^SELECT/i', 'CREATE TEMPORARY TABLE {' . $tablename . '} Engine=MEMORY SELECT', $query), $args, $options);
+ $this->query('CREATE TEMPORARY TABLE {' . $tablename . '} Engine=MEMORY ' . $query, $args, $options);
return $tablename;
}
diff --git a/core/lib/Drupal/Core/Database/Driver/pgsql/Connection.php b/core/lib/Drupal/Core/Database/Driver/pgsql/Connection.php
index 8c2a34a..cb8e501 100644
--- a/core/lib/Drupal/Core/Database/Driver/pgsql/Connection.php
+++ b/core/lib/Drupal/Core/Database/Driver/pgsql/Connection.php
@@ -179,7 +179,7 @@ class Connection extends DatabaseConnection {
public function queryTemporary($query, array $args = array(), array $options = array()) {
$tablename = $this->generateTemporaryTableName();
- $this->query(preg_replace('/^SELECT/i', 'CREATE TEMPORARY TABLE {' . $tablename . '} AS SELECT', $query), $args, $options);
+ $this->query('CREATE TEMPORARY TABLE {' . $tablename . '} AS ' . $query, $args, $options);
return $tablename;
}
diff --git a/core/lib/Drupal/Core/Database/Driver/sqlite/Connection.php b/core/lib/Drupal/Core/Database/Driver/sqlite/Connection.php
index 137e23c..2031e4b 100644
--- a/core/lib/Drupal/Core/Database/Driver/sqlite/Connection.php
+++ b/core/lib/Drupal/Core/Database/Driver/sqlite/Connection.php
@@ -266,7 +266,7 @@ class Connection extends DatabaseConnection {
$prefixes[$tablename] = '';
$this->setPrefix($prefixes);
- $this->query(preg_replace('/^SELECT/i', 'CREATE TEMPORARY TABLE ' . $tablename . ' AS SELECT', $query), $args, $options);
+ $this->query('CREATE TEMPORARY TABLE ' . $tablename . ' AS ' . $query, $args, $options);
return $tablename;
}
diff --git a/core/modules/system/lib/Drupal/system/Tests/Database/TemporaryQueryTest.php b/core/modules/system/lib/Drupal/system/Tests/Database/TemporaryQueryTest.php
index ea61cd9..c57d587 100644
--- a/core/modules/system/lib/Drupal/system/Tests/Database/TemporaryQueryTest.php
+++ b/core/modules/system/lib/Drupal/system/Tests/Database/TemporaryQueryTest.php
@@ -54,5 +54,14 @@ class TemporaryQueryTest extends DatabaseWebTestBase {
$this->assertEqual($this->countTableRows($table_name_test), $this->countTableRows('test'), 'A temporary table was created successfully in this request.');
$this->assertEqual($this->countTableRows($table_name_task), $this->countTableRows('test_task'), 'A second temporary table was created successfully in this request.');
+
+ // Check that leading whitespace and comments do not cause problems
+ // in the modified query.
+ $sql = "
+ -- Let's select some rows into a temporary table
+ SELECT name FROM {test}
+ ";
+ $table_name_test = db_query_temporary($sql, array());
+ $this->assertEqual($this->countTableRows($table_name_test), $this->countTableRows('test'), 'Leading white space and comments do not interfere with temporary table creation.');
}
}