mirror of
https://code.mensbeam.com/MensBeam/Arsse.git
synced 2024-12-22 21:22:40 +00:00
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()
This commit is contained in:
parent
68f18e463c
commit
bdf3182305
7 changed files with 38 additions and 22 deletions
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -12,5 +12,7 @@ interface Result extends \Iterator {
|
|||
function get();
|
||||
function getAll();
|
||||
function getValue();
|
||||
|
||||
function changes();
|
||||
function lastId();
|
||||
}
|
|
@ -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 {
|
||||
|
|
|
@ -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() {
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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");
|
||||
}
|
||||
}
|
|
@ -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() {
|
||||
|
|
Loading…
Reference in a new issue