Pdo.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. /**
  3. * MINZ - Copyright 2011 Marien Fressinaud
  4. * Sous licence AGPL3 <http://www.gnu.org/licenses/>
  5. */
  6. abstract class Minz_Pdo extends PDO {
  7. public function __construct($dsn, $username = null, $passwd = null, $options = null) {
  8. parent::__construct($dsn, $username, $passwd, $options);
  9. $this->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
  10. }
  11. abstract public function dbType();
  12. private $prefix = '';
  13. public function prefix() { return $this->prefix; }
  14. public function setPrefix($prefix) { $this->prefix = $prefix; }
  15. private function autoPrefix($sql) {
  16. return str_replace('`_', '`' . $this->prefix, $sql);
  17. }
  18. protected function preSql($statement) {
  19. if (preg_match('/^(?:UPDATE|INSERT|DELETE)/i', $statement)) {
  20. invalidateHttpCache();
  21. }
  22. return $this->autoPrefix($statement);
  23. }
  24. public function lastInsertId($name = null) {
  25. if ($name != null) {
  26. $name = $this->preSql($name);
  27. }
  28. return parent::lastInsertId($name);
  29. }
  30. public function prepare($statement, $driver_options = array()) {
  31. $statement = $this->preSql($statement);
  32. return parent::prepare($statement, $driver_options);
  33. }
  34. public function exec($statement) {
  35. $statement = $this->preSql($statement);
  36. return parent::exec($statement);
  37. }
  38. public function query($query, $fetch_mode = null, ...$fetch_mode_args) {
  39. $query = $this->preSql($query);
  40. return $fetch_mode ? parent::query($query, $fetch_mode, ...$fetch_mode_args) : parent::query($query);
  41. }
  42. }