public function getSqlStatement() { $statement = new \Maestro\Database\MSQL(); if (count($this->columns) == 0) { $this->select('*'); } $sqlColumns = array(); foreach ($this->columns as $column) { $sqlColumns[] = $this->getOperand($column)->getSql(); } $columns = implode(',', $sqlColumns); $statement->setColumns($columns, $this->distinct); if (($where = $this->whereCondition->getSql()) != '') { $statement->setWhere($where); } if (count($this->groups)) { $sqlGroup = array(); foreach ($this->groups as $group) { $sqlGroups[] = $this->getOperand($group)->getSqlGroup(); } $groups = implode(',', $sqlGroups); $statement->setGroupBy($groups); } if (($having = $this->havingCondition->getSql()) != '') { $statement->setHaving($having); } if (count($this->orders)) { $sqlOrders = array(); foreach ($this->orders as $order) { $o = explode(' ', $order); $sqlOrders[] = $this->getOperand($o[0])->getSqlOrder() . ' ' . $o[1]; } $orders = implode(',', $sqlOrders); $statement->setOrderBy($orders); } if ($n = count($this->tableCriteria)) { for ($i = 0; $i < $n; $i++) { $tables .= ($i > 0 ? ", " : "") . '(' . $this->tableCriteria[$i][0] . ')' . ' ' . $this->tableCriteria[$i][1]; } $statement->setTables($tables); } $hasJoin = false; $joins = $this->getForcedJoin(); if (count($joins)) { $hasJoin = true; foreach ($joins as $join) { $statement->join[] = $join; } } $joins = $this->getAssociationsJoin(); if (count($joins)) { $hasJoin = true; foreach ($joins as $join) { $statement->join[] = $join; } } if (!$hasJoin) { if (count($this->classes)) { $sqlTables = array(); foreach ($this->classes as $class) { $sqlTables[] = $this->getTableName($class[0]) . ' ' . $class[1]; } $tables = implode(',', $sqlTables); $statement->setTables($tables); } } // Set parameters to the select statement if (!is_null($this->parameters)) { $statement->setParameters($this->parameters); } // Add a range clause to the select statement if (!is_null($this->range)) { $statement->setRange($this->range); } // Add a FOR UPDATE clause to the select statement if ($this->forUpdate) { $statement->setForUpdate(TRUE); } // Add Set Operations if (count($this->setOperation)) { foreach ($this->setOperation as $s) { $statement->setSetOperation($s[0], $s[1]->getSqlStatement()); } } return $statement; }