From bdf3182305ea81914926651462d858572a04c869 Mon Sep 17 00:00:00 2001 From: "J. King" Date: Thu, 9 Mar 2017 14:48:42 -0500 Subject: [PATCH] Add result->lastId method; cleanup - Result->lastId() added as MySQL and PostgreSQL have equivalent functionality - Adjusted tests accordingly - Cleaned up Database class to make use of this and getAll() --- lib/Database.php | 19 +++++-------------- lib/Db/Result.php | 2 ++ lib/Db/SQLite3/Driver.php | 4 +++- lib/Db/SQLite3/Result.php | 10 ++++++++-- lib/Db/SQLite3/Statement.php | 4 +++- tests/Db/SQLite3/TestDbDriverSQLite3.php | 14 ++++++++++++-- tests/Db/SQLite3/TestDbResultSQLite3.php | 7 +++++-- 7 files changed, 38 insertions(+), 22 deletions(-) diff --git a/lib/Database.php b/lib/Database.php index 006567eb..e7336fe3 100644 --- a/lib/Database.php +++ b/lib/Database.php @@ -193,16 +193,11 @@ class Database { if(!$this->data->user->authorize("@".$domain, __FUNCTION__)) throw new User\ExceptionAuthz("notAuthorized", ["action" => __FUNCTION__, "user" => $domain]); $domain = str_replace(["\\","%","_"],["\\\\", "\\%", "\\_"], $domain); $domain = "%@".$domain; - $set = $this->db->prepare("SELECT id from newssync_users where id like ?", "str")->run($domain); + return $this->db->prepare("SELECT id from newssync_users where id like ?", "str")->run($domain)->getAll(); } else { if(!$this->data->user->authorize("", __FUNCTION__)) throw new User\ExceptionAuthz("notAuthorized", ["action" => __FUNCTION__, "user" => "global"]); - $set = $this->db->prepare("SELECT id from newssync_users")->run(); + return $this->db->prepare("SELECT id from newssync_users")->run()->getAll(); } - $out = []; - foreach($set as $row) { - $out[] = $row["id"]; - } - return $out; } public function userPasswordGet(string $user): string { @@ -287,7 +282,7 @@ class Database { throw new Feed\Exception($url, $e); } - $this->db->prepare( + $feedID = $this->db->prepare( "INSERT INTO newssync_feeds(url,title,favicon,source,updated,modified,etag,username,password) values(?,?,?,?,?,?,?,?,?)", "str", "str", "str", "str", "datetime", "datetime", "str", "str", "str" )->run( @@ -301,17 +296,13 @@ class Database { $resource->getEtag(), $fetchUser, $fetchPassword - ); + )->lastId(); // 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)->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)->getValue(); + $sub = $this->db->prepare("INSERT INTO newssync_subscriptions(owner,feed) values(?,?)", "str", "int")->run($user, $feedID)->lastId(); $this->db->commit(); return $sub; } diff --git a/lib/Db/Result.php b/lib/Db/Result.php index 35a3d4d9..0f853f7f 100644 --- a/lib/Db/Result.php +++ b/lib/Db/Result.php @@ -12,5 +12,7 @@ interface Result extends \Iterator { function get(); function getAll(); function getValue(); + function changes(); + function lastId(); } \ No newline at end of file diff --git a/lib/Db/SQLite3/Driver.php b/lib/Db/SQLite3/Driver.php index 7029919d..e415c028 100644 --- a/lib/Db/SQLite3/Driver.php +++ b/lib/Db/SQLite3/Driver.php @@ -104,7 +104,9 @@ class Driver extends \JKingWeb\NewsSync\Db\AbstractDriver { list($excClass, $excMsg, $excData) = $this->exceptionBuild(); throw new $excClass($excMsg, $excData); } - return new Result($r, $this->db->changes()); + $changes = $this->db->changes(); + $lastId = $this->db->lastInsertRowID(); + return new Result($r, [$changes, $lastId]); } public function prepareArray(string $query, array $paramTypes): \JKingWeb\NewsSync\Db\Statement { diff --git a/lib/Db/SQLite3/Result.php b/lib/Db/SQLite3/Result.php index 36d18e7a..438398b3 100644 --- a/lib/Db/SQLite3/Result.php +++ b/lib/Db/SQLite3/Result.php @@ -8,6 +8,7 @@ class Result implements \JKingWeb\NewsSync\Db\Result { protected $pos = 0; protected $cur = null; protected $rows = 0; + protected $id = 0; // actual public methods @@ -38,12 +39,17 @@ class Result implements \JKingWeb\NewsSync\Db\Result { return $this->rows; } + public function lastId() { + return $this->id; + } + // constructor/destructor - public function __construct(\SQLite3Result $result, int $changes = 0, Statement $statement = null) { + public function __construct(\SQLite3Result $result, array $changes = [0,0], Statement $statement = null) { $this->st = $statement; //keeps the statement from being destroyed, invalidating the result set $this->set = $result; - $this->rows = $changes; + $this->rows = $changes[0]; + $this->id = $changes[1]; } public function __destruct() { diff --git a/lib/Db/SQLite3/Statement.php b/lib/Db/SQLite3/Statement.php index a1cdde90..6448c4f6 100644 --- a/lib/Db/SQLite3/Statement.php +++ b/lib/Db/SQLite3/Statement.php @@ -72,6 +72,8 @@ class Statement extends \JKingWeb\NewsSync\Db\AbstractStatement { list($excClass, $excMsg, $excData) = $this->exceptionBuild(); throw new $excClass($excMsg, $excData); } - return new Result($r, $this->db->changes(), $this); + $changes = $this->db->changes(); + $lastId = $this->db->lastInsertRowID(); + return new Result($r, [$changes, $lastId], $this); } } \ No newline at end of file diff --git a/tests/Db/SQLite3/TestDbDriverSQLite3.php b/tests/Db/SQLite3/TestDbDriverSQLite3.php index 640a8638..1ea3e139 100644 --- a/tests/Db/SQLite3/TestDbDriverSQLite3.php +++ b/tests/Db/SQLite3/TestDbDriverSQLite3.php @@ -55,11 +55,11 @@ class TestDbDriverSQLite3 extends \PHPUnit\Framework\TestCase { $this->drv->exec("INSERT INTO test(id) values('ook')"); } - function testValidQuery() { + function testMakeAValidQuery() { $this->assertInstanceOf(Db\SQLite3\Result::class, $this->drv->query("SELECT 1")); } - function testInvalidQuery() { + function testMakeAnInvalidQuery() { $this->assertException("engineErrorGeneral", "Db"); $this->drv->query("Apollo was astonished; Dionysus thought me mad"); } @@ -82,4 +82,14 @@ class TestDbDriverSQLite3 extends \PHPUnit\Framework\TestCase { $this->assertException("typeViolation", "Db", "ExceptionInput"); $this->drv->query("INSERT INTO test(id) values('ook')"); } + + function testPrepareAValidQuery() { + $s = $this->drv->prepare("SELECT ?, ?", "int", "int"); + $this->assertInstanceOf(Db\SQLite3\Statement::class, $s); + } + + function testPrepareAnInvalidQuery() { + $this->assertException("engineErrorGeneral", "Db"); + $s = $this->drv->prepare("This is an invalid query", "int", "int"); + } } \ No newline at end of file diff --git a/tests/Db/SQLite3/TestDbResultSQLite3.php b/tests/Db/SQLite3/TestDbResultSQLite3.php index 8954a6b5..5d967fa0 100644 --- a/tests/Db/SQLite3/TestDbResultSQLite3.php +++ b/tests/Db/SQLite3/TestDbResultSQLite3.php @@ -24,11 +24,14 @@ class TestDbResultSQLite3 extends \PHPUnit\Framework\TestCase { $this->assertInstanceOf(Db\Result::class, new Db\SQLite3\Result($set)); } - function testGetChangeCount() { + function testGetChangeCountAndLastInsertId() { $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\SQLite3\Result($set,$rows))->changes()); + $id = $this->c->lastInsertRowID(); + $r = new Db\SQLite3\Result($set,[$rows,$id]); + $this->assertEquals($rows, $r->changes()); + $this->assertEquals($id, $r->lastId()); } function testIterateOverResults() {