phplibrary/src/Database/PDOStatement.php
2021-02-22 14:07:51 +03:00

101 lines
2.1 KiB
PHP

<?php
class Database_PDOStatement extends PDOStatement implements IteratorAggregate
{
protected $cursorPos = 0;
public $cache = array();
public $fields;
function getIterator(): Iterator {
return new Database_StatementIterator($this);
}
protected function __construct() {
}
function rewind() {
$this->cursorPos = 0;
}
public function seek($rownum) {
if ($rownum < 0) {
return false;
}
// PostgreSQL rows start w/ 0, but this works, because we are
// looking to move the position _before_ the next desired position
$this->cursorPos = $rownum;
return true;
}
function valid() {
return true;
}
public function first() {
if($this->cursorPos !== 0) { $this->seek(0); }
return $this->next();
}
function next() {
if ($this->getRecordCount() > $this->cursorPos) {
if (!isset($this->cache[$this->cursorPos])) {
$this->cache[$this->cursorPos] = $this->fetch(PDO::FETCH_ASSOC);
}
$this->fields = $this->cache[$this->cursorPos];
$this->cursorPos++;
return true;
} else {
$this->fields = null;
return false;
}
}
function key() {
return $this->cursorPos;
}
function current() {
return $this->cache[$this->cursorPos];
}
function getRow() {
return $this->fields;
}
function getInt($name) {
return (int)$this->fields[$name];
}
function getBlob($name) {
return $this->fields[$name];
}
function getString($name) {
return isset($this->fields[$name]) ? $this->fields[$name]: null;
}
function getBoolean($name) {
return (bool)$this->fields[$name];
}
function get($name) {
return $this->fields[$name];
}
function getArray($name) {
return Tools_String::strToArray($this->fields[$name]);
}
function getRecordCount() {
return count($this->cache);
}
function execute($args = null) {
$result = parent::execute($args);
return $result;
}
}