示例#1
0
 /**
  * Creates the SQL statement.
  * @param \Database\schema\CommandBuilder $builder the command builder
  * @return \Database\Command DB command instance representing the SQL statement
  */
 public function createCommand($builder)
 {
     $sql = ($this->distinct ? 'SELECT DISTINCT ' : 'SELECT ') . implode(', ', $this->selects);
     $sql .= ' FROM ' . implode(' ', $this->joins);
     $conditions = [];
     foreach ($this->conditions as $condition) {
         if ($condition !== '') {
             $conditions[] = $condition;
         }
     }
     if ($conditions !== []) {
         $sql .= ' WHERE (' . implode(') AND (', $conditions) . ')';
     }
     $groups = [];
     foreach ($this->groups as $group) {
         if ($group !== '') {
             $groups[] = $group;
         }
     }
     if ($groups !== []) {
         $sql .= ' GROUP BY ' . implode(', ', $groups);
     }
     $havings = [];
     foreach ($this->havings as $having) {
         if ($having !== '') {
             $havings[] = $having;
         }
     }
     if ($havings !== []) {
         $sql .= ' HAVING (' . implode(') AND (', $havings) . ')';
     }
     $orders = [];
     foreach ($this->orders as $order) {
         if ($order !== '') {
             $orders[] = $order;
         }
     }
     if ($orders !== []) {
         $sql .= ' ORDER BY ' . implode(', ', $orders);
     }
     $sql = $builder->applyLimit($sql, $this->limit, $this->offset);
     $command = $builder->getDbConnection()->createCommand($sql);
     $builder->bindValues($command, $this->params);
     return $command;
 }