select.php

来自「Bug tracker, and reporter.」· PHP 代码 · 共 1,108 行 · 第 1/3 页

PHP
1,108
字号
     * @param  string $tbl The table/join the columns come from.     * @param  array|string $cols The list of columns; preferably as     * an array, but possibly as a string containing one column.     * @return void     */    protected function _tableCols($correlationName, $cols)    {        if (!is_array($cols)) {            $cols = array($cols);        }        if ($correlationName == null) {            $correlationName = '';        }        foreach ($cols as $alias => $col) {            $currentCorrelationName = $correlationName;            if (is_string($col)) {                // Check for a column matching "<column> AS <alias>" and extract the alias name                if (preg_match('/^(.+)\s+' . self::SQL_AS . '\s+(.+)$/i', $col, $m)) {                    $col = $m[1];                    $alias = $m[2];                }                // Check for columns that look like functions and convert to Zend_Db_Expr                if (preg_match('/\(.*\)/', $col)) {                    $col = new Zend_Db_Expr($col);                } elseif (preg_match('/(.+)\.(.+)/', $col, $m)) {                    $currentCorrelationName = $m[1];                    $col = $m[2];                }            }            $this->_parts[self::COLUMNS][] = array($currentCorrelationName, $col, is_string($alias) ? $alias : null);        }    }    /**     * Internal function for creating the where clause     *     * @param string   $condition     * @param string   $value  optional     * @param string   $type   optional     * @param boolean  $bool  true = AND, false = OR     * @return string  clause     */    protected function _where($condition, $value = null, $type = null, $bool = true)    {        if ($value !== null) {            $condition = $this->_adapter->quoteInto($condition, $value, $type);        }        $cond = "";        if ($this->_parts[self::WHERE]) {            if ($bool === true) {                $cond = self::SQL_AND . ' ';            } else {                $cond = self::SQL_OR . ' ';            }        }                return $cond . "($condition)";    }    /**     * @return array     */    protected function _getDummyTable()    {        return array();    }    /**     * Return a quoted schema name     *     * @param string   $schema  The schema name OPTIONAL     * @return string|null     */    protected function _getQuotedSchema($schema = null)    {        if ($schema === null) {            return null;        }        return $this->_adapter->quoteIdentifier($schema, true) . '.';    }    /**     * Return a quoted table name     *     * @param string   $tableName        The table name     * @param string   $correlationName  The correlation name OPTIONAL     * @return string     */    protected function _getQuotedTable($tableName, $correlationName = null)    {        return $this->_adapter->quoteTableAs($tableName, $correlationName, true);    }    /**     * Render DISTINCT clause     *     * @param string   $sql SQL query     * @return string     */    protected function _renderDistinct($sql)    {        if ($this->_parts[self::DISTINCT]) {            $sql .= ' ' . self::SQL_DISTINCT;        }        return $sql;    }    /**     * Render DISTINCT clause     *     * @param string   $sql SQL query     * @return string     */    protected function _renderColumns($sql)    {        if (!count($this->_parts[self::COLUMNS])) {            return null;        }        $columns = array();        foreach ($this->_parts[self::COLUMNS] as $columnEntry) {            list($correlationName, $column, $alias) = $columnEntry;            if ($column instanceof Zend_Db_Expr) {                $columns[] = $this->_adapter->quoteColumnAs($column, $alias, true);            } else {                if ($column == self::SQL_WILDCARD) {                    $column = new Zend_Db_Expr(self::SQL_WILDCARD);                    $alias = null;                }                if (empty($correlationName)) {                    $columns[] = $this->_adapter->quoteColumnAs($column, $alias, true);                } else {                    $columns[] = $this->_adapter->quoteColumnAs(array($correlationName, $column), $alias, true);                }            }        }        return $sql .= ' ' . implode(', ', $columns);    }    /**     * Render FROM clause     *     * @param string   $sql SQL query     * @return string     */    protected function _renderFrom($sql)    {        /*         * If no table specified, use RDBMS-dependent solution         * for table-less query.  e.g. DUAL in Oracle.         */        if (empty($this->_parts[self::FROM])) {            $this->_parts[self::FROM] = $this->_getDummyTable();        }        $from = array();        foreach ($this->_parts[self::FROM] as $correlationName => $table) {            $tmp = '';            // Add join clause (if applicable)            if (! empty($from)) {                $tmp .= ' ' . strtoupper($table['joinType']) . ' ';            }            $tmp .= $this->_getQuotedSchema($table['schema']);            $tmp .= $this->_getQuotedTable($table['tableName'], $correlationName);            // Add join conditions (if applicable)            if (!empty($from) && ! empty($table['joinCondition'])) {                $tmp .= ' ' . self::SQL_ON . ' ' . $table['joinCondition'];            }            // Add the table name and condition add to the list            $from[] = $tmp;        }        // Add the list of all joins        if (!empty($from)) {            $sql .= ' ' . self::SQL_FROM . ' ' . implode("\n", $from);        }        return $sql;    }    /**     * Render WHERE clause     *     * @param string   $sql SQL query     * @return string     */    protected function _renderWhere($sql)    {        if ($this->_parts[self::FROM] && $this->_parts[self::WHERE]) {            $sql .= ' ' . self::SQL_WHERE . ' ' .  implode(' ', $this->_parts[self::WHERE]);        }        return $sql;    }    /**     * Render GROUP clause     *     * @param string   $sql SQL query     * @return string     */    protected function _renderGroup($sql)    {        if ($this->_parts[self::FROM] && $this->_parts[self::GROUP]) {            $group = array();            foreach ($this->_parts[self::GROUP] as $term) {                $group[] = $this->_adapter->quoteIdentifier($term, true);            }            $sql .= ' ' . self::SQL_GROUP_BY . ' ' . implode(",\n\t", $group);        }        return $sql;    }    /**     * Render HAVING clause     *     * @param string   $sql SQL query     * @return string     */    protected function _renderHaving($sql)    {        if ($this->_parts[self::FROM] && $this->_parts[self::HAVING]) {            $sql .= ' ' . self::SQL_HAVING . ' ' . implode(' ', $this->_parts[self::HAVING]);        }        return $sql;    }    /**     * Render ORDER clause     *     * @param string   $sql SQL query     * @return string     */    protected function _renderOrder($sql)    {        if ($this->_parts[self::ORDER]) {            $order = array();            foreach ($this->_parts[self::ORDER] as $term) {                if (is_array($term)) {                    $order[] = $this->_adapter->quoteIdentifier($term[0], true) . ' ' . $term[1];                } else {                    $order[] = $this->_adapter->quoteIdentifier($term, true);                }            }            $sql .= ' ' . self::SQL_ORDER_BY . ' ' . implode(', ', $order);        }        return $sql;    }    /**     * Render LIMIT OFFSET clause     *     * @param string   $sql SQL query     * @return string     */    protected function _renderLimitoffset($sql)    {        $count = 0;        $offset = 0;        if (!empty($this->_parts[self::LIMIT_OFFSET])) {            $offset = (int) $this->_parts[self::LIMIT_OFFSET];            // This should reduce to the max integer PHP can support            $count = intval(9223372036854775807);        }        if (!empty($this->_parts[self::LIMIT_COUNT])) {            $count = (int) $this->_parts[self::LIMIT_COUNT];        }        /*         * Add limits clause         */        if ($count > 0) {            $sql = trim($this->_adapter->limit($sql, $count, $offset));        }        return $sql;    }    /**     * Render FOR UPDATE clause     *     * @param string   $sql SQL query     * @return string     */    protected function _renderForupdate($sql)    {        if ($this->_parts[self::FOR_UPDATE]) {            $sql .= ' ' . self::SQL_FOR_UPDATE;        }        return $sql;    }    /**     * Turn magic function calls into non-magic function calls     * for joinUsing syntax     *     * @param string $method     * @param array $args OPTIONAL Zend_Db_Table_Select query modifier     * @return Zend_Db_Select     * @throws Zend_Db_Select_Exception If an invalid method is called.     */    protected function __call($method, array $args)    {        $matches = array();        /**         * Recognize methods for Has-Many cases:         * findParent<Class>()         * findParent<Class>By<Rule>()         * Use the non-greedy pattern repeat modifier e.g. \w+?         */        if (preg_match('/^join([a-zA-Z]*?)Using$/', $method, $matches)) {            $type = strtolower($matches[1]);            if ($type) {                $type .= ' join';                if (!in_array($type, self::$_joinTypes)) {                    require_once 'Zend/Db/Select/Exception.php';                    throw new Zend_Db_Select_Exception("Unrecognized method '$method()'");                }                if (in_array($type, array(self::CROSS_JOIN, self::NATURAL_JOIN))) {                    require_once 'Zend/Db/Select/Exception.php';                    throw new Zend_Db_Select_Exception("Cannot perform a joinUsing with method '$method()'");                }            } else {                $type = self::INNER_JOIN;            }            array_unshift($args, $type);            return call_user_func_array(array($this, '_joinUsing'), $args);        }        require_once 'Zend/Db/Select/Exception.php';        throw new Zend_Db_Select_Exception("Unrecognized method '$method()'");    }    /**     * Converts this object to an SQL SELECT string.     *     * @return string This object as a SELECT string.     */    public function __toString()    {        $sql = self::SQL_SELECT;        foreach (array_keys(self::$_partsInit) as $part) {            $method = '_render' . ucfirst($part);            if (method_exists($this, $method)) {                $sql = $this->$method($sql);            }        }        return $sql;    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?