ModelPdo.php 4.0 KB

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