mirror of
https://code.mensbeam.com/MensBeam/Arsse.git
synced 2024-12-22 05:02:40 +00:00
Introduce abstract class for result sets
Most of the SQLite3 Result class' methods are fully generic, so it should be helpful for future result sets
This commit is contained in:
parent
9304f99032
commit
f51152980c
2 changed files with 57 additions and 38 deletions
55
lib/Db/AbstractResult.php
Normal file
55
lib/Db/AbstractResult.php
Normal file
|
@ -0,0 +1,55 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
namespace JKingWeb\Arsse\Db;
|
||||
|
||||
abstract class AbstractResult implements Result {
|
||||
protected $pos = 0;
|
||||
protected $cur = null;
|
||||
|
||||
// actual public methods
|
||||
|
||||
public function getValue() {
|
||||
$this->next();
|
||||
if ($this->valid()) {
|
||||
$keys = array_keys($this->cur);
|
||||
return $this->cur[array_shift($keys)];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public function getRow() {
|
||||
$this->next();
|
||||
return ($this->valid() ? $this->cur : null);
|
||||
}
|
||||
|
||||
public function getAll(): array {
|
||||
return iterator_to_array($this, false);
|
||||
}
|
||||
|
||||
abstract public function changes();
|
||||
|
||||
abstract public function lastId();
|
||||
|
||||
// PHP iterator methods
|
||||
|
||||
abstract public function valid();
|
||||
|
||||
public function next() {
|
||||
$this->cur = null;
|
||||
$this->pos += 1;
|
||||
}
|
||||
|
||||
public function current() {
|
||||
return $this->cur;
|
||||
}
|
||||
|
||||
public function key() {
|
||||
return $this->pos;
|
||||
}
|
||||
|
||||
public function rewind() {
|
||||
if ($this->pos) {
|
||||
throw new Exception("resultReused");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,9 +1,10 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
namespace JKingWeb\Arsse\Db\SQLite3;
|
||||
|
||||
use JKingWeb\Arsse\Db\Exception;
|
||||
|
||||
class Result implements \JKingWeb\Arsse\Db\Result {
|
||||
class Result extends \JKingWeb\Arsse\Db\AbstractResult {
|
||||
protected $st;
|
||||
protected $set;
|
||||
protected $pos = 0;
|
||||
|
@ -13,24 +14,6 @@ class Result implements \JKingWeb\Arsse\Db\Result {
|
|||
|
||||
// actual public methods
|
||||
|
||||
public function getValue() {
|
||||
$this->next();
|
||||
if ($this->valid()) {
|
||||
$keys = array_keys($this->cur);
|
||||
return $this->cur[array_shift($keys)];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public function getRow() {
|
||||
$this->next();
|
||||
return ($this->valid() ? $this->cur : null);
|
||||
}
|
||||
|
||||
public function getAll(): array {
|
||||
return iterator_to_array($this, false);
|
||||
}
|
||||
|
||||
public function changes() {
|
||||
return $this->rows;
|
||||
}
|
||||
|
@ -62,23 +45,4 @@ class Result implements \JKingWeb\Arsse\Db\Result {
|
|||
$this->cur = $this->set->fetchArray(\SQLITE3_ASSOC);
|
||||
return ($this->cur !== false);
|
||||
}
|
||||
|
||||
public function next() {
|
||||
$this->cur = null;
|
||||
$this->pos += 1;
|
||||
}
|
||||
|
||||
public function current() {
|
||||
return $this->cur;
|
||||
}
|
||||
|
||||
public function key() {
|
||||
return $this->pos;
|
||||
}
|
||||
|
||||
public function rewind() {
|
||||
if ($this->pos) {
|
||||
throw new Exception("resultReused");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue