Ejemplo n.º 1
0
 /**
  * (non-PHPdoc)
  * @see QueryObjectInterface::buildQuery()
  */
 public function buildQuery()
 {
     if (!$this->__sql) {
         $sql = $this->sql;
         if (empty($sql)) {
             if ($this->accept(QueryObject::SELECT)) {
                 /**
                  *
                  * fields
                  *
                  */
                 $fields = $this->fields;
                 if (empty($fields)) {
                     $fields = '*';
                 } else {
                     foreach ($fields as &$field) {
                         if ($field instanceof QueryObjectInterface) {
                             $field = $field->buildQuery();
                         }
                     }
                     $fields = implode(', ', $fields);
                 }
                 $sql = "SELECT {$fields} FROM ";
             } elseif ($this->accept(QueryObject::INSERT)) {
                 $sql = "INSERT INTO ";
             } elseif ($this->accept(QueryObject::UPDATE)) {
                 $sql = "UPDATE ";
             } elseif ($this->accept(QueryObject::DELETE)) {
                 $sql = "DELETE FROM ";
             }
             /**
              *
              * tables
              *
              */
             if (empty($this->table)) {
                 throw new Exception('No table set for query');
             } else {
                 $tables = $this->table;
                 foreach ($tables as &$table) {
                     if ($table instanceof QueryObjectInterface) {
                         $table = $table->buildQuery();
                     }
                 }
                 $table = implode(', ', $tables);
             }
             $sql .= "{$table} ";
             if ($this->accept(QueryObject::UPDATE) && !empty($this->data)) {
                 $sql .= 'SET ' . QueryObject::buildUpdate($this->data) . ' ';
             } elseif ($this->accept(QueryObject::INSERT) && !empty($this->data)) {
                 $sql .= QueryObject::buildInsert($this->data) . ' ';
             }
             if ($this->accept(QueryObject::SELECT)) {
                 /**
                  *
                  * joins
                  *
                  */
                 if (!empty($this->joins)) {
                     $joins = $this->joins;
                     foreach ($joins as &$join) {
                         if ($join[1] instanceof QueryObjectInterface) {
                             $join[1] = $join->buildQuery();
                         }
                         $join = implode(' ', $join);
                     }
                     $sql .= implode(' ', $joins) . ' ';
                 }
             }
             if ($this->accept(QueryObject::ALL ^ QueryObject::INSERT)) {
                 /**
                  *
                  * where
                  *
                  */
                 if (!empty($this->where)) {
                     $where = $this->where;
                     foreach ($where as &$_where) {
                         if ($_where instanceof QueryObjectInterface) {
                             $_where = $_where->buildQuery();
                         }
                     }
                     $where = BoolExpr::parse($where);
                     $sql .= "WHERE {$where} ";
                 }
             }
             if ($this->accept(QueryObject::SELECT)) {
                 /**
                  *
                  * group by and having
                  *
                  */
                 if (!empty($this->groupBy)) {
                     $groupBy = implode(', ', $this->groupBy);
                     $sql .= "GROUP BY {$groupBy} ";
                     if (!empty($this->having)) {
                         $having = $this->having;
                         foreach ($having as &$_having) {
                             if ($_having instanceof QueryObjectInterface) {
                                 $_having = $_having->buildQuery();
                             }
                         }
                         $having = BoolExpr::parse($having);
                         $sql .= "HAVING {$having} ";
                     }
                 }
             }
             if ($this->accept(QueryObject::ALL ^ QueryObject::INSERT)) {
                 /**
                  *
                  * order by
                  *
                  */
                 if (!empty($this->orderBy)) {
                     $orderBy = $this->orderBy;
                     $order = array();
                     foreach ($orderBy as $field => $direction) {
                         $order[] = implode(' ', array($field, $direction));
                     }
                     $orderBy = implode(', ', $order);
                     $sql .= "ORDER BY {$orderBy} ";
                 }
             }
             if (!is_null($this->limit)) {
                 $sql .= "LIMIT {$this->limit} ";
             }
             if (!is_null($this->offset)) {
                 $sql .= "OFFSET {$this->offset} ";
             }
             if ($this->alias && $this->accept(QueryObject::SELECT)) {
                 $sql = "({$sql}) {$this->alias}";
             }
         }
         $this->__sql = $sql;
     }
     return $this->__sql;
 }