Ejemplo n.º 1
0
 /**
  * Compile the SQL query and return it.
  *
  * @param   object  Database instance
  * @return  string
  */
 public function compile(Database $db)
 {
     // Callback to quote identifiers
     $quote_ident = array($db, 'quote_identifier');
     // Start a selection query
     $query = 'SELECT ';
     if ($this->_distinct === TRUE) {
         // Select only unique results
         $query .= 'DISTINCT ';
     }
     if (empty($this->_select)) {
         // Select all columns
         $query .= '*';
     } else {
         // Select all columns
         $query .= implode(', ', array_map($quote_ident, $this->_select));
     }
     if (!empty($this->_from)) {
         // Set tables to select from
         $query .= ' FROM ' . implode(', ', array_map(array($db, 'quote_table'), $this->_from));
     }
     if (!empty($this->_join)) {
         // Add tables to join
         $query .= ' ' . Database_Query_Builder::compile_join($db, $this->_join);
     }
     if (!empty($this->_where)) {
         // Add selection conditions
         $query .= ' WHERE ' . Database_Query_Builder::compile_conditions($db, $this->_where);
     }
     if (!empty($this->_group_by)) {
         // Add sorting
         $query .= ' GROUP BY ' . implode(', ', array_map($quote_ident, $this->_group_by));
     }
     if (!empty($this->_having)) {
         // Add filtering conditions
         $query .= ' HAVING ' . Database_Query_Builder::compile_conditions($db, $this->_having);
     }
     if (!empty($this->_order_by)) {
         // Add sorting
         $query .= ' ' . Database_Query_Builder::compile_order_by($db, $this->_order_by);
     }
     if ($this->_limit !== NULL) {
         // Add limiting
         $query .= ' LIMIT ' . $this->_limit;
     }
     if ($this->_offset !== NULL) {
         // Add offsets
         $query .= ' OFFSET ' . $this->_offset;
     }
     return $query;
 }