Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial implementation of a buffered statement class for those drivers
that do not implement rowCount() correctly
- Loading branch information
Showing
1 changed file
with
58 additions
and
0 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
lib/Cake/Model/Datasource/Database/Statement/BufferedStatement.php
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 |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
namespace Cake\Model\Datasource\Database\Statement; | ||
|
||
class BufferedStatement extends \Cake\Model\Datasource\Database\Statement { | ||
|
||
protected $_count = 0; | ||
|
||
protected $_records = array(); | ||
|
||
protected $_allFetched = true; | ||
|
||
protected $_counter = 0; | ||
|
||
public function execute($params = null) { | ||
$this->_count = $this->_counter = 0; | ||
$this->_records = array(); | ||
$this->_allFetched = false; | ||
return parent::execute($params); | ||
} | ||
|
||
public function fetch($type = 'num') { | ||
if ($this->_allFetched) { | ||
$row = ($this->_counter <= $this->_count) ? $this->_records[$this->_counter++] : false; | ||
$row = ($row && $type === 'num') ? array_values($row) : $row; | ||
return $row; | ||
} | ||
|
||
$this->_fetchType = $type; | ||
$record = parent::fetch($type); | ||
|
||
if ($record !== false) { | ||
$this->_count++; | ||
$this->_counter++; | ||
} else { | ||
$this->_allFetched = true; | ||
$this->_counter++; | ||
} | ||
|
||
return $record; | ||
} | ||
|
||
public function fetchAll($type = 'num') { | ||
$this->_records = parent::fetchAll($type); | ||
$this->_count = count($this->_records); | ||
$this->_allFetched = true; | ||
return $this->_records; | ||
} | ||
|
||
public function rowCount() { | ||
if (!$this->_allFetched) { | ||
$this->_records = $this->fetchAll('assoc'); | ||
} | ||
return $this->_count; | ||
} | ||
|
||
} | ||
|