ModelPdo.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. */
  9. class Minz_ModelPdo {
  10. /**
  11. * Partage la connexion à la base de données entre toutes les instances.
  12. */
  13. public static $usesSharedPdo = true;
  14. private static $sharedPdo = null;
  15. private static $sharedCurrentUser;
  16. protected $pdo;
  17. protected $current_user;
  18. private function dbConnect() {
  19. $db = Minz_Configuration::get('system')->db;
  20. $driver_options = isset($db['pdo_options']) && is_array($db['pdo_options']) ? $db['pdo_options'] : [];
  21. $driver_options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_SILENT;
  22. $dbServer = parse_url('db://' . $db['host']);
  23. $dsn = '';
  24. $dsnParams = empty($db['connection_uri_params']) ? '' : (';' . $db['connection_uri_params']);
  25. switch ($db['type']) {
  26. case 'mysql':
  27. $dsn = 'mysql:host=' . (empty($dbServer['host']) ? $db['host'] : $dbServer['host']) . ';charset=utf8mb4';
  28. if (!empty($db['base'])) {
  29. $dsn .= ';dbname=' . $db['base'];
  30. }
  31. if (!empty($dbServer['port'])) {
  32. $dsn .= ';port=' . $dbServer['port'];
  33. }
  34. $driver_options[PDO::MYSQL_ATTR_INIT_COMMAND] = 'SET NAMES utf8mb4';
  35. $this->pdo = new Minz_PdoMysql($dsn . $dsnParams, $db['user'], $db['password'], $driver_options);
  36. $this->pdo->setPrefix($db['prefix'] . $this->current_user . '_');
  37. break;
  38. case 'sqlite':
  39. $dsn = 'sqlite:' . join_path(DATA_PATH, 'users', $this->current_user, 'db.sqlite');
  40. $this->pdo = new Minz_PdoSqlite($dsn . $dsnParams, $db['user'], $db['password'], $driver_options);
  41. $this->pdo->setPrefix('');
  42. break;
  43. case 'pgsql':
  44. $dsn = 'pgsql:host=' . (empty($dbServer['host']) ? $db['host'] : $dbServer['host']);
  45. if (!empty($db['base'])) {
  46. $dsn .= ';dbname=' . $db['base'];
  47. }
  48. if (!empty($dbServer['port'])) {
  49. $dsn .= ';port=' . $dbServer['port'];
  50. }
  51. $this->pdo = new Minz_PdoPgsql($dsn . $dsnParams, $db['user'], $db['password'], $driver_options);
  52. $this->pdo->setPrefix($db['prefix'] . $this->current_user . '_');
  53. break;
  54. default:
  55. throw new Minz_PDOConnectionException(
  56. 'Invalid database type!',
  57. $db['user'], Minz_Exception::ERROR
  58. );
  59. }
  60. self::$sharedPdo = $this->pdo;
  61. }
  62. /**
  63. * Créé la connexion à la base de données à l'aide des variables
  64. * HOST, BASE, USER et PASS définies dans le fichier de configuration
  65. */
  66. public function __construct($currentUser = null, $currentPdo = null) {
  67. if ($currentUser === null) {
  68. $currentUser = Minz_Session::param('currentUser');
  69. }
  70. if ($currentPdo != null) {
  71. $this->pdo = $currentPdo;
  72. return;
  73. }
  74. if ($currentUser == '') {
  75. throw new Minz_PDOConnectionException('Current user must not be empty!', '', Minz_Exception::ERROR);
  76. }
  77. if (self::$usesSharedPdo && self::$sharedPdo != null &&
  78. ($currentUser == '' || $currentUser === self::$sharedCurrentUser)) {
  79. $this->pdo = self::$sharedPdo;
  80. $this->current_user = self::$sharedCurrentUser;
  81. return;
  82. }
  83. $this->current_user = $currentUser;
  84. self::$sharedCurrentUser = $currentUser;
  85. $ex = null;
  86. //Attempt a few times to connect to database
  87. for ($attempt = 1; $attempt <= 5; $attempt++) {
  88. try {
  89. $this->dbConnect();
  90. return;
  91. } catch (PDOException $e) {
  92. $ex = $e;
  93. if (empty($e->errorInfo[0]) || $e->errorInfo[0] !== '08006') {
  94. //We are only only interested in: SQLSTATE connection exception / connection failure
  95. break;
  96. }
  97. }
  98. sleep(2);
  99. }
  100. $db = Minz_Configuration::get('system')->db;
  101. throw new Minz_PDOConnectionException(
  102. $ex->getMessage(),
  103. $db['user'], Minz_Exception::ERROR
  104. );
  105. }
  106. public function beginTransaction() {
  107. $this->pdo->beginTransaction();
  108. }
  109. public function inTransaction() {
  110. return $this->pdo->inTransaction();
  111. }
  112. public function commit() {
  113. $this->pdo->commit();
  114. }
  115. public function rollBack() {
  116. $this->pdo->rollBack();
  117. }
  118. public static function clean() {
  119. self::$sharedPdo = null;
  120. self::$sharedCurrentUser = '';
  121. }
  122. }