CodeIgniter icon indicating copy to clipboard operation
CodeIgniter copied to clipboard

Proposed new sqlsrv drivers.

Open frypan-handler opened this issue 14 years ago • 7 comments

Is it possible to include the fixes and modifications done at the following links which seem to fix most of the MSSQL related issue?

http://codeigniter.com/forums/viewthread/160626/P10/ https://bitbucket.org/ellislab/codeigniter-reactor/pull-request/11/revising-the-sqlsrv-driver-for-windows

frypan-handler avatar Dec 08 '11 03:12 frypan-handler

https://bitbucket.org/ellislab/codeigniter-reactor/pull-request/11/revising-the-sqlsrv-driver-for-windows The changes in the forge part are a total mess? I'm having problems with the DBForge and SQLSRV driver so though I'd try yours out, however your forge class is unusable :( can you check what you uploaded.

Mat-Moo avatar Dec 14 '11 21:12 Mat-Moo

I don't really like how the rownum column is forced into the result set. Is there a way around this?

narfbg avatar Jun 07 '12 13:06 narfbg

Did not see the earlier update on this change. Tsk tsk for not reading my email. I will find my code and post the changes or the entire classes that I have been using.

frypan-handler avatar Jun 07 '12 20:06 frypan-handler

Here it is for SQL Server 2012: https://github.com/EllisLab/CodeIgniter/commit/d25c589dc3bbfc83fb59bbeb1a3046b99bf694f8

narfbg avatar Jun 08 '12 13:06 narfbg

Here is my code for the _limit method. I was able to break out the original columns and everything. I'm not sure about the sqlsrv_forge being broken...

    /**
     * Limit string
     *
     * Generates a platform-specific LIMIT clause
     *
     * @param   string  the sql query string
     * @param   integer the number of rows to limit the query to
     * @param   integer the offset value
     * @return  string
     */
    protected function _limit($sql, $limit, $offset)
    {
        //return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.($limit + $offset).' ', $sql); //original
        if($offset === FALSE) {
            // do simple limit if no offset
            $i = $limit + $offset;
            return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.$i.' ', $sql);
        } else {
            // As of SQL Server 2012 (11.0.*) OFFSET is supported
            if(version_compare($this->version(), '11', '>=')) {
                return $sql.' OFFSET '.(int) $offset.' ROWS FETCH NEXT '.(int) $limit.' ROWS ONLY';
            } else {
                $orderBy  = "ORDER BY ";
                if(empty($this->ar_orderby))
                {
                    $message = "MSSQL must have an ORDER BY when limiting by offset.";
                    show_error($message);
                    log_message('error',$message);
                }

                $orderBy .= implode(', ', $this->ar_orderby);

                if ($this->ar_order !== FALSE)
                {
                    $orderBy .= ($this->ar_order == 'desc') ? ' DESC' : ' ASC';
                }
                // need to escape regex special characters in $orderBy by using preg_quote()
                $sql = preg_replace('/(\\'. preg_quote($orderBy) .'\n?)/i','', $sql); // remove orderBy from original SQL as the OVER statement uses it
                $sql = preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 row_number() OVER ('.$orderBy.') AS CI_offset_row_number, ', $sql);

                $columns = implode(',',$this->ar_select);
                $newSQL = "SELECT " . $columns . " \nFROM (\n" . $sql . ") AS A \nWHERE A.CI_offset_row_number BETWEEN (" .($offset + 1) . ") AND (".($offset + $limit).")";
                return     $newSQL;
            }
        }
    }

frypan-handler avatar Jun 08 '12 17:06 frypan-handler

I guess there's no other way around it ... https://github.com/EllisLab/CodeIgniter/commit/71379ca89226fe8af0314a8b70e5dc0f57367255

narfbg avatar Jun 11 '12 13:06 narfbg

Yeah, it's really messy until they decided to add the support for it in 2012... :(

frypan-handler avatar Jun 11 '12 21:06 frypan-handler