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

<?php
/**
* Класс базы данных.
* Возвращает идентификатор соединения
*
* @copyright
* @link
* @package Majestic
* @subpackage DB
* @since
* @version SVN: $Id$
* @filesource $URL$
*/
class DBConnector
{
/**
* PDO constant values.
*/
const FETCH_ASSOC = 2;
const FETCH_BOTH = 4;
const FETCH_NUM = 3;
const FETCH_OBJ = 5;
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);
}
/**
* Fetches a row from the result set.
*
* @param mysqli_result $result
* @param int $style OPTIONAL Fetch mode for this fetch operation.
* @return mixed Array, object, or scalar depending on fetch mode.
* @throws Exception
*/
static public function fetchArray($result, $style = null)
{
if (!$result) {
return false;
}
if ($style === null) {
$style = self::FETCH_ASSOC;
}
$row = false;
switch ($style) {
case self::FETCH_NUM:
$row = mysqli_fetch_array($result, MYSQLI_NUM);
break;
case self::FETCH_ASSOC:
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
break;
case self::FETCH_BOTH:
$row = mysqli_fetch_array($result, MYSQLI_BOTH);
break;
case self::FETCH_OBJ:
$row = mysqli_fetch_object($result, MYSQLI_BOTH);
break;
default:
throw new Exception('Invalid fetch mode "' . $style . '" specified');
break;
}
return $row;
}
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);
}
}
?>