1
0
mirror of https://github.com/dg/dibi.git synced 2025-02-22 18:02:25 +01:00

- removed DibiResultIterator

- DibiConnection::nativeQuery & query() returns DibiResult or NULL
- added optional parameters $offset, $limit, $simplify to DibiResult::fetchAll()
This commit is contained in:
David Grudl 2008-05-20 08:15:30 +00:00
parent 4334eaa963
commit 69876a70b7
4 changed files with 34 additions and 154 deletions

View File

@ -31,18 +31,24 @@ if (version_compare(PHP_VERSION, '5.1.0', '<')) {
// nette libraries
if (!class_exists('NotImplementedException', FALSE)) { require_once dirname(__FILE__) . '/Nette/exceptions.php'; }
if (!class_exists(/*Nette::*/'Object', FALSE)) { require_once dirname(__FILE__) . '/Nette/Object.php'; }
if (!interface_exists(/*Nette::*/'IDebuggable', FALSE)) { require_once dirname(__FILE__) . '/Nette/IDebuggable.php'; }
if (!class_exists('NotImplementedException', FALSE)) {
require_once dirname(__FILE__) . '/Nette/exceptions.php';
}
if (!class_exists(/*Nette::*/'Object', FALSE)) {
require_once dirname(__FILE__) . '/Nette/Object.php';
}
if (!interface_exists(/*Nette::*/'IDebuggable', FALSE)) {
require_once dirname(__FILE__) . '/Nette/IDebuggable.php';
}
// dibi libraries
require_once dirname(__FILE__) . '/libs/interfaces.php';
require_once dirname(__FILE__) . '/libs/DibiException.php';
require_once dirname(__FILE__) . '/libs/DibiConnection.php';
require_once dirname(__FILE__) . '/libs/DibiResult.php';
require_once dirname(__FILE__) . '/libs/DibiResultIterator.php';
require_once dirname(__FILE__) . '/libs/DibiTranslator.php';
require_once dirname(__FILE__) . '/libs/DibiLogger.php';
require_once dirname(__FILE__) . '/libs/DibiVariable.php';
require_once dirname(__FILE__) . '/libs/DibiTable.php';
require_once dirname(__FILE__) . '/libs/DibiDataSource.php';
@ -251,8 +257,8 @@ class dibi
/**
* Generates and executes SQL query - Monostate for DibiConnection::query().
*
* @param array|mixed one or more arguments
* @return DibiResult Result set object (if any)
* @param array|mixed one or more arguments
* @return DibiResult|NULL result set object (if any)
* @throws DibiException
*/
public static function query($args)
@ -266,8 +272,8 @@ class dibi
/**
* Executes the SQL query - Monostate for DibiConnection::nativeQuery().
*
* @param string SQL statement.
* @return DibiResult Result set object (if any)
* @param string SQL statement.
* @return DibiResult|NULL result set object (if any)
*/
public static function nativeQuery($sql)
{
@ -557,6 +563,8 @@ class dibi
*/
public static function startLogger($file, $logQueries = FALSE)
{
require_once dirname(__FILE__) . '/libs/DibiLogger.php';
$logger = new DibiLogger($file);
$logger->logQueries = $logQueries;
self::addHandler(array($logger, 'handler'));

View File

@ -226,8 +226,8 @@ class DibiConnection extends /*Nette::*/Object
/**
* Generates (translates) and executes SQL query.
*
* @param array|mixed one or more arguments
* @return DibiResult Result set object (if any)
* @param array|mixed one or more arguments
* @return DibiResult|NULL result set object (if any)
* @throws DibiException
*/
final public function query($args)
@ -265,8 +265,8 @@ class DibiConnection extends /*Nette::*/Object
/**
* Executes the SQL query.
*
* @param string SQL statement.
* @return DibiResult Result set object (if any)
* @param string SQL statement.
* @return DibiResult|NULL result set object (if any)
* @throws DibiException
*/
final public function nativeQuery($sql)
@ -279,7 +279,7 @@ class DibiConnection extends /*Nette::*/Object
$time = -microtime(TRUE);
dibi::notify($this, 'beforeQuery', $sql);
$res = $this->driver->query($sql) ? new DibiResult(clone $this->driver, $this->config) : TRUE; // backward compatibility - will be changed to NULL
$res = $this->driver->query($sql) ? new DibiResult(clone $this->driver, $this->config) : NULL;
$time += microtime(TRUE);
dibi::$elapsedTime = $time;

View File

@ -320,24 +320,32 @@ class DibiResult extends /*Nette::*/Object implements IDataSource
/**
* Fetches all records from table.
*
* @param int offset
* @param int limit
* @param bool simplify one-column result set?
* @return array
*/
final function fetchAll()
final function fetchAll($offset = NULL, $limit = NULL, $simplify = TRUE)
{
$this->seek(0);
$limit = $limit === NULL ? -1 : (int) $limit;
$this->seek((int) $offset);
$row = $this->fetch();
if (!$row) return array(); // empty resultset
$data = array();
if (!$this->objects && count($row) === 1) {
if ($simplify && !$this->objects && count($row) === 1) {
// special case: one-column result set
$key = key($row);
do {
if ($limit === 0) break;
$limit--;
$data[] = $row[$key];
} while ($row = $this->fetch());
} else {
do {
if ($limit === 0) break;
$limit--;
$data[] = $row;
} while ($row = $this->fetch());
}
@ -591,7 +599,7 @@ class DibiResult extends /*Nette::*/Object implements IDataSource
*/
final public function getIterator($offset = NULL, $limit = NULL)
{
return new DibiResultIterator($this, $offset, $limit);
return new ArrayIterator($this->fetchAll($offset, $limit, FALSE));
}

View File

@ -1,136 +0,0 @@
<?php
/**
* dibi - tiny'n'smart database abstraction layer
* ----------------------------------------------
*
* Copyright (c) 2005, 2008 David Grudl (http://davidgrudl.com)
*
* 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
*/
/**
* External result set iterator.
*
* This can be returned by DibiResult::getIterator() method or using foreach
* <code>
* $result = dibi::query('SELECT * FROM table');
* foreach ($result as $row) {
* print_r($row);
* }
* unset($result);
* </code>
*
* Optionally you can specify offset and limit:
* <code>
* foreach ($result->getIterator(2, 3) as $row) {
* print_r($row);
* }
* </code>
*
* @author David Grudl
* @copyright Copyright (c) 2005, 2008 David Grudl
* @package dibi
* @version $Revision$ $Date$
*/
final class DibiResultIterator implements Iterator
{
/** @var DibiResult */
private $result;
/** @var int */
private $offset;
/** @var int */
private $limit;
/** @var int */
private $row;
/** @var int */
private $pointer;
/**
* Required by the Iterator interface.
* @param int offset
* @param int limit
*/
public function __construct(DibiResult $result, $offset, $limit)
{
$this->result = $result;
$this->offset = (int) $offset;
$this->limit = $limit === NULL ? -1 : (int) $limit;
}
/**
* Rewinds the Iterator to the first element.
* @return void
*/
public function rewind()
{
$this->pointer = 0;
$this->result->seek($this->offset);
$this->row = $this->result->fetch();
}
/**
* Returns the key of the current element.
* @return mixed
*/
public function key()
{
return $this->pointer;
}
/**
* Returns the current element.
* @return mixed
*/
public function current()
{
return $this->row;
}
/**
* Moves forward to next element.
* @return void
*/
public function next()
{
//$this->result->seek($this->offset + $this->pointer + 1);
$this->row = $this->result->fetch();
$this->pointer++;
}
/**
* Checks if there is a current element after calls to rewind() or next().
* @return bool
*/
public function valid()
{
return !empty($this->row) && ($this->limit < 0 || $this->pointer < $this->limit);
}
}