/**
  * Get the SQL for a select statement
  * @param zibo\library\database\manipulation\statement\manipulation\SelectStatement $statement
  * @return string SQL of the select statement
  */
 protected function parseSelectStatement(SelectStatement $statement)
 {
     $operator = $statement->getOperator();
     $sql = 'SELECT ';
     if ($statement->isDistinct()) {
         $sql .= 'DISTINCT ';
     }
     $fields = $statement->getFields();
     if (empty($fields)) {
         $sql . '*';
     } else {
         $sql .= $this->parseExpressionsForSelect($fields);
     }
     $tables = $statement->getTables();
     if ($tables) {
         $sql .= ' FROM ' . $this->parseTableExpressionsForFrom($tables);
     }
     $conditions = $statement->getConditions();
     if ($conditions) {
         $sql .= ' WHERE ' . $this->parseConditions($conditions, $operator, false);
     }
     $group = $statement->getGroupBy();
     if ($group) {
         $sql .= ' GROUP BY ' . $this->parseExpressions($group, ', ', true);
     }
     $having = $statement->getHaving();
     if ($having) {
         $sql .= ' HAVING ' . $this->parseConditions($having, $operator, true);
     }
     $order = $statement->getOrderBy();
     if ($order) {
         $sql .= ' ORDER BY ' . $this->parseExpressions($order, ', ', false);
     }
     $limit = $statement->getLimit();
     if ($limit) {
         $sql .= $this->parseLimitExpression($limit);
     }
     return $sql;
 }
Пример #2
0
 /**
  * Gets the names of the models used by this query
  * @param zibo\library\database\manipulation\statement\SelectStatement $statemenet
  * @return array Array with the names of the models used by this query
  */
 private function getUsedModels(SelectStatement $statement)
 {
     $usedModels = array();
     $tables = $statement->getTables();
     foreach ($tables as $table) {
         $usedModels[] = $table->getName();
         $joins = $table->getJoins();
         foreach ($joins as $join) {
             $modelName = $join->getTable()->getName();
             $usedModels[$modelName] = $modelName;
         }
     }
     return $usedModels;
 }