From f51152980cbfd598e43e2e3a0949533f852a2d73 Mon Sep 17 00:00:00 2001 From: "J. King" Date: Sun, 5 Nov 2017 22:28:19 -0500 Subject: [PATCH] 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 --- lib/Db/AbstractResult.php | 55 +++++++++++++++++++++++++++++++++++++++ lib/Db/SQLite3/Result.php | 40 ++-------------------------- 2 files changed, 57 insertions(+), 38 deletions(-) create mode 100644 lib/Db/AbstractResult.php 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"); - } - } }