public function toSQL(Parameters $params, bool $inner_clause)
    {
        $drv = $params->getDriver();
        // First get the source tables: FROM and JOIN clauses
        $source = array();

        if ($table = $this->getTable())
            $source[] = "FROM " . $drv->toSQL($params, $table);

        foreach ($this->getJoins() as $join)
            $source[] = $drv->toSQL($params, $join);

        // Now build the start of the query, all tables should be known
        $parts = array();

        $parts[] = "SELECT";
        $fields = $this->getFields();
        if (!empty($fields))
        {
            $field_parts = array();
            foreach ($fields as $field)
                $field_parts[] = $drv->toSQL($params, $field);
            $parts[] = implode(", ", $field_parts);
        }
        else
            $parts[] = "*";

        // Add the source tables and joins to the query
        foreach ($source as $part)
            $parts[] = $part;

        if ($where = $this->getWhere())
            $parts[] = $drv->toSQL($params, $where);

        if ($union = $this->getUnion())
            $parts[] = $drv->toSQL($params, $union);
        
        if ($order = $this->getOrder())
            $parts[] = $drv->toSQL($params, $order);

        if ($limit = $this->getLimit())
            $parts[] = $drv->toSQL($params, $limit);
        
        if ($offset = $this->getOffset())
            $parts[] = $drv->toSQL($params, $offset);
        
        return implode(" ", $parts);
    }