ZF-79: Zend_Db_Pdo_Mssql uninitialized variable (TRAC#89 darby)

Description

http://framework.zend.com/developer/ticket/89

Rob Allen confirms this as a bug:

message from Jan Pieper:

I´ve found a variable where I can´t see where it comes from. It is not initialized and nothing.

Zend/Db/Pdo/Mssql.php:137 $order = implode(',', $parts['order']);

What is $parts? (Revision: 464)

-- Jan Pieper

Comments

06/05/06 11:20:07: Modified by gavin

The code in Zend/Db/Pdo/Mssql.php is not consistent with information presented here: http://lists.bestpractical.com/pipermail/rt-devel/…

Hopefully, someone familiar with MSSQL will volunteer a patch :)

{{ /** * Adds an adapter-specific LIMIT clause to the SELECT statement. * * @link http://lists.bestpractical.com/pipermail/rt-devel/… * @return string */ public function limit($sql, $count, $offset) { if ($count) {

       $orderby = stristr($sql, 'ORDER BY');
       if ($orderby !== false) {
           $sort = (stripos($orderby, 'desc') !== false) ? 'desc' : 'asc';
           $order = str_ireplace('ORDER BY', '', $orderby);
           $order = trim(preg_replace('/ASC|DESC/i', '', $order));
       }

       $sql = preg_replace('/^SELECT /i', 'SELECT TOP '.($count+$offset).' ', $sql);

       $sql = 'SELECT * FROM (SELECT TOP '.$count.' * FROM ('.$sql.') AS inner_tbl';
       if ($orderby !== false) {
           $sql .= ' ORDER BY '.$order.' ';
           $sql .= (stripos($sort, 'asc') !== false) ? 'DESC' : 'ASC';
       }
       $sql .= ') AS outer_tbl';
       if ($orderby !== false) {
           $sql .= ' ORDER BY '.$order.' '.$sort;
       }
  }
  return $sql;

}}}

I've found this code works pretty well in some other applications I've done


   /**
    * Adds an adapter-specific LIMIT clause to the SELECT statement.
    *
    * @link http://lists.bestpractical.com/pipermail/rt-devel/…
    * @return string
    */
   public function limit($sql, $count, $offset)
   {
       if ($count) {

           $orderby = stristr($sql, 'ORDER BY');
           if ($orderby !== false) {
               $sort = (stripos($orderby, 'desc') !== false) ? 'desc' : 'asc';
               $order = str_ireplace('ORDER BY', '', $orderby);
               $order = trim(preg_replace('/ASC|DESC/i', '', $order));
           }

           $sql = preg_replace('/^SELECT /i', 'SELECT TOP '.($count+$offset).' ', $sql);

           $sql = 'SELECT * FROM (SELECT TOP '.$count.' * FROM ('.$sql.') AS inner_tbl';
           if ($orderby !== false) {
               $sql .= ' ORDER BY '.$order.' ';
               $sql .= (stripos($sort, 'asc') !== false) ? 'DESC' : 'ASC';
           }
           $sql .= ') AS outer_tbl';
           if ($orderby !== false) {
               $sql .= ' ORDER BY '.$order.' '.$sort;
           }
      }
      return $sql;
  }

Let's try this again.

I don't think I have access to actually make this change in the SVN repository, but I believe this new limit() method will resolve this issue.