1
0
mirror of https://github.com/dg/dibi.git synced 2025-08-05 05:37:39 +02:00

- improved DibiDataSource

This commit is contained in:
David Grudl
2009-02-05 23:13:29 +00:00
parent dd6ffc3d0e
commit 60662bdae1
6 changed files with 87 additions and 36 deletions

View File

@@ -35,6 +35,9 @@ class DibiDataSource extends DibiObject implements IDataSource
/** @var string */ /** @var string */
private $sql; private $sql;
/** @var DibiResult */
private $result;
/** @var int */ /** @var int */
private $count; private $count;
@@ -47,47 +50,49 @@ class DibiDataSource extends DibiObject implements IDataSource
/** @var array */ /** @var array */
private $conds = array(); private $conds = array();
/** @var int */
private $offset;
/** @var int */
private $limit;
/** /**
* @param string SQL command or table name, as data source * @param string SQL command or table name, as data source
* @param DibiConnection connection * @param DibiConnection connection
*/ */
public function __construct($sql, DibiConnection $connection = NULL) public function __construct($sql, DibiConnection $connection)
{ {
$this->sql = $sql; if (strpos($sql, ' ') === FALSE) {
$this->connection = $connection === NULL ? dibi::getConnection() : $connection; $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 * @return DibiResultIterator
*/ */
public function getIterator($offset = NULL, $limit = NULL) public function getIterator()
{ {
return $this->connection->query(' return $this->getResult()->getIterator();
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();
} }
/** /**
* Returns the number of rows in a given data source.
* @return int * @return int
*/ */
public function count() public function count()
{ {
if ($this->count === NULL) { if ($this->count === NULL) {
$this->count = $this->connection->nativeQuery( $this->count = (int) $this->connection->nativeQuery(
'SELECT COUNT(*) FROM (' . $this->sql . ') AS t' 'SELECT COUNT(*) FROM ' . $this->sql
)->fetchSingle(); )->fetchSingle();
} }
return $this->count; 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 * @return DibiFluent
* @throws DibiException
*/ */
public function toFluent() public function toFluent()
{ {
@@ -109,6 +126,7 @@ class DibiDataSource extends DibiObject implements IDataSource
/** /**
* Returns this data source wrapped in DibiDataSource object.
* @return DibiDataSource * @return DibiDataSource
*/ */
public function toDataSource() public function toDataSource()
@@ -126,15 +144,20 @@ class DibiDataSource extends DibiObject implements IDataSource
{ {
return $this->connection->sql(' return $this->connection->sql('
SELECT %n', (empty($this->cols) ? '*' : $this->cols), ' SELECT %n', (empty($this->cols) ? '*' : $this->cols), '
FROM (%SQL) AS [t]', $this->sql, ' FROM %SQL', $this->sql, '
WHERE %and', $this->conds, ' WHERE %and', $this->conds, '
ORDER BY %by', $this->sorting, ' 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) public function select($col, $as = NULL)
{ {
@@ -143,11 +166,16 @@ class DibiDataSource extends DibiObject implements IDataSource
} else { } else {
$this->cols[$col] = $as; $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) public function where($cond)
{ {
@@ -157,11 +185,17 @@ class DibiDataSource extends DibiObject implements IDataSource
} else { } else {
$this->conds[] = func_get_args(); $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') public function orderBy($row, $sorting = 'ASC')
{ {
@@ -170,6 +204,24 @@ class DibiDataSource extends DibiObject implements IDataSource
} else { } else {
$this->sorting[$row] = $sorting; $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;
} }

View File

@@ -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 * @author David Grudl
* @copyright Copyright (c) 2005, 2009 David Grudl * @copyright Copyright (c) 2005, 2009 David Grudl
@@ -600,7 +600,6 @@ class DibiIndexInfo extends DibiObject
/** /**
* @return bool * @return bool
*/ */

View File

@@ -112,7 +112,7 @@ class DibiDriverException extends DibiException
/** /**
* Starts catching potential errors/warnings * Starts catching potential errors/warnings.
* @return void * @return void
*/ */
public static function tryError() public static function tryError()

View File

@@ -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) * @return array ($type, $format)
*/ */
final public function convert($value, $type, $format = NULL) final public function convert($value, $type, $format = NULL)

View File

@@ -21,7 +21,7 @@
/** /**
* dibi result-set row * Result-set single row.
* *
* @author David Grudl * @author David Grudl
* @copyright Copyright (c) 2005, 2009 David Grudl * @copyright Copyright (c) 2005, 2009 David Grudl

View File

@@ -45,7 +45,7 @@ interface IDibiVariable
*/ */
interface IDataSource extends Countable, IteratorAggregate interface IDataSource extends Countable, IteratorAggregate
{ {
//function IteratorAggregate::getIterator($offset = NULL, $limit = NULL); //function IteratorAggregate::getIterator();
//function Countable::count(); //function Countable::count();
} }