From b4d178c33f1a5a46f0149fc258bece04ff50529e Mon Sep 17 00:00:00 2001 From: "J. King" Date: Wed, 1 Mar 2017 22:47:51 -0500 Subject: [PATCH] Remove most PDO stuff PDO will not be supported: the feature-set is less than any of the common native APIs, so why bother? --- lib/Db/CommonPDO.php | 17 ------- lib/Db/CommonSQLite3.php | 50 --------------------- lib/Db/DriverSQLite3.php | 49 ++++++++++++++++++-- lib/Db/DriverSQLite3PDO.php | 28 ------------ lib/Db/StatementSQLite3.php | 2 +- tests/Db/SQLite3/TestDbStatementSQLite3.php | 31 +++++++++++++ tests/phpunit.xml | 1 + 7 files changed, 79 insertions(+), 99 deletions(-) delete mode 100644 lib/Db/CommonPDO.php delete mode 100644 lib/Db/CommonSQLite3.php delete mode 100644 lib/Db/DriverSQLite3PDO.php create mode 100644 tests/Db/SQLite3/TestDbStatementSQLite3.php diff --git a/lib/Db/CommonPDO.php b/lib/Db/CommonPDO.php deleted file mode 100644 index 4489c9a0..00000000 --- a/lib/Db/CommonPDO.php +++ /dev/null @@ -1,17 +0,0 @@ -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); - } -} \ No newline at end of file diff --git a/lib/Db/CommonSQLite3.php b/lib/Db/CommonSQLite3.php deleted file mode 100644 index ab5ceea6..00000000 --- a/lib/Db/CommonSQLite3.php +++ /dev/null @@ -1,50 +0,0 @@ -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); - } -} \ No newline at end of file diff --git a/lib/Db/DriverSQLite3.php b/lib/Db/DriverSQLite3.php index 7ca9307c..babf994a 100644 --- a/lib/Db/DriverSQLite3.php +++ b/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()); } diff --git a/lib/Db/DriverSQLite3PDO.php b/lib/Db/DriverSQLite3PDO.php deleted file mode 100644 index 72d02528..00000000 --- a/lib/Db/DriverSQLite3PDO.php +++ /dev/null @@ -1,28 +0,0 @@ -db = $db; $this->st = $st; $this->types = []; diff --git a/tests/Db/SQLite3/TestDbStatementSQLite3.php b/tests/Db/SQLite3/TestDbStatementSQLite3.php new file mode 100644 index 00000000..2bba457f --- /dev/null +++ b/tests/Db/SQLite3/TestDbStatementSQLite3.php @@ -0,0 +1,31 @@ +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)); + } +} \ No newline at end of file diff --git a/tests/phpunit.xml b/tests/phpunit.xml index 57cad7cc..b4b94ebc 100644 --- a/tests/phpunit.xml +++ b/tests/phpunit.xml @@ -29,6 +29,7 @@ Db/SQLite3/TestDbResultSQLite3.php + Db/SQLite3/TestDbStatementSQLite3.php \ No newline at end of file