Skip to content
Permalink
Browse files

Implemented result buffering inside ResultSet

  • Loading branch information...
lorenzo committed Dec 28, 2013
1 parent 35f33ff commit 28ae6c59d61ca23ce98a3277e32528656e8e33b2
@@ -429,7 +429,12 @@ public function getResults() {
if (isset($this->_results)) {
return $this->_results;
}
return $this->_decorateResults(new ResultSet($this, $this->_executeStatement()));
$this->_results = $this->_decorateResults(
new ResultSet($this, $this->_executeStatement())
);
return $this->_results;
}
/**
@@ -662,6 +667,8 @@ public function hydrate($enable = null) {
if ($enable === null) {
return $this->_hydrate;
}
$this->_dirty();
$this->_hydrate = (bool)$enable;
return $this;
}
@@ -920,4 +927,15 @@ public function find($finder, $options = []) {
return $this->repository()->callFinder($finder, $this, $options);
}
/**
* Marks a query as dirty, removing any preprocessed information
* from in memory caching such as previous results
*
* @return void
*/
public function _dirty() {
$this->_results = null;
parent::_dirty();
}
}
@@ -55,13 +55,6 @@ class ResultSet implements Countable, Iterator, Serializable, JsonSerializable {
*/
protected $_index = 0;
/**
* Points to the last record number that was fetched
*
* @var integer
*/
protected $_lastIndex = -1;
/**
* Last record fetched from the statement
*
@@ -153,7 +146,6 @@ public function key() {
*/
public function next() {
$this->_index++;
$this->_lastIndex = $this->_index;
}
/**
@@ -181,13 +173,22 @@ public function rewind() {
* @return boolean
*/
public function valid() {
if (isset($this->_results[$this->_index])) {
$this->_current = $this->_results[$this->_index];
return true;
}
$this->_current = $this->_fetchResult();
$valid = $this->_current !== false;
if (!$valid && $this->_statement) {
$this->_statement->closeCursor();
}
if ($valid) {
$this->_bufferResult($this->_current);
}
return $valid;
}
@@ -265,12 +266,10 @@ protected function _calculateAssociationMap() {
* @return mixed
*/
protected function _fetchResult() {
if (!empty($this->_results) && isset($this->_results[$this->_index])) {
return $this->_results[$this->_index];
}
if (!empty($this->_results)) {
if (!$this->_statement) {
return false;
}
$row = $this->_statement->fetch('assoc');
if ($row === false) {
return $row;
@@ -366,4 +365,8 @@ protected function _castValues($table, $values) {
return $values;
}
protected function _bufferResult($result) {
$this->_results[] = $result;
}
}
@@ -42,7 +42,7 @@ public function setUp() {
* @return void
*/
public function testRewind() {
return;
$this->markTestIncomplete();
$query = $this->table->find('all');
$results = $query->bufferResults()->execute();
$first = $second = [];
@@ -1111,7 +1111,7 @@ public function testFirstCleanQuery() {
$first = $query->hydrate(false)->first();
$this->assertEquals(['id' => 1], $first);
$this->assertNull($query->clause('limit'));
$this->assertEquals(1, $query->clause('limit'));
}
/**
@@ -146,8 +146,8 @@ public function testOne() {
$row = $results->one();
$this->assertEquals($this->fixtureData[0], $row);
$this->assertNull($results->one(), 'No more rows.');
$this->assertNull($results->one(), 'No more rows.');
$row = $results->one();
$this->assertEquals($this->fixtureData[0], $row);
}
/**

0 comments on commit 28ae6c5

Please sign in to comment.
You can’t perform that action at this time.