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.

109 lines
2.8 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. static private $handlers = array();
  17. static public $queries = array();
  18. /**
  19. * Запрещаем new и клонирование
  20. */
  21. private function __construct(){}
  22. private function __clone(){}
  23. /**
  24. * Получение соединения.
  25. * Если соединение с такими параметрами уже есть - новое не создается.
  26. *
  27. * @param array $db_settings - массив настроек
  28. * @return resource - идентификатор соединения
  29. */
  30. static public function getConnect($db_settings)
  31. {
  32. $handler_name = self::getConnectionName($db_settings);
  33. if (isset(self::$handlers[$handler_name])) {
  34. return self::$handlers[$handler_name];
  35. }
  36. if (!$handler = pg_connect("host='".$db_settings['host']."' dbname='".$db_settings['database']."' user='".$db_settings['user']."' password='".$db_settings['password']."'")) {
  37. throw new MJException('Can\'t connect to DB '.pg_last_error(), 2);
  38. }
  39. return self::$handlers[$handler_name] = $handler;
  40. }
  41. static protected function getConnectionName($db_settings)
  42. {
  43. return $db_settings['host'] . '-' . $db_settings['database'];
  44. }
  45. /////////
  46. static public function query($handler, $sql)
  47. {
  48. return pg_query($handler, $sql);
  49. }
  50. static public function escape($handler, $str)
  51. {
  52. return pg_escape_string($str);
  53. }
  54. static public function error($handler)
  55. {
  56. return pg_last_error($handler);
  57. }
  58. static public function free($result)
  59. {
  60. return pg_free_result($result);
  61. }
  62. static public function fetchObject($result, $class_name = false)
  63. {
  64. return $class_name ? pg_fetch_object($result, null, $class_name) : pg_fetch_object($result);
  65. }
  66. static public function numRows($result)
  67. {
  68. return pg_num_rows($result);
  69. }
  70. static public function affectedRows($handler, $result)
  71. {
  72. return pg_affected_rows($result);
  73. }
  74. static public function getId($handler)
  75. {
  76. return -1; //DISABLED FORM postgreSQL
  77. }
  78. static public function autocommit($handler, $switch)
  79. {
  80. throw new MJException('Autocommit disabled for postgreSQL Connector' ,1);
  81. }
  82. static public function commit($handler)
  83. {
  84. throw new MJException('Commit disabled for postgreSQL Connector' ,1);
  85. }
  86. static public function rollback($handler)
  87. {
  88. throw new MJException('Rollback disabled for postgreSQL Connector' ,1);
  89. }
  90. }
  91. ?>