2018-12-20 23:06:28 +00:00
|
|
|
<?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\MySQL;
|
|
|
|
|
|
|
|
use JKingWeb\Arsse\Arsse;
|
|
|
|
use JKingWeb\Arsse\Db\Exception;
|
|
|
|
|
|
|
|
class PDODriver extends Driver {
|
|
|
|
use \JKingWeb\Arsse\Db\PDODriver;
|
|
|
|
|
|
|
|
protected $db;
|
|
|
|
|
|
|
|
public static function requirementsMet(): bool {
|
|
|
|
return class_exists("PDO") && in_array("mysql", \PDO::getAvailableDrivers());
|
|
|
|
}
|
|
|
|
|
2020-01-20 18:34:03 +00:00
|
|
|
protected function makeConnection(string $db, string $user, string $password, string $host, int $port, string $socket): void {
|
2019-01-15 13:58:11 +00:00
|
|
|
$dsn = "mysql:".implode(";", [
|
|
|
|
"charset=utf8mb4",
|
|
|
|
"dbname=$db",
|
|
|
|
"host=$host",
|
|
|
|
"socket=$socket",
|
|
|
|
"port=$port",
|
|
|
|
]);
|
2019-01-15 15:51:55 +00:00
|
|
|
try {
|
|
|
|
$this->db = new \PDO($dsn, $user, $password, [
|
2022-01-11 22:54:02 +00:00
|
|
|
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
|
2022-06-05 03:02:24 +00:00
|
|
|
\PDO::ATTR_STRINGIFY_FETCHES => false,
|
2019-01-15 15:51:55 +00:00
|
|
|
]);
|
|
|
|
} catch (\PDOException $e) {
|
|
|
|
$msg = $e->getMessage();
|
|
|
|
$code = (int) substr($msg, 17, 4);
|
|
|
|
$msg = substr($msg, 23);
|
2020-03-01 20:16:50 +00:00
|
|
|
[$excClass, $excMsg, $excData] = $this->buildConnectionException($code, $msg);
|
2019-01-15 15:51:55 +00:00
|
|
|
throw new $excClass($excMsg, $excData);
|
|
|
|
}
|
2018-12-20 23:06:28 +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 {
|
|
|
|
throw new Exception("extMissing", self::driverName());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-21 22:37:22 +00:00
|
|
|
public function prepareArray(string $query, array $paramTypes): \JKingWeb\Arsse\Db\Statement {
|
|
|
|
return new PDOStatement($this->db, $query, $paramTypes);
|
|
|
|
}
|
2018-12-20 23:06:28 +00:00
|
|
|
|
|
|
|
public static function driverName(): string {
|
|
|
|
return Arsse::$lang->msg("Driver.Db.MySQLPDO.Name");
|
|
|
|
}
|
|
|
|
}
|