mirror of
https://code.mensbeam.com/MensBeam/Arsse.git
synced 2024-12-22 21:22:40 +00:00
Remove most PDO stuff
PDO will not be supported: the feature-set is less than any of the common native APIs, so why bother?
This commit is contained in:
parent
176247894d
commit
b4d178c33f
7 changed files with 79 additions and 99 deletions
|
@ -1,17 +0,0 @@
|
||||||
<?php
|
|
||||||
declare(strict_types=1);
|
|
||||||
namespace JKingWeb\NewsSync\Db;
|
|
||||||
|
|
||||||
Trait CommonPDO {
|
|
||||||
public function query(string $query): Result {
|
|
||||||
return new ResultPDO($this->db->query($query));
|
|
||||||
}
|
|
||||||
|
|
||||||
public function prepareArray(string $query, array $paramTypes): Statement {
|
|
||||||
return new StatementPDO($query, $paramTypes);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function prepare(string $query, string ...$paramType): Statement {
|
|
||||||
return $this->prepareArray($query, $paramType);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,50 +0,0 @@
|
||||||
<?php
|
|
||||||
declare(strict_types=1);
|
|
||||||
namespace JKingWeb\NewsSync\Db;
|
|
||||||
|
|
||||||
Trait CommonSQLite3 {
|
|
||||||
|
|
||||||
static public function driverName(): string {
|
|
||||||
return "SQLite 3";
|
|
||||||
}
|
|
||||||
|
|
||||||
public function schemaVersion(): int {
|
|
||||||
return $this->query("PRAGMA user_version")->getSingle();
|
|
||||||
}
|
|
||||||
|
|
||||||
public function update(int $to): bool {
|
|
||||||
$ver = $this->schemaVersion();
|
|
||||||
if(!$this->data->conf->dbSQLite3AutoUpd) throw new Update\Exception("manual", ['version' => $ver, 'driver_name' => $this->driverName()]);
|
|
||||||
if($ver >= $to) throw new Update\Exception("tooNew", ['difference' => ($ver - $to), 'current' => $ver, 'target' => $to, 'driver_name' => $this->driverName()]);
|
|
||||||
$sep = \DIRECTORY_SEPARATOR;
|
|
||||||
$path = \JKingWeb\NewsSync\BASE."sql".$sep."SQLite3".$sep;
|
|
||||||
$this->lock();
|
|
||||||
$this->begin();
|
|
||||||
for($a = $ver; $a < $to; $a++) {
|
|
||||||
$this->begin();
|
|
||||||
try {
|
|
||||||
$file = $path.$a.".sql";
|
|
||||||
if(!file_exists($file)) throw new Update\Exception("fileMissing", ['file' => $file, 'driver_name' => $this->driverName()]);
|
|
||||||
if(!is_readable($file)) throw new Update\Exception("fileUnreadable", ['file' => $file, 'driver_name' => $this->driverName()]);
|
|
||||||
$sql = @file_get_contents($file);
|
|
||||||
if($sql===false) throw new Update\Exception("fileUnusable", ['file' => $file, 'driver_name' => $this->driverName()]);
|
|
||||||
$this->exec($sql);
|
|
||||||
} catch(\Throwable $e) {
|
|
||||||
// undo any partial changes from the failed update
|
|
||||||
$this->rollback();
|
|
||||||
// commit any successful updates if updating by more than one version
|
|
||||||
$this->commit(true);
|
|
||||||
// throw the error received
|
|
||||||
throw $e;
|
|
||||||
}
|
|
||||||
$this->commit();
|
|
||||||
}
|
|
||||||
$this->unlock();
|
|
||||||
$this->commit();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function exec(string $query): bool {
|
|
||||||
return (bool) $this->db->exec($query);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -3,9 +3,7 @@ declare(strict_types=1);
|
||||||
namespace JKingWeb\NewsSync\Db;
|
namespace JKingWeb\NewsSync\Db;
|
||||||
|
|
||||||
class DriverSQLite3 implements Driver {
|
class DriverSQLite3 implements Driver {
|
||||||
use Common, CommonSQLite3 {
|
use Common;
|
||||||
CommonSQLite3::schemaVersion insteadof Common;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected $db;
|
protected $db;
|
||||||
protected $data;
|
protected $data;
|
||||||
|
@ -49,6 +47,51 @@ class DriverSQLite3 implements Driver {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static public function driverName(): string {
|
||||||
|
return "SQLite 3";
|
||||||
|
}
|
||||||
|
|
||||||
|
public function schemaVersion(): int {
|
||||||
|
return $this->query("PRAGMA user_version")->getSingle();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function update(int $to): bool {
|
||||||
|
$ver = $this->schemaVersion();
|
||||||
|
if(!$this->data->conf->dbSQLite3AutoUpd) throw new Update\Exception("manual", ['version' => $ver, 'driver_name' => $this->driverName()]);
|
||||||
|
if($ver >= $to) throw new Update\Exception("tooNew", ['difference' => ($ver - $to), 'current' => $ver, 'target' => $to, 'driver_name' => $this->driverName()]);
|
||||||
|
$sep = \DIRECTORY_SEPARATOR;
|
||||||
|
$path = \JKingWeb\NewsSync\BASE."sql".$sep."SQLite3".$sep;
|
||||||
|
$this->lock();
|
||||||
|
$this->begin();
|
||||||
|
for($a = $ver; $a < $to; $a++) {
|
||||||
|
$this->begin();
|
||||||
|
try {
|
||||||
|
$file = $path.$a.".sql";
|
||||||
|
if(!file_exists($file)) throw new Update\Exception("fileMissing", ['file' => $file, 'driver_name' => $this->driverName()]);
|
||||||
|
if(!is_readable($file)) throw new Update\Exception("fileUnreadable", ['file' => $file, 'driver_name' => $this->driverName()]);
|
||||||
|
$sql = @file_get_contents($file);
|
||||||
|
if($sql===false) throw new Update\Exception("fileUnusable", ['file' => $file, 'driver_name' => $this->driverName()]);
|
||||||
|
$this->exec($sql);
|
||||||
|
} catch(\Throwable $e) {
|
||||||
|
// undo any partial changes from the failed update
|
||||||
|
$this->rollback();
|
||||||
|
// commit any successful updates if updating by more than one version
|
||||||
|
$this->commit(true);
|
||||||
|
// throw the error received
|
||||||
|
throw $e;
|
||||||
|
}
|
||||||
|
$this->commit();
|
||||||
|
}
|
||||||
|
$this->unlock();
|
||||||
|
$this->commit();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function exec(string $query): bool {
|
||||||
|
return (bool) $this->db->exec($query);
|
||||||
|
}
|
||||||
|
|
||||||
public function query(string $query): Result {
|
public function query(string $query): Result {
|
||||||
return new ResultSQLite3($this->db->query($query), $this->db->changes());
|
return new ResultSQLite3($this->db->query($query), $this->db->changes());
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,28 +0,0 @@
|
||||||
<?php
|
|
||||||
declare(strict_types=1);
|
|
||||||
namespace JKingWeb\NewsSync\Db;
|
|
||||||
|
|
||||||
class DriverSQLite3 implements Driver {
|
|
||||||
use CommonPDO, CommonSQLite3;
|
|
||||||
|
|
||||||
protected $db;
|
|
||||||
|
|
||||||
private function __construct(\JKingWeb\NewsSync\RuntimeData $data, bool $install = false) {
|
|
||||||
// FIXME: stub
|
|
||||||
}
|
|
||||||
|
|
||||||
public function __destruct() {
|
|
||||||
// FIXME: stub
|
|
||||||
}
|
|
||||||
|
|
||||||
static public function create(\JKingWeb\NewsSync\RuntimeData $data, bool $install = false): Driver {
|
|
||||||
// check to make sure required extensions are loaded
|
|
||||||
if(class_exists("PDO") && in_array("sqlite",\PDO::getAvailableDrivers())) {
|
|
||||||
return new self($data, $install);
|
|
||||||
} else if(class_exists("SQLite3")) {
|
|
||||||
return new DriverSQLite3($data, $install);
|
|
||||||
} else {
|
|
||||||
throw new Exception("extMissing", self::driverName());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -7,7 +7,7 @@ class StatementSQLite3 implements Statement {
|
||||||
protected $st;
|
protected $st;
|
||||||
protected $types;
|
protected $types;
|
||||||
|
|
||||||
public function __construct($db, $st, array $bindings = null) {
|
public function __construct($db, $st, array $bindings = []) {
|
||||||
$this->db = $db;
|
$this->db = $db;
|
||||||
$this->st = $st;
|
$this->st = $st;
|
||||||
$this->types = [];
|
$this->types = [];
|
||||||
|
|
31
tests/Db/SQLite3/TestDbStatementSQLite3.php
Normal file
31
tests/Db/SQLite3/TestDbStatementSQLite3.php
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
namespace JKingWeb\NewsSync;
|
||||||
|
|
||||||
|
|
||||||
|
class TestDbStatementSQLite3 extends \PHPUnit\Framework\TestCase {
|
||||||
|
use Test\Tools;
|
||||||
|
|
||||||
|
protected $c;
|
||||||
|
protected $s;
|
||||||
|
|
||||||
|
function setUp() {
|
||||||
|
date_default_timezone_set("UTC");
|
||||||
|
$c = new \SQLite3(":memory:");
|
||||||
|
$c->enableExceptions(true);
|
||||||
|
$s = $c->prepare("SELECT ? as value");
|
||||||
|
$this->c = $c;
|
||||||
|
$this->s = $s;
|
||||||
|
}
|
||||||
|
|
||||||
|
function tearDown() {
|
||||||
|
try {$this->s->close();} catch(\Exception $e) {}
|
||||||
|
$this->c->close();
|
||||||
|
unset($this->s);
|
||||||
|
unset($this->c);
|
||||||
|
}
|
||||||
|
|
||||||
|
function testConstructStatement() {
|
||||||
|
$this->assertInstanceOf(Db\StatementSQLite3::class, new Db\StatementSQLite3($this->c, $this->s));
|
||||||
|
}
|
||||||
|
}
|
|
@ -29,6 +29,7 @@
|
||||||
|
|
||||||
<testsuite name="SQLite3 database driver">
|
<testsuite name="SQLite3 database driver">
|
||||||
<file>Db/SQLite3/TestDbResultSQLite3.php</file>
|
<file>Db/SQLite3/TestDbResultSQLite3.php</file>
|
||||||
|
<file>Db/SQLite3/TestDbStatementSQLite3.php</file>
|
||||||
</testsuite>
|
</testsuite>
|
||||||
|
|
||||||
</phpunit>
|
</phpunit>
|
Loading…
Reference in a new issue