Commit 3704b919 by Qiang Xue

Fixes #4697: MSSQL query builder does not work for newer MSSQL versions when…

Fixes #4697: MSSQL query builder does not work for newer MSSQL versions when LIMIT is used without ORDER BY
parent 9b1ab7cd
......@@ -314,6 +314,7 @@ Yii Framework 2 Change Log
- Bug #3691: Fixed the issue that `CookieCollection::has` always returns false for cookies from browser (sonicgd)
- Bug #4212: MSSQL query builder should not generate the `ORDER BY` clause when it is not needed (qiangxue)
- Bug #4232: `TableSchema::sequenceName` for PostgreSQL should remove the enclosing quotes (katzz0, qiangxue)
- Bug #4697: MSSQL query builder does not work for newer MSSQL versions when LIMIT is used without ORDER BY (qiangxue)
- Bug: Fixed `Call to a member function registerAssetFiles() on a non-object` in case of wrong `sourcePath` for an asset bundle (samdark)
- Bug: Fixed incorrect event name for `yii\jui\Spinner` (samdark)
- Bug: Json::encode() did not handle objects that implement JsonSerializable interface correctly (cebe)
......
......@@ -135,6 +135,12 @@ class QueryBuilder extends \yii\db\QueryBuilder
$params = empty($params) ? $query->params : array_merge($params, $query->params);
$orderBy = $this->buildOrderBy($query->orderBy);
if ($orderBy === '' && ($this->hasOffset($query->offset) || $this->hasLimit($query->limit)) && !$this->isOldMssql()) {
// ORDER BY clause is required when FETCH and OFFSET are in the SQL
$orderBy = 'ORDER BY (SELECT NULL)';
}
$clauses = [
$this->buildSelect($query->select, $params, $query->distinct, $query->selectOption),
$this->buildFrom($query->from, $params),
......@@ -142,7 +148,7 @@ class QueryBuilder extends \yii\db\QueryBuilder
$this->buildWhere($query->where, $params),
$this->buildGroupBy($query->groupBy),
$this->buildHaving($query->having, $params),
$this->buildOrderBy($query->orderBy),
$orderBy,
$this->isOldMssql() ? '' : $this->buildLimit($query->limit, $query->offset),
];
......@@ -181,7 +187,7 @@ class QueryBuilder extends \yii\db\QueryBuilder
* @param string $sql SQL query
* @param integer $limit
* @param integer $offset
* @param \yii\db\ActiveQuery $query the [[Query]] object from which the SQL statement generated
* @param \yii\db\Query $query the [[Query]] object from which the SQL statement generated
* @return string resulting SQL query
*/
private function rewriteLimitOffsetSql($sql, $limit, $offset, $query)
......
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