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.
111 lines
2.8 KiB
111 lines
2.8 KiB
<?php
|
|
/**
|
|
* Класс базы данных.
|
|
* Возвращает идентификатор соединения
|
|
*
|
|
* @copyright netmonsters.ru
|
|
* @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 = mysqli_connect($db_settings['host'], $db_settings['user'], $db_settings['password'], $db_settings['database'])) {
|
|
throw new MJException('Can\'t connect to DB '.mysqli_connect_error(), 2);
|
|
}
|
|
|
|
self::query($handler, "SET NAMES 'utf8'"); //cheat!!!
|
|
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 mysqli_query($handler, $sql);
|
|
}
|
|
|
|
static public function escape($handler, $str)
|
|
{
|
|
return mysqli_real_escape_string($handler, $str);
|
|
}
|
|
|
|
static public function error($handler)
|
|
{
|
|
return mysqli_error($handler);
|
|
}
|
|
|
|
static public function free($result)
|
|
{
|
|
return mysqli_free_result($result);
|
|
}
|
|
|
|
static public function fetchObject($result, $class_name = false)
|
|
{
|
|
return $class_name ? mysqli_fetch_object($result, $class_name) : mysqli_fetch_object($result);
|
|
}
|
|
|
|
static public function numRows($result)
|
|
{
|
|
return mysqli_num_rows($result);
|
|
}
|
|
|
|
static public function affectedRows($handler, $result)
|
|
{
|
|
return mysqli_affected_rows($handler);
|
|
}
|
|
|
|
static public function getId($handler)
|
|
{
|
|
return mysqli_insert_id($handler);
|
|
}
|
|
|
|
static public function autocommit($handler, $switch)
|
|
{
|
|
return mysqli_autocommit($handler, (bool) $switch);
|
|
}
|
|
|
|
static public function commit($handler)
|
|
{
|
|
return mysqli_commit($handler);
|
|
}
|
|
|
|
static public function rollback($handler)
|
|
{
|
|
return mysqli_rollback($handler);
|
|
}
|
|
}
|
|
?>
|