Skip to content

Commit

Permalink
Split union into 2 methods.
Browse files Browse the repository at this point in the history
Having separate methods for union and unionAll makes reading code
simpler as there are fewer boolean arguments floating around.
  • Loading branch information
markstory committed Dec 26, 2013
1 parent d5b00c9 commit 6c70be1
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Cake/Database/Dialect/SqliteDialectTrait.php
Expand Up @@ -125,7 +125,7 @@ protected function _insertQueryTranslator($query) {
}

$q = $newQuery->connection()->newQuery();
$newQuery->union($q->select($select), true);
$newQuery->unionAll($q->select($select));
}

if ($newQuery->type()) {
Expand Down
38 changes: 32 additions & 6 deletions Cake/Database/Query.php
Expand Up @@ -1163,7 +1163,7 @@ public function offset($num) {
* required by calling multiple times this method with different queries.
*
* By default, the UNION operator will remove duplicate rows, if you wish to include
* every row for all queries, set the second argument to true.
* every row for all queries, use unionAll()
*
* ## Examples
*
Expand All @@ -1176,25 +1176,51 @@ public function offset($num) {
*
* ``SELECT id, name FROM things d UNION SELECT id, title FROM articles a``
*
* @param string|Query $query full SQL query to be used in UNION operator
* @param boolean $overwrite whether to reset the list of queries to be operated or not
* @return Query
*/
public function union($query, $overwrite = false) {
if ($overwrite) {
$this->_parts['union'] = [];
}
$this->_parts['union'][] = [
'all' => false,
'query' => $query
];
$this->_dirty();
return $this;
}

/**
* Adds a complete query to be used in conjunction with the UNION ALL operator with
* this query. This is used to combine the result set of this query with the one
* that will be returned by the passed query. You can add as many queries as you
* required by calling multiple times this method with different queries.
*
* Unlike UNION, UNION ALL will not remove duplicate rows.
*
* {{{
* $union = (new Query($conn))->select(['id', 'title'])->from(['a' => 'articles']);
* $query->select(['id', 'name'])->from(['d' => 'things'])->union($union, true);
* $union = (new Query($conn))->select(['id', 'title'])->from(['a' => 'articles']);
* $query->select(['id', 'name'])->from(['d' => 'things'])->unionAll($union);
* }}}
*
* Will produce:
*
* ``SELECT id, name FROM things d UNION ALL SELECT id, title FROM articles a``
*
* @param string|Query $query full SQL query to be used in UNION operator
* @param boolean $all whether to use UNION ALL or not
* @param boolean $overwrite whether to reset the list of queries to be operated or not
* @return Query
*/
public function union($query, $all = false, $overwrite = false) {
public function unionAll($query, $overwrite = false) {
if ($overwrite) {
$this->_parts['union'] = [];
}
$this->_parts['union'][] = compact('all', 'query');
$this->_parts['union'][] = [
'all' => true,
'query' => $query
];
$this->_dirty();
return $this;
}
Expand Down
4 changes: 2 additions & 2 deletions Cake/Test/TestCase/Database/QueryTest.php
Expand Up @@ -1449,7 +1449,7 @@ public function testUnion() {
$union = (new Query($this->connection))
->select(['id', 'title'])
->from(['c' => 'articles']);
$query->select(['id', 'comment'], true)->union($union, false, true);
$query->select(['id', 'comment'], true)->union($union, true);
$result = $query->execute();
$this->assertCount(self::COMMENT_COUNT + self::ARTICLE_COUNT, $result);
$this->assertEquals($rows, $result->fetchAll());
Expand Down Expand Up @@ -1477,7 +1477,7 @@ public function testUnionAll() {
->where(['id ' => 1])
->order(['id' => 'desc']);

$query->select(['foo' => 'id', 'bar' => 'comment'])->union($union, true);
$query->select(['foo' => 'id', 'bar' => 'comment'])->unionAll($union);
$result = $query->execute();
$this->assertCount(1 + self::COMMENT_COUNT + self::ARTICLE_COUNT, $result);
$this->assertNotEquals($rows, $result->fetchAll());
Expand Down

0 comments on commit 6c70be1

Please sign in to comment.