1
0
mirror of https://github.com/dg/dibi.git synced 2025-08-03 20:57:36 +02:00

- added DibiDataSource as default implementation of IDataSource

- new modifiers %lmt %ofs
- removed old modifier %p (alias for %sql)
This commit is contained in:
David Grudl
2008-01-18 02:57:43 +00:00
parent c41167d49f
commit f6b781f12d
9 changed files with 204 additions and 18 deletions

View File

@@ -0,0 +1,93 @@
<?php
/**
* dibi - tiny'n'smart database abstraction layer
* ----------------------------------------------
*
* Copyright (c) 2005, 2008 David Grudl aka -dgx- (http://www.dgx.cz)
*
* This source file is subject to the "dibi license" that is bundled
* with this package in the file license.txt.
*
* For more information please see http://dibiphp.com/
*
* @copyright Copyright (c) 2005, 2008 David Grudl
* @license http://dibiphp.com/license dibi license
* @link http://dibiphp.com/
* @package dibi
*/
/**
* Default implementation of IDataSource for dibi
*
* @author David Grudl
* @copyright Copyright (c) 2005, 2008 David Grudl
* @package dibi
* @version $Revision$ $Date$
*/
class DibiDataSource extends NObject implements IDataSource
{
/** @var DibiConnection */
private $connection;
/** @var string */
private $sql;
/** @var int */
private $count;
/**
* @param string SQL command or table name, as data source
* @param DibiConnection connection
*/
public function __construct($sql, DibiConnection $connection = NULL)
{
if (strpos($sql, ' ') === FALSE) {
// table name
$this->sql = $sql;
} else {
// SQL command
$this->sql = '(' . $sql . ') AS [source]';
}
$this->connection = $connection === NULL ? dibi::getConnection() : $connection;
}
/**
* @param int offset
* @param int limit
* @param array columns
* @return ArrayIterator
*/
public function getIterator($offset = NULL, $limit = NULL, $cols = NULL)
{
return $this->connection->query('
SELECT %n', ($cols === NULL ? '*' : $cols), '
FROM %sql', $this->sql, '
%ofs %lmt', $offset, $limit
);
}
/**
* @return int
*/
public function count()
{
if ($this->count === NULL) {
$this->count = $this->connection->query('
SELECT COUNT(*)
FROM %sql', $this->sql
)->fetchSingle();
}
return $this->count;
}
}