diff --git a/lib/Db/AbstractResult.php b/lib/Db/AbstractResult.php new file mode 100644 index 00000000..dce29262 --- /dev/null +++ b/lib/Db/AbstractResult.php @@ -0,0 +1,55 @@ +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"); + } + } +} diff --git a/lib/Db/SQLite3/Result.php b/lib/Db/SQLite3/Result.php index df823903..9aed7a98 100644 --- a/lib/Db/SQLite3/Result.php +++ b/lib/Db/SQLite3/Result.php @@ -1,9 +1,10 @@ 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"); - } - } }