Welcome Guest, Not a member yet? Register   Sign In
MSSQL Limit - A working "Fix" .
#8

[eluser]Kyle Johnson[/eluser]
New update with this.

Couldn't get it to "work" with the Datatables plugin, but it was working, just the SELECT * was adding an additional column that the DataTables was using.

I am now specifically passing the columns back to the user that they are using with active record.

Code:
/**
  * 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);
  if($offset === FALSE) {
   // do simple limit if no offset
   $i = $limit + $offset;
   return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.$i.' ', $sql);
  } else {
   $orderBy  = "ORDER BY ";
   $orderBy .= implode(', ', $this->ar_orderby);
  
   if ($this->ar_order !== FALSE)
   {
    $orderBy .= ($this->ar_order == 'desc') ? ' DESC' : ' ASC';
   }
   $sql = preg_replace('/(\\'. $orderBy .'\n?)/i','', $sql);
   $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;
  }
}


Messages In This Thread
MSSQL Limit - A working "Fix" . - by El Forum - 07-13-2010, 08:03 AM
MSSQL Limit - A working "Fix" . - by El Forum - 07-14-2010, 08:55 AM
MSSQL Limit - A working "Fix" . - by El Forum - 07-14-2010, 10:43 AM
MSSQL Limit - A working "Fix" . - by El Forum - 07-15-2010, 01:34 PM
MSSQL Limit - A working "Fix" . - by El Forum - 02-18-2011, 02:38 PM
MSSQL Limit - A working "Fix" . - by El Forum - 02-28-2011, 08:01 PM
MSSQL Limit - A working "Fix" . - by El Forum - 08-10-2011, 05:46 AM
MSSQL Limit - A working "Fix" . - by El Forum - 03-07-2012, 02:10 PM
MSSQL Limit - A working "Fix" . - by El Forum - 09-11-2012, 06:22 PM
MSSQL Limit - A working "Fix" . - by El Forum - 06-12-2013, 08:45 AM



Theme © iAndrew 2016 - Forum software by © MyBB