1
1
Fork 0
mirror of https://code.mensbeam.com/MensBeam/Arsse.git synced 2024-12-23 17:12:41 +00:00
Arsse/lib/Db/SQLite3/PDODriver.php

85 lines
2.5 KiB
PHP
Raw Normal View History

<?php
/** @license MIT
* Copyright 2017 J. King, Dustin Wilson et al.
* See LICENSE and AUTHORS files for details */
declare(strict_types=1);
namespace JKingWeb\Arsse\Db\SQLite3;
use JKingWeb\Arsse\Arsse;
use JKingWeb\Arsse\Db\Exception;
class PDODriver extends AbstractPDODriver {
protected $db;
public static function requirementsMet(): bool {
return class_exists("PDO") && in_array("sqlite", \PDO::getAvailableDrivers());
}
protected function makeConnection(string $file, string $key): void {
2018-12-06 22:46:00 +00:00
$this->db = new \PDO("sqlite:".$file, "", "", [
2022-01-11 22:54:02 +00:00
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
\PDO::ATTR_STRINGIFY_FETCHES => true,
2018-12-06 22:46:00 +00:00
]);
}
public function __destruct() {
unset($this->db);
}
/** @codeCoverageIgnore */
public static function create(): \JKingWeb\Arsse\Db\Driver {
if (self::requirementsMet()) {
return new self;
} elseif (Driver::requirementsMet()) {
return new Driver;
} else {
2017-12-22 16:51:58 +00:00
throw new Exception("extMissing", self::driverName());
}
}
public static function driverName(): string {
return Arsse::$lang->msg("Driver.Db.SQLite3PDO.Name");
}
public function prepareArray(string $query, array $paramTypes): \JKingWeb\Arsse\Db\Statement {
return new PDOStatement($this->db, $query, $paramTypes);
}
/** @codeCoverageIgnore */
public function exec(string $query): bool {
2019-05-02 02:52:20 +00:00
// because PDO uses sqlite3_prepare() internally instead of sqlite3_prepare_v2(),
// we have to retry ourselves in cases of schema changes
// the SQLite3 class is not similarly affected
$attempts = 0;
retry:
try {
return parent::exec($query);
} catch (\JKingWeb\Arsse\Db\ExceptionRetry $e) {
if (++$attempts > 50) {
throw $e;
} else {
goto retry;
}
}
}
/** @codeCoverageIgnore */
public function query(string $query): \JKingWeb\Arsse\Db\Result {
2019-05-02 02:52:20 +00:00
// because PDO uses sqlite3_prepare() internally instead of sqlite3_prepare_v2(),
// we have to retry ourselves in cases of schema changes
// the SQLite3 class is not similarly affected
$attempts = 0;
retry:
try {
return parent::query($query);
} catch (\JKingWeb\Arsse\Db\ExceptionRetry $e) {
if (++$attempts > 50) {
throw $e;
} else {
goto retry;
}
}
}
}