Skip to content

Commit

Permalink
Initial implementation of a buffered statement class for those drivers
Browse files Browse the repository at this point in the history
that do not implement rowCount() correctly
  • Loading branch information
lorenzo committed Oct 14, 2012
1 parent f593b24 commit 49269b1
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions lib/Cake/Model/Datasource/Database/Statement/BufferedStatement.php
@@ -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;
}

}

0 comments on commit 49269b1

Please sign in to comment.