diff --git a/dibi/libs/DibiDataSource.php b/dibi/libs/DibiDataSource.php index 4b823504..f2a16ad2 100644 --- a/dibi/libs/DibiDataSource.php +++ b/dibi/libs/DibiDataSource.php @@ -77,103 +77,6 @@ class DibiDataSource extends DibiObject implements IDataSource - /** - * @return DibiResultIterator - */ - public function getIterator() - { - return $this->getResult()->getIterator(); - } - - - - /** - * Returns the number of rows in a given data source. - * @return int - */ - public function count() - { - if ($this->count === NULL) { - $this->count = $this->conds || $this->offset || $this->limit - ? (int) $this->connection->nativeQuery( - 'SELECT COUNT(*) FROM (' . $this->__toString() . ') AS t' - )->fetchSingle() - : $this->getTotalCount(); - } - return $this->count; - } - - - - /** - * Returns the number of rows in a given data source. - * @return int - */ - public function getTotalCount() - { - if ($this->totalCount === NULL) { - $this->totalCount = (int) $this->connection->nativeQuery( - 'SELECT COUNT(*) FROM ' . $this->sql - )->fetchSingle(); - } - return $this->totalCount; - } - - - - /** - * Returns (and queries) DibiResult. - * @return DibiResult - */ - public function getResult() - { - if ($this->result === NULL) { - $this->result = $this->connection->nativeQuery($this->__toString()); - } - return $this->result; - } - - - - /** - * Returns this data source wrapped in DibiFluent object. - * @return DibiFluent - */ - public function toFluent() - { - return $this->connection->select('*')->from('(%SQL) AS t', $this->__toString()); - } - - - - /** - * Returns this data source wrapped in DibiDataSource object. - * @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', $this->sql, ' - WHERE %and', $this->conds, ' - ORDER BY %by', $this->sorting, ' - %ofs %lmt', $this->offset, $this->limit - ); - } - - - /** * Selects columns to query. * @param string|array column name or array of column names @@ -256,4 +159,171 @@ class DibiDataSource extends DibiObject implements IDataSource return $this->connection; } + + + /********************* executing ****************d*g**/ + + + + /** + * Returns (and queries) DibiResult. + * @return DibiResult + */ + public function getResult() + { + if ($this->result === NULL) { + $this->result = $this->connection->nativeQuery($this->__toString()); + } + return $this->result; + } + + + + /** + * @return DibiResultIterator + */ + public function getIterator() + { + return $this->getResult()->getIterator(); + } + + + + /** + * Generates, executes SQL query and fetches the single row. + * @return DibiRow|FALSE array on success, FALSE if no next record + */ + public function fetch() + { + return $this->getResult()->fetch(); + } + + + + /** + * Like fetch(), but returns only first field. + * @return mixed value on success, FALSE if no next record + */ + public function fetchSingle() + { + return $this->getResult()->fetchSingle(); + } + + + + /** + * Fetches all records from table. + * @return array + */ + public function fetchAll() + { + return $this->getResult()->fetchAll(); + } + + + + /** + * Fetches all records from table and returns associative tree. + * @param string associative descriptor + * @return array + */ + public function fetchAssoc($assoc) + { + return $this->getResult()->fetchAssoc($assoc); + } + + + + /** + * Fetches all records from table like $key => $value pairs. + * @param string associative key + * @param string value + * @return array + */ + public function fetchPairs($key = NULL, $value = NULL) + { + return $this->getResult()->fetchPairs($key, $value); + } + + + + /********************* exporting ****************d*g**/ + + + + /** + * Returns this data source wrapped in DibiFluent object. + * @return DibiFluent + */ + public function toFluent() + { + return $this->connection->select('*')->from('(%SQL) AS t', $this->__toString()); + } + + + + /** + * Returns this data source wrapped in DibiDataSource object. + * @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', $this->sql, ' + WHERE %and', $this->conds, ' + ORDER BY %by', $this->sorting, ' + %ofs %lmt', $this->offset, $this->limit + ); + } + + + + /********************* counting ****************d*g**/ + + + + /** + * Returns the number of rows in a given data source. + * @return int + */ + public function count() + { + if ($this->count === NULL) { + $this->count = $this->conds || $this->offset || $this->limit + ? (int) $this->connection->nativeQuery( + 'SELECT COUNT(*) FROM (' . $this->__toString() . ') AS t' + )->fetchSingle() + : $this->getTotalCount(); + } + return $this->count; + } + + + + /** + * Returns the number of rows in a given data source. + * @return int + */ + public function getTotalCount() + { + if ($this->totalCount === NULL) { + $this->totalCount = (int) $this->connection->nativeQuery( + 'SELECT COUNT(*) FROM ' . $this->sql + )->fetchSingle(); + } + return $this->totalCount; + } + } diff --git a/dibi/libs/DibiFluent.php b/dibi/libs/DibiFluent.php index 2f28a16e..ff72940e 100644 --- a/dibi/libs/DibiFluent.php +++ b/dibi/libs/DibiFluent.php @@ -238,6 +238,21 @@ class DibiFluent extends DibiObject implements IDataSource + /** + * Returns the dibi connection. + * @return DibiConnection + */ + final public function getConnection() + { + return $this->connection; + } + + + + /********************* executing ****************d*g**/ + + + /** * Generates and executes SQL query. * @param mixed what to return? @@ -255,7 +270,6 @@ class DibiFluent extends DibiObject implements IDataSource /** * Generates, executes SQL query and fetches the single row. * @return DibiRow|FALSE array on success, FALSE if no next record - * @throws DibiException */ public function fetch() { @@ -298,11 +312,8 @@ class DibiFluent extends DibiObject implements IDataSource /** * Fetches all records from table and returns associative tree. - * Associative descriptor: assoc1,#,assoc2,=,assoc3,@ - * builds a tree: $data[assoc1][index][assoc2]['assoc3']->value = {record} * @param string associative descriptor * @return array - * @throws InvalidArgumentException */ public function fetchAssoc($assoc) { @@ -316,7 +327,6 @@ class DibiFluent extends DibiObject implements IDataSource * @param string associative key * @param string value * @return array - * @throws InvalidArgumentException */ public function fetchPairs($key = NULL, $value = NULL) { @@ -338,6 +348,18 @@ class DibiFluent extends DibiObject implements IDataSource + /** + * Generates and prints SQL query or it's part. + * @param string clause name + * @return bool + */ + public function test($clause = NULL) + { + return $this->connection->test($this->_export($clause)); + } + + + /** * @return int */ @@ -350,14 +372,27 @@ class DibiFluent extends DibiObject implements IDataSource + /********************* exporting ****************d*g**/ + + + /** - * Generates and prints SQL query or it's part. - * @param string clause name - * @return bool + * @return DibiDataSource */ - public function test($clause = NULL) + public function toDataSource() { - return $this->connection->test($this->_export($clause)); + return new DibiDataSource($this->connection->sql($this->_export()), $this->connection); + } + + + + /** + * Returns SQL query. + * @return string + */ + final public function __toString() + { + return $this->connection->sql($this->_export()); } @@ -411,38 +446,6 @@ class DibiFluent extends DibiObject implements IDataSource } - - - /** - * @return DibiDataSource - */ - public function toDataSource() - { - return new DibiDataSource($this->connection->sql($this->_export()), $this->connection); - } - - - - /** - * Returns SQL query. - * @return string - */ - final public function __toString() - { - return $this->connection->sql($this->_export()); - } - - - - /** - * Returns the dibi connection. - * @return DibiConnection - */ - final public function getConnection() - { - return $this->connection; - } - }