This repository has been archived by the owner on Jan 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Zend\Db\Resultset fix buffering #5308
Closed
turrsis
wants to merge
3
commits into
zendframework:develop
from
turrsis:hotfix/db-resultset-fix-buffering-and-refactor
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,6 @@ | |
namespace Zend\Db\ResultSet; | ||
|
||
use ArrayIterator; | ||
use ArrayObject; | ||
use Countable; | ||
use Iterator; | ||
use IteratorAggregate; | ||
|
@@ -48,6 +47,11 @@ abstract class AbstractResultSet implements Iterator, ResultSetInterface | |
*/ | ||
protected $position = 0; | ||
|
||
/** | ||
* @var mixed | ||
*/ | ||
protected $current = null; | ||
|
||
/** | ||
* Set the data source for the result set | ||
* | ||
|
@@ -62,6 +66,10 @@ public function initialize($dataSource) | |
$this->buffer = array(); | ||
} | ||
|
||
$this->count = null; | ||
$this->fieldCount = null; | ||
$this->current = null; | ||
|
||
if ($dataSource instanceof ResultInterface) { | ||
$this->count = $dataSource->count(); | ||
$this->fieldCount = $dataSource->getFieldCount(); | ||
|
@@ -73,9 +81,7 @@ public function initialize($dataSource) | |
$this->dataSource->rewind(); | ||
} | ||
return $this; | ||
} | ||
|
||
if (is_array($dataSource)) { | ||
} elseif (is_array($dataSource)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any reason for this to be turned into |
||
// its safe to get numbers from an array | ||
$first = current($dataSource); | ||
reset($dataSource); | ||
|
@@ -91,10 +97,26 @@ public function initialize($dataSource) | |
throw new Exception\InvalidArgumentException('DataSource provided is not an array, nor does it implement Iterator or IteratorAggregate'); | ||
} | ||
|
||
if ($this->count == null && $this->dataSource instanceof Countable) { | ||
if ($this->count === null && $this->dataSource instanceof Countable) { | ||
$this->count = $this->dataSource->count(); | ||
} | ||
|
||
if ($this->fieldCount === null) { | ||
if ($this->count == 0) { | ||
$this->fieldCount = 0; | ||
} else { | ||
$dataSource->rewind(); | ||
$row = $dataSource->current(); | ||
if ($row instanceof Countable) { | ||
$this->fieldCount = $row->count(); | ||
} else { | ||
$this->fieldCount = count((array)$row); | ||
} | ||
} | ||
} else { | ||
$this->fieldCount = 0; | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
|
@@ -113,10 +135,16 @@ public function buffer() | |
|
||
public function isBuffered() | ||
{ | ||
if ($this->buffer === -1 || is_array($this->buffer)) { | ||
return true; | ||
return ($this->buffer === -1 || is_array($this->buffer)); | ||
} | ||
|
||
protected function checkBuffered() | ||
{ | ||
if ($this->buffer === null) { | ||
// implicitly disable buffering from here on | ||
$this->buffer = -2; | ||
} | ||
return false; | ||
return $this; | ||
} | ||
|
||
/** | ||
|
@@ -136,29 +164,6 @@ public function getDataSource() | |
*/ | ||
public function getFieldCount() | ||
{ | ||
if (null !== $this->fieldCount) { | ||
return $this->fieldCount; | ||
} | ||
|
||
$dataSource = $this->getDataSource(); | ||
if (null === $dataSource) { | ||
return 0; | ||
} | ||
|
||
$dataSource->rewind(); | ||
if (!$dataSource->valid()) { | ||
$this->fieldCount = 0; | ||
return 0; | ||
} | ||
|
||
$row = $dataSource->current(); | ||
if (is_object($row) && $row instanceof Countable) { | ||
$this->fieldCount = $row->count(); | ||
return $this->fieldCount; | ||
} | ||
|
||
$row = (array) $row; | ||
$this->fieldCount = count($row); | ||
return $this->fieldCount; | ||
} | ||
|
||
|
@@ -169,11 +174,10 @@ public function getFieldCount() | |
*/ | ||
public function next() | ||
{ | ||
if ($this->buffer === null) { | ||
$this->buffer = -2; // implicitly disable buffering from here on | ||
} | ||
$this->checkBuffered(); | ||
$this->dataSource->next(); | ||
$this->position++; | ||
$this->current = null; | ||
} | ||
|
||
/** | ||
|
@@ -193,14 +197,36 @@ public function key() | |
*/ | ||
public function current() | ||
{ | ||
if ($this->buffer === null) { | ||
$this->buffer = -2; // implicitly disable buffering from here on | ||
} elseif (is_array($this->buffer) && isset($this->buffer[$this->position])) { | ||
return $this->buffer[$this->position]; | ||
if ($this->current !== null) { | ||
return $this->current; | ||
} | ||
$data = $this->dataSource->current(); | ||
$this->checkBuffered(); | ||
if (is_array($this->buffer)) { | ||
$this->buffer[$this->position] = $data; | ||
if (!isset($this->buffer[$this->position])) { | ||
return $this->current = $this->buffer[$this->position] = $this->hydrateCurrent(); | ||
} | ||
return $this->current = $this->buffer[$this->position]; | ||
} | ||
return $this->current = $this->hydrateCurrent(); | ||
} | ||
|
||
protected function hydrateCurrent() | ||
{ | ||
return $this->dataSource->current(); | ||
} | ||
|
||
protected function extract($data) | ||
{ | ||
if (is_array($data)) { | ||
return $data; | ||
} elseif (method_exists($data, 'toArray')) { | ||
return $data->toArray(); | ||
} elseif (method_exists($data, 'getArrayCopy')) { | ||
return $data->getArrayCopy(); | ||
} else { | ||
throw new Exception\RuntimeException( | ||
'Rows as part of this DataSource, with type ' . gettype($data) . ' cannot be cast to an array' | ||
); | ||
} | ||
return $data; | ||
} | ||
|
@@ -238,6 +264,7 @@ public function rewind() | |
} | ||
} | ||
$this->position = 0; | ||
$this->current = null; | ||
} | ||
|
||
/** | ||
|
@@ -264,17 +291,7 @@ public function toArray() | |
{ | ||
$return = array(); | ||
foreach ($this as $row) { | ||
if (is_array($row)) { | ||
$return[] = $row; | ||
} elseif (method_exists($row, 'toArray')) { | ||
$return[] = $row->toArray(); | ||
} elseif (method_exists($row, 'getArrayCopy')) { | ||
$return[] = $row->getArrayCopy(); | ||
} else { | ||
throw new Exception\RuntimeException( | ||
'Rows as part of this DataSource, with type ' . gettype($row) . ' cannot be cast to an array' | ||
); | ||
} | ||
$return[] = $this->extract($row); | ||
} | ||
return $return; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
= null
is not needed