Pdo.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. #[\Override]
  40. #[\ReturnTypeWillChange]
  41. public function lastInsertId($name = null) {
  42. if ($name != null) {
  43. $name = $this->preSql($name);
  44. }
  45. return parent::lastInsertId($name);
  46. }
  47. // PHP8+: PDO::prepare(string $query, array $options = []): PDOStatement|false
  48. /**
  49. * @param string $query
  50. * @param array<int,string> $options
  51. * @return PDOStatement|false
  52. * @throws PDOException if the attribute `PDO::ATTR_ERRMODE` is set to `PDO::ERRMODE_EXCEPTION`
  53. * @phpstan-ignore method.childParameterType, throws.unusedType
  54. */
  55. #[\Override]
  56. #[\ReturnTypeWillChange]
  57. public function prepare($query, $options = []) {
  58. $query = $this->preSql($query);
  59. return parent::prepare($query, $options);
  60. }
  61. // PHP8+: PDO::exec(string $statement): int|false
  62. /**
  63. * @param string $statement
  64. * @return int|false
  65. * @throws PDOException if the attribute `PDO::ATTR_ERRMODE` is set to `PDO::ERRMODE_EXCEPTION`
  66. * @phpstan-ignore throws.unusedType
  67. */
  68. #[\Override]
  69. #[\ReturnTypeWillChange]
  70. public function exec($statement) {
  71. $statement = $this->preSql($statement);
  72. return parent::exec($statement);
  73. }
  74. /**
  75. * @return PDOStatement|false
  76. * @throws PDOException if the attribute `PDO::ATTR_ERRMODE` is set to `PDO::ERRMODE_EXCEPTION`
  77. * @phpstan-ignore throws.unusedType
  78. */
  79. #[\Override]
  80. #[\ReturnTypeWillChange]
  81. public function query(string $query, ?int $fetch_mode = null, ...$fetch_mode_args) {
  82. $query = $this->preSql($query);
  83. return $fetch_mode === null ? parent::query($query) : parent::query($query, $fetch_mode, ...$fetch_mode_args);
  84. }
  85. }