File indexing completed on 2024-06-16 05:29:59

0001 <?php
0002 
0003 /**
0004  * Zend Framework
0005  *
0006  * LICENSE
0007  *
0008  * This source file is subject to the new BSD license that is bundled
0009  * with this package in the file LICENSE.txt.
0010  * It is also available through the world-wide-web at this URL:
0011  * http://framework.zend.com/license/new-bsd
0012  * If you did not receive a copy of the license and are unable to
0013  * obtain it through the world-wide-web, please send an email
0014  * to license@zend.com so we can send you a copy immediately.
0015  *
0016  * @category   Zend
0017  * @package    Zend_Db
0018  * @subpackage Select
0019  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0020  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0021  * @version    $Id$
0022  */
0023 
0024 
0025 /**
0026  * @see Zend_Db_Select
0027  */
0028 // require_once 'Zend/Db/Select.php';
0029 
0030 
0031 /**
0032  * @see Zend_Db_Table_Abstract
0033  */
0034 // require_once 'Zend/Db/Table/Abstract.php';
0035 
0036 
0037 /**
0038  * Class for SQL SELECT query manipulation for the Zend_Db_Table component.
0039  *
0040  * @category   Zend
0041  * @package    Zend_Db
0042  * @subpackage Table
0043  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0044  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0045  */
0046 class Zend_Db_Table_Select extends Zend_Db_Select
0047 {
0048     /**
0049      * Table schema for parent Zend_Db_Table.
0050      *
0051      * @var array
0052      */
0053     protected $_info;
0054 
0055     /**
0056      * Table integrity override.
0057      *
0058      * @var array
0059      */
0060     protected $_integrityCheck = true;
0061 
0062     /**
0063      * Table instance that created this select object
0064      *
0065      * @var Zend_Db_Table_Abstract
0066      */
0067     protected $_table;
0068 
0069     /**
0070      * Class constructor
0071      *
0072      * @param Zend_Db_Table_Abstract $adapter
0073      */
0074     public function __construct(Zend_Db_Table_Abstract $table)
0075     {
0076         parent::__construct($table->getAdapter());
0077 
0078         $this->setTable($table);
0079     }
0080 
0081     /**
0082      * Return the table that created this select object
0083      *
0084      * @return Zend_Db_Table_Abstract
0085      */
0086     public function getTable()
0087     {
0088         return $this->_table;
0089     }
0090 
0091     /**
0092      * Sets the primary table name and retrieves the table schema.
0093      *
0094      * @param Zend_Db_Table_Abstract $adapter
0095      * @return Zend_Db_Select This Zend_Db_Select object.
0096      */
0097     public function setTable(Zend_Db_Table_Abstract $table)
0098     {
0099         $this->_adapter = $table->getAdapter();
0100         $this->_info    = $table->info();
0101         $this->_table   = $table;
0102 
0103         return $this;
0104     }
0105 
0106     /**
0107      * Sets the integrity check flag.
0108      *
0109      * Setting this flag to false skips the checks for table joins, allowing
0110      * 'hybrid' table rows to be created.
0111      *
0112      * @param Zend_Db_Table_Abstract $adapter
0113      * @return Zend_Db_Select This Zend_Db_Select object.
0114      */
0115     public function setIntegrityCheck($flag = true)
0116     {
0117         $this->_integrityCheck = $flag;
0118         return $this;
0119     }
0120 
0121     /**
0122      * Tests query to determine if expressions or aliases columns exist.
0123      *
0124      * @return boolean
0125      */
0126     public function isReadOnly()
0127     {
0128         $readOnly = false;
0129         $fields   = $this->getPart(Zend_Db_Table_Select::COLUMNS);
0130         $cols     = $this->_info[Zend_Db_Table_Abstract::COLS];
0131 
0132         if (!count($fields)) {
0133             return $readOnly;
0134         }
0135 
0136         foreach ($fields as $columnEntry) {
0137             $column = $columnEntry[1];
0138             $alias = $columnEntry[2];
0139 
0140             if ($alias !== null) {
0141                 $column = $alias;
0142             }
0143 
0144             switch (true) {
0145                 case ($column == self::SQL_WILDCARD):
0146                     break;
0147 
0148                 case ($column instanceof Zend_Db_Expr):
0149                 case (!in_array($column, $cols)):
0150                     $readOnly = true;
0151                     break 2;
0152             }
0153         }
0154 
0155         return $readOnly;
0156     }
0157 
0158     /**
0159      * Adds a FROM table and optional columns to the query.
0160      *
0161      * The table name can be expressed
0162      *
0163      * @param  array|string|Zend_Db_Expr|Zend_Db_Table_Abstract $name The table name or an
0164                                                                       associative array relating
0165                                                                       table name to correlation
0166                                                                       name.
0167      * @param  array|string|Zend_Db_Expr $cols The columns to select from this table.
0168      * @param  string $schema The schema name to specify, if any.
0169      * @return Zend_Db_Table_Select This Zend_Db_Table_Select object.
0170      */
0171     public function from($name, $cols = self::SQL_WILDCARD, $schema = null)
0172     {
0173         if ($name instanceof Zend_Db_Table_Abstract) {
0174             $info = $name->info();
0175             $name = $info[Zend_Db_Table_Abstract::NAME];
0176             if (isset($info[Zend_Db_Table_Abstract::SCHEMA])) {
0177                 $schema = $info[Zend_Db_Table_Abstract::SCHEMA];
0178             }
0179         }
0180 
0181         return $this->joinInner($name, null, $cols, $schema);
0182     }
0183 
0184     /**
0185      * Performs a validation on the select query before passing back to the parent class.
0186      * Ensures that only columns from the primary Zend_Db_Table are returned in the result.
0187      *
0188      * @return string|null This object as a SELECT string (or null if a string cannot be produced)
0189      */
0190     public function assemble()
0191     {
0192         $fields  = $this->getPart(Zend_Db_Table_Select::COLUMNS);
0193         $primary = $this->_info[Zend_Db_Table_Abstract::NAME];
0194         $schema  = $this->_info[Zend_Db_Table_Abstract::SCHEMA];
0195 
0196 
0197         if (count($this->_parts[self::UNION]) == 0) {
0198 
0199             // If no fields are specified we assume all fields from primary table
0200             if (!count($fields)) {
0201                 $this->from($primary, self::SQL_WILDCARD, $schema);
0202                 $fields = $this->getPart(Zend_Db_Table_Select::COLUMNS);
0203             }
0204 
0205             $from = $this->getPart(Zend_Db_Table_Select::FROM);
0206 
0207             if ($this->_integrityCheck !== false) {
0208                 foreach ($fields as $columnEntry) {
0209                     list($table, $column) = $columnEntry;
0210 
0211                     // Check each column to ensure it only references the primary table
0212                     if ($column) {
0213                         if (!isset($from[$table]) || $from[$table]['tableName'] != $primary) {
0214                             // require_once 'Zend/Db/Table/Select/Exception.php';
0215                             throw new Zend_Db_Table_Select_Exception('Select query cannot join with another table');
0216                         }
0217                     }
0218                 }
0219             }
0220         }
0221 
0222         return parent::assemble();
0223     }
0224 }