From 8de2de0206929c9e72cd07dc987e3263970e0829 Mon Sep 17 00:00:00 2001 From: Dries Buytaert <dries@buytaert.net> Date: Wed, 9 Dec 2009 17:23:50 +0000 Subject: [PATCH] - Patch #653926 by Damien Tournoud: fixed range of sub-queries not being honored. --- includes/database/select.inc | 9 +++---- modules/simpletest/tests/database_test.test | 28 ++++++++++++++++++++- 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/includes/database/select.inc b/includes/database/select.inc index 84a01f294e00..3b9643b1e259 100644 --- a/includes/database/select.inc +++ b/includes/database/select.inc @@ -1085,10 +1085,6 @@ public function execute() { } $args = $this->getArguments(); - - if (!empty($this->range)) { - return $this->connection->queryRange((string)$this, $this->range['start'], $this->range['length'], $args, $this->queryOptions); - } return $this->connection->query((string)$this, $args, $this->queryOptions); } @@ -1358,7 +1354,10 @@ public function __toString() { $query .= implode(', ', $fields); } - // RANGE is database specific, so we can't do it here. + // RANGE + if (!empty($this->range)) { + $query .= "\nLIMIT " . $this->range['length'] . " OFFSET " . $this->range['start']; + } // UNION is a little odd, as the select queries to combine are passed into // this query, but syntactically they all end up on the same level. diff --git a/modules/simpletest/tests/database_test.test b/modules/simpletest/tests/database_test.test index bcba352d5e2a..cf9217ebbf60 100644 --- a/modules/simpletest/tests/database_test.test +++ b/modules/simpletest/tests/database_test.test @@ -1532,7 +1532,33 @@ class DatabaseSelectSubqueryTestCase extends DatabaseTestCase { } /** - * Test that we can use a subquery in a FROM clause. + * Test that we can use a subquery in a FROM clause with a limit. + */ + function testFromSubquerySelectWithLimit() { + // Create a subquery, which is just a normal query object. + $subquery = db_select('test_task', 'tt'); + $subquery->addField('tt', 'pid', 'pid'); + $subquery->addField('tt', 'task', 'task'); + $subquery->orderBy('priority', 'DESC'); + $subquery->range(0, 1); + + // Create another query that joins against the virtual table resulting + // from the subquery. + $select = db_select($subquery, 'tt2'); + $select->join('test', 't', 't.id=tt2.pid'); + $select->addField('t', 'name'); + + // The resulting query should be equivalent to: + // SELECT t.name + // FROM (SELECT tt.pid AS pid, tt.task AS task FROM test_task tt ORDER BY priority DESC LIMIT 1 OFFSET 0) tt + // INNER JOIN test t ON t.id=tt.pid + $people = $select->execute()->fetchCol(); + + $this->assertEqual(count($people), 1, t('Returned the correct number of rows.')); + } + + /** + * Test that we can use a subquery in a WHERE clause. */ function testConditionSubquerySelect() { // Create a subquery, which is just a normal query object. -- GitLab