ModelPdo.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. /**
  20. * Créé la connexion à la base de données à l'aide des variables
  21. * HOST, BASE, USER et PASS définies dans le fichier de configuration
  22. */
  23. public function __construct($currentUser = null, $currentPdo = null) {
  24. if ($currentUser === null) {
  25. $currentUser = Minz_Session::param('currentUser');
  26. }
  27. if ($currentPdo != null) {
  28. $this->pdo = $currentPdo;
  29. return;
  30. }
  31. if ($currentUser == '') {
  32. throw new Minz_PDOConnectionException('Current user must not be empty!', '', Minz_Exception::ERROR);
  33. }
  34. if (self::$usesSharedPdo && self::$sharedPdo != null &&
  35. ($currentUser == '' || $currentUser === self::$sharedCurrentUser)) {
  36. $this->pdo = self::$sharedPdo;
  37. $this->current_user = self::$sharedCurrentUser;
  38. return;
  39. }
  40. $this->current_user = $currentUser;
  41. self::$sharedCurrentUser = $currentUser;
  42. $conf = Minz_Configuration::get('system');
  43. $db = $conf->db;
  44. $driver_options = isset($db['pdo_options']) && is_array($db['pdo_options']) ? $db['pdo_options'] : [];
  45. $dbServer = parse_url('db://' . $db['host']);
  46. $dsn = '';
  47. $dsnParams = empty($db['connection_uri_params']) ? '' : (';' . $db['connection_uri_params']);
  48. try {
  49. switch ($db['type']) {
  50. case 'mysql':
  51. $dsn = 'mysql:host=' . (empty($dbServer['host']) ? $db['host'] : $dbServer['host']) . ';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'] . $currentUser . '_');
  61. break;
  62. case 'sqlite':
  63. $dsn = 'sqlite:' . join_path(DATA_PATH, 'users', $currentUser, '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'] . $currentUser . '_');
  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. } catch (Exception $e) {
  86. throw new Minz_PDOConnectionException(
  87. $e->getMessage(),
  88. $db['user'], Minz_Exception::ERROR
  89. );
  90. }
  91. }
  92. public function beginTransaction() {
  93. $this->pdo->beginTransaction();
  94. }
  95. public function inTransaction() {
  96. return $this->pdo->inTransaction();
  97. }
  98. public function commit() {
  99. $this->pdo->commit();
  100. }
  101. public function rollBack() {
  102. $this->pdo->rollBack();
  103. }
  104. public static function clean() {
  105. self::$sharedPdo = null;
  106. self::$sharedCurrentUser = '';
  107. }
  108. }