public function toSql()
    {
        $sql = 'SELECT ';
        if ($this->distinct != null) {
            $sql .= 'DISTINCT ON (' . $this->distinct . ') ';
        }
        $sql .= ($this->select != null ? $this->select : '*') . ' ';
        if (empty($this->from) === true) {
            throw new BadUse('from clause has not been defined (' . print_r($this, true) . ')');
        }
        $sql .= 'FROM ' . $this->from . ' ';

        foreach ($this->innerJoins as $join) {
            $sql .= 'INNER JOIN ' . $join['table'] . ' ON ' . $join['on']->toSql() . ' ';
        }
        foreach ($this->leftOuterJoins as $join) {
            $sql .= 'LEFT OUTER JOIN ' . $join['table'] . ' ON ' . $join['on']->toSql() . ' ';
        }
        foreach ($this->rightOuterJoins as $join) {
            $sql .= 'RIGHT OUTER JOIN ' . $join['table'] . ' ON ' . $join['on']->toSql() . ' ';
        }
        foreach ($this->fullOuterJoins as $join) {
            $sql .= 'FULL OUTER JOIN ' . $join['table'] . ' ON ' . $join['on']->toSql() . ' ';
        }

        if ($this->where != null) {
            $sql .= 'WHERE ' . $this->where->toSql() . ' ';
        }
        if ($this->groupBy != null) {
            $sql .= 'GROUP BY ' . $this->groupBy . ' ';
        }
        if ($this->having != null) {
            $sql .= 'HAVING ' . $this->having->toSql() . ' ';
        }
        if ($this->orderBy != null) {
            $sql .= 'ORDER BY ' . $this->orderBy . ' ';
        }
        if ($this->limit != null) {
            $sql .= 'LIMIT ' . $this->limit . ' ';
        }
        if ($this->offset != null) {
            $sql .= 'OFFSET ' . $this->offset . ' ';
        }

        return $sql;
    }
public function toSql()
    {
        $sql = 'SELECT ';
        if ($this->distinct != null) {
            $sql .= 'DISTINCT ON (' . $this->distinct . ') ';
        }
        $sql .= ($this->select != null ? $this->select : '*') . ' ';
        if (empty($this->from) === true) {
            throw new BadUse('from clause has not been defined (' . print_r($this, true) . ')');
        }
        $sql .= 'FROM ' . $this->from . ' ';

        foreach ($this->innerJoins as $join) {
            $sql .= 'INNER JOIN ' . $join['table'] . ' ON ' . $join['on']->toSql() . ' ';
        }
        foreach ($this->leftOuterJoins as $join) {
            $sql .= 'LEFT OUTER JOIN ' . $join['table'] . ' ON ' . $join['on']->toSql() . ' ';
        }
        foreach ($this->rightOuterJoins as $join) {
            $sql .= 'RIGHT OUTER JOIN ' . $join['table'] . ' ON ' . $join['on']->toSql() . ' ';
        }
        foreach ($this->fullOuterJoins as $join) {
            $sql .= 'FULL OUTER JOIN ' . $join['table'] . ' ON ' . $join['on']->toSql() . ' ';
        }

        if ($this->where != null) {
            $sql .= 'WHERE ' . $this->where->toSql() . ' ';
        }
        if ($this->groupBy != null) {
            $sql .= 'GROUP BY ' . $this->groupBy . ' ';
        }
        if ($this->having != null) {
            $sql .= 'HAVING ' . $this->having->toSql() . ' ';
        }
        if ($this->orderBy != null) {
            $sql .= 'ORDER BY ' . $this->orderBy . ' ';
        }
        if ($this->limit != null) {
            $sql .= 'LIMIT ' . $this->limit . ' ';
        }
        if ($this->offset != null) {
            $sql .= 'OFFSET ' . $this->offset . ' ';
        }

        return $sql;
    }
Extract the corresponding SQL code.

@throws BadUse if from clause has not been defined
@return string generated SQL code