Tests / PHP 8.4 (push) Failing after 5m34s
Drop support for PHP 8.3 and lower. The package now declares >=8.4, and CI runs only on 8.4. - composer.json: require php >=8.4. - mago.toml: php-version = "8.4", matching the minimum supported version. - phpstan.neon: phpVersion 80400. - Upgrade phpstan/phpstan to ^2.3: 1.x cannot parse `new Foo()->bar()`, which the 8.4 formatter now emits. - Fix the type errors PHPStan 2 reports: class-string<static> variance, optional array offsets in fromEnvironment, list vs array for PdoConnection::$lru, ReflectionClass<Model> docblocks, Repository made covariant in T, and a redundant null comparison in validateRelationPath. - CI: single PHP 8.4 job using the php:8.4-cli container instead of setup-php, which failed with a TLS handshake timeout on the runner. BREAKING CHANGE: PHP 8.4 is now required.
214 lines
5.1 KiB
PHP
214 lines
5.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Camelia\Orm\Connection;
|
|
|
|
use Camelia\Orm\Exception\ConfigurationException;
|
|
use Camelia\Orm\Exception\DatabaseException;
|
|
use PDO;
|
|
use PDOException;
|
|
use PDOStatement;
|
|
|
|
/**
|
|
* PDO-backed transport with a bounded LRU statement cache.
|
|
*
|
|
* Invariant: statements are always executed with execute(array). bindParam is never used,
|
|
* because cached statements are shared across calls.
|
|
*/
|
|
final class PdoConnection implements Connection
|
|
{
|
|
private const MAX_CACHED_STATEMENTS = 200;
|
|
|
|
/** @var array<string, PDOStatement> */
|
|
private array $statements = [];
|
|
|
|
/** @var array<int, string> LRU order, most recent last; reindexed after eviction */
|
|
private array $lru = [];
|
|
|
|
/** @var list<string> savepoint names for nested transactions */
|
|
private array $savepoints = [];
|
|
|
|
public function __construct(
|
|
private readonly PDO $pdo,
|
|
) {
|
|
if ($pdo->getAttribute(PDO::ATTR_ERRMODE) !== PDO::ERRMODE_EXCEPTION) {
|
|
throw ConfigurationException::invalidErrorMode();
|
|
}
|
|
}
|
|
|
|
public function execute(string $sql, array $params = []): int
|
|
{
|
|
$statement = $this->prepare($sql);
|
|
|
|
try {
|
|
$statement->execute($params);
|
|
} catch (PDOException $exception) {
|
|
throw DatabaseException::fromPdoException($exception, 'write', $sql, $params);
|
|
}
|
|
|
|
return $statement->rowCount();
|
|
}
|
|
|
|
public function select(string $sql, array $params = []): array
|
|
{
|
|
$statement = $this->prepare($sql);
|
|
|
|
try {
|
|
$statement->execute($params);
|
|
$rows = $statement->fetchAll();
|
|
} catch (PDOException $exception) {
|
|
throw DatabaseException::fromPdoException($exception, 'read', $sql, $params);
|
|
}
|
|
|
|
/** @var list<array<string, mixed>> $rows */
|
|
return $rows;
|
|
}
|
|
|
|
public function scalar(string $sql, array $params = []): mixed
|
|
{
|
|
$statement = $this->prepare($sql);
|
|
|
|
try {
|
|
$statement->execute($params);
|
|
$value = $statement->fetchColumn();
|
|
} catch (PDOException $exception) {
|
|
throw DatabaseException::fromPdoException($exception, 'read', $sql, $params);
|
|
}
|
|
|
|
$statement->closeCursor();
|
|
|
|
return $value === false ? null : $value;
|
|
}
|
|
|
|
public function lastInsertId(): string
|
|
{
|
|
$id = $this->pdo->lastInsertId();
|
|
|
|
return $id === false ? '0' : $id;
|
|
}
|
|
|
|
public function begin(): void
|
|
{
|
|
if ($this->pdo->inTransaction()) {
|
|
$name = 'camelia_sp_' . count($this->savepoints);
|
|
$this->pdo->exec('SAVEPOINT ' . $name);
|
|
$this->savepoints[] = $name;
|
|
|
|
return;
|
|
}
|
|
|
|
$this->pdo->beginTransaction();
|
|
}
|
|
|
|
public function commit(): void
|
|
{
|
|
if ($this->savepoints !== []) {
|
|
$name = array_pop($this->savepoints);
|
|
$this->pdo->exec('RELEASE SAVEPOINT ' . $name);
|
|
|
|
return;
|
|
}
|
|
|
|
if ($this->pdo->inTransaction()) {
|
|
$this->pdo->commit();
|
|
}
|
|
}
|
|
|
|
public function rollBack(): void
|
|
{
|
|
if ($this->savepoints !== []) {
|
|
$name = array_pop($this->savepoints);
|
|
$this->pdo->exec('ROLLBACK TO SAVEPOINT ' . $name);
|
|
|
|
return;
|
|
}
|
|
|
|
if ($this->pdo->inTransaction()) {
|
|
$this->pdo->rollBack();
|
|
}
|
|
}
|
|
|
|
public function inTransaction(): bool
|
|
{
|
|
return $this->pdo->inTransaction();
|
|
}
|
|
|
|
public function driver(): string
|
|
{
|
|
return (string) $this->pdo->getAttribute(PDO::ATTR_DRIVER_NAME);
|
|
}
|
|
|
|
/**
|
|
* Raw PDO handle for cases the library does not cover.
|
|
*/
|
|
public function pdo(): PDO
|
|
{
|
|
return $this->pdo;
|
|
}
|
|
|
|
/**
|
|
* Clears the statement cache. Tests only.
|
|
*/
|
|
public function clearStatementCache(): void
|
|
{
|
|
$this->statements = [];
|
|
$this->lru = [];
|
|
}
|
|
|
|
public function statementCacheSize(): int
|
|
{
|
|
return count($this->statements);
|
|
}
|
|
|
|
private function prepare(string $sql): PDOStatement
|
|
{
|
|
$key = md5($sql);
|
|
$cached = $this->statements[$key] ?? null;
|
|
|
|
if ($cached instanceof PDOStatement) {
|
|
$this->touch($key);
|
|
|
|
return $cached;
|
|
}
|
|
|
|
$statement = $this->pdo->prepare($sql);
|
|
|
|
if (!$statement instanceof PDOStatement) {
|
|
throw new DatabaseException(sprintf('Unable to prepare statement: %s', $sql), $sql);
|
|
}
|
|
|
|
$this->statements[$key] = $statement;
|
|
$this->lru[] = $key;
|
|
|
|
$this->evict();
|
|
|
|
return $statement;
|
|
}
|
|
|
|
private function touch(string $key): void
|
|
{
|
|
$position = array_search($key, $this->lru, true);
|
|
|
|
if ($position !== false) {
|
|
unset($this->lru[$position]);
|
|
$this->lru = array_values($this->lru);
|
|
}
|
|
|
|
$this->lru[] = $key;
|
|
}
|
|
|
|
private function evict(): void
|
|
{
|
|
while (count($this->statements) > self::MAX_CACHED_STATEMENTS) {
|
|
$oldest = array_shift($this->lru);
|
|
|
|
if ($oldest === null) {
|
|
break;
|
|
}
|
|
|
|
unset($this->statements[$oldest]);
|
|
}
|
|
}
|
|
}
|