Browse Source

Remove most PDO stuff

PDO will not be supported: the feature-set is less than any of the common native APIs, so why bother?
microsub
J. King 7 years ago
parent
commit
b4d178c33f
  1. 17
      lib/Db/CommonPDO.php
  2. 50
      lib/Db/CommonSQLite3.php
  3. 49
      lib/Db/DriverSQLite3.php
  4. 28
      lib/Db/DriverSQLite3PDO.php
  5. 2
      lib/Db/StatementSQLite3.php
  6. 31
      tests/Db/SQLite3/TestDbStatementSQLite3.php
  7. 1
      tests/phpunit.xml

17
lib/Db/CommonPDO.php

@ -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);
}
}

50
lib/Db/CommonSQLite3.php

@ -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);
}
}

49
lib/Db/DriverSQLite3.php

@ -3,9 +3,7 @@ declare(strict_types=1);
namespace JKingWeb\NewsSync\Db;
class DriverSQLite3 implements Driver {
use Common, CommonSQLite3 {
CommonSQLite3::schemaVersion insteadof Common;
}
use Common;
protected $db;
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 {
return new ResultSQLite3($this->db->query($query), $this->db->changes());
}

28
lib/Db/DriverSQLite3PDO.php

@ -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());
}
}
}

2
lib/Db/StatementSQLite3.php

@ -7,7 +7,7 @@ class StatementSQLite3 implements Statement {
protected $st;
protected $types;
public function __construct($db, $st, array $bindings = null) {
public function __construct($db, $st, array $bindings = []) {
$this->db = $db;
$this->st = $st;
$this->types = [];

31
tests/Db/SQLite3/TestDbStatementSQLite3.php

@ -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));
}
}

1
tests/phpunit.xml

@ -29,6 +29,7 @@
<testsuite name="SQLite3 database driver">
<file>Db/SQLite3/TestDbResultSQLite3.php</file>
<file>Db/SQLite3/TestDbStatementSQLite3.php</file>
</testsuite>
</phpunit>
Loading…
Cancel
Save