/** * Returns the complete SQL statement and the values to apply * * @return array ($sql, $vals) */ public function getSQL() { // FROM needs commas only for tables, not joins $from = ''; foreach ($this->from as $alias => $statement) { if ($this->type[$alias] == 'table' && $from) { $from .= ",\n"; } else { $from .= "\n"; } $from .= $statement; } // prepare aliases for the select columns $selects = array(); foreach ($this->select as $alias => $select) { $selects[] = "{$select} AS {$alias}"; } $sql = ' SELECT ' . join(",\n", $selects) . "\n" . ' FROM ' . $from . "\n" . ' WHERE ' . $this->where->toSQL() . "\n"; if ($this->groupby) { $sql .= 'GROUP BY ' . join(",\n", $this->groupby) . "\n"; } if ($this->orderby) { $sql .= 'ORDER BY ' . join(",\n", $this->orderby) . "\n"; } return $this->fixPlaceholders($sql); }