ModelPdo.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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
  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:';
  46. if (empty($dbServer['host'])) {
  47. $dsn .= 'unix_socket=' . $db['host'];
  48. } else {
  49. $dsn .= 'host=' . $dbServer['host'];
  50. }
  51. $dsn .= ';charset=utf8mb4';
  52. if (!empty($db['base'])) {
  53. $dsn .= ';dbname=' . $db['base'];
  54. }
  55. if (!empty($dbServer['port'])) {
  56. $dsn .= ';port=' . $dbServer['port'];
  57. }
  58. $driver_options[PDO::MYSQL_ATTR_INIT_COMMAND] = 'SET NAMES utf8mb4';
  59. $this->pdo = new Minz_PdoMysql($dsn . $dsnParams, $db['user'], $db['password'], $driver_options);
  60. $this->pdo->setPrefix($db['prefix'] . $this->current_user . '_');
  61. break;
  62. case 'sqlite':
  63. $dsn = 'sqlite:' . join_path(DATA_PATH, 'users', $this->current_user, 'db.sqlite');
  64. $this->pdo = new Minz_PdoSqlite($dsn . $dsnParams, $db['user'], $db['password'], $driver_options);
  65. $this->pdo->setPrefix('');
  66. break;
  67. case 'pgsql':
  68. $dsn = 'pgsql:host=' . (empty($dbServer['host']) ? $db['host'] : $dbServer['host']);
  69. if (!empty($db['base'])) {
  70. $dsn .= ';dbname=' . $db['base'];
  71. }
  72. if (!empty($dbServer['port'])) {
  73. $dsn .= ';port=' . $dbServer['port'];
  74. }
  75. $this->pdo = new Minz_PdoPgsql($dsn . $dsnParams, $db['user'], $db['password'], $driver_options);
  76. $this->pdo->setPrefix($db['prefix'] . $this->current_user . '_');
  77. break;
  78. default:
  79. throw new Minz_PDOConnectionException(
  80. 'Invalid database type!',
  81. $db['user'], Minz_Exception::ERROR
  82. );
  83. }
  84. self::$sharedPdo = $this->pdo;
  85. }
  86. /**
  87. * Create the connection to the database using the variables
  88. * HOST, BASE, USER and PASS variables defined in the configuration file
  89. * @param string|null $currentUser
  90. * @param Minz_Pdo|null $currentPdo
  91. * @throws Minz_ConfigurationNamespaceException
  92. * @throws Minz_PDOConnectionException
  93. */
  94. public function __construct($currentUser = null, $currentPdo = null) {
  95. if ($currentUser === null) {
  96. $currentUser = Minz_User::name();
  97. }
  98. if ($currentPdo !== null) {
  99. $this->pdo = $currentPdo;
  100. return;
  101. }
  102. if ($currentUser == '') {
  103. throw new Minz_PDOConnectionException('Current user must not be empty!', '', Minz_Exception::ERROR);
  104. }
  105. if (self::$usesSharedPdo && self::$sharedPdo !== null && $currentUser === self::$sharedCurrentUser) {
  106. $this->pdo = self::$sharedPdo;
  107. $this->current_user = self::$sharedCurrentUser;
  108. return;
  109. }
  110. $this->current_user = $currentUser;
  111. self::$sharedCurrentUser = $currentUser;
  112. $ex = null;
  113. //Attempt a few times to connect to database
  114. for ($attempt = 1; $attempt <= 5; $attempt++) {
  115. try {
  116. $this->dbConnect();
  117. return;
  118. } catch (PDOException $e) {
  119. $ex = $e;
  120. if (empty($e->errorInfo[0]) || $e->errorInfo[0] !== '08006') {
  121. //We are only interested in: SQLSTATE connection exception / connection failure
  122. break;
  123. }
  124. } catch (Exception $e) {
  125. $ex = $e;
  126. }
  127. sleep(2);
  128. }
  129. $db = Minz_Configuration::get('system')->db;
  130. throw new Minz_PDOConnectionException(
  131. $ex->getMessage(),
  132. $db['user'], Minz_Exception::ERROR
  133. );
  134. }
  135. /**
  136. * @return void
  137. */
  138. public function beginTransaction() {
  139. $this->pdo->beginTransaction();
  140. }
  141. public function inTransaction(): bool {
  142. return $this->pdo->inTransaction();
  143. }
  144. /**
  145. * @return void
  146. */
  147. public function commit() {
  148. $this->pdo->commit();
  149. }
  150. /**
  151. * @return void
  152. */
  153. public function rollBack() {
  154. $this->pdo->rollBack();
  155. }
  156. /**
  157. * @return void
  158. */
  159. public static function clean() {
  160. self::$sharedPdo = null;
  161. self::$sharedCurrentUser = '';
  162. }
  163. }