Files
FreshRSS/lib/Minz/Pdo.php
Alexandre Alapetite a81656c3ed Upgrade to PHP 8.1 (#6711)
* Upgrade to PHP 8.1
As discussed in https://github.com/FreshRSS/FreshRSS/discussions/5474

https://www.php.net/releases/8.0/en.php
https://www.php.net/releases/8.1/en.php

Upgrade to available native type declarations
https://php.net/language.types.declarations

Upgrade to https://phpunit.de/announcements/phpunit-10.html which requires PHP 8.1+ (good timing, as version 9 was not maintained anymore)

Upgrade `:oldest` Docker dev image to oldest Alpine version supporting PHP 8.1: Alpine 3.16, which includes PHP 8.1.22.

* Include 6736
https://github.com/FreshRSS/FreshRSS/pull/6736
2024-09-06 09:06:46 +02:00

82 lines
2.4 KiB
PHP

<?php
declare(strict_types=1);
/**
* MINZ - Copyright 2011 Marien Fressinaud
* Sous licence AGPL3 <http://www.gnu.org/licenses/>
*/
abstract class Minz_Pdo extends PDO {
/**
* @param array<int,int|string|bool>|null $options
* @throws PDOException
*/
public function __construct(string $dsn, ?string $username = null, ?string $passwd = null, ?array $options = null) {
parent::__construct($dsn, $username, $passwd, $options);
$this->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
}
abstract public function dbType(): string;
private string $prefix = '';
public function prefix(): string {
return $this->prefix;
}
public function setPrefix(string $prefix): void {
$this->prefix = $prefix;
}
private function autoPrefix(string $sql): string {
return str_replace('`_', '`' . $this->prefix, $sql);
}
protected function preSql(string $statement): string {
if (preg_match('/^(?:UPDATE|INSERT|DELETE)/i', $statement) === 1) {
invalidateHttpCache();
}
return $this->autoPrefix($statement);
}
/**
* @throws PDOException if the attribute `PDO::ATTR_ERRMODE` is set to `PDO::ERRMODE_EXCEPTION`
*/
#[\Override]
public function lastInsertId(?string $name = null): string|false {
if ($name != null) {
$name = $this->preSql($name);
}
return parent::lastInsertId($name);
}
/**
* @param array<int,string> $options
* @throws PDOException if the attribute `PDO::ATTR_ERRMODE` is set to `PDO::ERRMODE_EXCEPTION`
* @phpstan-ignore method.childParameterType, throws.unusedType
*/
#[\Override]
public function prepare(string $query, array $options = []): PDOStatement|false {
$query = $this->preSql($query);
return parent::prepare($query, $options);
}
/**
* @throws PDOException if the attribute `PDO::ATTR_ERRMODE` is set to `PDO::ERRMODE_EXCEPTION`
* @phpstan-ignore throws.unusedType
*/
#[\Override]
public function exec(string $statement): int|false {
$statement = $this->preSql($statement);
return parent::exec($statement);
}
/**
* @throws PDOException if the attribute `PDO::ATTR_ERRMODE` is set to `PDO::ERRMODE_EXCEPTION`
* @phpstan-ignore throws.unusedType
*/
#[\Override]
public function query(string $query, ?int $fetch_mode = null, ...$fetch_mode_args): PDOStatement|false {
$query = $this->preSql($query);
return $fetch_mode === null ? parent::query($query) : parent::query($query, $fetch_mode, ...$fetch_mode_args);
}
}