diff --git a/lib/Db/ResultSQLite3.php b/lib/Db/ResultSQLite3.php
index af7202f8..0f2d7ee1 100644
--- a/lib/Db/ResultSQLite3.php
+++ b/lib/Db/ResultSQLite3.php
@@ -9,7 +9,29 @@ class ResultSQLite3 implements Result {
protected $cur = null;
protected $rows = 0;
- public function __construct($result, $changes, $statement = null) {
+ // actual public methods
+
+ public function getSingle() {
+ $this->next();
+ if($this->valid()) {
+ $keys = array_keys($this->cur);
+ return $this->cur[array_shift($keys)];
+ }
+ return null;
+ }
+
+ public function get() {
+ $this->next();
+ return ($this->valid() ? $this->cur : null);
+ }
+
+ public function changes() {
+ return $this->rows;
+ }
+
+ // constructor/destructor
+
+ public function __construct($result, $changes = 0, $statement = null) {
$this->st = $statement; //keeps the statement from being destroyed, invalidating the result set
$this->set = $result;
$this->rows = $changes;
@@ -20,6 +42,8 @@ class ResultSQLite3 implements Result {
unset($this->set);
}
+ // PHP iterator methods
+
public function valid() {
$this->cur = $this->set->fetchArray(\SQLITE3_ASSOC);
return ($this->cur !== false);
@@ -43,22 +67,4 @@ class ResultSQLite3 implements Result {
$this->cur = null;
$this->set->reset();
}
-
- public function getSingle() {
- $this->next();
- if($this->valid()) {
- $keys = array_keys($this->cur);
- return $this->cur[array_shift($keys)];
- }
- return null;
- }
-
- public function get() {
- $this->next();
- return ($this->valid() ? $this->cur : null);
- }
-
- public function changes() {
- return $this->rows;
- }
}
\ No newline at end of file
diff --git a/tests/Db/SQLite3/TestDbResultSQLite3.php b/tests/Db/SQLite3/TestDbResultSQLite3.php
new file mode 100644
index 00000000..6ec8f917
--- /dev/null
+++ b/tests/Db/SQLite3/TestDbResultSQLite3.php
@@ -0,0 +1,76 @@
+enableExceptions(true);
+ $this->c = $c;
+ }
+
+ function tearDown() {
+ $this->c->close();
+ unset($this->c);
+ }
+
+ function testConstructResult() {
+ $set = $this->c->query("SELECT 1");
+ $this->assertInstanceOf(Db\ResultSQLite3::class, new Db\ResultSQLite3($set));
+ }
+
+ function testGetChangeCount() {
+ $this->c->query("CREATE TABLE test(col)");
+ $set = $this->c->query("INSERT INTO test(col) values(1)");
+ $rows = $this->c->changes();
+ $this->assertEquals($rows, (new Db\ResultSQLite3($set,$rows))->changes());
+ }
+
+ function testIterateOverResults() {
+ $set = $this->c->query("SELECT 1 as col union select 2 as col union select 3 as col");
+ $rows = [];
+ foreach(new Db\ResultSQLite3($set) as $row) {
+ $rows[] = $row['col'];
+ }
+ $this->assertEquals([1,2,3], $rows);
+ }
+
+ function testIterateOverResultsTwice() {
+ $set = $this->c->query("SELECT 1 as col union select 2 as col union select 3 as col");
+ $rows = [];
+ $test = new Db\ResultSQLite3($set);
+ foreach($test as $row) {
+ $rows[] = $row['col'];
+ }
+ foreach($test as $row) {
+ $rows[] = $row['col'];
+ }
+ $this->assertEquals([1,2,3,1,2,3], $rows);
+ }
+
+ function testGetSingleValues() {
+ $set = $this->c->query("SELECT 1867 as year union select 1970 as year union select 2112 as year");
+ $test = new Db\ResultSQLite3($set);
+ $this->assertEquals(1867, $test->getSingle());
+ $this->assertEquals(1970, $test->getSingle());
+ $this->assertEquals(2112, $test->getSingle());
+ $this->assertSame(null, $test->getSingle());
+ }
+
+ function testGetRows() {
+ $set = $this->c->query("SELECT '2112' as album, '2112' as track union select 'Clockwork Angels' as album, 'The Wreckers' as track");
+ $rows = [
+ ['album' => '2112', 'track' => '2112'],
+ ['album' => 'Clockwork Angels', 'track' => 'The Wreckers'],
+ ];
+ $test = new Db\ResultSQLite3($set);
+ $this->assertEquals($rows[0], $test->get());
+ $this->assertEquals($rows[1], $test->get());
+ $this->assertSame(null, $test->get());
+ }
+}
\ No newline at end of file
diff --git a/tests/phpunit.xml b/tests/phpunit.xml
index fd2effa9..57cad7cc 100644
--- a/tests/phpunit.xml
+++ b/tests/phpunit.xml
@@ -27,4 +27,8 @@
User/TestAuthorization.php
+
+ Db/SQLite3/TestDbResultSQLite3.php
+
+
\ No newline at end of file