Commit 3505a5ed authored by Ian Jenkins's avatar Ian Jenkins

An example of a PDOIterator which can be rewound .

parent 0ebafdbb
......@@ -19,6 +19,7 @@
namespace Doctrine\DBAL;
use Doctrine\DBAL\Driver\PDOStatementIterator;
use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
use Doctrine\DBAL\Exception\InvalidArgumentException;
use PDO;
......@@ -1033,7 +1034,7 @@ class Connection implements DriverConnection
$logger->stopQuery();
}
return $statement;
return new PDOStatementIterator($statement);
}
/**
......
<?php
namespace Doctrine\DBAL\Driver;
class PDOStatementIterator implements \Iterator
{
public $stmt;
public $cache;
public $position = 0;
public function __construct(\PDOStatement $stmt)
{
$this->cache = [];
$this->position = 0;
$this->stmt = $stmt;
$this->next();
}
public function rewind()
{
$this->position = 0;
}
public function valid()
{
return isset($this->cache[$this->position]);
}
public function current()
{
return $this->cache[$this->position];
}
public function key()
{
return $this->position;
}
public function next()
{
if ($this->valid()) {
$this->cache[$this->position];
$this->position++;
} else {
$this->cache[$this->position] = $this->stmt->fetch();
$this->position++;
}
}
public function __call($name, $arguments)
{
return call_user_func_array([$this->stmt, $name], $arguments);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment