Commit 661fe24a by Qiang Xue

Merge pull request #2137 from Ana1oliy/issue-1659

issue #1659: released buildLimitOffset() for MSSQL
parents 9eb27c63 9c97fe11
......@@ -49,10 +49,22 @@ class QueryBuilder extends \yii\db\QueryBuilder
// return '';
// }
// public function buildLimit($limit, $offset)
// {
// return '';
// }
/**
* @param integer $limit
* @param integer $offset
* @return string the LIMIT and OFFSET clauses built from [[query]].
*/
public function buildLimit($limit, $offset = 0)
{
$sql = '';
if ($offset !== null && $offset >= 0) {
$sql = 'OFFSET ' . (int)$offset . ' ROWS';
if ($limit !== null && $limit >= 0) {
$sql .= ' FETCH NEXT ' . (int)$limit . ' ROWS ONLY';
}
}
return $sql;
}
// public function resetSequence($table, $value = null)
// {
......@@ -120,4 +132,4 @@ class QueryBuilder extends \yii\db\QueryBuilder
$enable = $check ? 'CHECK' : 'NOCHECK';
return "ALTER TABLE {$table} {$enable} CONSTRAINT ALL";
}
}
}
\ No newline at end of file
<?php
namespace yiiunit\framework\db\mssql;
use yiiunit\framework\db\QueryBuilderTest;
use yii\db\Query;
/**
* @group db
* @group mssql
*/
class MSSQLQueryBuilderTest extends QueryBuilderTest
{
public $driverName = 'sqlsrv';
public function testOffsetLimit() {
$expectedQuerySql = 'SELECT `id` FROM `exapmle` OFFSET 5 ROWS FETCH NEXT 10 ROWS ONLY';
$expectedQueryParams = null;
$query = new Query();
$query->select('id')
->from('example')
->limit(10)->offset(5);
list($actualQuerySql, $actualQueryParams) = $this->getQueryBuilder()->build($query);
$this->assertEquals($expectedQuerySql, $actualQuerySql);
$this->assertEquals($expectedQueryParams, $actualQueryParams);
}
public function testLimit() {
$expectedQuerySql = 'SELECT `id` FROM `exapmle` OFFSET 0 ROWS FETCH NEXT 10 ROWS ONLY';
$expectedQueryParams = null;
$query = new Query();
$query->select('id')
->from('example')
->limit(10);
list($actualQuerySql, $actualQueryParams) = $this->getQueryBuilder()->build($query);
$this->assertEquals($expectedQuerySql, $actualQuerySql);
$this->assertEquals($expectedQueryParams, $actualQueryParams);
}
public function testOffset() {
$expectedQuerySql = 'SELECT `id` FROM `exapmle` OFFSET 10 ROWS';
$expectedQueryParams = null;
$query = new Query();
$query->select('id')
->from('example')
->offset(10);
list($actualQuerySql, $actualQueryParams) = $this->getQueryBuilder()->build($query);
$this->assertEquals($expectedQuerySql, $actualQuerySql);
$this->assertEquals($expectedQueryParams, $actualQueryParams);
}
}
\ No newline at end of file
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