ci2.x链接sqlsrv数据库limit不生效的解决方法

使用2.2版本查询sqrsrv数据库的时候,发现limit方法根本不生效。

为此,我特别到github上下载了最新的代码,然后做了相应的修改。

修改database/drivers/sqlsrv/sqlsrv _driver.phpdatabase/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);
    }

你可能感兴趣的:(ci2.x链接sqlsrv数据库limit不生效的解决方法)