Browse Source

Impelemented Result->getAll()

- Fixes #45
- Renamed getSingle to getValue to avoid possible confusion
- Added test to ensure getValue() always returns the first datum of each row rather than going column-to-column
microsub
J. King 7 years ago
parent
commit
37dad63dee
  1. 14
      lib/Database.php
  2. 3
      lib/Db/Result.php
  3. 11
      lib/Db/ResultSQLite3.php
  4. 18
      tests/Db/SQLite3/TestDbResultSQLite3.php

14
lib/Database.php

@ -173,7 +173,7 @@ class Database {
public function userExists(string $user): bool {
if(!$this->data->user->authorize($user, __FUNCTION__)) throw new User\ExceptionAuthz("notAuthorized", ["action" => __FUNCTION__, "user" => $user]);
return (bool) $this->db->prepare("SELECT count(*) from newssync_users where id is ?", "str")->run($user)->getSingle();
return (bool) $this->db->prepare("SELECT count(*) from newssync_users where id is ?", "str")->run($user)->getValue();
}
public function userAdd(string $user, string $password = null): string {
@ -212,7 +212,7 @@ class Database {
public function userPasswordGet(string $user): string {
if(!$this->data->user->authorize($user, __FUNCTION__)) throw new User\ExceptionAuthz("notAuthorized", ["action" => __FUNCTION__, "user" => $user]);
if(!$this->userExists($user)) throw new User\Exception("doesNotExist", ["action" => __FUNCTION__, "user" => $user]);
return (string) $this->db->prepare("SELECT password from newssync_users where id is ?", "str")->run($user)->getSingle();
return (string) $this->db->prepare("SELECT password from newssync_users where id is ?", "str")->run($user)->getValue();
}
public function userPasswordSet(string $user, string $password = null): string {
@ -249,7 +249,7 @@ class Database {
public function userRightsGet(string $user): int {
if(!$this->data->user->authorize($user, __FUNCTION__)) throw new User\ExceptionAuthz("notAuthorized", ["action" => __FUNCTION__, "user" => $user]);
return (int) $this->db->prepare("SELECT rights from newssync_users where id is ?", "str")->run($user)->getSingle();
return (int) $this->db->prepare("SELECT rights from newssync_users where id is ?", "str")->run($user)->getValue();
}
public function userRightsSet(string $user, int $rights): bool {
@ -273,7 +273,7 @@ class Database {
// If the feed doesn't already exist in the database then add it to the database after determining its validity with PicoFeed.
$qFeed = $this->db->prepare("SELECT id from newssync_feeds where url is ? and username is ? and password is ?", "str", "str", "str");
$feed = $qFeed->run($url, $fetchUser, $fetchPassword)->getSingle();
$feed = $qFeed->run($url, $fetchUser, $fetchPassword)->getValue();
if ($feed === null) {
try {
$reader = new Reader;
@ -310,19 +310,19 @@ class Database {
// TODO: Populate newssync_articles with contents of what was obtained from PicoFeed.
// Get the ID for the feed that was just added.
$feedID = $qFeed->run($url, $fetchUser, $fetchPassword)->getSingle();
$feedID = $qFeed->run($url, $fetchUser, $fetchPassword)->getValue();
}
// Add the feed to the user's subscriptions.
$this->db->prepare("INSERT INTO newssync_subscriptions(owner,feed) values(?,?)", "str", "int")->run($user, $feedID);
$sub = $this->db->prepare("SELECT id from newssync_subscriptions where owner is ? and feed is ?", "str", "int")->run($user, $feedID)->getSingle();
$sub = $this->db->prepare("SELECT id from newssync_subscriptions where owner is ? and feed is ?", "str", "int")->run($user, $feedID)->getValue();
$this->db->commit();
return $sub;
}
public function subscriptionRemove(int $id): bool {
$this->db->begin();
$user = $this->db->prepare("SELECT owner from newssync_subscriptions where id is ?", "int")->run($id)->getSingle();
$user = $this->db->prepare("SELECT owner from newssync_subscriptions where id is ?", "int")->run($id)->getValue();
if($user===null) return false;
if(!$this->data->user->authorize($user, __FUNCTION__)) throw new User\ExceptionAuthz("notAuthorized", ["action" => __FUNCTION__, "user" => $user]);
return (bool) $this->db->prepare("DELETE from newssync_subscriptions where id is ?", "int")->run($id)->changes();

3
lib/Db/Result.php

@ -10,6 +10,7 @@ interface Result extends \Iterator {
function valid();
function get();
function getSingle();
function getAll();
function getValue();
function changes();
}

11
lib/Db/ResultSQLite3.php

@ -11,7 +11,7 @@ class ResultSQLite3 implements Result {
// actual public methods
public function getSingle() {
public function getValue() {
$this->next();
if($this->valid()) {
$keys = array_keys($this->cur);
@ -25,6 +25,15 @@ class ResultSQLite3 implements Result {
return ($this->valid() ? $this->cur : null);
}
public function getAll() {
$out = [];
foreach($this as $row) {
$out [] = $row;
}
if(sizeof($out) < 1) return null;
return $out;
}
public function changes() {
return $this->rows;
}

18
tests/Db/SQLite3/TestDbResultSQLite3.php

@ -56,10 +56,19 @@ class TestDbResultSQLite3 extends \PHPUnit\Framework\TestCase {
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());
$this->assertEquals(1867, $test->getValue());
$this->assertEquals(1970, $test->getValue());
$this->assertEquals(2112, $test->getValue());
$this->assertSame(null, $test->getValue());
}
function testGetFirstValuesOnly() {
$set = $this->c->query("SELECT 1867 as year, 19 as century union select 1970 as year, 20 as century union select 2112 as year, 22 as century");
$test = new Db\ResultSQLite3($set);
$this->assertEquals(1867, $test->getValue());
$this->assertEquals(1970, $test->getValue());
$this->assertEquals(2112, $test->getValue());
$this->assertSame(null, $test->getValue());
}
function testGetRows() {
@ -72,5 +81,6 @@ class TestDbResultSQLite3 extends \PHPUnit\Framework\TestCase {
$this->assertEquals($rows[0], $test->get());
$this->assertEquals($rows[1], $test->get());
$this->assertSame(null, $test->get());
$this->assertEquals($rows, $test->getAll());
}
}
Loading…
Cancel
Save