You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

159 lines
4.1 KiB

  1. <?php
  2. /**
  3. * Класс базы данных.
  4. * Возвращает идентификатор соединения
  5. *
  6. * @copyright NetMonsters <team@netmonsters.ru>
  7. * @link
  8. * @package Majestic
  9. * @subpackage DB
  10. * @since
  11. * @version SVN: $Id$
  12. * @filesource $URL$
  13. */
  14. class DBConnector
  15. {
  16. /**
  17. * PDO constant values.
  18. */
  19. const FETCH_ASSOC = 2;
  20. const FETCH_BOTH = 4;
  21. const FETCH_NUM = 3;
  22. const FETCH_OBJ = 5;
  23. static private $handlers = array();
  24. static public $queries = array();
  25. /**
  26. * Запрещаем new и клонирование
  27. */
  28. private function __construct(){}
  29. private function __clone(){}
  30. /**
  31. * Получение соединения.
  32. * Если соединение с такими параметрами уже есть - новое не создается.
  33. *
  34. * @param array $db_settings - массив настроек
  35. * @return resource - идентификатор соединения
  36. */
  37. static public function getConnect($db_settings)
  38. {
  39. $handler_name = self::getConnectionName($db_settings);
  40. if (isset(self::$handlers[$handler_name])) {
  41. return self::$handlers[$handler_name];
  42. }
  43. if (!$handler = mysqli_connect($db_settings['host'], $db_settings['user'], $db_settings['password'], $db_settings['database'])) {
  44. throw new MJException('Can\'t connect to DB '.mysqli_connect_error(), 2);
  45. }
  46. self::query($handler, "SET NAMES 'utf8'"); //cheat!!!
  47. return self::$handlers[$handler_name] = $handler;
  48. }
  49. static protected function getConnectionName($db_settings)
  50. {
  51. return $db_settings['host'] . '-' . $db_settings['database'];
  52. }
  53. /////////
  54. static public function query($handler, $sql)
  55. {
  56. return mysqli_query($handler, $sql);
  57. }
  58. static public function escape($handler, $str)
  59. {
  60. return mysqli_real_escape_string($handler, $str);
  61. }
  62. static public function error($handler)
  63. {
  64. return mysqli_error($handler);
  65. }
  66. static public function free($result)
  67. {
  68. return mysqli_free_result($result);
  69. }
  70. static public function fetchObject($result, $class_name = false)
  71. {
  72. return $class_name ? mysqli_fetch_object($result, $class_name) : mysqli_fetch_object($result);
  73. }
  74. /**
  75. * Fetches a row from the result set.
  76. *
  77. * @param mysqli_result $result
  78. * @param int $style OPTIONAL Fetch mode for this fetch operation.
  79. * @return mixed Array, object, or scalar depending on fetch mode.
  80. * @throws Exception
  81. */
  82. static public function fetchArray($result, $style = null)
  83. {
  84. if (!$result) {
  85. return false;
  86. }
  87. if ($style === null) {
  88. $style = self::FETCH_ASSOC;
  89. }
  90. $row = false;
  91. switch ($style) {
  92. case self::FETCH_NUM:
  93. $row = mysqli_fetch_array($result, MYSQLI_NUM);
  94. break;
  95. case self::FETCH_ASSOC:
  96. $row = mysqli_fetch_array($result, MYSQLI_ASSOC);
  97. break;
  98. case self::FETCH_BOTH:
  99. $row = mysqli_fetch_array($result, MYSQLI_BOTH);
  100. break;
  101. case self::FETCH_OBJ:
  102. $row = mysqli_fetch_object($result, MYSQLI_BOTH);
  103. break;
  104. default:
  105. throw new Exception('Invalid fetch mode "' . $style . '" specified');
  106. break;
  107. }
  108. return $row;
  109. }
  110. static public function numRows($result)
  111. {
  112. return mysqli_num_rows($result);
  113. }
  114. static public function affectedRows($handler, $result)
  115. {
  116. return mysqli_affected_rows($handler);
  117. }
  118. static public function getId($handler)
  119. {
  120. return mysqli_insert_id($handler);
  121. }
  122. static public function autocommit($handler, $switch)
  123. {
  124. return mysqli_autocommit($handler, (bool) $switch);
  125. }
  126. static public function commit($handler)
  127. {
  128. return mysqli_commit($handler);
  129. }
  130. static public function rollback($handler)
  131. {
  132. return mysqli_rollback($handler);
  133. }
  134. }
  135. ?>