*/ private array $statements = []; /** @var array LRU order, most recent last; reindexed after eviction */ private array $lru = []; /** @var list 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> $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]); } } }