web/lib/Zend/Paginator/Adapter/DbSelect.php
changeset 807 877f952ae2bd
parent 207 621fa6caec0c
child 1230 68c69c656a2c
equal deleted inserted replaced
805:5e7a0fedabdf 807:877f952ae2bd
    12  * obtain it through the world-wide-web, please send an email
    12  * obtain it through the world-wide-web, please send an email
    13  * to license@zend.com so we can send you a copy immediately.
    13  * to license@zend.com so we can send you a copy immediately.
    14  *
    14  *
    15  * @category   Zend
    15  * @category   Zend
    16  * @package    Zend_Paginator
    16  * @package    Zend_Paginator
    17  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
    17  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
    18  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    18  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    19  * @version    $Id: DbSelect.php 20096 2010-01-06 02:05:09Z bkarwin $
    19  * @version    $Id: DbSelect.php 24754 2012-05-05 02:30:56Z adamlundrigan $
    20  */
    20  */
    21 
    21 
    22 /**
    22 /**
    23  * @see Zend_Paginator_Adapter_Interface
    23  * @see Zend_Paginator_Adapter_Interface
    24  */
    24  */
    35 require_once 'Zend/Db/Select.php';
    35 require_once 'Zend/Db/Select.php';
    36 
    36 
    37 /**
    37 /**
    38  * @category   Zend
    38  * @category   Zend
    39  * @package    Zend_Paginator
    39  * @package    Zend_Paginator
    40  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
    40  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
    41  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    41  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    42  */
    42  */
    43 class Zend_Paginator_Adapter_DbSelect implements Zend_Paginator_Adapter_Interface
    43 class Zend_Paginator_Adapter_DbSelect implements Zend_Paginator_Adapter_Interface
    44 {
    44 {
    45     /**
    45     /**
    69      * @var integer
    69      * @var integer
    70      */
    70      */
    71     protected $_rowCount = null;
    71     protected $_rowCount = null;
    72 
    72 
    73     /**
    73     /**
       
    74      * Identifies this adapter for caching purposes.  This value will remain constant for
       
    75      * the entire life of this adapter regardless of how many different pages are queried.
       
    76      *
       
    77      * @var string
       
    78      */
       
    79     protected $_cacheIdentifier = null;
       
    80 
       
    81     /**
    74      * Constructor.
    82      * Constructor.
    75      *
    83      *
    76      * @param Zend_Db_Select $select The select query
    84      * @param Zend_Db_Select $select The select query
    77      */
    85      */
    78     public function __construct(Zend_Db_Select $select)
    86     public function __construct(Zend_Db_Select $select)
    79     {
    87     {
    80         $this->_select = $select;
    88         $this->_select = $select;
    81     }
    89         $this->_cacheIdentifier = md5($select->assemble());
    82 
    90     }
       
    91 
       
    92     /**
       
    93      * Returns the cache identifier.
       
    94      * 
       
    95      * @return string
       
    96      */
       
    97     public function getCacheIdentifier()
       
    98     {
       
    99         return $this->_cacheIdentifier;
       
   100     }
       
   101     
    83     /**
   102     /**
    84      * Sets the total row count, either directly or through a supplied
   103      * Sets the total row count, either directly or through a supplied
    85      * query.  Without setting this, {@link getPages()} selects the count
   104      * query.  Without setting this, {@link getPages()} selects the count
    86      * as a subquery (SELECT COUNT ... FROM (SELECT ...)).  While this
   105      * as a subquery (SELECT COUNT ... FROM (SELECT ...)).  While this
    87      * yields an accurate count even with queries containing clauses like
   106      * yields an accurate count even with queries containing clauses like
    98     public function setRowCount($rowCount)
   117     public function setRowCount($rowCount)
    99     {
   118     {
   100         if ($rowCount instanceof Zend_Db_Select) {
   119         if ($rowCount instanceof Zend_Db_Select) {
   101             $columns = $rowCount->getPart(Zend_Db_Select::COLUMNS);
   120             $columns = $rowCount->getPart(Zend_Db_Select::COLUMNS);
   102 
   121 
   103             $countColumnPart = $columns[0][1];
   122             $countColumnPart = empty($columns[0][2])
       
   123                              ? $columns[0][1]
       
   124                              : $columns[0][2];
   104 
   125 
   105             if ($countColumnPart instanceof Zend_Db_Expr) {
   126             if ($countColumnPart instanceof Zend_Db_Expr) {
   106                 $countColumnPart = $countColumnPart->__toString();
   127                 $countColumnPart = $countColumnPart->__toString();
   107             }
   128             }
   108 
   129 
   199          * to the COUNT query.
   220          * to the COUNT query.
   200          */
   221          */
   201         if (!empty($unionParts)) {
   222         if (!empty($unionParts)) {
   202             $expression = new Zend_Db_Expr($countPart . $countColumn);
   223             $expression = new Zend_Db_Expr($countPart . $countColumn);
   203 
   224 
   204             $rowCount = $db->select()->from($rowCount, $expression);
   225             $rowCount = $db
       
   226                             ->select()
       
   227                             ->bind($rowCount->getBind())
       
   228                             ->from($rowCount, $expression);
   205         } else {
   229         } else {
   206             $columnParts = $rowCount->getPart(Zend_Db_Select::COLUMNS);
   230             $columnParts = $rowCount->getPart(Zend_Db_Select::COLUMNS);
   207             $groupParts  = $rowCount->getPart(Zend_Db_Select::GROUP);
   231             $groupParts  = $rowCount->getPart(Zend_Db_Select::GROUP);
   208             $havingParts = $rowCount->getPart(Zend_Db_Select::HAVING);
   232             $havingParts = $rowCount->getPart(Zend_Db_Select::HAVING);
   209             $isDistinct  = $rowCount->getPart(Zend_Db_Select::DISTINCT);
   233             $isDistinct  = $rowCount->getPart(Zend_Db_Select::DISTINCT);
   211             /**
   235             /**
   212              * If there is more than one column AND it's a DISTINCT query, more
   236              * If there is more than one column AND it's a DISTINCT query, more
   213              * than one group, or if the query has a HAVING clause, then take
   237              * than one group, or if the query has a HAVING clause, then take
   214              * the original query and use it as a subquery os the COUNT query.
   238              * the original query and use it as a subquery os the COUNT query.
   215              */
   239              */
   216             if (($isDistinct && count($columnParts) > 1) || count($groupParts) > 1 || !empty($havingParts)) {
   240             if (($isDistinct && ((count($columnParts) == 1 && $columnParts[0][1] == Zend_Db_Select::SQL_WILDCARD) 
   217                 $rowCount = $db->select()->from($this->_select);
   241                  || count($columnParts) > 1)) || count($groupParts) > 1 || !empty($havingParts)) {
       
   242                 $rowCount->reset(Zend_Db_Select::ORDER);
       
   243                 $rowCount = $db
       
   244                                ->select()
       
   245                                ->bind($rowCount->getBind())
       
   246                                ->from($rowCount);
   218             } else if ($isDistinct) {
   247             } else if ($isDistinct) {
   219                 $part = $columnParts[0];
   248                 $part = $columnParts[0];
   220 
   249 
   221                 if ($part[1] !== Zend_Db_Select::SQL_WILDCARD && !($part[1] instanceof Zend_Db_Expr)) {
   250                 if ($part[1] !== Zend_Db_Select::SQL_WILDCARD && !($part[1] instanceof Zend_Db_Expr)) {
   222                     $column = $db->quoteIdentifier($part[1], true);
   251                     $column = $db->quoteIdentifier($part[1], true);
   225                         $column = $db->quoteIdentifier($part[0], true) . '.' . $column;
   254                         $column = $db->quoteIdentifier($part[0], true) . '.' . $column;
   226                     }
   255                     }
   227 
   256 
   228                     $groupPart = $column;
   257                     $groupPart = $column;
   229                 }
   258                 }
   230             } else if (!empty($groupParts) && $groupParts[0] !== Zend_Db_Select::SQL_WILDCARD &&
   259             } else if (!empty($groupParts)) {
   231                        !($groupParts[0] instanceof Zend_Db_Expr)) {
       
   232                 $groupPart = $db->quoteIdentifier($groupParts[0], true);
   260                 $groupPart = $db->quoteIdentifier($groupParts[0], true);
   233             }
   261             }
   234 
   262 
   235             /**
   263             /**
   236              * If the original query had a GROUP BY or a DISTINCT part and only
   264              * If the original query had a GROUP BY or a DISTINCT part and only