web/lib/Zend/Db/Select.php
changeset 1230 68c69c656a2c
parent 807 877f952ae2bd
equal deleted inserted replaced
1229:5a6b6e770365 1230:68c69c656a2c
    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_Db
    16  * @package    Zend_Db
    17  * @subpackage Select
    17  * @subpackage Select
    18  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
    18  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    20  * @version    $Id: Select.php 24833 2012-05-30 13:29:41Z adamlundrigan $
    20  * @version    $Id$
    21  */
    21  */
    22 
    22 
    23 
    23 
    24 /**
    24 /**
    25  * @see Zend_Db_Adapter_Abstract
    25  * @see Zend_Db_Adapter_Abstract
    36  * Class for SQL SELECT generation and results.
    36  * Class for SQL SELECT generation and results.
    37  *
    37  *
    38  * @category   Zend
    38  * @category   Zend
    39  * @package    Zend_Db
    39  * @package    Zend_Db
    40  * @subpackage Select
    40  * @subpackage Select
    41  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
    41  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
    42  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    42  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    43  */
    43  */
    44 class Zend_Db_Select
    44 class Zend_Db_Select
    45 {
    45 {
    46 
    46 
   507         if (!is_array($spec)) {
   507         if (!is_array($spec)) {
   508             $spec = array($spec);
   508             $spec = array($spec);
   509         }
   509         }
   510 
   510 
   511         foreach ($spec as $val) {
   511         foreach ($spec as $val) {
   512             if (preg_match('/\(.*\)/', (string) $val)) {
   512             if (preg_match('/^([\w]*\(([^\)]|(?1))*\))$/', (string) $val)) {
   513                 $val = new Zend_Db_Expr($val);
   513                 $val = new Zend_Db_Expr($val);
   514             }
   514             }
   515             $this->_parts[self::GROUP][] = $val;
   515             $this->_parts[self::GROUP][] = $val;
   516         }
   516         }
   517 
   517 
   599                 $direction = self::SQL_ASC;
   599                 $direction = self::SQL_ASC;
   600                 if (preg_match('/(.*\W)(' . self::SQL_ASC . '|' . self::SQL_DESC . ')\b/si', $val, $matches)) {
   600                 if (preg_match('/(.*\W)(' . self::SQL_ASC . '|' . self::SQL_DESC . ')\b/si', $val, $matches)) {
   601                     $val = trim($matches[1]);
   601                     $val = trim($matches[1]);
   602                     $direction = $matches[2];
   602                     $direction = $matches[2];
   603                 }
   603                 }
   604                 if (preg_match('/\(.*\)/', $val)) {
   604                 if (preg_match('/^([\w]*\(([^\)]|(?1))*\))$/', (string) $val)) {
   605                     $val = new Zend_Db_Expr($val);
   605                     $val = new Zend_Db_Expr($val);
   606                 }
   606                 }
   607                 $this->_parts[self::ORDER][] = array($val, $direction);
   607                 $this->_parts[self::ORDER][] = array($val, $direction);
   608             }
   608             }
   609         }
   609         }
   652         $this->_parts[self::FOR_UPDATE] = (bool) $flag;
   652         $this->_parts[self::FOR_UPDATE] = (bool) $flag;
   653         return $this;
   653         return $this;
   654     }
   654     }
   655 
   655 
   656     /**
   656     /**
   657      * Get part of the structured information for the currect query.
   657      * Get part of the structured information for the current query.
   658      *
   658      *
   659      * @param string $part
   659      * @param string $part
   660      * @return mixed
   660      * @return mixed
   661      * @throws Zend_Db_Select_Exception
   661      * @throws Zend_Db_Select_Exception
   662      */
   662      */
   716      */
   716      */
   717     public function reset($part = null)
   717     public function reset($part = null)
   718     {
   718     {
   719         if ($part == null) {
   719         if ($part == null) {
   720             $this->_parts = self::$_partsInit;
   720             $this->_parts = self::$_partsInit;
   721         } else if (array_key_exists($part, self::$_partsInit)) {
   721         } elseif (array_key_exists($part, self::$_partsInit)) {
   722             $this->_parts[$part] = self::$_partsInit[$part];
   722             $this->_parts[$part] = self::$_partsInit[$part];
   723         }
   723         }
   724         return $this;
   724         return $this;
   725     }
   725     }
   726 
   726 
   766             throw new Zend_Db_Select_Exception("Invalid use of table with " . self::SQL_UNION);
   766             throw new Zend_Db_Select_Exception("Invalid use of table with " . self::SQL_UNION);
   767         }
   767         }
   768 
   768 
   769         if (empty($name)) {
   769         if (empty($name)) {
   770             $correlationName = $tableName = '';
   770             $correlationName = $tableName = '';
   771         } else if (is_array($name)) {
   771         } elseif (is_array($name)) {
   772             // Must be array($correlationName => $tableName) or array($ident, ...)
   772             // Must be array($correlationName => $tableName) or array($ident, ...)
   773             foreach ($name as $_correlationName => $_tableName) {
   773             foreach ($name as $_correlationName => $_tableName) {
   774                 if (is_string($_correlationName)) {
   774                 if (is_string($_correlationName)) {
   775                     // We assume the key is the correlation name and value is the table name
   775                     // We assume the key is the correlation name and value is the table name
   776                     $tableName = $_tableName;
   776                     $tableName = $_tableName;
   780                     $tableName = $_tableName;
   780                     $tableName = $_tableName;
   781                     $correlationName = $this->_uniqueCorrelation($tableName);
   781                     $correlationName = $this->_uniqueCorrelation($tableName);
   782                 }
   782                 }
   783                 break;
   783                 break;
   784             }
   784             }
   785         } else if ($name instanceof Zend_Db_Expr|| $name instanceof Zend_Db_Select) {
   785         } elseif ($name instanceof Zend_Db_Expr|| $name instanceof Zend_Db_Select) {
   786             $tableName = $name;
   786             $tableName = $name;
   787             $correlationName = $this->_uniqueCorrelation('t');
   787             $correlationName = $this->_uniqueCorrelation('t');
   788         } else if (preg_match('/^(.+)\s+AS\s+(.+)$/i', $name, $m)) {
   788         } elseif (preg_match('/^(.+)\s+AS\s+(.+)$/i', $name, $m)) {
   789             $tableName = $m[1];
   789             $tableName = $m[1];
   790             $correlationName = $m[2];
   790             $correlationName = $m[2];
   791         } else {
   791         } else {
   792             $tableName = $name;
   792             $tableName = $name;
   793             $correlationName = $this->_uniqueCorrelation($tableName);
   793             $correlationName = $this->_uniqueCorrelation($tableName);
   941                 if (preg_match('/^(.+)\s+' . self::SQL_AS . '\s+(.+)$/i', $col, $m)) {
   941                 if (preg_match('/^(.+)\s+' . self::SQL_AS . '\s+(.+)$/i', $col, $m)) {
   942                     $col = $m[1];
   942                     $col = $m[1];
   943                     $alias = $m[2];
   943                     $alias = $m[2];
   944                 }
   944                 }
   945                 // Check for columns that look like functions and convert to Zend_Db_Expr
   945                 // Check for columns that look like functions and convert to Zend_Db_Expr
   946                 if (preg_match('/\(.*\)/', $col)) {
   946                 if (preg_match('/^([\w]*\(([^\)]|(?1))*\))$/', (string) $col)) {
   947                     $col = new Zend_Db_Expr($col);
   947                     $col = new Zend_Db_Expr($col);
   948                 } elseif (preg_match('/(.+)\.(.+)/', $col, $m)) {
   948                 } elseif (preg_match('/(.+)\.(.+)/', $col, $m)) {
   949                     $currentCorrelationName = $m[1];
   949                     $currentCorrelationName = $m[1];
   950                     $col = $m[2];
   950                     $col = $m[2];
   951                 }
   951                 }
  1094                     $columns[] = $this->_adapter->quoteColumnAs(array($correlationName, $column), $alias, true);
  1094                     $columns[] = $this->_adapter->quoteColumnAs(array($correlationName, $column), $alias, true);
  1095                 }
  1095                 }
  1096             }
  1096             }
  1097         }
  1097         }
  1098 
  1098 
  1099         return $sql .= ' ' . implode(', ', $columns);
  1099         return $sql . ' ' . implode(', ', $columns);
  1100     }
  1100     }
  1101 
  1101 
  1102     /**
  1102     /**
  1103      * Render FROM clause
  1103      * Render FROM clause
  1104      *
  1104      *
  1236                     if(is_numeric($term[0]) && strval(intval($term[0])) == $term[0]) {
  1236                     if(is_numeric($term[0]) && strval(intval($term[0])) == $term[0]) {
  1237                         $order[] = (int)trim($term[0]) . ' ' . $term[1];
  1237                         $order[] = (int)trim($term[0]) . ' ' . $term[1];
  1238                     } else {
  1238                     } else {
  1239                         $order[] = $this->_adapter->quoteIdentifier($term[0], true) . ' ' . $term[1];
  1239                         $order[] = $this->_adapter->quoteIdentifier($term[0], true) . ' ' . $term[1];
  1240                     }
  1240                     }
  1241                 } else if (is_numeric($term) && strval(intval($term)) == $term) {
  1241                 } elseif (is_numeric($term) && strval(intval($term)) == $term) {
  1242                     $order[] = (int)trim($term);
  1242                     $order[] = (int)trim($term);
  1243                 } else {
  1243                 } else {
  1244                     $order[] = $this->_adapter->quoteIdentifier($term, true);
  1244                     $order[] = $this->_adapter->quoteIdentifier($term, true);
  1245                 }
  1245                 }
  1246             }
  1246             }