From 60662bdae18a5ef86e12376d9a1e9fc91c64cc67 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Thu, 5 Feb 2009 23:13:29 +0000 Subject: [PATCH] - improved DibiDataSource --- dibi/libs/DibiDataSource.php | 92 ++++++++++++++++++++++++++-------- dibi/libs/DibiDatabaseInfo.php | 11 ++-- dibi/libs/DibiException.php | 6 +-- dibi/libs/DibiResult.php | 10 ++-- dibi/libs/DibiRow.php | 2 +- dibi/libs/interfaces.php | 2 +- 6 files changed, 87 insertions(+), 36 deletions(-) diff --git a/dibi/libs/DibiDataSource.php b/dibi/libs/DibiDataSource.php index 7390177d..7d2d2387 100644 --- a/dibi/libs/DibiDataSource.php +++ b/dibi/libs/DibiDataSource.php @@ -35,6 +35,9 @@ class DibiDataSource extends DibiObject implements IDataSource /** @var string */ private $sql; + /** @var DibiResult */ + private $result; + /** @var int */ private $count; @@ -47,47 +50,49 @@ class DibiDataSource extends DibiObject implements IDataSource /** @var array */ private $conds = array(); + /** @var int */ + private $offset; + + /** @var int */ + private $limit; + /** * @param string SQL command or table name, as data source * @param DibiConnection connection */ - public function __construct($sql, DibiConnection $connection = NULL) + public function __construct($sql, DibiConnection $connection) { - $this->sql = $sql; - $this->connection = $connection === NULL ? dibi::getConnection() : $connection; + if (strpos($sql, ' ') === FALSE) { + $this->sql = $sql; // table name + } else { + $this->sql = '(' . $sql . ') AS t'; // SQL command + } + $this->connection = $connection; } /** - * @param int offset - * @param int limit - * @param array columns * @return DibiResultIterator */ - public function getIterator($offset = NULL, $limit = NULL) + public function getIterator() { - 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 $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->connection->nativeQuery( - 'SELECT COUNT(*) FROM (' . $this->sql . ') AS t' + $this->count = (int) $this->connection->nativeQuery( + 'SELECT COUNT(*) FROM ' . $this->sql )->fetchSingle(); } return $this->count; @@ -95,11 +100,23 @@ class DibiDataSource extends DibiObject implements IDataSource + /** + * Returns (and queries) DibiResult. + * @return DibiResult + */ + public function getResult() + { + if ($this->result === NULL) { + $this->result = $this->connection->nativeQuery($this->__toString()); + } + return $this->result; + } + + /** - * Returns SQL wrapped as DibiFluent. + * Returns this data source wrapped in DibiFluent object. * @return DibiFluent - * @throws DibiException */ public function toFluent() { @@ -109,6 +126,7 @@ class DibiDataSource extends DibiObject implements IDataSource /** + * Returns this data source wrapped in DibiDataSource object. * @return DibiDataSource */ public function toDataSource() @@ -126,15 +144,20 @@ class DibiDataSource extends DibiObject implements IDataSource { return $this->connection->sql(' SELECT %n', (empty($this->cols) ? '*' : $this->cols), ' - FROM (%SQL) AS [t]', $this->sql, ' + 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 + * @param string column alias + * @return DibiDataSource provides a fluent interface */ public function select($col, $as = NULL) { @@ -143,11 +166,16 @@ class DibiDataSource extends DibiObject implements IDataSource } else { $this->cols[$col] = $as; } + $this->result = NULL; + return $this; } /** + * Adds conditions to query. + * @param mixed conditions + * @return DibiDataSource provides a fluent interface */ public function where($cond) { @@ -157,11 +185,17 @@ class DibiDataSource extends DibiObject implements IDataSource } else { $this->conds[] = func_get_args(); } + $this->result = NULL; + return $this; } /** + * Selects columns to order by. + * @param string|array column name or array of column names + * @param string sorting direction + * @return DibiDataSource provides a fluent interface */ public function orderBy($row, $sorting = 'ASC') { @@ -170,6 +204,24 @@ class DibiDataSource extends DibiObject implements IDataSource } else { $this->sorting[$row] = $sorting; } + $this->result = NULL; + return $this; + } + + + + /** + * Limits number of rows. + * @param int limit + * @param int offset + * @return DibiDataSource provides a fluent interface + */ + public function applyLimit($limit, $offset = NULL) + { + $this->limit = $limit; + $this->offset = $offset; + $this->result = NULL; + return $this; } diff --git a/dibi/libs/DibiDatabaseInfo.php b/dibi/libs/DibiDatabaseInfo.php index 13596e35..c1d85e64 100644 --- a/dibi/libs/DibiDatabaseInfo.php +++ b/dibi/libs/DibiDatabaseInfo.php @@ -85,7 +85,7 @@ class DibiDatabaseInfo extends DibiObject /** - * @param string + * @param string * @return DibiTableInfo */ public function getTable($name) @@ -103,7 +103,7 @@ class DibiDatabaseInfo extends DibiObject /** - * @param string + * @param string * @return bool */ public function hasTable($name) @@ -220,7 +220,7 @@ class DibiTableInfo extends DibiObject /** - * @param string + * @param string * @return DibiColumnInfo */ public function getColumn($name) @@ -238,7 +238,7 @@ class DibiTableInfo extends DibiObject /** - * @param string + * @param string * @return bool */ public function hasColumn($name) @@ -551,7 +551,7 @@ class DibiForeignKeyInfo extends DibiObject /** - * Reflection metadata class for a index or primary key + * Reflection metadata class for a index or primary key. * * @author David Grudl * @copyright Copyright (c) 2005, 2009 David Grudl @@ -600,7 +600,6 @@ class DibiIndexInfo extends DibiObject - /** * @return bool */ diff --git a/dibi/libs/DibiException.php b/dibi/libs/DibiException.php index 27ae3848..5d48b84a 100644 --- a/dibi/libs/DibiException.php +++ b/dibi/libs/DibiException.php @@ -35,8 +35,8 @@ class DibiException extends Exception implements /*Nette\*/IDebuggable /** * Construct an dibi exception. - * @param string Message describing the exception - * @param int Some code + * @param string Message describing the exception + * @param int Some code * @param string SQL command */ public function __construct($message = NULL, $code = 0, $sql = NULL) @@ -112,7 +112,7 @@ class DibiDriverException extends DibiException /** - * Starts catching potential errors/warnings + * Starts catching potential errors/warnings. * @return void */ public static function tryError() diff --git a/dibi/libs/DibiResult.php b/dibi/libs/DibiResult.php index 6d2e1430..86f24fec 100644 --- a/dibi/libs/DibiResult.php +++ b/dibi/libs/DibiResult.php @@ -429,9 +429,9 @@ class DibiResult extends DibiObject implements IDataSource /** * Define column type. - * @param string column - * @param string type (use constant Dibi::FIELD_*) - * @param string optional format + * @param string column + * @param string type (use constant Dibi::FIELD_*) + * @param string optional format * @return void */ final public function setType($col, $type, $format = NULL) @@ -456,7 +456,7 @@ class DibiResult extends DibiObject implements IDataSource /** * Define multiple columns types. - * @param array + * @param array * @return void * @internal */ @@ -479,7 +479,7 @@ class DibiResult extends DibiObject implements IDataSource /** - * Converts value to specified type and format + * Converts value to specified type and format. * @return array ($type, $format) */ final public function convert($value, $type, $format = NULL) diff --git a/dibi/libs/DibiRow.php b/dibi/libs/DibiRow.php index c0903cde..fcdd5a8a 100644 --- a/dibi/libs/DibiRow.php +++ b/dibi/libs/DibiRow.php @@ -21,7 +21,7 @@ /** - * dibi result-set row + * Result-set single row. * * @author David Grudl * @copyright Copyright (c) 2005, 2009 David Grudl diff --git a/dibi/libs/interfaces.php b/dibi/libs/interfaces.php index 03155c48..ab57163a 100644 --- a/dibi/libs/interfaces.php +++ b/dibi/libs/interfaces.php @@ -45,7 +45,7 @@ interface IDibiVariable */ interface IDataSource extends Countable, IteratorAggregate { - //function IteratorAggregate::getIterator($offset = NULL, $limit = NULL); + //function IteratorAggregate::getIterator(); //function Countable::count(); }