ModelPdo.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /**
  3. * MINZ - Copyright 2011 Marien Fressinaud
  4. * Sous licence AGPL3 <http://www.gnu.org/licenses/>
  5. */
  6. /**
  7. * La classe Model_sql représente le modèle interragissant avec les bases de données
  8. * Seul la connexion MySQL est prise en charge pour le moment
  9. */
  10. class Minz_ModelPdo {
  11. /**
  12. * Partage la connexion à la base de données entre toutes les instances.
  13. */
  14. public static $useSharedBd = true;
  15. private static $sharedBd = null;
  16. private static $sharedPrefix;
  17. /**
  18. * $bd variable représentant la base de données
  19. */
  20. protected $bd;
  21. protected $prefix;
  22. /**
  23. * Créé la connexion à la base de données à l'aide des variables
  24. * HOST, BASE, USER et PASS définies dans le fichier de configuration
  25. */
  26. public function __construct () {
  27. if (self::$useSharedBd && self::$sharedBd != null) {
  28. $this->bd = self::$sharedBd;
  29. $this->prefix = self::$sharedPrefix;
  30. return;
  31. }
  32. $db = Minz_Configuration::dataBase ();
  33. $driver_options = null;
  34. try {
  35. $type = $db['type'];
  36. if($type == 'mysql') {
  37. $string = $type
  38. . ':host=' . $db['host']
  39. . ';dbname=' . $db['base']
  40. . ';charset=utf8';
  41. $driver_options = array(
  42. PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'
  43. );
  44. } elseif($type == 'sqlite') {
  45. $string = $type . ':/' . DATA_PATH . $db['base'] . '.sqlite'; //TODO: DEBUG UTF-8 http://www.siteduzero.com/forum/sujet/sqlite-connexion-utf-8-18797
  46. }
  47. $this->bd = new FreshPDO (
  48. $string,
  49. $db['user'],
  50. $db['password'],
  51. $driver_options
  52. );
  53. self::$sharedBd = $this->bd;
  54. $this->prefix = $db['prefix'] . Minz_Session::param('currentUser', '_') . '_';
  55. self::$sharedPrefix = $this->prefix;
  56. } catch (Exception $e) {
  57. throw new Minz_PDOConnectionException (
  58. $string,
  59. $db['user'], Minz_Exception::ERROR
  60. );
  61. }
  62. }
  63. public function beginTransaction() {
  64. $this->bd->beginTransaction();
  65. }
  66. public function commit() {
  67. $this->bd->commit();
  68. }
  69. public function rollBack() {
  70. $this->bd->rollBack();
  71. }
  72. public function size($all = false) {
  73. $db = Minz_Configuration::dataBase ();
  74. $sql = 'SELECT SUM(data_length + index_length) FROM information_schema.TABLES WHERE table_schema = ?';
  75. $values = array ($db['base']);
  76. if (!$all) {
  77. $sql .= ' AND table_name LIKE ?';
  78. $values[] = $this->prefix . '%';
  79. }
  80. $stm = $this->bd->prepare ($sql);
  81. $stm->execute ($values);
  82. $res = $stm->fetchAll(PDO::FETCH_COLUMN, 0);
  83. return $res[0];
  84. }
  85. public static function clean() {
  86. self::$sharedBd = null;
  87. self::$sharedPrefix = '';
  88. }
  89. }
  90. class FreshPDO extends PDO {
  91. private static function check($statement) {
  92. if (preg_match('/^(?:UPDATE|INSERT|DELETE)/i', $statement)) {
  93. invalidateHttpCache();
  94. }
  95. }
  96. public function prepare ($statement, $driver_options = array()) {
  97. FreshPDO::check($statement);
  98. return parent::prepare($statement, $driver_options);
  99. }
  100. public function exec ($statement) {
  101. FreshPDO::check($statement);
  102. return parent::exec($statement);
  103. }
  104. }