CI_DB_query_builder::_compile_select PHP Method

_compile_select() protected method

Generates a query string based on which functions were used. Should not be called directly.
protected _compile_select ( boolean $select_override = FALSE ) : string
$select_override boolean
return string
    protected function _compile_select($select_override = FALSE)
    {
        // Combine any cached components with the current statements
        $this->_merge_cache();
        // Write the "select" portion of the query
        if ($select_override !== FALSE) {
            $sql = $select_override;
        } else {
            $sql = !$this->qb_distinct ? 'SELECT ' : 'SELECT DISTINCT ';
            if (count($this->qb_select) === 0) {
                $sql .= '*';
            } else {
                // Cycle through the "select" portion of the query and prep each column name.
                // The reason we protect identifiers here rather than in the select() function
                // is because until the user calls the from() function we don't know if there are aliases
                foreach ($this->qb_select as $key => $val) {
                    $no_escape = isset($this->qb_no_escape[$key]) ? $this->qb_no_escape[$key] : NULL;
                    $this->qb_select[$key] = $this->protect_identifiers($val, FALSE, $no_escape);
                }
                $sql .= implode(', ', $this->qb_select);
            }
        }
        // Write the "FROM" portion of the query
        if (count($this->qb_from) > 0) {
            $sql .= "\nFROM " . $this->_from_tables();
        }
        // Write the "JOIN" portion of the query
        if (count($this->qb_join) > 0) {
            $sql .= "\n" . implode("\n", $this->qb_join);
        }
        $sql .= $this->_compile_wh('qb_where') . $this->_compile_group_by() . $this->_compile_wh('qb_having') . $this->_compile_order_by();
        // ORDER BY
        // LIMIT
        if ($this->qb_limit) {
            return $this->_limit($sql . "\n");
        }
        return $sql;
    }