Pdo.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * MINZ - Copyright 2011 Marien Fressinaud
  5. * Sous licence AGPL3 <http://www.gnu.org/licenses/>
  6. */
  7. abstract class Minz_Pdo extends PDO {
  8. /**
  9. * @param array<int,int|string|bool>|null $options
  10. * @throws PDOException
  11. */
  12. public function __construct(string $dsn, ?string $username = null, ?string $passwd = null, ?array $options = null) {
  13. parent::__construct($dsn, $username, $passwd, $options);
  14. $this->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
  15. }
  16. abstract public function dbType(): string;
  17. private string $prefix = '';
  18. public function prefix(): string {
  19. return $this->prefix;
  20. }
  21. public function setPrefix(string $prefix): void {
  22. $this->prefix = $prefix;
  23. }
  24. private function autoPrefix(string $sql): string {
  25. return str_replace('`_', '`' . $this->prefix, $sql);
  26. }
  27. protected function preSql(string $statement): string {
  28. if (preg_match('/^(?:UPDATE|INSERT|DELETE)/i', $statement) === 1) {
  29. invalidateHttpCache();
  30. }
  31. return $this->autoPrefix($statement);
  32. }
  33. // PHP8+: PDO::lastInsertId(?string $name = null): string|false
  34. /**
  35. * @param string|null $name
  36. * @return string|false
  37. * @throws PDOException if the attribute `PDO::ATTR_ERRMODE` is set to `PDO::ERRMODE_EXCEPTION`
  38. */
  39. #[\ReturnTypeWillChange]
  40. public function lastInsertId($name = null) {
  41. if ($name != null) {
  42. $name = $this->preSql($name);
  43. }
  44. return parent::lastInsertId($name);
  45. }
  46. // PHP8+: PDO::prepare(string $query, array $options = []): PDOStatement|false
  47. /**
  48. * @param string $query
  49. * @param array<int,string> $options
  50. * @return PDOStatement|false
  51. * @throws PDOException if the attribute `PDO::ATTR_ERRMODE` is set to `PDO::ERRMODE_EXCEPTION`
  52. * @phpstan-ignore-next-line
  53. */
  54. #[\ReturnTypeWillChange]
  55. public function prepare($query, $options = []) {
  56. $query = $this->preSql($query);
  57. return parent::prepare($query, $options);
  58. }
  59. // PHP8+: PDO::exec(string $statement): int|false
  60. /**
  61. * @param string $statement
  62. * @return int|false
  63. * @throws PDOException if the attribute `PDO::ATTR_ERRMODE` is set to `PDO::ERRMODE_EXCEPTION`
  64. * @phpstan-ignore-next-line
  65. */
  66. #[\ReturnTypeWillChange]
  67. public function exec($statement) {
  68. $statement = $this->preSql($statement);
  69. return parent::exec($statement);
  70. }
  71. /**
  72. * @return PDOStatement|false
  73. * @throws PDOException if the attribute `PDO::ATTR_ERRMODE` is set to `PDO::ERRMODE_EXCEPTION`
  74. * @phpstan-ignore-next-line
  75. */
  76. #[\ReturnTypeWillChange]
  77. public function query(string $query, ?int $fetch_mode = null, ...$fetch_mode_args) {
  78. $query = $this->preSql($query);
  79. return $fetch_mode === null ? parent::query($query) : parent::query($query, $fetch_mode, ...$fetch_mode_args);
  80. }
  81. }