ModelPdo.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. /**
  3. * MINZ - Copyright 2011 Marien Fressinaud
  4. * Sous licence AGPL3 <http://www.gnu.org/licenses/>
  5. */
  6. /**
  7. * The Model_sql class represents the model for interacting with databases.
  8. */
  9. class Minz_ModelPdo {
  10. /**
  11. * Shares the connection to the database between all instances.
  12. */
  13. public static $usesSharedPdo = true;
  14. /**
  15. * @var Minz_Pdo|null
  16. */
  17. private static $sharedPdo;
  18. /**
  19. * @var string|null
  20. */
  21. private static $sharedCurrentUser;
  22. /**
  23. * @var Minz_Pdo|null
  24. */
  25. protected $pdo;
  26. /**
  27. * @var string|null
  28. */
  29. protected $current_user;
  30. /**
  31. * @return void
  32. * @throws Minz_ConfigurationNamespaceException
  33. * @throws Minz_PDOConnectionException
  34. * @throws PDOException
  35. */
  36. private function dbConnect() {
  37. $db = Minz_Configuration::get('system')->db;
  38. $driver_options = isset($db['pdo_options']) && is_array($db['pdo_options']) ? $db['pdo_options'] : [];
  39. $driver_options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_SILENT;
  40. $dbServer = parse_url('db://' . $db['host']);
  41. $dsn = '';
  42. $dsnParams = empty($db['connection_uri_params']) ? '' : (';' . $db['connection_uri_params']);
  43. switch ($db['type']) {
  44. case 'mysql':
  45. $dsn = 'mysql:host=' . (empty($dbServer['host']) ? $db['host'] : $dbServer['host']) . ';charset=utf8mb4';
  46. if (!empty($db['base'])) {
  47. $dsn .= ';dbname=' . $db['base'];
  48. }
  49. if (!empty($dbServer['port'])) {
  50. $dsn .= ';port=' . $dbServer['port'];
  51. }
  52. $driver_options[PDO::MYSQL_ATTR_INIT_COMMAND] = 'SET NAMES utf8mb4';
  53. $this->pdo = new Minz_PdoMysql($dsn . $dsnParams, $db['user'], $db['password'], $driver_options);
  54. $this->pdo->setPrefix($db['prefix'] . $this->current_user . '_');
  55. break;
  56. case 'sqlite':
  57. $dsn = 'sqlite:' . join_path(DATA_PATH, 'users', $this->current_user, 'db.sqlite');
  58. $this->pdo = new Minz_PdoSqlite($dsn . $dsnParams, $db['user'], $db['password'], $driver_options);
  59. $this->pdo->setPrefix('');
  60. break;
  61. case 'pgsql':
  62. $dsn = 'pgsql:host=' . (empty($dbServer['host']) ? $db['host'] : $dbServer['host']);
  63. if (!empty($db['base'])) {
  64. $dsn .= ';dbname=' . $db['base'];
  65. }
  66. if (!empty($dbServer['port'])) {
  67. $dsn .= ';port=' . $dbServer['port'];
  68. }
  69. $this->pdo = new Minz_PdoPgsql($dsn . $dsnParams, $db['user'], $db['password'], $driver_options);
  70. $this->pdo->setPrefix($db['prefix'] . $this->current_user . '_');
  71. break;
  72. default:
  73. throw new Minz_PDOConnectionException(
  74. 'Invalid database type!',
  75. $db['user'], Minz_Exception::ERROR
  76. );
  77. }
  78. self::$sharedPdo = $this->pdo;
  79. }
  80. /**
  81. * Create the connection to the database using the variables
  82. * HOST, BASE, USER and PASS variables defined in the configuration file
  83. * @param string|null $currentUser
  84. * @param Minz_Pdo|null $currentPdo
  85. * @throws Minz_ConfigurationNamespaceException
  86. * @throws Minz_PDOConnectionException
  87. */
  88. public function __construct($currentUser = null, $currentPdo = null) {
  89. if ($currentUser === null) {
  90. $currentUser = Minz_Session::param('currentUser');
  91. }
  92. if ($currentPdo !== null) {
  93. $this->pdo = $currentPdo;
  94. return;
  95. }
  96. if ($currentUser == '') {
  97. throw new Minz_PDOConnectionException('Current user must not be empty!', '', Minz_Exception::ERROR);
  98. }
  99. if (self::$usesSharedPdo && self::$sharedPdo !== null && $currentUser === self::$sharedCurrentUser) {
  100. $this->pdo = self::$sharedPdo;
  101. $this->current_user = self::$sharedCurrentUser;
  102. return;
  103. }
  104. $this->current_user = $currentUser;
  105. self::$sharedCurrentUser = $currentUser;
  106. $ex = null;
  107. //Attempt a few times to connect to database
  108. for ($attempt = 1; $attempt <= 5; $attempt++) {
  109. try {
  110. $this->dbConnect();
  111. return;
  112. } catch (PDOException $e) {
  113. $ex = $e;
  114. if (empty($e->errorInfo[0]) || $e->errorInfo[0] !== '08006') {
  115. //We are only interested in: SQLSTATE connection exception / connection failure
  116. break;
  117. }
  118. } catch (Exception $e) {
  119. $ex = $e;
  120. }
  121. sleep(2);
  122. }
  123. $db = Minz_Configuration::get('system')->db;
  124. throw new Minz_PDOConnectionException(
  125. $ex->getMessage(),
  126. $db['user'], Minz_Exception::ERROR
  127. );
  128. }
  129. /**
  130. * @return void
  131. */
  132. public function beginTransaction() {
  133. $this->pdo->beginTransaction();
  134. }
  135. public function inTransaction(): bool {
  136. return $this->pdo->inTransaction();
  137. }
  138. /**
  139. * @return void
  140. */
  141. public function commit() {
  142. $this->pdo->commit();
  143. }
  144. /**
  145. * @return void
  146. */
  147. public function rollBack() {
  148. $this->pdo->rollBack();
  149. }
  150. /**
  151. * @return void
  152. */
  153. public static function clean() {
  154. self::$sharedPdo = null;
  155. self::$sharedCurrentUser = '';
  156. }
  157. }