使用2.2版本查詢sqrsrv數(shù)據(jù)庫的時候越平,發(fā)現(xiàn)limit方法根本不生效闸拿。
為此褐桌,我特別到github上下載了最新的代碼,然后做了相應的修改饼记。
修改database/drivers/sqlsrv/sqlsrv _driver.php
或database/drivers/mssql/mssql_driver.php
文件中的limit方法,完整代碼如下:
protected function _limit($sql,$limit, $offset)
{
// As of SQL Server 2012 (11.0.*) OFFSET is supported
if (version_compare($this->version(), '11', '>='))
{
// SQL Server OFFSET-FETCH can be used only with the ORDER BY clause
empty($this->ar_orderby) && $sql .= ' ORDER BY 1';
return $sql.' OFFSET '.(int) $offset.' ROWS FETCH NEXT '.$limit.' ROWS ONLY';
}
// $limit = $this->ar_offset + $this->ar_limit;
// An ORDER BY clause is required for ROW_NUMBER() to work
if ($this->ar_offset && ! empty($this->ar_orderby))
{
$orderby = $this->_compile_order_by();
// We have to strip the ORDER BY clause
$sql = trim(substr($sql, 0, strrpos($sql, $orderby)));
// Get the fields to select from our subquery, so that we can avoid CI_rownum appearing in the actual results
if (count($this->qb_select) === 0)
{
$select = '*'; // Inevitable
}
else
{
// Use only field names and their aliases, everything else is out of our scope.
$select = array();
$field_regexp = ($this->_quoted_identifier)
? '("[^\"]+")' : '(\[[^\]]+\])';
for ($i = 0, $c = count($this->qb_select); $i < $c; $i++)
{
$select[] = preg_match('/(?:\s|\.)'.$field_regexp.'$/i', $this->qb_select[$i], $m)
? $m[1] : $this->qb_select[$i];
}
$select = implode(', ', $select);
}
return 'SELECT '.$select." FROM (\n\n"
.preg_replace('/^(SELECT( DISTINCT)?)/i', '\\1 ROW_NUMBER() OVER('.trim($orderby).') AS '.$this->escape_identifiers('CI_rownum').', ', $sql)
."\n\n) ".$this->escape_identifiers('CI_subquery')
."\nWHERE ".$this->escape_identifiers('CI_rownum').' BETWEEN '.($offset + 1).' AND '.($offset+$limit);
}
return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.$limit.' ', $sql);
}