Commit 6d3fd710 authored by alexpott's avatar alexpott

Issue #1145076 by daffie, David_Rothstein, johnnydarkko, cbergmann, drewish,...

Issue #1145076 by daffie, David_Rothstein, johnnydarkko, cbergmann, drewish, rpayanm, somersoft, jhedstrom: UNION queries don't support ORDER BY clauses
parent 7c0a466b
......@@ -860,6 +860,14 @@ public function __toString() {
$query .= "\nHAVING " . $this->having;
}
// 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.
if ($this->union) {
foreach ($this->union as $union) {
$query .= ' ' . $union['type'] . ' ' . (string) $union['query'];
}
}
// ORDER BY
if ($this->order) {
$query .= "\nORDER BY ";
......@@ -879,14 +887,6 @@ public function __toString() {
$query .= "\nLIMIT " . (int) $this->range['length'] . " OFFSET " . (int) $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.
if ($this->union) {
foreach ($this->union as $union) {
$query .= ' ' . $union['type'] . ' ' . (string) $union['query'];
}
}
if ($this->forUpdate) {
$query .= ' FOR UPDATE';
}
......
......@@ -322,6 +322,64 @@ function testUnionCount() {
$this->assertEqual(count($names), $count, "The count query's result matched the number of rows in the UNION query.");
}
/**
* Tests that we can UNION multiple Select queries together and set the ORDER.
*/
function testUnionOrder() {
// This gives George and Ringo.
$query_1 = db_select('test', 't')
->fields('t', array('name'))
->condition('age', array(27, 28), 'IN');
// This gives Paul.
$query_2 = db_select('test', 't')
->fields('t', array('name'))
->condition('age', 26);
$query_1->union($query_2);
$query_1->orderBy('name', 'DESC');
$names = $query_1->execute()->fetchCol();
// Ensure we get all 3 records.
$this->assertEqual(count($names), 3, 'UNION returned rows from both queries.');
// Ensure that the names are in the correct reverse alphabetical order,
// regardless of which query they came from.
$this->assertEqual($names[0], 'Ringo', 'First query returned correct name.');
$this->assertEqual($names[1], 'Paul', 'Second query returned correct name.');
$this->assertEqual($names[2], 'George', 'Third query returned correct name.');
}
/**
* Tests that we can UNION multiple Select queries together with and a LIMIT.
*/
function testUnionOrderLimit() {
// This gives George and Ringo.
$query_1 = db_select('test', 't')
->fields('t', array('name'))
->condition('age', array(27, 28), 'IN');
// This gives Paul.
$query_2 = db_select('test', 't')
->fields('t', array('name'))
->condition('age', 26);
$query_1->union($query_2);
$query_1->orderBy('name', 'DESC');
$query_1->range(0, 2);
$names = $query_1->execute()->fetchCol();
// Ensure we get all only 2 of the 3 records.
$this->assertEqual(count($names), 2, 'UNION with a limit returned rows from both queries.');
// Ensure that the names are in the correct reverse alphabetical order,
// regardless of which query they came from.
$this->assertEqual($names[0], 'Ringo', 'First query returned correct name.');
$this->assertEqual($names[1], 'Paul', 'Second query returned correct name.');
}
/**
* Tests that random ordering of queries works.
*
......
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