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 */
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;
}

View File

@@ -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
*/

View File

@@ -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()

View File

@@ -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)

View File

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

View File

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