mirror of
https://github.com/dg/dibi.git
synced 2025-08-05 05:37:39 +02:00
- improved DibiDataSource
This commit is contained in:
@@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@@ -85,7 +85,7 @@ class DibiDatabaseInfo extends DibiObject
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string
|
* @param string
|
||||||
* @return DibiTableInfo
|
* @return DibiTableInfo
|
||||||
*/
|
*/
|
||||||
public function getTable($name)
|
public function getTable($name)
|
||||||
@@ -103,7 +103,7 @@ class DibiDatabaseInfo extends DibiObject
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string
|
* @param string
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function hasTable($name)
|
public function hasTable($name)
|
||||||
@@ -220,7 +220,7 @@ class DibiTableInfo extends DibiObject
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string
|
* @param string
|
||||||
* @return DibiColumnInfo
|
* @return DibiColumnInfo
|
||||||
*/
|
*/
|
||||||
public function getColumn($name)
|
public function getColumn($name)
|
||||||
@@ -238,7 +238,7 @@ class DibiTableInfo extends DibiObject
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string
|
* @param string
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function hasColumn($name)
|
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
|
* @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
|
||||||
*/
|
*/
|
||||||
|
@@ -35,8 +35,8 @@ class DibiException extends Exception implements /*Nette\*/IDebuggable
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Construct an dibi exception.
|
* Construct an dibi exception.
|
||||||
* @param string Message describing the exception
|
* @param string Message describing the exception
|
||||||
* @param int Some code
|
* @param int Some code
|
||||||
* @param string SQL command
|
* @param string SQL command
|
||||||
*/
|
*/
|
||||||
public function __construct($message = NULL, $code = 0, $sql = NULL)
|
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
|
* @return void
|
||||||
*/
|
*/
|
||||||
public static function tryError()
|
public static function tryError()
|
||||||
|
@@ -429,9 +429,9 @@ class DibiResult extends DibiObject implements IDataSource
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Define column type.
|
* Define column type.
|
||||||
* @param string column
|
* @param string column
|
||||||
* @param string type (use constant Dibi::FIELD_*)
|
* @param string type (use constant Dibi::FIELD_*)
|
||||||
* @param string optional format
|
* @param string optional format
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
final public function setType($col, $type, $format = NULL)
|
final public function setType($col, $type, $format = NULL)
|
||||||
@@ -456,7 +456,7 @@ class DibiResult extends DibiObject implements IDataSource
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Define multiple columns types.
|
* Define multiple columns types.
|
||||||
* @param array
|
* @param array
|
||||||
* @return void
|
* @return void
|
||||||
* @internal
|
* @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)
|
* @return array ($type, $format)
|
||||||
*/
|
*/
|
||||||
final public function convert($value, $type, $format = NULL)
|
final public function convert($value, $type, $format = NULL)
|
||||||
|
@@ -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
|
||||||
|
@@ -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();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user