From 6dba4da3ed7521ac81b88ea5c7efece12f7a25d1 Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Thu, 29 Aug 2013 16:47:48 -0400 Subject: [PATCH] Fixes #828: refactored QueryBuilder::build() --- framework/yii/db/ActiveQuery.php | 8 ++++---- framework/yii/db/ActiveRelation.php | 4 ++-- framework/yii/db/Query.php | 4 ++-- framework/yii/db/QueryBuilder.php | 20 ++++++++++++-------- tests/unit/framework/db/QueryBuilderTest.php | 2 +- 5 files changed, 21 insertions(+), 17 deletions(-) diff --git a/framework/yii/db/ActiveQuery.php b/framework/yii/db/ActiveQuery.php index 4d08659..12997ee 100644 --- a/framework/yii/db/ActiveQuery.php +++ b/framework/yii/db/ActiveQuery.php @@ -156,6 +156,8 @@ class ActiveQuery extends Query if ($db === null) { $db = $modelClass::getDb(); } + + $params = $this->params; if ($this->sql === null) { if ($this->from === null) { $tableName = $modelClass::tableName(); @@ -164,11 +166,9 @@ class ActiveQuery extends Query } $this->from = array($tableName); } - /** @var $qb QueryBuilder */ - $qb = $db->getQueryBuilder(); - $this->sql = $qb->build($this); + list ($this->sql, $params) = $db->getQueryBuilder()->build($this); } - return $db->createCommand($this->sql, $this->params); + return $db->createCommand($this->sql, $params); } /** diff --git a/framework/yii/db/ActiveRelation.php b/framework/yii/db/ActiveRelation.php index f10f287..0eee25b 100644 --- a/framework/yii/db/ActiveRelation.php +++ b/framework/yii/db/ActiveRelation.php @@ -297,7 +297,7 @@ class ActiveRelation extends ActiveQuery /** @var $primaryModel ActiveRecord */ $primaryModel = reset($primaryModels); $db = $primaryModel->getDb(); - $sql = $db->getQueryBuilder()->build($this); - return $db->createCommand($sql, $this->params)->queryAll(); + list ($sql, $params) = $db->getQueryBuilder()->build($this); + return $db->createCommand($sql, $params)->queryAll(); } } diff --git a/framework/yii/db/Query.php b/framework/yii/db/Query.php index 19ea028..d1e7864 100644 --- a/framework/yii/db/Query.php +++ b/framework/yii/db/Query.php @@ -149,8 +149,8 @@ class Query extends Component if ($db === null) { $db = Yii::$app->getDb(); } - $sql = $db->getQueryBuilder()->build($this); - return $db->createCommand($sql, $this->params); + list ($sql, $params) = $db->getQueryBuilder()->build($this); + return $db->createCommand($sql, $params); } /** diff --git a/framework/yii/db/QueryBuilder.php b/framework/yii/db/QueryBuilder.php index b55be3c..9a9329d 100644 --- a/framework/yii/db/QueryBuilder.php +++ b/framework/yii/db/QueryBuilder.php @@ -55,22 +55,24 @@ class QueryBuilder extends \yii\base\Object /** * Generates a SELECT SQL statement from a [[Query]] object. * @param Query $query the [[Query]] object from which the SQL statement will be generated - * @return string the generated SQL statement + * @return array the generated SQL statement (the first array element) and the corresponding + * parameters to be bound to the SQL statement (the second array element). */ public function build($query) { + $params = $query->params; $clauses = array( $this->buildSelect($query->select, $query->distinct, $query->selectOption), $this->buildFrom($query->from), - $this->buildJoin($query->join, $query->params), - $this->buildWhere($query->where, $query->params), + $this->buildJoin($query->join, $params), + $this->buildWhere($query->where, $params), $this->buildGroupBy($query->groupBy), - $this->buildHaving($query->having, $query->params), - $this->buildUnion($query->union, $query->params), + $this->buildHaving($query->having, $params), + $this->buildUnion($query->union, $params), $this->buildOrderBy($query->orderBy), $this->buildLimit($query->limit, $query->offset), ); - return implode($this->separator, array_filter($clauses)); + return array(implode($this->separator, array_filter($clauses)), $params); } /** @@ -718,9 +720,11 @@ class QueryBuilder extends \yii\base\Object } foreach ($unions as $i => $union) { if ($union instanceof Query) { + // save the original parameters so that we can restore them later to prevent from modifying the query object + $originalParams = $union->params; $union->addParams($params); - $unions[$i] = $this->build($union); - $params = $union->params; + list ($unions[$i], $params) = $this->build($union); + $union->params = $originalParams; } } return "UNION (\n" . implode("\n) UNION (\n", $unions) . "\n)"; diff --git a/tests/unit/framework/db/QueryBuilderTest.php b/tests/unit/framework/db/QueryBuilderTest.php index 146cfdb..e08ac87 100644 --- a/tests/unit/framework/db/QueryBuilderTest.php +++ b/tests/unit/framework/db/QueryBuilderTest.php @@ -112,7 +112,7 @@ class QueryBuilderTest extends DatabaseTestCase } } - public function testAddDropPrimayKey() + public function testAddDropPrimaryKey() { $tableName = 'tbl_constraints'; $pkeyName = $tableName . "_pkey";