Browse Source
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
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); |
|||
} |
|||
} |
@ -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()); |
|||
} |
|||
} |
|||
} |
@ -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)); |
|||
} |
|||
} |
Loading…
Reference in new issue