/** * Convert a Select SugarQuery Object into a string * * @return string */ protected function compileSelectQuery() { $select_part = '*'; $from_part = ''; $where_part = ''; $join_part = ''; $distinct = ''; $group_by_part = ''; $order_by_part = ''; $having_part = ''; // if there aren't any selected fields, add them all if (empty($this->sugar_query->select->select) && $this->sugar_query->select->getCountQuery() === false) { $this->sugar_query->select('*'); } if (!empty($this->sugar_query->from)) { $from_part = trim($this->compileFrom($this->sugar_query->from)); } if (!empty($this->sugar_query->select)) { $select_part = trim($this->compileSelect($this->sugar_query->select)); } if (!empty($this->sugar_query->where)) { $this->sugar_query->startData('where'); $where_part = trim($this->compileWhere($this->sugar_query->where)); $this->sugar_query->endData(); } if ($this->sugar_query->distinct) { $distinct = 'DISTINCT'; } if (!empty($this->sugar_query->group_by)) { $group_by_part = $this->compileGroupBy($this->sugar_query->group_by); } if (!empty($this->sugar_query->having)) { $this->sugar_query->startData('having'); $having_part = $this->compileHaving($this->sugar_query->having); $this->sugar_query->endData(); } if (!empty($this->sugar_query->order_by)) { $order_by_part = $this->compileOrderBy($this->sugar_query->order_by); } if (!empty($this->sugar_query->join)) { $this->sugar_query->startData('join'); $join_part = trim($this->compileJoin($this->sugar_query->join)); $this->sugar_query->endData(); } $data = array(); $sql = "SELECT {$distinct} {$select_part} FROM {$from_part}"; if (!empty($join_part)) { $sql .= " {$join_part} "; $data = array_merge($data, $this->sugar_query->getData('join')); } if (!empty($where_part)) { $sql .= " WHERE {$where_part} "; $data = array_merge($data, $this->sugar_query->getData('where')); } if (!empty($group_by_part)) { $sql .= " GROUP BY {$group_by_part} "; } if (!empty($having_part)) { $sql .= " HAVING {$having_part} "; $data = array_merge($data, $this->sugar_query->getData('having')); } if (!empty($order_by_part)) { $sql .= " ORDER BY {$order_by_part} "; } if (!empty($this->sugar_query->limit) && $this->sugar_query->select->getCountQuery() === false) { $sql = $this->db->limitQuery($sql, $this->sugar_query->offset, $this->sugar_query->limit, false, '', false); } $this->sugar_query->data = $data; return trim($sql); }