/** @return DibiDataSource */ public final function getDataSource() { if ($this->dataSource === NULL) { list($sorting, $limit, $offset) = $this->process(); FindByHelper::dibiProcess($this, $this->getMapper(), $this->getConventional(), $this->where, $this->findBy, $this->tableAlias); static $dsClass; if ($dsClass === NULL) { // @codeCoverageIgnoreStart $dsClass = 'DibiDataSource'; if (class_exists('DibiDataSourceX')) { $dsClass = 'DibiDataSourceX'; } } // @codeCoverageIgnoreEnd $ds = new $dsClass($this->sql, $this->getConnection()); foreach ($this->where as $where) { $ds->where($where); } foreach ($sorting as $tmp) { list($key, $direction) = $tmp; $ds->orderBy($key, $direction); } if ($limit !== NULL or $offset) { $ds->applyLimit($limit, $offset); } $this->dataSource = $ds; } return $this->dataSource; }
/** * Returns sql query * @return string */ public final function __toString() { list($sorting, $limit, $offset) = $this->process(); $orderBy = array(); end($sorting); $end = key($sorting); foreach ($sorting as $i => $tmp) { list($key, $direction) = $tmp; $orderBy[] = '%by' . ($end === $i ? '' : ', '); $orderBy[] = array($key => $direction); } FindByHelper::dibiProcess($this, $this->getMapper(), $this->getConventional(), $this->where, $this->findBy, $this->tableAlias); $join = array(); foreach ($this->join as $tmp) { $join = array_merge($join, $tmp); } return $this->connectionTranslate(' SELECT [e.*] FROM %n', $this->tableName, ' as e %ex', $join, ' %ex', $this->where ? array('WHERE %and', $this->where) : NULL, ' ' . ($join ? 'GROUP BY [e.id]' : '') . ' %ex', $orderBy ? array('ORDER BY %sql', $orderBy) : NULL, ' %ofs %lmt', $offset, $limit); }