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.
110 lines
2.8 KiB
110 lines
2.8 KiB
<?php
|
|
/**
|
|
* Класс базы данных.
|
|
* Возвращает идентификатор соединения
|
|
*
|
|
* @copyright
|
|
* @link
|
|
* @package Majestic
|
|
* @subpackage DB
|
|
* @since
|
|
* @version SVN: $Id$
|
|
* @filesource $URL$
|
|
*/
|
|
class DBConnector
|
|
{
|
|
static private $handlers = array();
|
|
static public $queries = array();
|
|
|
|
/**
|
|
* Запрещаем new и клонирование
|
|
*/
|
|
private function __construct(){}
|
|
|
|
private function __clone(){}
|
|
|
|
/**
|
|
* Получение соединения.
|
|
* Если соединение с такими параметрами уже есть - новое не создается.
|
|
*
|
|
* @param array $db_settings - массив настроек
|
|
* @return resource - идентификатор соединения
|
|
*/
|
|
static public function getConnect($db_settings)
|
|
{
|
|
$handler_name = self::getConnectionName($db_settings);
|
|
|
|
if (isset(self::$handlers[$handler_name])) {
|
|
return self::$handlers[$handler_name];
|
|
}
|
|
|
|
if (!$handler = pg_connect("host='".$db_settings['host']."' dbname='".$db_settings['database']."' user='".$db_settings['user']."' password='".$db_settings['password']."'")) {
|
|
throw new MJException('Can\'t connect to DB '.pg_last_error(), 2);
|
|
}
|
|
|
|
return self::$handlers[$handler_name] = $handler;
|
|
}
|
|
|
|
static protected function getConnectionName($db_settings)
|
|
{
|
|
return $db_settings['host'] . '-' . $db_settings['database'];
|
|
}
|
|
|
|
/////////
|
|
|
|
static public function query($handler, $sql)
|
|
{
|
|
return pg_query($handler, $sql);
|
|
}
|
|
|
|
static public function escape($handler, $str)
|
|
{
|
|
return pg_escape_string($str);
|
|
}
|
|
|
|
static public function error($handler)
|
|
{
|
|
return pg_last_error($handler);
|
|
}
|
|
|
|
static public function free($result)
|
|
{
|
|
return pg_free_result($result);
|
|
}
|
|
|
|
static public function fetchObject($result, $class_name = false)
|
|
{
|
|
return $class_name ? pg_fetch_object($result, null, $class_name) : pg_fetch_object($result);
|
|
}
|
|
|
|
static public function numRows($result)
|
|
{
|
|
return pg_num_rows($result);
|
|
}
|
|
|
|
static public function affectedRows($handler, $result)
|
|
{
|
|
return pg_affected_rows($result);
|
|
}
|
|
|
|
static public function getId($handler)
|
|
{
|
|
return pg_last_oid($handler);
|
|
}
|
|
|
|
static public function autocommit($handler, $switch)
|
|
{
|
|
throw new MJException('Autocommit disabled for postgreSQL Connector' ,1);
|
|
}
|
|
|
|
static public function commit($handler)
|
|
{
|
|
throw new MJException('Commit disabled for postgreSQL Connector' ,1);
|
|
}
|
|
|
|
static public function rollback($handler)
|
|
{
|
|
throw new MJException('Rollback disabled for postgreSQL Connector' ,1);
|
|
}
|
|
}
|
|
?>
|