From 212c5ee3ef2eb244f7ca346ea1825427200b3394 Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Wed, 3 Sep 2014 14:42:09 -0400 Subject: [PATCH] Fixes #4254: `SqlDataProvider` does not work with Oracle and SQL Server --- docs/guide/db-dao.md | 3 +- extensions/sphinx/QueryBuilder.php | 22 ++++ framework/CHANGELOG.md | 1 + framework/data/SqlDataProvider.php | 33 ++++-- framework/db/QueryBuilder.php | 24 +++- framework/db/mssql/QueryBuilder.php | 174 +++++++++++----------------- framework/db/oci/QueryBuilder.php | 52 ++------- 7 files changed, 145 insertions(+), 164 deletions(-) diff --git a/docs/guide/db-dao.md b/docs/guide/db-dao.md index 96caa045b40..392efe09fc8 100644 --- a/docs/guide/db-dao.md +++ b/docs/guide/db-dao.md @@ -12,8 +12,7 @@ uniform API and solves some inconsistencies between different DBMS. By default Y - [PostgreSQL](http://www.postgresql.org/) - [CUBRID](http://www.cubrid.org/): version 9.1.0 or higher. - [Oracle](http://www.oracle.com/us/products/database/overview/index.html) -- [MSSQL](https://www.microsoft.com/en-us/sqlserver/default.aspx): version 2012 or above is required if you - want to use LIMIT/OFFSET. +- [MSSQL](https://www.microsoft.com/en-us/sqlserver/default.aspx): version 2005 or higher. Configuration diff --git a/extensions/sphinx/QueryBuilder.php b/extensions/sphinx/QueryBuilder.php index 889ec2646ca..395fb479ca3 100644 --- a/extensions/sphinx/QueryBuilder.php +++ b/extensions/sphinx/QueryBuilder.php @@ -524,6 +524,27 @@ public function buildGroupBy($columns) return empty($columns) ? '' : 'GROUP BY ' . $this->buildColumns($columns); } + /** + * Builds the ORDER BY and LIMIT/OFFSET clauses and appends them to the given SQL. + * @param string $sql the existing SQL (without ORDER BY/LIMIT/OFFSET) + * @param array $orderBy the order by columns. See [[Query::orderBy]] for more details on how to specify this parameter. + * @param integer $limit the limit number. See [[Query::limit]] for more details. + * @param integer $offset the offset number. See [[Query::offset]] for more details. + * @return string the SQL completed with ORDER BY/LIMIT/OFFSET (if any) + */ + public function buildOrderByAndLimit($sql, $orderBy, $limit, $offset) + { + $orderBy = $this->buildOrderBy($orderBy); + if ($orderBy !== '') { + $sql .= $this->separator . $orderBy; + } + $limit = $this->buildLimit($limit, $offset); + if ($limit !== '') { + $sql .= $this->separator . $limit; + } + return $sql; + } + /** * @param array $columns * @return string the ORDER BY clause built from [[query]]. @@ -999,6 +1020,7 @@ protected function composeColumnValue($indexes, $columnName, $value, &$params) * @param array $operands contains two column names. * @param array $params the binding parameters to be populated * @return string the generated SQL expression + * @throws InvalidParamException if count($operands) is not 2 */ public function buildSimpleCondition($operator, $operands, &$params) { diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 223009ec0ae..18f930e1e1c 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -68,6 +68,7 @@ Yii Framework 2 Change Log - Bug #4127: `CaptchaValidator` clientside error message wasn't formed properly (samdark) - Bug #4162: Fixed bug where schema name was not used in ’SHOW CREATE TABLE’ query in `yii\db\mysql\Schema` (stevekr) - Bug #4241: `yii\widgets\Pjax` was incorrectly setting container id (mitalcoi) +- Bug #4254: `SqlDataProvider` does not work with Oracle and SQL Server (qiangxue, miramir) - Bug #4276: Added check for UPLOAD_ERR_NO_FILE in `yii\web\UploadedFile` and return null if no file was uploaded (OmgDef) - Bug #4342: mssql (dblib) driver does not support getting attributes (tof06) - Bug #4371: Active form client validation wasn't working in case of two models having same named fields (abrahamy) diff --git a/framework/data/SqlDataProvider.php b/framework/data/SqlDataProvider.php index 3a261c9bfb6..9ed142c2daf 100644 --- a/framework/data/SqlDataProvider.php +++ b/framework/data/SqlDataProvider.php @@ -10,6 +10,7 @@ use Yii; use yii\base\InvalidConfigException; use yii\db\Connection; +use yii\db\Expression; use yii\di\Instance; /** @@ -102,25 +103,33 @@ public function init() */ protected function prepareModels() { + $sort = $this->getSort(); + $pagination = $this->getPagination(); + if ($pagination === false && $sort === false) { + return $this->db->createCommand($this->sql, $this->params)->queryAll(); + } + $sql = $this->sql; - $qb = $this->db->getQueryBuilder(); - if (($sort = $this->getSort()) !== false) { - $orderBy = $qb->buildOrderBy($sort->getOrders()); - if (!empty($orderBy)) { - $orderBy = substr($orderBy, 9); - if (preg_match('/\s+order\s+by\s+[\w\s,\.]+$/i', $sql)) { - $sql .= ', ' . $orderBy; - } else { - $sql .= ' ORDER BY ' . $orderBy; - } + $orders = []; + $limit = $offset = null; + + if ($sort !== false) { + $orders = $sort->getOrders(); + $pattern = '/\s+order\s+by\s+([\w\s,\.]+)$/i'; + if (preg_match($pattern, $sql, $matches)) { + array_unshift($orders, new Expression($matches[1])); + $sql = preg_replace($pattern, '', $sql); } } - if (($pagination = $this->getPagination()) !== false) { + if ($pagination !== false) { $pagination->totalCount = $this->getTotalCount(); - $sql .= ' ' . $qb->buildLimit($pagination->getLimit(), $pagination->getOffset()); + $limit = $pagination->getLimit(); + $offset = $pagination->getOffset(); } + $sql = $this->db->getQueryBuilder()->buildOrderByAndLimit($sql, $orders, $limit, $offset); + return $this->db->createCommand($sql, $this->params)->queryAll(); } diff --git a/framework/db/QueryBuilder.php b/framework/db/QueryBuilder.php index e31685eeb28..ea8ca3373f7 100644 --- a/framework/db/QueryBuilder.php +++ b/framework/db/QueryBuilder.php @@ -96,11 +96,10 @@ public function build($query, $params = []) $this->buildWhere($query->where, $params), $this->buildGroupBy($query->groupBy), $this->buildHaving($query->having, $params), - $this->buildOrderBy($query->orderBy), - $this->buildLimit($query->limit, $query->offset), ]; $sql = implode($this->separator, array_filter($clauses)); + $sql = $this->buildOrderByAndLimit($sql, $query->orderBy, $query->limit, $query->offset); $union = $this->buildUnion($query->union, $params); if ($union !== '') { @@ -754,6 +753,27 @@ public function buildHaving($condition, &$params) return $having === '' ? '' : 'HAVING ' . $having; } + /** + * Builds the ORDER BY and LIMIT/OFFSET clauses and appends them to the given SQL. + * @param string $sql the existing SQL (without ORDER BY/LIMIT/OFFSET) + * @param array $orderBy the order by columns. See [[Query::orderBy]] for more details on how to specify this parameter. + * @param integer $limit the limit number. See [[Query::limit]] for more details. + * @param integer $offset the offset number. See [[Query::offset]] for more details. + * @return string the SQL completed with ORDER BY/LIMIT/OFFSET (if any) + */ + public function buildOrderByAndLimit($sql, $orderBy, $limit, $offset) + { + $orderBy = $this->buildOrderBy($orderBy); + if ($orderBy !== '') { + $sql .= $this->separator . $orderBy; + } + $limit = $this->buildLimit($limit, $offset); + if ($limit !== '') { + $sql .= $this->separator . $limit; + } + return $sql; + } + /** * @param array $columns * @return string the ORDER BY clause built from [[Query::$orderBy]]. diff --git a/framework/db/mssql/QueryBuilder.php b/framework/db/mssql/QueryBuilder.php index 7e26dd45b5e..492742a2c7c 100644 --- a/framework/db/mssql/QueryBuilder.php +++ b/framework/db/mssql/QueryBuilder.php @@ -41,25 +41,77 @@ class QueryBuilder extends \yii\db\QueryBuilder /** - * @param integer $limit - * @param integer $offset - * @return string the LIMIT and OFFSET clauses built from [[\yii\db\Query::$limit]]. + * @inheritdoc + */ + public function buildOrderByAndLimit($sql, $orderBy, $limit, $offset) + { + if (!$this->hasOffset($offset) && !$this->hasLimit($limit)) { + $orderBy = $this->buildOrderBy($orderBy); + return $orderBy === '' ? $sql : $sql . $this->separator . $orderBy; + } + + if ($this->isOldMssql()) { + return $this->oldbuildOrderByAndLimit($sql, $orderBy, $limit, $offset); + } else { + return $this->newBuildOrderByAndLimit($sql, $orderBy, $limit, $offset); + } + } + + /** + * Builds the ORDER BY/LIMIT/OFFSET clauses for SQL SERVER 2012 or newer. + * @param string $sql the existing SQL (without ORDER BY/LIMIT/OFFSET) + * @param array $orderBy the order by columns. See [[Query::orderBy]] for more details on how to specify this parameter. + * @param integer $limit the limit number. See [[Query::limit]] for more details. + * @param integer $offset the offset number. See [[Query::offset]] for more details. + * @return string the SQL completed with ORDER BY/LIMIT/OFFSET (if any) + */ + protected function newBuildOrderByAndLimit($sql, $orderBy, $limit, $offset) + { + $orderBy = $this->buildOrderBy($orderBy); + if ($orderBy === '') { + // ORDER BY clause is required when FETCH and OFFSET are in the SQL + $orderBy = 'ORDER BY (SELECT NULL)'; + } + $sql .= $this->separator . $orderBy; + + // http://technet.microsoft.com/en-us/library/gg699618.aspx + $offset = $this->hasOffset($offset) ? $offset : '0'; + $sql .= $this->separator . "OFFSET $offset ROWS"; + if ($this->hasLimit($limit)) { + $sql .= $this->separator . "FETCH NEXT $limit ROWS ONLY"; + } + + return $sql; + } + + /** + * Builds the ORDER BY/LIMIT/OFFSET clauses for SQL SERVER 2005 to 2008. + * @param string $sql the existing SQL (without ORDER BY/LIMIT/OFFSET) + * @param array $orderBy the order by columns. See [[Query::orderBy]] for more details on how to specify this parameter. + * @param integer $limit the limit number. See [[Query::limit]] for more details. + * @param integer $offset the offset number. See [[Query::offset]] for more details. + * @return string the SQL completed with ORDER BY/LIMIT/OFFSET (if any) */ - public function buildLimit($limit, $offset = 0) + protected function oldBuildOrderByAndLimit($sql, $orderBy, $limit, $offset) { - $hasOffset = $this->hasOffset($offset); - $hasLimit = $this->hasLimit($limit); - if ($hasOffset || $hasLimit) { - // http://technet.microsoft.com/en-us/library/gg699618.aspx - $sql = 'OFFSET ' . ($hasOffset ? $offset : '0') . ' ROWS'; - if ($hasLimit) { - $sql .= " FETCH NEXT $limit ROWS ONLY"; - } + $orderBy = $this->buildOrderBy($orderBy); + if ($orderBy === '') { + // ROW_NUMBER() requires an ORDER BY clause + $orderBy = 'ORDER BY (SELECT NULL)'; + } + + $sql = preg_replace('/^([\s(])*SELECT(\s+DISTINCT)?(?!\s*TOP\s*\()/i', "\\1SELECT\\2 rowNum = ROW_NUMBER() over ($orderBy),", $sql); - return $sql; + if ($this->hasLimit($limit)) { + $sql = "SELECT TOP $limit * FROM ($sql) sub"; } else { - return ''; + $sql = "SELECT * FROM ($sql) sub"; } + if ($this->hasOffset($offset)) { + $sql .= $this->separator . "WHERE rowNum > $offset"; + } + + return $sql; } /** @@ -126,98 +178,6 @@ public function checkIntegrity($check = true, $schema = '', $table = '') return "ALTER TABLE {$table} {$enable} CONSTRAINT ALL"; } - /** - * @inheritdoc - */ - public function build($query, $params = []) - { - $query->prepareBuild($this); - - $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), - $this->buildJoin($query->join, $params), - $this->buildWhere($query->where, $params), - $this->buildGroupBy($query->groupBy), - $this->buildHaving($query->having, $params), - $orderBy, - $this->isOldMssql() ? '' : $this->buildLimit($query->limit, $query->offset), - ]; - - $sql = implode($this->separator, array_filter($clauses)); - if ($this->isOldMssql()) { - $sql = $this->applyLimitAndOffset($sql, $query); - } - $union = $this->buildUnion($query->union, $params); - if ($union !== '') { - $sql = "($sql){$this->separator}$union"; - } - - return [$sql, $params]; - } - - /** - * Applies limit and offset to SQL query - * - * @param string $sql SQL query - * @param \yii\db\Query $query the [[Query]] object from which the SQL statement generated - * @return string resulting SQL - */ - private function applyLimitAndOffset($sql, $query) - { - $limit = $query->limit !== null ? (int)$query->limit : -1; - $offset = $query->offset !== null ? (int)$query->offset : -1; - if ($limit > 0 || $offset >= 0) { - $sql = $this->rewriteLimitOffsetSql($sql, $limit, $offset, $query); - } - return $sql; - } - - /** - * Rewrites limit and offset in SQL query - * - * @param string $sql SQL query - * @param integer $limit - * @param integer $offset - * @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) - { - $originalOrdering = $this->buildOrderBy($query->orderBy); - if ($query->select) { - $select = implode(', ', $query->select); - } else { - $select = $query->select = '*'; - } - if ($select === '*') { - $columns = $this->getAllColumnNames($query->modelClass); - if ($columns && is_array($columns)) { - $select = implode(', ', $columns); - } else { - $select = $columns; - } - } - $sql = str_replace($originalOrdering, '', $sql); - - if ($originalOrdering === '') { - // hack so LIMIT will work because ROW_NUMBER requires an ORDER BY clause - $originalOrdering = 'ORDER BY (SELECT NULL)'; - } - - $sql = preg_replace('/^([\s(])*SELECT( DISTINCT)?(?!\s*TOP\s*\()/i', "\\1SELECT\\2 rowNum = ROW_NUMBER() over ({$originalOrdering}),", $sql); - $sql = "SELECT TOP {$limit} {$select} FROM ($sql) sub WHERE rowNum > {$offset}"; - return $sql; - } - /** * Returns an array of column names given model name * @@ -242,7 +202,7 @@ protected function getAllColumnNames($modelClass = null) private $_oldMssql; /** - * @return boolean whether MSSQL used is old. + * @return boolean whether the version of the MSSQL being used is older than 2012. * @throws \yii\base\InvalidConfigException * @throws \yii\db\Exception */ diff --git a/framework/db/oci/QueryBuilder.php b/framework/db/oci/QueryBuilder.php index 91bdd398e79..d3f20eb44d7 100644 --- a/framework/db/oci/QueryBuilder.php +++ b/framework/db/oci/QueryBuilder.php @@ -40,66 +40,36 @@ class QueryBuilder extends \yii\db\QueryBuilder Schema::TYPE_MONEY => 'NUMBER(19,4)', ]; - private $_sql; - /** * @inheritdoc */ - public function build($query, $params = []) + public function buildOrderByAndLimit($sql, $orderBy, $limit, $offset) { - $query->prepareBuild($this); - - $params = empty($params) ? $query->params : array_merge($params, $query->params); - - $clauses = [ - $this->buildSelect($query->select, $params, $query->distinct, $query->selectOption), - $this->buildFrom($query->from, $params), - $this->buildJoin($query->join, $params), - $this->buildWhere($query->where, $params), - $this->buildGroupBy($query->groupBy), - $this->buildHaving($query->having, $params), - $this->buildOrderBy($query->orderBy), - ]; - $this->_sql = implode($this->separator, array_filter($clauses)); - - $this->_sql = $this->buildLimit($query->limit, $query->offset); - - $union = $this->buildUnion($query->union, $params); - if ($union !== '') { - $this->_sql = "{$this->_sql}{$this->separator}$union"; + $orderBy = $this->buildOrderBy($orderBy); + if ($orderBy !== '') { + $sql .= $this->separator . $orderBy; } - return [$this->_sql, $params]; - } - - /** - * @inheritdoc - */ - public function buildLimit($limit, $offset) - { $filters = []; - if ($this->hasOffset($offset) > 0) { + if ($this->hasOffset($offset)) { $filters[] = 'rowNumId > ' . $offset; } - if ($this->hasLimit($limit)) { $filters[] = 'rownum <= ' . $limit; } + if (empty($filters)) { + return $sql; + } - if (!empty($filters)) { - $filter = implode(' and ', $filters); - - return <<_sql}), + $filter = implode(' AND ', $filters); + return <<_sql; - } } /**