ci2.x鏈接sqlsrv數(shù)據(jù)庫limit不生效的解決方法

使用2.2版本查詢sqrsrv數(shù)據(jù)庫的時(shí)候,發(fā)現(xiàn)limit方法根本不生效。

為此,我特別到github上下載了最新的代碼,然后做了相應(yīng)的修改。

修改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);
    }

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • sqlmap用戶手冊(cè) 說明:本文為轉(zhuǎn)載,對(duì)原文中一些明顯的拼寫錯(cuò)誤進(jìn)行修正,并標(biāo)注對(duì)自己有用的信息。 ======...
    wind_飄閱讀 2,114評(píng)論 0 5
  • http://192.168.136.131/sqlmap/mysql/get_int.php?id=1 當(dāng)給sq...
    xuningbo閱讀 10,434評(píng)論 2 22
  • php.ini設(shè)置,上傳大文件: post_max_size = 128Mupload_max_filesize ...
    bycall閱讀 6,834評(píng)論 3 64
  • 語 句 功 能 數(shù)據(jù)操作 SELECT——從數(shù)據(jù)庫表中檢索數(shù)據(jù)行和列INSERT——向數(shù)據(jù)庫表添加新數(shù)據(jù)行DELE...
    戰(zhàn)敭閱讀 5,123評(píng)論 0 53
  • //獲得相關(guān)文章,有兩個(gè)參數(shù),分別是文章分類和顯示條數(shù).如果參數(shù)沒有寫的話,默認(rèn)分類為未分類,顯示條數(shù)為6條 fu...
    f675b1a02698閱讀 183評(píng)論 0 0