CI_DB_active_record::_compile_select PHP Method

_compile_select() public method

Generates a query string based on which functions were used. Should not be called directly. The get() function calls it.
public _compile_select ( $select_override = FALSE ) : string
return string
    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->ar_distinct ? 'SELECT ' : 'SELECT DISTINCT ';
            if (count($this->ar_select) == 0) {
                $sql .= '*';
            } else {
                // Cycle through the "select" portion of the query and prep each column name.
                // The reason we protect identifiers here rather then in the select() function
                // is because until the user calls the from() function we don't know if there are aliases
                foreach ($this->ar_select as $key => $val) {
                    $this->ar_select[$key] = $this->_protect_identifiers($val);
                }
                $sql .= implode(', ', $this->ar_select);
            }
        }
        // ----------------------------------------------------------------
        // Write the "FROM" portion of the query
        if (count($this->ar_from) > 0) {
            $sql .= "\nFROM ";
            $sql .= $this->_from_tables($this->ar_from);
        }
        // ----------------------------------------------------------------
        // Write the "JOIN" portion of the query
        if (count($this->ar_join) > 0) {
            $sql .= "\n";
            $sql .= implode("\n", $this->ar_join);
        }
        // ----------------------------------------------------------------
        // Write the "WHERE" portion of the query
        if (count($this->ar_where) > 0 or count($this->ar_like) > 0) {
            $sql .= "\n";
            $sql .= "WHERE ";
        }
        $sql .= implode("\n", $this->ar_where);
        // ----------------------------------------------------------------
        // Write the "LIKE" portion of the query
        if (count($this->ar_like) > 0) {
            if (count($this->ar_where) > 0) {
                $sql .= "\nAND ";
            }
            $sql .= implode("\n", $this->ar_like);
        }
        // ----------------------------------------------------------------
        // Write the "GROUP BY" portion of the query
        if (count($this->ar_groupby) > 0) {
            $sql .= "\nGROUP BY ";
            $sql .= implode(', ', $this->ar_groupby);
        }
        // ----------------------------------------------------------------
        // Write the "HAVING" portion of the query
        if (count($this->ar_having) > 0) {
            $sql .= "\nHAVING ";
            $sql .= implode("\n", $this->ar_having);
        }
        // ----------------------------------------------------------------
        // Write the "ORDER BY" portion of the query
        if (count($this->ar_orderby) > 0) {
            $sql .= "\nORDER BY ";
            $sql .= implode(', ', $this->ar_orderby);
            if ($this->ar_order !== FALSE) {
                $sql .= $this->ar_order == 'desc' ? ' DESC' : ' ASC';
            }
        }
        // ----------------------------------------------------------------
        // Write the "LIMIT" portion of the query
        if (is_numeric($this->ar_limit)) {
            $sql .= "\n";
            $sql = $this->_limit($sql, $this->ar_limit, $this->ar_offset);
        }
        return $sql;
    }