select.php
来自「Bug tracker, and reporter.」· PHP 代码 · 共 1,108 行 · 第 1/3 页
PHP
1,108 行
* * Otherwise identical to where(). * * @param string $cond The WHERE condition. * @param string $value OPTIONAL A single value to quote into the condition. * @param constant $type OPTIONAL The type of the given value * @return Zend_Db_Select This Zend_Db_Select object. * * @see where() */ public function orWhere($cond, $value = null, $type = null) { $this->_parts[self::WHERE][] = $this->_where($cond, $value, $type, false); return $this; } /** * Adds grouping to the query. * * @param array|string $spec The column(s) to group by. * @return Zend_Db_Select This Zend_Db_Select object. */ public function group($spec) { if (!is_array($spec)) { $spec = array($spec); } foreach ($spec as $val) { if (preg_match('/\(.*\)/', (string) $val)) { $val = new Zend_Db_Expr($val); } $this->_parts[self::GROUP][] = $val; } return $this; } /** * Adds a HAVING condition to the query by AND. * * If a value is passed as the second param, it will be quoted * and replaced into the condition wherever a question-mark * appears. See {@link where()} for an example * * @param string $cond The HAVING condition. * @param string|Zend_Db_Expr $val A single value to quote into the condition. * @return Zend_Db_Select This Zend_Db_Select object. */ public function having($cond) { if (func_num_args() > 1) { $val = func_get_arg(1); $cond = $this->_adapter->quoteInto($cond, $val); } if ($this->_parts[self::HAVING]) { $this->_parts[self::HAVING][] = self::SQL_AND . " ($cond)"; } else { $this->_parts[self::HAVING][] = "($cond)"; } return $this; } /** * Adds a HAVING condition to the query by OR. * * Otherwise identical to orHaving(). * * @param string $cond The HAVING condition. * @param string $val A single value to quote into the condition. * @return Zend_Db_Select This Zend_Db_Select object. * * @see having() */ public function orHaving($cond) { if (func_num_args() > 1) { $val = func_get_arg(1); $cond = $this->_adapter->quoteInto($cond, $val); } if ($this->_parts[self::HAVING]) { $this->_parts[self::HAVING][] = self::SQL_OR . " ($cond)"; } else { $this->_parts[self::HAVING][] = "($cond)"; } return $this; } /** * Adds a row order to the query. * * @param mixed $spec The column(s) and direction to order by. * @return Zend_Db_Select This Zend_Db_Select object. */ public function order($spec) { if (!is_array($spec)) { $spec = array($spec); } // force 'ASC' or 'DESC' on each order spec, default is ASC. foreach ($spec as $val) { if ($val instanceof Zend_Db_Expr) { $expr = $val->__toString(); if (empty($expr)) { continue; } $this->_parts[self::ORDER][] = $val; } else { if (empty($val)) { continue; } $direction = self::SQL_ASC; if (preg_match('/(.*\W)(' . self::SQL_ASC . '|' . self::SQL_DESC . ')\b/si', $val, $matches)) { $val = trim($matches[1]); $direction = $matches[2]; } if (preg_match('/\(.*\)/', $val)) { $val = new Zend_Db_Expr($val); } $this->_parts[self::ORDER][] = array($val, $direction); } } return $this; } /** * Sets a limit count and offset to the query. * * @param int $count OPTIONAL The number of rows to return. * @param int $offset OPTIONAL Start returning after this many rows. * @return Zend_Db_Select This Zend_Db_Select object. */ public function limit($count = null, $offset = null) { $this->_parts[self::LIMIT_COUNT] = (int) $count; $this->_parts[self::LIMIT_OFFSET] = (int) $offset; return $this; } /** * Sets the limit and count by page number. * * @param int $page Limit results to this page number. * @param int $rowCount Use this many rows per page. * @return Zend_Db_Select This Zend_Db_Select object. */ public function limitPage($page, $rowCount) { $page = ($page > 0) ? $page : 1; $rowCount = ($rowCount > 0) ? $rowCount : 1; $this->_parts[self::LIMIT_COUNT] = (int) $rowCount; $this->_parts[self::LIMIT_OFFSET] = (int) $rowCount * ($page - 1); return $this; } /** * Makes the query SELECT FOR UPDATE. * * @param bool $flag Whether or not the SELECT is FOR UPDATE (default true). * @return Zend_Db_Select This Zend_Db_Select object. */ public function forUpdate($flag = true) { $this->_parts[self::FOR_UPDATE] = (bool) $flag; return $this; } /** * Get part of the structured information for the currect query. * * @param string $part * @return mixed * @throws Zend_Db_Select_Exception */ public function getPart($part) { $part = strtolower($part); if (!array_key_exists($part, $this->_parts)) { require_once 'Zend/Db/Select/Exception.php'; throw new Zend_Db_Select_Exception("Invalid Select part '$part'"); } return $this->_parts[$part]; } /** * Executes the current select object and returns the result * * @param integer $fetchMode OPTIONAL * @return PDO_Statement|Zend_Db_Statement */ public function query($fetchMode = null) { $stmt = $this->_adapter->query($this); if ($fetchMode == null) { $fetchMode = $this->_adapter->getFetchMode(); } $stmt->setFetchMode($fetchMode); return $stmt; } /** * Clear parts of the Select object, or an individual part. * * @param string $part OPTIONAL * @return Zend_Db_Select */ public function reset($part = null) { if ($part == null) { $this->_parts = self::$_partsInit; } else if (array_key_exists($part, self::$_partsInit)) { $this->_parts[$part] = self::$_partsInit[$part]; } return $this; } /** * Populate the {@link $_parts} 'join' key * * Does the dirty work of populating the join key. * * The $name and $cols parameters follow the same logic * as described in the from() method. * * @param null|string $type Type of join; inner, left, and null are currently supported * @param array|string|Zend_Db_Expr $name Table name * @param string $cond Join on this condition * @param array|string $cols The columns to select from the joined table * @param string $schema The database name to specify, if any. * @return Zend_Db_Select This Zend_Db_Select object * @throws Zend_Db_Select_Exception */ protected function _join($type, $name, $cond, $cols, $schema = null) { if (!in_array($type, self::$_joinTypes)) { /** * @see Zend_Db_Select_Exception */ require_once 'Zend/Db/Select/Exception.php'; throw new Zend_Db_Select_Exception("Invalid join type '$type'"); } if (empty($name)) { $correlationName = $tableName = ''; } else if (is_array($name)) { // Must be array($correlationName => $tableName) or array($ident, ...) foreach ($name as $_correlationName => $_tableName) { if (is_string($_correlationName)) { // We assume the key is the correlation name and value is the table name $tableName = $_tableName; $correlationName = $_correlationName; } else { // We assume just an array of identifiers, with no correlation name $tableName = $name; $correlationName = $this->_uniqueCorrelation($tableName); } break; } } else if ($name instanceof Zend_Db_Expr|| $name instanceof Zend_Db_Select) { $tableName = $name; $correlationName = $this->_uniqueCorrelation('t'); } else if (preg_match('/^(.+)\s+AS\s+(.+)$/i', $name, $m)) { $tableName = $m[1]; $correlationName = $m[2]; } else { $tableName = $name; $correlationName = $this->_uniqueCorrelation($tableName); } // Schema from table name overrides schema argument if (!is_object($tableName) && false !== strpos($tableName, '.')) { list($schema, $tableName) = explode('.', $tableName); } if (!empty($correlationName)) { if (array_key_exists($correlationName, $this->_parts[self::FROM])) { /** * @see Zend_Db_Select_Exception */ require_once 'Zend/Db/Select/Exception.php'; throw new Zend_Db_Select_Exception("You cannot define a correlation name '$correlationName' more than once"); } $this->_parts[self::FROM][$correlationName] = array( 'joinType' => $type, 'schema' => $schema, 'tableName' => $tableName, 'joinCondition' => $cond ); } // add to the columns from this joined table $this->_tableCols($correlationName, $cols); return $this; } /** * Handle JOIN... USING... syntax * * This is functionality identical to the existing JOIN methods, however * the join condition can be passed as a single column name. This method * then completes the ON condition by using the same field for the FROM * table and the JOIN table. * * <code> * $select = $db->select()->from('table1') * ->joinUsing('table2', 'column1'); * * // SELECT * FROM table1 JOIN table2 ON table1.column1 = table2.column2 * </code> * * These joins are called by the developer simply by adding 'Using' to the * method name. E.g. * * joinUsing * * joinInnerUsing * * joinFullUsing * * joinRightUsing * * joinLeftUsing * * @return Zend_Db_Select This Zend_Db_Select object. */ public function _joinUsing($type, $name, $cond, $cols = '*', $schema = null) { if (empty($this->_parts[self::FROM])) { require_once 'Zend/Db/Select/Exception.php'; throw new Zend_Db_Select_Exception("You can only perform a joinUsing after specifying a FROM table"); } $join = $this->_adapter->quoteIdentifier(key($this->_parts[self::FROM])); $from = $this->_adapter->quoteIdentifier($this->_uniqueCorrelation($name)); $cond1 = $from . '.' . $cond; $cond2 = $join . '.' . $cond; $cond = $cond1 . ' = ' . $cond2; return $this->_join($type, $name, $cond, $cols, $schema); } /** * Generate a unique correlation name * * @param string|array $name A qualified identifier. * @return string A unique correlation name. */ private function _uniqueCorrelation($name) { if (is_array($name)) { $c = end($name); } else { // Extract just the last name of a qualified table name $dot = strrpos($name,'.'); $c = ($dot === false) ? $name : substr($name, $dot+1); } for ($i = 2; array_key_exists($c, $this->_parts[self::FROM]); ++$i) { $c = $name . '_' . (string) $i; } return $c; } /** * Adds to the internal table-to-column mapping array. *
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?