sql = $sql; $this->connection = $connection === NULL ? dibi::getConnection() : $connection; } /** * @param int offset * @param int limit * @param array columns * @return DibiResultIterator */ public function getIterator($offset = NULL, $limit = NULL) { return $this->connection->query(' SELECT %n', (empty($this->cols) ? '*' : $this->cols), ' FROM (%SQL) AS [t]', $this->sql, ' WHERE %and', $this->conds, ' ORDER BY %by', $this->sorting, ' %ofs %lmt', $offset, $limit )->getIterator(); } /** * @return int */ public function count() { if ($this->count === NULL) { $this->count = $this->connection->nativeQuery( 'SELECT COUNT(*) FROM (' . $this->sql . ') AS t' )->fetchSingle(); } return $this->count; } /** * Returns SQL wrapped as DibiFluent. * @return DibiFluent * @throws DibiException */ public function toFluent() { return $this->connection->select('*')->from('(%SQL) AS [t]', $this->__toString()); } /** * @return DibiDataSource */ public function toDataSource() { return new self($this->__toString(), $this->connection); } /** * Returns SQL query. * @return string */ final public function __toString() { return $this->connection->sql(' SELECT %n', (empty($this->cols) ? '*' : $this->cols), ' FROM (%SQL) AS [t]', $this->sql, ' WHERE %and', $this->conds, ' ORDER BY %by', $this->sorting, ' '); } /** */ public function select($col, $as = NULL) { if (is_array($col)) { $this->cols = $col; } else { $this->cols[$col] = $as; } } /** */ public function where($cond) { if (is_array($cond)) { // TODO: not consistent with select and orderBy $this->conds[] = $cond; } else { $this->conds[] = func_get_args(); } } /** */ public function orderBy($row, $sorting = 'ASC') { if (is_array($row)) { $this->sorting = $row; } else { $this->sorting[$row] = $sorting; } } /** * Returns the dibi connection. * @return DibiConnection */ final public function getConnection() { return $this->connection; } }