Commit c442bf63 by Qiang Xue

Merge pull request #2124 from iworker/master

Update QueryBuilder.php and Query.php
parents 0eb9ff1e 71cc3050
...@@ -82,6 +82,7 @@ Yii Framework 2 Change Log ...@@ -82,6 +82,7 @@ Yii Framework 2 Change Log
- Enh #2051: Do not save null data into database when using RBAC (qiangxue) - Enh #2051: Do not save null data into database when using RBAC (qiangxue)
- Enh #2101: Gii is now using model labels when generating search (thiagotalma) - Enh #2101: Gii is now using model labels when generating search (thiagotalma)
- Enh #2103: Renamed AccessDeniedHttpException to ForbiddenHttpException, added new commonly used HTTP exception classes (danschmidt5189) - Enh #2103: Renamed AccessDeniedHttpException to ForbiddenHttpException, added new commonly used HTTP exception classes (danschmidt5189)
- Enh #2124: Added support for UNION ALL queries (Ivan Pomortsev, iworker)
- Enh: Added `favicon.ico` and `robots.txt` to default application templates (samdark) - Enh: Added `favicon.ico` and `robots.txt` to default application templates (samdark)
- Enh: Added `Widget::autoIdPrefix` to support prefixing automatically generated widget IDs (qiangxue) - Enh: Added `Widget::autoIdPrefix` to support prefixing automatically generated widget IDs (qiangxue)
- Enh: Support for file aliases in console command 'message' (omnilight) - Enh: Support for file aliases in console command 'message' (omnilight)
......
...@@ -635,11 +635,12 @@ class Query extends Component implements QueryInterface ...@@ -635,11 +635,12 @@ class Query extends Component implements QueryInterface
/** /**
* Appends a SQL statement using UNION operator. * Appends a SQL statement using UNION operator.
* @param string|Query $sql the SQL statement to be appended using UNION * @param string|Query $sql the SQL statement to be appended using UNION
* @param bool $all TRUE if using UNION ALL and FALSE if using UNION
* @return static the query object itself * @return static the query object itself
*/ */
public function union($sql) public function union($sql, $all = false)
{ {
$this->union[] = $sql; $this->union[] = [ 'query' => $sql, 'all' => $all ];
return $this; return $this;
} }
......
...@@ -739,16 +739,23 @@ class QueryBuilder extends \yii\base\Object ...@@ -739,16 +739,23 @@ class QueryBuilder extends \yii\base\Object
if (empty($unions)) { if (empty($unions)) {
return ''; return '';
} }
$result = '';
foreach ($unions as $i => $union) { foreach ($unions as $i => $union) {
if ($union instanceof Query) { $query = $union['query'];
if ($query instanceof Query) {
// save the original parameters so that we can restore them later to prevent from modifying the query object // save the original parameters so that we can restore them later to prevent from modifying the query object
$originalParams = $union->params; $originalParams = $query->params;
$union->addParams($params); $query->addParams($params);
list ($unions[$i], $params) = $this->build($union); list ($unions[$i]['query'], $params) = $this->build($query);
$union->params = $originalParams; $query->params = $originalParams;
} }
$result .= 'UNION ' . ($union['all'] ? 'ALL ' : '') . '( ' . $unions[$i]['query'] . ' ) ';
} }
return "UNION (\n" . implode("\n) UNION (\n", $unions) . "\n)";
return trim($result);
} }
/** /**
......
...@@ -215,4 +215,30 @@ class QueryBuilderTest extends DatabaseTestCase ...@@ -215,4 +215,30 @@ class QueryBuilderTest extends DatabaseTestCase
$this->assertEquals($expectedQueryParams, $queryParams); $this->assertEquals($expectedQueryParams, $queryParams);
} }
*/ */
/*
This test contains three select queries connected with UNION and UNION ALL constructions.
It could be useful to use "phpunit --group=db --filter testBuildUnion" command for run it.
public function testBuildUnion()
{
$expectedQuerySql = "SELECT `id` FROM `TotalExample` `t1` WHERE (w > 0) AND (x < 2) UNION ( SELECT `id` FROM `TotalTotalExample` `t2` WHERE w > 5 ) UNION ALL ( SELECT `id` FROM `TotalTotalExample` `t3` WHERE w = 3 )";
$query = new Query();
$secondQuery = new Query();
$secondQuery->select('id')
->from('TotalTotalExample t2')
->where('w > 5');
$thirdQuery = new Query();
$thirdQuery->select('id')
->from('TotalTotalExample t3')
->where('w = 3');
$query->select('id')
->from('TotalExample t1')
->where(['and', 'w > 0', 'x < 2'])
->union($secondQuery)
->union($thirdQuery, TRUE);
list($actualQuerySql, $queryParams) = $this->getQueryBuilder()->build($query);
$this->assertEquals($expectedQuerySql, $actualQuerySql);
}*/
} }
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