public function getQuery()
{
if (count($this->parts['select']) === 0) {
throw new QueryException('Missing "select" parts to generate query.');
}
$sql = 'SELECT ';
if ($this->distinct) {
$sql .= 'DISTINCT ';
}
$sql .= implode(', ', $this->parts['select']) . ' FROM';
if (count($this->parts['from']) === 0) {
throw new QueryException('Missing "from" parts to generate query.');
}
$renderedFromBefore = false;
foreach ($this->parts['from'] as $fromPart) {
if ($fromPart['type'] === 'FROM') {
if ($renderedFromBefore === true) {
$sql .= ',';
}
$sql .= ' ' . $fromPart['table'];
$renderedFromBefore = true;
} else {
$sql .= ' ' . $fromPart['type'] . ' JOIN ' . $fromPart['table'];
if ($fromPart['condition']) {
$sql .= ' ON ' . $fromPart['condition'];
}
}
}
if (count($this->parts['where']) > 0) {
$sql .= ' WHERE ' . implode(' AND ', $this->parts['where']);
}
if (count($this->parts['groupBy']) > 0) {
$sql .= ' GROUP BY ' . implode(', ', $this->parts['groupBy']);
}
if (count($this->parts['having']) > 0) {
$sql .= ' HAVING ' . implode(' AND ', $this->parts['having']);
}
if (count($this->parts['orderBy']) > 0) {
$sql .= ' ORDER BY ' . implode(', ', $this->parts['orderBy']);
}
if ($this->limit || $this->offset) {
$sql = $this->connection->getDatabasePlatform()->modifyLimitQuery($sql, $this->limit, $this->offset);
}
return $sql;
}