web/lib/Zend/Paginator/Adapter/DbSelect.php
changeset 807 877f952ae2bd
parent 207 621fa6caec0c
child 1230 68c69c656a2c
--- a/web/lib/Zend/Paginator/Adapter/DbSelect.php	Thu Mar 21 17:31:31 2013 +0100
+++ b/web/lib/Zend/Paginator/Adapter/DbSelect.php	Thu Mar 21 19:50:53 2013 +0100
@@ -14,9 +14,9 @@
  *
  * @category   Zend
  * @package    Zend_Paginator
- * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
+ * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  * @license    http://framework.zend.com/license/new-bsd     New BSD License
- * @version    $Id: DbSelect.php 20096 2010-01-06 02:05:09Z bkarwin $
+ * @version    $Id: DbSelect.php 24754 2012-05-05 02:30:56Z adamlundrigan $
  */
 
 /**
@@ -37,7 +37,7 @@
 /**
  * @category   Zend
  * @package    Zend_Paginator
- * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
+ * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  * @license    http://framework.zend.com/license/new-bsd     New BSD License
  */
 class Zend_Paginator_Adapter_DbSelect implements Zend_Paginator_Adapter_Interface
@@ -71,6 +71,14 @@
     protected $_rowCount = null;
 
     /**
+     * Identifies this adapter for caching purposes.  This value will remain constant for
+     * the entire life of this adapter regardless of how many different pages are queried.
+     *
+     * @var string
+     */
+    protected $_cacheIdentifier = null;
+
+    /**
      * Constructor.
      *
      * @param Zend_Db_Select $select The select query
@@ -78,9 +86,20 @@
     public function __construct(Zend_Db_Select $select)
     {
         $this->_select = $select;
+        $this->_cacheIdentifier = md5($select->assemble());
     }
 
     /**
+     * Returns the cache identifier.
+     * 
+     * @return string
+     */
+    public function getCacheIdentifier()
+    {
+        return $this->_cacheIdentifier;
+    }
+    
+    /**
      * Sets the total row count, either directly or through a supplied
      * query.  Without setting this, {@link getPages()} selects the count
      * as a subquery (SELECT COUNT ... FROM (SELECT ...)).  While this
@@ -100,7 +119,9 @@
         if ($rowCount instanceof Zend_Db_Select) {
             $columns = $rowCount->getPart(Zend_Db_Select::COLUMNS);
 
-            $countColumnPart = $columns[0][1];
+            $countColumnPart = empty($columns[0][2])
+                             ? $columns[0][1]
+                             : $columns[0][2];
 
             if ($countColumnPart instanceof Zend_Db_Expr) {
                 $countColumnPart = $countColumnPart->__toString();
@@ -201,7 +222,10 @@
         if (!empty($unionParts)) {
             $expression = new Zend_Db_Expr($countPart . $countColumn);
 
-            $rowCount = $db->select()->from($rowCount, $expression);
+            $rowCount = $db
+                            ->select()
+                            ->bind($rowCount->getBind())
+                            ->from($rowCount, $expression);
         } else {
             $columnParts = $rowCount->getPart(Zend_Db_Select::COLUMNS);
             $groupParts  = $rowCount->getPart(Zend_Db_Select::GROUP);
@@ -213,8 +237,13 @@
              * than one group, or if the query has a HAVING clause, then take
              * the original query and use it as a subquery os the COUNT query.
              */
-            if (($isDistinct && count($columnParts) > 1) || count($groupParts) > 1 || !empty($havingParts)) {
-                $rowCount = $db->select()->from($this->_select);
+            if (($isDistinct && ((count($columnParts) == 1 && $columnParts[0][1] == Zend_Db_Select::SQL_WILDCARD) 
+                 || count($columnParts) > 1)) || count($groupParts) > 1 || !empty($havingParts)) {
+                $rowCount->reset(Zend_Db_Select::ORDER);
+                $rowCount = $db
+                               ->select()
+                               ->bind($rowCount->getBind())
+                               ->from($rowCount);
             } else if ($isDistinct) {
                 $part = $columnParts[0];
 
@@ -227,8 +256,7 @@
 
                     $groupPart = $column;
                 }
-            } else if (!empty($groupParts) && $groupParts[0] !== Zend_Db_Select::SQL_WILDCARD &&
-                       !($groupParts[0] instanceof Zend_Db_Expr)) {
+            } else if (!empty($groupParts)) {
                 $groupPart = $db->quoteIdentifier($groupParts[0], true);
             }