Verified Commit cfb8a71b authored by Juraj Nemec's avatar Juraj Nemec
Browse files

Issue #2772107 by poker10, cgv, eojthebrave, q0rban, daffie, johnnydarkko,...

Issue #2772107 by poker10, cgv, eojthebrave, q0rban, daffie, johnnydarkko, drewish, David_Rothstein, cbergmann: UNION queries don't support ORDER BY clauses
parent 9fd49e36
Loading
Loading
Loading
Loading
+8 −8
Changes for includes/database/select.inc: 8 added lines, 8 removed lines.
Original line number Diff line number Diff line
@@ -1589,6 +1589,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 ";
@@ -1608,14 +1616,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';
    }
+58 −0
Changes for modules/simpletest/tests/database_test.test: 58 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -1655,6 +1655,64 @@ class DatabaseSelectTestCase extends DatabaseTestCase {
    $this->assertEqual($names[2], 'Ringo', 'Third query returned correct name.');
  }

  /**
   * 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 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 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.');
  }

  /**
   * Test that random ordering of queries works.
   *