Skip to content

Commit

Permalink
Fixes yiisoft#4254: SqlDataProvider does not work with Oracle and S…
Browse files Browse the repository at this point in the history
…QL Server
  • Loading branch information
qiangxue committed Sep 3, 2014
1 parent 85af21c commit 212c5ee
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 164 deletions.
3 changes: 1 addition & 2 deletions docs/guide/db-dao.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ uniform API and solves some inconsistencies between different DBMS. By default Y
- [PostgreSQL](http:https://www.postgresql.org/)
- [CUBRID](http:https://www.cubrid.org/): version 9.1.0 or higher.
- [Oracle](http:https://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
Expand Down
22 changes: 22 additions & 0 deletions extensions/sphinx/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]].
Expand Down Expand Up @@ -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)
{
Expand Down
1 change: 1 addition & 0 deletions framework/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
33 changes: 21 additions & 12 deletions framework/data/SqlDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Yii;
use yii\base\InvalidConfigException;
use yii\db\Connection;
use yii\db\Expression;
use yii\di\Instance;

/**
Expand Down Expand Up @@ -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();
}

Expand Down
24 changes: 22 additions & 2 deletions framework/db/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 !== '') {
Expand Down Expand Up @@ -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]].
Expand Down
174 changes: 67 additions & 107 deletions framework/db/mssql/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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:https://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:https://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;
}

/**
Expand Down Expand Up @@ -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
*
Expand All @@ -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
*/
Expand Down
Loading

0 comments on commit 212c5ee

Please sign in to comment.